From f7b625e4cb363c23ed5c8c144ee7d06c4136a0eb Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Wed, 25 Apr 2018 09:15:25 +1000 Subject: [PATCH] cmd/go/internal/load: use lowercase package directory comparisons on Windows go build command is short for go build . and it builds . package. When command above is executed from directory inside of GOPATH, it uses GOPATH to calculate package source directory. So . package uses GOPATH as part of package source directory. On the other hand go build -ldflags=abc only passes flag to the linker for packages that are listed on the command line. The command above assumes . package again, and that package source path is compared with current directory. Current code compares result of os.Getwd with what GOPATH environment variable contains. But these values might differ in letter case on Windows. For example, one might return c:\gopath\..., while the other might contain C:\GOPATH. Fixes #24750 Fixes #24232 Fixes #25046 Change-Id: I03d8c7a9b73e847f88ae61c88cd41efa546c6d0e Reviewed-on: https://go-review.googlesource.com/109235 Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/cmd/go/go_test.go | 33 ++++++++++++++++++++++++++++++ src/cmd/go/internal/load/search.go | 8 +++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go index 967b2c67b3..fb8846c710 100644 --- a/src/cmd/go/go_test.go +++ b/src/cmd/go/go_test.go @@ -6292,3 +6292,36 @@ func TestLinkerTmpDirIsDeleted(t *testing.T) { t.Fatalf("Stat(%q) returns unexpected error: %v", tmpdir, err) } } + +func testCDAndGOPATHAreDifferent(tg *testgoData, cd, gopath string) { + tg.setenv("GOPATH", gopath) + + tg.tempDir("dir") + exe := tg.path("dir/a.exe") + + tg.cd(cd) + + tg.run("build", "-o", exe, "-ldflags", "-X=my.pkg.Text=linkXworked") + out, err := exec.Command(exe).CombinedOutput() + if err != nil { + tg.t.Fatal(err) + } + if string(out) != "linkXworked\n" { + tg.t.Errorf(`incorrect output with GOPATH=%q and CD=%q: expected "linkXworked\n", but have %q`, gopath, cd, string(out)) + } +} + +func TestCDAndGOPATHAreDifferent(t *testing.T) { + tg := testgo(t) + defer tg.cleanup() + + gopath := filepath.Join(tg.pwd(), "testdata") + cd := filepath.Join(gopath, "src/my.pkg/main") + + testCDAndGOPATHAreDifferent(tg, cd, gopath) + if runtime.GOOS == "windows" { + testCDAndGOPATHAreDifferent(tg, cd, strings.Replace(gopath, `\`, `/`, -1)) + testCDAndGOPATHAreDifferent(tg, cd, strings.ToUpper(gopath)) + testCDAndGOPATHAreDifferent(tg, cd, strings.ToLower(gopath)) + } +} diff --git a/src/cmd/go/internal/load/search.go b/src/cmd/go/internal/load/search.go index 595de07904..6494f8e569 100644 --- a/src/cmd/go/internal/load/search.go +++ b/src/cmd/go/internal/load/search.go @@ -13,6 +13,7 @@ import ( "path" "path/filepath" "regexp" + "runtime" "strings" ) @@ -282,7 +283,12 @@ func MatchPackage(pattern, cwd string) func(*Package) bool { } dir = filepath.Join(cwd, dir) if pattern == "" { - return func(p *Package) bool { return p.Dir == dir } + return func(p *Package) bool { + if runtime.GOOS != "windows" { + return p.Dir == dir + } + return strings.EqualFold(p.Dir, dir) + } } matchPath := matchPattern(pattern) return func(p *Package) bool { -- 2.50.0