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

Build/test fails with "Undefined symbols for architecture x86_64" error on Apple M1 & go1.16beta1 #1087

Closed
PieterVoorwinden opened this issue Jan 7, 2021 · 15 comments
Labels
FrozenDueToAge upstream-go Issues that are caused by bugs in the Go toolchain.

Comments

@PieterVoorwinden
Copy link

What version of Go, VS Code & VS Code Go extension are you using?

  • Run go version to get version of Go
    • go version go1.16beta1 darwin/arm64
  • Run gopls -v version to get version of Gopls if you are using the language server.
    • golang.org/x/tools/gopls v0.6.2
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders
    • 1.52.1
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.20.1
  • Run go env to get the go development environment details

GO111MODULE="on"
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/pietervoorwinden/Library/Caches/go-build"
GOENV="/Users/pietervoorwinden/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/pietervoorwinden/Repositories/pkg/mod"
GONOPROXY="git.profects.com"
GONOSUMDB="git.profects.com"
GOOS="darwin"
GOPATH="/Users/pietervoorwinden/Repositories"
GOPRIVATE="git.profects.com"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.15.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.15.6/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.16beta1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/pietervoorwinden/Repositories/src/git.profects.com/agro4all/comparison-api/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-build817711015=/tmp/go-build -gno-record-gcc-switches -fno-common"

Share the Go related settings you have added/edited

"go.formatTool": "goimports",
"go.useLanguageServer": true,
"gopls": {
    "gofumpt": true,
},
"go.testTimeout": "45s",

Describe the bug

Testing in VScode isn't working for me. Running go test -timeout 45s -run ^TestComparison$ git.profects.com/agro4all/comparison-api/handler in my terminal works without any issue:
ok git.profects.com/agro4all/comparison-api/handler 0.425s

But when I press Run test inside VSCode it fails with an error.
The same error occurs when testing via the integrated terminal.

I am not sure whether this is a problem on my side but the test succeeds when running in my terminal, which is weird. Thanks for the help in advance.

@hyangah
Copy link
Contributor

hyangah commented Jan 8, 2021

@PieterVoorwinden It looks like, VSCode is using go1.15.6, not go1.16beta1.
macOS on arm64 support is available only from go1.16.

Either open the code in the right PATH environment variable - the extension host searches PATH to find the go, or switch the go version to go1.16beta1 using Go: Choose Go Environment UI.

@hyangah hyangah added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jan 8, 2021
@PieterVoorwinden
Copy link
Author

@hyangah I've got this MacBook since 2 days and I'm sure I only installed Go 1.16beta1. See this screenshot. It says 1.16beta1 in the corner left below and to use Go 1.15 I've to go get it.

@hyangah
Copy link
Contributor

hyangah commented Jan 8, 2021

@PieterVoorwinden Thanks for the screenshot. Looks like the go extension was confused and lying.
It's pointing to the homebrew installed go1.15.6 while showing go1.16beta1 as the version. Don't know how 🤔

Screen Shot 2021-01-08 at 8 41 35 AM

  • How did you install go1.16beta1 and where is the path?
  • What is the output of the following commands from the terminal outside of vscode
    which go
    go version
    /opt/homebrew/bin/go version
    /opt/homebrew/Cellar/go/1.15.6/libexec/bin/go version
  • Output of Go: Locate Configured Go Tools

@PieterVoorwinden
Copy link
Author

@hyangah I installed go with brew install go. Maybe the 1.15.6 is coming from here? Not sure tho, I'm new to Homebrew.

Output from Iterm:

➜ ~ which go
/opt/homebrew/bin/go
➜ ~ go version
go version go1.16beta1 darwin/arm64
➜ ~ /opt/homebrew/bin/go version
go version go1.16beta1 darwin/arm64
➜ ~ /opt/homebrew/Cellar/go/1.15.6/libexec/bin/go version
go version go1.16beta1 darwin/arm64

Output of Go: Locate Configured Go Tools:

Checking configured tools....
GOBIN: undefined
toolsGopath:
gopath: /Users/pietervoorwinden/Repositories
GOROOT: /opt/homebrew/Cellar/go/1.15.6/libexec
PATH: /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/pietervoorwinden/Repositories/bin

gopkgs: /Users/pietervoorwinden/Repositories/bin/gopkgs installed
go-outline: /Users/pietervoorwinden/Repositories/bin/go-outline installed
gotests: gotests not installed
gomodifytags: gomodifytags not installed
impl: impl not installed
goplay: goplay not installed
dlv: dlv not installed
golint: /Users/pietervoorwinden/Repositories/bin/golint installed
gopls: /Users/pietervoorwinden/Repositories/bin/gopls installed

The go envs printed in Go: Locate Configured Go Tools are identical to the envs described in the issue.

@hyangah
Copy link
Contributor

hyangah commented Jan 12, 2021

@PieterVoorwinden Thanks. That's so strange. Can you whare with me how to install go1.16beta1 using homebrew?

And, can you run the go build command -x -v flags from the integrated terminal (where you observed the issue) and the terminal (where the test runs successfully? I hope that gives us some hint.

Edit: Looks like homebrew is doing something funny for arm CPU - so, the version mismatch is a redherring. https://github.com/Homebrew/homebrew-core/blob/736c68b16a9df5c45ac6cd043fe30aaa9847cb80/Formula/go.rb#L7-L10
But still the test build error message looks like a bug caused by incorrect goos/goarch.

@PieterVoorwinden
Copy link
Author

@hyangah here is the output for go build -x -v:
integrated_terminal.txt
iterm.txt

It indeed looks like the 1.15.6 version is coming from the Homebrew repository. I literally ran brew install go. This is the output when I reinstall go:

➜  comparison-api git:(master) ✗ brew reinstall go
==> Downloading https://homebrew.bintray.com/bottles/go-1.15.6.arm64_big_sur.bottle.tar.gz
Already downloaded: /Users/pietervoorwinden/Library/Caches/Homebrew/downloads/c9b4b4bdafcd2d163c425bee5e2485bc182699c1e9b7c0f5e72873e666b65523--go-1.15.6.arm64_big_sur.bottle.tar.gz
==> Reinstalling go
==> Pouring go-1.15.6.arm64_big_sur.bottle.tar.gz
==> Caveats
This is a beta version of the Go compiler for Apple Silicon
(Go 1.16beta1).
==> Summary
🍺  /opt/homebrew/Cellar/go/1.15.6: 10,058 files, 522.2MB

It's indeed the 1.16 beta installed at the go 1.15.6 location.

@PieterVoorwinden
Copy link
Author

@hyangah It somehow doesn't fail in every project:
image
This is a different project but the test in VS Code does succeed. Thoughts?

@hyangah
Copy link
Contributor

hyangah commented Jan 13, 2021

@PieterVoorwinden Thanks for providing the info. We think this is because VSCode is an x86 binary - if the ancestor process is for x86, child processes default to x86 which confuses the C compiler later. A workaround suggested is to explicitly specify the arch. Can you try
CC="clang -arch arm64" go build from the integrated terminal and see if the build succeeds?

The other project succeeds probably because that doesn't involve cgo.

@PieterVoorwinden
Copy link
Author

PieterVoorwinden commented Jan 14, 2021

@hyangah

➜  comparison-api git:(master) ✗ CC="clang -arch arm64" go build
# git.profects.com/agro4all/comparison-api
/opt/homebrew/Cellar/go/1.15.6/libexec/pkg/tool/darwin_arm64/link: running clang failed: exit status 1
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000000.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/go.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000001.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000002.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000003.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000004.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000005.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000006.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000007.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000010.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000012.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64ld: warning: 
ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000011.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000008.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000009.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000013.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000015.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000017.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000016.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000014.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000018.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-949495219/000019.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is the -x -v output:
integrated_terminal.txt

@hyangah
Copy link
Contributor

hyangah commented Jan 14, 2021

@bcmills @cherrymui From the log @PieterVoorwinden provided, I still see -extld=clang and am afraid CC of this form isn't propagated as I hoped. Can you please take a look? If you like, I can transfer it to the main go repo.

➜  comparison-api git:(master) ✗ CC="clang -arch arm64" go build -x -v
...
/opt/homebrew/Cellar/go/1.15.6/libexec/pkg/tool/darwin_arm64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=W5Tp8qUx5g4k0-Gvu_4d/8a4epZkMOvFqk3uXvS3l/YszBVTanTFOzIlsLgZwy/W5Tp8qUx5g4k0-Gvu_4d -extld=clang /Users/pietervoorwinden/Library/Caches/go-build/07/075fe30ae948a8bda26208ac267a691aab36ac9f7cc7431fa17811bd55534fc3-d
# git.profects.com/agro4all/comparison-api
/opt/homebrew/Cellar/go/1.15.6/libexec/pkg/tool/darwin_arm64/link: running clang failed: exit status 1
ld: warning: ignoring file /var/folders/j8/kln9bkmn77d3qkw5f9tnzrh80000gn/T/go-link-939404782/000000.o, building for macOS-x86_64 but attempting to link with file built for unknown-arm64
...
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable

@rsc
Copy link
Contributor

rsc commented Jan 14, 2021

I'd still like to see "go1.15.6" not appear in the failure output before filing this as a Go 1.16 bug.

@hyangah
Copy link
Contributor

hyangah commented Jan 14, 2021

Re: 'go1.15.6' string in the trace: Homebrew/homebrew-core#69000 will not be fixed - it requires a bigger change in their CI, and go 1.16 release is not too far away I hope.

It looks like vscode M1 support will be included in the next release (microsoft/vscode#106770) and the insider version is available for download (https://code.visualstudio.com/insiders/).

@hyangah hyangah changed the title Pressing Run test in VScode fails with an error while testing via the terminal works without any issue Build/test fails with "Undefined symbols for architecture x86_64" error on Apple M1 & go1.16beta1 Jan 14, 2021
@gopherbot
Copy link
Collaborator

Change https://golang.org/cl/283812 mentions this issue: cmd/dist, cmd/go: pass -arch for C compilation on Darwin by default

gopherbot pushed a commit to golang/go that referenced this issue Jan 14, 2021
On Apple Silicon Mac, the C compiler has an annoying default
target selection, depending on the ancestor processes'
architecture. In particular, if the shell or IDE is x86, when
running "go build" even with a native ARM64 Go toolchain, the C
compiler defaults to x86, causing build failures. We pass "-arch"
flag explicitly to avoid this situation.

Fixes #43692.
Fixes #43476.
Updates golang/vscode-go#1087.

Change-Id: I80b6a116a114e11e273c6886e377a1cc969fa3f6
Reviewed-on: https://go-review.googlesource.com/c/go/+/283812
Trust: Cherry Zhang <[email protected]>
Run-TryBot: Cherry Zhang <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Reviewed-by: Bryan C. Mills <[email protected]>
@PieterVoorwinden
Copy link
Author

@hyangah Thanks for the help, much appreciated! Not sure when/if this issue can be closed, leaving it up to you.

@hyangah hyangah added upstream-go Issues that are caused by bugs in the Go toolchain. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Jan 15, 2021
@hyangah
Copy link
Contributor

hyangah commented Jan 15, 2021

@PieterVoorwinden Thank you for reporting the issue and providing sufficient info necessary for investigation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge upstream-go Issues that are caused by bugs in the Go toolchain.
Projects
None yet
Development

No branches or pull requests

4 participants