Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: reject the -modfile flag in workspace mode #59996

Closed
ZekeLu opened this issue May 5, 2023 · 5 comments
Closed

cmd/go: reject the -modfile flag in workspace mode #59996

ZekeLu opened this issue May 5, 2023 · 5 comments
Labels
GoCommand cmd/go help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@ZekeLu
Copy link
Contributor

ZekeLu commented May 5, 2023

What version of Go are you using (go version)?

$ go version
go version go1.20.4 linux/amd64

Does this issue reproduce with the latest release?

Yes. And reproduce on gotip too.

What operating system and processor architecture are you using (go env)?

go env
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/zeke/.cache/go-build"
GOENV="/home/zeke/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/zeke/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/zeke/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/snap/go/current"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/snap/go/current/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK="/home/zeke/src/temp/76174480/go.work"
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2566520163=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I run go run with the -modfile flag in a workspace:

$ go run -modfile ./m1/go.mod ./m1
directory structure and files
├── go.work
├── m1
│   ├── go.mod
│   └── main.go
└── m2
    ├── go.mod
    └── main.go

go.work:

go 1.20

use (
	./m1
	./m2
)

m1/go.mod:

module example.com/m1

go 1.20

m1/main.go:

package main

import "fmt"

func main() {
	fmt.Println("Hello m1")
}

m2/go.mod:

module example.com/m2

go 1.20

m2/main.go:

package main

import "fmt"

func main() {
	fmt.Println("Hello m2")
}

What did you expect to see?

It runs the program or displays more friendly error message.

What did you see instead?

go: module example.com/m1 appears multiple times in workspace

What's wrong?

func modFilePath(modRoot string) string {
if cfg.ModFile != "" {
return cfg.ModFile
}
return filepath.Join(modRoot, "go.mod")
}

When -modfile is specified, the func modFilePath ignores the parameter modRoot and returns the specified mod file stored in cfg.ModFile directly. This is incorrect in workspace mode when there are multiple mod roots (specified by the use directive) because all the mod roots will be resolved to the same go.mod file.

for _, modroot := range modRoots {
gomod := modFilePath(modroot)

I would like to work on this issue. But I'm not sure what should we do about it. The most simple fix is to prevent the -modfile flag in workspace mode. Opinions?

@seankhliao
Copy link
Member

is this worth fixing over using go run -C ./m1 .?

@ZekeLu
Copy link
Contributor Author

ZekeLu commented May 5, 2023

This issue is forked from this stackoverflow question. I don't use it myself. I don't understand why the OP wants to specify the -modfile flag yet. I will copy your suggestion there. Thank you!

Update: In fact, it works without the -modfile flag. Don't need to apply the -C flag.

@bcmills bcmills added NeedsFix The path to resolution is known, but the work has not been done. GoCommand cmd/go modules labels May 5, 2023
@bcmills bcmills changed the title cmd/go: the -modfile flag does not work in workspace mode cmd/go: reject the -modfile flag in workspace mode May 5, 2023
@bcmills
Copy link
Contributor

bcmills commented May 5, 2023

What did you expect to see?

It runs the program or displays more friendly error message.

Thanks for the report! I agree that a clearer error message would be good to have here.
(CC @matloob)

@gopherbot
Copy link

Change https://go.dev/cl/493315 mentions this issue: cmd/go/internal/modload: reject the -modfile flag in workspace mode

@ZekeLu
Copy link
Contributor Author

ZekeLu commented May 8, 2023

I don't understand why the OP wants to specify the -modfile flag yet.

This is clear now. The OP found that one of his module is built with an unexpected third-party module due to the minimal version selection (MVS) in workspace, and he tried to workaround it with the -modfile flag. Now he understand how MVS works, and there is not need for the -modfile flag (or anything else) now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants