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: build sets PWD to relative path #46832

Open
trofi opened this issue Jun 19, 2021 · 3 comments
Open

cmd/go: build sets PWD to relative path #46832

trofi opened this issue Jun 19, 2021 · 3 comments
Labels
Milestone

Comments

@trofi
Copy link

@trofi trofi commented Jun 19, 2021

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

$ go version
go version go1.16.5 linux/amd64

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

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/slyfox/.cache/go-build"
GOENV="/home/slyfox/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/slyfox/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/slyfox/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="x86_64-pc-linux-gnu-gcc"
CXX="x86_64-pc-linux-gnu-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-build2541492012=/tmp/go-build -gno-record-gcc-switches"

What did you do?

This is a forward of downstream ccache/ccache#860 bug where ccache was confused by PWD=. set by go build. In ccache/ccache#860 (comment) Joel Rosdahl notes that PWD should be an absolute path.

ccache is fixed since but I think it's worth fixing go build not to produce relative paths.

Executable single-file example:

Here is a directly executable example to illustrate the problem extracted from docker project:

#!/bin/bash

CC=gcc

# cleanup
rm -rf -- temp bug ccache-wrappers .cache

# create test files:
## simple go.mod:
cat > go.mod <<EOF
module github.com/PWD-bug

go 1.13
EOF

## simple main.go:
cat > main.go <<EOF
package main

import "C"

func main() {}
EOF

# create local ccache wrapper for $CC
mkdir -p ccache-wrappers
ln -s /usr/bin/ccache ccache-wrappers/${CC}

# create temp dir
mkdir -p temp

env -i \
  "CC=$CC" \
  "HOME=$(pwd)" \
  "TMPDIR=$(pwd)/temp" \
  "CCACHE_BASEDIR=$(pwd)" \
  "PATH=$(pwd)/ccache-wrappers:/usr/bin:/bin" \
go build -o bug .

What did you expect to see?

Expected the test to build successfully.

What did you see instead?

The ccache detects relative path in PWD and crashes:

$ ./bug.bash
# github.com/PWD-bug
/usr/lib/go/pkg/tool/linux_amd64/link: running gcc failed: signal: aborted (core dumped)
ccache: Util.cpp:731: std::string Util::get_relative_path(nonstd::sv_lite::string_view, nonstd::sv_lite::string_view): failed assertion: Util::is_absolute_path(dir)
@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Jun 21, 2021

@LeGEC
Copy link

@LeGEC LeGEC commented Jun 21, 2021

FWIW : the failing shell script works on my machine. I run :

  • Ubuntu 20.04
  • go version go1.16.5 linux/amd64
  • gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
  • an invalid ccache directory (for some reason, I don't have a /usr/bin/ccache on my machine, the link created by your script points to an invalid directory)

(note: I don't know if any of this is relevant to your issue.)

Can you check what $PWD value you get from within your script ?

replacing go build -o bug . by :

  • env, I see only the 5 entries explicitly set (no PWD variable in the environment)
  • pwd, I do get the current directory I expect to see (an absolute path, not .)

@jayconrod jayconrod added this to the Backlog milestone Jun 21, 2021
@jayconrod
Copy link
Contributor

@jayconrod jayconrod commented Jun 21, 2021

Looked into this a bit. I was able to reproduce the failure on a Debian machine. Adding a panic on non-absolute paths to base.AppendPWD caused problems very quickly. There are cases where we pass in "" and ".".

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

Successfully merging a pull request may close this issue.

None yet
4 participants