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 build should not fetch replaced module if incorrectly imported #30925

Closed
andrewsjg opened this issue Mar 19, 2019 · 2 comments

Comments

Projects
None yet
3 participants
@andrewsjg
Copy link

commented Mar 19, 2019

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

$ go version
go1.12.1 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
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\andrewsj\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=H:\Development\Go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=H:\Development\goprojects\projects\myproj\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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\ANDREW~1\AppData\Local\Temp\go-build092032086=/tmp/go-build -gno-record-gcc-switches

What did you do?

When you import a module rather than a package from a module (a common mistake). i.e.:

import foo.com/module

rather than

import foo.com/module/package

and use the replace directive expecting go to reference a local copy of the module, go ignores the replace and attempts to fetch the module remotely regardless. If this fails (because of proxies or whatever) the build fails. The build will also fail because of the incorrect import

What appears to be happening is go ignores the replace and even though the module/package is imported incorrectly it still tries to fetch the module.

What did you expect to see?

I would expect go not to attempt the fetch (honour the replace) and throw an error on the module import.

What did you see instead?

It attempts to fetch the module. Which, when you are using a replace to avoid a fetch, breaks the build to early.

I think there is some sort of disconnect when the module instructions and the error in the code disagree with each other and it doesnt behave as I would expect.

Apologies if this is hard to follow! Its hard to explain is a simple way. Bascially if you try and import a module with out a package go seems to want to fetch the module anyway which is probably fine unless you are trying to use a replace in go.mod.

Thanks,
James

@katiehockman katiehockman changed the title Confusing behavior when wrongly importing modules. Particularly when using the replace directive cmd/go: go build should not fetch replaced module if incorrectly imported Mar 19, 2019

@katiehockman

This comment has been minimized.

Copy link
Contributor

commented Mar 19, 2019

/cc @bcmills

@bcmills

This comment has been minimized.

Copy link
Member

commented Mar 28, 2019

If you have replaced the module foo.com/module with something that does not contain the package foo.com/module, the go command in general has no way to know that there is not a different module (such as foo.com, without the /module suffix) that provides that package. So the go command must attempt to determine whether such a module exists.

In general, if you want to avoid fetching modules you should set GOPROXY=off or pass the option -mod=readonly or -mod=vendor. The purpose of a replace directive is to replace a specific module, not to suppress fetches.

(CC @jayconrod)

@bcmills bcmills closed this Mar 28, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.