Noticed an existing bug in modload.MainModuleSet.DirImportPath introduced in https://go.dev/cl/129798 (for #27022).
Reproduced the bug as follows:
go build testdata/q/q.go
-- go.mod --
module example
go 1.23
require (
testdata/q v0.1.0
)
replace (
testdata/q => ./testdata/q
)
-- internal/p/p.go --
package p
-- testdata/q/go.mod --
module testdata/q
go 1.23
-- testdata/q/q.go --
package q
import _ "example/internal/p"
What did you see happen?
$ go build ./testdata/q/q.go
(no error)
What did you expect to see?
An error, because (per the behavior agreed upon in #23970) a package in the module testdata/q should not be allowed to import an internal module from a module that does not share its import path.
In GOPATH mode the rule has always been that 'go run x.go' can
import whatever the package in x.go's directory would be able to
import. Apply the same rule here.
go build testdata/q confirms that the package in q.go's directory is not allowed to import that package:
$ go build testdata/q
package testdata/q
testdata/q/q.go:3:8: use of internal package example/internal/p not allowed
The underlying problem is that DirImportPath is just checking for a file path prefix, not using modload.dirInModule to properly check that the directory is contained in the module without intervening go.mod files carving out module boundaries.
Go version
5dcc04aeacdaa78cc431e3f8b2119d2f351685b5
Output of
go env
in your module/workspace:What did you do?
Reviewed test failures on pending https://go.dev/cl/567435.
Noticed an existing bug in
modload.MainModuleSet.DirImportPath
introduced in https://go.dev/cl/129798 (for #27022).Reproduced the bug as follows:
What did you see happen?
(no error)
What did you expect to see?
An error, because (per the behavior agreed upon in #23970) a package in the module
testdata/q
should not be allowed to import aninternal
module from a module that does not share its import path.Per https://go.dev/cl/129798:
go build testdata/q
confirms that the package inq.go
's directory is not allowed to import that package:The underlying problem is that
DirImportPath
is just checking for a file path prefix, not usingmodload.dirInModule
to properly check that the directory is contained in the module without interveninggo.mod
files carving out module boundaries.(CC @matloob @samthanawalla)