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: setting the User or System Environment Variables #30190

Closed
akhilravuri1 opened this issue Feb 12, 2019 · 12 comments
Closed

cmd/go: setting the User or System Environment Variables #30190

akhilravuri1 opened this issue Feb 12, 2019 · 12 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@akhilravuri1
Copy link

akhilravuri1 commented Feb 12, 2019

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

$ go version
go version go1.10.3 windows/amd64

Does this issue reproduce with the latest release?

NO

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

go env Output
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\rakhil\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\rakhil\go
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\rakhil\AppData\Local\Temp\go-build179340394=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
"fmt"
"os"
"path"
)

func main(){
a := os.Getenv("GOPATH")
base := path.Join(a,"/src/github.com/ibmdb/go_ibm_db")
fmt.Println(base)
os.Setenv("CGO_LDFLAGS",base)
fmt.Println(os.Getenv("CGO_LDFLAGS"))
}

What did you expect to see?

CGO_LDFLAGS will be updated.

What did you see instead?

Nothing has been changed in that Env Variable

@agnivade
Copy link
Contributor

Please tell us what you did exactly.

@akhilravuri1
Copy link
Author

I was trying to set the environment variables from the Go program.

@bcmills
Copy link
Contributor

bcmills commented Feb 12, 2019

Please provide a concrete list of commands that we can run to reproduce the problem, along with the output you observed from those commands.

@bcmills bcmills changed the title Setting the User or System Environment Variables. cmd/go: setting the User or System Environment Variables Feb 12, 2019
@bcmills bcmills added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Feb 12, 2019
@akhilravuri1
Copy link
Author

package main

import (
"fmt"
"os"
"path"
)

func main(){
a := os.Getenv("GOPATH")
base := path.Join(a,"/src/github.com/ibmdb/go_ibm_db")
fmt.Println(base)
os.Setenv("CGO_LDFLAGS",base)
fmt.Println(os.Getenv("CGO_LDFLAGS"))
}

@agnivade
Copy link
Contributor

Can you show us the output you are getting ? I am failing to reproduce the issue here.

@akhilravuri1
Copy link
Author

The code works fine. But when I check for the CGO_LDFLAGS in user or system variables, I did not find it.

@AlexRouSg
Copy link
Contributor

os.Setenv does not set the user or system variables. It only sets the variables for the current process/program.

If you want to set the user or system variables you need admin permissions and you need the Windows API.

@akhilravuri1
Copy link
Author

Can we set it from exec.Command() function?

@akhilravuri1
Copy link
Author

I have created a package when the user does go get command, I want a go file to run automatically so that it can set environmental variables.

Can we run a file in golang when the user downloads the package?

@AlexRouSg
Copy link
Contributor

Can we set it from exec.Command() function?

You can try the setx command but you still need admin permissions.
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/setx

As this is not a bug, I suggest you ask for further help in one of the places listed in https://github.com/golang/go/wiki/Questions

Or maybe even https://social.msdn.microsoft.com/Forums/en-US/home

Can we run a file in golang when the user downloads the package?

No, if you want that you need to make your own downloader/installer

@akhilravuri1
Copy link
Author

akhilravuri1 commented Feb 13, 2019

If we create a package in node we can do that if the user downloads a package using npm. (Run a file after package gets downloaded)

Is it a backlog in golang?

@agnivade
Copy link
Contributor

Is it a backlog in golang?

This was raised before and closed. Please see #15536.

Specifically this comment -

We don't want arbitrary command to run when you simply go get a package (even go build doesn't run arbitrary commands.)

If you think you have a use case which might be beneficial to others, please feel free to open a proposal issue laying out the requirements and your proposed outline. Please note that there are other places too where Go differs from npm style package install. For eg- there is no concept of devDependencies in a go.mod and that is intentional because we do not want to make the developer learn more concepts. The recommended approach for that is https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module.

It sounds like the current issue is resolved anyways. So closing this.

@golang golang locked and limited conversation to collaborators Feb 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

5 participants