-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
go/build: invoke go command to find modules during Import, Context.Im…
…port The introduction of modules has broken (intentionally) the rule that the source code for a package x/y/z is in GOPATH/src/x/y/z (or GOROOT/src/x/y/z). This breaks the code in go/build.Import, which uses that rule to find the directory for a package. In the long term, the fix is to move programs that load packages off of go/build and onto golang.org/x/tools/go/packages, which we hope will eventually become go/packages. That code invokes the go command to learn what it needs to know about where packages are. In the short term, though, there are lots of programs that use go/build and will not be able to find code in module dependencies. To help those programs, go/build now runs the go command to ask where a package's source code can be found, if it sees that modules are in use. (If modules are not in use, it falls back to the usual lookup code and does not invoke the go command, so that existing uses are unaffected and not slowed down.) Helps #24661. Fixes #26504. Change-Id: I0dac68854cf5011005c3b2272810245d81b7cc5a Reviewed-on: https://go-review.googlesource.com/125296 Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
- Loading branch information
Showing
6 changed files
with
211 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# go/build's Import should find modules by invoking the go command | ||
|
||
go build -o $WORK/testimport.exe ./testimport | ||
|
||
# GO111MODULE=off | ||
env GO111MODULE=off | ||
! exec $WORK/testimport.exe x/y/z/w . | ||
|
||
# GO111MODULE=auto in GOPATH/src | ||
env GO111MODULE= | ||
! exec $WORK/testimport.exe x/y/z/w . | ||
env GO111MODULE=auto | ||
! exec $WORK/testimport.exe x/y/z/w . | ||
|
||
# GO111MODULE=auto outside GOPATH/src | ||
cd $GOPATH/other | ||
env GO111MODULE= | ||
exec $WORK/testimport.exe other/x/y/z/w . | ||
stdout w2.go | ||
|
||
! exec $WORK/testimport.exe x/y/z/w . | ||
stderr 'cannot find module providing package x/y/z/w' | ||
|
||
cd z | ||
env GO111MODULE=auto | ||
exec $WORK/testimport.exe other/x/y/z/w . | ||
stdout w2.go | ||
|
||
# GO111MODULE=on outside GOPATH/src | ||
env GO111MODULE=on | ||
exec $WORK/testimport.exe other/x/y/z/w . | ||
stdout w2.go | ||
|
||
# GO111MODULE=on in GOPATH/src | ||
cd $GOPATH/src | ||
exec $WORK/testimport.exe x/y/z/w . | ||
stdout w1.go | ||
cd w | ||
exec $WORK/testimport.exe x/y/z/w .. | ||
stdout w1.go | ||
|
||
-- go.mod -- | ||
module x/y/z | ||
|
||
-- z.go -- | ||
package z | ||
|
||
-- w/w1.go -- | ||
package w | ||
|
||
-- testimport/x.go -- | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"go/build" | ||
"log" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
p, err := build.Import(os.Args[1], os.Args[2], 0) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Printf("%s\n%s\n", p.Dir, strings.Join(p.GoFiles, " ")) | ||
} | ||
|
||
-- $GOPATH/other/go.mod -- | ||
module other/x/y | ||
|
||
-- $GOPATH/other/z/w/w2.go -- | ||
package w |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters