1
0
mirror of https://github.com/golang/go synced 2024-11-23 00:20:12 -07:00

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 <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Alex Brainman 2018-04-25 09:15:25 +10:00
parent dbafdac0c1
commit f7b625e4cb
2 changed files with 40 additions and 1 deletions

View 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))
}
}

View 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 {