Skip to content

Commit a0eb2d2

Browse files
fredbiclaude
andcommitted
fix(packages): resolve the standard library's own vendored imports
crypto/tls imports golang.org/x/crypto/chacha20poly1305, which the Go distribution vendors at GOROOT/src/vendor. The toolchain-free loader could not find it: the stdlib branch is gated on an import path whose first segment has no dot, and every other branch answers for the module under scan. So a scan of any tree reaching crypto/tls synthesized three x/crypto packages and said so, where the go/packages loader synthesized none. The resolver now carries a second vendor root. That is not a special case so much as the second half of a rule it already had: go mod vendor flattens the whole graph into the MAIN module's vendor directory and a dependency's own is never read, so one root answers for module mode entirely -- and std is the exception because std never goes through modload. The go command resolves its imports with the older walk that climbs from the importing package to the source root, which is how a package in GOROOT/src reaches GOROOT/src/vendor and nothing outside it does. That bound is why resolution now takes the importing directory. Both call sites already had it. The package path carries the vendor/ prefix go list reports, so the two loaders agree on identity. The stub memo needed the same: it keyed on the import path alone, and the same path has two honest answers now -- resolvable from std, not from anywhere else -- so the first asker's was handed to everybody. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent c5a6901 commit a0eb2d2

4 files changed

Lines changed: 198 additions & 13 deletions

File tree

‎internal/packages/list/resolve.go‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ type Resolver struct {
4343
modPath string // the main module's path
4444
vendor string // <modRoot>/vendor when it is authoritative (see vendorMode), else ""
4545
srcRoot string // GOROOT/src
46+
// stdVendor is GOROOT/src/vendor, the standard library's OWN vendor tree, when it has one.
47+
//
48+
// A second vendor root, because std is the second module loaded from its own tree: everything else arrives
49+
// pre-flattened, either from the module cache or from the main module's vendor directory, and `go mod vendor`
50+
// is what flattens it. A dependency's own vendor directory is never consulted - see modload/import.go, "everything
51+
// must be in the main modules or the main module's or workspace's vendor directory".
52+
//
53+
// std is the exception because std never goes through modload at all. cmd/go resolves its imports with the older
54+
// vendoredImportPath walk, which climbs from the importing package looking for a vendor directory and stops at the
55+
// source root - so a package in GOROOT/src reaches GOROOT/src/vendor and nothing outside it does.
56+
stdVendor string
4657

4758
// modDirs maps a required module path to the directory holding its source: the module cache for an ordinary
4859
// requirement, an arbitrary directory for a `replace` target.
@@ -187,7 +198,28 @@ func (r *Resolver) ResolvePatterns(patterns []string) ([]Target, error) {
187198
return out, nil
188199
}
189200

201+
// ResolveImportFrom maps an import path onto a directory, for an import made from fromDir.
202+
//
203+
// The importing directory decides one thing only, and it is the thing [Resolver.ResolveImport] cannot know: whether
204+
// the standard library's own vendor tree is in scope. It is in scope for a package inside GOROOT/src and for nothing
205+
// else, which is what keeps a user package importing golang.org/x/crypto from silently picking up the copy pinned
206+
// inside the Go installation.
207+
//
208+
// Vendor first, as the go command has it: for an importer under the source root the vendored copy IS the package,
209+
// and the flat GOROOT/src lookup would miss it anyway.
210+
func (r *Resolver) ResolveImportFrom(importPath, fromDir string) (dir, pkgPath string, ok bool) {
211+
if d, p, found := r.resolveStdVendor(importPath, fromDir); found {
212+
return d, p, true
213+
}
214+
215+
return r.ResolveImport(importPath)
216+
}
217+
190218
// ResolveImport maps an import path onto a directory.
219+
//
220+
// It answers for an import from anywhere in the main module, which is every caller that has no importer to name. Use
221+
// [Resolver.ResolveImportFrom] where the importer is known: it is the only way the standard library's own vendor tree
222+
// can be reached.
191223
func (r *Resolver) ResolveImport(importPath string) (dir, pkgPath string, ok bool) {
192224
// Standard library: GOROOT/src is laid out by import path.
193225
if !strings.Contains(firstSegment(importPath), ".") {
@@ -255,11 +287,50 @@ func (r *Resolver) InMainModule(importPath string) bool {
255287
return importPath == r.modPath || strings.HasPrefix(importPath, r.modPath+"/")
256288
}
257289

290+
// UnderGoroot reports whether an import made from dir can see the standard library's own vendor tree.
291+
//
292+
// Exported for the loader's stub memo, which must not give one importer's answer to another.
293+
func (r *Resolver) UnderGoroot(dir string) bool { return r.underSrcRoot(dir) }
294+
295+
// resolveStdVendor maps an import made from inside GOROOT onto the standard library's vendored copy of it.
296+
//
297+
// The package path carries the `vendor/` prefix, which is what `go list` reports for these and therefore what the
298+
// go/packages loader reports too - the two must agree on identity or a type recognized under one name goes unrecognized
299+
// under the other.
300+
func (r *Resolver) resolveStdVendor(importPath, fromDir string) (dir, pkgPath string, ok bool) {
301+
if r.stdVendor == "" || !r.underSrcRoot(fromDir) {
302+
return "", "", false
303+
}
304+
d := r.vfs.Join(r.stdVendor, importPath)
305+
if !r.vfs.IsDir(d) {
306+
return "", "", false
307+
}
308+
309+
return d, "vendor/" + importPath, true
310+
}
311+
312+
// underSrcRoot reports whether dir is GOROOT/src or sits inside it.
313+
//
314+
// Compared as a path rather than as a string, so that a directory merely NAMED like the source root does not qualify.
315+
func (r *Resolver) underSrcRoot(dir string) bool {
316+
if dir == "" || r.srcRoot == "" {
317+
return false
318+
}
319+
if dir == r.srcRoot {
320+
return true
321+
}
322+
323+
return strings.HasPrefix(dir, r.srcRoot+string(filepath.Separator)) || strings.HasPrefix(dir, r.srcRoot+"/")
324+
}
325+
258326
func (r *Resolver) init() error {
259327
if r.dir == "" {
260328
r.dir = "."
261329
}
262330
r.srcRoot = r.vfs.Join(r.ctx.GOROOT, "src")
331+
if v := r.vfs.Join(r.srcRoot, "vendor"); r.isVendorTree(v) {
332+
r.stdVendor = v
333+
}
263334
r.nearestMod = map[string]moduleAt{}
264335
r.ws = r.findWorkspace(r.gowork)
265336

@@ -308,6 +379,15 @@ func (r *Resolver) vendorMode(dir string) bool {
308379
if r.modFlag == "mod" {
309380
return false
310381
}
382+
383+
return r.isVendorTree(dir)
384+
}
385+
386+
// isVendorTree reports whether dir is a vendor directory `go mod vendor` wrote, by the same test the go command uses.
387+
//
388+
// Split out of [Resolver.vendorMode] for the standard library's own tree, which -mod does not speak for: that flag
389+
// chooses how the MAIN module resolves its dependencies, and says nothing about the Go installation.
390+
func (r *Resolver) isVendorTree(dir string) bool {
311391
_, err := r.vfs.ReadFile(r.vfs.Join(dir, "modules.txt"))
312392

313393
return err == nil
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 list
5+
6+
import (
7+
"go/build"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/go-openapi/codescan/internal/packages/vfs"
12+
"github.com/go-openapi/testify/v2/assert"
13+
"github.com/go-openapi/testify/v2/require"
14+
)
15+
16+
// The standard library vendors a handful of golang.org/x packages, and crypto/tls imports one of
17+
// them by its real path rather than by a std one.
18+
//
19+
// Nothing else in the resolver can find it: the stdlib branch is gated on an import path whose first
20+
// segment has no dot, and every other branch answers for the module under scan.
21+
const (
22+
stdVendored = "golang.org/x/crypto/chacha20poly1305"
23+
stdImporter = "crypto/tls"
24+
)
25+
26+
// newGorootResolver builds a resolver over the real Go installation, which is the only place a
27+
// standard library with a vendor directory can be found.
28+
func newGorootResolver(t *testing.T) *Resolver {
29+
t.Helper()
30+
31+
ctx := build.Default
32+
r, err := NewResolver(Config{Dir: t.TempDir(), Context: &ctx, FS: vfs.New(nil)})
33+
require.NoError(t, err)
34+
require.NotEmpty(t, r.srcRoot)
35+
36+
if !r.vfs.IsDir(filepath.Join(r.srcRoot, "vendor", stdVendored)) {
37+
t.Skipf("this Go installation does not vendor %s into its standard library", stdVendored)
38+
}
39+
40+
return r
41+
}
42+
43+
// TestResolveImportFrom_StdVendor covers the second vendor root: the standard library's own.
44+
//
45+
// go mod vendor flattens the whole module graph into the MAIN module's vendor directory, and a
46+
// dependency's own is never read - so one vendor root answers for module mode entirely. std is the
47+
// exception, because std never goes through modload: the go command resolves its imports with the
48+
// older walk that climbs from the importing package to the source root, which is how a package in
49+
// GOROOT/src reaches GOROOT/src/vendor and nothing outside it does.
50+
func TestResolveImportFrom_StdVendor(t *testing.T) {
51+
t.Parallel()
52+
53+
r := newGorootResolver(t)
54+
55+
t.Run("an importer inside GOROOT reaches the vendored copy", func(t *testing.T) {
56+
dir, pkgPath, ok := r.ResolveImportFrom(stdVendored, filepath.Join(r.srcRoot, stdImporter))
57+
require.True(t, ok, "crypto/tls imports this, so it has to resolve")
58+
assert.Equal(t, filepath.Join(r.srcRoot, "vendor", stdVendored), dir)
59+
60+
// The path go list reports, so that the two loaders agree on identity: a type recognized under
61+
// one name and not the other is the whole failure mode this prefix exists to avoid.
62+
assert.Equal(t, "vendor/"+stdVendored, pkgPath)
63+
})
64+
65+
// The bound is the point. Without it the Go installation's pinned copy would answer for anybody
66+
// whose own module graph came up short, which is a build the go command refuses outright.
67+
t.Run("an importer outside GOROOT does not", func(t *testing.T) {
68+
_, _, ok := r.ResolveImportFrom(stdVendored, t.TempDir())
69+
assert.False(t, ok)
70+
})
71+
72+
t.Run("and neither does one that names no importer", func(t *testing.T) {
73+
_, _, ok := r.ResolveImport(stdVendored)
74+
assert.False(t, ok)
75+
})
76+
77+
// A std import from a std importer still resolves the ordinary way: the vendor lookup is tried
78+
// first and has nothing to say about it.
79+
t.Run("a std import is unaffected", func(t *testing.T) {
80+
dir, pkgPath, ok := r.ResolveImportFrom("encoding/json", filepath.Join(r.srcRoot, stdImporter))
81+
require.True(t, ok)
82+
assert.Equal(t, filepath.Join(r.srcRoot, "encoding/json"), dir)
83+
assert.Equal(t, "encoding/json", pkgPath)
84+
})
85+
}

‎internal/packages/loader.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ func (ld *loadState) loadDir(dir, pkgPath string, root bool) (*Package, error) {
430430

431431
// Fabricate whatever this package selects through imports that will not resolve.
432432
// This way, the checker has something to bind those selectors to.
433-
ld.synthesizeFrom(syntax)
433+
ld.synthesizeFrom(syntax, dir)
434434

435435
// Only the two maps codescan reads.
436436
// [types.Info] is pure output — every map handed in is one the checker then fills for every node it visits.
@@ -442,7 +442,7 @@ func (ld *loadState) loadDir(dir, pkgPath string, root bool) (*Package, error) {
442442
Defs: map[*ast.Ident]types.Object{},
443443
}
444444
conf := types.Config{
445-
Importer: &importer{ld: ld, from: pkg},
445+
Importer: &importer{ld: ld, from: pkg, fromDir: dir},
446446
// Function bodies are checked for the packages under scan.
447447
//
448448
// An annotated type may be declared inside one (see the swagger:response in classification/operations/responses.go),
@@ -530,6 +530,11 @@ func inAnyFile(pos string, files []string) bool {
530530
type importer struct {
531531
ld *loadState
532532
from *Package
533+
// fromDir is where the importing package's source lives.
534+
//
535+
// Carried because it decides whether the standard library's own vendor tree is in scope: a package inside GOROOT/src
536+
// resolves golang.org/x/crypto/... to the copy vendored beside it, and a package anywhere else must not.
537+
fromDir string
533538
}
534539

535540
func (i *importer) Import(importPath string) (*types.Package, error) {
@@ -557,7 +562,7 @@ func (i *importer) Import(importPath string) (*types.Package, error) {
557562
}
558563
}
559564

560-
dir, pkgPath, ok := i.ld.res.ResolveImport(importPath)
565+
dir, pkgPath, ok := i.ld.res.ResolveImportFrom(importPath, i.fromDir)
561566
if ok {
562567
dep, err := i.ld.loadDir(dir, pkgPath, false)
563568
if err == nil && dep != nil && dep.Types != nil {

‎internal/packages/synthesize.go‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,49 @@ import (
2424
// This is not enough for anything structural: a synthesized type has no fields to walk and no methods,
2525
// so drilling into it, or asking whether it implements an interface, both fail.
2626

27-
// willStub reports whether an import path will be synthesized rather than loaded.
28-
func (ld *loadState) willStub(path string) bool {
27+
// willStub reports whether an import path will be synthesized rather than loaded, for an import made from fromDir.
28+
//
29+
// The importing directory is part of the question, not context around it: the standard library's vendored packages
30+
// resolve for an importer inside GOROOT/src and for no other, so the same path has two honest answers. Memoizing on
31+
// the path alone gave whichever was asked first to everybody, which is how a resolvable import came to be announced
32+
// as synthesized.
33+
func (ld *loadState) willStub(path, fromDir string) bool {
2934
if path == "unsafe" {
3035
return false
3136
}
32-
if known, ok := ld.stubbable[path]; ok {
37+
key := stubKey(path, ld.res.UnderGoroot(fromDir))
38+
if known, ok := ld.stubbable[key]; ok {
3339
return known
3440
}
3541
if ld.hasExportData(path) {
36-
ld.stubbable[path] = false
42+
ld.stubbable[key] = false
3743

3844
return false
3945
}
4046

41-
_, _, resolved := ld.res.ResolveImport(path)
42-
ld.stubbable[path] = !resolved
47+
_, _, resolved := ld.res.ResolveImportFrom(path, fromDir)
48+
ld.stubbable[key] = !resolved
4349

4450
return !resolved
4551
}
4652

53+
// stubKey distinguishes the two answers an import path can honestly have.
54+
func stubKey(path string, fromGoroot bool) string {
55+
if fromGoroot {
56+
return "goroot\x00" + path
57+
}
58+
59+
return path
60+
}
61+
4762
// synthesizeFrom records the names a package selects through its unresolvable imports, so that the synthesized packages
4863
// hold them before this package is type-checked against them.
4964
//
5065
// Names are collected per importing package, which is why this runs before each Check rather than once up front: the
5166
// set of packages is discovered lazily, as imports are followed.
52-
func (ld *loadState) synthesizeFrom(files []*ast.File) {
67+
func (ld *loadState) synthesizeFrom(files []*ast.File, fromDir string) {
5368
for _, f := range files {
54-
aliases := ld.stubbedImports(f)
69+
aliases := ld.stubbedImports(f, fromDir)
5570
if len(aliases) == 0 {
5671
continue
5772
}
@@ -75,11 +90,11 @@ func (ld *loadState) synthesizeFrom(files []*ast.File) {
7590
}
7691

7792
// stubbedImports maps the local name of each to-be-synthesized import onto its path.
78-
func (ld *loadState) stubbedImports(f *ast.File) map[string]string {
93+
func (ld *loadState) stubbedImports(f *ast.File, fromDir string) map[string]string {
7994
var aliases map[string]string
8095
for _, spec := range f.Imports {
8196
path, err := strconv.Unquote(spec.Path.Value)
82-
if err != nil || !ld.willStub(path) {
97+
if err != nil || !ld.willStub(path, fromDir) {
8398
continue
8499
}
85100

0 commit comments

Comments
 (0)
⌂ 换网址