Skip to content

Commit

Permalink
cmd/go: report tool errors in go list all
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ConradIrwin authored and matloob committed Dec 6, 2024
1 parent 1a193b4 commit 2440717
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/cmd/go/internal/modload/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions src/cmd/go/testdata/script/mod_tool_70582.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2440717

Please sign in to comment.