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/compile: go tool compile could not import fmt #58629

Closed
chimission opened this issue Feb 22, 2023 · 1 comment
Closed

cmd/compile: go tool compile could not import fmt #58629

chimission opened this issue Feb 22, 2023 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge

Comments

@chimission
Copy link

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

$ go version
go version go1.20.1 darwin/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/10043479/Library/Caches/go-build"
GOENV="/Users/10043479/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/10043479/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/10043479/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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 -fdebug-prefix-map=/var/folders/6r/y_jqv72j6mlctgk7_k_lkt_0v3s_6z/T/go-build4156745098=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.20.1 darwin/amd64
GOROOT/bin/go tool compile -V: compile version go1.20.1
uname -v: Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000
ProductName:	macOS
ProductVersion:	12.5.1
BuildVersion:	21G83
lldb --version: lldb-1400.0.38.17
Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)

What did you do?

package main

import "fmt"

func main() {
	fmt.Println(1)
}

I wrote a simple Go program and ran "go run" and "go build", both of which succeeded and produced the expected result. However, I encountered a problem when running "go tool compile -S ./main.go".

What did you expect to see?

I expected the "go tool compile -S ./main.go" command to output the result normally.

What did you see instead?

go tool compile -S ./main.go
./main.go:3:8: could not import fmt (file not found)
@chimission chimission changed the title go tool compile could not import fmt cmd/compile: go tool compile could not import fmt Feb 22, 2023
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Feb 22, 2023
@thanm
Copy link
Contributor

thanm commented Feb 22, 2023

hello @chimission,

the expectation is that when users want to build Go code they will use "go build" as opposed to running "go tool compile" directly. The command line flags and APIs for "go tool compile" are complicated and tend to change from release to release. Better to let the "go" command invoke the compiler .

In the specific case of getting "-S" output, you can use

$ go build -gcflags=-S main.go

for that. If you want to see the specifics of how the go command is invoking the compiler, try running your build with "-x -work", then look at the transcript:

$ go build -x -work main.go 1> transcript.txt 2>&1
$

If you look at the transcript, you'll see that the Go command is passing in a config file that tells the compiler where to read the export data for the "fmt" package (among others).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

3 participants