Skip to content

runtime: cgo can't set new environment variables for Go #44108

Description

@rakeshlacework

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

$ go version
go version go1.15.6 linux/amd64

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/works/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user/works"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go-1.15.6"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go-1.15.6/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-build987882216=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. Set environment variable before invoking the executable
  2. Overwrite and add new environment variable in cgo constructor
  3. print them in main

Here is the source code (also on playground does not work https://play.golang.org/p/knyUM81kREi)

vm: 17:11:38 ~/go/src/clear_env$ cat main.go
package main
/*
#cgo CFLAGS: -Werror -I/usr/include
#cgo LDFLAGS: -L/usr/lib64/ -L/usr/lib/x86_64-linux-gnu/ -ldl
#include <stdio.h>
#include <stdlib.h>
__attribute__((constructor)) void setup_env(void) {
	clearenv();
	setenv("EXISTING_VAR", "newValue", 1);
	setenv("NEW_VAR", "newValue", 1);
}
void print_env(void) {
	printf("C> PATH='%s'\n", getenv("PATH"));
	printf("C> EXISTING_VAR='%s'\n", getenv("EXISTING_VAR"));
	printf("C> NEW_VAR='%s'\n", getenv("NEW_VAR"));
}
*/
import "C"
import (
	"fmt"
	"os"
)
func main() {
	fmt.Printf("GO> PATH='%v'\n", os.Getenv("PATH"))
	fmt.Printf("GO> EXISTING_VAR='%v'\n", os.Getenv("EXISTING_VAR"))
	fmt.Printf("GO> NEW_VAR='%v'\n", os.Getenv("NEW_VAR"))
	C.print_env()
}

What did you expect to see?

vm: 17:11:42 ~/go/src/clear_env$ EXISTING_VAR="oldValue" ./main
GO> PATH='/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin'
GO> EXISTING_VAR='newValue'
GO> NEW_VAR='newValue'
C> PATH='/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin'
C> EXISTING_VAR='newValue'
C> NEW_VAR='newValue'

What did you see instead?

vm: 17:11:42 ~/go/src/clear_env$ EXISTING_VAR="oldValue" ./main
GO> PATH='/home/user/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin'
GO> EXISTING_VAR='oldValue'
GO> NEW_VAR=''
C> PATH='(null)'
C> EXISTING_VAR='newValue'
C> NEW_VAR='newValue'

Activity

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

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions