]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: report tool errors in go list all
authorConrad Irwin <conrad.irwin@gmail.com>
Fri, 6 Dec 2024 05:25:14 +0000 (22:25 -0700)
committerMichael Matloob <matloob@golang.org>
Fri, 6 Dec 2024 19:33:02 +0000 (19:33 +0000)
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
src/cmd/go/internal/modload/load.go
src/cmd/go/testdata/script/mod_tool_70582.txt [new file with mode: 0644]

index e8f8e7fa5cd61a6adf0ab167d566deee7c976663..12dd9425f6596265c6d012a4443cf36f2d8e4c5c 100644 (file)
@@ -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 (file)
index 0000000..7e28317
--- /dev/null
@@ -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