Skip to content

Commit 3580ef9

Browse files
author
Bryan C. Mills
committed
os/exec: on Windows, suppress ErrDot if the implicit path matches the explicit one
If the current directory is also listed explicitly in %PATH%, this changes the behavior of LookPath to prefer the explicit name for it (and thereby avoid ErrDot). However, in order to avoid running a different executable from what would have been run by previous Go versions, we still return the implicit path (and ErrDot) if it refers to a different file entirely. Fixes #53536. Updates #43724. Change-Id: I7ab01074e21a0e8b07a176e3bc6d3b8cf0c873cd Reviewed-on: https://go-review.googlesource.com/c/go/+/414054 Reviewed-by: Russ Cox <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Alex Brainman <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 34f3ac5 commit 3580ef9

File tree

2 files changed

+116
-7
lines changed

2 files changed

+116
-7
lines changed

src/os/exec/dot_test.go

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ import (
1515
"testing"
1616
)
1717

18+
var pathVar string = func() string {
19+
if runtime.GOOS == "plan9" {
20+
return "path"
21+
}
22+
return "PATH"
23+
}()
24+
1825
func TestLookPath(t *testing.T) {
1926
testenv.MustHaveExec(t)
2027

@@ -42,22 +49,24 @@ func TestLookPath(t *testing.T) {
4249
if err = os.Chdir(tmpDir); err != nil {
4350
t.Fatal(err)
4451
}
45-
origPath := os.Getenv("PATH")
46-
defer os.Setenv("PATH", origPath)
52+
t.Setenv("PWD", tmpDir)
53+
t.Logf(". is %#q", tmpDir)
54+
55+
origPath := os.Getenv(pathVar)
4756

4857
// Add "." to PATH so that exec.LookPath looks in the current directory on all systems.
4958
// And try to trick it with "../testdir" too.
5059
for _, dir := range []string{".", "../testdir"} {
51-
os.Setenv("PATH", dir+string(filepath.ListSeparator)+origPath)
52-
t.Run("PATH="+dir, func(t *testing.T) {
60+
t.Run(pathVar+"="+dir, func(t *testing.T) {
61+
t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath)
5362
good := dir + "/execabs-test"
5463
if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) {
55-
t.Fatalf("LookPath(%q) = %q, %v, want \"%s...\", nil", good, found, err, good)
64+
t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good)
5665
}
5766
if runtime.GOOS == "windows" {
5867
good = dir + `\execabs-test`
5968
if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) {
60-
t.Fatalf("LookPath(%q) = %q, %v, want \"%s...\", nil", good, found, err, good)
69+
t.Fatalf(`LookPath(%#q) = %#q, %v, want "%s...", nil`, good, found, err, good)
6170
}
6271
}
6372

@@ -84,4 +93,81 @@ func TestLookPath(t *testing.T) {
8493
}
8594
})
8695
}
96+
97+
// Test the behavior when the first entry in PATH is an absolute name for the
98+
// current directory.
99+
//
100+
// On Windows, "." may or may not be implicitly included before the explicit
101+
// %PATH%, depending on the process environment;
102+
// see https://go.dev/issue/4394.
103+
//
104+
// If the relative entry from "." resolves to the same executable as what
105+
// would be resolved from an absolute entry in %PATH% alone, LookPath should
106+
// return the absolute version of the path instead of ErrDot.
107+
// (See https://go.dev/issue/53536.)
108+
//
109+
// If PATH does not implicitly include "." (such as on Unix platforms, or on
110+
// Windows configured with NoDefaultCurrentDirectoryInExePath), then this
111+
// lookup should succeed regardless of the behavior for ".", so it may be
112+
// useful to run as a control case even on those platforms.
113+
t.Run(pathVar+"=$PWD", func(t *testing.T) {
114+
t.Setenv(pathVar, tmpDir+string(filepath.ListSeparator)+origPath)
115+
good := filepath.Join(tmpDir, "execabs-test")
116+
if found, err := LookPath(good); err != nil || !strings.HasPrefix(found, good) {
117+
t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, good, found, err, good)
118+
}
119+
120+
if found, err := LookPath("execabs-test"); err != nil || !strings.HasPrefix(found, good) {
121+
t.Fatalf(`LookPath(%#q) = %#q, %v, want \"%s...\", nil`, "execabs-test", found, err, good)
122+
}
123+
124+
cmd := Command("execabs-test")
125+
if cmd.Err != nil {
126+
t.Fatalf("Command(%#q).Err = %v; want nil", "execabs-test", cmd.Err)
127+
}
128+
})
129+
130+
t.Run(pathVar+"=$OTHER", func(t *testing.T) {
131+
// Control case: if the lookup returns ErrDot when PATH is empty, then we
132+
// know that PATH implicitly includes ".". If it does not, then we don't
133+
// expect to see ErrDot at all in this test (because the path will be
134+
// unambiguously absolute).
135+
wantErrDot := false
136+
t.Setenv(pathVar, "")
137+
if found, err := LookPath("execabs-test"); errors.Is(err, ErrDot) {
138+
wantErrDot = true
139+
} else if err == nil {
140+
t.Fatalf(`with PATH='', LookPath(%#q) = %#q; want non-nil error`, "execabs-test", found)
141+
}
142+
143+
// Set PATH to include an explicit directory that contains a completely
144+
// independent executable that happens to have the same name as an
145+
// executable in ".". If "." is included implicitly, looking up the
146+
// (unqualified) executable name will return ErrDot; otherwise, the
147+
// executable in "." should have no effect and the lookup should
148+
// unambiguously resolve to the directory in PATH.
149+
150+
dir := t.TempDir()
151+
executable := "execabs-test"
152+
if runtime.GOOS == "windows" {
153+
executable += ".exe"
154+
}
155+
if err := os.WriteFile(filepath.Join(dir, executable), []byte{1, 2, 3}, 0777); err != nil {
156+
t.Fatal(err)
157+
}
158+
t.Setenv(pathVar, dir+string(filepath.ListSeparator)+origPath)
159+
160+
found, err := LookPath("execabs-test")
161+
if wantErrDot {
162+
wantFound := filepath.Join(".", executable)
163+
if found != wantFound || !errors.Is(err, ErrDot) {
164+
t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, Is ErrDot`, "execabs-test", found, err, wantFound)
165+
}
166+
} else {
167+
wantFound := filepath.Join(dir, executable)
168+
if found != wantFound || err != nil {
169+
t.Fatalf(`LookPath(%#q) = %#q, %v, want %#q, nil`, "execabs-test", found, err, wantFound)
170+
}
171+
}
172+
})
87173
}

src/os/exec/lp_windows.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,43 @@ func LookPath(file string) (string, error) {
9696
// have configured their environment this way!
9797
// https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-needcurrentdirectoryforexepathw
9898
// See also go.dev/issue/43947.
99+
var (
100+
dotf string
101+
dotErr error
102+
)
99103
if _, found := syscall.Getenv("NoDefaultCurrentDirectoryInExePath"); !found {
100104
if f, err := findExecutable(filepath.Join(".", file), exts); err == nil {
101-
return f, &Error{file, ErrDot}
105+
dotf, dotErr = f, &Error{file, ErrDot}
102106
}
103107
}
104108

105109
path := os.Getenv("path")
106110
for _, dir := range filepath.SplitList(path) {
107111
if f, err := findExecutable(filepath.Join(dir, file), exts); err == nil {
112+
if dotErr != nil {
113+
// https://go.dev/issue/53536: if we resolved a relative path implicitly,
114+
// and it is the same executable that would be resolved from the explicit %PATH%,
115+
// prefer the explicit name for the executable (and, likely, no error) instead
116+
// of the equivalent implicit name with ErrDot.
117+
//
118+
// Otherwise, return the ErrDot for the implicit path as soon as we find
119+
// out that the explicit one doesn't match.
120+
dotfi, dotfiErr := os.Lstat(dotf)
121+
fi, fiErr := os.Lstat(f)
122+
if dotfiErr != nil || fiErr != nil || !os.SameFile(dotfi, fi) {
123+
return dotf, dotErr
124+
}
125+
}
126+
108127
if !filepath.IsAbs(f) {
109128
return f, &Error{file, ErrDot}
110129
}
111130
return f, nil
112131
}
113132
}
133+
134+
if dotErr != nil {
135+
return dotf, dotErr
136+
}
114137
return "", &Error{file, ErrNotFound}
115138
}

0 commit comments

Comments
 (0)