Source file src/os/exec/lp_windows.go

     1  // Copyright 2010 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 exec
     6  
     7  import (
     8  	"errors"
     9  	"io/fs"
    10  	"os"
    11  	"path/filepath"
    12  	"strings"
    13  )
    14  
    15  // ErrNotFound is the error resulting if a path search failed to find an executable file.
    16  var ErrNotFound = errors.New("executable file not found in %PATH%")
    17  
    18  func chkStat(file string) error {
    19  	d, err := os.Stat(file)
    20  	if err != nil {
    21  		return err
    22  	}
    23  	if d.IsDir() {
    24  		return fs.ErrPermission
    25  	}
    26  	return nil
    27  }
    28  
    29  func hasExt(file string) bool {
    30  	i := strings.LastIndex(file, ".")
    31  	if i < 0 {
    32  		return false
    33  	}
    34  	return strings.LastIndexAny(file, `:\/`) < i
    35  }
    36  
    37  func findExecutable(file string, exts []string) (string, error) {
    38  	if len(exts) == 0 {
    39  		return file, chkStat(file)
    40  	}
    41  	if hasExt(file) {
    42  		if chkStat(file) == nil {
    43  			return file, nil
    44  		}
    45  		// Keep checking exts below, so that programs with weird names
    46  		// like "foo.bat.exe" will resolve instead of failing.
    47  	}
    48  	for _, e := range exts {
    49  		if f := file + e; chkStat(f) == nil {
    50  			return f, nil
    51  		}
    52  	}
    53  	if hasExt(file) {
    54  		return "", fs.ErrNotExist
    55  	}
    56  	return "", ErrNotFound
    57  }
    58  
    59  // LookPath searches for an executable named file in the
    60  // directories named by the PATH environment variable.
    61  // LookPath also uses PATHEXT environment variable to match
    62  // a suitable candidate.
    63  // If file contains a slash, it is tried directly and the PATH is not consulted.
    64  // Otherwise, on success, the result is an absolute path.
    65  //
    66  // In older versions of Go, LookPath could return a path relative to the current directory.
    67  // As of Go 1.19, LookPath will instead return that path along with an error satisfying
    68  // [errors.Is](err, [ErrDot]). See the package documentation for more details.
    69  func LookPath(file string) (string, error) {
    70  	return lookPath(file, pathExt())
    71  }
    72  
    73  // lookExtensions finds windows executable by its dir and path.
    74  // It uses LookPath to try appropriate extensions.
    75  // lookExtensions does not search PATH, instead it converts `prog` into `.\prog`.
    76  //
    77  // If the path already has an extension found in PATHEXT,
    78  // lookExtensions returns it directly without searching
    79  // for additional extensions. For example,
    80  // "C:\foo\example.com" would be returned as-is even if the
    81  // program is actually "C:\foo\example.com.exe".
    82  func lookExtensions(path, dir string) (string, error) {
    83  	if filepath.Base(path) == path {
    84  		path = "." + string(filepath.Separator) + path
    85  	}
    86  	exts := pathExt()
    87  	if ext := filepath.Ext(path); ext != "" {
    88  		for _, e := range exts {
    89  			if strings.EqualFold(ext, e) {
    90  				// Assume that path has already been resolved.
    91  				return path, nil
    92  			}
    93  		}
    94  	}
    95  	if dir == "" {
    96  		return lookPath(path, exts)
    97  	}
    98  	if filepath.VolumeName(path) != "" {
    99  		return lookPath(path, exts)
   100  	}
   101  	if len(path) > 1 && os.IsPathSeparator(path[0]) {
   102  		return lookPath(path, exts)
   103  	}
   104  	dirandpath := filepath.Join(dir, path)
   105  	// We assume that LookPath will only add file extension.
   106  	lp, err := lookPath(dirandpath, exts)
   107  	if err != nil {
   108  		return "", err
   109  	}
   110  	ext := strings.TrimPrefix(lp, dirandpath)
   111  	return path + ext, nil
   112  }
   113  
   114  func pathExt() []string {
   115  	var exts []string
   116  	x := os.Getenv(`PATHEXT`)
   117  	if x != "" {
   118  		for _, e := range strings.Split(strings.ToLower(x), `;`) {
   119  			if e == "" {
   120  				continue
   121  			}
   122  			if e[0] != '.' {
   123  				e = "." + e
   124  			}
   125  			exts = append(exts, e)
   126  		}
   127  	} else {
   128  		exts = []string{".com", ".exe", ".bat", ".cmd"}
   129  	}
   130  	return exts
   131  }
   132  
   133  // lookPath implements LookPath for the given PATHEXT list.
   134  func lookPath(file string, exts []string) (string, error) {
   135  	if strings.ContainsAny(file, `:\/`) {
   136  		f, err := findExecutable(file, exts)
   137  		if err == nil {
   138  			return f, nil
   139  		}
   140  		return "", &Error{file, err}
   141  	}
   142  
   143  	// On Windows, creating the NoDefaultCurrentDirectoryInExePath
   144  	// environment variable (with any value or no value!) signals that
   145  	// path lookups should skip the current directory.
   146  	// In theory we are supposed to call NeedCurrentDirectoryForExePathW
   147  	// "as the registry location of this environment variable can change"
   148  	// but that seems exceedingly unlikely: it would break all users who
   149  	// have configured their environment this way!
   150  	// https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-needcurrentdirectoryforexepathw
   151  	// See also go.dev/issue/43947.
   152  	var (
   153  		dotf   string
   154  		dotErr error
   155  	)
   156  	if _, found := os.LookupEnv("NoDefaultCurrentDirectoryInExePath"); !found {
   157  		if f, err := findExecutable(filepath.Join(".", file), exts); err == nil {
   158  			if execerrdot.Value() == "0" {
   159  				execerrdot.IncNonDefault()
   160  				return f, nil
   161  			}
   162  			dotf, dotErr = f, &Error{file, ErrDot}
   163  		}
   164  	}
   165  
   166  	path := os.Getenv("path")
   167  	for _, dir := range filepath.SplitList(path) {
   168  		if dir == "" {
   169  			// Skip empty entries, consistent with what PowerShell does.
   170  			// (See https://go.dev/issue/61493#issuecomment-1649724826.)
   171  			continue
   172  		}
   173  
   174  		if f, err := findExecutable(filepath.Join(dir, file), exts); err == nil {
   175  			if dotErr != nil {
   176  				// https://go.dev/issue/53536: if we resolved a relative path implicitly,
   177  				// and it is the same executable that would be resolved from the explicit %PATH%,
   178  				// prefer the explicit name for the executable (and, likely, no error) instead
   179  				// of the equivalent implicit name with ErrDot.
   180  				//
   181  				// Otherwise, return the ErrDot for the implicit path as soon as we find
   182  				// out that the explicit one doesn't match.
   183  				dotfi, dotfiErr := os.Lstat(dotf)
   184  				fi, fiErr := os.Lstat(f)
   185  				if dotfiErr != nil || fiErr != nil || !os.SameFile(dotfi, fi) {
   186  					return dotf, dotErr
   187  				}
   188  			}
   189  
   190  			if !filepath.IsAbs(f) {
   191  				if execerrdot.Value() != "0" {
   192  					// If this is the same relative path that we already found,
   193  					// dotErr is non-nil and we already checked it above.
   194  					// Otherwise, record this path as the one to which we must resolve,
   195  					// with or without a dotErr.
   196  					if dotErr == nil {
   197  						dotf, dotErr = f, &Error{file, ErrDot}
   198  					}
   199  					continue
   200  				}
   201  				execerrdot.IncNonDefault()
   202  			}
   203  			return f, nil
   204  		}
   205  	}
   206  
   207  	if dotErr != nil {
   208  		return dotf, dotErr
   209  	}
   210  	return "", &Error{file, ErrNotFound}
   211  }
   212  

View as plain text