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: Unexpected behavior when building from a parent directory of a module #34300

Closed
firelizzard18 opened this issue Sep 14, 2019 · 1 comment
Closed

Comments

@firelizzard18
Copy link

@firelizzard18 firelizzard18 commented Sep 14, 2019

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

$ go version
go version go1.13 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 GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=REDACTED
set GOENV=REDACTED
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=REDACTED
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
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:\Tools\Cygwin\tmp\go-build079254262=/tmp/go-build -gno-record-gcc-switches

What did you do?


  • Issues with -trimpath

    1. Build from the parent directory - builds
      • go build -trimpath -o a.exe ./a/b
    2. Build from the module directory - builds
      • cd ./a && go build -trimpath -o ../b.exe ./b
    3. Build from the parent directory, forcing modules - fails with "cannot find main module"
      • GO111MODULE=on go build -trimpath -o am.exe ./a/b
  • Issues with imports

    1. Build from the parent directory - fails with "cannot find package"
      • go build ./c/d
    2. Build from the module directory - builds
      • cd ./c && go build ./d
    3. Build from the parent directory, forcing modules - fails with "cannot find main module"
      • GO111MODULE=on go build ./c/d

I used this script, sym.go to dump the paths stored in the executables.

What did you expect to see?

I expected builds to work from both directories, and I expected -trimpath to... trim paths.

What did you see instead?

Weird paths for the main package (results for b.exe are what I expected for both):

% go run $GOROOT/src/cmd/objtest/sym.go a.exe
main.main => _/C_/Source/test/a/b/main.go:3

% go run $GOROOT/src/cmd/objtest/sym.go b.exe
main.main => test@/b/main.go:3

Errors when building:

c\d\main.go:3:8: cannot find package "github.com/etcd-io/bbolt" in any of:
        GOROOT\src\github.com\etcd-io\bbolt (from $GOROOT)
        GOPATH\src\github.com\etcd-io\bbolt (from $GOPATH)
go: cannot find main module, but found .git/config in C:\Source\test
        to create a module there, run:
        go mod init
@toothrot

This comment has been minimized.

Copy link
Contributor

@toothrot toothrot commented Sep 17, 2019

Hi @firelizzard18,

First of all, thank you for filing such a detailed issue with clear examples.

I believe the build from parent directory in module mode is working as intended. See the documentation here for details: https://golang.org/cmd/go/#hdr-Module_support and https://github.com/golang/go/wiki/Modules#when-do-i-get-old-behavior-vs-new-module-based-behavior. As I understand it, module files are searched for in the current or parent directory, not nested directories.

When building in GOPATH mode, you'll need your dependencies fetched first with "go get". go help gopath-get provides useful instructions.

Finally, the trimpath behavior also seems correct when operating outside of your GOPATH in GOPATH mode. I'm not positive, but I think the package name is inferred based on the fully qualified path in that case.

I don't believe there is a bug here, so I am going to close this issue. Please re-open if you disagree.

If you have further questions on why build behaves this way, I suggest checking out one of the places listed on https://golang.org/wiki/Questions, such as golang-nuts.

Thanks!

@toothrot toothrot closed this Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.