Source file src/cmd/go/internal/work/shell.go

     1  // Copyright 2023 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 work
     6  
     7  import (
     8  	"bytes"
     9  	"cmd/go/internal/base"
    10  	"cmd/go/internal/cache"
    11  	"cmd/go/internal/cfg"
    12  	"cmd/go/internal/load"
    13  	"cmd/go/internal/str"
    14  	"cmd/internal/par"
    15  	"cmd/internal/pathcache"
    16  	"errors"
    17  	"fmt"
    18  	"internal/lazyregexp"
    19  	"io"
    20  	"io/fs"
    21  	"os"
    22  	"os/exec"
    23  	"path/filepath"
    24  	"runtime"
    25  	"strconv"
    26  	"strings"
    27  	"sync"
    28  	"time"
    29  )
    30  
    31  // A Shell runs shell commands and performs shell-like file system operations.
    32  //
    33  // Shell tracks context related to running commands, and form a tree much like
    34  // context.Context.
    35  type Shell struct {
    36  	action       *Action // nil for the root shell
    37  	*shellShared         // per-Builder state shared across Shells
    38  }
    39  
    40  // shellShared is Shell state shared across all Shells derived from a single
    41  // root shell (generally a single Builder).
    42  type shellShared struct {
    43  	workDir string // $WORK, immutable
    44  
    45  	printLock sync.Mutex
    46  	printer   load.Printer
    47  	scriptDir string // current directory in printed script
    48  
    49  	mkdirCache par.Cache[string, error] // a cache of created directories
    50  }
    51  
    52  // NewShell returns a new Shell.
    53  //
    54  // Shell will internally serialize calls to the printer.
    55  // If printer is nil, it uses load.DefaultPrinter.
    56  func NewShell(workDir string, printer load.Printer) *Shell {
    57  	if printer == nil {
    58  		printer = load.DefaultPrinter()
    59  	}
    60  	shared := &shellShared{
    61  		workDir: workDir,
    62  		printer: printer,
    63  	}
    64  	return &Shell{shellShared: shared}
    65  }
    66  
    67  func (sh *Shell) pkg() *load.Package {
    68  	if sh.action == nil {
    69  		return nil
    70  	}
    71  	return sh.action.Package
    72  }
    73  
    74  // Printf emits a to this Shell's output stream, formatting it like fmt.Printf.
    75  // It is safe to call concurrently.
    76  func (sh *Shell) Printf(format string, a ...any) {
    77  	sh.printLock.Lock()
    78  	defer sh.printLock.Unlock()
    79  	sh.printer.Printf(sh.pkg(), format, a...)
    80  }
    81  
    82  func (sh *Shell) printfLocked(format string, a ...any) {
    83  	sh.printer.Printf(sh.pkg(), format, a...)
    84  }
    85  
    86  // Errorf reports an error on sh's package and sets the process exit status to 1.
    87  func (sh *Shell) Errorf(format string, a ...any) {
    88  	sh.printLock.Lock()
    89  	defer sh.printLock.Unlock()
    90  	sh.printer.Errorf(sh.pkg(), format, a...)
    91  }
    92  
    93  // WithAction returns a Shell identical to sh, but bound to Action a.
    94  func (sh *Shell) WithAction(a *Action) *Shell {
    95  	sh2 := *sh
    96  	sh2.action = a
    97  	return &sh2
    98  }
    99  
   100  // Shell returns a shell for running commands on behalf of Action a.
   101  func (b *Builder) Shell(a *Action) *Shell {
   102  	if a == nil {
   103  		// The root shell has a nil Action. The point of this method is to
   104  		// create a Shell bound to an Action, so disallow nil Actions here.
   105  		panic("nil Action")
   106  	}
   107  	if a.sh == nil {
   108  		a.sh = b.backgroundSh.WithAction(a)
   109  	}
   110  	return a.sh
   111  }
   112  
   113  // BackgroundShell returns a Builder-wide Shell that's not bound to any Action.
   114  // Try not to use this unless there's really no sensible Action available.
   115  func (b *Builder) BackgroundShell() *Shell {
   116  	return b.backgroundSh
   117  }
   118  
   119  // moveOrCopyFile is like 'mv src dst' or 'cp src dst'.
   120  func (sh *Shell) moveOrCopyFile(dst, src string, perm fs.FileMode, force bool) error {
   121  	if cfg.BuildN {
   122  		sh.ShowCmd("", "mv %s %s", src, dst)
   123  		return nil
   124  	}
   125  
   126  	err := checkDstOverwrite(dst, force)
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	// If we can update the mode and rename to the dst, do it.
   132  	// Otherwise fall back to standard copy.
   133  
   134  	// If the source is in the build cache, we need to copy it.
   135  	dir, _, _ := cache.DefaultDir()
   136  	if strings.HasPrefix(src, dir) {
   137  		return sh.CopyFile(dst, src, perm, force)
   138  	}
   139  
   140  	// On Windows, always copy the file, so that we respect the NTFS
   141  	// permissions of the parent folder. https://golang.org/issue/22343.
   142  	// What matters here is not cfg.Goos (the system we are building
   143  	// for) but runtime.GOOS (the system we are building on).
   144  	if runtime.GOOS == "windows" {
   145  		return sh.CopyFile(dst, src, perm, force)
   146  	}
   147  
   148  	// If the destination directory has the group sticky bit set,
   149  	// we have to copy the file to retain the correct permissions.
   150  	// https://golang.org/issue/18878
   151  	if fi, err := os.Stat(filepath.Dir(dst)); err == nil {
   152  		if fi.IsDir() && (fi.Mode()&fs.ModeSetgid) != 0 {
   153  			return sh.CopyFile(dst, src, perm, force)
   154  		}
   155  	}
   156  
   157  	// The perm argument is meant to be adjusted according to umask,
   158  	// but we don't know what the umask is.
   159  	// Create a dummy file to find out.
   160  	// This avoids build tags and works even on systems like Plan 9
   161  	// where the file mask computation incorporates other information.
   162  	mode := perm
   163  	f, err := os.OpenFile(filepath.Clean(dst)+"-go-tmp-umask", os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
   164  	if err == nil {
   165  		fi, err := f.Stat()
   166  		if err == nil {
   167  			mode = fi.Mode() & 0777
   168  		}
   169  		name := f.Name()
   170  		f.Close()
   171  		os.Remove(name)
   172  	}
   173  
   174  	if err := os.Chmod(src, mode); err == nil {
   175  		if err := os.Rename(src, dst); err == nil {
   176  			if cfg.BuildX {
   177  				sh.ShowCmd("", "mv %s %s", src, dst)
   178  			}
   179  			return nil
   180  		}
   181  	}
   182  
   183  	return sh.CopyFile(dst, src, perm, force)
   184  }
   185  
   186  // CopyFile is like 'cp src dst'.
   187  func (sh *Shell) CopyFile(dst, src string, perm fs.FileMode, force bool) error {
   188  	if cfg.BuildN || cfg.BuildX {
   189  		sh.ShowCmd("", "cp %s %s", src, dst)
   190  		if cfg.BuildN {
   191  			return nil
   192  		}
   193  	}
   194  
   195  	sf, err := os.Open(src)
   196  	if err != nil {
   197  		return err
   198  	}
   199  	defer sf.Close()
   200  
   201  	err = checkDstOverwrite(dst, force)
   202  	if err != nil {
   203  		return err
   204  	}
   205  
   206  	// On Windows, remove lingering ~ file from last attempt.
   207  	if runtime.GOOS == "windows" {
   208  		if _, err := os.Stat(dst + "~"); err == nil {
   209  			os.Remove(dst + "~")
   210  		}
   211  	}
   212  
   213  	mayberemovefile(dst)
   214  	df, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
   215  	if err != nil && runtime.GOOS == "windows" {
   216  		// Windows does not allow deletion of a binary file
   217  		// while it is executing. Try to move it out of the way.
   218  		// If the move fails, which is likely, we'll try again the
   219  		// next time we do an install of this binary.
   220  		if err := os.Rename(dst, dst+"~"); err == nil {
   221  			os.Remove(dst + "~")
   222  		}
   223  		df, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
   224  	}
   225  	if err != nil {
   226  		return fmt.Errorf("copying %s: %w", src, err) // err should already refer to dst
   227  	}
   228  
   229  	_, err = io.Copy(df, sf)
   230  	df.Close()
   231  	if err != nil {
   232  		mayberemovefile(dst)
   233  		return fmt.Errorf("copying %s to %s: %v", src, dst, err)
   234  	}
   235  	return nil
   236  }
   237  
   238  // mayberemovefile removes a file only if it is a regular file
   239  // When running as a user with sufficient privileges, we may delete
   240  // even device files, for example, which is not intended.
   241  func mayberemovefile(s string) {
   242  	if fi, err := os.Lstat(s); err == nil && !fi.Mode().IsRegular() {
   243  		return
   244  	}
   245  	os.Remove(s)
   246  }
   247  
   248  // Be careful about removing/overwriting dst.
   249  // Do not remove/overwrite if dst exists and is a directory
   250  // or a non-empty non-object file.
   251  func checkDstOverwrite(dst string, force bool) error {
   252  	if fi, err := os.Stat(dst); err == nil {
   253  		if fi.IsDir() {
   254  			return fmt.Errorf("build output %q already exists and is a directory", dst)
   255  		}
   256  		if !force && fi.Mode().IsRegular() && fi.Size() != 0 && !isObject(dst) {
   257  			return fmt.Errorf("build output %q already exists and is not an object file", dst)
   258  		}
   259  	}
   260  	return nil
   261  }
   262  
   263  // writeFile writes the text to file.
   264  func (sh *Shell) writeFile(file string, text []byte) error {
   265  	if cfg.BuildN || cfg.BuildX {
   266  		switch {
   267  		case len(text) == 0:
   268  			sh.ShowCmd("", "echo -n > %s # internal", file)
   269  		case bytes.IndexByte(text, '\n') == len(text)-1:
   270  			// One line. Use a simpler "echo" command.
   271  			sh.ShowCmd("", "echo '%s' > %s # internal", bytes.TrimSuffix(text, []byte("\n")), file)
   272  		default:
   273  			// Use the most general form.
   274  			sh.ShowCmd("", "cat >%s << 'EOF' # internal\n%sEOF", file, text)
   275  		}
   276  	}
   277  	if cfg.BuildN {
   278  		return nil
   279  	}
   280  	return os.WriteFile(file, text, 0666)
   281  }
   282  
   283  // Mkdir makes the named directory.
   284  func (sh *Shell) Mkdir(dir string) error {
   285  	// Make Mkdir(a.Objdir) a no-op instead of an error when a.Objdir == "".
   286  	if dir == "" {
   287  		return nil
   288  	}
   289  
   290  	// We can be a little aggressive about being
   291  	// sure directories exist. Skip repeated calls.
   292  	return sh.mkdirCache.Do(dir, func() error {
   293  		if cfg.BuildN || cfg.BuildX {
   294  			sh.ShowCmd("", "mkdir -p %s", dir)
   295  			if cfg.BuildN {
   296  				return nil
   297  			}
   298  		}
   299  
   300  		return os.MkdirAll(dir, 0777)
   301  	})
   302  }
   303  
   304  // RemoveAll is like 'rm -rf'. It attempts to remove all paths even if there's
   305  // an error, and returns the first error.
   306  func (sh *Shell) RemoveAll(paths ...string) error {
   307  	if cfg.BuildN || cfg.BuildX {
   308  		// Don't say we are removing the directory if we never created it.
   309  		show := func() bool {
   310  			for _, path := range paths {
   311  				if _, ok := sh.mkdirCache.Get(path); ok {
   312  					return true
   313  				}
   314  				if _, err := os.Stat(path); !os.IsNotExist(err) {
   315  					return true
   316  				}
   317  			}
   318  			return false
   319  		}
   320  		if show() {
   321  			sh.ShowCmd("", "rm -rf %s", strings.Join(paths, " "))
   322  		}
   323  	}
   324  	if cfg.BuildN {
   325  		return nil
   326  	}
   327  
   328  	var err error
   329  	for _, path := range paths {
   330  		if err2 := os.RemoveAll(path); err2 != nil && err == nil {
   331  			err = err2
   332  		}
   333  	}
   334  	return err
   335  }
   336  
   337  // Symlink creates a symlink newname -> oldname.
   338  func (sh *Shell) Symlink(oldname, newname string) error {
   339  	// It's not an error to try to recreate an existing symlink.
   340  	if link, err := os.Readlink(newname); err == nil && link == oldname {
   341  		return nil
   342  	}
   343  
   344  	if cfg.BuildN || cfg.BuildX {
   345  		sh.ShowCmd("", "ln -s %s %s", oldname, newname)
   346  		if cfg.BuildN {
   347  			return nil
   348  		}
   349  	}
   350  	return os.Symlink(oldname, newname)
   351  }
   352  
   353  // fmtCmd formats a command in the manner of fmt.Sprintf but also:
   354  //
   355  //	fmtCmd replaces the value of b.WorkDir with $WORK.
   356  func (sh *Shell) fmtCmd(dir string, format string, args ...any) string {
   357  	cmd := fmt.Sprintf(format, args...)
   358  	if sh.workDir != "" && !strings.HasPrefix(cmd, "cat ") {
   359  		cmd = strings.ReplaceAll(cmd, sh.workDir, "$WORK")
   360  		escaped := strconv.Quote(sh.workDir)
   361  		escaped = escaped[1 : len(escaped)-1] // strip quote characters
   362  		if escaped != sh.workDir {
   363  			cmd = strings.ReplaceAll(cmd, escaped, "$WORK")
   364  		}
   365  	}
   366  	return cmd
   367  }
   368  
   369  // ShowCmd prints the given command to standard output
   370  // for the implementation of -n or -x.
   371  //
   372  // ShowCmd also replaces the name of the current script directory with dot (.)
   373  // but only when it is at the beginning of a space-separated token.
   374  //
   375  // If dir is not "" or "/" and not the current script directory, ShowCmd first
   376  // prints a "cd" command to switch to dir and updates the script directory.
   377  func (sh *Shell) ShowCmd(dir string, format string, args ...any) {
   378  	// Use the output lock directly so we can manage scriptDir.
   379  	sh.printLock.Lock()
   380  	defer sh.printLock.Unlock()
   381  
   382  	cmd := sh.fmtCmd(dir, format, args...)
   383  
   384  	if dir != "" && dir != "/" {
   385  		if dir != sh.scriptDir {
   386  			// Show changing to dir and update the current directory.
   387  			sh.printfLocked("%s", sh.fmtCmd("", "cd %s\n", dir))
   388  			sh.scriptDir = dir
   389  		}
   390  		// Replace scriptDir is our working directory. Replace it
   391  		// with "." in the command.
   392  		dot := " ."
   393  		if dir[len(dir)-1] == filepath.Separator {
   394  			dot += string(filepath.Separator)
   395  		}
   396  		cmd = strings.ReplaceAll(" "+cmd, " "+dir, dot)[1:]
   397  	}
   398  
   399  	sh.printfLocked("%s\n", cmd)
   400  }
   401  
   402  // reportCmd reports the output and exit status of a command. The cmdOut and
   403  // cmdErr arguments are the output and exit error of the command, respectively.
   404  //
   405  // The exact reporting behavior is as follows:
   406  //
   407  //	cmdOut  cmdErr  Result
   408  //	""      nil     print nothing, return nil
   409  //	!=""    nil     print output, return nil
   410  //	""      !=nil   print nothing, return cmdErr (later printed)
   411  //	!=""    !=nil   print nothing, ignore err, return output as error (later printed)
   412  //
   413  // reportCmd returns a non-nil error if and only if cmdErr != nil. It assumes
   414  // that the command output, if non-empty, is more detailed than the command
   415  // error (which is usually just an exit status), so prefers using the output as
   416  // the ultimate error. Typically, the caller should return this error from an
   417  // Action, which it will be printed by the Builder.
   418  //
   419  // reportCmd formats the output as "# desc" followed by the given output. The
   420  // output is expected to contain references to 'dir', usually the source
   421  // directory for the package that has failed to build. reportCmd rewrites
   422  // mentions of dir with a relative path to dir when the relative path is
   423  // shorter. This is usually more pleasant. For example, if fmt doesn't compile
   424  // and we are in src/html, the output is
   425  //
   426  //	$ go build
   427  //	# fmt
   428  //	../fmt/print.go:1090: undefined: asdf
   429  //	$
   430  //
   431  // instead of
   432  //
   433  //	$ go build
   434  //	# fmt
   435  //	/usr/gopher/go/src/fmt/print.go:1090: undefined: asdf
   436  //	$
   437  //
   438  // reportCmd also replaces references to the work directory with $WORK, replaces
   439  // cgo file paths with the original file path, and replaces cgo-mangled names
   440  // with "C.name".
   441  //
   442  // desc is optional. If "", a.Package.Desc() is used.
   443  //
   444  // dir is optional. If "", a.Package.Dir is used.
   445  func (sh *Shell) reportCmd(desc, dir string, cmdOut []byte, cmdErr error) error {
   446  	if len(cmdOut) == 0 && cmdErr == nil {
   447  		// Common case
   448  		return nil
   449  	}
   450  	if len(cmdOut) == 0 && cmdErr != nil {
   451  		// Just return the error.
   452  		//
   453  		// TODO: This is what we've done for a long time, but it may be a
   454  		// mistake because it loses all of the extra context and results in
   455  		// ultimately less descriptive output. We should probably just take the
   456  		// text of cmdErr as the output in this case and do everything we
   457  		// otherwise would. We could chain the errors if we feel like it.
   458  		return cmdErr
   459  	}
   460  
   461  	// Fetch defaults from the package.
   462  	var p *load.Package
   463  	a := sh.action
   464  	if a != nil {
   465  		p = a.Package
   466  	}
   467  	var importPath string
   468  	if p != nil {
   469  		importPath = p.ImportPath
   470  		if desc == "" {
   471  			desc = p.Desc()
   472  		}
   473  		if dir == "" {
   474  			dir = p.Dir
   475  		}
   476  	}
   477  
   478  	out := string(cmdOut)
   479  
   480  	if !strings.HasSuffix(out, "\n") {
   481  		out = out + "\n"
   482  	}
   483  
   484  	// Replace workDir with $WORK
   485  	out = replacePrefix(out, sh.workDir, "$WORK")
   486  
   487  	// Rewrite mentions of dir with a relative path to dir
   488  	// when the relative path is shorter.
   489  	for {
   490  		// Note that dir starts out long, something like
   491  		// /foo/bar/baz/root/a
   492  		// The target string to be reduced is something like
   493  		// (blah-blah-blah) /foo/bar/baz/root/sibling/whatever.go:blah:blah
   494  		// /foo/bar/baz/root/a doesn't match /foo/bar/baz/root/sibling, but the prefix
   495  		// /foo/bar/baz/root does.  And there may be other niblings sharing shorter
   496  		// prefixes, the only way to find them is to look.
   497  		// This doesn't always produce a relative path --
   498  		// /foo is shorter than ../../.., for example.
   499  		if reldir := base.ShortPath(dir); reldir != dir {
   500  			out = replacePrefix(out, dir, reldir)
   501  			if filepath.Separator == '\\' {
   502  				// Don't know why, sometimes this comes out with slashes, not backslashes.
   503  				wdir := strings.ReplaceAll(dir, "\\", "/")
   504  				out = replacePrefix(out, wdir, reldir)
   505  			}
   506  		}
   507  		dirP := filepath.Dir(dir)
   508  		if dir == dirP {
   509  			break
   510  		}
   511  		dir = dirP
   512  	}
   513  
   514  	// Fix up output referring to cgo-generated code to be more readable.
   515  	// Replace x.go:19[/tmp/.../x.cgo1.go:18] with x.go:19.
   516  	// Replace *[100]_Ctype_foo with *[100]C.foo.
   517  	// If we're using -x, assume we're debugging and want the full dump, so disable the rewrite.
   518  	if !cfg.BuildX && cgoLine.MatchString(out) {
   519  		out = cgoLine.ReplaceAllString(out, "")
   520  		out = cgoTypeSigRe.ReplaceAllString(out, "C.")
   521  	}
   522  
   523  	// Usually desc is already p.Desc(), but if not, signal cmdError.Error to
   524  	// add a line explicitly mentioning the import path.
   525  	needsPath := importPath != "" && p != nil && desc != p.Desc()
   526  
   527  	err := &cmdError{desc, out, importPath, needsPath}
   528  	if cmdErr != nil {
   529  		// The command failed. Report the output up as an error.
   530  		return err
   531  	}
   532  	// The command didn't fail, so just print the output as appropriate.
   533  	if a != nil && a.output != nil {
   534  		// The Action is capturing output.
   535  		a.output = append(a.output, err.Error()...)
   536  	} else {
   537  		// Write directly to the Builder output.
   538  		sh.Printf("%s", err)
   539  	}
   540  	return nil
   541  }
   542  
   543  // replacePrefix is like strings.ReplaceAll, but only replaces instances of old
   544  // that are preceded by ' ', '\t', or appear at the beginning of a line.
   545  func replacePrefix(s, old, new string) string {
   546  	n := strings.Count(s, old)
   547  	if n == 0 {
   548  		return s
   549  	}
   550  
   551  	s = strings.ReplaceAll(s, " "+old, " "+new)
   552  	s = strings.ReplaceAll(s, "\n"+old, "\n"+new)
   553  	s = strings.ReplaceAll(s, "\n\t"+old, "\n\t"+new)
   554  	if strings.HasPrefix(s, old) {
   555  		s = new + s[len(old):]
   556  	}
   557  	return s
   558  }
   559  
   560  type cmdError struct {
   561  	desc       string
   562  	text       string
   563  	importPath string
   564  	needsPath  bool // Set if desc does not already include the import path
   565  }
   566  
   567  func (e *cmdError) Error() string {
   568  	var msg string
   569  	if e.needsPath {
   570  		// Ensure the import path is part of the message.
   571  		// Clearly distinguish the description from the import path.
   572  		msg = fmt.Sprintf("# %s\n# [%s]\n", e.importPath, e.desc)
   573  	} else {
   574  		msg = "# " + e.desc + "\n"
   575  	}
   576  	return msg + e.text
   577  }
   578  
   579  func (e *cmdError) ImportPath() string {
   580  	return e.importPath
   581  }
   582  
   583  var cgoLine = lazyregexp.New(`\[[^\[\]]+\.(cgo1|cover)\.go:[0-9]+(:[0-9]+)?\]`)
   584  var cgoTypeSigRe = lazyregexp.New(`\b_C2?(type|func|var|macro)_\B`)
   585  
   586  // run runs the command given by cmdline in the directory dir.
   587  // If the command fails, run prints information about the failure
   588  // and returns a non-nil error.
   589  func (sh *Shell) run(dir string, desc string, env []string, cmdargs ...any) error {
   590  	out, err := sh.runOut(dir, env, cmdargs...)
   591  	if desc == "" {
   592  		desc = sh.fmtCmd(dir, "%s", strings.Join(str.StringList(cmdargs...), " "))
   593  	}
   594  	return sh.reportCmd(desc, dir, out, err)
   595  }
   596  
   597  // runOut runs the command given by cmdline in the directory dir.
   598  // It returns the command output and any errors that occurred.
   599  // It accumulates execution time in a.
   600  func (sh *Shell) runOut(dir string, env []string, cmdargs ...any) ([]byte, error) {
   601  	a := sh.action
   602  
   603  	cmdline := str.StringList(cmdargs...)
   604  
   605  	for _, arg := range cmdline {
   606  		// GNU binutils commands, including gcc and gccgo, interpret an argument
   607  		// @foo anywhere in the command line (even following --) as meaning
   608  		// "read and insert arguments from the file named foo."
   609  		// Don't say anything that might be misinterpreted that way.
   610  		if strings.HasPrefix(arg, "@") {
   611  			return nil, fmt.Errorf("invalid command-line argument %s in command: %s", arg, joinUnambiguously(cmdline))
   612  		}
   613  	}
   614  
   615  	if cfg.BuildN || cfg.BuildX {
   616  		var envcmdline string
   617  		for _, e := range env {
   618  			if j := strings.IndexByte(e, '='); j != -1 {
   619  				if strings.ContainsRune(e[j+1:], '\'') {
   620  					envcmdline += fmt.Sprintf("%s=%q", e[:j], e[j+1:])
   621  				} else {
   622  					envcmdline += fmt.Sprintf("%s='%s'", e[:j], e[j+1:])
   623  				}
   624  				envcmdline += " "
   625  			}
   626  		}
   627  		envcmdline += joinUnambiguously(cmdline)
   628  		sh.ShowCmd(dir, "%s", envcmdline)
   629  		if cfg.BuildN {
   630  			return nil, nil
   631  		}
   632  	}
   633  
   634  	var buf bytes.Buffer
   635  	path, err := pathcache.LookPath(cmdline[0])
   636  	if err != nil {
   637  		return nil, err
   638  	}
   639  	cmd := exec.Command(path, cmdline[1:]...)
   640  	if cmd.Path != "" {
   641  		cmd.Args[0] = cmd.Path
   642  	}
   643  	cmd.Stdout = &buf
   644  	cmd.Stderr = &buf
   645  	cleanup := passLongArgsInResponseFiles(cmd)
   646  	defer cleanup()
   647  	if dir != "." {
   648  		cmd.Dir = dir
   649  	}
   650  	cmd.Env = cmd.Environ() // Pre-allocate with correct PWD.
   651  
   652  	// Add the TOOLEXEC_IMPORTPATH environment variable for -toolexec tools.
   653  	// It doesn't really matter if -toolexec isn't being used.
   654  	// Note that a.Package.Desc is not really an import path,
   655  	// but this is consistent with 'go list -f {{.ImportPath}}'.
   656  	// Plus, it is useful to uniquely identify packages in 'go list -json'.
   657  	if a != nil && a.Package != nil {
   658  		cmd.Env = append(cmd.Env, "TOOLEXEC_IMPORTPATH="+a.Package.Desc())
   659  	}
   660  
   661  	cmd.Env = append(cmd.Env, env...)
   662  	start := time.Now()
   663  	err = cmd.Run()
   664  	if a != nil && a.json != nil {
   665  		aj := a.json
   666  		aj.Cmd = append(aj.Cmd, joinUnambiguously(cmdline))
   667  		aj.CmdReal += time.Since(start)
   668  		if ps := cmd.ProcessState; ps != nil {
   669  			aj.CmdUser += ps.UserTime()
   670  			aj.CmdSys += ps.SystemTime()
   671  		}
   672  	}
   673  
   674  	// err can be something like 'exit status 1'.
   675  	// Add information about what program was running.
   676  	// Note that if buf.Bytes() is non-empty, the caller usually
   677  	// shows buf.Bytes() and does not print err at all, so the
   678  	// prefix here does not make most output any more verbose.
   679  	if err != nil {
   680  		err = errors.New(cmdline[0] + ": " + err.Error())
   681  	}
   682  	return buf.Bytes(), err
   683  }
   684  
   685  // joinUnambiguously prints the slice, quoting where necessary to make the
   686  // output unambiguous.
   687  // TODO: See issue 5279. The printing of commands needs a complete redo.
   688  func joinUnambiguously(a []string) string {
   689  	var buf strings.Builder
   690  	for i, s := range a {
   691  		if i > 0 {
   692  			buf.WriteByte(' ')
   693  		}
   694  		q := strconv.Quote(s)
   695  		// A gccgo command line can contain -( and -).
   696  		// Make sure we quote them since they are special to the shell.
   697  		// The trimpath argument can also contain > (part of =>) and ;. Quote those too.
   698  		if s == "" || strings.ContainsAny(s, " ()>;") || len(q) > len(s)+2 {
   699  			buf.WriteString(q)
   700  		} else {
   701  			buf.WriteString(s)
   702  		}
   703  	}
   704  	return buf.String()
   705  }
   706  

View as plain text