From 2440717918434aee8450757c160cea5280d0bbe3 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 5 Dec 2024 22:25:14 -0700 Subject: [PATCH] cmd/go: report tool errors in go list all Before tools there was no way to directly import a package in another module, and so missing packages were always marked as "all" due to being dependencies of a package in a main module. Tools break that assumption, and so to report errors in tool packages correctly we need to mark packages as being in "all" even if they do not exist. Fixes #70582 Change-Id: I3273e0ec7910894565206de77986f5c249a5658c Reviewed-on: https://go-review.googlesource.com/c/go/+/634155 LUCI-TryBot-Result: Go LUCI Reviewed-by: Sam Thanawalla Reviewed-by: Michael Matloob --- src/cmd/go/internal/modload/load.go | 9 ++++++--- src/cmd/go/testdata/script/mod_tool_70582.txt | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 src/cmd/go/testdata/script/mod_tool_70582.txt diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go index e8f8e7fa5c..12dd9425f6 100644 --- a/src/cmd/go/internal/modload/load.go +++ b/src/cmd/go/internal/modload/load.go @@ -1852,6 +1852,12 @@ func (ld *loader) load(ctx context.Context, pkg *loadPkg) { var modroot string pkg.mod, modroot, pkg.dir, pkg.altMods, pkg.err = importFromModules(ctx, pkg.path, ld.requirements, mg, ld.skipImportModFiles) + if MainModules.Tools()[pkg.path] { + // Tools declared by main modules are always in "all". + // We apply the package flags before returning so that missing + // tool dependencies report an error https://go.dev/issue/70582 + ld.applyPkgFlags(ctx, pkg, pkgInAll) + } if pkg.dir == "" { return } @@ -1866,9 +1872,6 @@ func (ld *loader) load(ctx context.Context, pkg *loadPkg) { // essentially nothing (these atomic flag ops are essentially free compared // to scanning source code for imports). ld.applyPkgFlags(ctx, pkg, pkgInAll) - } else if MainModules.Tools()[pkg.path] { - // Tools declared by main modules are always in "all". - ld.applyPkgFlags(ctx, pkg, pkgInAll) } if ld.AllowPackage != nil { if err := ld.AllowPackage(ctx, pkg.path, pkg.mod); err != nil { diff --git a/src/cmd/go/testdata/script/mod_tool_70582.txt b/src/cmd/go/testdata/script/mod_tool_70582.txt new file mode 100644 index 0000000000..7e2831783a --- /dev/null +++ b/src/cmd/go/testdata/script/mod_tool_70582.txt @@ -0,0 +1,9 @@ +! go list all +stderr 'no required module provides package example.com/tools/cmd/hello' + +-- go.mod -- +go 1.24 + +module example.com/foo + +tool example.com/tools/cmd/hello -- 2.48.1