Source file src/cmd/go/internal/base/env.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package base
     6  
     7  import (
     8  	"cmd/go/internal/cfg"
     9  	"fmt"
    10  	"os"
    11  	"path/filepath"
    12  	"runtime"
    13  )
    14  
    15  // AppendPWD returns the result of appending PWD=dir to the environment base.
    16  //
    17  // The resulting environment makes os.Getwd more efficient for a subprocess
    18  // running in dir, and also improves the accuracy of paths relative to dir
    19  // if one or more elements of dir is a symlink.
    20  func AppendPWD(base []string, dir string) []string {
    21  	// POSIX requires PWD to be absolute.
    22  	// Internally we only use absolute paths, so dir should already be absolute.
    23  	if !filepath.IsAbs(dir) {
    24  		panic(fmt.Sprintf("AppendPWD with relative path %q", dir))
    25  	}
    26  	return append(base, "PWD="+dir)
    27  }
    28  
    29  // AppendPATH returns the result of appending PATH=$GOROOT/bin:$PATH
    30  // (or the platform equivalent) to the environment base.
    31  func AppendPATH(base []string) []string {
    32  	if cfg.GOROOTbin == "" {
    33  		return base
    34  	}
    35  
    36  	pathVar := "PATH"
    37  	if runtime.GOOS == "plan9" {
    38  		pathVar = "path"
    39  	}
    40  
    41  	path := os.Getenv(pathVar)
    42  	if path == "" {
    43  		return append(base, pathVar+"="+cfg.GOROOTbin)
    44  	}
    45  	return append(base, pathVar+"="+cfg.GOROOTbin+string(os.PathListSeparator)+path)
    46  }
    47  

View as plain text