Skip to content

Commit 35ecb56

Browse files
fredbiclaude
andcommitted
fix(scanner): match a dependency's source file by name, not by path
Joining a position out of compiled export data to the syntax parsed beside it compared the two file names whole. They are not spelled alike: the compiler recorded one and `go list` handed us the other, and on Windows the two differ in separator and in drive-letter case. The comparison then found nothing, and a field promoted from another package left the spec without a diagnostic — the go-swagger#2417 fixture losing "hue" again on windows/go1.26 while every other platform agreed. Compare the base name instead. Go keeps every file of a package in one directory, so base names are unique among the candidates, and it is the only part of a path the two routes are certain to agree on. The Windows spellings are asserted from any host, since a comparison that only fails on one platform is one that only regresses there. Refers to go-swagger/go-swagger#2417. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 6e7d1bb commit 35ecb56

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎internal/packages/attach.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ func readBackSource(pkg *Package, open func(string) (io.ReadCloser, error)) bool
7070
//
7171
// The bytes come from the caller's filesystem rather than from the parser's own read, so a virtual tree is honoured;
7272
// the path is still handed to [parser.ParseFile], because it is what the positions are recorded against.
73-
// Those have to match the ones export data carries.
73+
//
74+
// Those positions have to line up with the ones export data carries, and only the line and the file's base name
75+
// actually do. The compiler and `go list` do not spell a path the same way — on Windows they differ in separator and
76+
// in drive-letter case — so nothing downstream may compare these names whole.
7477
func parseFilesForComments(fset *token.FileSet, paths []string, open func(string) (io.ReadCloser, error)) []*ast.File {
7578
syntax := make([]*ast.File, 0, len(paths))
7679

‎internal/scanner/scan_context.go‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,14 +1074,41 @@ func (s *ScanCtx) FileForPos(pkgPath string, pos token.Pos) (*ast.File, bool) {
10741074
// ourselves. Identity is the right test when both come from one type-check and the only test that
10751075
// distinguishes two files of the same name, so the name comparison is the fallback rather than the rule.
10761076
for _, file := range pkg.Syntax {
1077-
if at := pkg.Fset.File(file.Pos()); at != nil && at.Name() == target.Name() {
1077+
if at := pkg.Fset.File(file.Pos()); at != nil && sameSourceFile(at.Name(), target.Name()) {
10781078
return file, true
10791079
}
10801080
}
10811081

10821082
return nil, false
10831083
}
10841084

1085+
// sameSourceFile reports whether two token.File names denote the same file of one package.
1086+
//
1087+
// Compared by base name, deliberately. The two names reach this point by different routes — one recorded by the
1088+
// compiler into export data, the other the path `go list` handed us — and the routes do not agree on how a path is
1089+
// spelled. Separator, drive-letter case and absolute-versus-relative all vary on Windows, where comparing the strings
1090+
// whole made a cross-package promoted field vanish from the spec without a word.
1091+
//
1092+
// A base name is enough because Go puts every file of a package in one directory, so no two entries of pkg.Syntax can
1093+
// share one. It is also the only part of a path both routes are certain to agree on.
1094+
func sameSourceFile(a, b string) bool {
1095+
return sourceFileBase(a) == sourceFileBase(b)
1096+
}
1097+
1098+
// sourceFileBase is the last element of a path written under either convention.
1099+
//
1100+
// Neither path.Base nor filepath.Base will do: each knows one separator, and the two names being compared here can be
1101+
// spelled differently from one another on the same machine. Treating a backslash as a separator on a system where it
1102+
// is a legal filename character only widens the match within one package's file list, where base names are unique
1103+
// anyway.
1104+
func sourceFileBase(name string) string {
1105+
if i := strings.LastIndexAny(name, `/\`); i >= 0 {
1106+
return name[i+1:]
1107+
}
1108+
1109+
return name
1110+
}
1111+
10851112
func (s *ScanCtx) FindComments(pkg *packages.Package, name string) (*ast.CommentGroup, bool) {
10861113
for _, f := range pkg.Syntax {
10871114
for _, d := range f.Decls {

‎internal/scanner/scan_context_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,43 @@ func TestNewScanCtx_NonBuildingCode_FallsBackToSource(t *testing.T) {
590590
"opting out skips the attempt entirely, so there is nothing to fall back from")
591591
}
592592

593+
// TestSameSourceFile covers the comparison that joins a position out of compiled export data to the
594+
// syntax parsed beside it.
595+
//
596+
// The two names come from different places — the compiler wrote one into export data, `go list`
597+
// handed us the other — and they are not spelled alike. Comparing them whole passed everywhere the
598+
// two happened to agree and failed on Windows, where a cross-package promoted field then vanished
599+
// from the spec with no diagnostic: go-swagger#2417's fixture lost "hue" again.
600+
//
601+
// The Windows shapes are the point of this test, so they are asserted from any host.
602+
func TestSameSourceFile(t *testing.T) {
603+
t.Parallel()
604+
605+
same := map[string][2]string{
606+
"identical": {"/src/color/color.go", "/src/color/color.go"},
607+
"separator": {`D:\a\codescan\color\color.go`, "D:/a/codescan/color/color.go"},
608+
"drive-letter case": {
609+
`D:\a\codescan\color\color.go`, `d:\a\codescan\color\color.go`,
610+
},
611+
"absolute against relative": {"/src/color/color.go", "color/color.go"},
612+
"bare name": {"/src/color/color.go", "color.go"},
613+
}
614+
for name, pair := range same {
615+
t.Run(name, func(t *testing.T) {
616+
t.Parallel()
617+
assert.True(t, sameSourceFile(pair[0], pair[1]), "%q vs %q", pair[0], pair[1])
618+
})
619+
}
620+
621+
// Base names stay unique inside a package, since Go keeps every file of one in a single directory.
622+
// So two different base names are two different files, whatever the directories say.
623+
t.Run("different files do not match", func(t *testing.T) {
624+
t.Parallel()
625+
assert.False(t, sameSourceFile("/src/color/color.go", "/src/color/shade.go"))
626+
assert.False(t, sameSourceFile(`D:\a\color\color.go`, `D:\a\color\shade.go`))
627+
})
628+
}
629+
593630
func TestScanCtx_findEnumValue_EdgeCases(t *testing.T) {
594631
sctx := &ScanCtx{}
595632

0 commit comments

Comments
 (0)
⌂ 换网址