github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/base/env.go (about)

     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  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"runtime"
    12  
    13  	"github.com/go-asm/go/cmd/go/cfg"
    14  )
    15  
    16  // AppendPWD returns the result of appending PWD=dir to the environment base.
    17  //
    18  // The resulting environment makes os.Getwd more efficient for a subprocess
    19  // running in dir, and also improves the accuracy of paths relative to dir
    20  // if one or more elements of dir is a symlink.
    21  func AppendPWD(base []string, dir string) []string {
    22  	// POSIX requires PWD to be absolute.
    23  	// Internally we only use absolute paths, so dir should already be absolute.
    24  	if !filepath.IsAbs(dir) {
    25  		panic(fmt.Sprintf("AppendPWD with relative path %q", dir))
    26  	}
    27  	return append(base, "PWD="+dir)
    28  }
    29  
    30  // AppendPATH returns the result of appending PATH=$GOROOT/bin:$PATH
    31  // (or the platform equivalent) to the environment base.
    32  func AppendPATH(base []string) []string {
    33  	if cfg.GOROOTbin == "" {
    34  		return base
    35  	}
    36  
    37  	pathVar := "PATH"
    38  	if runtime.GOOS == "plan9" {
    39  		pathVar = "path"
    40  	}
    41  
    42  	path := os.Getenv(pathVar)
    43  	if path == "" {
    44  		return append(base, pathVar+"="+cfg.GOROOTbin)
    45  	}
    46  	return append(base, pathVar+"="+cfg.GOROOTbin+string(os.PathListSeparator)+path)
    47  }