1
0
mirror of https://github.com/golang/go synced 2024-11-26 08:48:13 -07:00

cmd/go: set $PWD when running commands

This makes os.Getwd inside those commands much faster.

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/7324055
This commit is contained in:
Russ Cox 2013-02-14 14:21:44 -05:00
parent 551f3f27aa
commit 357a18a2c6
4 changed files with 23 additions and 2 deletions

View 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

View 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

View 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 {

View 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, " "))