Verified Solution[StackOverflow/go] Getting "Cannot query module due to -mod=vendor(Go version in go.mod is at least 1.14 and vendor directory exists.)" error when executing unit test
Sponsored Content
### ROOT CAUSE
The error occurs because the Go tool is operating in `-mod=vendor` mode, which requires all dependencies to be present in the vendor directory. If the vendor directory is not up-to-date or the required module is missing, the tool cannot proceed. This is common when running tests in environments where `-mod=vendor` is enforced, such as some CI setups or local development configurations.
### CODE FIX
1. **Update the vendor directory**:
```bash
go mod vendor
```
This ensures all dependencies are copied to the `vendor` directory.
2. **Remove the `-mod=vendor` flag** from your test command or environment (if not necessary):
```bash
go test ./... # without -mod=vendor
```
Or unset the environment variable if it was set:
```bash
unset GOFLAGS # or remove -mod=vendor from your shell's GOFLAGS
```
3. **If using a CI pipeline**, ensure the `vendor` directory is explicitly copied or the `-mod=vendor` flag is handled correctly in the build steps.
This resolves the issue by either keeping the vendor directory updated or switching to a different module resolution mode.
Deploy on DigitalOcean ($200 Credit)
Related Fixes
[golang/go] crypto/rsa:gofips140: TestEverything/525 failures
[golang/go] crypto/rsa:gofips140-v1.0.0: TestEverything/241 failures
[StackOverflow/kubernetes] Reconciliation from DB -> Kubernetes or DB as a caching layer