Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Go refuses a module version lookup because a vendor folder exists

Your error

go: cannot query module due to -mod=vendor1 more line it can show

Fix it fast

Most common cause, 1 of 2

A go list -m query with @version runs inside a vendored project

Run the lookup with -mod=mod, which tells the go command to ignore the vendor directory for that one call.

Fix
go list -mod=mod -m example.com/some/module@latest
go list -mod=mod -m -u all

# for a script or tool that runs go list itself
GOFLAGS=-mod=mod ./scripts/check-updates.sh

# after changing a version
go mod tidy
go mod vendor
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

go: cannot query module due to -mod=vendor appears when go.mod says go 1.14 or later and a vendor directory exists. Go then defaults to -mod=vendor and refuses version queries such as go list -m example.com/m@latest. Add -mod=mod to that command, set GOFLAGS=-mod=mod for the tool, or delete an unwanted vendor folder. Applies to Go 1.26-1.27.

  • Applies to Go 1.27, Go 1.26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Check whether the vendor folder is part of the repository or just lying around. Tracked files mean the project vendors on purpose; an empty result means the folder is a leftover.

Run this first
git ls-files vendor | head -5

Then find the line you see in this list and open its fix:

  1. 1A go list -m query with @version runs inside a vendored projectvendor/ is committed, and the failing command names a module with @latest, @v1.2.3 or another version query, typed by you or run by an update script
  2. 2An old or copied vendor folder switches vendoring on by accidentgit ls-files vendor prints nothing, or the team does not vendor dependencies and nobody remembers creating the folder

What does the error look like?

  • go: cannot query module due to -mod=vendor
  • (Go version in go.mod is at least 1.14 and vendor directory exists.)

Cause 1 of 2

A go list -m query with @version runs inside a vendored project

You seevendor/ is committed, and the failing command names a module with @latest, @v1.2.3 or another version query, typed by you or run by an update script

Check

cannot query module due to -mod=vendor is Go protecting the vendor folder. With go 1.14 or higher in go.mod and a vendor directory present, the go command behaves as if -mod=vendor were passed, and in that mode it may not use the network or the module cache. Resolving @latest or any other version query needs both, so the query is refused. The related flags -u and -versions stop even earlier with a different message, can't determine available upgrades using the vendor directory, and take the same fix.

Check
go list -m example.com/some/module@latest

Fix

Run the lookup with -mod=mod, which tells the go command to ignore the vendor directory for that one call. For a tool you cannot pass flags to, put the flag in GOFLAGS for that tool only. Re-vendor after any version change so vendor/modules.txt matches go.mod.

Fix
go list -mod=mod -m example.com/some/module@latest
go list -mod=mod -m -u all

# for a script or tool that runs go list itself
GOFLAGS=-mod=mod ./scripts/check-updates.sh

# after changing a version
go mod tidy
go mod vendor

Verify

The query prints the module path and the version it resolved to, and go build still works from the vendor folder.

Verify
go build ./... && go list -mod=mod -m example.com/some/module@latest

Confirmed by go.dev

Did this fix it?

Cause 2 of 2

An old or copied vendor folder switches vendoring on by accident

You seegit ls-files vendor prints nothing, or the team does not vendor dependencies and nobody remembers creating the folder

Check

Go never asks whether you meant to vendor. If a directory named vendor sits in the main module's root and go.mod declares 1.14 or later, the go command starts using it automatically. A folder left by an old build script, an archive or a copy from another project turns on vendor mode and blocks version lookups.

Check
ls vendor/modules.txt && git status --short vendor | head

Fix

Remove the folder and let the module cache serve dependencies again. Run go mod tidy afterwards so go.mod and go.sum are complete without it.

Fix
rm -rf vendor
go mod tidy

Verify

The version lookup now works without extra flags, and builds read from the module cache.

Verify
go list -m example.com/some/module@latest

Confirmed by go.dev

Did this fix it?

How do I stop it coming back?

  • Decide once whether the repository vendors. If it does, commit vendor/ and add a CI step that runs go mod vendor and fails on a diff.
  • Where the team does not vendor, let CI reject any commit that brings a vendor folder back, for example with test ! -d vendor.
  • In update scripts, write -mod=mod into every go list -m call that uses @version, -u or -versions, instead of relying on each developer's environment.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.