Skip to content

cmd/go: fails with go: cannot find GOROOT directory after cross-compiling from WSL to Windows. #45763

Not planned
@aodhneine

Description

@aodhneine

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

Activity

AlexRouSg

AlexRouSg commented on Apr 25, 2021

@AlexRouSg
Contributor

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

aodhneine

aodhneine commented on Apr 25, 2021

@aodhneine
Author

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
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.[/+] on Apr 25, 2021
AlexRouSg

AlexRouSg commented on Apr 25, 2021

@AlexRouSg
Contributor

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?

aodhneine

aodhneine commented on Apr 26, 2021

@aodhneine
Author

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.

cherrymui

cherrymui commented on Apr 26, 2021

@cherrymui
Member

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

added
WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.
on Apr 26, 2021
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.[/+] on Apr 26, 2021
aodhneine

aodhneine commented on Apr 26, 2021

@aodhneine
Author

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

AlexRouSg

AlexRouSg commented on Apr 26, 2021

@AlexRouSg
Contributor

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.

aodhneine

aodhneine commented on Apr 26, 2021

@aodhneine
Author

As I previously wrote, it returns the same error.

AlexRouSg

AlexRouSg commented on Apr 26, 2021

@AlexRouSg
Contributor

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

cherrymui

cherrymui commented on Apr 26, 2021

@cherrymui
Member

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

cherrymui

cherrymui commented on Apr 26, 2021

@cherrymui
Member
aodhneine

aodhneine commented on Apr 26, 2021

@aodhneine
Author

... 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
AlexRouSg

AlexRouSg commented on Apr 26, 2021

@AlexRouSg
Contributor

Did you change from WSL1 to WSL2?

aodhneine

aodhneine commented on Apr 26, 2021

@aodhneine
Author

No, it was WSL2 before too.

alexbrainman

alexbrainman commented on Apr 28, 2021

@alexbrainman
Member

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

rayjcwu

rayjcwu commented on May 10, 2021

@rayjcwu
Contributor

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

AlexRouSg

AlexRouSg commented on May 10, 2021

@AlexRouSg
Contributor

@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.

rayjcwu

rayjcwu commented on May 10, 2021

@rayjcwu
Contributor

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.

AlexRouSg

AlexRouSg commented on May 10, 2021

@AlexRouSg
Contributor

@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

rayjcwu

rayjcwu commented on May 10, 2021

@rayjcwu
Contributor

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.

seankhliao

seankhliao commented on Jun 12, 2022

@seankhliao
Member

Looks like there is no bug here

locked and limited conversation to collaborators on Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeOS-WindowsWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rayjcwu@AlexRouSg@gopherbot@alexbrainman@seankhliao

        Issue actions

          cmd/go: fails with `go: cannot find GOROOT directory` after cross-compiling from WSL to Windows. · Issue #45763 · golang/go