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: document that empty environment variables are treated as unset #50335

Closed
vikyd opened this issue Dec 24, 2021 · 8 comments
Closed

cmd/go: document that empty environment variables are treated as unset #50335

vikyd opened this issue Dec 24, 2021 · 8 comments
Labels
Documentation GoCommand cmd/go help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@vikyd
Copy link

vikyd commented Dec 24, 2021

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

$ go version
go version go1.17.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/fault/.cache/go-build"
GOENV="/home/fault/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/data/gopath/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/data/gopath"
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.17.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="my/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 -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3814949289=/tmp/go-build -gno-record-gcc-switches"

What did you do?

~ export GOPRIVATE=aaa   
➜  ~ go env -w GOPRIVATE=bbb
warning: go env -w GOPRIVATE=... does not override conflicting OS environment variable
➜  ~ export GOPRIVATE=  
➜  ~ go env GOPRIVATE       
bbb

What did you expect to see?

Expect go env GOPRIVATE print nothing. Because GOPRIVATE is exist, and its value is empty.

an environment variable is set with empty value vs an environment variable is not exist

  • Go should use values from GOENV only if an environment variable is not exist.
  • Go should not use values from GOENV when an environment variable is set with empty value.
    • this behavior is the same as the shell

What did you see instead?

go env GOPRIVATE print bbb.

Suggestion

Relative code: https://github.com/golang/go/blob/go1.17.5/src/cmd/go/internal/cfg/cfg.go#L239-L242

	val := os.Getenv(key)
	if val != "" {
		return val
	}

Should be:

	val, present := os.LookupEnv(key)
	if present {
		return val
	}
@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 28, 2021
@thanm
Copy link
Contributor

thanm commented Dec 28, 2021

@bcmills @matloob per owners.

@LiangZeFenglzf
Copy link

how can i unset goprivate

@bcmills bcmills added the GoCommand cmd/go label Apr 8, 2022
@bcmills bcmills added this to the Backlog milestone Apr 8, 2022
@bcmills
Copy link
Contributor

bcmills commented Apr 8, 2022

You can use go env -u to permanently unset a setting written with go env -w.

The Go environment variables are intended to be structured such that there is always some valid non-empty string you can use to get the default behavior. For GOPRIVATE, you could set something like GOPRIVATE=none, which wouldn't match any actual modules.

@LiangZeFenglzf
Copy link

You can use go env -u to permanently unset a setting written with go env -w.

The Go environment variables are intended to be structured such that there is always some valid non-empty string you can use to get the default behavior. For GOPRIVATE, you could set something like GOPRIVATE=none, which wouldn't match any actual modules.
steps belows:

  1. go env
in local terminal,i see :
set GOPRIVATE=five.com/library.git
set GOENV=C:\Users\001\AppData\Roaming\go\env

2.go env -w GOPRIVATE=none
teminal says:

warning: go env -w GOPRIVATE=... does not override conflicting OS environment variable

watch in GOENV=C:\Users\001\AppData\Roaming\go\env

GO111MODULE=auto
GOPRIVATE=none
GOPROXY=https://goproxy.cn

go env found:

set GOPRIVATE=five.com/library.git
  1. go env -u GOPRIVATE
watch in GOENV=C:\Users\001\AppData\Roaming\go\env
GO111MODULE=auto
GOPROXY=https://goproxy.cn
watch env goprivate disappears,but go env found:
set GOPRIVATE=five.com/library.git
it still exist.

@LiangZeFenglzf
Copy link

You can use go env -u to permanently unset a setting written with go env -w.

The Go environment variables are intended to be structured such that there is always some valid non-empty string you can use to get the default behavior. For GOPRIVATE, you could set something like GOPRIVATE=none, which wouldn't match any actual modules.

Dear bcmills,I slove it.In my OS Envrionment,i see it,and then delete it.It succeses!

@bcmills
Copy link
Contributor

bcmills commented Apr 12, 2022

The current behavior of treating the empty string in the process environment as equivalent to unset is intentional.

And from what I can tell the behavior you are seeing with go env -w and set is consistent with that: if you set the variable to a non-empty value in both the GOENV file and the process environment, the environment wins; if you set it in the GOENV file but not the process environment, then the value from the GOENV file is used.

However, looking at https://pkg.go.dev/cmd/go#hdr-Environment_variables, I see:

If an environment variable is unset, the go command uses a sensible default setting.

That sentence should be updated to say “unset or empty” instead of “unset”.

@bcmills bcmills changed the title cmd/go/internal/cfg/cfg.go: environment variable with empty value vs unset cmd/go: document that empty environment variables are treated as unset Apr 12, 2022
@bcmills bcmills added NeedsFix The path to resolution is known, but the work has not been done. help wanted labels Apr 12, 2022
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 12, 2022
@LiangZeFenglzf
Copy link

LiangZeFenglzf commented Apr 14, 2022 via email

@gopherbot
Copy link

Change https://go.dev/cl/475695 mentions this issue: cmd/go: update help for empty environment variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation GoCommand cmd/go help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants