Skip to content

Commit 9e914f5

Browse files
a8mBryan C. Mills
authored and
Bryan C. Mills
committed
cmd/go/internal/modload: fail if no package was found in local module
Changing the Import function to return a PackageNotInModuleError if no package was found in a local module. This replacing the vague message "missing dot in first path element" you get today with much more friendly one - "module was found, but does not contain package". Fixes #35273 Change-Id: I6d726c17e6412258274b10f58f76621617d26e0a Reviewed-on: https://go-review.googlesource.com/c/go/+/203118 Reviewed-by: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]>
1 parent bababde commit 9e914f5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Diff for: src/cmd/go/internal/modload/import.go

+11
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ func Import(path string) (m module.Version, dir string, err error) {
237237
return m, "", &ImportMissingError{Path: path, Module: m}
238238
}
239239
}
240+
if len(mods) > 0 && module.CheckPath(path) != nil {
241+
// The package path is not valid to fetch remotely,
242+
// so it can only exist if in a replaced module,
243+
// and we know from the above loop that it is not.
244+
return module.Version{}, "", &PackageNotInModuleError{
245+
Mod: mods[0],
246+
Query: "latest",
247+
Pattern: path,
248+
Replacement: Replacement(mods[0]),
249+
}
250+
}
240251
}
241252

242253
candidates, err := QueryPackage(path, "latest", Allowed)

Diff for: src/cmd/go/testdata/script/mod_replace_import.txt

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ stdout 'example.com/y v0.0.0-00010101000000-000000000000 => ./y'
2323
stdout 'example.com/x/v3 v3.0.0-00010101000000-000000000000 => ./v3'
2424
stdout 'example.com/v v1.12.0 => ./v12'
2525

26+
# The go command should print an informative error when the matched
27+
# module does not contain a package.
28+
cd fail
29+
! go list all
30+
stdout 'localhost.fail'
31+
stderr '^can.t load package: m.go:3:8: module w@latest found \(v0.0.0-00010101000000-000000000000, replaced by ../w\), but does not contain package w$'
32+
2633
-- go.mod --
2734
module example.com/m
2835

@@ -107,3 +114,16 @@ package v
107114
module v.localhost
108115
-- v/v.go --
109116
package v
117+
118+
-- fail/m.go --
119+
package main
120+
121+
import _ "w"
122+
123+
func main() {}
124+
125+
-- fail/go.mod --
126+
module localhost.fail
127+
128+
replace w => ../w
129+

0 commit comments

Comments
 (0)