Correctly add GOROOT to empty subprocess environments

The way I was forcing GOROOT wound up clearing the execution environment
of subprocesses when the caller intended to simply re-use the current
execution environment. This path correctly adds GOROOT in all cases.
This commit is contained in:
Chris Masone
2015-12-05 16:18:03 -08:00
parent 986a7dd491
commit 2fd0d57c16
2 changed files with 7 additions and 6 deletions
+1 -4
View File
@@ -15,10 +15,7 @@ func main() {
d.Chk.NoError(err)
fileutil.ForceSymlink("../../js/.babelrc", path)
path, err = filepath.Abs("link.sh")
d.Chk.NoError(err)
runner.ForceRun(path)
runner.ForceRun("./link.sh")
runner.ForceRun("npm", "install")
if _, present := os.LookupEnv("NOMS_SERVER"); !present {
os.Setenv("NOMS_SERVER", "http://localhost:8000")
+6 -2
View File
@@ -18,7 +18,11 @@ func (e Env) toStrings() (out []string) {
for n, v := range e {
out = append(out, fmt.Sprintf("%s=%s", n, v))
}
return
if out == nil {
// Sadly, it seems like we need to force-set GOROOT in the environment, which means that we need to manually copy out the current environment and add to it instead of just returning nil and allowing the exec library to take its course.
out = os.Environ()
}
return append(out, "GOROOT="+runtime.GOROOT())
}
// ForceRun runs 'exe [args...]' in current working directory, and d.Chk()s on failure. Inherits the environment of the current process.
@@ -31,7 +35,7 @@ func ForceRun(exe string, args ...string) {
func runEnvDir(out, err io.Writer, env Env, dir, exe string, args ...string) error {
cmd := exec.Command(exe, args...)
cmd.Dir = dir
cmd.Env = append(env.toStrings(), "GOROOT="+runtime.GOROOT())
cmd.Env = env.toStrings()
cmd.Stdout = out
cmd.Stderr = err
return cmd.Run()