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

Go module replace doesn't work when 2 levels deep #35450

Closed
kstenerud opened this issue Nov 8, 2019 · 1 comment
Closed

Go module replace doesn't work when 2 levels deep #35450

kstenerud opened this issue Nov 8, 2019 · 1 comment

Comments

@kstenerud
Copy link

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

$ go version
go version go1.13.3 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/karl/.cache/go-build"
GOENV="/home/karl/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/karl/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/karl/tmp/c/go.mod"
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-build553992755=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I created 3 modules in the following directory structure:

.
├── a
│   ├── a.go
│   ├── a_test.go
│   └── go.mod
├── b
│   ├── b.go
│   ├── b_test.go
│   └── go.mod
└── c
    ├── c.go
    ├── c_test.go
    └── go.mod

The go files are just enough to form dependencies c -> b -> a:

a/a.go:

package a

import "fmt"

func DoIt() {
	fmt.Printf("Doing it\n")
}

b/b.go:

package b

import "github.com/test/a"

func DoA() {
	a.DoIt()
}

c/c.go:

package c

import "github.com/test/b"

func DoB() {
	b.DoA()
}

The test files are all empty, just enough to run go test successfully:

a/a_test.go:

package a

import "testing"

func TestNothing(t *testing.T) {
	
}

... b and c are the same.

The modules have replace statements:

a/go.mod:

module github.com/test/a

go 1.13

b/go.mod:

module github.com/test/b

go 1.13

replace github.com/test/a => ../a

c/go.mod:

module github.com/test/c

go 1.13

replace github.com/test/b => ../b

What did you expect to see?

$ cd a
$ go test
PASS
ok  	github.com/test/a	0.001s
$ cd ../b
$ go test
PASS
ok  	github.com/test/b	0.001s
$ cd ../c
$ go test
PASS
ok  	github.com/test/c	0.001s

What did you see instead?

$ cd a
$ go test
PASS
ok  	github.com/test/a	0.001s
$ cd ../b
$ go test
PASS
ok  	github.com/test/b	0.001s
$ cd ../c
$ go test
go: github.com/test/b@v0.0.0-00010101000000-000000000000 requires
	github.com/test/a@v0.0.0-00010101000000-000000000000: invalid version: git fetch -f https://github.com/test/a refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /home/karl/go/pkg/mod/cache/vcs/c0c8a2e25befe4efbfc70a367829c70c25266a092b79a63d54afae917f3e6b65: exit status 128:
	ERROR: Repository not found.
	fatal: Could not read from remote repository.
	
	Please make sure you have the correct access rights
	and the repository exists.
@bcmills bcmills added the modules label Nov 8, 2019
@bcmills
Copy link
Contributor

bcmills commented Nov 8, 2019

This is working as designed. Per https://golang.org/cmd/go/#hdr-The_go_mod_file:

Exclude and replace apply only in the main module's go.mod and are ignored in dependencies.

If you want to apply both replacements simultaneously, then you need both of those replace directives in the main module's go.mod file. (The “main module” is the module in whose directory the go command was invoked.)

@bcmills bcmills closed this as completed Nov 8, 2019
@golang golang locked and limited conversation to collaborators Nov 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants