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

compiler detects module based on current working directory #68043

Closed
rittneje opened this issue Jun 18, 2024 · 4 comments
Closed

compiler detects module based on current working directory #68043

rittneje opened this issue Jun 18, 2024 · 4 comments

Comments

@rittneje
Copy link
Contributor

rittneje commented Jun 18, 2024

Go version

go version go1.22.4 darwin/amd64

Output of go env in your module/workspace:

GO111MODULE='auto'
GOARCH='amd64'
GOBIN=''
GOCACHE='/tmp/.gocache'
GOENV='/Users/rittneje/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/rittneje/go/pkg/mod'
GONOPROXY='<redacted>'
GONOSUMDB='<redacted>'
GOOS='darwin'
GOPATH='/Users/rittneje/go'
GOPRIVATE='git.intra.lutron.com'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/rittneje/go1.22.4'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/Users/rittneje/go1.22.4/pkg/tool/darwin_amd64'
GOVCS='<redacted>'
GOVERSION='go1.22.4'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD=''
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/kf/kr7_s3xx0l12zbj3jrn082hmzy5gvy/T/go-build3773258374=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

We originally noticed this issue because debug.ReadBuildInfo() was not reporting the expected value for DefaultGODEBUG.

It seems that go build will only detect that we are building a module if the current working directory is with the module root.

module godebugbug

go 1.22.4
package main

import (
	"fmt"
	"runtime/debug"
)

func main() {
	fmt.Println(debug.ReadBuildInfo())
}

What did you see happen?

It ignores go.mod completely, and uses the 1.21 DefaultGODEBUG settings.

Inside the module folder:
go build && ./godebugbug

go go1.22.4
path godebugbug
mod godebugbug (devel)
build -buildmode=exe
build -compiler=gc
build CGO_ENABLED=1
build CGO_CFLAGS=
build CGO_CPPFLAGS=
build CGO_CXXFLAGS=
build CGO_LDFLAGS=
build GOARCH=amd64
build GOOS=darwin
build GOAMD64=v1
true

Outside the module folder:
go build -o main ./godebugbug && ./main

go go1.22.4
path _/Users/jrittner/gomodtest/src/godebugbug
build -buildmode=exe
build -compiler=gc
build DefaultGODEBUG=httplaxcontentlength=1,httpmuxgo121=1,panicnil=1,tls10server=1,tlsrsakex=1,tlsunsafeekm=1
build CGO_ENABLED=1
build CGO_CFLAGS=
build CGO_CPPFLAGS=
build CGO_CXXFLAGS=
build CGO_LDFLAGS=
build GOARCH=amd64
build GOOS=darwin
build GOAMD64=v1
true

What did you expect to see?

It should instead detect it based upon the target package being within a module.

That is, if I run go build ./foo/bar/baz, where the go.mod file is located at ./foo/go.mod, then it should detect the module and build properly, using the DefaultGODEBUG that goes with the version of Go listed in the go.mod file.

@rittneje
Copy link
Contributor Author

Adding to the confusion, the 1.22 compiler will compile with 1.22 loop semantics when it cannot find the go.mod file, even though it will also use the 1.21 DefaultGODEBUG.

@seankhliao
Copy link
Member

I believe this is working as intended for your setting of GO111MODULE=auto where it falls back to GOPATH mode if a module is not found.
The default setting is GO111MODULE=on, which should report an error like:

go: go.mod file not found in current directory or any parent directory; see 'go help modules'

Using the module based on the go command is the intended behaviour, see also the -C flag.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 18, 2024
@rittneje
Copy link
Contributor Author

@seankhliao then the behavior ought to be revisited, as it is completely undesirable and unexpected. The toolchain should go by the go.mod file for the code being built; the current working directory should never be relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants