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.mod failed to replace module #29608

Closed
jinq0123 opened this issue Jan 8, 2019 · 4 comments
Closed

go.mod failed to replace module #29608

jinq0123 opened this issue Jan 8, 2019 · 4 comments

Comments

@jinq0123
Copy link

jinq0123 commented Jan 8, 2019

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

λ go version
go version go1.11.4 windows/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
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\jinqing\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=e:\gopath
set GOPROXY=
set GORACE=
set GOROOT=D:\Go
set GOTMPDIR=
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=E:\temp\hhhh\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\jinqing\AppData\Local\Temp\go-build780192066=/tmp/go-build -gno-record-gcc-switches

What did you do?

Init module

E:\temp\hhhh
λ go mod init github.com/jinq0123/hhhh
go: creating new go.mod: module github.com/jinq0123/hhhh

Create main.go

package main

import (
	"fmt"
	_ "my.com/x/text"
)

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

Add replace in mod.go

module github.com/jinq0123/hhhh

replace my.com/x/text => github.com/golang/text v0.3.0

require my.com/x/text v0.0.0

And go build succeeds.

Change v0.3.0 to master

replace my.com/x/text => github.com/golang/text master

What did you expect to see?

Build OK.

Because it says (https://github.com/golang/go/wiki/Modules#when-should-i-use-the-replace-directive):

The replace directive allows you to supply another import path that might be another module located in VCS (GitHub or elsewhere), or on your local filesystem with a relative or absolute file path. The new import path from the replace directive is used without needing to update the import paths in the actual source code.

What did you see instead?

go build failed:

E:\temp\hhhh
λ go build -v -x
WORK=C:\Users\jinqing\AppData\Local\Temp\go-build195710594
go: github.com/golang/[email protected]: parsing go.mod: unexpected module path "golang.org/x/text"
go: error loading module requirements
@agnivade
Copy link
Contributor

agnivade commented Jan 8, 2019

Could you also try with 1.12beta1 ?

/cc @myitcv @bcmills

@bcmills
Copy link
Contributor

bcmills commented Jan 8, 2019

This is working as designed. The canonical module path for the repository stored at github.com/golang/textas declared by its own go.mod file — is golang.org/x/text, and that is the path by which it must be addressed in replace directives.

@bcmills bcmills closed this as completed Jan 8, 2019
@jinq0123
Copy link
Author

jinq0123 commented Jan 9, 2019

But replacing to local works:

replace my.com/x/text => ../text

And the ../text has its own go.mod declaring as golang.org/x/text.

I am expecting the same behavior, from Git or from local. Why it must check the declared module name?

@bcmills
Copy link
Contributor

bcmills commented Jan 9, 2019

Module replacements replace an original path with a new path. To correctly replace the paths for same-module imports within the module (such as this one), we must be able to identify those as same-module imports. That is, we must know what the module believes its own path to be, and the module declaration in the go.mod file tells us exactly that.

When you try to use github.com/golang/text as the replacement, you introduce an ambiguity: should the packages in github.com/golang/text be interpreted as golang.org/x/text (according to their go.mod file), or as github.com/golang/text (according to your replace directive)?

Then, what happens if you then also add a require golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db to your go.mod file? (Do you get an ambiguous import path error? Do you get two separate copies of the packages from that repo?)

See also #26607.

@golang golang locked and limited conversation to collaborators Jan 9, 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

4 participants