[release-branch.go1.18] cmd/go: ignore the workspace when running a package at a specified version

Fixes #51390

Change-Id: I805e66809b2aafb48f7040dee72910dd7d6c1396
Reviewed-on: https://go-review.googlesource.com/c/go/+/388917
Trust: Bryan Mills <bcmills@google.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
(cherry picked from commit 86b5f6a7be)
Reviewed-on: https://go-review.googlesource.com/c/go/+/389575
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
This commit is contained in:
Bryan C. Mills 2022-03-02 17:14:55 -05:00 committed by Dmitri Shuralyov
parent 9f5e2849e1
commit 9f40b4f7a4
3 changed files with 24 additions and 2 deletions

View File

@ -288,6 +288,11 @@ func BinDir() string {
// operate in workspace mode. It should not be called by other commands,
// for example 'go mod tidy', that don't operate in workspace mode.
func InitWorkfile() {
if RootMode == NoRoot {
workFilePath = ""
return
}
switch gowork := cfg.Getenv("GOWORK"); gowork {
case "off":
workFilePath = ""

View File

@ -73,8 +73,6 @@ func printStderr(args ...any) (int, error) {
}
func runRun(ctx context.Context, cmd *base.Command, args []string) {
modload.InitWorkfile()
if shouldUseOutsideModuleMode(args) {
// Set global module flags for 'go run cmd@version'.
// This must be done before modload.Init, but we need to call work.BuildInit
@ -84,7 +82,10 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
modload.RootMode = modload.NoRoot
modload.AllowMissingModuleImports()
modload.Init()
} else {
modload.InitWorkfile()
}
work.BuildInit()
var b work.Builder
b.Init()

View File

@ -0,0 +1,16 @@
[short] skip
go run example.com/printversion@v0.1.0
stdout '^main is example.com/printversion v0.1.0$'
-- go.work --
go 1.18
use (
.
)
-- go.mod --
module example
go 1.18
require example.com/printversion v1.0.0