diff options
| author | yongqijia <YongqiJia520@gmail.com> | 2026-02-12 18:50:43 +0800 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-06 12:09:35 -0800 |
| commit | 07e487bc5ceb05d5fdbbec3c66f75a8a36460038 (patch) | |
| tree | 732ff2cd35ed5d98b9cfe2da65702bab3f3a4475 /src/cmd | |
| parent | e36ae89c186a5220075ef3ee0b7e2a41470fa6dc (diff) | |
| download | go-07e487bc5ceb05d5fdbbec3c66f75a8a36460038.tar.xz | |
cmd/go: exclude vendor directories in go work use -r
When using "go work use -r" to recursively add modules, vendor
directories are incorrectly included. Modules under vendor/ are
managed by "go mod vendor" and should not be added to go.work.
The WalkDir callback in workUse did not skip directories named
"vendor", unlike other recursive walkers in the codebase (e.g.
modload/search.go, doc/dirs.go) which all use SkipDir on
d.Name() == "vendor".
Add the same vendor skip to the WalkDir callback. This uses
d.Name() == "vendor" rather than path prefix matching, consistent
with Bryan Mills's review feedback on the earlier CL 393814 which
was abandoned for using the wrong approach.
Fixes #51710
Change-Id: I4cdd46a8566e9461bf1f5f9e60099f0585e3fae3
Reviewed-on: https://go-review.googlesource.com/c/go/+/744900
Reviewed-by: Michael Matloob <matloob@golang.org>
Auto-Submit: Sean Liao <sean@liao.dev>
Reviewed-by: Michael Matloob <matloob@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Sean Liao <sean@liao.dev>
Diffstat (limited to 'src/cmd')
| -rw-r--r-- | src/cmd/go/internal/workcmd/use.go | 3 | ||||
| -rw-r--r-- | src/cmd/go/testdata/script/work_use_vendor.txt | 126 |
2 files changed, 129 insertions, 0 deletions
diff --git a/src/cmd/go/internal/workcmd/use.go b/src/cmd/go/internal/workcmd/use.go index 041aa069e2..cf817e0fcc 100644 --- a/src/cmd/go/internal/workcmd/use.go +++ b/src/cmd/go/internal/workcmd/use.go @@ -163,6 +163,9 @@ func workUse(ctx context.Context, s *modload.State, gowork string, wf *modfile.W } return nil } + if d.Name() == "vendor" { + return filepath.SkipDir + } lookDir(path) return nil }) diff --git a/src/cmd/go/testdata/script/work_use_vendor.txt b/src/cmd/go/testdata/script/work_use_vendor.txt new file mode 100644 index 0000000000..9a61db0ddf --- /dev/null +++ b/src/cmd/go/testdata/script/work_use_vendor.txt @@ -0,0 +1,126 @@ +# Test that 'go work use -r' skips vendor directories. +# See https://go.dev/issue/51710. + +# Scenario 1: Basic recursive use should skip all vendor directories. +# - Top-level vendor/ should be excluded. +# - Nested vendor/ under submodules (sub/vendor/) should be excluded. +# - Deeply nested vendor/a/vendor/ should be excluded (already unreachable via SkipDir). +# - vendor/ under a non-module directory (subdir/vendor/) should also be excluded, +# because d.Name() == "vendor" skips all directories named "vendor" regardless +# of whether the parent is a module. +# - A directory named "notvendor" or "myvendor" should NOT be excluded. +go work use -r . +cmp go.work go.work.want_recursive + +# Scenario 2: Non-recursive 'go work use' on a directory named "vendor" +# should still work — the vendor skip only applies to -r walks. +go work use vendor/example.com/dep +cmp go.work go.work.want_explicit_vendor + +# Scenario 3: Re-running recursive use from clean state should produce +# consistent results. +cp go.work.clean go.work +go work use -r . +cmp go.work go.work.want_recursive + +-- go.work -- +go 1.24 + +-- go.work.clean -- +go 1.24 + +-- go.work.want_recursive -- +go 1.24 + +use ( + . + ./myvendor + ./notvendor + ./sub + ./subdir +) +-- go.work.want_explicit_vendor -- +go 1.24 + +use ( + . + ./myvendor + ./notvendor + ./sub + ./subdir + ./vendor/example.com/dep +) +-- go.mod -- +module example.com/main + +go 1.24 + +-- main.go -- +package main + +-- sub/go.mod -- +module example.com/sub + +go 1.24 + +-- sub/main.go -- +package sub + +-- subdir/go.mod -- +module example.com/subdir + +go 1.24 + +-- subdir/main.go -- +package subdir + +# subdir/vendor/ contains a module, but it is skipped by 'go work use -r' +# because all directories named "vendor" are skipped, not just those at +# the root of a module with an existing vendor directory. +-- subdir/vendor/example.com/vendored/go.mod -- +module example.com/vendored + +go 1.24 + +-- subdir/vendor/example.com/vendored/vendored.go -- +package vendored + +-- vendor/example.com/dep/go.mod -- +module example.com/dep + +go 1.24 + +-- vendor/example.com/dep/dep.go -- +package dep + +-- sub/vendor/example.com/subdep/go.mod -- +module example.com/subdep + +go 1.24 + +-- sub/vendor/example.com/subdep/subdep.go -- +package subdep + +-- vendor/example.com/dep/vendor/example.com/transitive/go.mod -- +module example.com/transitive + +go 1.24 + +-- vendor/example.com/dep/vendor/example.com/transitive/transitive.go -- +package transitive + +-- notvendor/go.mod -- +module example.com/notvendor + +go 1.24 + +-- notvendor/main.go -- +package notvendor + +-- myvendor/go.mod -- +module example.com/myvendor + +go 1.24 + +-- myvendor/main.go -- +package myvendor |
