]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: set $PWD when running commands
authorRuss Cox <rsc@golang.org>
Thu, 14 Feb 2013 19:21:44 +0000 (14:21 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 14 Feb 2013 19:21:44 +0000 (14:21 -0500)
This makes os.Getwd inside those commands much faster.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/7324055

src/cmd/go/build.go
src/cmd/go/main.go
src/cmd/go/test.go
src/cmd/go/vcs.go

index 126bb465e4ed4d34301c240eda320fd5fb2a5897..e7c9e696695714177b02e9157d659f060a18d6b4 100644 (file)
@@ -1226,7 +1226,7 @@ func (b *builder) runOut(dir string, desc string, cmdargs ...interface{}) ([]byt
                cmd.Stdout = &buf
                cmd.Stderr = &buf
                cmd.Dir = dir
-               // TODO: cmd.Env
+               cmd.Env = envForDir(cmd.Dir)
                err := cmd.Run()
 
                // cmd.Run will fail on Unix if some other process has the binary
index bd5d889711ffb014b89ed30723b8552748e37b20..10513d723543903bf5b4b69c0b3a1cc42eb672d3 100644 (file)
@@ -379,6 +379,25 @@ func runOut(dir string, cmdargs ...interface{}) []byte {
        return out
 }
 
+// envForDir returns a copy of the environment
+// suitable for running in the given directory.
+// The environment is the current process's environment
+// but with an updated $PWD, so that an os.Getwd in the
+// child will be faster.
+func envForDir(dir string) []string {
+       env := os.Environ()
+       for i, kv := range env {
+               if strings.HasPrefix(kv, "PWD=") {
+                       env[i] = "PWD=" + dir
+                       return env
+               }
+       }
+       // Internally we only use rooted paths, so dir is rooted.
+       // Even if dir is not rooted, no harm done.
+       env = append(env, "PWD="+dir)
+       return env
+}
+
 // matchPattern(pattern)(name) reports whether
 // name matches pattern.  Pattern is a limited glob
 // pattern in which '...' means 'any string' and there
index 9a8e11d27c1592dd256dedb25f30996a087157d4..3132ab2100f1acb6c7c39159aebf02a478fd2bde 100644 (file)
@@ -635,6 +635,7 @@ func (b *builder) runTest(a *action) error {
 
        cmd := exec.Command(args[0], args[1:]...)
        cmd.Dir = a.p.Dir
+       cmd.Env = envForDir(cmd.Dir)
        var buf bytes.Buffer
        if testStreamOutput {
                cmd.Stdout = os.Stdout
@@ -647,7 +648,7 @@ func (b *builder) runTest(a *action) error {
        // If there are any local SWIG dependencies, we want to load
        // the shared library from the build directory.
        if a.p.usesSwig() {
-               env := os.Environ()
+               env := cmd.Env
                found := false
                prefix := "LD_LIBRARY_PATH="
                for i, v := range env {
index 8042935b9dc2ac2dd6334a53c87fa05ce7d17b6b..b995794416b877b289282d6eb9ecd81995baa95f 100644 (file)
@@ -190,6 +190,7 @@ func (v *vcsCmd) run1(dir string, cmdline string, keyval []string, verbose bool)
 
        cmd := exec.Command(v.cmd, args...)
        cmd.Dir = dir
+       cmd.Env = envForDir(cmd.Dir)
        if buildX {
                fmt.Printf("cd %s\n", dir)
                fmt.Printf("%s %s\n", v.cmd, strings.Join(args, " "))