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: fails with go: cannot find GOROOT directory after cross-compiling from WSL to Windows. #45763

Open
aodhneine opened this issue Apr 25, 2021 · 20 comments

Comments

@aodhneine
Copy link

@aodhneine aodhneine commented Apr 25, 2021

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

$ GOROOT=/usr/local/go go version
go version go1.16.3 linux/amd64

Does this issue reproduce with the latest release?

No, building from go1.16.3 branch doesn't reproduce this bug.

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

Using WSL 2 with Alpine Linux on Windows 10 Home, build 21364.co_release.210416-1504.

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/freya/.cache/go-build"
GOENV="/home/freya/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/freya/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/freya/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3203524129=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Cross-compiled Go toolchain master using Go 1.16.3 for windows/amd64, using the standard procedures as described on https://golang.org/doc/install/source. When running /mnt/c/Users/freya/goroot/bin/windows_amd64/go.exe version I got the following error instead: go: cannot find GOROOT directory: \mnt\c\Users\freya\goroot. The same happens when setting GOROOT=/mnt/c/Users/admer/goroot/.

What did you expect to see?

go.exe version running correctly and not failing.

What did you see instead?

% /mnt/c/Users/freya/goroot/bin/windows_amd64/go.exe version
go: cannot find GOROOT directory: \mnt\c\Users\freya\goroot
% GOROOT=/mnt/c/Users/freya/goroot/ /mnt/c/Users/freya/goroot/bin/windows_amd64/go.exe version
go: cannot find GOROOT directory: \mnt\c\Users\freya\goroot
@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Apr 25, 2021

What happens when you unset GOROOT? You normally do not have to set it as the default is dertermined by the executable location.

Loading

@aodhneine
Copy link
Author

@aodhneine aodhneine commented Apr 25, 2021

Same as whenGOROOT is set. Tried with GOROOT= /mnt/..., export GOROOT=, and unset GOROOT.

% /mnt/c/Users/freya/goroot/bin/windows_amd64/go.exe version
go: cannot find GOROOT directory: \mnt\c\Users\freya\goroot

Loading

@aodhneine aodhneine changed the title Fails with go: cannot find GOROOT directory after cross-compiling from Linux to Windows. Fails with go: cannot find GOROOT directory after cross-compiling from WSL to Windows. Apr 25, 2021
@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Apr 25, 2021

The path /mnt/c/Users/freya/goroot/bin/windows_amd64/go.exe looks like you are running inside WSL, which is weird cause you are trying to run a .exe.
Can you run it through powershell?

Loading

@aodhneine
Copy link
Author

@aodhneine aodhneine commented Apr 26, 2021

It returns the exactly same error.

Let me try to clarify what I exactly did. First, I cloned Go repo to C:\Users\freya\goroot, since I cannot build it in WSL, as it requires support for LockFileEx, which isn't currently supported (see #37461 and microsoft/WSL#5762). Then I downloaded Go 1.16.3 release for Linux to /usr/local/go in WSL, and added it to the PATH. Lastly, I went to the goroot folder and ran GOOS=windows GOARCH=amd64 ./all.bash, building the Windows version of the compiler. When I ran bin/windows_amd64/go.exe, it failed with that error.

Loading

@cherrymui
Copy link
Contributor

@cherrymui cherrymui commented Apr 26, 2021

How do you run the Windows program? I think it is supposed to be run from Windows, not from the Linux environment.

Loading

@cherrymui cherrymui changed the title Fails with go: cannot find GOROOT directory after cross-compiling from WSL to Windows. cmd/go: fails with go: cannot find GOROOT directory after cross-compiling from WSL to Windows. Apr 26, 2021
@aodhneine
Copy link
Author

@aodhneine aodhneine commented Apr 26, 2021

WSL allows to transparently run Windows programs from the Linux environment.

Loading

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Apr 26, 2021

WSL allows to transparently run Windows programs from the Linux environment.

And I am asking whether it runs in a normal power shell, this is importatnt to know whether it is a WSL specific problem.

Loading

@aodhneine
Copy link
Author

@aodhneine aodhneine commented Apr 26, 2021

As I previously wrote, it returns the same error.

Loading

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Apr 26, 2021

Was GOROOT unset while running in power shell?
Did you try setting GOROOT=C:/Users/freya/goroot

Loading

@cherrymui
Copy link
Contributor

@cherrymui cherrymui commented Apr 26, 2021

Could you provide the exact commands that you use to build the toolchain, as well as how you run it? Thanks.

Loading

@cherrymui
Copy link
Contributor

@cherrymui cherrymui commented Apr 26, 2021

Loading

@aodhneine
Copy link
Author

@aodhneine aodhneine commented Apr 26, 2021

... Why does it work now?

% git clone https://go.googlesource.com/go goroot-master --depth 1
Cloning into 'goroot-master'...
remote: Finding sources: 100% (10857/10857)
remote: Total 10857 (delta 1384), reused 7013 (delta 1384)
Receiving objects: 100% (10857/10857), 22.75 MiB | 4.05 MiB/s, done.
Resolving deltas: 100% (1384/1384), done.
Updating files: 100% (10022/10022), done.
% cd goroot-master
% git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
% cd src
% GOOS=windows GOARCH=amd64 GOROOT_BOOTSTRAP=/usr/local/go ./all.bash
Building Go cmd/dist using /usr/local/go. (go1.16.3 linux/amd64)
Building Go toolchain1 using /usr/local/go.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for host, linux/amd64.
Building packages and commands for target, windows/amd64.

##### Testing packages.
... see https://p.teknik.io/Raw/5dgnv ...
% /home/freya/goroot-master/bin/go version
go version devel go1.17-d020264 Mon Apr 26 17:52:46 2021 +0000 linux/amd64
% /home/freya/goroot-master/bin/windows_amd64/go.exe version
go version devel go1.17-d020264 Mon Apr 26 17:52:46 2021 +0000 windows/amd64
PS Microsoft.PowerShell.Core\FileSystem::\\wsl.localhost\Alpine\home\freya\goroot-master> .\bin\windows_amd64\go.exe version
go version devel go1.17-d020264 Mon Apr 26 17:52:46 2021 +0000 windows/amd64

Loading

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Apr 26, 2021

Did you change from WSL1 to WSL2?

Loading

@aodhneine
Copy link
Author

@aodhneine aodhneine commented Apr 26, 2021

No, it was WSL2 before too.

Loading

@alexbrainman
Copy link
Member

@alexbrainman alexbrainman commented Apr 28, 2021

How do you run the Windows program? I think it is supposed to be run from Windows, not from the Linux environment.

WSL is not Windows.

If you want to use WSL environment, then you should follow Linux instructions on how to install and use Go.

If you want to use Windows environment, then you should follow Windows instructions for Go.

Alex

Loading

@rayjcwu
Copy link
Contributor

@rayjcwu rayjcwu commented May 10, 2021

I encountered this issue when trying to do cross-compiling golint from mac to Linux.

On my mac, it has

> go env GOROOT
/usr/local/Cellar/go/1.16.3/libexec

, and I run GOOS=linux go build to build and upload the binary to Linux
On my Linux box, it has

> go env GOROOT
/usr/local/go

. But when I run the above binary, it shows

go/build: go list module/pkg1: exit status 2  
go: cannot find GOROOT directory: /usr/local/Cellar/go/1.16.3/libexec

rather than picking up the go env GOROOT on Linux. It seems somehow the GOROOT info on my mac is baked into the binary.

go env of my mac Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/raywu/Library/Caches/go-build"
GOENV="/Users/raywu/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/raywu/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/raywu/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.16.3/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.16.3/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.16.3"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/v8/q5brnv35699835dr_mt01t0m0000gn/T/go-build441443732=/tmp/go-build -gno-record-gcc-switches -fno-common"

More context here https://gophers.slack.com/archives/C0VPK4Z5E/p1620506869256300

Loading

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented May 10, 2021

@rayjcwu

GOROOT is being detected here
https://github.com/golang/lint/blob/83fdc39ff7b56453e3793356bcff3070b9b96445/golint/import.go#L27

GOROOT returns the root of the Go tree. It uses the GOROOT environment variable, if set at process start, or else the root used during the Go build.

https://golang.org/pkg/runtime/#GOROOT

So your issue seems unrelated since the env var would not be set when running ./golint unless you did so manually.

Loading

@rayjcwu
Copy link
Contributor

@rayjcwu rayjcwu commented May 10, 2021

Let me update my above comment. Here is my environment:

mac:

> go env GOROOT
/usr/local/Cellar/go/1.16.3/libexec

I run GOOS=linux go build and upload the binary to Linux

Linux:

> go env GOROOT
/usr/local/go

. But when I run the above binary, it shows go: cannot find GOROOT directory: /usr/local/Cellar/go/1.16.3/libexec, rather than pick up the go env GOROOT on Linux.

Loading

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented May 10, 2021

@rayjcwu go env GOROOT is set and detected by the go command and is only valid for the go command, the env var is not set for any other program.
Do echo ${GOROOT} to print what golint sees

Since GOROOT is not set while running golint, it then defaults to the GOROOT that built it. i.e. /usr/local/Cellar/go/1.16.3/libexec

Loading

@rayjcwu
Copy link
Contributor

@rayjcwu rayjcwu commented May 10, 2021

echo $GOROOT is empty in both mac and Linux.

Since GOROOT is not set while running golint, it then defaults to the GOROOT that built it. i.e. /usr/local/Cellar/go/1.16.3/libexec

Got it, thanks. This is what I missed.

Loading

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

Successfully merging a pull request may close this issue.

None yet
5 participants