Skip to content

Commit 43272dd

Browse files
committed
go/build: trim filename extension starting at the final dot
Previously, goodOSArchFile trimmed the file name's extension from the first instance of "." in the name. This resulted in strange behavior when compiling files with dots in their names. This change causes goodOSArchFile to trim the file name's extension from the final instance of ".", as path.Ext suggests should happen. Fixes golang#46662
1 parent aa5540c commit 43272dd

9 files changed

+23
-14
lines changed

src/go/build/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ func (ctxt *Context) matchTag(name string, allTags map[string]bool) bool {
19441944
// if GOOS=illumos, then files with GOOS=solaris are also matched.
19451945
// if GOOS=ios, then files with GOOS=darwin are also matched.
19461946
func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool {
1947-
if dot := strings.Index(name, "."); dot != -1 {
1947+
if dot := strings.LastIndex(name, "."); dot != -1 {
19481948
name = name[:dot]
19491949
}
19501950

src/go/build/build_test.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ var matchFileTests = []struct {
396396
{ctxtP9, "foo.go", "", true},
397397
{ctxtP9, "foo1.go", "// +build linux\n\npackage main\n", false},
398398
{ctxtP9, "foo.badsuffix", "", false},
399+
{ctxtP9, "file.name_plan9.go", "", true},
400+
{ctxtP9, "file.name_linux.go", "", false},
399401
{ctxtAndroid, "foo_linux.go", "", true},
400402
{ctxtAndroid, "foo_android.go", "", true},
401403
{ctxtAndroid, "foo_plan9.go", "", false},
@@ -404,24 +406,29 @@ var matchFileTests = []struct {
404406
{ctxtAndroid, "plan9_test.go", "", true},
405407
{ctxtAndroid, "arm.s", "", true},
406408
{ctxtAndroid, "amd64.s", "", true},
409+
{ctxtAndroid, "file.name_android.go", "", true},
410+
{ctxtAndroid, "file.name_linux.go", "", true},
411+
{ctxtAndroid, "file.name_plan9.go", "", false},
407412
}
408413

409414
func TestMatchFile(t *testing.T) {
410415
for _, tt := range matchFileTests {
411-
ctxt := tt.ctxt
412-
ctxt.OpenFile = func(path string) (r io.ReadCloser, err error) {
413-
if path != "x+"+tt.name {
414-
t.Fatalf("OpenFile asked for %q, expected %q", path, "x+"+tt.name)
416+
t.Run("", func(t *testing.T) {
417+
ctxt := tt.ctxt
418+
ctxt.OpenFile = func(path string) (r io.ReadCloser, err error) {
419+
if path != "x+"+tt.name {
420+
t.Fatalf("OpenFile asked for %q, expected %q", path, "x+"+tt.name)
421+
}
422+
return &readNopCloser{strings.NewReader(tt.data)}, nil
415423
}
416-
return &readNopCloser{strings.NewReader(tt.data)}, nil
417-
}
418-
ctxt.JoinPath = func(elem ...string) string {
419-
return strings.Join(elem, "+")
420-
}
421-
match, err := ctxt.MatchFile("x", tt.name)
422-
if match != tt.match || err != nil {
423-
t.Fatalf("MatchFile(%q) = %v, %v, want %v, nil", tt.name, match, err, tt.match)
424-
}
424+
ctxt.JoinPath = func(elem ...string) string {
425+
return strings.Join(elem, "+")
426+
}
427+
match, err := ctxt.MatchFile("x", tt.name)
428+
if match != tt.match || err != nil {
429+
t.Fatalf("MatchFile(%q) = %v, %v, want %v, nil", tt.name, match, err, tt.match)
430+
}
431+
})
425432
}
426433
}
427434

src/go/build/syslist_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ var tests = []GoodFileTest{
5151
{"file_foo_" + otherArch + ".go", false},
5252
{"file_" + thisOS + ".c", true},
5353
{"file_" + otherOS + ".c", false},
54+
{"file.name_" + thisOS + ".go", true},
55+
{"file.name_" + otherOS + ".go", false},
5456
}
5557

5658
func TestGoodOSArch(t *testing.T) {

0 commit comments

Comments
 (0)