Skip to content

Commit 5764653

Browse files
author
Bryan C. Mills
committed
cmd/api: omit outside dependencies when listing the packages in "std"
As of CL 251159, when 'go list -deps std' is run within GOROOT/src, it treats the vendored external dependencies as real module dependencies, not standard-library "vendor/" packages (which still exist in that case, but are treated as distinct packages outside the "std" module). Fixes #41358 Updates #30241 Change-Id: Ic23eae9829d90e74a340d49ca9052e9191597410 Reviewed-on: https://go-review.googlesource.com/c/go/+/254738 Run-TryBot: Bryan C. Mills <[email protected]> Trust: Bryan C. Mills <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 14c7caa commit 5764653

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/cmd/api/goapi.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ var contexts = []*build.Context{
8787
func contextName(c *build.Context) string {
8888
s := c.GOOS + "-" + c.GOARCH
8989
if c.CgoEnabled {
90-
return s + "-cgo"
90+
s += "-cgo"
91+
}
92+
if c.Dir != "" {
93+
s += fmt.Sprintf(" [%s]", c.Dir)
9194
}
9295
return s
9396
}
@@ -478,6 +481,9 @@ func (w *Walker) loadImports() {
478481

479482
cmd := exec.Command(goCmd(), "list", "-e", "-deps", "-json", "std")
480483
cmd.Env = listEnv(w.context)
484+
if w.context.Dir != "" {
485+
cmd.Dir = w.context.Dir
486+
}
481487
out, err := cmd.CombinedOutput()
482488
if err != nil {
483489
log.Fatalf("loading imports: %v\n%s", err, out)
@@ -491,6 +497,7 @@ func (w *Walker) loadImports() {
491497
var pkg struct {
492498
ImportPath, Dir string
493499
ImportMap map[string]string
500+
Standard bool
494501
}
495502
err := dec.Decode(&pkg)
496503
if err == io.EOF {
@@ -503,11 +510,13 @@ func (w *Walker) loadImports() {
503510
// - Package "unsafe" contains special signatures requiring
504511
// extra care when printing them - ignore since it is not
505512
// going to change w/o a language change.
506-
// - internal and vendored packages do not contribute to our
507-
// API surface.
513+
// - Internal and vendored packages do not contribute to our
514+
// API surface. (If we are running within the "std" module,
515+
// vendored dependencies appear as themselves instead of
516+
// their "vendor/" standard-library copies.)
508517
// - 'go list std' does not include commands, which cannot be
509518
// imported anyway.
510-
if ip := pkg.ImportPath; ip != "unsafe" && !strings.HasPrefix(ip, "vendor/") && !internalPkg.MatchString(ip) {
519+
if ip := pkg.ImportPath; pkg.Standard && ip != "unsafe" && !strings.HasPrefix(ip, "vendor/") && !internalPkg.MatchString(ip) {
511520
stdPackages = append(stdPackages, ip)
512521
}
513522
importDir[pkg.ImportPath] = pkg.Dir

src/cmd/api/goapi_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,16 @@ func TestIssue29837(t *testing.T) {
216216
}
217217
}
218218
}
219+
220+
func TestIssue41358(t *testing.T) {
221+
context := new(build.Context)
222+
*context = build.Default
223+
context.Dir = filepath.Join(context.GOROOT, "src")
224+
225+
w := NewWalker(context, context.Dir)
226+
for _, pkg := range w.stdPackages {
227+
if strings.HasPrefix(pkg, "vendor/") || strings.HasPrefix(pkg, "golang.org/x/") {
228+
t.Fatalf("stdPackages contains unexpected package %s", pkg)
229+
}
230+
}
231+
}

src/cmd/go/internal/modload/load.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,12 @@ func (ld *loader) stdVendor(parentPath, path string) string {
11061106
// Do the same for importers beginning with the prefix 'vendor/' even if we
11071107
// are *inside* of the 'std' module: the 'vendor/' packages that resolve
11081108
// globally from GOROOT/src/vendor (and are listed as part of 'go list std')
1109-
// are distinct from the real module dependencies, and cannot import internal
1110-
// packages from the real module.
1109+
// are distinct from the real module dependencies, and cannot import
1110+
// internal packages from the real module.
1111+
//
1112+
// (Note that although the 'vendor/' packages match the 'std' *package*
1113+
// pattern, they are not part of the std *module*, and do not affect
1114+
// 'go mod tidy' and similar module commands when working within std.)
11111115
vendorPath := pathpkg.Join("vendor", path)
11121116
if _, err := os.Stat(filepath.Join(cfg.GOROOTsrc, filepath.FromSlash(vendorPath))); err == nil {
11131117
return vendorPath

0 commit comments

Comments
 (0)