Skip to content

Commit a68cf3a

Browse files
fredbiclaude
andcommitted
fix(packages): read paths the way the host writes them
Three places in the toolchain-free loader assumed a path is slash-separated and unrooted, which is true of an import path and of nothing else. On Windows the consequences ranged from a quietly thinner spec to a walk that goes where the go command does not. GOPATH was split on ':' or ';', whichever came first. A Windows GOPATH starts with a drive letter, so "C:\Users\x\go" became "C", the module cache resolved to a directory that cannot exist, and every cached dependency fell through to synthesis — a synthesized type has no fields and no comments, so a field of strfmt.DateTime kept its name and lost its type and its date-time format while the scan reported success. Only the host's separator separates now; on Unix that also stops cutting a directory whose name contains a semicolon in half. A directory's last element was taken with path.Base, which looks for a forward slash and hands a Windows path back whole. That is how "D:\src\m\vendor" reads as "not called vendor", so a wildcard walked straight into a vendored tree, and how a module-less scan fell back to naming its package after the absolute path it was scanned from. A path handed to a virtual filesystem is normalised by its own shape rather than by the host's. filepath.ToSlash and filepath.VolumeName defer to the machine running the scan, so a Windows-shaped tree was normalised only when Windows read it — and an embedded or recorded tree, which is what the option exists for, need not come from the machine reading it at all. A walk still hands paths back in the caller's own form, since they become the file name in every position the scan reports. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 8b52530 commit a68cf3a

5 files changed

Lines changed: 207 additions & 8 deletions

File tree

‎internal/packages/list/resolve.go‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"go/build"
99
"io/fs"
1010
"path"
11+
"path/filepath"
1112
"sort"
1213
"strings"
1314

@@ -172,7 +173,7 @@ func (r *Resolver) ResolvePatterns(patterns []string) ([]Target, error) {
172173
// or ./mycode/vendor.
173174
// The directory itself is not special though: a package that merely happens to be called vendor is an ordinary
174175
// match, which is why this prunes the children rather than the node.
175-
if d != dir && path.Base(d) == "vendor" {
176+
if d != dir && r.vfs.Base(d) == "vendor" {
176177
return fs.SkipDir
177178
}
178179

@@ -380,9 +381,15 @@ func (r *Resolver) moduleCache() string {
380381
return ""
381382
}
382383
// GOPATH may be a list; the module cache lives under the first entry.
383-
if i := strings.IndexAny(gopath, ":;"); i >= 0 {
384-
gopath = gopath[:i]
384+
//
385+
// Only the HOST's separator separates — ';' on Windows, ':' everywhere else — and the distinction is not academic.
386+
// Accepting either cuts every Windows GOPATH at its drive letter, leaving "C", so the cache resolves to a directory
387+
// that cannot exist and every cached dependency falls through to synthesis; on Unix it splits a directory whose name
388+
// contains a semicolon in half.
389+
if entries := filepath.SplitList(gopath); len(entries) > 0 {
390+
gopath = entries[0]
385391
}
392+
386393
return r.vfs.Join(gopath, "pkg", "mod")
387394
}
388395

@@ -458,7 +465,7 @@ func (r *Resolver) pkgPathFor(dir string) string {
458465
func (r *Resolver) pathlessID(dir string) string {
459466
rel, ok := r.vfs.HasSubdir(r.dir, dir)
460467
if !ok || rel == "." || rel == "" {
461-
return path.Base(dir)
468+
return r.vfs.Base(dir)
462469
}
463470

464471
return rel

‎internal/packages/loader_test.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,21 @@ func TestLoadKeepsRootedPathsUnderRecursivePatterns(t *testing.T) {
326326
require.NoError(t, os.WriteFile(filepath.Join(root, "sub", "s.go"),
327327
[]byte("package sub\n\ntype S struct{ A string }\n"), 0o600))
328328

329-
loader := packages.NewLoader(packages.WithFS(os.DirFS("/")), packages.WithGoEnv(packages.GoEnv{GOOS: "linux", GOARCH: "amd64"}))
329+
// The FS is rooted at the top of whatever holds the temp tree — "/" on a POSIX system, the volume elsewhere — because
330+
// a rooted path is exactly what has to survive the round trip through io/fs's own namespace.
331+
fsRoot := filepath.VolumeName(root) + string(filepath.Separator)
332+
333+
loader := packages.NewLoader(packages.WithFS(os.DirFS(fsRoot)), packages.WithGoEnv(packages.GoEnv{GOOS: "linux", GOARCH: "amd64"}))
330334
pkgs, err := loader.Load(&packages.Config{Dir: root}, "./...")
331335
require.NoError(t, err)
332336
require.NotEmpty(t, pkgs)
333337

334338
for _, p := range pkgs {
335339
for _, f := range p.GoFiles {
336-
assert.True(t, strings.HasPrefix(f, "/"),
340+
assert.True(t, filepath.IsAbs(f),
337341
"file %q lost its root; positions derived from it would not resolve", f)
342+
assert.True(t, strings.HasPrefix(f, root),
343+
"file %q was not reported under the directory that was scanned", f)
338344
}
339345
}
340346
}

‎internal/packages/resolve_test.go‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,51 @@ func TestPkgPath_NoModuleDoesNotLeakAbsolutePaths(t *testing.T) {
340340
assert.Equal(t, "a", got[0])
341341
assert.NotContains(t, got[0], root, "the scanning machine's layout is not part of the answer")
342342
}
343+
344+
// A dependency in the module cache resolves whatever the shape of GOPATH.
345+
//
346+
// GOPATH may be a list, and only the host's separator separates it. Accepting ':' and ';' interchangeably cut every
347+
// Windows GOPATH at its drive letter — "C:\Users\x\go" became "C" — so the module cache pointed at a directory that
348+
// cannot exist and every cached dependency fell through to synthesis. A synthesized type has no fields, which is
349+
// exactly how a `swagger:strfmt` mark on a dependency's type went missing while the scan still succeeded.
350+
func TestModuleCache_GOPATHSplitsOnTheHostSeparatorOnly(t *testing.T) {
351+
t.Parallel()
352+
353+
// A directory whose name carries the separator the host does NOT use. Where ';' is not the separator it is an
354+
// ordinary character and makes the witness; where it is, the drive letter every absolute path starts with is the
355+
// witness already.
356+
name := "gopath"
357+
if filepath.ListSeparator != ';' {
358+
name = "go;path"
359+
}
360+
361+
gopath := filepath.Join(t.TempDir(), name)
362+
dep := filepath.Join(gopath, "pkg", "mod", "example.com", "dep@v1.0.0")
363+
require.NoError(t, os.MkdirAll(dep, 0o750))
364+
require.NoError(t, os.WriteFile(filepath.Join(dep, "go.mod"),
365+
[]byte("module example.com/dep\n\ngo 1.25.0\n"), 0o600))
366+
require.NoError(t, os.WriteFile(filepath.Join(dep, "d.go"),
367+
[]byte("package dep\n\ntype D struct{ A string }\n"), 0o600))
368+
369+
root := writeTree(t, map[string]string{
370+
"go.mod": "module example.com/main\n\ngo 1.25.0\n\nrequire example.com/dep v1.0.0\n",
371+
"a/a.go": "package a\n\nimport \"example.com/dep\"\n\ntype A struct{ D dep.D }\n",
372+
})
373+
374+
pkgs, err := packages.NewLoader(packages.WithStrategy(packages.StrategyToolchainFree)).
375+
// GOMODCACHE is cleared so the cache is derived from GOPATH, which is the case that had the fault.
376+
Load(&packages.Config{Dir: root, Env: append(os.Environ(), "GOPATH="+gopath, "GOMODCACHE=")}, "./a")
377+
require.NoError(t, err)
378+
require.Len(t, pkgs, 1)
379+
380+
loaded := pkgs[0].Imports["example.com/dep"]
381+
require.NotNil(t, loaded, "the dependency was not resolved at all")
382+
383+
obj := loaded.Types.Scope().Lookup("D")
384+
require.NotNil(t, obj)
385+
386+
strct, ok := obj.Type().Underlying().(*types.Struct)
387+
require.True(t, ok)
388+
assert.Equal(t, 1, strct.NumFields(),
389+
"the dependency was synthesized rather than read: a synthesized type is an empty struct")
390+
}

‎internal/packages/vfs/vfs.go‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,22 @@ func (v *FS) Virtual() bool { return v.fsys != nil }
3333
//
3434
// io/fs requires slash-separated, unrooted paths, while callers naturally write OS paths and sometimes absolute ones.
3535
// Normalising here (rather than rejecting) keeps the same patterns working against both backends.
36+
//
37+
// A drive letter goes first, because it is what makes a Windows path absolute and it is not a separator: "D:\a" would
38+
// otherwise survive as "D:/a" and address nothing inside the FS. Dropping it puts a rooted OS path in the same place
39+
// the rooted POSIX form lands, which is the convention the option documents.
40+
//
41+
// Both steps read the path rather than the host. A virtual filesystem need not have been built on the machine that is
42+
// scanning it — an embedded or recorded tree carries the shape it was captured with — so deferring to the host (which
43+
// is what filepath.ToSlash and filepath.VolumeName do) would normalise a Windows-shaped tree only when Windows was
44+
// reading it. The cost is that a backslash cannot be part of a name here, which is already true wherever such a tree
45+
// came from.
3646
func (v *FS) Clean(p string) string {
3747
if !v.Virtual() {
3848
return p
3949
}
40-
p = filepath.ToSlash(p)
50+
p = trimDrive(p)
51+
p = strings.ReplaceAll(p, `\`, "/")
4152
p = strings.TrimPrefix(p, "./")
4253
p = strings.TrimLeft(p, "/")
4354
if p == "" {
@@ -99,6 +110,18 @@ func (v *FS) Join(elem ...string) string {
99110
return path.Join(elem...)
100111
}
101112

113+
// Base is the last element of a directory path, under whichever convention the path was written with.
114+
//
115+
// path.Base is not a substitute: it looks for a forward slash, so on Windows it hands back "D:\src\vendor" whole, and
116+
// every caller comparing a directory's name against a fixed one silently stops matching.
117+
func (v *FS) Base(p string) string {
118+
if !v.Virtual() {
119+
return filepath.Base(p)
120+
}
121+
122+
return path.Base(v.Clean(p))
123+
}
124+
102125
func (v *FS) IsAbs(p string) bool {
103126
if !v.Virtual() {
104127
return filepath.IsAbs(p)
@@ -159,6 +182,9 @@ func (v *FS) WalkDirs(root string, yield func(string) error) error {
159182
}
160183

161184
// rebase maps a path produced by walking under inner back onto the caller's root.
185+
//
186+
// The root decides the separator, because the result has to read like the path the caller wrote: a scan pointed at an
187+
// OS path gets OS paths back even though the walk happened in io/fs's slash-separated namespace.
162188
func rebase(root, inner, p string) string {
163189
if p == inner {
164190
return root
@@ -169,7 +195,34 @@ func rebase(root, inner, p string) string {
169195
rel = p
170196
}
171197

172-
return strings.TrimSuffix(root, "/") + "/" + rel
198+
sep := separator(root)
199+
if sep != "/" {
200+
rel = strings.ReplaceAll(rel, "/", sep)
201+
}
202+
203+
return strings.TrimSuffix(root, sep) + sep + rel
204+
}
205+
206+
// separator reports the path separator a caller-supplied path is written with, defaulting to the io/fs one.
207+
func separator(p string) string {
208+
if i := strings.LastIndexAny(p, `/\`); i >= 0 && p[i] == '\\' {
209+
return `\`
210+
}
211+
212+
return "/"
213+
}
214+
215+
// trimDrive drops a Windows drive prefix, so that what remains is a path a rooted namespace can hold.
216+
func trimDrive(p string) string {
217+
if len(p) >= 2 && p[1] == ':' && isDriveLetter(p[0]) {
218+
return p[2:]
219+
}
220+
221+
return p
222+
}
223+
224+
func isDriveLetter(c byte) bool {
225+
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
173226
}
174227

175228
// skipDir reports directories the go tool itself never treats as packages.

‎internal/packages/vfs/vfs_test.go‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package vfs
5+
6+
import (
7+
"testing"
8+
"testing/fstest"
9+
10+
"github.com/go-openapi/testify/v2/assert"
11+
)
12+
13+
// A path handed to a virtual filesystem is normalised by its own shape, not by the host's.
14+
//
15+
// The two forms have to land in the same place: io/fs holds one rooted namespace, and a caller who writes an absolute
16+
// path means the top of it either way. A Windows drive is the case that does not follow from the POSIX rule — it is
17+
// what makes the path absolute and it is not a separator — so "D:\a\b" left as "D:/a/b" addressed nothing, and every
18+
// read under a scan pointed at such a path failed.
19+
func TestCleanNormalisesEveryRootedForm(t *testing.T) {
20+
t.Parallel()
21+
22+
v := New(fstest.MapFS{})
23+
24+
for _, tc := range []struct {
25+
name string
26+
in string
27+
want string
28+
}{
29+
{"posix absolute", "/a/b", "a/b"},
30+
{"posix relative", "./a/b", "a/b"},
31+
{"windows drive", `D:\a\b`, "a/b"},
32+
{"windows drive, slashes", "D:/a/b", "a/b"},
33+
{"windows drive, lowercase", `c:\a\b`, "a/b"},
34+
{"windows relative", `a\b`, "a/b"},
35+
{"drive root", `D:\`, "."},
36+
{"empty", "", "."},
37+
{"root", "/", "."},
38+
{"colon that is not a drive", "ab:c/d", "ab:c/d"},
39+
} {
40+
t.Run(tc.name, func(t *testing.T) {
41+
t.Parallel()
42+
43+
assert.Equal(t, tc.want, v.Clean(tc.in))
44+
})
45+
}
46+
}
47+
48+
// Naming the last element of a directory is how the walk recognises a vendor directory, so it has to work whichever
49+
// separator the path carries: path.Base looks for a forward slash and hands a Windows path back whole, which reads as
50+
// "this directory is not called vendor" and lets the wildcard walk straight into it.
51+
func TestBaseNamesTheLastElement(t *testing.T) {
52+
t.Parallel()
53+
54+
v := New(fstest.MapFS{})
55+
56+
assert.Equal(t, "vendor", v.Base("/src/m/vendor"))
57+
assert.Equal(t, "vendor", v.Base(`D:\src\m\vendor`))
58+
assert.Equal(t, "m", v.Base("src/m"))
59+
}
60+
61+
// A walk hands paths back in the form the caller wrote them, because they become the file names in every position the
62+
// scan reports. The walk itself happens in io/fs's slash-separated namespace, so the caller's separator has to be put
63+
// back on the way out.
64+
func TestRebaseKeepsTheCallersForm(t *testing.T) {
65+
t.Parallel()
66+
67+
for _, tc := range []struct {
68+
name string
69+
root, inner, out string
70+
want string
71+
}{
72+
{"posix absolute", "/src/m", "src/m", "src/m/sub", "/src/m/sub"},
73+
{"posix root", "/", ".", "sub/pkg", "/sub/pkg"},
74+
{"relative", ".", ".", "sub", "./sub"},
75+
{"the root itself", "/src/m", "src/m", "src/m", "/src/m"},
76+
{"windows drive", `D:\src\m`, "src/m", "src/m/sub/pkg", `D:\src\m\sub\pkg`},
77+
{"windows drive root", `D:\`, ".", "sub", `D:\sub`},
78+
} {
79+
t.Run(tc.name, func(t *testing.T) {
80+
t.Parallel()
81+
82+
assert.Equal(t, tc.want, rebase(tc.root, tc.inner, tc.out))
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)
⌂ 换网址