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

os: ExpandEnv: curly brackets not matched correctly #41208

Closed
sding3 opened this issue Sep 3, 2020 · 3 comments
Closed

os: ExpandEnv: curly brackets not matched correctly #41208

sding3 opened this issue Sep 3, 2020 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@sding3
Copy link
Contributor

sding3 commented Sep 3, 2020

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

go version go1.14.4 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/shang/.cache/go-build"
GOENV="/home/shang/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/shang/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/shang/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"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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-build728725462=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Ran the following code:

package main

import (
	"fmt"
	"os"
)

func main() {
	s := `${${foo}}`
	handleError(os.Setenv("HOME", "~"))
	handleError(os.Setenv("foo", "HOME"))
	handleError(os.Setenv("{foo}", "bar"))
	handleError(os.Setenv("{${foo}}", "baz"))
	fmt.Println(os.ExpandEnv(s))
}

func handleError(e error) {
	if e != nil {
		panic(e)
	}
}

What did you expect to see?

I expect to see any one of ~,${HOME},${bar}, the empty string, or baz

What did you see instead?

}


Slightly related: #35752

@sding3 sding3 changed the title os: ExpandEnv: curly bracket not matched correctly os: ExpandEnv: curly brackets not matched correctly Sep 3, 2020
@dmitshur
Copy link
Contributor

dmitshur commented Sep 3, 2020

/cc @ianlancetaylor @bradfitz per owners.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 3, 2020
@dmitshur dmitshur added this to the Backlog milestone Sep 3, 2020
@ianlancetaylor
Copy link
Contributor

This is working as documented. os.ExpandEnv is not the shell, it's just a simple one level expander. In the string ${${foo}}, it replaces ${var} with the value of var in the environment. In this case var is ${foo. That is the string that appears from ${ to the next }. That string has no value in the environment, so it is replaced with the empty string. That leaves just the trailing }, which is what you see.

@vatine
Copy link

vatine commented Sep 13, 2020

This is also (effectively) what bash does:

test$ echo $foo
bar
test$ echo $bar
gazonk
test$ echo ${${foo}}
bash: ${${foo}}: bad substitution

@golang golang locked and limited conversation to collaborators Sep 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants