Skip to content

Commit 76d2f6c

Browse files
author
Bryan C. Mills
committed
cmd/go: include cfg.BuildModReason in 'import lookup disabled' errors
This location was missed in CL 204521. Updates #33326 Updates #33848 Change-Id: I0ece6d9b37548d8abb54f79c69be5548a0428c76 Reviewed-on: https://go-review.googlesource.com/c/go/+/210341 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent df0ac45 commit 76d2f6c

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

src/cmd/go/internal/modload/import.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,14 @@ func Import(path string) (m module.Version, dir string, err error) {
183183
// Look up module containing the package, for addition to the build list.
184184
// Goal is to determine the module, download it to dir, and return m, dir, ErrMissing.
185185
if cfg.BuildMod == "readonly" {
186-
if pathIsStd {
187-
// 'import lookup disabled' would be confusing for standard-library paths,
188-
// since the user probably isn't expecting us to look up a module for
189-
// those anyway.
190-
return module.Version{}, "", &ImportMissingError{Path: path}
186+
var queryErr error
187+
if !pathIsStd {
188+
if cfg.BuildModReason == "" {
189+
queryErr = fmt.Errorf("import lookup disabled by -mod=%s", cfg.BuildMod)
190+
}
191+
queryErr = fmt.Errorf("import lookup disabled by -mod=%s\n\t(%s)", cfg.BuildMod, cfg.BuildModReason)
191192
}
192-
return module.Version{}, "", fmt.Errorf("import lookup disabled by -mod=%s", cfg.BuildMod)
193+
return module.Version{}, "", &ImportMissingError{Path: path, QueryErr: queryErr}
193194
}
194195
if modRoot == "" && !allowMissingModuleImports {
195196
return module.Version{}, "", &ImportMissingError{

src/cmd/go/testdata/script/mod_list_e_readonly.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# to individual missing packages.
33
# Verifies golang.org/issue/34829.
44
go list -mod=readonly -e -deps -f '{{if .Error}}{{.ImportPath}}: {{.Error}}{{end}}' .
5-
stdout 'example.com/missing: use.go:3:8: import lookup disabled by -mod=readonly'
5+
stdout 'example.com/missing: use.go:3:8: cannot find module providing package example.com/missing: import lookup disabled by -mod=readonly'
66

77
-- go.mod --
88
module example.com/m

src/cmd/go/testdata/script/mod_readonly.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ env GOFLAGS=-mod=readonly
66
go mod edit -fmt
77
cp go.mod go.mod.empty
88
! go list all
9-
stderr 'import lookup disabled by -mod=readonly'
9+
stderr '^can''t load package: x.go:2:8: cannot find module providing package rsc\.io/quote: import lookup disabled by -mod=readonly'
1010
cmp go.mod go.mod.empty
1111

1212
# -mod=readonly should be set implicitly if the go.mod file is read-only
1313
chmod 0400 go.mod
1414
env GOFLAGS=
1515
! go list all
16+
stderr '^can''t load package: x.go:2:8: cannot find module providing package rsc\.io/quote: import lookup disabled by -mod=readonly\n\t\(go.mod file is read-only\.\)$'
1617

1718
chmod 0600 go.mod
1819
env GOFLAGS=-mod=readonly

0 commit comments

Comments
 (0)