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 foo.go " doesn't work #56776

Closed
erifan opened this issue Nov 17, 2022 · 8 comments
Closed

cmd/compile: "go tool compile foo.go " doesn't work #56776

erifan opened this issue Nov 17, 2022 · 8 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@erifan
Copy link

erifan commented Nov 17, 2022

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

$ go version
go version devel go1.20-fee0ab8b5e Thu Nov 17 03:47:35 2022 +0000 darwin/arm64

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
darwin/arm64

What did you do?

$ cat foo.go
package main

import (
"fmt"
)

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

$ go tool compile foo.go

What did you expect to see?

The program compiles.

What did you see instead?

foo.go:4:2: could not import fmt (file not found)

Git bisect points to https://go-review.googlesource.com/c/go/+/432535 cc @michaelmatloob

@cuiweixie
Copy link
Contributor

weixiecui@weixiecui-pc0 MINGW64 /d/code/go/src/go (master)
$ go tool compile foo.go
foo.go:4:2: could not import fmt (file not found)

weixiecui@weixiecui-pc0 MINGW64 /d/code/go/src/go (master)
$ go build -o ./pkg/windows_amd64/fmt.a fmt

weixiecui@weixiecui-pc0 MINGW64 /d/code/go/src/go (master)
$ go tool compile foo.go

maybe just need to compile the fmt package to goroot or gopath.

@seankhliao seankhliao changed the title cmd/go: "go tool compile foo.go " doesn't work cmd/compile: "go tool compile foo.go " doesn't work Nov 17, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Nov 17, 2022
@joedian joedian added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Nov 17, 2022
@ianlancetaylor
Copy link
Contributor

You can also set GODEBUG=installgoroot=all in the environment and run go install fmt.

In general go tool compile is not expected to always work by itself. go build is supported, go tool compile is not.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Nov 17, 2022
@erifan
Copy link
Author

erifan commented Nov 18, 2022

For go1.19, go1.18 and previous versions of go, this usage is feasible, the latest code before CL 432535 also supports this usage, and I didn't see any discussion of changing this behavior in #47257. In my opinion, this is a common user behavior change, does not it require a proposal process? @rsc

"go tool compile" is very useful, especially "go tool compile -S", because you don't need to go through "go build" + "go tool objdump" to check the generated assembly code.

Temporarily reopen this issue, I'll close it if this is still not considered an issue.

@erifan erifan reopened this Nov 18, 2022
@randall77
Copy link
Contributor

I often get tripped up on this when compiling a test for other architectures, where the test imports a package like fmt that has not been compiled yet for that architecture. This is a problem even with 1.19 and earlier - we'd have to include .a files for all os/arch combinations to really fix it. I don't see any way go tool compile -S can solve this problem. I end up doing go build -gcflags=-S or equivalent and then look through the (sometimes large) output for the disassembly I want.

@ianlancetaylor
Copy link
Contributor

You can go build -gcflags=MYPKG=-S to pass the -S option only when compiling the package you are interested in.

@erifan
Copy link
Author

erifan commented Nov 18, 2022

Thanks @randall77 @ianlancetaylor go build -gcflags=MYPKG=-S can basically replace go tool compile -S. But there is another case that ordinary developers may not use so often, that is, sometimes I need to check the information in the .o file, such as the relocation types, especially when doing linker-related work. I tried go build -gcflags="-o XXX.o" but it doesn't work. And go build doesn't seem to have an option to stop the build process just after compilation?

In fact, I think whether go tool compile simple_program.go can work is the same as whether go build simple_program.go can work without the .mod file. My personal opinion is that if simple_program.go only imports standard libraries, it should be able to work, that is, the compiler should be able to find the location of the standard library. This can bring great convenience to debugging simple programs, and the compiler tool should not be designed to be used only by go build.

@ianlancetaylor
Copy link
Contributor

If you run GODEBUG=installgoroot=all go install std then you should be able to run go tool compile for code that just imports the standard library.

Closing because this is a workflow that we've decided, for what we think are good reasons, to not support by default.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Nov 18, 2022
@erifan
Copy link
Author

erifan commented Nov 21, 2022

for what we think are good reasons

Okay, can you elaborate ? Or point me to the issue link. Is it #47257 ?

@golang golang locked and limited conversation to collaborators Nov 21, 2023
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 NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

6 participants