Skip to content

os/exec: Cmd.Args not passed to the command as environmental variables #62429

@coder-with-a-bushido

Description

@coder-with-a-bushido

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

$ go version go1.20.4 darwin/arm64

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="arm64"
GOBIN=""
GOCACHE="/Users/karthikeyan_s/Library/Caches/go-build"
GOENV="/Users/karthikeyan_s/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/karthikeyan_s/go/pkg/mod"
GONOPROXY="github.com"
GONOSUMDB="github.com"
GOOS="darwin"
GOPATH="/Users/karthikeyan_s/go"
GOPRIVATE="github.com"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/wn/dhtfqqk17y95h22m4lh2_n780000gn/T/go-build605004963=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I was going through the ffmpeg docs and found a way to create a log file by setting an env variable FFREPORT. Initially, I couldn't find any doc on how to set a command specific environment variable in golang(This is important for me because I run multiple ffmpeg commands inside the program and want different env vars for them). Then, I found this SO answer and tried it out.

What did you expect to see?

The env variable FFREPORT being recognised and used by the ffmpeg command.

What did you see instead?

It didn't look like the env var was being read.

The command ran fine, but it didn't look like ffmpeg recognized the env var FFREPORT I set. So, I tried to run a simple shell script to check if it was working fine and I found this. The env vars set before calling the Cmd.Start aren't actually present (or accessible) to the command being run.

Reproduce the issue

You can easily reproduce this issue with a simple shell script:

#!/usr/bin/env bash
echo ${KEY1}
echo ${KEY2}

printenv

Now, try running this sh script from this go program on playground locally.

We set 2 env vars, KEY1 and KEY2 in the go program and ideally, you should see the output:

val1
val2
// then, all the env vars

But instead, the output you will see will be 2 empty lines and then all the env vars. And you won't find those 2 env vars set from the program in them anywhere.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions