]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/load: use lowercase package directory comparisons on Windows
authorAlex Brainman <alex.brainman@gmail.com>
Tue, 24 Apr 2018 23:15:25 +0000 (09:15 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Sat, 26 May 2018 07:56:52 +0000 (07:56 +0000)
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 <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/go/go_test.go
src/cmd/go/internal/load/search.go

index 967b2c67b32d3a40d5fab28bf226f4c041c0a9cd..fb8846c710dc9bcee6af8a3d019efc71018ce602 100644 (file)
@@ -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))
+       }
+}
index 595de079046607819192ff3ee35b72de8d8f6ac3..6494f8e569f3c2266947c0e8e088f6530f432bfc 100644 (file)
@@ -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 {