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: go.work: go build requires versioned replace directive when replacing a used module with a local path #54264

Open
mlaventure opened this issue Aug 4, 2022 · 4 comments
Labels
GoCommand cmd/go modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Thinking
Milestone

Comments

@mlaventure
Copy link

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

$ go version
go version go1.18.5 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mlaventure/.cache/go-build"
GOENV="/home/mlaventure/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/mlaventure/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mlaventure/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/mlaventure/tmp/go/workspace/realdeal/go.mod"
GOWORK="/home/mlaventure/tmp/go/workspace/go.work"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2178506260=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Given the following workspace structure:

~/tmp/go/workspace $ tree
.
├── fubar
│   ├── fubar.go
│   └── go.mod
├── go.work
└── realdeal
    ├── go.mod
    ├── realdeal
    └── realdeal.go

2 directories, 6 files

With the following go.work:

~/tmp/go/workspace $ cat go.work 
go 1.18

use ./fubar
use ./realdeal

replace github.com/foo/fubar   => ./fubar

Build realdeal which depends on fubar:

~/tmp/go/workspace/realdeal $ go build .

What did you expect to see?

Nothing, the build should succeed

What did you see instead?

go: workspace module github.com/foo/fubar is replaced at all versions in the go.work file. To fix, remove the replacement from the go.work file or specify the version at which to replace the module.

Found workaround

The only way I've found to fix this is to update the go.work to be as follow:

~/tmp/go/workspace $ cat go.work 
go 1.18

use ./fubar
use ./realdeal

replace github.com/foo/fubar v0.0.0-00010101000000-000000000000 => ./fubar

This is not needed if a workspace is not used. It also seems to contradict the example from the documentation (https://go.dev/ref/mod#workspaces):

replace (
    golang.org/x/net v1.2.3 => example.com/fork/net v1.4.5
    golang.org/x/net => example.com/fork/net v1.4.5
    golang.org/x/net v1.2.3 => ./fork/net
    golang.org/x/net => ./fork/net
)
@dmitshur dmitshur added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. GoCommand cmd/go labels Aug 4, 2022
@dmitshur dmitshur added this to the Backlog milestone Aug 4, 2022
@dmitshur
Copy link
Contributor

dmitshur commented Aug 4, 2022

CC @bcmills, @matloob.

@BourgeoisBear
Copy link

BourgeoisBear commented Nov 16, 2022

workspace replace lines also cause problems with go mod tidy

go: downloading mydomain.com/module v0.0.0-00010101000000-000000000000
github.com/acct/testcmd imports
        mydomain.com/module: unrecognized import path "mydomain.com/module": https fetch: Get "https://mydomain.com/module?go-get=1": ...

@wangmir
Copy link

wangmir commented May 3, 2023

@dmitshur @bcmills Is there any progress in this? I also have similar problem, when I trying to migrate existing project to use go workspace. It seems replace in go.work is not available to me too. I cannot run go mod tidy

@intel352
Copy link

intel352 commented May 9, 2023

Same problem for me.
I presume this could be getting caused by the fact that I'm specifying the same local module in the use directive as in the replace directive, which I suspect is not intended behavior, but considering Go keeps trying to remotely fetch workspace modules during build rather than respecting the go.work, I was hoping the replace would solve the situation.

Frankly I'm surprised, most of the go tooling is generally high quality, but go.work seems quite error prone...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Thinking
Projects
None yet
Development

No branches or pull requests

6 participants