From d0cec9b0868cc2852d2386a9bd4f87dcf2a0da0c Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:42:03 +0000 Subject: [PATCH 01/86] fix(indexer): end the store write a grace period after the deadline The indexer stores the indexes of the files that finished before the context of the run ended, so the next run does not parse them again. It wrote them with a context that never ends, so a store that hangs blocked the run past the budget of the caller with no limit. The write now ends 10 s after the context of the run ends. The error of a write that fails after the deadline also wraps the error of the context, so a caller that checks for context.DeadlineExceeded still sees it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/indexer.go | 25 +++++++++++++++++++--- plugin/indexer/indexer_test.go | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/plugin/indexer/indexer.go b/plugin/indexer/indexer.go index e3317e24..aa69afbb 100644 --- a/plugin/indexer/indexer.go +++ b/plugin/indexer/indexer.go @@ -10,6 +10,7 @@ import ( "path" "runtime" "slices" + "time" "github.com/safedep/code/core" "github.com/safedep/code/fileindex" @@ -361,13 +362,31 @@ func (r *run) flush(ctx context.Context, batch []pending) error { put, made, parseErr := r.parse(ctx, parse) // The files that finished before ctx was done hold whole indexes and // artifacts. The store keeps them past the end of ctx, so the next run - // with the same deadline does not parse them again. - if err := r.save(context.WithoutCancel(ctx), put, made); err != nil { - return err + // with the same deadline does not parse them again. A store that hangs + // must not hold the caller long past its budget, so the write ends + // saveGrace after ctx. + saveCtx, cancel := graceContext(ctx, saveGrace) + defer cancel() + if err := r.save(saveCtx, put, made); err != nil { + return errors.Join(err, ctx.Err()) } return parseErr } +// saveGrace is the time that a store write has after the context of the +// run ends. +var saveGrace = 10 * time.Second + +// graceContext returns a context that ends grace after ctx ends. +func graceContext(ctx context.Context, grace time.Duration) (context.Context, context.CancelFunc) { + out, cancel := context.WithCancel(context.WithoutCancel(ctx)) + stop := context.AfterFunc(ctx, func() { time.AfterFunc(grace, cancel) }) + return out, func() { + stop() + cancel() + } +} + // save stores the new indexes and artifacts of a batch. func (r *run) save(ctx context.Context, indexes []*fileindex.Index, artifacts []store.Artifact) error { if r.opts.Store == nil { diff --git a/plugin/indexer/indexer_test.go b/plugin/indexer/indexer_test.go index a48ac8a6..7d54f4f9 100644 --- a/plugin/indexer/indexer_test.go +++ b/plugin/indexer/indexer_test.go @@ -177,6 +177,44 @@ func TestIndexerStopsAfterTheDeadline(t *testing.T) { assert.Len(t, result.Indexes, batchSize) } +// hangingStore cancels the run at its first Put, then blocks until the +// context of the write ends. +type hangingStore struct { + store.Store + cancel context.CancelFunc +} + +func (s *hangingStore) Put(ctx context.Context, _ []*fileindex.Index) error { + s.cancel() + <-ctx.Done() + return ctx.Err() +} + +func TestIndexerEndsAHangingStoreWriteAfterTheGrace(t *testing.T) { + defer func(grace time.Duration) { saveGrace = grace }(saveGrace) + saveGrace = 50 * time.Millisecond + + dir := t.TempDir() + writeFiles(t, dir, map[string]string{"a.js": "function a() {}\n"}) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + done := make(chan error, 1) + go func() { + _, err := runIndexer(t, ctx, appFileSystem(t, dir), Options{ + Languages: languages(t, core.LanguageCodeJavascript), + Store: &hangingStore{Store: memstore.New(), cancel: cancel}, + }) + done <- err + }() + select { + case err := <-done: + assert.ErrorIs(t, err, context.Canceled) + case <-time.After(10 * time.Second): + require.Fail(t, "the store write did not end after the grace") + } +} + // recordingPlugin records the name of each file that it analyzes. type recordingPlugin struct { mu sync.Mutex From d6abc2d64460dfc9881d1bc83b227d680dd73a0d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:42:03 +0000 Subject: [PATCH 02/86] test(projectgraph): list golang.org/x/text for colly in the method corpus The build list of colly v2.1.0 holds golang.org/x/text v0.3.2, an affected version of GO-2020-0015. corpus.txt did not list it, so the method measure had no target for utf16Decoder.Transform and the govulncheck comparison left one symbol with no match. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- test/projectgraph/public/methods/corpus.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/projectgraph/public/methods/corpus.txt b/test/projectgraph/public/methods/corpus.txt index 14db9e55..8d13e151 100644 --- a/test/projectgraph/public/methods/corpus.txt +++ b/test/projectgraph/public/methods/corpus.txt @@ -3,7 +3,7 @@ # advisory of advisories.json names at its pinned version. go bluemonday github.com/microcosm-cc/bluemonday@v1.0.16 golang.org/x/net@v0.0.0-20210614182718-04defd469f4e go goquery github.com/PuerkitoBio/goquery@v1.8.0 golang.org/x/net@v0.0.0-20210916014120-12bc252f5db8 -go colly github.com/gocolly/colly/v2@v2.1.0 github.com/antchfx/xmlquery@v1.2.4 github.com/antchfx/xpath@v1.1.8 golang.org/x/crypto@v0.0.0-20190605123033-f99c8df09eb5 golang.org/x/net@v0.0.0-20200602114024-627f9648deb9 google.golang.org/protobuf@v1.24.0 +go colly github.com/gocolly/colly/v2@v2.1.0 github.com/antchfx/xmlquery@v1.2.4 github.com/antchfx/xpath@v1.1.8 golang.org/x/crypto@v0.0.0-20190605123033-f99c8df09eb5 golang.org/x/net@v0.0.0-20200602114024-627f9648deb9 golang.org/x/text@v0.3.2 google.golang.org/protobuf@v1.24.0 go hey github.com/rakyll/hey@v0.1.4 golang.org/x/net@v0.0.0-20181017193950-04a2e542c03f golang.org/x/text@v0.3.0 go melody github.com/olahol/melody@v1.1.1 github.com/gorilla/websocket@v1.5.0 go scany github.com/georgysavva/scany/v2@v2.0.0 github.com/jackc/pgx/v5@v5.0.0 golang.org/x/crypto@v0.0.0-20220829220503-c86fa9a7ed90 golang.org/x/sys@v0.0.0-20210823070655-63515b42dcdf golang.org/x/text@v0.3.7 From ec0b792ae95c9d05e5a36f5fbf8f0e9179968025 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:00:49 +0000 Subject: [PATCH 03/86] fix(projectgraph): link the implicit instance methods of Python classes A call with an unknown receiver can run an object with __call__ that a class attribute holds, or a value that __getattr__ gives. No call edge names these methods, so a target that only they call read as not-found. Python now has implicit methods, as PHP and Ruby have. A reference edge joins each class to the __call__, __getattr__, __getattribute__ and __get__ that an instance of the class runs, from the class or from a base in its MRO. Code that makes an instance reaches the class, so the search reaches the method as it reaches a stored function value. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 18 +++++ projectgraph/python.go | 27 +++++++ .../name-ambiguous-callable/expect.yaml | 35 +++++++-- .../python/name-ambiguous-getattr/expect.yaml | 33 +++++++- .../unknown-receiver-callable/expect.yaml | 78 +++++++++++++++++++ .../project/Procfile | 1 + .../project/handlers.py | 14 ++++ .../unknown-receiver-callable/project/main.py | 8 ++ .../unknown-receiver-getattr/expect.yaml | 73 +++++++++++++++++ .../unknown-receiver-getattr/project/Procfile | 1 + .../project/handlers.py | 10 +++ .../unknown-receiver-getattr/project/main.py | 8 ++ 12 files changed, 297 insertions(+), 9 deletions(-) create mode 100644 test/projectgraph/cases/python/unknown-receiver-callable/expect.yaml create mode 100644 test/projectgraph/cases/python/unknown-receiver-callable/project/Procfile create mode 100644 test/projectgraph/cases/python/unknown-receiver-callable/project/handlers.py create mode 100644 test/projectgraph/cases/python/unknown-receiver-callable/project/main.py create mode 100644 test/projectgraph/cases/python/unknown-receiver-getattr/expect.yaml create mode 100644 test/projectgraph/cases/python/unknown-receiver-getattr/project/Procfile create mode 100644 test/projectgraph/cases/python/unknown-receiver-getattr/project/handlers.py create mode 100644 test/projectgraph/cases/python/unknown-receiver-getattr/project/main.py diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 301dfcb7..69d73920 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -282,6 +282,24 @@ run nothing but a method of the name or a function value that a `python/name-ambiguous-getattr`, `python/name-ambiguous-module` and `python/name-ambiguous-nounit` show each form. +Python runs some methods of an instance with no call of their name. A +call of the instance runs `__call__`. A read of an attribute runs +`__getattribute__`, `__getattr__` for a name that the class does not +define, and `__get__` of a descriptor in a class attribute. The code that +calls or reads the instance often has an unknown receiver, or is code +outside the graph. So a `reference` edge with the rule `implicit` joins +each Python class to the method of each of these names that an instance +of the class runs, from the class or from a base in its MRO. The code that +makes the instance reaches the class, so a search from the roots reaches +the method, as it reaches a stored function value. A call with an unknown +receiver keeps its record with the method name. The target is then +found. The edge proves only that an instance exists, and not that code +calls it. The cases +`python/unknown-receiver-callable` and `python/unknown-receiver-getattr` +show it. A function that code binds to a module attribute, as +`hooks.handler = wipe`, has a `reference` edge from that code, and the +assignment has the flag `monkey-patch`, so it needs no rule of its own. + A call on the result of another call, as `html.NewTokenizer(r).Next()`, needs the type of that result. When the link step does not know it, the site is a record with the method name, so a target of that method is diff --git a/projectgraph/python.go b/projectgraph/python.go index 3f723575..2a8f6dbb 100644 --- a/projectgraph/python.go +++ b/projectgraph/python.go @@ -32,6 +32,33 @@ var pythonProfile = profile{ importBindsRoot: true, moduleNames: true, starImports: true, + implicitMethods: pythonImplicit, + link: (*builder).linkInstanceMethods, +} + +// pythonImplicit names the methods that Python runs on an instance with no +// call of the name. A call of the instance runs __call__. A read of an +// attribute runs __getattribute__, __getattr__ for a name that the class +// does not define, and __get__ of a descriptor in a class attribute. The +// code that calls or reads the instance often has an unknown receiver, or +// it is code outside the graph. No call edge then names these methods. +var pythonImplicit = map[string]bool{"__call__": true, "__getattr__": true, "__getattribute__": true, "__get__": true} + +// linkInstanceMethods adds a reference edge from each class to the method +// of each implicit name that an instance of the class runs, from the class +// or from a base in its MRO. Code that makes an instance reaches the class, +// so a search reaches these methods as it reaches a stored function value. +// Without the edge, a target that only such a method calls reads as +// not-found, as for a class attribute that holds an object with __call__. +func (b *builder) linkInstanceMethods(ix *fileindex.Index) { + names := slices.Sorted(maps.Keys(profileOf(ix.Language).implicitMethods)) + for _, c := range ix.Classes { + for _, name := range names { + if m, ok := b.ownMethod(c.Key, name); ok && b.isMethod(m) { + b.link(ix.Language, c.Key, m, b.siteOf(m), EdgeReference, ruleImplicit) + } + } + } } // pythonDependencies returns the requirements of the Python manifests of diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml index c3de824a..60539cb6 100644 --- a/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml +++ b/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml @@ -1,24 +1,49 @@ source: safedep:hand description: >- dispatch calls handle on the parameter handler. 24 classes define a method handle, so the site is over - the cap of 20 and is ambiguous. H0 holds an object of Cb in its class attribute handle. main passes an H0, so the call runs Cb.__call__, which calls os.remove. That code is no - definition of the name handle. So the site keeps no method name and concerns every target, and os.remove - is uncertain and not not-found. + the cap of 20 and is ambiguous. H0 holds an object of Cb in its class attribute handle. main passes an H0, so + the call runs Cb.__call__, which calls os.remove. That code is no definition of the name handle, so the site + keeps no method name and concerns every target. The class body of H0 makes the object, and a call of an + object of Cb runs Cb.__call__, so a reference edge joins Cb to Cb.__call__ and os.remove is found. No code + calls shutil.rmtree, and the ambiguous site makes it uncertain and not not-found. roots: - key: main.py kind: manifest rule: procfile +no_edges: + - from: main.py//dispatch + to: handlers.py//Cb//__call__ unresolved: - caller: main.py//dispatch site: main.py:4 reason: ambiguous results: - target: os//remove - status: uncertain + status: found + path: + - key: main.py + - key: handlers.py + site: main.py:1 + kind: import + confidence: high + - key: handlers.py//Cb + site: handlers.py:8 + kind: direct + confidence: high + - key: handlers.py//Cb//__call__ + site: handlers.py:4 + kind: reference + confidence: low + - key: os//remove + site: handlers.py:5 + kind: direct + confidence: high call_sites: - caller: handlers.py//Cb//__call__ site: handlers.py:5 - reached: false + reached: true + - target: shutil//rmtree + status: uncertain uncertainties: - kind: unresolved-call site: main.py:4 diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml index e5f43d55..d5d16ebf 100644 --- a/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml @@ -2,23 +2,48 @@ source: safedep:hand description: >- dispatch calls handle on the parameter handler. 24 classes define a method handle, so the site is over the cap of 20 and is ambiguous. main passes an H0, which defines no handle. Its __getattr__ gives cleanup, - which calls os.remove. That code is no definition of the name handle. So the site keeps no method name - and concerns every target, and os.remove is uncertain and not not-found. + which calls os.remove. That code is no definition of the name handle, so the site keeps no method name and + concerns every target. A read of an attribute of an H0 can run H0.__getattr__, so a reference edge joins H0 + to H0.__getattr__ and os.remove is found. No code calls shutil.rmtree, and the ambiguous site makes it + uncertain and not not-found. roots: - key: main.py kind: manifest rule: procfile +no_edges: + - from: main.py//dispatch + to: handlers.py//H0//__getattr__ unresolved: - caller: main.py//dispatch site: main.py:4 reason: ambiguous results: - target: os//remove - status: uncertain + status: found + path: + - key: main.py + - key: handlers.py//H0 + site: main.py:6 + kind: direct + confidence: high + - key: handlers.py//H0//__getattr__ + site: handlers.py:7 + kind: reference + confidence: low + - key: handlers.py//cleanup + site: handlers.py:8 + kind: reference + confidence: low + - key: os//remove + site: handlers.py:4 + kind: direct + confidence: high call_sites: - caller: handlers.py//cleanup site: handlers.py:4 - reached: false + reached: true + - target: shutil//rmtree + status: uncertain uncertainties: - kind: unresolved-call site: main.py:4 diff --git a/test/projectgraph/cases/python/unknown-receiver-callable/expect.yaml b/test/projectgraph/cases/python/unknown-receiver-callable/expect.yaml new file mode 100644 index 00000000..9035da73 --- /dev/null +++ b/test/projectgraph/cases/python/unknown-receiver-callable/expect.yaml @@ -0,0 +1,78 @@ +source: safedep:hand +description: >- + The class attribute handle of Handler holds an object of Cleaner, which inherits __call__ from Base. + dispatch calls handle on its parameter, so the receiver is unknown and the site is a record with the + method name handle. No definition of the name handle exists, and the call runs Base.__call__, which calls + os.remove. The class body of Handler makes the object, so the search reaches Cleaner, and a reference + edge joins Cleaner to the __call__ of its MRO. +roots: + - key: main.py + kind: manifest + rule: procfile +exact_edges: true +edges: + - from: handlers.py + to: handlers.py//Cleaner + kind: direct + confidence: high + site: handlers.py:14 + - from: handlers.py//Base + to: handlers.py//Base//__call__ + kind: reference + confidence: low + site: handlers.py:5 + - from: handlers.py//Base//__call__ + to: os//remove + kind: direct + confidence: high + site: handlers.py:6 + - from: handlers.py//Cleaner + to: handlers.py//Base//__call__ + kind: reference + confidence: low + site: handlers.py:5 + - from: main.py + to: handlers.py + kind: import + confidence: high + site: main.py:1 + - from: main.py + to: main.py//dispatch + kind: direct + confidence: high + site: main.py:8 + - from: main.py + to: handlers.py//Handler + kind: direct + confidence: high + site: main.py:8 +unresolved: + - caller: main.py//dispatch + site: main.py:5 + reason: unknown-receiver + method: handle +results: + - target: os//remove + status: found + path: + - key: main.py + - key: handlers.py + site: main.py:1 + kind: import + confidence: high + - key: handlers.py//Cleaner + site: handlers.py:14 + kind: direct + confidence: high + - key: handlers.py//Base//__call__ + site: handlers.py:5 + kind: reference + confidence: low + - key: os//remove + site: handlers.py:6 + kind: direct + confidence: high + call_sites: + - caller: handlers.py//Base//__call__ + site: handlers.py:6 + reached: true diff --git a/test/projectgraph/cases/python/unknown-receiver-callable/project/Procfile b/test/projectgraph/cases/python/unknown-receiver-callable/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/unknown-receiver-callable/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/unknown-receiver-callable/project/handlers.py b/test/projectgraph/cases/python/unknown-receiver-callable/project/handlers.py new file mode 100644 index 00000000..d0d61c45 --- /dev/null +++ b/test/projectgraph/cases/python/unknown-receiver-callable/project/handlers.py @@ -0,0 +1,14 @@ +import os + + +class Base: + def __call__(self, path): + os.remove(path) + + +class Cleaner(Base): + pass + + +class Handler: + handle = Cleaner() diff --git a/test/projectgraph/cases/python/unknown-receiver-callable/project/main.py b/test/projectgraph/cases/python/unknown-receiver-callable/project/main.py new file mode 100644 index 00000000..5c147caa --- /dev/null +++ b/test/projectgraph/cases/python/unknown-receiver-callable/project/main.py @@ -0,0 +1,8 @@ +from handlers import Handler + + +def dispatch(handler, path): + return handler.handle(path) + + +dispatch(Handler(), "a") diff --git a/test/projectgraph/cases/python/unknown-receiver-getattr/expect.yaml b/test/projectgraph/cases/python/unknown-receiver-getattr/expect.yaml new file mode 100644 index 00000000..97511892 --- /dev/null +++ b/test/projectgraph/cases/python/unknown-receiver-getattr/expect.yaml @@ -0,0 +1,73 @@ +source: safedep:hand +description: >- + Handler defines no handle, and its __getattr__ gives cleanup for any name. dispatch calls handle on its + parameter, so the receiver is unknown and the site is a record with the method name handle. No definition + of the name handle exists, and the call runs cleanup, which calls os.remove. main makes a Handler, and a + read of an attribute of a Handler can run Handler.__getattr__, so a reference edge joins Handler to + Handler.__getattr__. The function that __getattr__ returns is a reference of it. +roots: + - key: main.py + kind: manifest + rule: procfile +exact_edges: true +edges: + - from: handlers.py//Handler + to: handlers.py//Handler//__getattr__ + kind: reference + confidence: low + site: handlers.py:9 + - from: handlers.py//Handler//__getattr__ + to: handlers.py//cleanup + kind: reference + confidence: low + site: handlers.py:10 + - from: handlers.py//cleanup + to: os//remove + kind: direct + confidence: high + site: handlers.py:5 + - from: main.py + to: handlers.py + kind: import + confidence: high + site: main.py:1 + - from: main.py + to: main.py//dispatch + kind: direct + confidence: high + site: main.py:8 + - from: main.py + to: handlers.py//Handler + kind: direct + confidence: high + site: main.py:8 +unresolved: + - caller: main.py//dispatch + site: main.py:5 + reason: unknown-receiver + method: handle +results: + - target: os//remove + status: found + path: + - key: main.py + - key: handlers.py//Handler + site: main.py:8 + kind: direct + confidence: high + - key: handlers.py//Handler//__getattr__ + site: handlers.py:9 + kind: reference + confidence: low + - key: handlers.py//cleanup + site: handlers.py:10 + kind: reference + confidence: low + - key: os//remove + site: handlers.py:5 + kind: direct + confidence: high + call_sites: + - caller: handlers.py//cleanup + site: handlers.py:5 + reached: true diff --git a/test/projectgraph/cases/python/unknown-receiver-getattr/project/Procfile b/test/projectgraph/cases/python/unknown-receiver-getattr/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/unknown-receiver-getattr/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/unknown-receiver-getattr/project/handlers.py b/test/projectgraph/cases/python/unknown-receiver-getattr/project/handlers.py new file mode 100644 index 00000000..d0c30a13 --- /dev/null +++ b/test/projectgraph/cases/python/unknown-receiver-getattr/project/handlers.py @@ -0,0 +1,10 @@ +import os + + +def cleanup(path): + os.remove(path) + + +class Handler: + def __getattr__(self, name): + return cleanup diff --git a/test/projectgraph/cases/python/unknown-receiver-getattr/project/main.py b/test/projectgraph/cases/python/unknown-receiver-getattr/project/main.py new file mode 100644 index 00000000..5c147caa --- /dev/null +++ b/test/projectgraph/cases/python/unknown-receiver-getattr/project/main.py @@ -0,0 +1,8 @@ +from handlers import Handler + + +def dispatch(handler, path): + return handler.handle(path) + + +dispatch(Handler(), "a") From c8ffaf32207ed30afba045b6020ebde9f6d2c034 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:00:55 +0000 Subject: [PATCH 04/86] fix(rootmatch): make each component file and route file a root A framework loads a Vue, Svelte or Astro component by a route table with a dynamic import, a bundler alias, import.meta.glob or a file-based router. No static import of a reached file names such a component, so the search lost every call in it and its targets read as not-found. The rule node.component marks each component file as a root. The rules node.sveltekit.route and node.astro.page mark the route files of SvelteKit and the pages of Astro with their exports, as load of +page.ts and POST of +server.ts. A new rule field keeps_fallback stops these roots from turning off the library-surface roots, because they name the files that a framework loads and not the entry, as the index.html of a Vite app. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 24 +++- fileindex/roots/rules.go | 15 ++- fileindex/roots/rules.yaml | 19 +++ fileindex/roots/rules_test.go | 3 +- projectgraph/roots.go | 11 +- .../cases/javascript/astro-markup/expect.yaml | 7 +- .../cases/javascript/astro-pages/expect.yaml | 69 ++++++++++ .../astro-pages/project/package.json | 5 + .../javascript/astro-pages/project/server.js | 3 + .../astro-pages/project/src/pages/api/data.ts | 5 + .../astro-pages/project/src/pages/index.astro | 5 + .../cases/javascript/nuxt-pages/expect.yaml | 65 ++++++++++ .../nuxt-pages/project/layouts/default.vue | 9 ++ .../nuxt-pages/project/package.json | 5 + .../nuxt-pages/project/pages/index.vue | 9 ++ .../javascript/nuxt-pages/project/server.js | 3 + .../javascript/svelte-markup/expect.yaml | 7 +- .../javascript/sveltekit-routes/expect.yaml | 121 ++++++++++++++++++ .../sveltekit-routes/project/package.json | 5 + .../sveltekit-routes/project/server.js | 3 + .../project/src/routes/+layout.svelte | 6 + .../project/src/routes/+page.svelte | 6 + .../project/src/routes/+page.ts | 5 + .../project/src/routes/api/+server.ts | 5 + .../javascript/vue-alias-import/expect.yaml | 56 ++++++++ .../vue-alias-import/project/package.json | 5 + .../project/src/components/Panel.vue | 9 ++ .../vue-alias-import/project/src/main.js | 4 + .../vue-import-meta-glob/expect.yaml | 66 ++++++++++ .../vue-import-meta-glob/project/package.json | 5 + .../vue-import-meta-glob/project/src/main.js | 8 ++ .../project/src/pages/About.vue | 9 ++ .../javascript/vue-options-api/expect.yaml | 14 +- .../vue-route-dynamic-import/expect.yaml | 90 +++++++++++++ .../project/package.json | 5 + .../project/src/main.js | 5 + .../project/src/router.js | 7 + .../project/src/views/Home.vue | 9 ++ .../javascript/vue-script-src/expect.yaml | 7 +- .../javascript/vue-setup-handler/expect.yaml | 3 + .../vue-template-markup/expect.yaml | 7 +- .../javascript/vue-vite-fallback/expect.yaml | 81 ++++++++++++ .../vue-vite-fallback/project/index.html | 2 + .../vue-vite-fallback/project/package.json | 5 + .../vue-vite-fallback/project/src/App.vue | 9 ++ .../vue-vite-fallback/project/src/main.js | 6 + 46 files changed, 796 insertions(+), 31 deletions(-) create mode 100644 test/projectgraph/cases/javascript/astro-pages/expect.yaml create mode 100644 test/projectgraph/cases/javascript/astro-pages/project/package.json create mode 100644 test/projectgraph/cases/javascript/astro-pages/project/server.js create mode 100644 test/projectgraph/cases/javascript/astro-pages/project/src/pages/api/data.ts create mode 100644 test/projectgraph/cases/javascript/astro-pages/project/src/pages/index.astro create mode 100644 test/projectgraph/cases/javascript/nuxt-pages/expect.yaml create mode 100644 test/projectgraph/cases/javascript/nuxt-pages/project/layouts/default.vue create mode 100644 test/projectgraph/cases/javascript/nuxt-pages/project/package.json create mode 100644 test/projectgraph/cases/javascript/nuxt-pages/project/pages/index.vue create mode 100644 test/projectgraph/cases/javascript/nuxt-pages/project/server.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-routes/expect.yaml create mode 100644 test/projectgraph/cases/javascript/sveltekit-routes/project/package.json create mode 100644 test/projectgraph/cases/javascript/sveltekit-routes/project/server.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+layout.svelte create mode 100644 test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+page.svelte create mode 100644 test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+page.ts create mode 100644 test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/api/+server.ts create mode 100644 test/projectgraph/cases/javascript/vue-alias-import/expect.yaml create mode 100644 test/projectgraph/cases/javascript/vue-alias-import/project/package.json create mode 100644 test/projectgraph/cases/javascript/vue-alias-import/project/src/components/Panel.vue create mode 100644 test/projectgraph/cases/javascript/vue-alias-import/project/src/main.js create mode 100644 test/projectgraph/cases/javascript/vue-import-meta-glob/expect.yaml create mode 100644 test/projectgraph/cases/javascript/vue-import-meta-glob/project/package.json create mode 100644 test/projectgraph/cases/javascript/vue-import-meta-glob/project/src/main.js create mode 100644 test/projectgraph/cases/javascript/vue-import-meta-glob/project/src/pages/About.vue create mode 100644 test/projectgraph/cases/javascript/vue-route-dynamic-import/expect.yaml create mode 100644 test/projectgraph/cases/javascript/vue-route-dynamic-import/project/package.json create mode 100644 test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/main.js create mode 100644 test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/router.js create mode 100644 test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/views/Home.vue create mode 100644 test/projectgraph/cases/javascript/vue-vite-fallback/expect.yaml create mode 100644 test/projectgraph/cases/javascript/vue-vite-fallback/project/index.html create mode 100644 test/projectgraph/cases/javascript/vue-vite-fallback/project/package.json create mode 100644 test/projectgraph/cases/javascript/vue-vite-fallback/project/src/App.vue create mode 100644 test/projectgraph/cases/javascript/vue-vite-fallback/project/src/main.js diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 69d73920..22d8993f 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -630,7 +630,29 @@ file imports it. With no root of kind `main`, `manifest` or `framework`, every exported definition is a root, and `Graph.Stats.RootFallback` says so. Each result -of a reach query then carries a `root-fallback` uncertainty. +of a reach query then carries a `root-fallback` uncertainty. A root of a +rule with `keeps_fallback` does not count here. Such a rule names files +that a framework loads, but not the entry that loads them. + +A framework loads a component file of Vue, Svelte or Astro in ways that no +static import shows: a route table with `() => import('./views/X.vue')`, +an alias of the bundler as `@/`, `import.meta.glob`, or a file-based +router, as the `pages` of Nuxt. So the rule `node.component` marks each +component file as a root, with no `requires`. A component file holds a +`markup` record, so this root makes each target that the search does not +find read `uncertain` in a project with a component file. A static import +of a component file already did that in most such projects. The rule keeps +the fallback on, because a Vite app starts at `index.html`, which no rule +reads. The rules `node.sveltekit.route` and `node.astro.page` mark the +route files of SvelteKit and the pages of Astro, as `+page.ts`, +`+server.ts` and `src/pages/api/data.ts`, with their exports. They keep +the fallback on too, because a framework can load a file by a convention +that a rule does not list. Nuxt gets no rule of its own: it imports the +code of `composables` and `utils` with no import statement, so no list of +paths is complete. The cases `javascript/vue-route-dynamic-import`, +`javascript/vue-alias-import`, `javascript/vue-import-meta-glob`, +`javascript/nuxt-pages`, `javascript/sveltekit-routes`, +`javascript/astro-pages` and `javascript/vue-vite-fallback` show each form. ## Sources diff --git a/fileindex/roots/rules.go b/fileindex/roots/rules.go index c0b4826a..3c1936ec 100644 --- a/fileindex/roots/rules.go +++ b/fileindex/roots/rules.go @@ -57,13 +57,16 @@ var manifestFields = map[string][]string{ // on one of them: a package.json, a pyproject.toml, a setup.py, a Pipfile, // a requirements file, a go.mod, a composer.json or a Cargo.toml. A project // whose manifests declare no dependency gives no evidence, so the rule -// applies. +// applies. KeepsFallback marks a rule whose roots do not turn off the +// library-surface roots. Such a rule names files that a framework loads, +// but not the entry that loads them, as the index.html of a Vite app. type Rule struct { - ID string `yaml:"id"` - Language string `yaml:"language"` - Kind Kind `yaml:"kind"` - Requires []string `yaml:"requires,omitempty"` - Match Match `yaml:"match"` + ID string `yaml:"id"` + Language string `yaml:"language"` + Kind Kind `yaml:"kind"` + Requires []string `yaml:"requires,omitempty"` + KeepsFallback bool `yaml:"keeps_fallback,omitempty"` + Match Match `yaml:"match"` } // MatchKey names one key of a Match. diff --git a/fileindex/roots/rules.yaml b/fileindex/roots/rules.yaml index a2a57cb3..c8d44e21 100644 --- a/fileindex/roots/rules.yaml +++ b/fileindex/roots/rules.yaml @@ -3,6 +3,8 @@ # A callee is a symbol key as the graph writes it, as django//urls//path for # path of django.urls. requires names the packages of the framework of a # rule, and the rule applies only to a project that depends on one of them. +# keeps_fallback marks a rule whose roots do not turn off the library-surface +# roots, because it names the files that a framework loads and not the entry. version: 1 rules: - id: go.main @@ -92,6 +94,23 @@ rules: kind: framework requires: [next] match: { path: ["app/**/route.*", "app/**/page.*", "app/**/layout.*", "src/app/**/route.*", "src/app/**/page.*", "src/app/**/layout.*", "pages/**", "src/pages/**"] } + - id: node.component + language: javascript + kind: framework + keeps_fallback: true + match: { path: ["*.vue", "*.svelte", "*.astro"] } + - id: node.sveltekit.route + language: javascript + kind: framework + requires: ["@sveltejs/kit"] + keeps_fallback: true + match: { path: ["**/src/routes/**/+*", "**/src/hooks.*", "**/src/params/*", "**/src/service-worker.*", "**/src/service-worker/index.*"] } + - id: node.astro.page + language: javascript + kind: framework + requires: [astro] + keeps_fallback: true + match: { path: ["**/src/pages/**", "**/src/middleware.*", "**/src/middleware/index.*"] } - id: node.serverless.handler language: javascript kind: framework diff --git a/fileindex/roots/rules_test.go b/fileindex/roots/rules_test.go index f1ea43ac..f87a26c4 100644 --- a/fileindex/roots/rules_test.go +++ b/fileindex/roots/rules_test.go @@ -20,7 +20,8 @@ func TestDefaultRules(t *testing.T) { "python.project-scripts", "python.setup-py", "procfile", "dockerfile", "python.flask.route", "python.fastapi.route", "python.django.urls", "python.django.view", "python.click.command", "python.click.subcommand", "python.typer.command", "node.express.route", "node.koa.route", - "node.commander.action", "node.next.route", "node.serverless.handler", "java.spring.web", "java.spring.component", + "node.commander.action", "node.next.route", "node.component", "node.sveltekit.route", "node.astro.page", + "node.serverless.handler", "java.spring.web", "java.spring.component", "csharp.main", "csharp.top-level-statements", "csharp.aspnet.action", "csharp.aspnet.controllers", "php.script", "php.composer-bin", "php.laravel.routes", "php.laravel.controllers", "php.symfony.attribute", "ruby.main-guard", "ruby.script", "ruby.rails.app", diff --git a/projectgraph/roots.go b/projectgraph/roots.go index aa6d7d79..6077f93f 100644 --- a/projectgraph/roots.go +++ b/projectgraph/roots.go @@ -16,8 +16,9 @@ import ( // markRoots adds the roots that the rules mark. With no root of kind main, // manifest or framework, every exported definition and every module top // level of a file that is not a test becomes a root of kind -// library-surface. Dependency code runs only when the project calls it, so -// it holds no root. +// library-surface. A root of a rule that keeps the fallback does not count +// here. Dependency code runs only when the project calls it, so it holds no +// root. func (b *builder) markRoots(ctx context.Context) error { indexes := make([]*fileindex.Index, 0, len(b.byPath)) for _, p := range b.paths { @@ -48,11 +49,15 @@ func (b *builder) markRoots(ctx context.Context) error { } b.problems = append(b.problems, problems...) + keeps := map[string]bool{} + for _, r := range b.rules { + keeps[r.ID] = r.KeepsFallback + } entry := false for _, r := range found { b.addRootAt(r.Key, r.Kind, r.Rule, Site(r.Site)) helper := b.profileAt(r.Key).helperProgram - entry = entry || r.Kind != RootTest && (helper == nil || !helper(fileindex.Unit(r.Key))) + entry = entry || r.Kind != RootTest && !keeps[r.Rule] && (helper == nil || !helper(fileindex.Unit(r.Key))) // A Go program initializes its package before main runs, so the // module top level of each file of the package is a root too. if u := b.unitOf[fileindex.Unit(r.Key)]; r.Kind == RootMain && u != nil && b.profileAt(r.Key).loadsPackages { diff --git a/test/projectgraph/cases/javascript/astro-markup/expect.yaml b/test/projectgraph/cases/javascript/astro-markup/expect.yaml index 166ed1f0..0f7d34a6 100644 --- a/test/projectgraph/cases/javascript/astro-markup/expect.yaml +++ b/test/projectgraph/cases/javascript/astro-markup/expect.yaml @@ -1,6 +1,9 @@ source: safedep:hand description: An expression in the body of Page.astro calls adanger, which its fence imports. The index does not parse the body, so the file has a markup record, and adanger reads uncertain, not no-such-target. roots: + - key: src/Page.astro + kind: framework + rule: node.component - key: src/main.js kind: manifest rule: node.package-json @@ -24,11 +27,7 @@ results: - target: plug//asetup status: found path: - - key: src/main.js - key: src/Page.astro - site: src/main.js:1 - kind: import - confidence: high - key: plug//asetup site: src/Page.astro:3 kind: direct diff --git a/test/projectgraph/cases/javascript/astro-pages/expect.yaml b/test/projectgraph/cases/javascript/astro-pages/expect.yaml new file mode 100644 index 00000000..ae9554e3 --- /dev/null +++ b/test/projectgraph/cases/javascript/astro-pages/expect.yaml @@ -0,0 +1,69 @@ +source: safedep:hand +description: >- + Astro loads each file of src/pages by its path, and no file imports it. Each component file is a root + of the rule node.component, and the rule node.astro.page marks each file of src/pages and its exports. + So danger of index.astro and store of the endpoint GET of api/data.ts are found. +roots: + - key: server.js + kind: manifest + rule: node.package-json + - key: src/pages/api/data.ts + kind: framework + rule: node.astro.page + - key: src/pages/api/data.ts//GET + kind: framework + rule: node.astro.page + - key: src/pages/index.astro + kind: framework + rule: node.astro.page + - key: src/pages/index.astro + kind: framework + rule: node.component +exact_edges: true +edges: + - from: server.js + to: plug//listen + kind: direct + confidence: high + site: server.js:3 + - from: src/pages/api/data.ts//GET + to: plug//store + kind: direct + confidence: high + site: src/pages/api/data.ts:4 + - from: src/pages/index.astro + to: plug//danger + kind: direct + confidence: high + site: src/pages/index.astro:3 +unresolved: + - caller: src/pages/index.astro + site: src/pages/index.astro:1 + reason: markup +results: + - target: plug//danger + status: found + path: + - key: src/pages/index.astro + root: framework + - key: plug//danger + site: src/pages/index.astro:3 + kind: direct + confidence: high + call_sites: + - caller: src/pages/index.astro + site: src/pages/index.astro:3 + reached: true + - target: plug//store + status: found + path: + - key: src/pages/api/data.ts//GET + root: framework + - key: plug//store + site: src/pages/api/data.ts:4 + kind: direct + confidence: high + call_sites: + - caller: src/pages/api/data.ts//GET + site: src/pages/api/data.ts:4 + reached: true diff --git a/test/projectgraph/cases/javascript/astro-pages/project/package.json b/test/projectgraph/cases/javascript/astro-pages/project/package.json new file mode 100644 index 00000000..7c47ca78 --- /dev/null +++ b/test/projectgraph/cases/javascript/astro-pages/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "astro-pages", + "main": "server.js", + "dependencies": { "astro": "^4.5.0" } +} diff --git a/test/projectgraph/cases/javascript/astro-pages/project/server.js b/test/projectgraph/cases/javascript/astro-pages/project/server.js new file mode 100644 index 00000000..fc1cd6f8 --- /dev/null +++ b/test/projectgraph/cases/javascript/astro-pages/project/server.js @@ -0,0 +1,3 @@ +import { listen } from 'plug'; + +listen(3000); diff --git a/test/projectgraph/cases/javascript/astro-pages/project/src/pages/api/data.ts b/test/projectgraph/cases/javascript/astro-pages/project/src/pages/api/data.ts new file mode 100644 index 00000000..b218695a --- /dev/null +++ b/test/projectgraph/cases/javascript/astro-pages/project/src/pages/api/data.ts @@ -0,0 +1,5 @@ +import { store } from 'plug'; + +export function GET() { + return store(); +} diff --git a/test/projectgraph/cases/javascript/astro-pages/project/src/pages/index.astro b/test/projectgraph/cases/javascript/astro-pages/project/src/pages/index.astro new file mode 100644 index 00000000..81fdf4a9 --- /dev/null +++ b/test/projectgraph/cases/javascript/astro-pages/project/src/pages/index.astro @@ -0,0 +1,5 @@ +--- +import { danger } from 'plug'; +danger(); +--- +

home

diff --git a/test/projectgraph/cases/javascript/nuxt-pages/expect.yaml b/test/projectgraph/cases/javascript/nuxt-pages/expect.yaml new file mode 100644 index 00000000..bf72ed4f --- /dev/null +++ b/test/projectgraph/cases/javascript/nuxt-pages/expect.yaml @@ -0,0 +1,65 @@ +source: safedep:hand +description: >- + Nuxt loads pages/index.vue and layouts/default.vue by their paths, and no file imports them. Each + component file is a root of the rule node.component, so the calls in their scripts are found. +roots: + - key: layouts/default.vue + kind: framework + rule: node.component + - key: pages/index.vue + kind: framework + rule: node.component + - key: server.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: layouts/default.vue + to: plug//frame + kind: direct + confidence: high + site: layouts/default.vue:4 + - from: pages/index.vue + to: plug//danger + kind: direct + confidence: high + site: pages/index.vue:4 + - from: server.js + to: plug//listen + kind: direct + confidence: high + site: server.js:3 +unresolved: + - caller: layouts/default.vue + site: layouts/default.vue:1 + reason: markup + - caller: pages/index.vue + site: pages/index.vue:1 + reason: markup +results: + - target: plug//danger + status: found + path: + - key: pages/index.vue + root: framework + - key: plug//danger + site: pages/index.vue:4 + kind: direct + confidence: high + call_sites: + - caller: pages/index.vue + site: pages/index.vue:4 + reached: true + - target: plug//frame + status: found + path: + - key: layouts/default.vue + root: framework + - key: plug//frame + site: layouts/default.vue:4 + kind: direct + confidence: high + call_sites: + - caller: layouts/default.vue + site: layouts/default.vue:4 + reached: true diff --git a/test/projectgraph/cases/javascript/nuxt-pages/project/layouts/default.vue b/test/projectgraph/cases/javascript/nuxt-pages/project/layouts/default.vue new file mode 100644 index 00000000..62892847 --- /dev/null +++ b/test/projectgraph/cases/javascript/nuxt-pages/project/layouts/default.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/javascript/nuxt-pages/project/package.json b/test/projectgraph/cases/javascript/nuxt-pages/project/package.json new file mode 100644 index 00000000..d6b28003 --- /dev/null +++ b/test/projectgraph/cases/javascript/nuxt-pages/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "nuxt-pages", + "main": "server.js", + "dependencies": { "nuxt": "^3.11.0" } +} diff --git a/test/projectgraph/cases/javascript/nuxt-pages/project/pages/index.vue b/test/projectgraph/cases/javascript/nuxt-pages/project/pages/index.vue new file mode 100644 index 00000000..bce26238 --- /dev/null +++ b/test/projectgraph/cases/javascript/nuxt-pages/project/pages/index.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/javascript/nuxt-pages/project/server.js b/test/projectgraph/cases/javascript/nuxt-pages/project/server.js new file mode 100644 index 00000000..fc1cd6f8 --- /dev/null +++ b/test/projectgraph/cases/javascript/nuxt-pages/project/server.js @@ -0,0 +1,3 @@ +import { listen } from 'plug'; + +listen(3000); diff --git a/test/projectgraph/cases/javascript/svelte-markup/expect.yaml b/test/projectgraph/cases/javascript/svelte-markup/expect.yaml index dbc823b2..47a63079 100644 --- a/test/projectgraph/cases/javascript/svelte-markup/expect.yaml +++ b/test/projectgraph/cases/javascript/svelte-markup/expect.yaml @@ -1,6 +1,9 @@ source: safedep:hand description: An event handler in the markup of Other.svelte calls sdanger, which its script imports. The index does not parse the markup, so the file has a markup record, and sdanger reads uncertain, not no-such-target. roots: + - key: src/Other.svelte + kind: framework + rule: node.component - key: src/main.js kind: manifest rule: node.package-json @@ -29,11 +32,7 @@ results: - target: plug//ssetup status: found path: - - key: src/main.js - key: src/Other.svelte - site: src/main.js:2 - kind: import - confidence: high - key: plug//ssetup site: src/Other.svelte:3 kind: direct diff --git a/test/projectgraph/cases/javascript/sveltekit-routes/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-routes/expect.yaml new file mode 100644 index 00000000..5b5997b6 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-routes/expect.yaml @@ -0,0 +1,121 @@ +source: safedep:hand +description: >- + SvelteKit loads the files of src/routes by their names, and no file imports them. Each component file + is a root of the rule node.component. The rule node.sveltekit.route marks each route file and its + exports, so load of +page.ts and POST of +server.ts are roots too, and store and fetchData are found + and not only uncertain by the markup records. +roots: + - key: server.js + kind: manifest + rule: node.package-json + - key: src/routes/+layout.svelte + kind: framework + rule: node.component + - key: src/routes/+layout.svelte + kind: framework + rule: node.sveltekit.route + - key: src/routes/+page.svelte + kind: framework + rule: node.component + - key: src/routes/+page.svelte + kind: framework + rule: node.sveltekit.route + - key: src/routes/+page.ts + kind: framework + rule: node.sveltekit.route + - key: src/routes/+page.ts//load + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/+server.ts + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/+server.ts//POST + kind: framework + rule: node.sveltekit.route +exact_edges: true +edges: + - from: server.js + to: plug//listen + kind: direct + confidence: high + site: server.js:3 + - from: src/routes/+layout.svelte + to: plug//frame + kind: direct + confidence: high + site: src/routes/+layout.svelte:3 + - from: src/routes/+page.svelte + to: plug//danger + kind: direct + confidence: high + site: src/routes/+page.svelte:3 + - from: src/routes/+page.ts//load + to: plug//fetchData + kind: direct + confidence: high + site: src/routes/+page.ts:4 + - from: src/routes/api/+server.ts//POST + to: plug//store + kind: direct + confidence: high + site: src/routes/api/+server.ts:4 +unresolved: + - caller: src/routes/+layout.svelte + site: src/routes/+layout.svelte:1 + reason: markup + - caller: src/routes/+page.svelte + site: src/routes/+page.svelte:1 + reason: markup +results: + - target: plug//danger + status: found + path: + - key: src/routes/+page.svelte + root: framework + - key: plug//danger + site: src/routes/+page.svelte:3 + kind: direct + confidence: high + call_sites: + - caller: src/routes/+page.svelte + site: src/routes/+page.svelte:3 + reached: true + - target: plug//frame + status: found + path: + - key: src/routes/+layout.svelte + root: framework + - key: plug//frame + site: src/routes/+layout.svelte:3 + kind: direct + confidence: high + call_sites: + - caller: src/routes/+layout.svelte + site: src/routes/+layout.svelte:3 + reached: true + - target: plug//fetchData + status: found + path: + - key: src/routes/+page.ts//load + root: framework + - key: plug//fetchData + site: src/routes/+page.ts:4 + kind: direct + confidence: high + call_sites: + - caller: src/routes/+page.ts//load + site: src/routes/+page.ts:4 + reached: true + - target: plug//store + status: found + path: + - key: src/routes/api/+server.ts//POST + root: framework + - key: plug//store + site: src/routes/api/+server.ts:4 + kind: direct + confidence: high + call_sites: + - caller: src/routes/api/+server.ts//POST + site: src/routes/api/+server.ts:4 + reached: true diff --git a/test/projectgraph/cases/javascript/sveltekit-routes/project/package.json b/test/projectgraph/cases/javascript/sveltekit-routes/project/package.json new file mode 100644 index 00000000..da1b56d0 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-routes/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "sveltekit-routes", + "main": "server.js", + "devDependencies": { "@sveltejs/kit": "^2.5.0", "svelte": "^4.2.0" } +} diff --git a/test/projectgraph/cases/javascript/sveltekit-routes/project/server.js b/test/projectgraph/cases/javascript/sveltekit-routes/project/server.js new file mode 100644 index 00000000..fc1cd6f8 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-routes/project/server.js @@ -0,0 +1,3 @@ +import { listen } from 'plug'; + +listen(3000); diff --git a/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+layout.svelte b/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+layout.svelte new file mode 100644 index 00000000..a5974fc3 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+layout.svelte @@ -0,0 +1,6 @@ + + + diff --git a/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+page.svelte b/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+page.svelte new file mode 100644 index 00000000..bdb3e7fa --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+page.svelte @@ -0,0 +1,6 @@ + + +

home

diff --git a/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+page.ts b/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+page.ts new file mode 100644 index 00000000..1cea69c2 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/+page.ts @@ -0,0 +1,5 @@ +import { fetchData } from 'plug'; + +export function load() { + return fetchData(); +} diff --git a/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/api/+server.ts b/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/api/+server.ts new file mode 100644 index 00000000..5b0d767b --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-routes/project/src/routes/api/+server.ts @@ -0,0 +1,5 @@ +import { store } from 'plug'; + +export function POST() { + return store(); +} diff --git a/test/projectgraph/cases/javascript/vue-alias-import/expect.yaml b/test/projectgraph/cases/javascript/vue-alias-import/expect.yaml new file mode 100644 index 00000000..44e1159f --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-alias-import/expect.yaml @@ -0,0 +1,56 @@ +source: safedep:hand +description: >- + main.js imports Panel.vue by the alias @/ of the bundler, and the project has no tsconfig paths, so the + import names an external key and no path leads to Panel.vue. Each component file is a root of the rule + node.component, so the call of danger in the script of Panel.vue is found. +roots: + - key: src/components/Panel.vue + kind: framework + rule: node.component + - key: src/main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: src/components/Panel.vue + to: plug//danger + kind: direct + confidence: high + site: src/components/Panel.vue:4 + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:4 + - from: src/main.js + to: vue//createApp//mount + kind: direct + confidence: high + site: src/main.js:4 + - from: src/main.js + to: "@//components//Panel.vue" + kind: reference + confidence: low + site: src/main.js:4 +unresolved: + - caller: src/components/Panel.vue + site: src/components/Panel.vue:1 + reason: markup + - caller: src/main.js + site: src/main.js:4 + reason: no-dependency-source + method: mount +results: + - target: plug//danger + status: found + path: + - key: src/components/Panel.vue + root: framework + - key: plug//danger + site: src/components/Panel.vue:4 + kind: direct + confidence: high + call_sites: + - caller: src/components/Panel.vue + site: src/components/Panel.vue:4 + reached: true diff --git a/test/projectgraph/cases/javascript/vue-alias-import/project/package.json b/test/projectgraph/cases/javascript/vue-alias-import/project/package.json new file mode 100644 index 00000000..e9d60369 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-alias-import/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "vue-alias-import", + "main": "src/main.js", + "dependencies": { "vue": "^3.4.0" } +} diff --git a/test/projectgraph/cases/javascript/vue-alias-import/project/src/components/Panel.vue b/test/projectgraph/cases/javascript/vue-alias-import/project/src/components/Panel.vue new file mode 100644 index 00000000..87537a4f --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-alias-import/project/src/components/Panel.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-alias-import/project/src/main.js b/test/projectgraph/cases/javascript/vue-alias-import/project/src/main.js new file mode 100644 index 00000000..000dc0df --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-alias-import/project/src/main.js @@ -0,0 +1,4 @@ +import { createApp } from 'vue'; +import Panel from '@/components/Panel.vue'; + +createApp(Panel).mount('#app'); diff --git a/test/projectgraph/cases/javascript/vue-import-meta-glob/expect.yaml b/test/projectgraph/cases/javascript/vue-import-meta-glob/expect.yaml new file mode 100644 index 00000000..99876d8a --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-import-meta-glob/expect.yaml @@ -0,0 +1,66 @@ +source: safedep:hand +description: >- + main.js loads each page of src/pages by import.meta.glob, which the index does not read, so no path + leads to About.vue. Each component file is a root of the rule node.component, so the call of danger in + the script of About.vue is found. +roots: + - key: src/main.js + kind: manifest + rule: node.package-json + - key: src/pages/About.vue + kind: framework + rule: node.component +exact_edges: true +edges: + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:4 + - from: src/main.js + to: Object//entries + kind: direct + confidence: high + site: src/main.js:5 + - from: src/main.js + to: vue//createApp//component + kind: direct + confidence: high + site: src/main.js:6 + - from: src/main.js + to: vue//defineAsyncComponent + kind: direct + confidence: high + site: src/main.js:6 + - from: src/main.js + to: vue//createApp//mount + kind: direct + confidence: high + site: src/main.js:8 + - from: src/pages/About.vue + to: plug//danger + kind: direct + confidence: high + site: src/pages/About.vue:4 +unresolved: + - caller: src/main.js + site: src/main.js:3 + reason: unknown-receiver + method: glob + - caller: src/pages/About.vue + site: src/pages/About.vue:1 + reason: markup +results: + - target: plug//danger + status: found + path: + - key: src/pages/About.vue + root: framework + - key: plug//danger + site: src/pages/About.vue:4 + kind: direct + confidence: high + call_sites: + - caller: src/pages/About.vue + site: src/pages/About.vue:4 + reached: true diff --git a/test/projectgraph/cases/javascript/vue-import-meta-glob/project/package.json b/test/projectgraph/cases/javascript/vue-import-meta-glob/project/package.json new file mode 100644 index 00000000..55893cc5 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-import-meta-glob/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "vue-import-meta-glob", + "main": "src/main.js", + "dependencies": { "vue": "^3.4.0" } +} diff --git a/test/projectgraph/cases/javascript/vue-import-meta-glob/project/src/main.js b/test/projectgraph/cases/javascript/vue-import-meta-glob/project/src/main.js new file mode 100644 index 00000000..d2ec149b --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-import-meta-glob/project/src/main.js @@ -0,0 +1,8 @@ +import { createApp, defineAsyncComponent } from 'vue'; + +const pages = import.meta.glob('./pages/*.vue'); +const app = createApp({}); +for (const [path, load] of Object.entries(pages)) { + app.component(path, defineAsyncComponent(load)); +} +app.mount('#app'); diff --git a/test/projectgraph/cases/javascript/vue-import-meta-glob/project/src/pages/About.vue b/test/projectgraph/cases/javascript/vue-import-meta-glob/project/src/pages/About.vue new file mode 100644 index 00000000..220168de --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-import-meta-glob/project/src/pages/About.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-options-api/expect.yaml b/test/projectgraph/cases/javascript/vue-options-api/expect.yaml index 3ff5fd4a..cbe23b23 100644 --- a/test/projectgraph/cases/javascript/vue-options-api/expect.yaml +++ b/test/projectgraph/cases/javascript/vue-options-api/expect.yaml @@ -1,6 +1,12 @@ source: safedep:hand description: Vue calls the methods and the hooks of the options object of a component. No code of the project calls go of Opt.vue or mounted of Hooks.vue. Hooks.vue has no template, and it still has a markup record, so hdanger reads uncertain and not not-found. roots: + - key: src/Hooks.vue + kind: framework + rule: node.component + - key: src/Opt.vue + kind: framework + rule: node.component - key: src/main.js kind: manifest rule: node.package-json @@ -51,10 +57,10 @@ results: site: src/Opt.vue:11 reached: false uncertainties: - - kind: unresolved-call - site: src/Opt.vue:1 - kind: unresolved-call site: src/Hooks.vue:1 + - kind: unresolved-call + site: src/Opt.vue:1 - target: plug//hdanger status: uncertain call_sites: @@ -62,7 +68,7 @@ results: site: src/Hooks.vue:6 reached: false uncertainties: - - kind: unresolved-call - site: src/Opt.vue:1 - kind: unresolved-call site: src/Hooks.vue:1 + - kind: unresolved-call + site: src/Opt.vue:1 diff --git a/test/projectgraph/cases/javascript/vue-route-dynamic-import/expect.yaml b/test/projectgraph/cases/javascript/vue-route-dynamic-import/expect.yaml new file mode 100644 index 00000000..da290c03 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-route-dynamic-import/expect.yaml @@ -0,0 +1,90 @@ +source: safedep:hand +description: >- + The route table of router.js loads Home.vue by a dynamic import in an arrow function that vue-router + calls. No code of the graph calls the arrow function, so no path leads to Home.vue. Each component file + is a root of the rule node.component, so the call of danger in the script of Home.vue is found. +roots: + - key: src/main.js + kind: manifest + rule: node.package-json + - key: src/views/Home.vue + kind: framework + rule: node.component +exact_edges: true +edges: + - from: src/main.js + to: src/router.js + kind: import + confidence: high + site: src/main.js:3 + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:5 + - from: src/main.js + to: vue//createApp//use + kind: direct + confidence: high + site: src/main.js:5 + - from: src/main.js + to: vue//createApp//use//mount + kind: direct + confidence: high + site: src/main.js:5 + - from: src/main.js//render + to: vue//h + kind: direct + confidence: high + site: src/main.js:5 + - from: src/main.js//render + to: vue-router//RouterView + kind: reference + confidence: low + site: src/main.js:5 + - from: src/router.js + to: vue-router//createRouter + kind: direct + confidence: high + site: src/router.js:7 + - from: src/router.js + to: vue-router//createWebHistory + kind: direct + confidence: high + site: src/router.js:7 + - from: src/router.js//component + to: src/views/Home.vue + kind: import + confidence: high + site: src/router.js:4 + - from: src/views/Home.vue + to: plug//danger + kind: direct + confidence: high + site: src/views/Home.vue:4 +unresolved: + - caller: src/main.js + site: src/main.js:5 + reason: no-dependency-source + method: use + - caller: src/main.js + site: src/main.js:5 + reason: no-dependency-source + method: mount + - caller: src/views/Home.vue + site: src/views/Home.vue:1 + reason: markup +results: + - target: plug//danger + status: found + path: + - key: src/views/Home.vue + root: framework + - key: plug//danger + site: src/views/Home.vue:4 + kind: direct + confidence: high + call_sites: + - caller: src/views/Home.vue + site: src/views/Home.vue:4 + reached: true diff --git a/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/package.json b/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/package.json new file mode 100644 index 00000000..030f19ba --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "vue-route-dynamic-import", + "main": "src/main.js", + "dependencies": { "vue": "^3.4.0", "vue-router": "^4.3.0" } +} diff --git a/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/main.js b/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/main.js new file mode 100644 index 00000000..39085b95 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/main.js @@ -0,0 +1,5 @@ +import { createApp, h } from 'vue'; +import { RouterView } from 'vue-router'; +import { router } from './router.js'; + +createApp({ render: () => h(RouterView) }).use(router).mount('#app'); diff --git a/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/router.js b/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/router.js new file mode 100644 index 00000000..e6268f17 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/router.js @@ -0,0 +1,7 @@ +import { createRouter, createWebHistory } from 'vue-router'; + +const routes = [ + { path: '/', component: () => import('./views/Home.vue') }, +]; + +export const router = createRouter({ history: createWebHistory(), routes }); diff --git a/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/views/Home.vue b/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/views/Home.vue new file mode 100644 index 00000000..05dab581 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-route-dynamic-import/project/src/views/Home.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-script-src/expect.yaml b/test/projectgraph/cases/javascript/vue-script-src/expect.yaml index 29f39ff6..ea07141b 100644 --- a/test/projectgraph/cases/javascript/vue-script-src/expect.yaml +++ b/test/projectgraph/cases/javascript/vue-script-src/expect.yaml @@ -1,6 +1,9 @@ source: safedep:hand description: The script element of Ext.vue loads logic.js by its src attribute. The element is an import of logic.js, so the search reaches edanger through it. roots: + - key: src/Ext.vue + kind: framework + rule: node.component - key: src/main.js kind: manifest rule: node.package-json @@ -34,11 +37,7 @@ results: - target: plug//edanger status: found path: - - key: src/main.js - key: src/Ext.vue - site: src/main.js:2 - kind: import - confidence: high - key: src/logic.js site: src/Ext.vue:2 kind: import diff --git a/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml b/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml index e0c011bc..f419a488 100644 --- a/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml +++ b/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml @@ -1,6 +1,9 @@ source: safedep:hand description: The template of App.vue names onClick as a click handler. No code of the script calls onClick, so only the markup record makes danger, which onClick calls, read uncertain and not not-found. roots: + - key: src/App.vue + kind: framework + rule: node.component - key: src/main.js kind: manifest rule: node.package-json diff --git a/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml b/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml index 41d80d3a..4363352f 100644 --- a/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml +++ b/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml @@ -1,6 +1,9 @@ source: safedep:hand description: The template of App.vue calls danger and render, which its script imports. The index does not parse the template, so the file has a markup record, and each target reads uncertain, not no-such-target. setup runs in the script and is found. roots: + - key: src/App.vue + kind: framework + rule: node.component - key: src/main.js kind: manifest rule: node.package-json @@ -29,11 +32,7 @@ results: - target: plug//setup status: found path: - - key: src/main.js - key: src/App.vue - site: src/main.js:2 - kind: import - confidence: high - key: plug//setup site: src/App.vue:8 kind: direct diff --git a/test/projectgraph/cases/javascript/vue-vite-fallback/expect.yaml b/test/projectgraph/cases/javascript/vue-vite-fallback/expect.yaml new file mode 100644 index 00000000..a7f79c79 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-vite-fallback/expect.yaml @@ -0,0 +1,81 @@ +source: safedep:hand +description: >- + Vite loads src/main.js from index.html, which no rule reads, and package.json names no entry. So the + roots fall back to the library surface. App.vue is a root of the rule node.component, and that rule + keeps the fallback on, so init, which only main.js calls, stays found. +roots: + - key: src/App.vue + kind: framework + rule: node.component + - key: src/App.vue + kind: library-surface + rule: library-surface + - key: src/main.js + kind: library-surface + rule: library-surface +exact_edges: true +edges: + - from: src/App.vue + to: plug//danger + kind: direct + confidence: high + site: src/App.vue:4 + - from: src/main.js + to: src/App.vue + kind: import + confidence: high + site: src/main.js:3 + - from: src/main.js + to: plug//init + kind: direct + confidence: high + site: src/main.js:5 + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:6 + - from: src/main.js + to: vue//createApp//mount + kind: direct + confidence: high + site: src/main.js:6 +unresolved: + - caller: src/App.vue + site: src/App.vue:1 + reason: markup + - caller: src/main.js + site: src/main.js:6 + reason: no-dependency-source + method: mount +results: + - target: plug//init + status: found + path: + - key: src/main.js + root: library-surface + - key: plug//init + site: src/main.js:5 + kind: direct + confidence: high + call_sites: + - caller: src/main.js + site: src/main.js:5 + reached: true + uncertainties: + - kind: root-fallback + - target: plug//danger + status: found + path: + - key: src/App.vue + root: framework + - key: plug//danger + site: src/App.vue:4 + kind: direct + confidence: high + call_sites: + - caller: src/App.vue + site: src/App.vue:4 + reached: true + uncertainties: + - kind: root-fallback diff --git a/test/projectgraph/cases/javascript/vue-vite-fallback/project/index.html b/test/projectgraph/cases/javascript/vue-vite-fallback/project/index.html new file mode 100644 index 00000000..74acba4f --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-vite-fallback/project/index.html @@ -0,0 +1,2 @@ +
+ diff --git a/test/projectgraph/cases/javascript/vue-vite-fallback/project/package.json b/test/projectgraph/cases/javascript/vue-vite-fallback/project/package.json new file mode 100644 index 00000000..b5cd7dd7 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-vite-fallback/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "vue-vite-fallback", + "private": true, + "dependencies": { "vue": "^3.4.0" } +} diff --git a/test/projectgraph/cases/javascript/vue-vite-fallback/project/src/App.vue b/test/projectgraph/cases/javascript/vue-vite-fallback/project/src/App.vue new file mode 100644 index 00000000..db48f660 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-vite-fallback/project/src/App.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-vite-fallback/project/src/main.js b/test/projectgraph/cases/javascript/vue-vite-fallback/project/src/main.js new file mode 100644 index 00000000..58df0d7e --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-vite-fallback/project/src/main.js @@ -0,0 +1,6 @@ +import { createApp } from 'vue'; +import { init } from 'plug'; +import App from './App.vue'; + +init(); +createApp(App).mount('#app'); From 91740aab19029d90f5ce2f53d0ab45739188e8e6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:50:43 +0000 Subject: [PATCH 05/86] fix(golang): record a call of a local function value for a source search A call of a parameter, a local or a range variable that holds a function had only a record with its name. A search from a source scoped that record to the definitions of the name, so a function that unreached code passes in read not-found. The link step now also gives such a site a function-value record. It does the same for a call of a typed local of a function type and for a call of a member that a field of the graph names. A search from the roots keeps its answers, and a search with a source answers uncertain. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 25 ++-- projectgraph/link.go | 7 +- projectgraph/resolve.go | 6 + projectgraph/values.go | 50 +++++++ .../cases/go/dependency-callback/expect.yaml | 6 + .../go_github.com_acme_srv_v1.0.0.json | 30 +++- .../go/dependency-data-reads/expect.yaml | 3 + .../go_example.com_markup_v1.0.0.json | 28 ++-- .../go/dependency-failed-callback/expect.yaml | 3 + .../go_github.com_acme_srv_v1.0.0.json | 13 +- .../cases/go/source-local-func/expect.yaml | 128 ++++++++++++++++++ .../cases/go/source-local-func/project/go.mod | 3 + .../go/source-local-func/project/main.go | 48 +++++++ .../stdlib-callback-method-record/expect.yaml | 13 +- 14 files changed, 333 insertions(+), 30 deletions(-) create mode 100644 test/projectgraph/cases/go/source-local-func/expect.yaml create mode 100644 test/projectgraph/cases/go/source-local-func/project/go.mod create mode 100644 test/projectgraph/cases/go/source-local-func/project/main.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 22d8993f..5c96cbcc 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -1016,14 +1016,23 @@ that search the load code of its Go package. The cases `javascript/source-module-load` hold a value in the load code and call it from a source. -A call of a parameter, a local or a field that holds a function, as `f(x)` -or `t.hook(x)`, is no `function-value` record. It is a record with the -name, as `f` or `hook`, which concerns what a definition of the name -reaches. In a search from the roots of a Go program, the `reference` edge -of the stored function covers the call. In a search with a source, and in -Rust, a function of another name that no path reaches can run. So such a -call can read as `not-found`. Only an ambiguous site of the form concerns -every target. +A call of a parameter, a local or a range variable that holds a function, +as `f(x)`, has a record with the name `f`, which concerns what a +definition of the name reaches. A local of a declared function type, as +`g` in `var g F = h`, has no record of its own. The function value has +another name, so in Go the link step also gives each such site a +`function-value` record. It does the same for a call of a member whose +receiver it does not know, as `h.fn()`, when a field of a type of the graph +has the name. In a search from the roots, the `reference` edge of the stored +function covers the call, and the `function-value` record concerns no +target. In a search with a source and in a summary, it concerns every +target. The case `go/source-local-func` shows each form. + +Two gaps stay. A field of a type of a package that no unit gives can hold a +function, and no definition names that field. So a call of it keeps only +the record with the name. In Rust, a call of a parameter or a local keeps +only the record with the name, and in a search with a source it can read as +`not-found`. The rule does not apply to Rust. The Rust index records a function that an argument, a `let`, an assignment, a `return`, a field initializer or an diff --git a/projectgraph/link.go b/projectgraph/link.go index cdc4c1a3..00a89a8c 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -135,8 +135,10 @@ type builder struct { visiting map[nameKey]bool bindings map[string]string classes map[string]classRecord - // defs holds the definition records by key. - defs map[string]*fileindex.Definition + // defs holds the definition records by key, and fieldNames the names + // of the fields among them. + defs map[string]*fileindex.Definition + fieldNames map[string]bool // reading holds the variables whose value a walk reads now, so a cycle // of variables across files stops. stepping holds the names that a // walk of a unit reads now for the result type of an external key. @@ -403,6 +405,7 @@ func (b *builder) linkAll(ctx context.Context) error { b.copyUnresolved(ix) b.linkReflection(ix) } + b.recordValueCalls() if b.opts.Surface { b.addSurfaces() } diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 96a64aee..9b93450f 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -96,6 +96,12 @@ func (b *builder) linkCalls(ix *fileindex.Index) { } continue } + // A local of a declared function type, as g in var g F = h, + // holds a function value. The walk ends at the type F, and no + // edge names the function. + if r.fail == nil && r.declared && call.Receiver == "" && sc.profile.functionValues && b.isType(r.key) { + b.addUnresolved(UnresolvedSite{Caller: from, Site: site, Text: call.Callee, Reason: fileindex.UnresolvedFunctionValue}) + } if r.unbound && sc.profile.thisCalls { r = b.thisCall(ix, caller, call, r) } diff --git a/projectgraph/values.go b/projectgraph/values.go index d2d9ac25..e3a0a6cc 100644 --- a/projectgraph/values.go +++ b/projectgraph/values.go @@ -24,6 +24,56 @@ func (b *builder) linkUnknownValue(sc *scope, ref fileindex.Reference, from stri b.addUnresolved(UnresolvedSite{Caller: from, Site: site, Text: ref.Text, Method: lastWritten(ref.Text), Reason: reason}) } +// recordValueCalls adds a function-value record at each call that can run +// a function value and that has only a record with its name: a call of a +// parameter or a local, as f(x), or a call of a member whose name a field +// of the graph has, as h.fn(). The name scopes the record to the +// definitions of the name, and a function value has another name. A search +// from the roots reaches the reference to the function, but a search from a +// source can miss the code that stores the value. A field of a type that +// no unit gives can also hold a function, and no definition shows it. +func (b *builder) recordValueCalls() { + recorded := map[Site]bool{} + for u := range b.unresolved { + if u.Reason == fileindex.UnresolvedFunctionValue { + recorded[u.Site] = true + } + } + var add []UnresolvedSite + for u := range b.unresolved { + if u.Reason != fileindex.UnresolvedUnknownReceiver || u.Site.Path == "" || recorded[u.Site] || !b.callsValue(u) { + continue + } + ix := b.byPath[u.Site.Path] + if ix == nil || !profileOf(ix.Language).functionValues { + continue + } + recorded[u.Site] = true + add = append(add, UnresolvedSite{Caller: u.Caller, Site: u.Site, Text: u.Text, Receiver: u.Receiver, Reason: fileindex.UnresolvedFunctionValue}) + } + for _, u := range add { + b.addUnresolved(u) + } +} + +// callsValue reports whether a call with an unknown receiver can call a +// function value: a name with no receiver, or a member that a field of the +// graph names. +func (b *builder) callsValue(u UnresolvedSite) bool { + if u.Receiver == "" { + return isIdentifier(u.Text) + } + if b.fieldNames == nil { + b.fieldNames = map[string]bool{} + for _, def := range b.defs { + if def.Kind == fileindex.DefField { + b.fieldNames[def.Name] = true + } + } + } + return b.fieldNames[u.Method] +} + // reflection is a package of a language that gives or runs a function // that no reference names, and the methods of it that do it. type reflection struct { diff --git a/test/projectgraph/cases/go/dependency-callback/expect.yaml b/test/projectgraph/cases/go/dependency-callback/expect.yaml index 89f2d0d4..05d0989d 100644 --- a/test/projectgraph/cases/go/dependency-callback/expect.yaml +++ b/test/projectgraph/cases/go/dependency-callback/expect.yaml @@ -50,6 +50,9 @@ unresolved: - caller: os//exec//LookPath reason: no-dependency-source callback: true + - caller: go:github.com/acme/srv@v1.0.0//srv.go//Run + site: go:github.com/acme/srv@v1.0.0//srv.go:12 + reason: function-value - caller: go:github.com/acme/srv@v1.0.0//srv.go//Run site: go:github.com/acme/srv@v1.0.0//srv.go:12 reason: unknown-receiver @@ -61,6 +64,9 @@ unresolved: site: go:github.com/acme/srv@v1.0.0//srv.go:20 reason: interface-receiver method: Handle + - caller: go:github.com/acme/srv@v1.0.0//srv.go//Bind + site: go:github.com/acme/srv@v1.0.0//srv.go:24 + reason: function-value - caller: go:github.com/acme/srv@v1.0.0//srv.go//Bind site: go:github.com/acme/srv@v1.0.0//srv.go:24 reason: unknown-receiver diff --git a/test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json b/test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json index fe5ebda6..7bc7e100 100644 --- a/test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json @@ -12,7 +12,8 @@ "reaches": [], "uncertainties": [ 0, - 1 + 1, + 2 ] }, { @@ -29,7 +30,7 @@ ], "reaches": [], "uncertainties": [ - 2 + 3 ] }, { @@ -47,7 +48,8 @@ "reaches": [], "uncertainties": [ 0, - 3 + 4, + 5 ] }, { @@ -58,7 +60,7 @@ "reaches": [], "uncertainties": [ 0, - 4 + 6 ] } ], @@ -72,6 +74,16 @@ "text": "the Go standard library, which no unit gives (no-dependency-source)", "callback": true }, + { + "kind": "unresolved-call", + "key": "go:github.com/acme/srv@v1.0.0//srv.go//Bind", + "site": { + "path": "go:github.com/acme/srv@v1.0.0//srv.go", + "line": 24, + "column": 2 + }, + "text": "f (function-value)" + }, { "kind": "unresolved-call", "key": "go:github.com/acme/srv@v1.0.0//srv.go//Bind", @@ -93,6 +105,16 @@ }, "text": "fs[0] (function-value)" }, + { + "kind": "unresolved-call", + "key": "go:github.com/acme/srv@v1.0.0//srv.go//Run", + "site": { + "path": "go:github.com/acme/srv@v1.0.0//srv.go", + "line": 12, + "column": 2 + }, + "text": "f (function-value)" + }, { "kind": "unresolved-call", "key": "go:github.com/acme/srv@v1.0.0//srv.go//Run", diff --git a/test/projectgraph/cases/go/dependency-data-reads/expect.yaml b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml index 8c39d603..e52d1756 100644 --- a/test/projectgraph/cases/go/dependency-data-reads/expect.yaml +++ b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml @@ -267,6 +267,9 @@ unresolved: - caller: example.com//markup//Handler site: go:example.com/markup@v1.0.0 reason: unknown-receiver + - caller: go:example.com/markup@v1.0.0//node.go//Render + site: go:example.com/markup@v1.0.0//node.go:39 + reason: function-value - caller: go:example.com/markup@v1.0.0//node.go//Render site: go:example.com/markup@v1.0.0//node.go:39 reason: unknown-receiver diff --git a/test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json b/test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json index 3367ae13..e6392bf4 100644 --- a/test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json @@ -140,7 +140,8 @@ ], "uncertainties": [ 0, - 2 + 2, + 3 ] }, { @@ -203,14 +204,17 @@ }, "text": "example.com//markup//Handler (unknown-receiver)", "unit": "go:example.com/markup@v1.0.0", - "targets": [ - "errors//New", - "os//Remove", - "os//exec//Command" - ], - "methods": [ - "Handler" - ] + "every": true + }, + { + "kind": "unresolved-call", + "key": "go:example.com/markup@v1.0.0//node.go//Render", + "site": { + "path": "go:example.com/markup@v1.0.0//node.go", + "line": 39, + "column": 2 + }, + "text": "Handler (function-value)" }, { "kind": "unresolved-call", @@ -222,11 +226,7 @@ }, "text": "Handler (unknown-receiver)", "method": "Handler", - "targets": [ - "errors//New", - "os//Remove", - "os//exec//Command" - ] + "every": true } ] } diff --git a/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml b/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml index 03240056..944f945e 100644 --- a/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml +++ b/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml @@ -26,6 +26,9 @@ unresolved: - caller: os//exec//LookPath reason: no-dependency-source callback: true + - caller: go:github.com/acme/srv@v1.0.0//srv.go//Serve + site: go:github.com/acme/srv@v1.0.0//srv.go:8 + reason: function-value - caller: go:github.com/acme/srv@v1.0.0//srv.go//Serve site: go:github.com/acme/srv@v1.0.0//srv.go:8 reason: unknown-receiver diff --git a/test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json b/test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json index d7ea90bb..d9fa8639 100644 --- a/test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json @@ -23,7 +23,8 @@ "uncertainties": [ 0, 1, - 2 + 2, + 3 ] } ], @@ -45,6 +46,16 @@ "text": "the Go standard library, which no unit gives (no-dependency-source)", "callback": true }, + { + "kind": "unresolved-call", + "key": "go:github.com/acme/srv@v1.0.0//srv.go//Serve", + "site": { + "path": "go:github.com/acme/srv@v1.0.0//srv.go", + "line": 8, + "column": 2 + }, + "text": "serve (function-value)" + }, { "kind": "unresolved-call", "key": "go:github.com/acme/srv@v1.0.0//srv.go//Serve", diff --git a/test/projectgraph/cases/go/source-local-func/expect.yaml b/test/projectgraph/cases/go/source-local-func/expect.yaml new file mode 100644 index 00000000..f07c5133 --- /dev/null +++ b/test/projectgraph/cases/go/source-local-func/expect.yaml @@ -0,0 +1,128 @@ +source: safedep:hand +description: >- + main passes remove to run, lookup to each in a slice, getwd to fire in the field fn of a hook, and getenv + to call, which stores it in the typed local g. run calls its parameter f, each its range variable g, fire + the field fn of its range variable h, and call its local g. A search from the roots reaches main, which + holds each function as a value, so os.Remove is found through the reference. A search from run, each, + fire or call alone does not reach main. Each call there runs a function value that the search does not + reach, so the call is a function-value record, which concerns every target in a search with a source. + Each target is then uncertain. A record with the name alone, as f or fn, gave not-found there. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +no_edges: + - from: main.go//run + to: main.go//remove + - from: main.go//run + to: os//Remove +unresolved: + - caller: main.go//each + reason: no-dependency-source + callback: true + - caller: main.go//fire + reason: no-dependency-source + callback: true + - caller: main.go//run + reason: no-dependency-source + callback: true + - caller: os//Getenv + reason: no-dependency-source + callback: true + - caller: os//Getwd + reason: no-dependency-source + callback: true + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: os//exec//LookPath + reason: no-dependency-source + callback: true + - caller: main.go//run + site: main.go:23 + reason: function-value + - caller: main.go//run + site: main.go:23 + reason: unknown-receiver + method: f + - caller: main.go//each + site: main.go:28 + reason: function-value + - caller: main.go//each + site: main.go:28 + reason: unknown-receiver + method: g + - caller: main.go//fire + site: main.go:34 + reason: function-value + - caller: main.go//fire + site: main.go:34 + reason: unknown-receiver + method: fn + - caller: main.go//call + site: main.go:40 + reason: function-value +results: + - target: os//Remove + status: found + path: + - key: main.go//main + - key: main.go//remove + site: main.go:44 + kind: reference + confidence: low + - key: os//Remove + site: main.go:14 + kind: direct + confidence: high + call_sites: + - caller: main.go//remove + site: main.go:14 + reached: true + - target: os//Remove + sources: [main.go//run] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//remove + site: main.go:14 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:23 + - target: os//exec//LookPath + sources: [main.go//each] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//lookup + site: main.go:16 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:28 + - target: os//Getwd + sources: [main.go//fire] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:34 + - target: os//Getenv + sources: [main.go//call] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//getenv + site: main.go:20 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:40 diff --git a/test/projectgraph/cases/go/source-local-func/project/go.mod b/test/projectgraph/cases/go/source-local-func/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/source-local-func/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/source-local-func/project/main.go b/test/projectgraph/cases/go/source-local-func/project/main.go new file mode 100644 index 00000000..d0ceb5e9 --- /dev/null +++ b/test/projectgraph/cases/go/source-local-func/project/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "os" + "os/exec" +) + +type F func() + +type hook struct { + fn func() +} + +func remove() { os.Remove("x") } + +func lookup() { exec.LookPath("ls") } + +func getwd() { os.Getwd() } + +func getenv() { os.Getenv("HOME") } + +func run(f func()) { + f() +} + +func each(fs []func()) { + for _, g := range fs { + g() + } +} + +func fire(hs []hook) { + for _, h := range hs { + h.fn() + } +} + +func call(h F) { + var g F = h + g() +} + +func main() { + run(remove) + each([]func(){lookup}) + fire([]hook{{fn: getwd}}) + call(getenv) +} diff --git a/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml b/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml index 557459b5..feb9a2ec 100644 --- a/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml +++ b/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml @@ -6,7 +6,8 @@ description: >- method value, and a dot import. viaField calls a function value that a field holds. Each call is a record with a method name, and such a call can run a method of the standard library, which calls back ServeHTTP. So the caller of each record also has a callback record, and os.Remove is uncertain from each via - function. quiet.Idle calls nothing, and its package holds no load code, so os.Remove is not found from it. + function. A call of a function value, as the field of viaField or a name that no definition of the file + binds, as ListenAndServe after the dot import, also concerns every target from a source. quiet.Idle calls nothing, and its package holds no load code, so os.Remove is not found from it. A source in package main would enter the module top level of main.go, which holds http.ListenAndServe as a value. roots: @@ -47,6 +48,9 @@ unresolved: - caller: os//Remove reason: no-dependency-source callback: true + - caller: dot.go//viaDotImport + site: dot.go:6 + reason: function-value - caller: dot.go//viaDotImport site: dot.go:6 reason: unknown-receiver @@ -67,6 +71,9 @@ unresolved: site: main.go:43 reason: interface-receiver method: ListenAndServe + - caller: main.go//viaField + site: main.go:47 + reason: function-value - caller: main.go//viaField site: main.go:47 reason: unknown-receiver @@ -138,6 +145,8 @@ results: reached: false uncertainties: - kind: unresolved-call + site: main.go:47 + - kind: unresolved-call - target: os//Remove sources: [main.go//viaMethodValue] sources_only: true @@ -162,6 +171,8 @@ results: - kind: unresolved-call site: dot.go:6 - kind: unresolved-call + site: dot.go:6 + - kind: unresolved-call - target: os//Remove sources: [quiet/quiet.go//Idle] sources_only: true From b21a40de1a2b94d07989a17a024186c5c8a6e519 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:55:43 +0000 Subject: [PATCH 06/86] fix(golang): check the binding of a typed name at each call The index records the typed locals of a Go function with no extent, and the link step reads the type of the first local of a name. An inner block that binds the name again made a call on it resolve to a method of the outer type, and the method of the inner type read not-found. The index now checks that the binding in scope at a call or a method value is the one whose type the link step reads, or has the same type. Otherwise the site is an unknown-receiver record with the method name. A short variable declaration that assigns a name of its own block binds no new name. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 13 ++ plugin/indexer/calls.go | 6 +- plugin/indexer/golang.go | 28 +++- plugin/indexer/govalues.go | 150 ++++++++++++++++-- plugin/indexer/languages.go | 5 + plugin/indexer/project.go | 18 ++- .../cases/go/typed-local-shadow/expect.yaml | 112 +++++++++++++ .../go/typed-local-shadow/project/go.mod | 3 + .../go/typed-local-shadow/project/main.go | 27 ++++ 9 files changed, 335 insertions(+), 27 deletions(-) create mode 100644 test/projectgraph/cases/go/typed-local-shadow/expect.yaml create mode 100644 test/projectgraph/cases/go/typed-local-shadow/project/go.mod create mode 100644 test/projectgraph/cases/go/typed-local-shadow/project/main.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 5c96cbcc..1f78590c 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -331,6 +331,19 @@ edge joins the key to the definition in the unit. A chained call, as | A result with no named type, or a function that the unit does not define | A record with the reason `unknown-receiver` | | Two definitions of the function, as in two platform files of a Go package, with two result types | A record with the reason `unknown-receiver`. The walk follows one type, and a method of the other type would be `not-found` | +The index records the typed parameters and locals of a Go function with no +extent, and the link step reads the type of the first one of a name. An +inner block can bind the name again, as `x` in +`x := local{}; { x := remote{}; x.run() }`, and a function literal can +take a parameter of the name. So the index checks each call and each +method value on a typed name. The binding of the name in scope there must +be the binding whose type the link step reads, or a binding of the same +type. Otherwise the site is a record with the reason `unknown-receiver` +and the method name, and the method of the real type gets a `name` edge. A +short variable declaration that assigns a name of its own block, as `err` +in `n, err := f()` after `m, err := g()`, binds no new name. The case +`go/typed-local-shadow` shows it. + Rust follows the same rules, with these additions. - The link step links the code of a `cargo:` unit, as the crate directory diff --git a/plugin/indexer/calls.go b/plugin/indexer/calls.go index 87eeb05c..280363df 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -32,7 +32,8 @@ func (p *projection) retargetTyped(ctx context.Context, call *fileindex.Call, un } // bindsAsWritten reports whether the call graph binds the names of a call -// as the code does, in a language with declaredAt. The call graph reads +// as the code does, in a language with typedAt or declaredAt. The call +// graph reads // only some binders of a name, so it can bind a receiver to a parameter or // to the value of a let while a pattern binds the name again, as t in // fn run(t: Local) { if let Some(t) = dep::try_thing() { t.inspect() } }. @@ -42,6 +43,9 @@ func (p *projection) retargetTyped(ctx context.Context, call *fileindex.Call, un // name must have its declared type at the call. func (p *projection) bindsAsWritten(ctx context.Context, call fileindex.Call) bool { rules := p.rules() + if rules.typedAt != nil && !rules.typedAt(p, call) { + return false + } if rules.declaredAt == nil { return true } diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 699dc5b2..28d558c3 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -23,6 +23,7 @@ var goRules = languageRules{ signature: goSignature, exported: goExported, definitions: goDefinitions, + typedAt: goTypedCall, visitors: []func(p *projection) visitor{referenceVisitor}, references: &referenceSyntax{ holders: map[string]string{ @@ -366,7 +367,7 @@ func goSignature(p *projection, def *fileindex.Definition, node *sitter.Node) { if len(results) > 1 { def.Results = results } - def.Locals = p.goLocals(node.ChildByFieldName("body")) + def.Locals, p.goOrigins[def.Key] = p.goLocals(node.ChildByFieldName("body")) } // goExported reports that a name starts with an upper case letter, a @@ -460,27 +461,40 @@ func (p *projection) goResults(result *sitter.Node) []string { // source declares, as var p Parser, or constructs, as p := &Parser{}, // p := new(Parser) or p := v.(*Parser). A name that holds the first result // of a call, as p := NewParser(), names the function. A function literal in -// the body shares the scope of the body, so its names count too. -func (p *projection) goLocals(body *sitter.Node) []fileindex.Typed { +// the body shares the scope of the body, so its names count too. It also +// returns the origin of each local: the start of its scope as goScopes +// records it, or goAssignedOrigin for an assignment. +func (p *projection) goLocals(body *sitter.Node) ([]fileindex.Typed, []uint32) { var locals []fileindex.Typed + var origins []uint32 + add := func(typed []fileindex.Typed, origin uint32) { + locals = append(locals, typed...) + for range typed { + origins = append(origins, origin) + } + } walkBody(body, nil, func(node *sitter.Node) { switch node.Type() { case "var_spec": names := childrenByField(node, "name") if typ := p.goTypeName(node.ChildByFieldName("type")); typ != "" { for _, name := range names { - locals = append(locals, fileindex.Typed{Name: p.text(name), Type: typ}) + add([]fileindex.Typed{{Name: p.text(name), Type: typ}}, node.EndByte()) } } else { - locals = append(locals, p.goAssigned(names, node.ChildByFieldName("value"))...) + add(p.goAssigned(names, node.ChildByFieldName("value")), node.EndByte()) } case "short_var_declaration", "assignment_statement": + origin := node.EndByte() + if node.Type() == "assignment_statement" { + origin = goAssignedOrigin + } if left := node.ChildByFieldName("left"); left != nil { - locals = append(locals, p.goAssigned(ts.NamedChildren(left), node.ChildByFieldName("right"))...) + add(p.goAssigned(ts.NamedChildren(left), node.ChildByFieldName("right")), origin) } } }) - return locals + return locals, origins } // goAssigned returns the typed names of an assignment. One call on the diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 1febb324..473e419c 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -27,6 +27,12 @@ func goReference(p *projection, caller string, value *sitter.Node) (string, *sit target, _ := p.goName(caller, value) return target, nil case "selector_expression": + // The link step reads the type of the first binding of a typed + // name, so a method value on another binding of the name keeps + // only its method name. + if root := goSelectorRoot(value); root != nil && !p.goTypedAt(caller, p.text(root), root.StartByte()) { + return "", value + } if target, ok := p.reference(*p.rules().references, caller, value); ok { return target, nil } @@ -43,6 +49,18 @@ func goReference(p *projection, caller string, value *sitter.Node) (string, *sit return "", nil } +// goSelectorRoot returns the name that starts a member chain, as x for +// x.cfg.Run, or nil for a chain that starts with no name. +func goSelectorRoot(node *sitter.Node) *sitter.Node { + for depth := 0; node != nil && node.Type() == "selector_expression" && depth < maxReferenceSegments; depth++ { + node = node.ChildByFieldName("operand") + } + if node == nil || node.Type() != "identifier" { + return nil + } + return node +} + // goValueName returns the expression that names the function of a value: // the value in parentheses, the operand of &, which a dereference gives // back, and the function of a generic instantiation, as F for F[int]. An @@ -194,9 +212,9 @@ func (p *projection) goPredeclared(name string) bool { } // goScope holds the bytes of the source where a local name of a Go -// function is in scope. +// function is in scope, and the start of the block that declares it. type goScope struct { - from, to uint32 + from, to, block uint32 } // goBlocks are the nodes whose extent is the scope of a name that a @@ -215,6 +233,14 @@ var goTypeLiterals = map[string]bool{"function_type": true, "interface_type": tr // byte: a parameter, a result, a variable, a constant or a type that the // function or a function literal in it declares. func (p *projection) goLocal(caller, name string, at uint32) bool { + _, ok := p.goBinding(caller, name, at) + return ok +} + +// goBinding returns the scope of the local name of the caller that is in +// scope at the byte. An inner scope starts after an outer one that holds +// it, so the scope with the last start is the binding. +func (p *projection) goBinding(caller, name string, at uint32) (goScope, bool) { if p.goScoped == nil { p.goScoped = map[string]map[string][]goScope{} } @@ -223,7 +249,99 @@ func (p *projection) goLocal(caller, name string, at uint32) bool { locals = p.goScopes(p.syntax[caller]) p.goScoped[caller] = locals } - return slices.ContainsFunc(locals[name], func(s goScope) bool { return s.from <= at && at < s.to }) + var binding goScope + found := false + for _, s := range locals[name] { + if s.from <= at && at < s.to && (!found || s.from > binding.from) { + binding, found = s, true + } + } + return binding, found +} + +// goTypedAt reports whether the binding of a typed name in scope at the +// byte has the type that the link step reads. The index records the typed +// locals of a function with no extent, and the link step reads the first +// parameter or local of the name. An inner block can bind the name again, +// as x in x := local{}; { x := remote{}; x.run() }. A call on the name +// there runs a method of the inner type, and the type of the first binding +// loses it. +func (p *projection) goTypedAt(caller, name string, at uint32) bool { + def, fn := p.defs[caller], p.syntax[caller] + if def == nil || fn == nil { + return true + } + origins := p.goOrigins[caller] + read, origin, ok := fileindex.Typed{}, goParamOrigin(fn), false + for _, t := range def.Params { + if t.Name == name { + read, ok = t, true + break + } + } + if !ok { + i := slices.IndexFunc(def.Locals, func(t fileindex.Typed) bool { return t.Name == name }) + if i < 0 || i >= len(origins) { + return true + } + read, origin = def.Locals[i], origins[i] + } + binding, bound := p.goBinding(caller, name, at) + switch { + case !bound: + // An assignment types a variable of the package, which no local + // binds. + return origin == goAssignedOrigin + case binding.from == origin: + return true + } + for i, t := range def.Locals { + if t.Name == name && i < len(origins) && origins[i] == binding.from { + return t.Type == read.Type && t.Result == read.Result && t.Index == read.Index + } + } + return false +} + +// goTypedCall reports whether the type that the link step reads for each +// typed name that starts a target of a call holds at the call. +func goTypedCall(p *projection, call fileindex.Call) bool { + for _, target := range call.Targets { + if name, ok := p.typedName(call.Caller, target); ok && !p.goTypedAt(call.Caller, name, p.byteAt(call.Line, call.Column)) { + return false + } + } + return true +} + +// byteAt returns the start of the innermost named node at a line and a +// column of a site, as a call or a record holds them. +func (p *projection) byteAt(line, column int) uint32 { + at := sitter.Point{Row: uint32(max(line-1, 0)), Column: uint32(max(column-1, 0))} + return p.tree.Tree().RootNode().NamedDescendantForPointRange(at, at).StartByte() +} + +// goAssignedOrigin is the origin of a typed local that an assignment types and +// that no declaration binds, as client in client = &http.Client{}. +const goAssignedOrigin = ^uint32(0) + +// goParamOrigin returns the start of the scope of the parameters of a +// function, as goScopes records it. +func goParamOrigin(fn *sitter.Node) uint32 { + return fn.StartByte() +} + +// goBlockStart returns the start of the block that a name of the block +// belongs to. The parameters of a function share the block of its body, +// so x, err := f() in the body binds no new x for a parameter x. +func goBlockStart(block *sitter.Node) uint32 { + switch block.Type() { + case "function_declaration", "method_declaration", "func_literal": + if body := block.ChildByFieldName("body"); body != nil { + return body.StartByte() + } + } + return block.StartByte() } // goScopes returns the scopes of each name that a function declares. The @@ -234,11 +352,19 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { if function == nil || function.Type() != "function_declaration" && function.Type() != "method_declaration" { return locals } - add := func(nodes []*sitter.Node, from uint32, block *sitter.Node) { + add := func(nodes []*sitter.Node, from uint32, block *sitter.Node, redeclares bool) { + start := goBlockStart(block) for _, n := range nodes { - if n.Type() == "identifier" { - locals[p.text(n)] = append(locals[p.text(n)], goScope{from: from, to: block.EndByte()}) + if n.Type() != "identifier" { + continue + } + name := p.text(n) + // A short variable declaration assigns a name that its block + // declares already, and binds no new variable. + if redeclares && slices.ContainsFunc(locals[name], func(s goScope) bool { return s.block == start && s.to == block.EndByte() }) { + continue } + locals[name] = append(locals[name], goScope{from: from, to: block.EndByte(), block: start}) } } var walk func(node, block *sitter.Node, depth int) @@ -256,20 +382,20 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { } switch node.Type() { case "parameter_declaration", "variadic_parameter_declaration": - add(childrenByField(node, "name"), block.StartByte(), block) + add(childrenByField(node, "name"), goParamOrigin(block), block, false) case "short_var_declaration": - add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block) + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, true) case "var_spec", "const_spec": - add(childrenByField(node, "name"), node.EndByte(), block) + add(childrenByField(node, "name"), node.EndByte(), block, false) case "type_spec", "type_alias": - add(childrenByField(node, "name"), node.StartByte(), block) + add(childrenByField(node, "name"), node.StartByte(), block, false) case "range_clause", "receive_statement": if goDeclares(node) { - add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block) + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, false) } case "type_switch_statement": if alias := node.ChildByFieldName("alias"); alias != nil { - add(ts.NamedChildren(alias), alias.EndByte(), block) + add(ts.NamedChildren(alias), alias.EndByte(), block, false) } } for _, child := range ts.NamedChildren(node) { diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 3325e8e4..2398babe 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -45,6 +45,11 @@ type languageRules struct { // if let Some(t) = x. The call graph then binds a call on the name to // the value of another binding. rebound func(ctx context.Context, p *projection, call fileindex.Call, name string) bool + // typedAt reports whether the declared type that the link step reads + // for the receiver of a call holds at the call. The Go index records + // the typed locals of a function with no extent, so an inner block can + // bind the name of the receiver again. + typedAt func(p *projection, call fileindex.Call) bool // packageOf returns the package or the namespace of the file, which the // files of one unit share. diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index ddb18328..38a7074a 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -59,6 +59,9 @@ type projection struct { // whose values the index reads, by the key of the function and the // name. goScoped map[string]map[string][]goScope + // goOrigins holds the origin of each typed local of each Go function, + // by the key of the function, as goLocals gives it. + goOrigins map[string][]uint32 } // project builds the call graph of tree and projects it into an index. It @@ -89,13 +92,14 @@ func project(ctx context.Context, tree core.ParseTree) (*fileindex.Index, *callg ContentHash: fileindex.HashContent(*data), Unit: unitOf(cg.FileName), }, - defs: map[string]*fileindex.Definition{}, - syntax: map[string]*sitter.Node{}, - spans: map[span]string{}, - bodies: map[span]string{}, - exports: map[string]bool{}, - bindings: map[string]bool{}, - members: map[[2]uint32]bool{}, + defs: map[string]*fileindex.Definition{}, + syntax: map[string]*sitter.Node{}, + spans: map[span]string{}, + bodies: map[span]string{}, + exports: map[string]bool{}, + bindings: map[string]bool{}, + members: map[[2]uint32]bool{}, + goOrigins: map[string][]uint32{}, } rules := p.rules() diff --git a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml new file mode 100644 index 00000000..42d1c2de --- /dev/null +++ b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml @@ -0,0 +1,112 @@ +source: safedep:hand +description: >- + main declares x as a local, and an inner block declares x again as a remote. The call x.run() in the + block runs remote.run, which calls os/exec.LookPath. The index records the typed locals of a function + with no extent, and the link step reads the type of the first one. So the index checks that the binding + of x in scope at the call is the binding whose type the link step reads. Here it is not, so the call is + an unknown-receiver record with the method name run, which gets a name edge to remote.run. The method + value x.stop is a record of the same kind, with the name stop. A type of the first x gave not-found for + os/exec.LookPath. The call x.run() before the block keeps its member edge to local.run. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +edges: + - from: main.go//main + to: main.go//local//run + kind: member + confidence: high + site: main.go:20 + - from: main.go//main + to: main.go//remote//run + kind: name + confidence: low + site: main.go:23 + - from: main.go//main + to: main.go//remote//stop + kind: name + confidence: low + site: main.go:24 +no_edges: + - from: main.go//local//run + to: os//exec//LookPath +unresolved: + - caller: main.go//main + reason: no-dependency-source + callback: true + - caller: os//Getwd + reason: no-dependency-source + callback: true + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: os//exec//LookPath + reason: no-dependency-source + callback: true + - caller: main.go//main + site: main.go:23 + reason: unknown-receiver + method: run + - caller: main.go//main + site: main.go:24 + reason: unknown-receiver + method: stop + - caller: main.go//main + site: main.go:25 + reason: function-value + - caller: main.go//main + site: main.go:25 + reason: unknown-receiver + method: f +results: + - target: os//Getwd + status: found + path: + - key: main.go//main + - key: main.go//local//run + site: main.go:20 + kind: member + confidence: high + - key: os//Getwd + site: main.go:10 + kind: direct + confidence: high + call_sites: + - caller: main.go//local//run + site: main.go:10 + reached: true + - target: os//exec//LookPath + status: found + path: + - key: main.go//main + - key: main.go//remote//run + site: main.go:23 + kind: name + confidence: low + - key: os//exec//LookPath + site: main.go:14 + kind: direct + confidence: high + call_sites: + - caller: main.go//remote//run + site: main.go:14 + reached: true + - target: os//Remove + status: found + path: + - key: main.go//main + - key: main.go//remote//stop + site: main.go:24 + kind: name + confidence: low + - key: os//Remove + site: main.go:16 + kind: direct + confidence: high + call_sites: + - caller: main.go//remote//stop + site: main.go:16 + reached: true diff --git a/test/projectgraph/cases/go/typed-local-shadow/project/go.mod b/test/projectgraph/cases/go/typed-local-shadow/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/typed-local-shadow/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/typed-local-shadow/project/main.go b/test/projectgraph/cases/go/typed-local-shadow/project/main.go new file mode 100644 index 00000000..49e65bc8 --- /dev/null +++ b/test/projectgraph/cases/go/typed-local-shadow/project/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "os" + "os/exec" +) + +type local struct{} + +func (local) run() { os.Getwd() } + +type remote struct{} + +func (remote) run() { exec.LookPath("ls") } + +func (remote) stop() { os.Remove("x") } + +func main() { + x := local{} + x.run() + { + x := remote{} + x.run() + f := x.stop + f() + } +} From 880d9f09dbf34f4eb5f6924966facccaf915958d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:56:59 +0000 Subject: [PATCH 07/86] fix(golang): record a range over a function A range over a function value calls it, as Go 1.23 allows. The index recorded no reference to the range expression, so the function and its callees were lost. A range clause now holds a value, as an argument does, and the function gets a reference edge. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 20 ++-- plugin/indexer/golang.go | 3 + .../cases/go/range-over-func/expect.yaml | 101 ++++++++++++++++++ .../cases/go/range-over-func/project/go.mod | 3 + .../cases/go/range-over-func/project/main.go | 34 ++++++ 5 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 test/projectgraph/cases/go/range-over-func/expect.yaml create mode 100644 test/projectgraph/cases/go/range-over-func/project/go.mod create mode 100644 test/projectgraph/cases/go/range-over-func/project/main.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 1f78590c..6984a1c2 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -901,8 +901,10 @@ a call can run. A Go function value is one of these: - A function or a method that code uses as a value: an argument, the operand of a conversion, the right side of an assignment or a - declaration, a return value, an element of a composite literal, or the - value of a send on a channel. The index records a reference there, and + declaration, a return value, an element of a composite literal, the + value of a send on a channel, or the expression of a range clause, as + `walk` in `for x := range walk`. A range over a function calls it, and the + function calls the body of the loop. The index records a reference there, and the link step adds a `reference` edge from the code that holds the value. The value exists only after that code runs, so the search reaches that code and then the function. The value is a name, as a function of @@ -1041,12 +1043,18 @@ function covers the call, and the `function-value` record concerns no target. In a search with a source and in a summary, it concerns every target. The case `go/source-local-func` shows each form. -Two gaps stay. A field of a type of a package that no unit gives can hold a -function, and no definition names that field. So a call of it keeps only -the record with the name. In Rust, a call of a parameter or a local keeps -only the record with the name, and in a search with a source it can read as +Three gaps stay. In each of them, a search with a source can read `not-found`. +- A field of a type of a package that no unit gives can hold a function, + and no definition names that field. So a call of it keeps only the + record with the name. +- A range over a parameter or a local that holds a function, as + `for x := range seq`, has no record. The index does not know whether + `seq` holds a function or data. +- In Rust, a call of a parameter or a local keeps only the record with the + name. + The rule does not apply to Rust. The Rust index records a function that an argument, a `let`, an assignment, a `return`, a field initializer or an array names. It does not record a function that the tail expression of a diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 28d558c3..0f21a3c5 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -34,6 +34,9 @@ var goRules = languageRules{ "return_statement": "", "literal_value": "", "send_statement": "value", + // A range over a function calls it, as walk in + // for x := range walk. + "range_clause": "right", // A conversion, as (func())(f), or a call of a generic // function with type arguments, as id[func()](f), which the // grammar parses as a conversion. diff --git a/test/projectgraph/cases/go/range-over-func/expect.yaml b/test/projectgraph/cases/go/range-over-func/expect.yaml new file mode 100644 index 00000000..6785767f --- /dev/null +++ b/test/projectgraph/cases/go/range-over-func/expect.yaml @@ -0,0 +1,101 @@ +source: safedep:hand +description: >- + main ranges over the function walk and over the method value t.All, as Go 1.23 allows. The range + calls the function, and the function calls the body of the loop through yield. So the range + expression is a reference, as an argument is, and os.Remove and os/exec.LookPath are found through it. A + range clause with no reference lost both. The range over a slice names no function. No code uses unused, + so os.Getwd is not found. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +edges: + - from: main.go//main + to: main.go//walk + kind: reference + confidence: low + site: main.go:25 + - from: main.go//main + to: main.go//tree//All + kind: reference + confidence: low + site: main.go:28 +no_edges: + - from: main.go//main + to: main.go//unused + - from: main.go//main + to: os//Remove + - from: main.go//main + to: os//exec//LookPath +unresolved: + - caller: main.go//tree//All + reason: no-dependency-source + callback: true + - caller: main.go//walk + reason: no-dependency-source + callback: true + - caller: os//Getwd + reason: no-dependency-source + callback: true + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: os//exec//LookPath + reason: no-dependency-source + callback: true + - caller: main.go//walk + site: main.go:10 + reason: function-value + - caller: main.go//walk + site: main.go:10 + reason: unknown-receiver + method: yield + - caller: main.go//tree//All + site: main.go:17 + reason: function-value + - caller: main.go//tree//All + site: main.go:17 + reason: unknown-receiver + method: yield +results: + - target: os//Remove + status: found + path: + - key: main.go//main + - key: main.go//walk + site: main.go:25 + kind: reference + confidence: low + - key: os//Remove + site: main.go:9 + kind: direct + confidence: high + call_sites: + - caller: main.go//walk + site: main.go:9 + reached: true + - target: os//exec//LookPath + status: found + path: + - key: main.go//main + - key: main.go//tree//All + site: main.go:28 + kind: reference + confidence: low + - key: os//exec//LookPath + site: main.go:16 + kind: direct + confidence: high + call_sites: + - caller: main.go//tree//All + site: main.go:16 + reached: true + - target: os//Getwd + status: not-found + call_sites: + - caller: main.go//unused + site: main.go:21 + reached: false diff --git a/test/projectgraph/cases/go/range-over-func/project/go.mod b/test/projectgraph/cases/go/range-over-func/project/go.mod new file mode 100644 index 00000000..b40d6d0c --- /dev/null +++ b/test/projectgraph/cases/go/range-over-func/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.23 diff --git a/test/projectgraph/cases/go/range-over-func/project/main.go b/test/projectgraph/cases/go/range-over-func/project/main.go new file mode 100644 index 00000000..e4fb7dea --- /dev/null +++ b/test/projectgraph/cases/go/range-over-func/project/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "os" + "os/exec" +) + +func walk(yield func(string) bool) { + os.Remove("x") + yield("a") +} + +type tree struct{} + +func (tree) All(yield func(int) bool) { + exec.LookPath("ls") + yield(1) +} + +func unused(yield func(string) bool) { + os.Getwd() +} + +func main() { + for range walk { + } + var t tree + for x := range t.All { + _ = x + } + for _, s := range []string{"a"} { + _ = s + } +} From 7852d793ed65a9c9d1a4ceb563b940216d78ed8c Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:01:49 +0000 Subject: [PATCH 08/86] fix(golang): record a function with no body A Go function with no body, as a stub of assembly or a function that //go:linkname binds, runs code that the graph does not hold. A path ended at the stub, and what that code calls read not-found. The index now gives such a function a no-dependency-source record. The link step gives it a scope: the callback for code of the standard library, the unit for the assembly of a unit, and every target for the project or another package. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 18 +++ fileindex/index.go | 3 +- plugin/indexer/golang.go | 59 ++++++++++ plugin/indexer/project.go | 3 + projectgraph/golang.go | 21 ++++ projectgraph/link.go | 3 + .../cases/go/function-no-body/expect.yaml | 105 ++++++++++++++++++ .../go/function-no-body/project/add_amd64.s | 5 + .../cases/go/function-no-body/project/asm.go | 8 ++ .../cases/go/function-no-body/project/go.mod | 5 + .../gomod/github.com/acme/fast@v1.0.0/fast.go | 8 ++ .../gomod/github.com/acme/fast@v1.0.0/go.mod | 3 + .../github.com/acme/fast@v1.0.0/sum_amd64.s | 5 + .../cases/go/function-no-body/project/main.go | 29 +++++ .../go_github.com_acme_fast_v1.0.0.json | 45 ++++++++ 15 files changed, 319 insertions(+), 1 deletion(-) create mode 100644 test/projectgraph/cases/go/function-no-body/expect.yaml create mode 100644 test/projectgraph/cases/go/function-no-body/project/add_amd64.s create mode 100644 test/projectgraph/cases/go/function-no-body/project/asm.go create mode 100644 test/projectgraph/cases/go/function-no-body/project/go.mod create mode 100644 test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/fast.go create mode 100644 test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/sum_amd64.s create mode 100644 test/projectgraph/cases/go/function-no-body/project/main.go create mode 100644 test/projectgraph/cases/go/function-no-body/summaries/go_github.com_acme_fast_v1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 6984a1c2..39d300cc 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -1131,6 +1131,24 @@ depends on what the code can call. the value of a panic that no `recover` stops with its method `Error` or `String`. So `panic` has a record for each of the two methods. +A Go function with no body runs code that the graph does not hold too: a +stub of assembly, as `func Sum(xs []int) int` with a `sum_amd64.s` file, +or a function that `//go:linkname` binds to a function of another package. +The index gives such a function a record with the reason +`no-dependency-source`, and the link step gives it a scope. + +- A linkname into the standard library, or a stub of assembly in a unit of + the standard library, runs code of the standard library. So its record is + a callback, as above. +- The assembly of a package of a unit calls code of its package and of the + runtime. So its record concerns what the code of its unit reaches. +- A linkname into another package and the assembly of the project can call + any function. So their records concern every target. + +The case `go/function-no-body` shows each form. A function of the standard +library that only its assembly calls, as a function of the runtime, can +call a target, and the callback does not concern that target. + A Go call of a method on a value whose type the link step does not know is a record with the method name, as `srv.ListenAndServe()` on a package-level variable, on an element of a slice, on a range variable, on a diff --git a/fileindex/index.go b/fileindex/index.go index fde9f3b8..96ca5df4 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -113,7 +113,8 @@ const ( // UnresolvedNoDependencySource is a call on a value whose type comes // from a package with no code in the graph, as the result of a // function of a module that no unit gives, or an external key of such a - // package, whose code can call back into the graph. + // package, whose code can call back into the graph. A Go function with + // no body, as a stub of assembly, has the reason too. UnresolvedNoDependencySource UnresolvedReason = "no-dependency-source" // UnresolvedFunctionValue is a call of a function value, as f(x)(y) or // handlers[i](c), in a language whose function values the link step diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 0f21a3c5..928f6af8 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -2,9 +2,11 @@ package indexer import ( "slices" + "strings" "unicode" "unicode/utf8" + "github.com/safedep/code/core/ast" "github.com/safedep/code/fileindex" "github.com/safedep/code/langcode" "github.com/safedep/code/pkg/ts" @@ -371,6 +373,63 @@ func goSignature(p *projection, def *fileindex.Definition, node *sitter.Node) { def.Results = results } def.Locals, p.goOrigins[def.Key] = p.goLocals(node.ChildByFieldName("body")) + if node.ChildByFieldName("body") == nil { + p.goStub(def, node) + } +} + +// goStub records a function with no body, as a stub of assembly or a +// function that //go:linkname binds to a function of another package. Its +// code is not Go source of the file, and that code can call code of the +// graph. With no record, a path ends at the stub, and what the code calls +// reads not-found. The receiver of the record is the import path of a +// linkname, so the link step can give the record the scope of that +// package. +func (p *projection) goStub(def *fileindex.Definition, node *sitter.Node) { + site := ast.GetNodePosition(node) + text, pkg := def.Name, "" + if remote, ok := p.goLinknames()[def.Name]; ok && def.Kind == fileindex.DefFunction { + text, pkg = remote, goLinknamePackage(remote) + } + p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ + Caller: def.Key, + Line: int(site.StartLine), + Column: int(site.StartColumn), + Text: text, + Receiver: pkg, + Reason: fileindex.UnresolvedNoDependencySource, + }) +} + +// goLinknames returns the remote name of each local name that a +// //go:linkname directive of the file binds, as runtime.nanotime for +// nanotime. +func (p *projection) goLinknames() map[string]string { + if p.linknames != nil { + return p.linknames + } + p.linknames = map[string]string{} + for _, node := range ts.NamedChildren(p.tree.Tree().RootNode()) { + if node.Type() != "comment" { + continue + } + fields := strings.Fields(p.text(node)) + if len(fields) == 3 && fields[0] == "//go:linkname" { + p.linknames[fields[1]] = fields[2] + } + } + return p.linknames +} + +// goLinknamePackage returns the import path of a linkname target, as +// crypto/internal/boring for crypto/internal/boring.Encrypt or +// example.com/x for example.com/x.(*T).m. +func goLinknamePackage(remote string) string { + slash := strings.LastIndex(remote, "/") + 1 + if dot := strings.Index(remote[slash:], "."); dot >= 0 { + return remote[:slash+dot] + } + return remote } // goExported reports that a name starts with an upper case letter, a diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index 38a7074a..59abf178 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -62,6 +62,9 @@ type projection struct { // goOrigins holds the origin of each typed local of each Go function, // by the key of the function, as goLocals gives it. goOrigins map[string][]uint32 + // linknames holds the remote name of each local name that a + // //go:linkname directive of a Go file binds. + linknames map[string]string } // project builds the call graph of tree and projects it into an index. It diff --git a/projectgraph/golang.go b/projectgraph/golang.go index 134d6f4c..fd419cc1 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -447,6 +447,27 @@ func (b *builder) goOutsideRecords(key string) []UnresolvedSite { return []UnresolvedSite{{Caller: key, Text: key, Reason: fileindex.UnresolvedNoDependencySource}} } +// scopeGoStub gives the record of a Go function with no body the scope of +// the code that runs in its place. The receiver of the record names the +// package of a //go:linkname target. +// +// - Code of the standard library calls back only a method of an exported +// name, as goOutsideRecords argues. So a linkname into the standard +// library, or a stub of assembly in it, gets the callback. +// - The assembly of a package of a unit calls code of its package and of +// the runtime. So its record concerns what the code of its unit reaches. +// - A linkname into another package and the assembly of the project can +// call any function, so their records concern every target. +func (b *builder) scopeGoStub(ix *fileindex.Index, site *UnresolvedSite) { + unit, _ := fileindex.ParseUnit(ix.Unit) + switch { + case site.Receiver != "" && modules.IsStdPath(site.Receiver), site.Receiver == "" && unit.Ecosystem == "go" && unit.Name == modules.StdModule: + site.Callback = true + case site.Receiver == "" && ix.Unit != "": + site.Unit = ix.Unit + } +} + // recordGoMethodsOutside adds the records of code outside the graph to each // Go call of a method on a value whose type the link step does not know. // Such a call can run a method of a type of a package that no unit gives, diff --git a/projectgraph/link.go b/projectgraph/link.go index 00a89a8c..a5ff34fb 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -511,6 +511,9 @@ func (b *builder) copyUnresolved(ix *fileindex.Index) { if u.Reason == fileindex.UnresolvedDynamicImport { site.ImportPrefix = b.importPrefix(ix, u.Line) } + if u.Reason == fileindex.UnresolvedNoDependencySource && ix.Language == langcode.Go { + b.scopeGoStub(ix, &site) + } b.addUnresolved(site) } } diff --git a/test/projectgraph/cases/go/function-no-body/expect.yaml b/test/projectgraph/cases/go/function-no-body/expect.yaml new file mode 100644 index 00000000..847d0e55 --- /dev/null +++ b/test/projectgraph/cases/go/function-no-body/expect.yaml @@ -0,0 +1,105 @@ +source: safedep:hand +description: >- + Three functions have no body. add of the project and Sum of the unit fast are stubs of assembly, and + the assembly calls slow and fallback. now is a function that //go:linkname binds to runtime.nanotime. + The code of each one is not Go source of the graph, so the index gives each stub a record with the + reason no-dependency-source. The assembly of the project can call any function, so its record concerns + every target, and os.Remove and os.Getwd are uncertain from viaAsm. The assembly of a unit calls the + code of its package and of the runtime, so its record concerns what the code of the unit reaches. + os/exec.LookPath is uncertain from viaUnit, and os.Getwd is not found from it. The code of the standard + library calls back only a method of an exported name, so the record of now is a callback. os.Chdir, + which closer.Close calls, is uncertain from viaLinkname, and os.Getwd is not found from it. With no + record, each search ended at the stub, and each target read not-found. +units: + gomod/github.com/acme/fast@v1.0.0: go:github.com/acme/fast@v1.0.0 +roots: + - key: asm.go + kind: main + rule: go.main + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: os//Chdir + reason: no-dependency-source + callback: true + - caller: os//Getwd + reason: no-dependency-source + callback: true + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: os//exec//LookPath + reason: no-dependency-source + callback: true + - caller: asm.go//add + site: asm.go:6 + reason: no-dependency-source + - caller: go:github.com/acme/fast@v1.0.0//fast.go//Sum + site: go:github.com/acme/fast@v1.0.0//fast.go:6 + reason: no-dependency-source + - caller: main.go//now + site: main.go:11 + reason: no-dependency-source + callback: true +results: + - target: os//Remove + status: uncertain + call_sites: + - caller: asm.go//slow + site: asm.go:8 + reached: false + uncertainties: + - kind: unresolved-call + site: asm.go:6 + - target: os//Getwd + sources: [main.go//viaAsm] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//unused + site: main.go:17 + reached: false + uncertainties: + - kind: unresolved-call + site: asm.go:6 + - target: os//exec//LookPath + sources: [main.go//viaUnit] + sources_only: true + status: uncertain + call_sites: + - caller: go:github.com/acme/fast@v1.0.0//fast.go//fallback + site: go:github.com/acme/fast@v1.0.0//fast.go:8 + reached: false + uncertainties: + - kind: unresolved-call + site: go:github.com/acme/fast@v1.0.0//fast.go:6 + - target: os//Getwd + sources: [main.go//viaUnit] + sources_only: true + status: not-found + call_sites: + - caller: main.go//unused + site: main.go:17 + reached: false + - target: os//Chdir + sources: [main.go//viaLinkname] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//closer//Close + site: main.go:15 + reached: false + uncertainties: + - kind: unresolved-call + - target: os//Getwd + sources: [main.go//viaLinkname] + sources_only: true + status: not-found + call_sites: + - caller: main.go//unused + site: main.go:17 + reached: false diff --git a/test/projectgraph/cases/go/function-no-body/project/add_amd64.s b/test/projectgraph/cases/go/function-no-body/project/add_amd64.s new file mode 100644 index 00000000..5894911a --- /dev/null +++ b/test/projectgraph/cases/go/function-no-body/project/add_amd64.s @@ -0,0 +1,5 @@ +#include "textflag.h" + +TEXT ·add(SB), NOSPLIT, $0-24 + CALL ·slow(SB) + RET diff --git a/test/projectgraph/cases/go/function-no-body/project/asm.go b/test/projectgraph/cases/go/function-no-body/project/asm.go new file mode 100644 index 00000000..ffbe3d72 --- /dev/null +++ b/test/projectgraph/cases/go/function-no-body/project/asm.go @@ -0,0 +1,8 @@ +package main + +import "os" + +// add is in add_amd64.s, which calls slow. +func add(a, b int) int + +func slow() { os.Remove("x") } diff --git a/test/projectgraph/cases/go/function-no-body/project/go.mod b/test/projectgraph/cases/go/function-no-body/project/go.mod new file mode 100644 index 00000000..de7d65f2 --- /dev/null +++ b/test/projectgraph/cases/go/function-no-body/project/go.mod @@ -0,0 +1,5 @@ +module example.com/app + +go 1.22 + +require github.com/acme/fast v1.0.0 diff --git a/test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/fast.go b/test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/fast.go new file mode 100644 index 00000000..782939e6 --- /dev/null +++ b/test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/fast.go @@ -0,0 +1,8 @@ +package fast + +import "os/exec" + +// Sum is in sum_amd64.s, which calls fallback. +func Sum(xs []int) int + +func fallback() { exec.LookPath("ls") } diff --git a/test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/go.mod b/test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/go.mod new file mode 100644 index 00000000..38e8de12 --- /dev/null +++ b/test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module github.com/acme/fast + +go 1.22 diff --git a/test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/sum_amd64.s b/test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/sum_amd64.s new file mode 100644 index 00000000..c91ae693 --- /dev/null +++ b/test/projectgraph/cases/go/function-no-body/project/gomod/github.com/acme/fast@v1.0.0/sum_amd64.s @@ -0,0 +1,5 @@ +#include "textflag.h" + +TEXT ·Sum(SB), NOSPLIT, $0-32 + CALL ·fallback(SB) + RET diff --git a/test/projectgraph/cases/go/function-no-body/project/main.go b/test/projectgraph/cases/go/function-no-body/project/main.go new file mode 100644 index 00000000..28e86437 --- /dev/null +++ b/test/projectgraph/cases/go/function-no-body/project/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "os" + _ "unsafe" + + "github.com/acme/fast" +) + +//go:linkname now runtime.nanotime +func now() int64 + +type closer struct{} + +func (closer) Close() error { return os.Chdir("/") } + +func unused() { os.Getwd() } + +func viaAsm() { add(1, 2) } + +func viaUnit() { fast.Sum(nil) } + +func viaLinkname() { now() } + +func main() { + viaAsm() + viaUnit() + viaLinkname() +} diff --git a/test/projectgraph/cases/go/function-no-body/summaries/go_github.com_acme_fast_v1.0.0.json b/test/projectgraph/cases/go/function-no-body/summaries/go_github.com_acme_fast_v1.0.0.json new file mode 100644 index 00000000..e9dbeab9 --- /dev/null +++ b/test/projectgraph/cases/go/function-no-body/summaries/go_github.com_acme_fast_v1.0.0.json @@ -0,0 +1,45 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "19", + "unit": "go:github.com/acme/fast@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//fast//Sum", + "definitions": [ + "go:github.com/acme/fast@v1.0.0//fast.go//Sum" + ], + "reaches": [], + "uncertainties": [ + 1 + ] + } + ], + "targets": [ + "os//exec//LookPath" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true + }, + { + "kind": "unresolved-call", + "key": "go:github.com/acme/fast@v1.0.0//fast.go//Sum", + "site": { + "path": "go:github.com/acme/fast@v1.0.0//fast.go", + "line": 6, + "column": 1 + }, + "text": "Sum (no-dependency-source)", + "unit": "go:github.com/acme/fast@v1.0.0", + "targets": [ + "os//exec//LookPath" + ] + } + ] +} From 2f9372f111d9e3547bb4a6062bb9612cc34df6a5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:02:43 +0000 Subject: [PATCH 09/86] test(golang): call a map element at a variable key from a source A call through a map or a slice index at a variable key, as m[name](), gives a function-value record, as a constant key does. The case go/source-local-func now holds the form with a parameter map, whose record concerns every target in a search with a source. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- .../cases/go/source-local-func/expect.yaml | 38 ++++++++++++++++--- .../go/source-local-func/project/main.go | 7 ++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/test/projectgraph/cases/go/source-local-func/expect.yaml b/test/projectgraph/cases/go/source-local-func/expect.yaml index f07c5133..1d57a194 100644 --- a/test/projectgraph/cases/go/source-local-func/expect.yaml +++ b/test/projectgraph/cases/go/source-local-func/expect.yaml @@ -1,10 +1,11 @@ source: safedep:hand description: >- - main passes remove to run, lookup to each in a slice, getwd to fire in the field fn of a hook, and getenv - to call, which stores it in the typed local g. run calls its parameter f, each its range variable g, fire - the field fn of its range variable h, and call its local g. A search from the roots reaches main, which - holds each function as a value, so os.Remove is found through the reference. A search from run, each, - fire or call alone does not reach main. Each call there runs a function value that the search does not + main passes remove to run, lookup to each in a slice, getwd to fire in the field fn of a hook, and + getenv to call, which stores it in the typed local g, and chdir to pick in a map. run calls its + parameter f, each its range variable g, fire the field fn of its range variable h, call its local g, and + pick the element of its parameter m at a variable key. A search from the roots reaches main, which holds + each function as a value, so os.Remove is found through the reference. A search from run, each, fire, + call or pick alone does not reach main. Each call there runs a function value that the search does not reach, so the call is a function-value record, which concerns every target in a search with a source. Each target is then uncertain. A record with the name alone, as f or fn, gave not-found there. roots: @@ -26,9 +27,18 @@ unresolved: - caller: main.go//fire reason: no-dependency-source callback: true + - caller: main.go//pick + reason: no-dependency-source + callback: true - caller: main.go//run reason: no-dependency-source callback: true + - caller: os//Args + reason: no-dependency-source + callback: true + - caller: os//Chdir + reason: no-dependency-source + callback: true - caller: os//Getenv reason: no-dependency-source callback: true @@ -65,6 +75,13 @@ unresolved: - caller: main.go//call site: main.go:40 reason: function-value + - caller: main.go//pick + site: main.go:54 + reason: function-value + - caller: main.go//pick + site: main.go:54 + reason: unknown-receiver + method: m results: - target: os//Remove status: found @@ -126,3 +143,14 @@ results: uncertainties: - kind: unresolved-call site: main.go:40 + - target: os//Chdir + sources: [main.go//pick] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//chdir + site: main.go:51 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:54 diff --git a/test/projectgraph/cases/go/source-local-func/project/main.go b/test/projectgraph/cases/go/source-local-func/project/main.go index d0ceb5e9..a61e47ee 100644 --- a/test/projectgraph/cases/go/source-local-func/project/main.go +++ b/test/projectgraph/cases/go/source-local-func/project/main.go @@ -45,4 +45,11 @@ func main() { each([]func(){lookup}) fire([]hook{{fn: getwd}}) call(getenv) + pick(map[string]func(){"cd": chdir}, os.Args[1]) +} + +func chdir() { os.Chdir("/") } + +func pick(m map[string]func(), name string) { + m[name]() } From 21f8c73c8528ccdc83193881c4c77e5cb998f042 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:50:05 +0000 Subject: [PATCH 10/86] fix(projectgraph): resolve a Rust path through a use of a module A path of a chained use, as util::helpers after use crate::deep and use deep::util, or of a glob use that brings such a binding, walked to the module of the binding and stopped there. A walk that ends on a unit fails, so the path named no module and a call under it got an external key with no record. resolveQualified now steps from unit to unit through such bindings. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 6 ++- projectgraph/namespaces.go | 17 +++++- .../cases/rust/glob-use-binding/expect.yaml | 53 +++++++++++++++++++ .../rust/glob-use-binding/project/Cargo.toml | 4 ++ .../glob-use-binding/project/src/jobs/mod.rs | 3 ++ .../project/src/jobs/worker.rs | 5 ++ .../rust/glob-use-binding/project/src/main.rs | 6 +++ .../project/src/store/disk.rs | 3 ++ .../glob-use-binding/project/src/store/mod.rs | 1 + .../cases/rust/use-chain-module/expect.yaml | 53 +++++++++++++++++++ .../rust/use-chain-module/project/Cargo.toml | 4 ++ .../use-chain-module/project/src/deep/mod.rs | 1 + .../project/src/deep/util/helpers.rs | 3 ++ .../project/src/deep/util/mod.rs | 1 + .../rust/use-chain-module/project/src/main.rs | 6 +++ .../use-chain-module/project/src/worker.rs | 7 +++ 16 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 test/projectgraph/cases/rust/glob-use-binding/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-binding/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-binding/project/src/jobs/mod.rs create mode 100644 test/projectgraph/cases/rust/glob-use-binding/project/src/jobs/worker.rs create mode 100644 test/projectgraph/cases/rust/glob-use-binding/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use-binding/project/src/store/disk.rs create mode 100644 test/projectgraph/cases/rust/glob-use-binding/project/src/store/mod.rs create mode 100644 test/projectgraph/cases/rust/use-chain-module/expect.yaml create mode 100644 test/projectgraph/cases/rust/use-chain-module/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-chain-module/project/src/deep/mod.rs create mode 100644 test/projectgraph/cases/rust/use-chain-module/project/src/deep/util/helpers.rs create mode 100644 test/projectgraph/cases/rust/use-chain-module/project/src/deep/util/mod.rs create mode 100644 test/projectgraph/cases/rust/use-chain-module/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-chain-module/project/src/worker.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 39d300cc..d4c81b1d 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -431,7 +431,11 @@ Rust follows the same rules, with these additions. `helpers::run` after `use crate::deep::helpers;` names the function of the module `deep::helpers`, and `sub::f` after `use dep::sub;` names `dep//sub//f`. A `use` never reads its own binding, so - `use smallvec::smallvec;` names the crate `smallvec`. + `use smallvec::smallvec;` names the crate `smallvec`. A binding can + name a module, so `use util::helpers;` after `use crate::deep;` and + `use deep::util;` names the module `deep::util::helpers`, as in the case + `rust/use-chain-module`. A glob `use` brings such a binding of its + module too, as in the case `rust/glob-use-binding`. - A glob `use` brings each name of its module into the module of the file. So `helpers::cleanup` after `use super::*;` in `src/deep/worker.rs` names the function of the module `deep::helpers`, diff --git a/projectgraph/namespaces.go b/projectgraph/namespaces.go index abf1d621..10c5ac00 100644 --- a/projectgraph/namespaces.go +++ b/projectgraph/namespaces.go @@ -58,7 +58,22 @@ func (b *builder) resolveQualified(family langcode.Code, name string) (string, b if keys := u.names[parts[i]]; i == len(parts)-1 && len(keys) > 0 { return keys[0], true } - if r := b.walk(u.key, parts[i:]); r.fail == nil { + key, rest := u.key, parts[i:] + // A Rust use can bind a module, as util of use deep::util. A walk + // that ends on a unit fails, so the path through such a binding + // steps from unit to unit here. Without the step, a path of a + // chained use or of a glob use named no module. + for len(rest) > 0 && b.units[key] != nil && len(b.units[key].names[rest[0]]) == 0 { + member, ok := b.unitMember(key, rest[0]) + if !ok { + return "", false + } + key, rest = member, rest[1:] + } + if len(rest) == 0 && b.units[key] != nil { + return key, true + } + if r := b.walk(key, rest); r.fail == nil { return r.key, true } return "", false diff --git a/test/projectgraph/cases/rust/glob-use-binding/expect.yaml b/test/projectgraph/cases/rust/glob-use-binding/expect.yaml new file mode 100644 index 00000000..27720faa --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-binding/expect.yaml @@ -0,0 +1,53 @@ +source: safedep:hand +description: >- + src/jobs/mod.rs has use crate::store::disk, so disk names the module src/store/disk.rs in the module jobs. + src/jobs/worker.rs has use super::*, which brings each name of jobs into worker, the binding disk too. So + disk::wipe in go calls the function wipe of src/store/disk.rs, which calls std::fs::remove_dir_all. No module + disk is a child of worker or of the root. A link step that stops the glob path on the module that a use + binds gives the call the key disk//wipe with no record, and std::fs::remove_dir_all reads not-found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: src/main.rs//main + to: src/jobs/worker.rs//go + kind: direct + confidence: high + site: src/main.rs:5 + - from: src/jobs/worker.rs//go + to: src/store/disk.rs//wipe + kind: direct + confidence: high + site: src/jobs/worker.rs:4 + - from: src/store/disk.rs//wipe + to: std//fs//remove_dir_all + kind: direct + confidence: high + site: src/store/disk.rs:2 +unresolved: [] +results: + - target: std//fs//remove_dir_all + status: found + path: + - key: src/main.rs//main + - key: src/jobs/worker.rs//go + site: src/main.rs:5 + kind: direct + confidence: high + - key: src/store/disk.rs//wipe + site: src/jobs/worker.rs:4 + kind: direct + confidence: high + - key: std//fs//remove_dir_all + site: src/store/disk.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/store/disk.rs//wipe + site: src/store/disk.rs:2 + reached: true diff --git a/test/projectgraph/cases/rust/glob-use-binding/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-binding/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-binding/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-binding/project/src/jobs/mod.rs b/test/projectgraph/cases/rust/glob-use-binding/project/src/jobs/mod.rs new file mode 100644 index 00000000..34096fe2 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-binding/project/src/jobs/mod.rs @@ -0,0 +1,3 @@ +use crate::store::disk; + +pub mod worker; diff --git a/test/projectgraph/cases/rust/glob-use-binding/project/src/jobs/worker.rs b/test/projectgraph/cases/rust/glob-use-binding/project/src/jobs/worker.rs new file mode 100644 index 00000000..7699dc39 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-binding/project/src/jobs/worker.rs @@ -0,0 +1,5 @@ +use super::*; + +pub fn go() { + disk::wipe("x"); +} diff --git a/test/projectgraph/cases/rust/glob-use-binding/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-binding/project/src/main.rs new file mode 100644 index 00000000..f42066e6 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-binding/project/src/main.rs @@ -0,0 +1,6 @@ +mod jobs; +mod store; + +fn main() { + jobs::worker::go(); +} diff --git a/test/projectgraph/cases/rust/glob-use-binding/project/src/store/disk.rs b/test/projectgraph/cases/rust/glob-use-binding/project/src/store/disk.rs new file mode 100644 index 00000000..c1f927fd --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-binding/project/src/store/disk.rs @@ -0,0 +1,3 @@ +pub fn wipe(p: &str) { + let _ = std::fs::remove_dir_all(p); +} diff --git a/test/projectgraph/cases/rust/glob-use-binding/project/src/store/mod.rs b/test/projectgraph/cases/rust/glob-use-binding/project/src/store/mod.rs new file mode 100644 index 00000000..088789b7 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-binding/project/src/store/mod.rs @@ -0,0 +1 @@ +pub mod disk; diff --git a/test/projectgraph/cases/rust/use-chain-module/expect.yaml b/test/projectgraph/cases/rust/use-chain-module/expect.yaml new file mode 100644 index 00000000..d4fb621d --- /dev/null +++ b/test/projectgraph/cases/rust/use-chain-module/expect.yaml @@ -0,0 +1,53 @@ +source: safedep:hand +description: >- + src/worker.rs has use crate::deep, then use deep::util, then use util::helpers. Each use reads the module + that the use before it binds, so helpers names the module src/deep/util/helpers.rs, and go calls its function + cleanup. cleanup calls std::fs::remove_file, so the target is found. A link step that stops a path on a + module that a use binds does not resolve deep::util. The call of helpers::cleanup then gets the edge to the + key util//helpers//cleanup with no record, and std::fs::remove_file reads not-found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: src/main.rs//main + to: src/worker.rs//go + kind: direct + confidence: high + site: src/main.rs:5 + - from: src/worker.rs//go + to: src/deep/util/helpers.rs//cleanup + kind: direct + confidence: high + site: src/worker.rs:6 + - from: src/deep/util/helpers.rs//cleanup + to: std//fs//remove_file + kind: direct + confidence: high + site: src/deep/util/helpers.rs:2 +unresolved: [] +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/worker.rs//go + site: src/main.rs:5 + kind: direct + confidence: high + - key: src/deep/util/helpers.rs//cleanup + site: src/worker.rs:6 + kind: direct + confidence: high + - key: std//fs//remove_file + site: src/deep/util/helpers.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/deep/util/helpers.rs//cleanup + site: src/deep/util/helpers.rs:2 + reached: true diff --git a/test/projectgraph/cases/rust/use-chain-module/project/Cargo.toml b/test/projectgraph/cases/rust/use-chain-module/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/use-chain-module/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/use-chain-module/project/src/deep/mod.rs b/test/projectgraph/cases/rust/use-chain-module/project/src/deep/mod.rs new file mode 100644 index 00000000..812d1edf --- /dev/null +++ b/test/projectgraph/cases/rust/use-chain-module/project/src/deep/mod.rs @@ -0,0 +1 @@ +pub mod util; diff --git a/test/projectgraph/cases/rust/use-chain-module/project/src/deep/util/helpers.rs b/test/projectgraph/cases/rust/use-chain-module/project/src/deep/util/helpers.rs new file mode 100644 index 00000000..af9dfae2 --- /dev/null +++ b/test/projectgraph/cases/rust/use-chain-module/project/src/deep/util/helpers.rs @@ -0,0 +1,3 @@ +pub fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} diff --git a/test/projectgraph/cases/rust/use-chain-module/project/src/deep/util/mod.rs b/test/projectgraph/cases/rust/use-chain-module/project/src/deep/util/mod.rs new file mode 100644 index 00000000..1630fabc --- /dev/null +++ b/test/projectgraph/cases/rust/use-chain-module/project/src/deep/util/mod.rs @@ -0,0 +1 @@ +pub mod helpers; diff --git a/test/projectgraph/cases/rust/use-chain-module/project/src/main.rs b/test/projectgraph/cases/rust/use-chain-module/project/src/main.rs new file mode 100644 index 00000000..21d9fc49 --- /dev/null +++ b/test/projectgraph/cases/rust/use-chain-module/project/src/main.rs @@ -0,0 +1,6 @@ +mod deep; +mod worker; + +fn main() { + worker::go(); +} diff --git a/test/projectgraph/cases/rust/use-chain-module/project/src/worker.rs b/test/projectgraph/cases/rust/use-chain-module/project/src/worker.rs new file mode 100644 index 00000000..851a06aa --- /dev/null +++ b/test/projectgraph/cases/rust/use-chain-module/project/src/worker.rs @@ -0,0 +1,7 @@ +use crate::deep; +use deep::util; +use util::helpers; + +pub fn go() { + helpers::cleanup("x"); +} From d30405a256cd1130ac23f8144d4b59b4bee5e807 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:51:40 +0000 Subject: [PATCH 11/86] fix(indexer): record a Rust call on a name that a second let binds The call graph binds a name to the value of its first let. After a second let of the name with another type, a call on the name kept the method of the first type, and a method of the real type read as not-found. Such a call is now a record with the reason unknown-receiver. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 6 +- plugin/indexer/rust.go | 20 ++++-- .../cases/rust/shadowed-let-type/expect.yaml | 72 +++++++++++++++++++ .../rust/shadowed-let-type/project/Cargo.toml | 4 ++ .../shadowed-let-type/project/src/main.rs | 25 +++++++ 5 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 test/projectgraph/cases/rust/shadowed-let-type/expect.yaml create mode 100644 test/projectgraph/cases/rust/shadowed-let-type/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/shadowed-let-type/project/src/main.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index d4c81b1d..e841f77f 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -398,8 +398,10 @@ Rust follows the same rules, with these additions. name that its call passes as a token. A macro of std binds no such name, except `thread_local!`, which makes a `static`. So a call on a name that such a binder binds before the call is a record with the reason `unknown-receiver`, and so is - a call whose declared type does not hold. A method of the real type then - reads as `uncertain` and not as `not-found`. + a call whose declared type does not hold. The call graph binds a name to + the value of its first `let`, so a call after a second `let` of the name + is a record too, as in the case `rust/shadowed-let-type`. A method of the + real type then reads as `uncertain` and not as `not-found`. - The link step resolves the declared type of a parameter or a `let` at the root of the file. So the index keeps a declared type only when no scope that holds the binder declares the first name of the type. Such a diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 1d8acfc1..f2881aa5 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -502,17 +502,29 @@ func rustDeclaredAt(ctx context.Context, p *projection, call fileindex.Call, nam } // rustRebound reports whether the function binds a name before a call by a -// binder that the call graph does not read. A static or a const binds its -// name in the whole block, also before it. A function whose walk stopped -// early can bind any name. +// binder that the call graph does not read, or by a second let. A static or +// a const binds its name in the whole block, also before it. The call graph +// binds a name to the value of its first let, so it sends t.run() after +// let t = B to A::run when let t = A comes first. A function whose walk +// stopped early can bind any name. func rustRebound(ctx context.Context, p *projection, call fileindex.Call, name string) bool { binders, ok := p.rustBinders(ctx, call.Caller) if !ok { return true } + lets := 0 return slices.ContainsFunc(binders[name], func(binder *sitter.Node) bool { item := binder.Type() == "static_item" || binder.Type() == "const_item" - return (item || startsBefore(binder, call)) && !rustPlainBinder(binder) + if !item && !startsBefore(binder, call) { + return false + } + if !rustPlainBinder(binder) { + return true + } + if binder.Type() == "let_declaration" { + lets++ + } + return lets > 1 }) } diff --git a/test/projectgraph/cases/rust/shadowed-let-type/expect.yaml b/test/projectgraph/cases/rust/shadowed-let-type/expect.yaml new file mode 100644 index 00000000..87f11ecd --- /dev/null +++ b/test/projectgraph/cases/rust/shadowed-let-type/expect.yaml @@ -0,0 +1,72 @@ +source: safedep:hand +description: >- + go binds t with a let to a Disk and calls t.save(), then binds t again with a second let to a Cloud and + calls t.save() again. The second call runs Cloud::save, which calls std::fs::remove_dir_all. The call graph + binds a name to the value of its first let, so it sends both calls to Disk::save. The second call then gets + no record and no edge to Cloud::save, and std::fs::remove_dir_all reads not-found. The index keeps the first + call and makes the second one a record with the reason unknown-receiver, so the second call gets the low + name edges of save. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//go + to: src/main.rs//Disk//save + kind: member + confidence: high + site: src/main.rs:18 + - from: src/main.rs//go + to: src/main.rs//Cloud//save + kind: name + confidence: low + site: src/main.rs:20 +unresolved: + - caller: src/main.rs//go + site: src/main.rs:20 + reason: unknown-receiver + method: save +results: + - target: std//fs//write + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//go + site: src/main.rs:24 + kind: direct + confidence: high + - key: src/main.rs//Disk//save + site: src/main.rs:18 + kind: member + confidence: high + - key: std//fs//write + site: src/main.rs:6 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//Disk//save + site: src/main.rs:6 + reached: true + - target: std//fs//remove_dir_all + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//go + site: src/main.rs:24 + kind: direct + confidence: high + - key: src/main.rs//Cloud//save + site: src/main.rs:20 + kind: name + confidence: low + - key: std//fs//remove_dir_all + site: src/main.rs:12 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//Cloud//save + site: src/main.rs:12 + reached: true diff --git a/test/projectgraph/cases/rust/shadowed-let-type/project/Cargo.toml b/test/projectgraph/cases/rust/shadowed-let-type/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/shadowed-let-type/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/shadowed-let-type/project/src/main.rs b/test/projectgraph/cases/rust/shadowed-let-type/project/src/main.rs new file mode 100644 index 00000000..fc5eaeb8 --- /dev/null +++ b/test/projectgraph/cases/rust/shadowed-let-type/project/src/main.rs @@ -0,0 +1,25 @@ +pub struct Disk; +pub struct Cloud; + +impl Disk { + pub fn save(&self) { + let _ = std::fs::write("disk", "x"); + } +} + +impl Cloud { + pub fn save(&self) { + let _ = std::fs::remove_dir_all("cloud"); + } +} + +fn go() { + let t = Disk; + t.save(); + let t = Cloud; + t.save(); +} + +fn main() { + go(); +} From 1c353ede772549aed4b0dbb6bf73b7884504062d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:52:44 +0000 Subject: [PATCH 12/86] fix(indexer): read a Rust macro of the file with the name of a std macro A macro of std binds no name of its caller, so the index trusted a call on a name that a vec! or a println! call held as a token. A macro_rules! or a use of the file can give such a name to a macro that binds the name. The call then kept the target of the first binding with no record, and a method of the real type read as not-found. The index now reads such a macro as a binder. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 4 +- plugin/indexer/project.go | 3 ++ plugin/indexer/rust.go | 22 ++++++++- .../cases/rust/macro-std-name/expect.yaml | 47 +++++++++++++++++++ .../rust/macro-std-name/project/Cargo.toml | 4 ++ .../rust/macro-std-name/project/src/main.rs | 30 ++++++++++++ 6 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 test/projectgraph/cases/rust/macro-std-name/expect.yaml create mode 100644 test/projectgraph/cases/rust/macro-std-name/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-std-name/project/src/main.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index e841f77f..8fa22b6f 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -396,7 +396,9 @@ Rust follows the same rules, with these additions. name again with another type. The hygiene of Rust keeps a `let` of a macro body apart from the names of the caller, so a macro binds only a name that its call passes as a token. A macro of std binds no such name, - except `thread_local!`, which makes a `static`. So a call on a name that such a binder binds + except `thread_local!`, which makes a `static`. A `macro_rules!` or a + `use` of the file can give the name of a macro of std to a macro that + binds a name, as in the case `rust/macro-std-name`. So a call on a name that such a binder binds before the call is a record with the reason `unknown-receiver`, and so is a call whose declared type does not hold. The call graph binds a name to the value of its first `let`, so a call after a second `let` of the name diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index 59abf178..0b9412e6 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -65,6 +65,9 @@ type projection struct { // linknames holds the remote name of each local name that a // //go:linkname directive of a Go file binds. linknames map[string]string + // rustMacros holds the names that a macro_rules! or a use of a Rust + // file binds, once a call of a macro reads them. + rustMacros map[string]bool } // project builds the call graph of tree and projects it into an index. It diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index f2881aa5..53187039 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -2,6 +2,7 @@ package indexer import ( "context" + "maps" "slices" "strings" @@ -621,7 +622,9 @@ func (p *projection) rustBinder(id *sitter.Node) *sitter.Node { // rustBindingMacro returns the call of the macro that holds a token tree, // when the macro can bind a name of its caller, as bind!(t) with the rule // ($n:ident) => { let $n = dep::new_thing(); }. A macro of std binds no -// name of its caller, except thread_local!, which makes a static. +// name of its caller, except thread_local!, which makes a static. A +// macro_rules! or a use of the file can give a name of std to another +// macro, which can bind a name. func (p *projection) rustBindingMacro(tree *sitter.Node) *sitter.Node { for tree != nil && tree.Type() == "token_tree" { tree = tree.Parent() @@ -629,12 +632,27 @@ func (p *projection) rustBindingMacro(tree *sitter.Node) *sitter.Node { if tree == nil || tree.Type() != "macro_invocation" { return nil } - if name := rustLastName(p.text(tree.ChildByFieldName("macro"))); langcode.IsBuiltin(langcode.Rust, name) && name != "thread_local" { + name := rustLastName(p.text(tree.ChildByFieldName("macro"))) + if langcode.IsBuiltin(langcode.Rust, name) && name != "thread_local" && !p.rustOwnMacro(name) { return nil } return tree } +// rustOwnMacro reports whether the file defines a macro of the name with +// macro_rules! or binds the name with a use. +func (p *projection) rustOwnMacro(name string) bool { + if p.rustMacros == nil { + p.rustMacros = maps.Clone(p.bindings) + for key, node := range p.syntax { + if node.Type() == "macro_definition" { + p.rustMacros[fileindex.Last(key)] = true + } + } + } + return p.rustMacros[name] +} + // rustBoundName returns the name that a pattern binds, as t for t or for // mut t, and nil for any other pattern. func rustBoundName(pattern *sitter.Node) *sitter.Node { diff --git a/test/projectgraph/cases/rust/macro-std-name/expect.yaml b/test/projectgraph/cases/rust/macro-std-name/expect.yaml new file mode 100644 index 00000000..5e198bb9 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-std-name/expect.yaml @@ -0,0 +1,47 @@ +source: safedep:hand +description: >- + The file defines its own macro_rules! vec, whose rule binds the name that its call passes to a Cloud. go + binds t to a Disk, calls vec!(t), and calls t.save(), which runs Cloud::save. Cloud::save calls + std::fs::remove_dir_all. A macro of std binds no name of its caller, but this vec is no macro of std. An + index that takes vec! for the macro of std keeps the call graph target Disk::save with no record, and + std::fs::remove_dir_all reads not-found. The index makes the call a record with the reason + unknown-receiver, so it gets the low name edges of save. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//go + to: src/main.rs//Cloud//save + kind: name + confidence: low + site: src/main.rs:25 +unresolved: + - caller: src/main.rs//go + site: src/main.rs:25 + reason: unknown-receiver + method: save +results: + - target: std//fs//remove_dir_all + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//go + site: src/main.rs:29 + kind: direct + confidence: high + - key: src/main.rs//Cloud//save + site: src/main.rs:25 + kind: name + confidence: low + - key: std//fs//remove_dir_all + site: src/main.rs:12 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//Cloud//save + site: src/main.rs:12 + reached: true diff --git a/test/projectgraph/cases/rust/macro-std-name/project/Cargo.toml b/test/projectgraph/cases/rust/macro-std-name/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/macro-std-name/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/macro-std-name/project/src/main.rs b/test/projectgraph/cases/rust/macro-std-name/project/src/main.rs new file mode 100644 index 00000000..fe2c6971 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-std-name/project/src/main.rs @@ -0,0 +1,30 @@ +pub struct Disk; +pub struct Cloud; + +impl Disk { + pub fn save(&self) { + let _ = std::fs::write("disk", "x"); + } +} + +impl Cloud { + pub fn save(&self) { + let _ = std::fs::remove_dir_all("cloud"); + } +} + +macro_rules! vec { + ($n:ident) => { + let $n = Cloud; + }; +} + +fn go() { + let t = Disk; + vec!(t); + t.save(); +} + +fn main() { + go(); +} From 11e36186ce9f54f038082386ac2cf7b12814eb4f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:54:17 +0000 Subject: [PATCH 13/86] fix(lang): read self and super of a Rust use in its inline module The index keeps the uses of a file in one scope, and the link step reads a path of a use from the module of the file. So use self::tls::conn in mod client named the tls of the file, and use super::tls in mod client named a module of the parent of the file. A call through the use got the edge to the wrong function, and its real target read as not-found. The resolver now writes such a path from the module of the file. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 5 ++ lang/rust_resolvers.go | 58 ++++++++++++++++++- .../rust/use-self-inline-module/expect.yaml | 51 ++++++++++++++++ .../use-self-inline-module/project/Cargo.toml | 4 ++ .../project/src/main.rs | 5 ++ .../use-self-inline-module/project/src/net.rs | 23 ++++++++ .../rust/use-super-inline-module/expect.yaml | 51 ++++++++++++++++ .../project/Cargo.toml | 4 ++ .../project/src/main.rs | 6 ++ .../project/src/net.rs | 9 +++ .../project/src/net/tls.rs | 3 + .../project/src/tls.rs | 3 + 12 files changed, 221 insertions(+), 1 deletion(-) create mode 100644 test/projectgraph/cases/rust/use-self-inline-module/expect.yaml create mode 100644 test/projectgraph/cases/rust/use-self-inline-module/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-self-inline-module/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-self-inline-module/project/src/net.rs create mode 100644 test/projectgraph/cases/rust/use-super-inline-module/expect.yaml create mode 100644 test/projectgraph/cases/rust/use-super-inline-module/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-super-inline-module/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-super-inline-module/project/src/net.rs create mode 100644 test/projectgraph/cases/rust/use-super-inline-module/project/src/net/tls.rs create mode 100644 test/projectgraph/cases/rust/use-super-inline-module/project/src/tls.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 8fa22b6f..f6a94d21 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -440,6 +440,11 @@ Rust follows the same rules, with these additions. `use deep::util;` names the module `deep::util::helpers`, as in the case `rust/use-chain-module`. A glob `use` brings such a binding of its module too, as in the case `rust/glob-use-binding`. +- A `use` in an inline module reads `self` and `super` from that module. + The index keeps the uses of a file in one scope, so it writes such a + path from the module of the file, as `self::client::tls::conn` for + `use self::tls::conn;` in `mod client`, as in the cases + `rust/use-self-inline-module` and `rust/use-super-inline-module`. - A glob `use` brings each name of its module into the module of the file. So `helpers::cleanup` after `use super::*;` in `src/deep/worker.rs` names the function of the module `deep::helpers`, diff --git a/lang/rust_resolvers.go b/lang/rust_resolvers.go index 3bf4d544..1c4f1b2a 100644 --- a/lang/rust_resolvers.go +++ b/lang/rust_resolvers.go @@ -3,6 +3,8 @@ package lang import ( "context" "fmt" + "slices" + "strings" "unicode" "unicode/utf8" @@ -45,7 +47,13 @@ func (r *rustResolvers) ResolveImports(ctx context.Context, tree core.ParseTree) err = walkNamed(ctx, tree.Tree().RootNode(), func(node *sitter.Node) bool { switch node.Type() { case "use_declaration": - imports = append(imports, rustUseImports(data, node.ChildByFieldName("argument"), nil, nil)...) + uses := rustUseImports(data, node.ChildByFieldName("argument"), nil, nil) + if mods := rustInlineModules(data, node); len(mods) > 0 { + for _, imp := range uses { + imp.SetModuleName(rustFromFile(imp.ModuleName(), mods)) + } + } + imports = append(imports, uses...) return false case "extern_crate_declaration": name := node.ChildByFieldName("name") @@ -84,6 +92,54 @@ func (r *rustResolvers) ResolveImports(ctx context.Context, tree core.ParseTree) return imports, nil } +// rustInlineModules returns the names of the inline modules that hold a +// node, the outermost first, as [outer inner] for a use in mod outer { mod +// inner { .. } }. +func rustInlineModules(data *[]byte, node *sitter.Node) []string { + var mods []string + for n := node.Parent(); n != nil; n = n.Parent() { + if n.Type() != "mod_item" { + continue + } + if name := n.ChildByFieldName("name"); name != nil { + mods = append(mods, name.Content(*data)) + } + } + slices.Reverse(mods) + return mods +} + +// rustFromFile writes a path that starts with self or super in an inline +// module as a path from the module of the file, as self::outer::x for +// super::x in mod outer { mod inner { .. } }. The index keeps the uses of a +// file in one scope, which reads such a path from the module of the file. +// So self::x in mod m named the x of the file, and a call through it lost +// its edge to m::x. +func rustFromFile(path string, mods []string) string { + parts := strings.Split(path, "::") + ups := 0 + switch { + case parts[0] == "self": + parts = parts[1:] + case parts[0] == "super": + for ups < len(parts) && parts[ups] == "super" { + ups++ + } + parts = parts[ups:] + default: + return path + } + var out []string + if ups > len(mods) { + for range ups - len(mods) { + out = append(out, "super") + } + } else { + out = append(append(out, "self"), mods[:len(mods)-ups]...) + } + return strings.Join(append(out, parts...), "::") +} + // rustUseImports returns the imports of a use tree. The prefix is the path of // the outer use list and the module of each import. The nested path is the // path of a use list inside it. An import node has one node for each part, so diff --git a/test/projectgraph/cases/rust/use-self-inline-module/expect.yaml b/test/projectgraph/cases/rust/use-self-inline-module/expect.yaml new file mode 100644 index 00000000..7a72ab60 --- /dev/null +++ b/test/projectgraph/cases/rust/use-self-inline-module/expect.yaml @@ -0,0 +1,51 @@ +source: safedep:hand +description: >- + src/net.rs declares the module tls at its top level and the inline module client, which declares its own + module tls. client has use self::tls::conn, so conn names client::tls::conn, and go calls conn::open, which + calls std::fs::create_dir. The tls of the top level has its own conn::open, which calls std::fs::read and + which nothing calls. A link step that reads self of the inline module as the module of the file gives the call + the edge to the wrong open, so std::fs::create_dir reads not-found and std::fs::read reads found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/net.rs//client//go + to: src/net.rs//client//tls//conn//open + kind: member + confidence: high + site: src/net.rs:21 +no_edges: + - from: src/net.rs//client//go + to: src/net.rs//tls//conn//open +unresolved: [] +results: + - target: std//fs//create_dir + status: found + path: + - key: src/main.rs//main + - key: src/net.rs//client//go + site: src/main.rs:4 + kind: direct + confidence: high + - key: src/net.rs//client//tls//conn//open + site: src/net.rs:21 + kind: member + confidence: high + - key: std//fs//create_dir + site: src/net.rs:13 + kind: direct + confidence: high + call_sites: + - caller: src/net.rs//client//tls//conn//open + site: src/net.rs:13 + reached: true + - target: std//fs//read + status: not-found + call_sites: + - caller: src/net.rs//tls//conn//open + site: src/net.rs:4 + reached: false diff --git a/test/projectgraph/cases/rust/use-self-inline-module/project/Cargo.toml b/test/projectgraph/cases/rust/use-self-inline-module/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/use-self-inline-module/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/use-self-inline-module/project/src/main.rs b/test/projectgraph/cases/rust/use-self-inline-module/project/src/main.rs new file mode 100644 index 00000000..12c48ae4 --- /dev/null +++ b/test/projectgraph/cases/rust/use-self-inline-module/project/src/main.rs @@ -0,0 +1,5 @@ +mod net; + +fn main() { + net::client::go(); +} diff --git a/test/projectgraph/cases/rust/use-self-inline-module/project/src/net.rs b/test/projectgraph/cases/rust/use-self-inline-module/project/src/net.rs new file mode 100644 index 00000000..931eb200 --- /dev/null +++ b/test/projectgraph/cases/rust/use-self-inline-module/project/src/net.rs @@ -0,0 +1,23 @@ +pub mod tls { + pub mod conn { + pub fn open() { + let _ = std::fs::read("top"); + } + } +} + +pub mod client { + pub mod tls { + pub mod conn { + pub fn open() { + let _ = std::fs::create_dir("client"); + } + } + } + + use self::tls::conn; + + pub fn go() { + conn::open(); + } +} diff --git a/test/projectgraph/cases/rust/use-super-inline-module/expect.yaml b/test/projectgraph/cases/rust/use-super-inline-module/expect.yaml new file mode 100644 index 00000000..459385db --- /dev/null +++ b/test/projectgraph/cases/rust/use-super-inline-module/expect.yaml @@ -0,0 +1,51 @@ +source: safedep:hand +description: >- + src/net.rs declares the module tls in the file src/net/tls.rs and the inline module client. client has + use super::tls, and super of client is the module net, so tls names net::tls, and go calls tls::open, which + calls std::fs::create_dir. The root declares a module tls of its own in src/tls.rs, whose open calls + std::fs::read and which nothing calls. A link step that reads super of the inline module from the module of + the file names the tls of the root, so std::fs::create_dir reads not-found and std::fs::read reads found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/net.rs//client//go + to: src/net/tls.rs//open + kind: direct + confidence: high + site: src/net.rs:7 +no_edges: + - from: src/net.rs//client//go + to: src/tls.rs//open +unresolved: [] +results: + - target: std//fs//create_dir + status: found + path: + - key: src/main.rs//main + - key: src/net.rs//client//go + site: src/main.rs:5 + kind: direct + confidence: high + - key: src/net/tls.rs//open + site: src/net.rs:7 + kind: direct + confidence: high + - key: std//fs//create_dir + site: src/net/tls.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/net/tls.rs//open + site: src/net/tls.rs:2 + reached: true + - target: std//fs//read + status: not-found + call_sites: + - caller: src/tls.rs//open + site: src/tls.rs:2 + reached: false diff --git a/test/projectgraph/cases/rust/use-super-inline-module/project/Cargo.toml b/test/projectgraph/cases/rust/use-super-inline-module/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/use-super-inline-module/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/use-super-inline-module/project/src/main.rs b/test/projectgraph/cases/rust/use-super-inline-module/project/src/main.rs new file mode 100644 index 00000000..a2002ce8 --- /dev/null +++ b/test/projectgraph/cases/rust/use-super-inline-module/project/src/main.rs @@ -0,0 +1,6 @@ +mod net; +mod tls; + +fn main() { + net::client::go(); +} diff --git a/test/projectgraph/cases/rust/use-super-inline-module/project/src/net.rs b/test/projectgraph/cases/rust/use-super-inline-module/project/src/net.rs new file mode 100644 index 00000000..f6ce7552 --- /dev/null +++ b/test/projectgraph/cases/rust/use-super-inline-module/project/src/net.rs @@ -0,0 +1,9 @@ +pub mod tls; + +pub mod client { + use super::tls; + + pub fn go() { + tls::open(); + } +} diff --git a/test/projectgraph/cases/rust/use-super-inline-module/project/src/net/tls.rs b/test/projectgraph/cases/rust/use-super-inline-module/project/src/net/tls.rs new file mode 100644 index 00000000..f9aac751 --- /dev/null +++ b/test/projectgraph/cases/rust/use-super-inline-module/project/src/net/tls.rs @@ -0,0 +1,3 @@ +pub fn open() { + let _ = std::fs::create_dir("net"); +} diff --git a/test/projectgraph/cases/rust/use-super-inline-module/project/src/tls.rs b/test/projectgraph/cases/rust/use-super-inline-module/project/src/tls.rs new file mode 100644 index 00000000..ee18742b --- /dev/null +++ b/test/projectgraph/cases/rust/use-super-inline-module/project/src/tls.rs @@ -0,0 +1,3 @@ +pub fn open() { + let _ = std::fs::read("root"); +} From 94d9ef2b14edf92ce95791aa9269d4af1d377535 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:56:20 +0000 Subject: [PATCH 14/86] fix(indexer): follow a Rust use or a type parameter that shadows a name The call graph finds the first name of a call path in the scopes that hold the call and reads no use and no type parameter. So util::wipe() after use crate::store::util in a block or an inline module went to a module util of the file, and Local::make() in fn build() went to a struct Local of the file. The real target read as not-found. The index now gives such a call the path of the use, and makes a call through a type parameter a record with the reason unknown-receiver. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 7 ++ plugin/indexer/calls.go | 4 + plugin/indexer/languages.go | 4 + plugin/indexer/rust.go | 79 +++++++++++++++++++ .../rust/generic-shadows-type/expect.yaml | 32 ++++++++ .../generic-shadows-type/project/Cargo.toml | 4 + .../generic-shadows-type/project/src/main.rs | 29 +++++++ .../cases/rust/use-shadows-module/expect.yaml | 59 ++++++++++++++ .../use-shadows-module/project/Cargo.toml | 4 + .../use-shadows-module/project/src/jobs.rs | 18 +++++ .../use-shadows-module/project/src/main.rs | 7 ++ .../project/src/store/mod.rs | 1 + .../project/src/store/util.rs | 3 + 13 files changed, 251 insertions(+) create mode 100644 test/projectgraph/cases/rust/generic-shadows-type/expect.yaml create mode 100644 test/projectgraph/cases/rust/generic-shadows-type/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/generic-shadows-type/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-shadows-module/expect.yaml create mode 100644 test/projectgraph/cases/rust/use-shadows-module/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-shadows-module/project/src/jobs.rs create mode 100644 test/projectgraph/cases/rust/use-shadows-module/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-shadows-module/project/src/store/mod.rs create mode 100644 test/projectgraph/cases/rust/use-shadows-module/project/src/store/util.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index f6a94d21..701bc2f5 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -445,6 +445,13 @@ Rust follows the same rules, with these additions. path from the module of the file, as `self::client::tls::conn` for `use self::tls::conn;` in `mod client`, as in the cases `rust/use-self-inline-module` and `rust/use-super-inline-module`. +- The call graph finds the first name of a path in the scopes that hold + the call, and reads no `use` and no type parameter. A `use` in a block + or in an inline module can bind the name again, and the index then gives + the call the path of that `use`, as in the case `rust/use-shadows-module`. + A type parameter of the name names a type that the caller gives, so the + call is a record with the reason `unknown-receiver`, as in the case + `rust/generic-shadows-type`. - A glob `use` brings each name of its module into the module of the file. So `helpers::cleanup` after `use super::*;` in `src/deep/worker.rs` names the function of the module `deep::helpers`, diff --git a/plugin/indexer/calls.go b/plugin/indexer/calls.go index 280363df..0a60714a 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -113,6 +113,10 @@ func (p *projection) addCalls(ctx context.Context) { Args: p.arguments(key, ref.Site), } p.retargetTyped(ctx, &call, untyped[[2]uint32{ref.Site.StartLine, ref.Site.StartColumn}]) + if shadowed := p.rules().shadowed; shadowed != nil && !shadowed(p, &call, ref.Site) { + p.addRecord(call, fileindex.UnresolvedUnknownReceiver) + continue + } if !p.bindsAsWritten(ctx, call) { p.addRecord(call, fileindex.UnresolvedUnknownReceiver) continue diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 2398babe..d74eaca5 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -91,6 +91,10 @@ type languageRules struct { // prune drops the definitions that hold no record, after the records. prune func(p *projection) + // shadowed fixes the targets of a call whose first name a scope between + // the call and the definition of the target binds again, as a use in a + // block of Rust. It reports false when the call is a record. + shadowed func(p *projection, call *fileindex.Call, callee ast.NodePosition) bool // markTries marks each ? in the targets of a call from the syntax of // its callee, as the ? of Rust in dep//load//?//run. The call graph // keeps no such mark. diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 53187039..a21f797c 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -28,6 +28,7 @@ var rustRules = languageRules{ exported: rustPublic, definitions: rustTypes, markTries: rustMarkTries, + shadowed: rustShadowed, visitors: []func(p *projection) visitor{referenceVisitor, rustVisitor}, declaredAt: rustDeclaredAt, rebound: rustRebound, @@ -665,6 +666,84 @@ func rustBoundName(pattern *sitter.Node) *sitter.Node { return pattern } +// rustShadowed fixes a target that the call graph finds for the path of a +// call in an outer scope, when a scope between the call and that scope +// binds the first name of the path again. The call graph reads no use and +// no type parameter. So util::g() after use crate::deep::util in a block +// went to the module util of the file, and the real target read as +// not-found. A use gives the target through the path of the use. A type +// parameter names a type that the call does not know, so the call is a +// record. +func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) bool { + node := p.descendant(callee) + if node == nil || node.Type() != "identifier" && node.Type() != "scoped_identifier" { + return true + } + names := strings.Split(p.text(node), "::") + switch names[0] { + case "crate", "self", "super", "Self", "": + return true + } + written := strings.Join(names, fileindex.Separator) + for i, target := range call.Targets { + scope, ok := strings.CutSuffix(target, fileindex.Separator+written) + if !ok || !fileindex.HasKeyPrefix(scope, p.cg.FileName) { + continue + } + binder, use := p.rustRebinder(node, p.syntax[scope], names[0]) + switch { + case binder == nil: + case !use: + return false + default: + namespace, ok := p.rustUseNamespace(binder, names[0]) + if !ok { + return false + } + call.Targets[i] = fileindex.Join(namespace, names[1:]...) + } + } + return true +} + +// rustRebinder returns the innermost use or item with type parameters +// between a node and the definition of a scope that binds a name, and +// whether it is a use. A nil scope is the file. +func (p *projection) rustRebinder(node, scope *sitter.Node, name string) (*sitter.Node, bool) { + for n := node.Parent(); n != nil && (scope == nil || !ts.SameNode(n, scope)); n = n.Parent() { + switch n.Type() { + case "block", "declaration_list": + for _, item := range ts.NamedChildren(n) { + if item.Type() != "use_declaration" { + continue + } + if names, _ := p.rustUseNames(item.ChildByFieldName("argument"), nil); slices.Contains(names, name) { + return item, true + } + } + case "function_item", "impl_item", "trait_item": + if slices.ContainsFunc(ts.NamedChildren(n.ChildByFieldName("type_parameters")), func(param *sitter.Node) bool { + return p.rustTypeParameter(param) == name + }) { + return n, false + } + } + } + return nil, false +} + +// rustUseNamespace returns the namespace of the import of a use that binds +// a name. +func (p *projection) rustUseNamespace(use *sitter.Node, name string) (string, bool) { + first, last := lineOf(use), int(use.EndPoint().Row)+1 + for _, imp := range p.ix.Imports { + if imp.Line >= first && imp.Line <= last && imp.Alias == name && imp.Namespace != "" { + return imp.Namespace, true + } + } + return "", false +} + // rustMarkTries marks each ? in the receiver of a method call in the // targets of the call, as dep//load//?//run for dep::load()?.run() and for // t.run() after let t = dep::load()?. The call graph reads through a ?, so diff --git a/test/projectgraph/cases/rust/generic-shadows-type/expect.yaml b/test/projectgraph/cases/rust/generic-shadows-type/expect.yaml new file mode 100644 index 00000000..53db1bba --- /dev/null +++ b/test/projectgraph/cases/rust/generic-shadows-type/expect.yaml @@ -0,0 +1,32 @@ +source: safedep:hand +description: >- + build has the type parameter Local with the bound Maker, which shadows the struct Local of the file. So + Local::make() in build runs make of the type that the caller gives, Real here, whose make calls + std::fs::create_dir. The make of the struct Local calls std::fs::remove_file and nothing calls it. The call + graph reads no type parameter, so it sends the call to Local::make of the struct, and std::fs::create_dir + reads not-found. The index makes the call a record with the reason unknown-receiver. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +no_edges: + - from: src/main.rs//build + to: src/main.rs//Local//make +edges: [] +unresolved: + - caller: src/main.rs//build + site: src/main.rs:24 + reason: unknown-receiver +results: + - target: std//fs//create_dir + status: uncertain + call_sites: + - caller: src/main.rs//Real//make + site: src/main.rs:17 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:24 diff --git a/test/projectgraph/cases/rust/generic-shadows-type/project/Cargo.toml b/test/projectgraph/cases/rust/generic-shadows-type/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/generic-shadows-type/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/generic-shadows-type/project/src/main.rs b/test/projectgraph/cases/rust/generic-shadows-type/project/src/main.rs new file mode 100644 index 00000000..07177f3f --- /dev/null +++ b/test/projectgraph/cases/rust/generic-shadows-type/project/src/main.rs @@ -0,0 +1,29 @@ +pub struct Local; +pub struct Real; + +pub trait Maker { + fn make() -> Self; +} + +impl Local { + pub fn make() -> Local { + let _ = std::fs::remove_file("local"); + Local + } +} + +impl Maker for Real { + fn make() -> Real { + let _ = std::fs::create_dir("real"); + Real + } +} + +#[allow(non_camel_case_types)] +fn build() -> Local { + Local::make() +} + +fn main() { + let _ = build::(); +} diff --git a/test/projectgraph/cases/rust/use-shadows-module/expect.yaml b/test/projectgraph/cases/rust/use-shadows-module/expect.yaml new file mode 100644 index 00000000..c3481f30 --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadows-module/expect.yaml @@ -0,0 +1,59 @@ +source: safedep:hand +description: >- + src/jobs.rs declares its own inline module util, whose wipe calls std::fs::remove_file and which nothing calls. + The inline module worker has use crate::store::util, and the function flush has the same use in its block. So + util::wipe in worker::go and in flush names the module src/store/util.rs, whose wipe calls + std::fs::remove_dir_all. The call graph reads no use, so it finds util in the scope of the file and sends both + calls to the inline util. std::fs::remove_dir_all then reads not-found. The index gives each call the path + of its use. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/jobs.rs//worker//go + to: src/store/util.rs//wipe + kind: direct + confidence: high + site: src/jobs.rs:11 + - from: src/jobs.rs//flush + to: src/store/util.rs//wipe + kind: direct + confidence: high + site: src/jobs.rs:17 +no_edges: + - from: src/jobs.rs//worker//go + to: src/jobs.rs//util//wipe + - from: src/jobs.rs//flush + to: src/jobs.rs//util//wipe +unresolved: [] +results: + - target: std//fs//remove_dir_all + status: found + path: + - key: src/main.rs//main + - key: src/jobs.rs//worker//go + site: src/main.rs:5 + kind: direct + confidence: high + - key: src/store/util.rs//wipe + site: src/jobs.rs:11 + kind: direct + confidence: high + - key: std//fs//remove_dir_all + site: src/store/util.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/store/util.rs//wipe + site: src/store/util.rs:2 + reached: true + - target: std//fs//remove_file + status: not-found + call_sites: + - caller: src/jobs.rs//util//wipe + site: src/jobs.rs:3 + reached: false diff --git a/test/projectgraph/cases/rust/use-shadows-module/project/Cargo.toml b/test/projectgraph/cases/rust/use-shadows-module/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadows-module/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/use-shadows-module/project/src/jobs.rs b/test/projectgraph/cases/rust/use-shadows-module/project/src/jobs.rs new file mode 100644 index 00000000..b11ccf9e --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadows-module/project/src/jobs.rs @@ -0,0 +1,18 @@ +mod util { + pub fn wipe(p: &str) { + let _ = std::fs::remove_file(p); + } +} + +pub mod worker { + use crate::store::util; + + pub fn go() { + util::wipe("x"); + } +} + +pub fn flush() { + use crate::store::util; + util::wipe("y"); +} diff --git a/test/projectgraph/cases/rust/use-shadows-module/project/src/main.rs b/test/projectgraph/cases/rust/use-shadows-module/project/src/main.rs new file mode 100644 index 00000000..a8b5cea2 --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadows-module/project/src/main.rs @@ -0,0 +1,7 @@ +mod jobs; +mod store; + +fn main() { + jobs::worker::go(); + jobs::flush(); +} diff --git a/test/projectgraph/cases/rust/use-shadows-module/project/src/store/mod.rs b/test/projectgraph/cases/rust/use-shadows-module/project/src/store/mod.rs new file mode 100644 index 00000000..812d1edf --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadows-module/project/src/store/mod.rs @@ -0,0 +1 @@ +pub mod util; diff --git a/test/projectgraph/cases/rust/use-shadows-module/project/src/store/util.rs b/test/projectgraph/cases/rust/use-shadows-module/project/src/store/util.rs new file mode 100644 index 00000000..c1f927fd --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadows-module/project/src/store/util.rs @@ -0,0 +1,3 @@ +pub fn wipe(p: &str) { + let _ = std::fs::remove_dir_all(p); +} From 91e5c8ae08721b3a3fff829ae5567b5998ee0601 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:00:36 +0000 Subject: [PATCH 15/86] fix(projectgraph): resolve a Rust type in its inline module The link step resolved a type that a field, a result or a call path of an inline module names at the root of the file. A type of the inline module with the name of a type of the root then sent a method call to the root type, and the method of the real type read as not-found. The Rust profile now looks for a type of the name in the inline modules that hold the name first, as Java does for nested classes. The index also drops the type of a field or of a result when a scope declares its first name in another way, as a type parameter of the struct or of the impl. It keeps a type that a struct, an enum, a union or a trait of the inline module declares, which the link step now reads. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 22 +++--- plugin/indexer/rust.go | 61 ++++++++++---- projectgraph/profile.go | 4 +- projectgraph/rust.go | 1 + .../cases/rust/generic-field-type/expect.yaml | 79 +++++++++++++++++++ .../generic-field-type/project/Cargo.toml | 4 + .../generic-field-type/project/src/main.rs | 51 ++++++++++++ .../rust/inline-module-types/expect.yaml | 69 ++++++++++++++++ .../inline-module-types/project/Cargo.toml | 4 + .../inline-module-types/project/src/main.rs | 5 ++ .../inline-module-types/project/src/store.rs | 35 ++++++++ 11 files changed, 310 insertions(+), 25 deletions(-) create mode 100644 test/projectgraph/cases/rust/generic-field-type/expect.yaml create mode 100644 test/projectgraph/cases/rust/generic-field-type/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/generic-field-type/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/inline-module-types/expect.yaml create mode 100644 test/projectgraph/cases/rust/inline-module-types/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/inline-module-types/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/inline-module-types/project/src/store.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 701bc2f5..d792ae8b 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -404,21 +404,25 @@ Rust follows the same rules, with these additions. the value of its first `let`, so a call after a second `let` of the name is a record too, as in the case `rust/shadowed-let-type`. A method of the real type then reads as `uncertain` and not as `not-found`. -- The link step resolves the declared type of a parameter or a `let` at - the root of the file. So the index keeps a declared type only when no - scope that holds the binder declares the first name of the type. Such a - scope is a block or an inline module with an item, a `use` or an - `extern crate` of the name, or a function, an `impl` or a trait with a - type parameter of the name. A glob `use` can declare any name, except +- The link step resolves a type that a parameter, a `let`, a field or the + result of a function declares at the root of the file, or as a struct, + an enum, a union or a trait of an inline module that holds it. A name of + a call path in an inline module also names such a type of the module + first. So the index keeps a declared type only when no scope that holds + it declares the first name of the type in another way. Such a scope is a + block or an inline module with an item, a `use` or an `extern crate` of + the name, or a function, an `impl`, a trait or a type with a type + parameter of the name. A glob `use` can declare any name, except `use super::*` in a module of the root, which reads the names of the root. `self` and `super` in an inline module name other modules. So in `fn run() { type Local = dep::Thing; let t: Local = dep::new_thing(); t.inspect(); }` with a `struct Local` at the root, `t.inspect()` takes no method of the root `Local`. The call keeps the target that the call graph gives, or a record with the reason `unknown-receiver`. A type parameter with a bound, - as `S` in `fn run(s: S)`, names its bound. The type of a field - and the result type of a function do not get this check, so a type that - an inline module declares for them still resolves at the root. + as `S` in `fn run(s: S)`, names its bound. The case + `rust/inline-module-types` shows the types of an inline module, and the + case `rust/generic-field-type` a type parameter of a struct and of an + `impl`. - `extern crate x;`, `extern crate x as y;`, `use x;` and `pub use x as y;` of a whole crate bind `x` or `y` in their module to the crate. A path through the binding is a path of the crate, so diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index a21f797c..b7560c20 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -182,7 +182,7 @@ func rustFields(p *projection, item *sitter.Node) []fileindex.Typed { if field.Type() != "field_declaration" { continue } - if typ := p.rustTypeName(field.ChildByFieldName("type"), nil); typ != "" { + if typ := p.rustScopedType(field, p.rustTypeName(field.ChildByFieldName("type"), nil)); typ != "" { fields = append(fields, fileindex.Typed{Name: p.text(field.ChildByFieldName("name")), Type: typ}) } } @@ -307,7 +307,10 @@ func rustSignature(p *projection, def *fileindex.Definition, node *sitter.Node) def.Params = append(def.Params, fileindex.Typed{Name: p.text(name), Type: typ}) } } - def.Returns, def.Wraps = p.rustResult(node.ChildByFieldName("return_type"), bounds) + result := node.ChildByFieldName("return_type") + if def.Returns, def.Wraps = p.rustResult(result, bounds); def.Returns != "" && p.rustScopedType(result, def.Returns) == "" { + def.Returns, def.Wraps = "", "" + } def.Locals = p.rustLocals(node.ChildByFieldName("body"), bounds) if impl := rustImpl(node); impl != nil && !def.Static && !p.rustDefinesType(impl) { def.Params = append([]fileindex.Typed{{Name: extensionParam, Type: p.rustItemName(impl)}}, def.Params...) @@ -352,16 +355,22 @@ func (p *projection) rustLocals(body *sitter.Node, bounds map[string]string) []f // rustDeclaredType returns the type that a let or a parameter declares, or // "" when a scope that holds the binder declares the first name of the -// type. The link step resolves a declared type at the root of the file. So -// Local in fn run() { type Local = dep::Thing; let t: Local = ... } -// resolved to a struct Local of the root. A call on t then took the method -// of that struct with no record, and a method of dep::Thing read as -// not-found. Without the type, the call keeps the target of the call graph -// or its record. +// type. func (p *projection) rustDeclaredType(binder *sitter.Node, bounds map[string]string) string { - typ := p.rustTypeName(binder.ChildByFieldName("type"), bounds) + return p.rustScopedType(binder, p.rustTypeName(binder.ChildByFieldName("type"), bounds)) +} + +// rustScopedType returns a type that a node names, or "" when a scope that +// holds the node declares the first name of the type. The link step +// resolves a type at the root of the file, or as a type of an inline module +// that holds the node. So Local in fn run() { type Local = dep::Thing; +// let t: Local = ... } resolved to a struct Local of the root. A call on t +// then took the method of that struct with no record, and a method of +// dep::Thing read as not-found. Without the type, the call keeps the target +// of the call graph or its record. +func (p *projection) rustScopedType(node *sitter.Node, typ string) string { first, _, _ := strings.Cut(typ, "::") - if typ == "" || p.rustScopeDeclares(binder, first) { + if typ == "" || p.rustScopeDeclares(node, first) { return "" } return typ @@ -370,9 +379,12 @@ func (p *projection) rustDeclaredType(binder *sitter.Node, bounds map[string]str // rustScopeDeclares reports whether a scope between a node and the root of // the file can give a name another meaning than the root gives it: an item // or a use of a block or of an inline module, or a type parameter of a -// function, an impl or a trait. A glob use can declare any name, except -// use super::* in a module of the root, which reads the names of the root. -// In an inline module, self and super name other modules than the root. +// function, an impl, a trait or a type. A glob use can declare any name, +// except use super::* in a module of the root, which reads the names of the +// root. In an inline module, self and super name other modules than the +// root. A struct, an enum, a union or a trait of the inline module that +// declares the name first does not count, because the link step looks for +// a type of the name in the inline modules that hold the node. func (p *projection) rustScopeDeclares(node *sitter.Node, name string) bool { for scope := node.Parent(); scope != nil; scope = scope.Parent() { switch scope.Type() { @@ -386,10 +398,16 @@ func (p *projection) rustScopeDeclares(node *sitter.Node, name string) bool { continue } ofRoot := mod.Parent() != nil && mod.Parent().Type() == "source_file" - if name == "self" || name == "super" || p.rustItemsDeclare(scope, name, ofRoot) { + if name == "self" || name == "super" { return true } - case "function_item", "impl_item", "trait_item": + if p.rustItemsDefineType(scope, name) { + return false + } + if p.rustItemsDeclare(scope, name, ofRoot) { + return true + } + case "function_item", "impl_item", "trait_item", "struct_item", "enum_item", "union_item": if slices.ContainsFunc(ts.NamedChildren(scope.ChildByFieldName("type_parameters")), func(param *sitter.Node) bool { return p.rustTypeParameter(param) == name }) { @@ -400,6 +418,19 @@ func (p *projection) rustScopeDeclares(node *sitter.Node, name string) bool { return false } +// rustItemsDefineType reports whether the items of a module define a +// struct, an enum, a union or a trait of the name. The index gives each one +// a key under the module. +func (p *projection) rustItemsDefineType(list *sitter.Node, name string) bool { + return slices.ContainsFunc(ts.NamedChildren(list), func(item *sitter.Node) bool { + switch item.Type() { + case "struct_item", "enum_item", "union_item", "trait_item": + return p.text(item.ChildByFieldName("name")) == name + } + return false + }) +} + // rustItemsDeclare reports whether the items of a block or of a module // declare a type, a trait, a module or a use of the name. ofRoot tells // that the module is a module of the root, whose use super::* reads the diff --git a/projectgraph/profile.go b/projectgraph/profile.go index af2a7e82..5473158a 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -144,7 +144,9 @@ type profile struct { // class with no instance keyword, as Java. implicitFields bool // nestedTypes marks a language where a class names the classes nested - // in the classes that hold it with no qualifier, as Java. + // in the classes that hold it with no qualifier, as Java. The index of + // Rust gives an inline module the kind class, and a name in the module + // names a type of the module before a type of the file. nestedTypes bool // implicitInterfaces marks a language where a type implements an // interface with no declaration, as Go. Only an interface type can diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 786d07f8..b41a3b2b 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -29,6 +29,7 @@ var rustProfile = profile{ symbol: rustSymbol, surface: rustSurface, unitTypes: true, + nestedTypes: true, receiverMethods: true, sameTypeMethods: map[string]bool{"clone": true, "to_owned": true, "default": true, "from": true}, abstractReceiver: fileindex.UnresolvedTraitReceiver, diff --git a/test/projectgraph/cases/rust/generic-field-type/expect.yaml b/test/projectgraph/cases/rust/generic-field-type/expect.yaml new file mode 100644 index 00000000..dc02bf2a --- /dev/null +++ b/test/projectgraph/cases/rust/generic-field-type/expect.yaml @@ -0,0 +1,79 @@ +source: safedep:hand +description: >- + The file declares the struct T, and the type parameter T of Holder and of impl Wrap shadows + it. So the field item: T of Holder and the result &T of Wrap::get hold the type that the caller gives: Disk + in by_field and Cloud in by_result. Disk::save calls std::fs::remove_file and Cloud::save calls + std::fs::create_dir. An index that keeps T as the type of the field and of the result makes the link step + read the struct T, so both calls go to T::save with no record, and both targets read not-found. The index + keeps no such type, so each call is a record. The record of h.item.save() gets the low name edges of save. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//by_field + to: src/main.rs//Disk//save + kind: name + confidence: low + site: src/main.rs:41 + - from: src/main.rs//by_result + to: src/main.rs//Wrap//get + kind: member + confidence: high + site: src/main.rs:45 +no_edges: + - from: src/main.rs//by_result + to: src/main.rs//T//save +unresolved: + - caller: src/main.rs//by_field + site: src/main.rs:41 + reason: no-definition + method: save + - caller: src/main.rs//by_result + site: src/main.rs:45 + reason: unknown-receiver + method: save +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//by_field + site: src/main.rs:49 + kind: direct + confidence: high + - key: src/main.rs//Disk//save + site: src/main.rs:41 + kind: name + confidence: low + - key: std//fs//remove_file + site: src/main.rs:18 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//Disk//save + site: src/main.rs:18 + reached: true + - target: std//fs//create_dir + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//by_field + site: src/main.rs:49 + kind: direct + confidence: high + - key: src/main.rs//Cloud//save + site: src/main.rs:41 + kind: name + confidence: low + - key: std//fs//create_dir + site: src/main.rs:24 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//Cloud//save + site: src/main.rs:24 + reached: true diff --git a/test/projectgraph/cases/rust/generic-field-type/project/Cargo.toml b/test/projectgraph/cases/rust/generic-field-type/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/generic-field-type/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/generic-field-type/project/src/main.rs b/test/projectgraph/cases/rust/generic-field-type/project/src/main.rs new file mode 100644 index 00000000..78573bc1 --- /dev/null +++ b/test/projectgraph/cases/rust/generic-field-type/project/src/main.rs @@ -0,0 +1,51 @@ +pub struct T; + +impl T { + pub fn save(&self) { + let _ = std::fs::write("t", "x"); + } +} + +pub trait Save { + fn save(&self); +} + +pub struct Disk; +pub struct Cloud; + +impl Save for Disk { + fn save(&self) { + let _ = std::fs::remove_file("disk"); + } +} + +impl Save for Cloud { + fn save(&self) { + let _ = std::fs::create_dir("cloud"); + } +} + +pub struct Holder { + pub item: T, +} + +pub struct Wrap(T); + +impl Wrap { + pub fn get(&self) -> &T { + &self.0 + } +} + +fn by_field(h: Holder) { + h.item.save(); +} + +fn by_result(w: Wrap) { + w.get().save(); +} + +fn main() { + by_field(Holder { item: Disk }); + by_result(Wrap(Cloud)); +} diff --git a/test/projectgraph/cases/rust/inline-module-types/expect.yaml b/test/projectgraph/cases/rust/inline-module-types/expect.yaml new file mode 100644 index 00000000..0ab581f2 --- /dev/null +++ b/test/projectgraph/cases/rust/inline-module-types/expect.yaml @@ -0,0 +1,69 @@ +source: safedep:hand +description: >- + src/store.rs declares the structs Holder and Item at its top level, and the inline module cache declares its + own Holder and Item. In cache, the field item of Holder, the result of make and the struct expression + Holder { .. } name the types of cache. So h.item.save() and make().save() run cache::Item::save, which calls + std::fs::create_dir. The save of the top level calls std::fs::write and nothing calls it. A link step that + resolves these names at the root of the file sends both calls to the save of the top level, so + std::fs::create_dir reads not-found and std::fs::write reads found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/store.rs//cache//go + to: src/store.rs//cache//Holder + kind: direct + confidence: high + site: src/store.rs:31 + - from: src/store.rs//cache//go + to: src/store.rs//cache//Item//save + kind: member + confidence: high + site: src/store.rs:32 + - from: src/store.rs//cache//go + to: src/store.rs//cache//make + kind: direct + confidence: high + site: src/store.rs:33 + - from: src/store.rs//cache//go + to: src/store.rs//cache//Item//save + kind: member + confidence: high + site: src/store.rs:33 +no_edges: + - from: src/store.rs//cache//go + to: src/store.rs//Item//save + - from: src/store.rs//cache//go + to: src/store.rs//Holder +unresolved: [] +results: + - target: std//fs//create_dir + status: found + path: + - key: src/main.rs//main + - key: src/store.rs//cache//go + site: src/main.rs:4 + kind: direct + confidence: high + - key: src/store.rs//cache//Item//save + site: src/store.rs:32 + kind: member + confidence: high + - key: std//fs//create_dir + site: src/store.rs:18 + kind: direct + confidence: high + call_sites: + - caller: src/store.rs//cache//Item//save + site: src/store.rs:18 + reached: true + - target: std//fs//write + status: not-found + call_sites: + - caller: src/store.rs//Item//save + site: src/store.rs:5 + reached: false diff --git a/test/projectgraph/cases/rust/inline-module-types/project/Cargo.toml b/test/projectgraph/cases/rust/inline-module-types/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/inline-module-types/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/inline-module-types/project/src/main.rs b/test/projectgraph/cases/rust/inline-module-types/project/src/main.rs new file mode 100644 index 00000000..fea7b954 --- /dev/null +++ b/test/projectgraph/cases/rust/inline-module-types/project/src/main.rs @@ -0,0 +1,5 @@ +mod store; + +fn main() { + store::cache::go(); +} diff --git a/test/projectgraph/cases/rust/inline-module-types/project/src/store.rs b/test/projectgraph/cases/rust/inline-module-types/project/src/store.rs new file mode 100644 index 00000000..8f26e808 --- /dev/null +++ b/test/projectgraph/cases/rust/inline-module-types/project/src/store.rs @@ -0,0 +1,35 @@ +pub struct Item; + +impl Item { + pub fn save(&self) { + let _ = std::fs::write("top", "x"); + } +} + +pub struct Holder { + pub item: Item, +} + +pub mod cache { + pub struct Item; + + impl Item { + pub fn save(&self) { + let _ = std::fs::create_dir("cache"); + } + } + + pub struct Holder { + pub item: Item, + } + + pub fn make() -> Item { + Item + } + + pub fn go() { + let h = Holder { item: Item }; + h.item.save(); + make().save(); + } +} From b7ca05f1d810b3f40bed61ef563156627413e707 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:08:06 +0000 Subject: [PATCH 16/86] fix(projectgraph): resolve a Rust glob use and an exported macro of a unit A glob use of a module of another crate brought no name. So html after use dep::* read as a crate html, the call got an external key with no record, and dep::html::render read as not-found. render after use dep::html::* stayed a record. A path now reads a name that a glob use of a crate brings when a unit gives the crate and the name. #[macro_export] puts a macro at the root of its crate from any module. The provision of the unit now finds such a macro, and a call of a single name that no item binds gets the edge to an exported macro of a dependency. A single name can name a closure or a parameter, so such a call keeps its record. A macro now reads $crate::sink as crate::sink. A path of a unit that names a module, as dep::html, gets no external node, because the walk of its provision ends on a module and recorded the whole unit. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 17 ++ plugin/callgraph/rust.go | 12 +- projectgraph/rust.go | 151 ++++++++++++++++-- .../glob-use-dependency-module/expect.yaml | 43 +++++ .../project/Cargo.toml | 7 + .../project/src/main.rs | 5 + .../project/vendor/dep/Cargo.toml | 4 + .../project/vendor/dep/src/html.rs | 3 + .../project/vendor/dep/src/lib.rs | 1 + .../summaries/cargo_dep_1.2.0.json | 23 +++ .../cases/rust/glob-use/expect.yaml | 30 +++- .../rust/macro-export-module/expect.yaml | 57 +++++++ .../macro-export-module/project/Cargo.toml | 7 + .../macro-export-module/project/src/main.rs | 5 + .../project/vendor/dep/Cargo.toml | 4 + .../project/vendor/dep/src/lib.rs | 5 + .../project/vendor/dep/src/macros.rs | 6 + .../summaries/cargo_dep_1.2.0.json | 23 +++ .../cases/rust/macro-import/expect.yaml | 36 ++++- 19 files changed, 412 insertions(+), 27 deletions(-) create mode 100644 test/projectgraph/cases/rust/glob-use-dependency-module/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-dependency-module/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-dependency-module/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/src/html.rs create mode 100644 test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-dependency-module/summaries/cargo_dep_1.2.0.json create mode 100644 test/projectgraph/cases/rust/macro-export-module/expect.yaml create mode 100644 test/projectgraph/cases/rust/macro-export-module/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-export-module/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/src/macros.rs create mode 100644 test/projectgraph/cases/rust/macro-export-module/summaries/cargo_dep_1.2.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index d792ae8b..8c6c5266 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -472,6 +472,23 @@ Rust follows the same rules, with these additions. path of a crate with the name of that sibling would lose its edge to the crate, as the case `rust/glob-use-inline-module` shows. The path of a glob `use` reads no other glob `use`. +- A glob `use` of a module of another crate, as `use dep::html::*;`, + brings a name when a unit gives the crate and the name in that module. + So `render()` names `dep//html//render`, as in the case `rust/glob-use`, + and `html::render()` after `use dep::*;` names it too, and not a crate + `html`, as in the case `rust/glob-use-dependency-module`. A glob `use` + of a crate that no unit gives can bring any name, so the path resolves + as if the glob `use` were not there. A local closure or a parameter can + bind a single name, so such a call keeps its record too. +- `#[macro_export]` puts a macro at the root of its crate, from any module + of the crate. So `dep::wipe` names the macro `wipe` of a private module + of the unit, as in the case `rust/macro-export-module`. A call of a + single name that no item binds, as `render_now!()` after + `#[macro_use] extern crate dep;`, gets the edge to such a macro of a + dependency of the crate, as in the case `rust/macro-import`. The index + does not tell a call of a macro from a call of a function value, so the + call keeps its record. A macro reads `$crate::sink` as `crate::sink` of + the crate of the macro. - The index records a type alias, as `pub type Image

= ImageBuffer

;`, as a definition of the kind `alias`. The link step resolves the type that it names, as the file of the alias names it, when a walk first diff --git a/plugin/callgraph/rust.go b/plugin/callgraph/rust.go index 38e50242..c2dfb3f5 100644 --- a/plugin/callgraph/rust.go +++ b/plugin/callgraph/rust.go @@ -91,7 +91,7 @@ func rustTokenTreeProcessor(node *sitter.Node, treeData []byte, currentNamespace continue } start := i - 1 - for start >= 2 && tokens[start-1].Type() == "::" && tokens[start-2].Type() == "identifier" { + for start >= 2 && tokens[start-1].Type() == "::" && rustPathToken(tokens[start-2], treeData) { start -= 2 } name := tokens[i-1] @@ -104,7 +104,7 @@ func rustTokenTreeProcessor(node *sitter.Node, treeData []byte, currentNamespace } var path strings.Builder for _, t := range tokens[start:i] { - path.WriteString(t.Content(treeData)) + path.WriteString(strings.TrimPrefix(t.Content(treeData), "$")) } kind := CallCall if creation { @@ -115,3 +115,11 @@ func rustTokenTreeProcessor(node *sitter.Node, treeData []byte, currentNamespace } return newProcessorResult() } + +// rustPathToken reports whether a token of a macro can start or continue a +// path: a name, or $crate, which names the crate of the macro as crate does. +// Without $crate, $crate::sink() of a macro read as a call of the name sink, +// which the file of the macro does not bind. +func rustPathToken(token *sitter.Node, treeData []byte) bool { + return token.Type() == "identifier" || token.Type() == "metavariable" && token.Content(treeData) == "$crate" +} diff --git a/projectgraph/rust.go b/projectgraph/rust.go index b41a3b2b..88d842af 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -145,8 +145,8 @@ var rustRoots = map[string]bool{"std": true, "core": true, "alloc": true} // rustState is the state of Rust in a build: the crates of each space, the // module path of each Rust file, the paths that a resolution or a crate -// route reads now, by the file that writes them, and the module paths of -// the glob uses of each file. +// route reads now, by the file that writes them, the module paths of the +// glob uses of each file, and the exported macros of each unit by name. type rustState struct { cargo map[*space]*modules.Cargo modules map[string]string @@ -154,6 +154,7 @@ type rustState struct { routing map[[2]string]bool routes map[[2]string]crateRoute globs map[string][]string + macros map[*space]map[string]string } // crateRoute is the path in another crate that a path of a file names, and @@ -362,9 +363,47 @@ func rustProvide(b *builder, sp *space, module string, _ modules.Form) []provisi if key, ok := b.resolveQualified(langcode.Rust, joinPath(sp.unit, parts[1:])); ok { return []provision{{sp: sp, module: key, via: "crate"}} } + if key, ok := b.rustExportedMacro(sp, parts[1:]); ok { + return []provision{{sp: sp, module: key, via: "crate"}} + } return []provision{{sp: sp}} } +// rustExportedMacro returns the macro of a unit that #[macro_export] puts +// at the root of its crate, as from_sub of src/macros.rs for +// dep::from_sub. A walk of the modules of the crate does not find it. +func (b *builder) rustExportedMacro(sp *space, names []string) (string, bool) { + if len(names) != 1 { + return "", false + } + s := b.rustState() + if s.macros == nil { + s.macros = map[*space]map[string]string{} + } + macros, ok := s.macros[sp] + if !ok { + macros = map[string]string{} + for _, p := range b.paths { + ix := b.byPath[p] + if ix == nil || ix.Language != langcode.Rust || fileindex.PackageOf(p) != sp.unit { + continue + } + for _, def := range ix.Definitions { + if _, taken := macros[def.Name]; !taken && slices.Contains(def.Decorators, rustMacroExport) { + macros[def.Name] = def.Key + } + } + } + s.macros[sp] = macros + } + key, ok := macros[names[0]] + return key, ok +} + +// rustMacroExport is the attribute that puts a macro at the root of its +// crate. +const rustMacroExport = "@macro_export" + // rustImportedModule returns the path of the package that a path of a file // names. A path writes the name that the Cargo.toml of its crate gives a // dependency, and the crate of the unit has the name of the package. @@ -483,8 +522,22 @@ func (r rustResolver) Module(importer, spec string) (string, string, bool) { // A use can bind an item of the crate, as fl of use crate::ffi::flush // as fl after pub use dep as ffi. A call of fl links the key itself, // so the key needs its node. Without it the edge reached no node, and - // the target read as not-found. - return r.b.external(r.b.rustCrateKey(t.file, t.path)), "crate", true + // the target read as not-found. A module of a unit, as html of + // dep::html, runs no code, and its node would get a record of the unit + // for a walk that ends on a module. + key := r.b.rustCrateKey(t.file, t.path) + if r.b.rustUnitModule(t.file, t.path) { + return key, "crate", true + } + return r.b.external(key), "crate", true +} + +// rustUnitModule reports whether a path of another crate that a file names +// is a module of a unit. +func (b *builder) rustUnitModule(file, path string) bool { + ix := b.byPath[file] + o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, path), form: modules.FormImport} + return slices.ContainsFunc(b.providers(o), func(pv provision) bool { return b.units[pv.module] != nil }) } // rustTarget is what a path of a file names: a definition or a module of @@ -503,6 +556,9 @@ func (b *builder) rustPath(importer, written string) (rustTarget, bool) { if path, file, ok := b.rustCratePath(importer, written); ok { return rustTarget{path: path, file: file}, true } + if path, file, ok := b.rustGlobPath(importer, written); ok { + return rustTarget{path: path, file: file}, true + } return rustTarget{}, false } @@ -701,22 +757,93 @@ func (r rustResolver) FirstPartyFrom(importer, spec string) bool { // child module, or else a path of another crate, as serde_json::to_string, // whose external key the edge gets. The crate is the origin of the key, so a // unit of dependency code that gives the crate links it. A single name stays a record, because -// a local closure can bind it. +// a local closure or a parameter can bind it. A single name that a glob use +// of another crate or an exported macro of a dependency gives also gets the +// edge to that item. func (b *builder) rustScopeFallback(sc *scope, _, first string, rest []string, target string) []resolution { written := strings.Join(append([]string{first}, rest...), "::") - if t, ok := b.rustPath(sc.ix.Path, written); ok { - if t.key != "" { - return []resolution{b.walkNamespace(t.key)} - } + record := resolution{fail: unknownReceiver(first)} + t, ok := b.rustPath(sc.ix.Path, written) + switch { + case ok && t.key != "": + return []resolution{b.walkNamespace(t.key)} + case ok: crate, names := rustCrateNames(t.path) - return []resolution{b.importedWalk(b.byPath[t.file], fileindex.Namespace(langcode.Rust, t.path), crate, modules.FormImport, names)} - } - if len(rest) > 0 { + r := b.importedWalk(b.byPath[t.file], fileindex.Namespace(langcode.Rust, t.path), crate, modules.FormImport, names) + if len(rest) > 0 { + return []resolution{r} + } + return []resolution{r, record} + case len(rest) > 0: return []resolution{b.importedWalk(sc.ix, target, first, modules.FormImport, rest)} } + if crate, ok := b.rustMacroCrate(sc.ix, first); ok { + return []resolution{b.importedWalk(sc.ix, fileindex.Join(crate, first), crate, modules.FormImport, []string{first}), record} + } return nil } +// rustMacroCrate returns a dependency of the crate of a file whose unit +// exports a macro of the name with #[macro_export], as dep for +// render_now!() after #[macro_use] extern crate dep. The index does not +// tell a call of a macro from a call of a function, so the caller keeps +// the record of the name too. +func (b *builder) rustMacroCrate(ix *fileindex.Index, name string) (string, bool) { + if !b.hasUnits() { + return "", false + } + for _, dep := range b.cargoOf(b.spaceOf(ix.Path)).Dependencies() { + crate := strings.ReplaceAll(dep, "-", "_") + o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, crate+"::"+name), form: modules.FormImport} + if slices.ContainsFunc(b.providers(o), func(pv provision) bool { + def := b.defs[pv.module] + return def != nil && slices.Contains(def.Decorators, rustMacroExport) + }) { + return crate, true + } + } + return "", false +} + +// rustGlobPath returns the path in another crate that a path of a file +// names through a glob use of a module of that crate, as dep::html::render +// for render after use dep::html::*, when a unit gives the crate and the +// first name of the path in the module. Rust reads a name of a glob use +// before a crate, and rejects a name that two glob uses bring. A glob use +// of a crate that no unit gives can bring any name, and the path then +// resolves as before. A glob use of an inline module stays out, as +// rustGlobModules says. +func (b *builder) rustGlobPath(importer, written string) (string, string, bool) { + sc := b.scopes[importer] + first := rustFirstName(written) + if sc == nil || len(sc.wildcards) == 0 || !b.hasUnits() || strings.HasPrefix(written, "::") { + return "", "", false + } + switch first { + case "crate", "self", "super": + return "", "", false + } + for _, imp := range sc.wildcards { + if def := b.defs[sc.enclosing(imp.Line)]; def != nil && def.Kind == fileindex.DefClass { + continue + } + path, file, ok := b.rustCratePath(importer, imp.Module) + switch { + case ok: + case b.rustLocalRoot(importer, imp.Module): + continue + default: + path, file = imp.Module, importer + } + ix := b.byPath[file] + o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, joinPath(path, []string{first})), form: modules.FormImport} + if slices.ContainsFunc(b.providers(o), func(pv provision) bool { return pv.module != "" }) { + return joinPath(path, strings.Split(written, "::")), file, true + } + } + return "", "", false +} + // joinImplBlocks makes an impl block of a type of another module one class // with the type, as impl Store for FileStore after use crate::FileStore. func (b *builder) joinImplBlocks() { diff --git a/test/projectgraph/cases/rust/glob-use-dependency-module/expect.yaml b/test/projectgraph/cases/rust/glob-use-dependency-module/expect.yaml new file mode 100644 index 00000000..3d9dfb1b --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-dependency-module/expect.yaml @@ -0,0 +1,43 @@ +source: safedep:hand +description: >- + main.rs has use dep::*, which brings the module html of the crate dep into scope, and main calls html::render. + The code of the crate dep is the unit cargo:dep@1.2.0. Rust reads a name of a glob use before a crate, so html + names dep::html and no crate html. A link step that reads html as a crate gives the call the key html//render + with no record, and dep//html//render reads not-found. Nothing calls escape, so dep//html//escape is not-found. +units: + vendor/dep: cargo:dep@1.2.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: src/main.rs//main + to: dep//html//render + kind: direct + confidence: high + site: src/main.rs:4 + - from: dep//html//render + to: cargo:dep@1.2.0//src/html.rs//render + kind: package + confidence: high + site: cargo:dep@1.2.0//src/html.rs:1 +unresolved: [] +results: + - target: dep//html//render + status: found + path: + - key: src/main.rs//main + - key: dep//html//render + site: src/main.rs:4 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//main + site: src/main.rs:4 + reached: true + - target: dep//html//escape + status: not-found diff --git a/test/projectgraph/cases/rust/glob-use-dependency-module/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-dependency-module/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-dependency-module/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.2.0" diff --git a/test/projectgraph/cases/rust/glob-use-dependency-module/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-dependency-module/project/src/main.rs new file mode 100644 index 00000000..8cca6efc --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-dependency-module/project/src/main.rs @@ -0,0 +1,5 @@ +use dep::*; + +fn main() { + html::render(); +} diff --git a/test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.2.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/src/html.rs b/test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/src/html.rs new file mode 100644 index 00000000..38c45e42 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/src/html.rs @@ -0,0 +1,3 @@ +pub fn render() {} + +pub fn escape() {} diff --git a/test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..3d4613dd --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-dependency-module/project/vendor/dep/src/lib.rs @@ -0,0 +1 @@ +pub mod html; diff --git a/test/projectgraph/cases/rust/glob-use-dependency-module/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/glob-use-dependency-module/summaries/cargo_dep_1.2.0.json new file mode 100644 index 00000000..9e9f91b1 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-dependency-module/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,23 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "19", + "unit": "cargo:dep@1.2.0", + "symbols": [ + { + "key": "dep//html//escape", + "definitions": [ + "cargo:dep@1.2.0//src/html.rs//escape" + ], + "reaches": [] + }, + { + "key": "dep//html//render", + "definitions": [ + "cargo:dep@1.2.0//src/html.rs//render" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/glob-use/expect.yaml b/test/projectgraph/cases/rust/glob-use/expect.yaml index 843d7711..116d5afd 100644 --- a/test/projectgraph/cases/rust/glob-use/expect.yaml +++ b/test/projectgraph/cases/rust/glob-use/expect.yaml @@ -1,9 +1,9 @@ source: safedep:hand description: >- The case of the vet reach corpus rust/glob-use. main brings in every item of the module dep::html with a glob use - and calls render. The code of the crate dep is the unit cargo:dep@1.2.0. The link step does not resolve a name - that a glob use of a module of a unit brings in, so the call stays a record of the method render. The record makes - render uncertain, and not not-found. No record names escape, so escape is not-found. + and calls render. The code of the crate dep is the unit cargo:dep@1.2.0, which gives render in that module, so the + call gets the edge to dep//html//render and its package edge. A local closure or a parameter can also bind a + single name, so the call keeps its record of the method render. Nothing calls escape, so escape is not-found. units: vendor/dep: cargo:dep@1.2.0 roots: @@ -14,6 +14,17 @@ roots: kind: main rule: rust.main exact_edges: true +edges: + - from: src/main.rs//main + to: dep//html//render + kind: direct + confidence: high + site: src/main.rs:4 + - from: dep//html//render + to: cargo:dep@1.2.0//src/html.rs//render + kind: package + confidence: high + site: cargo:dep@1.2.0//src/html.rs:1 unresolved: - caller: src/main.rs//main site: src/main.rs:4 @@ -21,9 +32,16 @@ unresolved: method: render results: - target: dep//html//render - status: uncertain - uncertainties: - - kind: unresolved-call + status: found + path: + - key: src/main.rs//main + - key: dep//html//render + site: src/main.rs:4 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//main site: src/main.rs:4 + reached: true - target: dep//html//escape status: not-found diff --git a/test/projectgraph/cases/rust/macro-export-module/expect.yaml b/test/projectgraph/cases/rust/macro-export-module/expect.yaml new file mode 100644 index 00000000..44e300b0 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-export-module/expect.yaml @@ -0,0 +1,57 @@ +source: safedep:hand +description: >- + The crate dep, the unit cargo:dep@1.2.0, defines the macro wipe in its private module macros with + #[macro_export], which puts the macro at the root of the crate. Its rule calls $crate::sink, the function sink + of the root of dep. main.rs has use dep::wipe and calls wipe!(). A link step that looks for wipe in the root + module of dep finds no definition and records the unit, so every target of dep reads uncertain. A macro that + reads $crate::sink as the name sink records the call too. +units: + vendor/dep: cargo:dep@1.2.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: src/main.rs//main + to: dep//wipe + kind: direct + confidence: high + site: src/main.rs:4 + - from: dep//wipe + to: cargo:dep@1.2.0//src/macros.rs//wipe + kind: package + confidence: high + site: cargo:dep@1.2.0//src/macros.rs:2 + - from: cargo:dep@1.2.0//src/macros.rs//wipe + to: cargo:dep@1.2.0//src/lib.rs//sink + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/macros.rs:4 +unresolved: [] +results: + - target: dep//sink + status: found + path: + - key: src/main.rs//main + - key: dep//wipe + site: src/main.rs:4 + kind: direct + confidence: high + - key: cargo:dep@1.2.0//src/macros.rs//wipe + site: cargo:dep@1.2.0//src/macros.rs:2 + kind: package + confidence: high + - key: cargo:dep@1.2.0//src/lib.rs//sink + site: cargo:dep@1.2.0//src/macros.rs:4 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.2.0//src/macros.rs//wipe + site: cargo:dep@1.2.0//src/macros.rs:4 + reached: true + - target: dep//unused + status: not-found diff --git a/test/projectgraph/cases/rust/macro-export-module/project/Cargo.toml b/test/projectgraph/cases/rust/macro-export-module/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/macro-export-module/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.2.0" diff --git a/test/projectgraph/cases/rust/macro-export-module/project/src/main.rs b/test/projectgraph/cases/rust/macro-export-module/project/src/main.rs new file mode 100644 index 00000000..b54e6f1e --- /dev/null +++ b/test/projectgraph/cases/rust/macro-export-module/project/src/main.rs @@ -0,0 +1,5 @@ +use dep::wipe; + +fn main() { + wipe!(); +} diff --git a/test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.2.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..25c954c1 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/src/lib.rs @@ -0,0 +1,5 @@ +mod macros; + +pub fn sink() {} + +pub fn unused() {} diff --git a/test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/src/macros.rs b/test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/src/macros.rs new file mode 100644 index 00000000..1e08caab --- /dev/null +++ b/test/projectgraph/cases/rust/macro-export-module/project/vendor/dep/src/macros.rs @@ -0,0 +1,6 @@ +#[macro_export] +macro_rules! wipe { + () => { + $crate::sink() + }; +} diff --git a/test/projectgraph/cases/rust/macro-export-module/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/macro-export-module/summaries/cargo_dep_1.2.0.json new file mode 100644 index 00000000..f6470bbb --- /dev/null +++ b/test/projectgraph/cases/rust/macro-export-module/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,23 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "19", + "unit": "cargo:dep@1.2.0", + "symbols": [ + { + "key": "dep//sink", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//sink" + ], + "reaches": [] + }, + { + "key": "dep//unused", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//unused" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/macro-import/expect.yaml b/test/projectgraph/cases/rust/macro-import/expect.yaml index 97d9d859..85508a25 100644 --- a/test/projectgraph/cases/rust/macro-import/expect.yaml +++ b/test/projectgraph/cases/rust/macro-import/expect.yaml @@ -2,8 +2,9 @@ source: safedep:hand description: >- The case of the vet reach corpus rust/macro-import. main uses the macro render_now of the crate dep, which #[macro_use] extern crate dep brings in. The macro expands to a call of dep::render. The code of the crate dep is - the unit cargo:dep@1.2.0. The link step does not resolve a macro that #[macro_export] gives at the root of a crate - of a unit, so the call of render_now stays a record. The record makes render uncertain, and not not-found. + the unit cargo:dep@1.2.0, and #[macro_export] gives render_now at the root of the crate. So the call of + render_now gets the edge to dep//render_now, whose package edge leads to the macro and on to render. The index + does not tell a call of a macro from a call of a function value, so the call keeps its record too. units: vendor/dep: cargo:dep@1.2.0 roots: @@ -15,6 +16,16 @@ roots: rule: rust.main exact_edges: true edges: + - from: src/main.rs//main + to: dep//render_now + kind: direct + confidence: high + site: src/main.rs:5 + - from: dep//render_now + to: cargo:dep@1.2.0//src/lib.rs//render_now + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:4 - from: cargo:dep@1.2.0//src/lib.rs//render_now to: cargo:dep@1.2.0//src/lib.rs//render kind: direct @@ -27,11 +38,22 @@ unresolved: method: render_now results: - target: dep//render - status: uncertain + status: found + path: + - key: src/main.rs//main + - key: dep//render_now + site: src/main.rs:5 + kind: direct + confidence: high + - key: cargo:dep@1.2.0//src/lib.rs//render_now + site: cargo:dep@1.2.0//src/lib.rs:4 + kind: package + confidence: high + - key: cargo:dep@1.2.0//src/lib.rs//render + site: cargo:dep@1.2.0//src/lib.rs:6 + kind: direct + confidence: high call_sites: - caller: cargo:dep@1.2.0//src/lib.rs//render_now site: cargo:dep@1.2.0//src/lib.rs:6 - reached: false - uncertainties: - - kind: unresolved-call - site: src/main.rs:5 + reached: true From 9f4b761a422700c0a7b5b23782c1a968d79b1164 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:09:43 +0000 Subject: [PATCH 17/86] test(lang): cover each form of a Rust self or super path in an inline module rustFromFile now joins the parts with slices.Concat, which also clears a staticcheck finding. The table checks a path that goes up past the module of the file, which no case of projectgraph reaches. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- lang/rust_resolvers.go | 24 ++++++++---------------- lang/rust_resolvers_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 lang/rust_resolvers_test.go diff --git a/lang/rust_resolvers.go b/lang/rust_resolvers.go index 1c4f1b2a..e78816b2 100644 --- a/lang/rust_resolvers.go +++ b/lang/rust_resolvers.go @@ -118,26 +118,18 @@ func rustInlineModules(data *[]byte, node *sitter.Node) []string { func rustFromFile(path string, mods []string) string { parts := strings.Split(path, "::") ups := 0 + for ups < len(parts) && parts[ups] == "super" { + ups++ + } switch { case parts[0] == "self": - parts = parts[1:] - case parts[0] == "super": - for ups < len(parts) && parts[ups] == "super" { - ups++ - } - parts = parts[ups:] - default: + return strings.Join(slices.Concat([]string{"self"}, mods, parts[1:]), "::") + case ups == 0: return path + case ups > len(mods): + return strings.Join(slices.Concat(slices.Repeat([]string{"super"}, ups-len(mods)), parts[ups:]), "::") } - var out []string - if ups > len(mods) { - for range ups - len(mods) { - out = append(out, "super") - } - } else { - out = append(append(out, "self"), mods[:len(mods)-ups]...) - } - return strings.Join(append(out, parts...), "::") + return strings.Join(slices.Concat([]string{"self"}, mods[:len(mods)-ups], parts[ups:]), "::") } // rustUseImports returns the imports of a use tree. The prefix is the path of diff --git a/lang/rust_resolvers_test.go b/lang/rust_resolvers_test.go new file mode 100644 index 00000000..93d91741 --- /dev/null +++ b/lang/rust_resolvers_test.go @@ -0,0 +1,28 @@ +package lang + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRustFromFile(t *testing.T) { + cases := []struct { + path string + mods []string + want string + }{ + {"self::tls::conn", []string{"client"}, "self::client::tls::conn"}, + {"super::tls", []string{"client"}, "self::tls"}, + {"super::sub", []string{"outer", "inner"}, "self::outer::sub"}, + {"super::super::x", []string{"inner"}, "super::x"}, + {"super", []string{"tests"}, "self"}, + {"crate::store::disk", []string{"client"}, "crate::store::disk"}, + {"dep::html", []string{"client"}, "dep::html"}, + } + for _, c := range cases { + t.Run(c.path, func(t *testing.T) { + assert.Equal(t, c.want, rustFromFile(c.path, c.mods)) + }) + } +} From 12e12725c01283fd8926b93a5a738fa7c45c583d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:18:13 +0000 Subject: [PATCH 18/86] perf(projectgraph): skip the Rust lookups that no name of the graph needs The lookup of an exported macro for each single name that no item binds asked the provisions of each dependency, and the lookup of a nested type walked up the classes that hold each name. Together they made the link step of the Rust corpus a quarter slower. A table of the names of the exported macros and of the types of the inline modules now skips both for a name that no unit and no inline module defines. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/java.go | 2 +- projectgraph/rust.go | 66 ++++++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/projectgraph/java.go b/projectgraph/java.go index 3187aacf..8224e160 100644 --- a/projectgraph/java.go +++ b/projectgraph/java.go @@ -196,7 +196,7 @@ func (b *builder) selfName(class, caller, name string) string { // module of another file can hold it too, as Loader in module Shop for // Shop::Loader. func (b *builder) nestedType(sc *scope, from, name string) (string, bool) { - if !sc.profile.nestedTypes { + if !sc.profile.nestedTypes || sc.profile.code == langcode.Rust && !b.rustNestedName(name) { return "", false } for class, ok := b.enclosingClass(from); ok; class, ok = b.enclosingClass(fileindex.Parent(class)) { diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 88d842af..5b4943d3 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -146,7 +146,8 @@ var rustRoots = map[string]bool{"std": true, "core": true, "alloc": true} // rustState is the state of Rust in a build: the crates of each space, the // module path of each Rust file, the paths that a resolution or a crate // route reads now, by the file that writes them, the module paths of the -// glob uses of each file, and the exported macros of each unit by name. +// glob uses of each file, the exported macros of each unit by name, and +// the names of the types of the inline modules. type rustState struct { cargo map[*space]*modules.Cargo modules map[string]string @@ -155,6 +156,7 @@ type rustState struct { routes map[[2]string]crateRoute globs map[string][]string macros map[*space]map[string]string + nested map[string]bool } // crateRoute is the path in another crate that a path of a file names, and @@ -376,28 +378,37 @@ func (b *builder) rustExportedMacro(sp *space, names []string) (string, bool) { if len(names) != 1 { return "", false } + key, ok := b.rustMacros()[sp][names[0]] + return key, ok +} + +// rustMacros returns the macros that #[macro_export] puts at the root of +// the crate of each unit, by name. +func (b *builder) rustMacros() map[*space]map[string]string { s := b.rustState() - if s.macros == nil { - s.macros = map[*space]map[string]string{} + if s.macros != nil { + return s.macros } - macros, ok := s.macros[sp] - if !ok { - macros = map[string]string{} - for _, p := range b.paths { - ix := b.byPath[p] - if ix == nil || ix.Language != langcode.Rust || fileindex.PackageOf(p) != sp.unit { + s.macros = map[*space]map[string]string{} + for _, p := range b.paths { + ix := b.byPath[p] + sp := b.spaces[fileindex.PackageOf(p)] + if ix == nil || ix.Language != langcode.Rust || sp == nil || sp.unit == "" { + continue + } + for _, def := range ix.Definitions { + if !slices.Contains(def.Decorators, rustMacroExport) { continue } - for _, def := range ix.Definitions { - if _, taken := macros[def.Name]; !taken && slices.Contains(def.Decorators, rustMacroExport) { - macros[def.Name] = def.Key - } + if s.macros[sp] == nil { + s.macros[sp] = map[string]string{} + } + if _, taken := s.macros[sp][def.Name]; !taken { + s.macros[sp][def.Name] = def.Key } } - s.macros[sp] = macros } - key, ok := macros[names[0]] - return key, ok + return s.macros } // rustMacroExport is the attribute that puts a macro at the root of its @@ -789,7 +800,12 @@ func (b *builder) rustScopeFallback(sc *scope, _, first string, rest []string, t // tell a call of a macro from a call of a function, so the caller keeps // the record of the name too. func (b *builder) rustMacroCrate(ix *fileindex.Index, name string) (string, bool) { - if !b.hasUnits() { + exported := false + for _, macros := range b.rustMacros() { + _, ok := macros[name] + exported = exported || ok + } + if !exported { return "", false } for _, dep := range b.cargoOf(b.spaceOf(ix.Path)).Dependencies() { @@ -844,6 +860,22 @@ func (b *builder) rustGlobPath(importer, written string) (string, string, bool) return "", "", false } +// rustNestedName reports whether an inline module of the graph defines a +// type of the name. Most names in a Rust file name no such type, and a walk +// up the classes that hold each name cost a fifth of the link step. +func (b *builder) rustNestedName(name string) bool { + s := b.rustState() + if s.nested == nil { + s.nested = map[string]bool{} + for key := range b.g.Nodes { + if b.isType(key) && b.isType(fileindex.Parent(key)) { + s.nested[fileindex.Last(key)] = true + } + } + } + return s.nested[name] +} + // joinImplBlocks makes an impl block of a type of another module one class // with the type, as impl Store for FileStore after use crate::FileStore. func (b *builder) joinImplBlocks() { From c0b88cd4410ea06586cde3ad5340a183c8eace3b Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:20:52 +0000 Subject: [PATCH 19/86] fix(projectgraph): undo a providers memo that an unsettled glob pass writes The Rust profile of providers resolves a path of the unit, and that resolution can read the glob uses of a scope. A pass of a glob read that does not settle gives a part of the modules, and the providers memo kept the answer for the whole build. The write now logs an undo step, as the other memos that a glob read can feed do. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/packages.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/projectgraph/packages.go b/projectgraph/packages.go index 7b7317a5..37b7fcff 100644 --- a/projectgraph/packages.go +++ b/projectgraph/packages.go @@ -356,7 +356,8 @@ type provisionKey struct { // package has the name of the import: the npm name for JavaScript, the // module path for Go, and the top-level package for Python. The answer // depends only on the family, the module and the form, so the builder -// keeps it. +// keeps it. The Rust profile resolves a path of the unit, which can read +// glob uses, so a pass of a glob read that does not settle undoes it. func (b *builder) providers(o origin) []provision { family := o.lang.Family() k := provisionKey{family: family, module: o.module, form: o.form} @@ -373,6 +374,7 @@ func (b *builder) providers(o origin) []provision { out = narrow(out) } b.provisions[k] = out + b.memo(func() { delete(b.provisions, k) }) return out } From 0ca0672964a1e371a8025b20397185f9ee29bb53 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:23:42 +0000 Subject: [PATCH 20/86] chore(projectgraph): bump fileindex to 17 and projectgraph to 20 The Go, Rust and JavaScript fixes change what the indexer writes and what the link step builds. The reach and summary rules do not change, so reach.Version stays 5 and summary.Version stays 3. The golden files and the index fixtures change only in their version field. The new hash rows cover the new cases of the fixes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- fileindex/index.go | 2 +- fileindex/testdata/index.json | 2 +- plugin/indexer/plugin_test.go | 1 + plugin/indexer/testdata/golden/App.cs.json | 2 +- plugin/indexer/testdata/golden/App.java.json | 2 +- plugin/indexer/testdata/golden/App.kt.json | 2 +- plugin/indexer/testdata/golden/app.go.json | 2 +- plugin/indexer/testdata/golden/app.js.json | 2 +- plugin/indexer/testdata/golden/app.php.json | 2 +- plugin/indexer/testdata/golden/app.py.json | 2 +- plugin/indexer/testdata/golden/app.rb.json | 2 +- plugin/indexer/testdata/golden/app.rs.json | 2 +- plugin/indexer/testdata/golden/app.ts.json | 2 +- plugin/indexer/testdata/golden/exports.js.json | 2 +- plugin/indexer/testdata/golden/exports.py.json | 2 +- plugin/indexer/testdata/golden/exports.ts.json | 2 +- plugin/indexer/testdata/golden/flags.js.json | 2 +- plugin/indexer/testdata/golden/flags.py.json | 2 +- plugin/indexer/testdata/golden/flags.ts.json | 2 +- projectgraph/graph.go | 2 +- .../javascript/barrel-chain/graph.golden.json | 2 +- .../testdata/javascript/barrel-chain/indexes.json | 8 ++++---- .../javascript/commonjs-require/graph.golden.json | 2 +- .../javascript/commonjs-require/indexes.json | 6 +++--- .../javascript/default-import/graph.golden.json | 2 +- .../testdata/javascript/default-import/indexes.json | 6 +++--- .../javascript/esm-named-import/graph.golden.json | 2 +- .../javascript/esm-named-import/indexes.json | 6 +++--- .../javascript/export-star/graph.golden.json | 2 +- .../testdata/javascript/export-star/indexes.json | 10 +++++----- .../javascript/exports-conditions/graph.golden.json | 2 +- .../javascript/exports-conditions/indexes.json | 8 ++++---- .../javascript/unknown-receiver/graph.golden.json | 2 +- .../javascript/unknown-receiver/indexes.json | 2 +- .../testdata/javascript/workspaces/graph.golden.json | 2 +- .../testdata/javascript/workspaces/indexes.json | 6 +++--- .../testdata/python/all-hides/graph.golden.json | 2 +- projectgraph/testdata/python/all-hides/indexes.json | 6 +++--- .../testdata/python/assignment/graph.golden.json | 2 +- projectgraph/testdata/python/assignment/indexes.json | 2 +- .../testdata/python/class-body/graph.golden.json | 2 +- projectgraph/testdata/python/class-body/indexes.json | 2 +- .../testdata/python/decorators/graph.golden.json | 2 +- projectgraph/testdata/python/decorators/indexes.json | 2 +- .../testdata/python/external/graph.golden.json | 2 +- projectgraph/testdata/python/external/indexes.json | 2 +- .../testdata/python/local-receiver/graph.golden.json | 2 +- .../testdata/python/local-receiver/indexes.json | 2 +- .../testdata/python/member-call/graph.golden.json | 2 +- .../testdata/python/member-call/indexes.json | 6 +++--- .../testdata/python/missing-file/graph.golden.json | 2 +- .../testdata/python/missing-file/indexes.json | 2 +- .../testdata/python/package-init/graph.golden.json | 2 +- .../testdata/python/package-init/indexes.json | 6 +++--- .../testdata/python/reexport-cycle/graph.golden.json | 2 +- .../testdata/python/reexport-cycle/indexes.json | 6 +++--- .../python/relative-imports/graph.golden.json | 2 +- .../testdata/python/relative-imports/indexes.json | 6 +++--- .../testdata/python/sibling-import/graph.golden.json | 2 +- .../testdata/python/sibling-import/indexes.json | 4 ++-- .../testdata/python/src-layout/graph.golden.json | 2 +- projectgraph/testdata/python/src-layout/indexes.json | 6 +++--- .../testdata/python/sys-path/graph.golden.json | 2 +- projectgraph/testdata/python/sys-path/indexes.json | 4 ++-- .../testdata/python/wildcard/graph.golden.json | 2 +- projectgraph/testdata/python/wildcard/indexes.json | 4 ++-- .../testdata/roots/extra-rules/graph.golden.json | 2 +- projectgraph/testdata/roots/extra-rules/indexes.json | 6 +++--- .../testdata/roots/library-surface/graph.golden.json | 2 +- .../testdata/roots/library-surface/indexes.json | 4 ++-- .../testdata/roots/main-guard/graph.golden.json | 2 +- projectgraph/testdata/roots/main-guard/indexes.json | 4 ++-- .../roots/package-json-bin/graph.golden.json | 2 +- .../testdata/roots/package-json-bin/indexes.json | 4 ++-- .../roots/package-json-main/graph.golden.json | 2 +- .../testdata/roots/package-json-main/indexes.json | 8 ++++---- .../roots/procfile-dockerfile/graph.golden.json | 2 +- .../testdata/roots/procfile-dockerfile/indexes.json | 12 ++++++------ .../roots/pyproject-scripts/graph.golden.json | 2 +- .../testdata/roots/pyproject-scripts/indexes.json | 10 +++++----- .../testdata/roots/setup-py/graph.golden.json | 2 +- projectgraph/testdata/roots/setup-py/indexes.json | 4 ++-- .../testdata/roots/test-file/graph.golden.json | 2 +- projectgraph/testdata/roots/test-file/indexes.json | 2 +- .../typescript/js-specifier/graph.golden.json | 2 +- .../testdata/typescript/js-specifier/indexes.json | 4 ++-- .../typescript/tsconfig-paths/graph.golden.json | 2 +- .../testdata/typescript/tsconfig-paths/indexes.json | 6 +++--- .../testdata/typescript/type-only/graph.golden.json | 2 +- .../testdata/typescript/type-only/indexes.json | 6 +++--- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_srv_v1.0.0.json | 4 ++-- .../summaries/go_example.com_markup_v1.0.0.json | 4 ++-- .../summaries/go_std_go1.25.1.json | 4 ++-- .../summaries/go_github.com_acme_srv_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_a_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_b_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_dep_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_yamlish_v1.2.0.json | 4 ++-- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../summaries/go_example.com_markup_v1.0.0.json | 4 ++-- .../summaries/go_std_go1.25.1.json | 4 ++-- .../summaries/go_github.com_acme_fast_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../plugin-lookup-std/summaries/go_std_go1.25.1.json | 4 ++-- .../summaries/go_github.com_acme_kit_v1.0.0.json | 4 ++-- .../summaries/go_github.com_acme_ws_v1.0.0.json | 4 ++-- .../builtin-chain/summaries/npm_plug_1.0.0.json | 4 ++-- .../summaries/npm_store-kit_1.0.0.json | 4 ++-- .../summaries/npm_kit_1.0.0.json | 4 ++-- .../summaries/npm_kit_1.0.0.json | 4 ++-- .../summaries/npm_plug_1.0.0.json | 4 ++-- .../summaries/npm_util-kit_1.0.0.json | 4 ++-- .../summaries/npm_lodash._reinterpolate_3.0.0.json | 4 ++-- .../summaries/npm_lodash_4.17.21.json | 4 ++-- .../summaries/pypi_speedy_1.0.0.json | 4 ++-- .../summaries/pypi_PyYAML_6.0.1.json | 4 ++-- .../summaries/pypi_PyYAML_6.0.1.json | 4 ++-- .../summaries/pypi_badpkg_1.0.0.json | 4 ++-- .../summaries/pypi_okpkg_1.0.0.json | 4 ++-- .../summaries/pypi_badpkg_1.0.0.json | 4 ++-- .../summaries/pypi_okpkg_1.0.0.json | 4 ++-- .../summaries/pypi_google-auth_2.0.0.json | 4 ++-- .../summaries/pypi_google-cloud-storage_2.1.0.json | 4 ++-- .../summaries/pypi_outer_1.0.0.json | 4 ++-- .../summaries/pypi_PyYAML_6.0.1.json | 4 ++-- .../summaries/pypi_requests_2.31.0.json | 4 ++-- .../summaries/pypi_urllib3_2.0.7.json | 4 ++-- .../summaries/python_std_3.12.3.json | 4 ++-- .../summaries/pypi_dep_1.0.0.json | 4 ++-- .../summaries/pypi_dep_1.0.0.json | 4 ++-- .../crate-bindings/summaries/cargo_kit_1.0.0.json | 4 ++-- .../dependency-crate/summaries/cargo_kit_1.0.0.json | 4 ++-- .../summaries/cargo_kit_1.0.0.json | 4 ++-- .../summaries/cargo_kit_1.0.0.json | 4 ++-- .../summaries/cargo_dep_1.2.0.json | 4 ++-- .../rust/glob-use/summaries/cargo_dep_1.2.0.json | 4 ++-- .../into-dependency/summaries/cargo_kit_1.0.0.json | 4 ++-- .../summaries/cargo_kit_1.0.0.json | 4 ++-- .../summaries/cargo_dep_1.2.0.json | 4 ++-- .../rust/macro-import/summaries/cargo_dep_1.2.0.json | 4 ++-- .../summaries/cargo_dep_1.2.0.json | 4 ++-- .../rust/trait-object/summaries/cargo_dep_1.2.0.json | 4 ++-- .../use-binding-path/summaries/cargo_dep_1.0.0.json | 4 ++-- .../summaries/cargo_dep_1.0.0.json | 4 ++-- test/projectgraph/version_test.go | 3 +++ 152 files changed, 271 insertions(+), 267 deletions(-) diff --git a/fileindex/index.go b/fileindex/index.go index 96ca5df4..c6bd98bd 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -20,7 +20,7 @@ import ( // Version is the version of the index records and of the rules that fill // them. Bump it when a field, a kind, an order or a fill rule changes. A // store keys an index by Version, so the bump makes every older index miss. -const Version = "16" +const Version = "17" // DefaultTestPaths are the globs that mark the test files of the common // layouts, as projectgraph.Options.TestPaths takes them. A glob with no "/" diff --git a/fileindex/testdata/index.json b/fileindex/testdata/index.json index 655f2247..534108c8 100644 --- a/fileindex/testdata/index.json +++ b/fileindex/testdata/index.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "src/app.py", "language": "python", "content_hash": "a172cedcae47474b615c54d510a5d84a8dea3032e958587430b413538be3f333", diff --git a/plugin/indexer/plugin_test.go b/plugin/indexer/plugin_test.go index 912ef2b0..979574c5 100644 --- a/plugin/indexer/plugin_test.go +++ b/plugin/indexer/plugin_test.go @@ -107,6 +107,7 @@ var goldenHashes = map[string]string{ "14": "26f22c80fd4120cd04f228a53188accb1695434d0ed1ff5654b110981f8f44dd", "15": "c4b949e72be5b2b4474e06f20ae18b0cc4e080b6c5fe20ccaf712c3714bf2a12", "16": "235591d02eeb5e1bb771fbacf0ba6db228146a6a70c55670de6cb93392d75d81", + "17": "400a131636f20520cfe73263a2f96f0072137282b0339b101315b2b10b46b4d9", } func TestFileIndexGoldenHashFollowsVersion(t *testing.T) { diff --git a/plugin/indexer/testdata/golden/App.cs.json b/plugin/indexer/testdata/golden/App.cs.json index eeed7e2c..3f290593 100644 --- a/plugin/indexer/testdata/golden/App.cs.json +++ b/plugin/indexer/testdata/golden/App.cs.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "App.cs", "language": "csharp", "content_hash": "8991919e54610418b58a53fb6b26dc563c5ef5af684165b60e8af5b6e7e8773c", diff --git a/plugin/indexer/testdata/golden/App.java.json b/plugin/indexer/testdata/golden/App.java.json index 5b5fab38..4689f557 100644 --- a/plugin/indexer/testdata/golden/App.java.json +++ b/plugin/indexer/testdata/golden/App.java.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "App.java", "language": "java", "content_hash": "4b93f5c2c4ed8e1881cb857a9b7cd65c3a0d4cb4cd0929140539d0a418a27a95", diff --git a/plugin/indexer/testdata/golden/App.kt.json b/plugin/indexer/testdata/golden/App.kt.json index 9ae8908f..6586c529 100644 --- a/plugin/indexer/testdata/golden/App.kt.json +++ b/plugin/indexer/testdata/golden/App.kt.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "App.kt", "language": "kotlin", "content_hash": "458147a6c6ebc2561187207bb3bca7e091914bfd2e52f0540f58887976983462", diff --git a/plugin/indexer/testdata/golden/app.go.json b/plugin/indexer/testdata/golden/app.go.json index e56de8c4..3422b16a 100644 --- a/plugin/indexer/testdata/golden/app.go.json +++ b/plugin/indexer/testdata/golden/app.go.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "app.go", "language": "go", "content_hash": "3d65f36ed0edfd5e009c06d07d3d038337e9faebaca6c9762b5c473a14826166", diff --git a/plugin/indexer/testdata/golden/app.js.json b/plugin/indexer/testdata/golden/app.js.json index 3e4f2cfe..a3b2ec9c 100644 --- a/plugin/indexer/testdata/golden/app.js.json +++ b/plugin/indexer/testdata/golden/app.js.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "app.js", "language": "javascript", "content_hash": "247fdd512217fb5e7ce19cbf3f60b327e145abc15a26ee510e167cbc651b0a54", diff --git a/plugin/indexer/testdata/golden/app.php.json b/plugin/indexer/testdata/golden/app.php.json index 2421e377..67603908 100644 --- a/plugin/indexer/testdata/golden/app.php.json +++ b/plugin/indexer/testdata/golden/app.php.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "app.php", "language": "php", "content_hash": "b57d8d52aafac8d7cbe2d19e5889e4e7b851ee71a48ea966cdd9cbfd14214466", diff --git a/plugin/indexer/testdata/golden/app.py.json b/plugin/indexer/testdata/golden/app.py.json index d0977a83..0ea83719 100644 --- a/plugin/indexer/testdata/golden/app.py.json +++ b/plugin/indexer/testdata/golden/app.py.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "app.py", "language": "python", "content_hash": "a9cdb5fb012d2a29970c07deda9cc8360983015e2360cb9091bca7bfce5c801d", diff --git a/plugin/indexer/testdata/golden/app.rb.json b/plugin/indexer/testdata/golden/app.rb.json index 91f987a7..10ae26f1 100644 --- a/plugin/indexer/testdata/golden/app.rb.json +++ b/plugin/indexer/testdata/golden/app.rb.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "app.rb", "language": "ruby", "content_hash": "f3a3e601660e0130750119df4267cac37972c412ccc8f5d8f16e3ba516f659d9", diff --git a/plugin/indexer/testdata/golden/app.rs.json b/plugin/indexer/testdata/golden/app.rs.json index bc202bbc..59d5d3fe 100644 --- a/plugin/indexer/testdata/golden/app.rs.json +++ b/plugin/indexer/testdata/golden/app.rs.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "app.rs", "language": "rust", "content_hash": "f07a751f9610f47e7b49679223d4ca80f85ecb21abde61488935d2f21a5c3603", diff --git a/plugin/indexer/testdata/golden/app.ts.json b/plugin/indexer/testdata/golden/app.ts.json index de5bc283..6348d1be 100644 --- a/plugin/indexer/testdata/golden/app.ts.json +++ b/plugin/indexer/testdata/golden/app.ts.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "app.ts", "language": "typescript", "content_hash": "d3908948ab8ec7b3adc898e0707ce74bb3a2d6393a724dfbeb67147840397234", diff --git a/plugin/indexer/testdata/golden/exports.js.json b/plugin/indexer/testdata/golden/exports.js.json index b44ef90b..570c1701 100644 --- a/plugin/indexer/testdata/golden/exports.js.json +++ b/plugin/indexer/testdata/golden/exports.js.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "exports.js", "language": "javascript", "content_hash": "765716b5977b05d7bbc4ef5e0192e196e0209400366036053df632d54fdb0f95", diff --git a/plugin/indexer/testdata/golden/exports.py.json b/plugin/indexer/testdata/golden/exports.py.json index 4f230ace..71855d8f 100644 --- a/plugin/indexer/testdata/golden/exports.py.json +++ b/plugin/indexer/testdata/golden/exports.py.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "exports.py", "language": "python", "content_hash": "653115ddc22536763049e9209d4942bca5c6817b98bc01722879b69c04d8605b", diff --git a/plugin/indexer/testdata/golden/exports.ts.json b/plugin/indexer/testdata/golden/exports.ts.json index a76cf808..b4f83d2b 100644 --- a/plugin/indexer/testdata/golden/exports.ts.json +++ b/plugin/indexer/testdata/golden/exports.ts.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "exports.ts", "language": "typescript", "content_hash": "0edcb9324080b924f3ac45d57f5b5cbe90d625038a0dd7f99fa9e5fcbeee1edc", diff --git a/plugin/indexer/testdata/golden/flags.js.json b/plugin/indexer/testdata/golden/flags.js.json index 2e1bcde4..89368568 100644 --- a/plugin/indexer/testdata/golden/flags.js.json +++ b/plugin/indexer/testdata/golden/flags.js.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "flags.js", "language": "javascript", "content_hash": "70731d0448bc5af9ecf5c81156eefbd39535a3fc674fffc250937d76c2ba4610", diff --git a/plugin/indexer/testdata/golden/flags.py.json b/plugin/indexer/testdata/golden/flags.py.json index c49674dc..fbe8d317 100644 --- a/plugin/indexer/testdata/golden/flags.py.json +++ b/plugin/indexer/testdata/golden/flags.py.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "flags.py", "language": "python", "content_hash": "9d58772a61d2cb32039a7e81164d1cb50c3c63a536c2676c91e7a36fad954132", diff --git a/plugin/indexer/testdata/golden/flags.ts.json b/plugin/indexer/testdata/golden/flags.ts.json index e75b532a..edfd7e5a 100644 --- a/plugin/indexer/testdata/golden/flags.ts.json +++ b/plugin/indexer/testdata/golden/flags.ts.json @@ -1,5 +1,5 @@ { - "version": "16", + "version": "17", "path": "flags.ts", "language": "typescript", "content_hash": "a84c4ad21735c1c250e33317463237749489ea1f220a93f97e2bea3f07b040c4", diff --git a/projectgraph/graph.go b/projectgraph/graph.go index cb561324..6265c4ca 100644 --- a/projectgraph/graph.go +++ b/projectgraph/graph.go @@ -18,7 +18,7 @@ import ( // Version is the version of the link rules. Bump it when a rule that makes a // node, an edge, a root or an unresolved site changes, so a diff of two // reports shows the change. -const Version = "19" +const Version = "20" type NodeKind string diff --git a/projectgraph/testdata/javascript/barrel-chain/graph.golden.json b/projectgraph/testdata/javascript/barrel-chain/graph.golden.json index a4e90ffe..debc6e3a 100644 --- a/projectgraph/testdata/javascript/barrel-chain/graph.golden.json +++ b/projectgraph/testdata/javascript/barrel-chain/graph.golden.json @@ -178,7 +178,7 @@ "src/main.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 4, "nodes": 7, "edges": 6, diff --git a/projectgraph/testdata/javascript/barrel-chain/indexes.json b/projectgraph/testdata/javascript/barrel-chain/indexes.json index 0b002b43..13ff3ebe 100644 --- a/projectgraph/testdata/javascript/barrel-chain/indexes.json +++ b/projectgraph/testdata/javascript/barrel-chain/indexes.json @@ -1,22 +1,22 @@ [ { - "version": "16", "path": "src/main.js", "language": "javascript", + "version": "17", "path": "src/main.js", "language": "javascript", "imports": [{"line": 1, "module": "./api", "item": "parse", "alias": "parse", "kind": "static", "namespace": ".//api//parse"}], "definitions": [{"key": "src/main.js", "kind": "module", "name": "main", "start_line": 1, "end_line": 2, "exported": true}], "calls": [{"caller": "src/main.js", "line": 2, "column": 1, "callee": "parse", "targets": [".//api//parse"], "kind": "call"}] }, { - "version": "16", "path": "src/api/index.js", "language": "javascript", + "version": "17", "path": "src/api/index.js", "language": "javascript", "exports": [{"name": "parse", "target": "", "from": "../core", "item": "parse", "line": 1}], "definitions": [{"key": "src/api/index.js", "kind": "module", "name": "index", "start_line": 1, "end_line": 1, "exported": true}] }, { - "version": "16", "path": "src/core/index.js", "language": "javascript", + "version": "17", "path": "src/core/index.js", "language": "javascript", "exports": [{"name": "parse", "target": "", "from": "./parse", "item": "doParse", "line": 1}], "definitions": [{"key": "src/core/index.js", "kind": "module", "name": "index", "start_line": 1, "end_line": 1, "exported": true}] }, { - "version": "16", "path": "src/core/parse.js", "language": "javascript", + "version": "17", "path": "src/core/parse.js", "language": "javascript", "exports": [{"name": "doParse", "target": "src/core/parse.js//doParse", "line": 1}], "definitions": [ {"key": "src/core/parse.js", "kind": "module", "name": "parse", "start_line": 1, "end_line": 3, "exported": true}, diff --git a/projectgraph/testdata/javascript/commonjs-require/graph.golden.json b/projectgraph/testdata/javascript/commonjs-require/graph.golden.json index 8836aa06..31cbbb44 100644 --- a/projectgraph/testdata/javascript/commonjs-require/graph.golden.json +++ b/projectgraph/testdata/javascript/commonjs-require/graph.golden.json @@ -141,7 +141,7 @@ "main.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 5, "edges": 4, diff --git a/projectgraph/testdata/javascript/commonjs-require/indexes.json b/projectgraph/testdata/javascript/commonjs-require/indexes.json index b529ac24..5fc27dfb 100644 --- a/projectgraph/testdata/javascript/commonjs-require/indexes.json +++ b/projectgraph/testdata/javascript/commonjs-require/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "main.js", "language": "javascript", + "version": "17", "path": "main.js", "language": "javascript", "imports": [ {"line": 1, "module": "./lib", "alias": "utils", "kind": "static", "namespace": ".//lib"}, {"line": 2, "module": "./helper", "item": "helper", "alias": "helper", "kind": "static", "namespace": ".//helper//helper"} @@ -12,7 +12,7 @@ ] }, { - "version": "16", "path": "lib/index.js", "language": "javascript", + "version": "17", "path": "lib/index.js", "language": "javascript", "exports": [{"name": "run", "target": "lib/index.js//run", "line": 3}], "definitions": [ {"key": "lib/index.js", "kind": "module", "name": "index", "start_line": 1, "end_line": 3, "exported": true}, @@ -20,7 +20,7 @@ ] }, { - "version": "16", "path": "helper.js", "language": "javascript", + "version": "17", "path": "helper.js", "language": "javascript", "exports": [{"name": "helper", "target": "helper.js//helper", "line": 3}], "definitions": [ {"key": "helper.js", "kind": "module", "name": "helper", "start_line": 1, "end_line": 3, "exported": true}, diff --git a/projectgraph/testdata/javascript/default-import/graph.golden.json b/projectgraph/testdata/javascript/default-import/graph.golden.json index 15e7c262..b43f396c 100644 --- a/projectgraph/testdata/javascript/default-import/graph.golden.json +++ b/projectgraph/testdata/javascript/default-import/graph.golden.json @@ -141,7 +141,7 @@ "make.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 5, "edges": 4, diff --git a/projectgraph/testdata/javascript/default-import/indexes.json b/projectgraph/testdata/javascript/default-import/indexes.json index f43e6fb7..9de68b7c 100644 --- a/projectgraph/testdata/javascript/default-import/indexes.json +++ b/projectgraph/testdata/javascript/default-import/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "main.js", "language": "javascript", + "version": "17", "path": "main.js", "language": "javascript", "imports": [ {"line": 1, "module": "./log", "alias": "log", "kind": "static", "namespace": ".//log"}, {"line": 2, "module": "./make", "alias": "make", "kind": "static", "namespace": ".//make"} @@ -12,7 +12,7 @@ ] }, { - "version": "16", "path": "log.js", "language": "javascript", + "version": "17", "path": "log.js", "language": "javascript", "exports": [{"name": "default", "target": "log.js//log", "line": 1}], "definitions": [ {"key": "log.js", "kind": "module", "name": "log", "start_line": 1, "end_line": 1, "exported": true}, @@ -20,7 +20,7 @@ ] }, { - "version": "16", "path": "make.js", "language": "javascript", + "version": "17", "path": "make.js", "language": "javascript", "exports": [{"name": "default", "target": "make.js//make", "line": 1}], "definitions": [ {"key": "make.js", "kind": "module", "name": "make", "start_line": 1, "end_line": 1, "exported": true}, diff --git a/projectgraph/testdata/javascript/esm-named-import/graph.golden.json b/projectgraph/testdata/javascript/esm-named-import/graph.golden.json index 20ecfee1..ff81743e 100644 --- a/projectgraph/testdata/javascript/esm-named-import/graph.golden.json +++ b/projectgraph/testdata/javascript/esm-named-import/graph.golden.json @@ -169,7 +169,7 @@ "src/util.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 6, "edges": 5, diff --git a/projectgraph/testdata/javascript/esm-named-import/indexes.json b/projectgraph/testdata/javascript/esm-named-import/indexes.json index 7f761be4..bddf8157 100644 --- a/projectgraph/testdata/javascript/esm-named-import/indexes.json +++ b/projectgraph/testdata/javascript/esm-named-import/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "src/main.js", "language": "javascript", + "version": "17", "path": "src/main.js", "language": "javascript", "imports": [ {"line": 1, "module": "./parser.js", "item": "parse", "alias": "parse", "kind": "static", "namespace": ".//parser.js//parse"}, {"line": 2, "module": "./util", "item": "fmt", "alias": "fmt", "kind": "static", "namespace": ".//util//fmt"}, @@ -14,7 +14,7 @@ "unresolved": [{"caller": "src/main.js", "line": 5, "column": 1, "text": "import", "reason": "dynamic-import"}] }, { - "version": "16", "path": "src/parser.js", "language": "javascript", + "version": "17", "path": "src/parser.js", "language": "javascript", "imports": [{"line": 1, "module": "js-yaml", "alias": "yaml", "kind": "static", "namespace": "js-yaml"}], "exports": [{"name": "parse", "target": "src/parser.js//parse", "line": 2}], "definitions": [ @@ -24,7 +24,7 @@ "calls": [{"caller": "src/parser.js//parse", "line": 3, "column": 10, "callee": "yaml.load", "targets": ["js-yaml//load"], "receiver": "yaml", "method": "load", "kind": "call"}] }, { - "version": "16", "path": "src/util.js", "language": "javascript", + "version": "17", "path": "src/util.js", "language": "javascript", "exports": [{"name": "fmt", "target": "src/util.js//fmt", "line": 1}], "definitions": [ {"key": "src/util.js", "kind": "module", "name": "util", "start_line": 1, "end_line": 1, "exported": true}, diff --git a/projectgraph/testdata/javascript/export-star/graph.golden.json b/projectgraph/testdata/javascript/export-star/graph.golden.json index 67c4e5c3..8d8d5d0e 100644 --- a/projectgraph/testdata/javascript/export-star/graph.golden.json +++ b/projectgraph/testdata/javascript/export-star/graph.golden.json @@ -281,7 +281,7 @@ "main.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 5, "nodes": 11, "edges": 10, diff --git a/projectgraph/testdata/javascript/export-star/indexes.json b/projectgraph/testdata/javascript/export-star/indexes.json index 4046a108..83cc326f 100644 --- a/projectgraph/testdata/javascript/export-star/indexes.json +++ b/projectgraph/testdata/javascript/export-star/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "main.js", "language": "javascript", + "version": "17", "path": "main.js", "language": "javascript", "imports": [ {"line": 1, "module": "./all", "item": "a", "alias": "a", "kind": "static", "namespace": ".//all//a"}, {"line": 1, "module": "./all", "item": "b", "alias": "b", "kind": "static", "namespace": ".//all//b"}, @@ -14,7 +14,7 @@ ] }, { - "version": "16", "path": "all.js", "language": "javascript", + "version": "17", "path": "all.js", "language": "javascript", "exports": [ {"name": "*", "target": "", "from": "./a", "item": "*", "line": 1}, {"name": "*", "target": "", "from": "./b", "item": "*", "line": 2}, @@ -23,7 +23,7 @@ "definitions": [{"key": "all.js", "kind": "module", "name": "all", "start_line": 1, "end_line": 3, "exported": true}] }, { - "version": "16", "path": "a.js", "language": "javascript", + "version": "17", "path": "a.js", "language": "javascript", "exports": [{"name": "a", "target": "a.js//a", "line": 1}], "definitions": [ {"key": "a.js", "kind": "module", "name": "a", "start_line": 1, "end_line": 1, "exported": true}, @@ -31,7 +31,7 @@ ] }, { - "version": "16", "path": "b.js", "language": "javascript", + "version": "17", "path": "b.js", "language": "javascript", "exports": [{"name": "b", "target": "b.js//b", "line": 1}], "definitions": [ {"key": "b.js", "kind": "module", "name": "b", "start_line": 1, "end_line": 1, "exported": true}, @@ -39,7 +39,7 @@ ] }, { - "version": "16", "path": "c.js", "language": "javascript", + "version": "17", "path": "c.js", "language": "javascript", "exports": [{"name": "c", "target": "c.js//c", "line": 1}], "definitions": [ {"key": "c.js", "kind": "module", "name": "c", "start_line": 1, "end_line": 1, "exported": true}, diff --git a/projectgraph/testdata/javascript/exports-conditions/graph.golden.json b/projectgraph/testdata/javascript/exports-conditions/graph.golden.json index f78a5bde..96910cc9 100644 --- a/projectgraph/testdata/javascript/exports-conditions/graph.golden.json +++ b/projectgraph/testdata/javascript/exports-conditions/graph.golden.json @@ -148,7 +148,7 @@ "lib/src/feature/y.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 4, "nodes": 7, "edges": 4, diff --git a/projectgraph/testdata/javascript/exports-conditions/indexes.json b/projectgraph/testdata/javascript/exports-conditions/indexes.json index db20bd33..7c770c74 100644 --- a/projectgraph/testdata/javascript/exports-conditions/indexes.json +++ b/projectgraph/testdata/javascript/exports-conditions/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "app/main.mjs", "language": "javascript", + "version": "17", "path": "app/main.mjs", "language": "javascript", "imports": [ {"line": 1, "module": "@acme/lib", "item": "x", "alias": "x", "kind": "static", "namespace": "@acme//lib//x"}, {"line": 2, "module": "@acme/lib/feature/y", "item": "y", "alias": "y", "kind": "static", "namespace": "@acme//lib//feature//y//y"} @@ -12,7 +12,7 @@ ] }, { - "version": "16", "path": "lib/esm/index.mjs", "language": "javascript", + "version": "17", "path": "lib/esm/index.mjs", "language": "javascript", "exports": [{"name": "x", "target": "lib/esm/index.mjs//x", "line": 1}], "definitions": [ {"key": "lib/esm/index.mjs", "kind": "module", "name": "index", "start_line": 1, "end_line": 1, "exported": true}, @@ -20,7 +20,7 @@ ] }, { - "version": "16", "path": "lib/cjs/index.cjs", "language": "javascript", + "version": "17", "path": "lib/cjs/index.cjs", "language": "javascript", "exports": [{"name": "x", "target": "lib/cjs/index.cjs//x", "line": 1}], "definitions": [ {"key": "lib/cjs/index.cjs", "kind": "module", "name": "index", "start_line": 1, "end_line": 1, "exported": true}, @@ -28,7 +28,7 @@ ] }, { - "version": "16", "path": "lib/src/feature/y.js", "language": "javascript", + "version": "17", "path": "lib/src/feature/y.js", "language": "javascript", "exports": [{"name": "y", "target": "lib/src/feature/y.js//y", "line": 1}], "definitions": [ {"key": "lib/src/feature/y.js", "kind": "module", "name": "y", "start_line": 1, "end_line": 1, "exported": true}, diff --git a/projectgraph/testdata/javascript/unknown-receiver/graph.golden.json b/projectgraph/testdata/javascript/unknown-receiver/graph.golden.json index fdb2512d..ab7b5f81 100644 --- a/projectgraph/testdata/javascript/unknown-receiver/graph.golden.json +++ b/projectgraph/testdata/javascript/unknown-receiver/graph.golden.json @@ -125,7 +125,7 @@ "main.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 1, "nodes": 5, "edges": 3, diff --git a/projectgraph/testdata/javascript/unknown-receiver/indexes.json b/projectgraph/testdata/javascript/unknown-receiver/indexes.json index feb78b07..2e57fcb3 100644 --- a/projectgraph/testdata/javascript/unknown-receiver/indexes.json +++ b/projectgraph/testdata/javascript/unknown-receiver/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", + "version": "17", "path": "main.js", "language": "javascript", "imports": [ diff --git a/projectgraph/testdata/javascript/workspaces/graph.golden.json b/projectgraph/testdata/javascript/workspaces/graph.golden.json index 67a6463f..30e08383 100644 --- a/projectgraph/testdata/javascript/workspaces/graph.golden.json +++ b/projectgraph/testdata/javascript/workspaces/graph.golden.json @@ -130,7 +130,7 @@ "packages/core/util.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 6, "edges": 5, diff --git a/projectgraph/testdata/javascript/workspaces/indexes.json b/projectgraph/testdata/javascript/workspaces/indexes.json index 14feff26..5765010a 100644 --- a/projectgraph/testdata/javascript/workspaces/indexes.json +++ b/projectgraph/testdata/javascript/workspaces/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "packages/app/src/main.js", "language": "javascript", + "version": "17", "path": "packages/app/src/main.js", "language": "javascript", "imports": [ {"line": 1, "module": "@acme/core", "item": "start", "alias": "start", "kind": "static", "namespace": "@acme//core//start"}, {"line": 2, "module": "@acme/core/util", "item": "tidy", "alias": "tidy", "kind": "static", "namespace": "@acme//core//util//tidy"}, @@ -14,7 +14,7 @@ ] }, { - "version": "16", "path": "packages/core/lib/index.js", "language": "javascript", + "version": "17", "path": "packages/core/lib/index.js", "language": "javascript", "exports": [{"name": "start", "target": "packages/core/lib/index.js//start", "line": 1}], "definitions": [ {"key": "packages/core/lib/index.js", "kind": "module", "name": "index", "start_line": 1, "end_line": 1, "exported": true}, @@ -22,7 +22,7 @@ ] }, { - "version": "16", "path": "packages/core/util.js", "language": "javascript", + "version": "17", "path": "packages/core/util.js", "language": "javascript", "exports": [{"name": "tidy", "target": "packages/core/util.js//tidy", "line": 1}], "definitions": [ {"key": "packages/core/util.js", "kind": "module", "name": "util", "start_line": 1, "end_line": 1, "exported": true}, diff --git a/projectgraph/testdata/python/all-hides/graph.golden.json b/projectgraph/testdata/python/all-hides/graph.golden.json index 1734e12b..96a9206d 100644 --- a/projectgraph/testdata/python/all-hides/graph.golden.json +++ b/projectgraph/testdata/python/all-hides/graph.golden.json @@ -177,7 +177,7 @@ "pkg/a.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 7, "edges": 6, diff --git a/projectgraph/testdata/python/all-hides/indexes.json b/projectgraph/testdata/python/all-hides/indexes.json index a6a6064d..3a5f5d94 100644 --- a/projectgraph/testdata/python/all-hides/indexes.json +++ b/projectgraph/testdata/python/all-hides/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "main.py", "language": "python", + "version": "17", "path": "main.py", "language": "python", "imports": [ {"line": 1, "module": "pkg", "item": "f", "alias": "f", "kind": "static", "namespace": "pkg//f"}, {"line": 1, "module": "pkg", "item": "g", "alias": "g", "kind": "static", "namespace": "pkg//g"} @@ -12,7 +12,7 @@ ] }, { - "version": "16", "path": "pkg/__init__.py", "language": "python", + "version": "17", "path": "pkg/__init__.py", "language": "python", "imports": [ {"line": 1, "module": ".a", "item": "f", "alias": "f", "kind": "static", "namespace": "//a//f"}, {"line": 1, "module": ".a", "item": "g", "alias": "g", "kind": "static", "namespace": "//a//g"} @@ -21,7 +21,7 @@ "definitions": [{"key": "pkg/__init__.py", "kind": "module", "name": "__init__", "start_line": 1, "end_line": 2, "exported": true}] }, { - "version": "16", "path": "pkg/a.py", "language": "python", + "version": "17", "path": "pkg/a.py", "language": "python", "definitions": [ {"key": "pkg/a.py", "kind": "module", "name": "a", "start_line": 1, "end_line": 4, "exported": true}, {"key": "pkg/a.py//f", "kind": "function", "name": "f", "start_line": 1, "end_line": 2, "exported": true}, diff --git a/projectgraph/testdata/python/assignment/graph.golden.json b/projectgraph/testdata/python/assignment/graph.golden.json index 1578e2a6..832f0eff 100644 --- a/projectgraph/testdata/python/assignment/graph.golden.json +++ b/projectgraph/testdata/python/assignment/graph.golden.json @@ -71,7 +71,7 @@ "main.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 1, "nodes": 3, "edges": 2, diff --git a/projectgraph/testdata/python/assignment/indexes.json b/projectgraph/testdata/python/assignment/indexes.json index 2bb03823..d7ffc6c2 100644 --- a/projectgraph/testdata/python/assignment/indexes.json +++ b/projectgraph/testdata/python/assignment/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "main.py", "language": "python", + "version": "17", "path": "main.py", "language": "python", "imports": [{"line": 1, "module": "yaml", "alias": "yaml", "kind": "static", "namespace": "yaml"}], "definitions": [ {"key": "main.py", "kind": "module", "name": "main", "start_line": 1, "end_line": 7, "exported": true}, diff --git a/projectgraph/testdata/python/class-body/graph.golden.json b/projectgraph/testdata/python/class-body/graph.golden.json index 47bbcdc2..510afd05 100644 --- a/projectgraph/testdata/python/class-body/graph.golden.json +++ b/projectgraph/testdata/python/class-body/graph.golden.json @@ -225,7 +225,7 @@ "config.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 1, "nodes": 12, "edges": 7, diff --git a/projectgraph/testdata/python/class-body/indexes.json b/projectgraph/testdata/python/class-body/indexes.json index 6c463e77..80110771 100644 --- a/projectgraph/testdata/python/class-body/indexes.json +++ b/projectgraph/testdata/python/class-body/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", + "version": "17", "path": "config.py", "language": "python", "imports": [ diff --git a/projectgraph/testdata/python/decorators/graph.golden.json b/projectgraph/testdata/python/decorators/graph.golden.json index 2548a8f2..164cd3f0 100644 --- a/projectgraph/testdata/python/decorators/graph.golden.json +++ b/projectgraph/testdata/python/decorators/graph.golden.json @@ -195,7 +195,7 @@ "app.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 1, "nodes": 9, "edges": 5, diff --git a/projectgraph/testdata/python/decorators/indexes.json b/projectgraph/testdata/python/decorators/indexes.json index fb3c1026..aba46fce 100644 --- a/projectgraph/testdata/python/decorators/indexes.json +++ b/projectgraph/testdata/python/decorators/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", + "version": "17", "path": "app.py", "language": "python", "imports": [ diff --git a/projectgraph/testdata/python/external/graph.golden.json b/projectgraph/testdata/python/external/graph.golden.json index 09ebfb11..4ff0e473 100644 --- a/projectgraph/testdata/python/external/graph.golden.json +++ b/projectgraph/testdata/python/external/graph.golden.json @@ -109,7 +109,7 @@ "main.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 1, "nodes": 5, "edges": 3, diff --git a/projectgraph/testdata/python/external/indexes.json b/projectgraph/testdata/python/external/indexes.json index d32422bb..46684fc7 100644 --- a/projectgraph/testdata/python/external/indexes.json +++ b/projectgraph/testdata/python/external/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "main.py", "language": "python", + "version": "17", "path": "main.py", "language": "python", "imports": [ {"line": 1, "module": "requests", "alias": "requests", "kind": "static", "namespace": "requests"}, {"line": 2, "module": "yaml", "item": "load", "alias": "yl", "kind": "static", "namespace": "yaml//load"}, diff --git a/projectgraph/testdata/python/local-receiver/graph.golden.json b/projectgraph/testdata/python/local-receiver/graph.golden.json index 53d28704..dd196229 100644 --- a/projectgraph/testdata/python/local-receiver/graph.golden.json +++ b/projectgraph/testdata/python/local-receiver/graph.golden.json @@ -146,7 +146,7 @@ "main.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 1, "nodes": 5, "edges": 3, diff --git a/projectgraph/testdata/python/local-receiver/indexes.json b/projectgraph/testdata/python/local-receiver/indexes.json index f48f01e2..e359a9f0 100644 --- a/projectgraph/testdata/python/local-receiver/indexes.json +++ b/projectgraph/testdata/python/local-receiver/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", + "version": "17", "path": "main.py", "language": "python", "imports": [ diff --git a/projectgraph/testdata/python/member-call/graph.golden.json b/projectgraph/testdata/python/member-call/graph.golden.json index ba99aec7..d0c4cc15 100644 --- a/projectgraph/testdata/python/member-call/graph.golden.json +++ b/projectgraph/testdata/python/member-call/graph.golden.json @@ -261,7 +261,7 @@ "parser.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 9, "edges": 10, diff --git a/projectgraph/testdata/python/member-call/indexes.json b/projectgraph/testdata/python/member-call/indexes.json index 36d770e8..29fbc53a 100644 --- a/projectgraph/testdata/python/member-call/indexes.json +++ b/projectgraph/testdata/python/member-call/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "main.py", "language": "python", + "version": "17", "path": "main.py", "language": "python", "imports": [{"line": 1, "module": "parser", "item": "Parser", "alias": "Parser", "kind": "static", "namespace": "parser//Parser"}], "definitions": [{"key": "main.py", "kind": "module", "name": "main", "start_line": 1, "end_line": 4, "exported": true}], "calls": [ @@ -10,7 +10,7 @@ ] }, { - "version": "16", "path": "parser.py", "language": "python", + "version": "17", "path": "parser.py", "language": "python", "imports": [{"line": 1, "module": "base", "item": "Base", "alias": "Base", "kind": "static", "namespace": "base//Base"}], "definitions": [ {"key": "parser.py", "kind": "module", "name": "parser", "start_line": 1, "end_line": 7, "exported": true}, @@ -22,7 +22,7 @@ "calls": [{"caller": "parser.py//Parser//parse", "line": 6, "column": 9, "callee": "self.helper", "targets": ["parser.py//Parser//helper"], "receiver": "self", "method": "helper", "kind": "call"}] }, { - "version": "16", "path": "base.py", "language": "python", + "version": "17", "path": "base.py", "language": "python", "imports": [{"line": 1, "module": "io", "alias": "io", "kind": "static", "namespace": "io"}], "definitions": [ {"key": "base.py", "kind": "module", "name": "base", "start_line": 1, "end_line": 4, "exported": true}, diff --git a/projectgraph/testdata/python/missing-file/graph.golden.json b/projectgraph/testdata/python/missing-file/graph.golden.json index 655943b8..a8261600 100644 --- a/projectgraph/testdata/python/missing-file/graph.golden.json +++ b/projectgraph/testdata/python/missing-file/graph.golden.json @@ -56,7 +56,7 @@ "main.py": "cached" }, "stats": { - "version": "19", + "version": "20", "files": 2, "nodes": 2, "edges": 2, diff --git a/projectgraph/testdata/python/missing-file/indexes.json b/projectgraph/testdata/python/missing-file/indexes.json index 904544ca..27d7fe9f 100644 --- a/projectgraph/testdata/python/missing-file/indexes.json +++ b/projectgraph/testdata/python/missing-file/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "main.py", "language": "python", + "version": "17", "path": "main.py", "language": "python", "imports": [{"line": 1, "module": "handlers", "item": "parse", "alias": "parse", "kind": "static", "namespace": "handlers//parse"}], "definitions": [{"key": "main.py", "kind": "module", "name": "main", "start_line": 1, "end_line": 2, "exported": true}], "calls": [{"caller": "main.py", "line": 2, "column": 1, "callee": "parse", "targets": ["handlers//parse"], "kind": "call"}] diff --git a/projectgraph/testdata/python/package-init/graph.golden.json b/projectgraph/testdata/python/package-init/graph.golden.json index 79cd482e..415b0a23 100644 --- a/projectgraph/testdata/python/package-init/graph.golden.json +++ b/projectgraph/testdata/python/package-init/graph.golden.json @@ -147,7 +147,7 @@ "pkg/loader.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 6, "edges": 5, diff --git a/projectgraph/testdata/python/package-init/indexes.json b/projectgraph/testdata/python/package-init/indexes.json index 84f0963a..d633dc6b 100644 --- a/projectgraph/testdata/python/package-init/indexes.json +++ b/projectgraph/testdata/python/package-init/indexes.json @@ -1,17 +1,17 @@ [ { - "version": "16", "path": "main.py", "language": "python", + "version": "17", "path": "main.py", "language": "python", "imports": [{"line": 1, "module": "pkg", "item": "load", "alias": "load", "kind": "static", "namespace": "pkg//load"}], "definitions": [{"key": "main.py", "kind": "module", "name": "main", "start_line": 1, "end_line": 2, "exported": true}], "calls": [{"caller": "main.py", "line": 2, "column": 1, "callee": "load", "targets": ["pkg//load"], "kind": "call"}] }, { - "version": "16", "path": "pkg/__init__.py", "language": "python", + "version": "17", "path": "pkg/__init__.py", "language": "python", "imports": [{"line": 1, "module": ".loader", "item": "load", "alias": "load", "kind": "static", "namespace": "//loader//load"}], "definitions": [{"key": "pkg/__init__.py", "kind": "module", "name": "__init__", "start_line": 1, "end_line": 1, "exported": true}] }, { - "version": "16", "path": "pkg/loader.py", "language": "python", + "version": "17", "path": "pkg/loader.py", "language": "python", "imports": [{"line": 1, "module": "yaml", "alias": "yaml", "kind": "static", "namespace": "yaml"}], "definitions": [ {"key": "pkg/loader.py", "kind": "module", "name": "loader", "start_line": 1, "end_line": 4, "exported": true}, diff --git a/projectgraph/testdata/python/reexport-cycle/graph.golden.json b/projectgraph/testdata/python/reexport-cycle/graph.golden.json index 940e627c..b1791724 100644 --- a/projectgraph/testdata/python/reexport-cycle/graph.golden.json +++ b/projectgraph/testdata/python/reexport-cycle/graph.golden.json @@ -108,7 +108,7 @@ "main.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 3, "edges": 3, diff --git a/projectgraph/testdata/python/reexport-cycle/indexes.json b/projectgraph/testdata/python/reexport-cycle/indexes.json index 4f61c8a3..d1cb3b60 100644 --- a/projectgraph/testdata/python/reexport-cycle/indexes.json +++ b/projectgraph/testdata/python/reexport-cycle/indexes.json @@ -1,17 +1,17 @@ [ { - "version": "16", "path": "main.py", "language": "python", + "version": "17", "path": "main.py", "language": "python", "imports": [{"line": 1, "module": "a", "item": "x", "alias": "x", "kind": "static", "namespace": "a//x"}], "definitions": [{"key": "main.py", "kind": "module", "name": "main", "start_line": 1, "end_line": 2, "exported": true}], "calls": [{"caller": "main.py", "line": 2, "column": 1, "callee": "x", "targets": ["a//x"], "kind": "call"}] }, { - "version": "16", "path": "a/__init__.py", "language": "python", + "version": "17", "path": "a/__init__.py", "language": "python", "imports": [{"line": 1, "module": "b", "item": "x", "alias": "x", "kind": "static", "namespace": "b//x"}], "definitions": [{"key": "a/__init__.py", "kind": "module", "name": "__init__", "start_line": 1, "end_line": 1, "exported": true}] }, { - "version": "16", "path": "b/__init__.py", "language": "python", + "version": "17", "path": "b/__init__.py", "language": "python", "imports": [{"line": 1, "module": "a", "item": "x", "alias": "x", "kind": "static", "namespace": "a//x"}], "definitions": [{"key": "b/__init__.py", "kind": "module", "name": "__init__", "start_line": 1, "end_line": 1, "exported": true}] } diff --git a/projectgraph/testdata/python/relative-imports/graph.golden.json b/projectgraph/testdata/python/relative-imports/graph.golden.json index 11e53ed3..7a1f10df 100644 --- a/projectgraph/testdata/python/relative-imports/graph.golden.json +++ b/projectgraph/testdata/python/relative-imports/graph.golden.json @@ -157,7 +157,7 @@ "pkg/sub/b.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 6, "edges": 4, diff --git a/projectgraph/testdata/python/relative-imports/indexes.json b/projectgraph/testdata/python/relative-imports/indexes.json index 68bb7eb2..08ef6351 100644 --- a/projectgraph/testdata/python/relative-imports/indexes.json +++ b/projectgraph/testdata/python/relative-imports/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "pkg/sub/a.py", "language": "python", + "version": "17", "path": "pkg/sub/a.py", "language": "python", "imports": [ {"line": 1, "module": ".b", "item": "f", "alias": "f", "kind": "static", "namespace": "//b//f"}, {"line": 2, "module": "..c", "item": "g", "alias": "g", "kind": "static", "namespace": "////c//g"} @@ -15,14 +15,14 @@ ] }, { - "version": "16", "path": "pkg/sub/b.py", "language": "python", + "version": "17", "path": "pkg/sub/b.py", "language": "python", "definitions": [ {"key": "pkg/sub/b.py", "kind": "module", "name": "b", "start_line": 1, "end_line": 2, "exported": true}, {"key": "pkg/sub/b.py//f", "kind": "function", "name": "f", "start_line": 1, "end_line": 2, "exported": true} ] }, { - "version": "16", "path": "pkg/c.py", "language": "python", + "version": "17", "path": "pkg/c.py", "language": "python", "definitions": [ {"key": "pkg/c.py", "kind": "module", "name": "c", "start_line": 1, "end_line": 2, "exported": true}, {"key": "pkg/c.py//g", "kind": "function", "name": "g", "start_line": 1, "end_line": 2, "exported": true} diff --git a/projectgraph/testdata/python/sibling-import/graph.golden.json b/projectgraph/testdata/python/sibling-import/graph.golden.json index 63d16527..bdbd3aa7 100644 --- a/projectgraph/testdata/python/sibling-import/graph.golden.json +++ b/projectgraph/testdata/python/sibling-import/graph.golden.json @@ -100,7 +100,7 @@ "main.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 2, "nodes": 4, "edges": 3, diff --git a/projectgraph/testdata/python/sibling-import/indexes.json b/projectgraph/testdata/python/sibling-import/indexes.json index cd6aa575..418ac602 100644 --- a/projectgraph/testdata/python/sibling-import/indexes.json +++ b/projectgraph/testdata/python/sibling-import/indexes.json @@ -1,12 +1,12 @@ [ { - "version": "16", "path": "main.py", "language": "python", + "version": "17", "path": "main.py", "language": "python", "imports": [{"line": 1, "module": "handlers", "item": "parse", "alias": "parse", "kind": "static", "namespace": "handlers//parse"}], "definitions": [{"key": "main.py", "kind": "module", "name": "main", "start_line": 1, "end_line": 3, "exported": true}], "calls": [{"caller": "main.py", "line": 3, "column": 1, "callee": "parse", "targets": ["handlers//parse"], "kind": "call"}] }, { - "version": "16", "path": "handlers.py", "language": "python", + "version": "17", "path": "handlers.py", "language": "python", "imports": [{"line": 1, "module": "yaml", "alias": "yaml", "kind": "static", "namespace": "yaml"}], "definitions": [ {"key": "handlers.py", "kind": "module", "name": "handlers", "start_line": 1, "end_line": 4, "exported": true}, diff --git a/projectgraph/testdata/python/src-layout/graph.golden.json b/projectgraph/testdata/python/src-layout/graph.golden.json index 7373e1be..902da6fe 100644 --- a/projectgraph/testdata/python/src-layout/graph.golden.json +++ b/projectgraph/testdata/python/src-layout/graph.golden.json @@ -102,7 +102,7 @@ "services/api/src/api/core.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 4, "edges": 2, diff --git a/projectgraph/testdata/python/src-layout/indexes.json b/projectgraph/testdata/python/src-layout/indexes.json index 0da84fa5..f02eabda 100644 --- a/projectgraph/testdata/python/src-layout/indexes.json +++ b/projectgraph/testdata/python/src-layout/indexes.json @@ -1,16 +1,16 @@ [ { - "version": "16", "path": "services/api/src/api/__init__.py", "language": "python", + "version": "17", "path": "services/api/src/api/__init__.py", "language": "python", "definitions": [{"key": "services/api/src/api/__init__.py", "kind": "module", "name": "__init__", "start_line": 1, "end_line": 1, "exported": true}] }, { - "version": "16", "path": "services/api/src/api/cli.py", "language": "python", + "version": "17", "path": "services/api/src/api/cli.py", "language": "python", "imports": [{"line": 1, "module": "api.core", "item": "run", "alias": "run", "kind": "static", "namespace": "api//core//run"}], "definitions": [{"key": "services/api/src/api/cli.py", "kind": "module", "name": "cli", "start_line": 1, "end_line": 2, "exported": true}], "calls": [{"caller": "services/api/src/api/cli.py", "line": 2, "column": 1, "callee": "run", "targets": ["api//core//run"], "kind": "call"}] }, { - "version": "16", "path": "services/api/src/api/core.py", "language": "python", + "version": "17", "path": "services/api/src/api/core.py", "language": "python", "definitions": [ {"key": "services/api/src/api/core.py", "kind": "module", "name": "core", "start_line": 1, "end_line": 2, "exported": true}, {"key": "services/api/src/api/core.py//run", "kind": "function", "name": "run", "start_line": 1, "end_line": 2, "exported": true} diff --git a/projectgraph/testdata/python/sys-path/graph.golden.json b/projectgraph/testdata/python/sys-path/graph.golden.json index de34821d..59da0cda 100644 --- a/projectgraph/testdata/python/sys-path/graph.golden.json +++ b/projectgraph/testdata/python/sys-path/graph.golden.json @@ -84,7 +84,7 @@ "lib/shared.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 2, "nodes": 3, "edges": 2, diff --git a/projectgraph/testdata/python/sys-path/indexes.json b/projectgraph/testdata/python/sys-path/indexes.json index d8120327..be34d343 100644 --- a/projectgraph/testdata/python/sys-path/indexes.json +++ b/projectgraph/testdata/python/sys-path/indexes.json @@ -1,12 +1,12 @@ [ { - "version": "16", "path": "app/main.py", "language": "python", + "version": "17", "path": "app/main.py", "language": "python", "imports": [{"line": 1, "module": "shared", "alias": "shared", "kind": "static", "namespace": "shared"}], "definitions": [{"key": "app/main.py", "kind": "module", "name": "main", "start_line": 1, "end_line": 2, "exported": true}], "calls": [{"caller": "app/main.py", "line": 2, "column": 1, "callee": "shared.go", "targets": ["shared//go"], "receiver": "shared", "method": "go", "kind": "call"}] }, { - "version": "16", "path": "lib/shared.py", "language": "python", + "version": "17", "path": "lib/shared.py", "language": "python", "definitions": [ {"key": "lib/shared.py", "kind": "module", "name": "shared", "start_line": 1, "end_line": 2, "exported": true}, {"key": "lib/shared.py//go", "kind": "function", "name": "go", "start_line": 1, "end_line": 2, "exported": true} diff --git a/projectgraph/testdata/python/wildcard/graph.golden.json b/projectgraph/testdata/python/wildcard/graph.golden.json index d7824985..ddb2d13e 100644 --- a/projectgraph/testdata/python/wildcard/graph.golden.json +++ b/projectgraph/testdata/python/wildcard/graph.golden.json @@ -114,7 +114,7 @@ "util.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 2, "nodes": 4, "edges": 3, diff --git a/projectgraph/testdata/python/wildcard/indexes.json b/projectgraph/testdata/python/wildcard/indexes.json index 6178747d..78f16764 100644 --- a/projectgraph/testdata/python/wildcard/indexes.json +++ b/projectgraph/testdata/python/wildcard/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "main.py", "language": "python", + "version": "17", "path": "main.py", "language": "python", "imports": [{"line": 1, "module": "util", "wildcard": true, "kind": "static", "namespace": "util"}], "definitions": [{"key": "main.py", "kind": "module", "name": "main", "start_line": 1, "end_line": 3, "exported": true}], "calls": [ @@ -9,7 +9,7 @@ ] }, { - "version": "16", "path": "util.py", "language": "python", + "version": "17", "path": "util.py", "language": "python", "definitions": [ {"key": "util.py", "kind": "module", "name": "util", "start_line": 1, "end_line": 4, "exported": true}, {"key": "util.py//helper", "kind": "function", "name": "helper", "start_line": 1, "end_line": 2, "exported": true}, diff --git a/projectgraph/testdata/roots/extra-rules/graph.golden.json b/projectgraph/testdata/roots/extra-rules/graph.golden.json index e4e11268..3c83f193 100644 --- a/projectgraph/testdata/roots/extra-rules/graph.golden.json +++ b/projectgraph/testdata/roots/extra-rules/graph.golden.json @@ -108,7 +108,7 @@ "views.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 7, "edges": 0, diff --git a/projectgraph/testdata/roots/extra-rules/indexes.json b/projectgraph/testdata/roots/extra-rules/indexes.json index e0415772..7585eea1 100644 --- a/projectgraph/testdata/roots/extra-rules/indexes.json +++ b/projectgraph/testdata/roots/extra-rules/indexes.json @@ -1,10 +1,10 @@ [ { - "version": "16", "path": "app/users/route.ts", "language": "typescript", + "version": "17", "path": "app/users/route.ts", "language": "typescript", "definitions": [{"key": "app/users/route.ts", "kind": "module", "name": "route", "start_line": 1, "end_line": 1, "exported": true}] }, { - "version": "16", "path": "src/handler.js", "language": "javascript", + "version": "17", "path": "src/handler.js", "language": "javascript", "definitions": [ {"key": "src/handler.js", "kind": "module", "name": "handler", "start_line": 1, "end_line": 4, "exported": true}, {"key": "src/handler.js//Handler", "kind": "class", "name": "Handler", "start_line": 1, "end_line": 4, "exported": true}, @@ -13,7 +13,7 @@ ] }, { - "version": "16", "path": "views.py", "language": "python", + "version": "17", "path": "views.py", "language": "python", "definitions": [ {"key": "views.py", "kind": "module", "name": "views", "start_line": 1, "end_line": 3, "exported": true}, {"key": "views.py//index", "kind": "function", "name": "index", "start_line": 2, "end_line": 3, "exported": true, "decorators": ["@app.route"]} diff --git a/projectgraph/testdata/roots/library-surface/graph.golden.json b/projectgraph/testdata/roots/library-surface/graph.golden.json index 8a6ac8b6..f6f403dd 100644 --- a/projectgraph/testdata/roots/library-surface/graph.golden.json +++ b/projectgraph/testdata/roots/library-surface/graph.golden.json @@ -84,7 +84,7 @@ "lib/test_api.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 2, "nodes": 5, "edges": 0, diff --git a/projectgraph/testdata/roots/library-surface/indexes.json b/projectgraph/testdata/roots/library-surface/indexes.json index a3d9cf18..d8c9bd86 100644 --- a/projectgraph/testdata/roots/library-surface/indexes.json +++ b/projectgraph/testdata/roots/library-surface/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "lib/api.py", "language": "python", + "version": "17", "path": "lib/api.py", "language": "python", "definitions": [ {"key": "lib/api.py", "kind": "module", "name": "api", "start_line": 1, "end_line": 4, "exported": true}, {"key": "lib/api.py//public", "kind": "function", "name": "public", "start_line": 1, "end_line": 2, "exported": true}, @@ -8,7 +8,7 @@ ] }, { - "version": "16", "path": "lib/test_api.py", "language": "python", + "version": "17", "path": "lib/test_api.py", "language": "python", "definitions": [ {"key": "lib/test_api.py", "kind": "module", "name": "test_api", "start_line": 1, "end_line": 2, "exported": true}, {"key": "lib/test_api.py//test_public", "kind": "function", "name": "test_public", "start_line": 1, "end_line": 2, "exported": true} diff --git a/projectgraph/testdata/roots/main-guard/graph.golden.json b/projectgraph/testdata/roots/main-guard/graph.golden.json index 0673501b..0a09c277 100644 --- a/projectgraph/testdata/roots/main-guard/graph.golden.json +++ b/projectgraph/testdata/roots/main-guard/graph.golden.json @@ -62,7 +62,7 @@ "lib.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 2, "nodes": 4, "edges": 1, diff --git a/projectgraph/testdata/roots/main-guard/indexes.json b/projectgraph/testdata/roots/main-guard/indexes.json index 35a00d96..04d6e576 100644 --- a/projectgraph/testdata/roots/main-guard/indexes.json +++ b/projectgraph/testdata/roots/main-guard/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "app.py", "language": "python", "main_guard": true, + "version": "17", "path": "app.py", "language": "python", "main_guard": true, "definitions": [ {"key": "app.py", "kind": "module", "name": "app", "start_line": 1, "end_line": 5, "exported": true}, {"key": "app.py//run", "kind": "function", "name": "run", "start_line": 1, "end_line": 2, "exported": true} @@ -8,7 +8,7 @@ "calls": [{"caller": "app.py", "line": 5, "column": 5, "callee": "run", "targets": ["app.py//run"], "kind": "call"}] }, { - "version": "16", "path": "lib.py", "language": "python", + "version": "17", "path": "lib.py", "language": "python", "definitions": [ {"key": "lib.py", "kind": "module", "name": "lib", "start_line": 1, "end_line": 2, "exported": true}, {"key": "lib.py//lib", "kind": "function", "name": "lib", "start_line": 1, "end_line": 2, "exported": true} diff --git a/projectgraph/testdata/roots/package-json-bin/graph.golden.json b/projectgraph/testdata/roots/package-json-bin/graph.golden.json index f2c54528..c1e56c91 100644 --- a/projectgraph/testdata/roots/package-json-bin/graph.golden.json +++ b/projectgraph/testdata/roots/package-json-bin/graph.golden.json @@ -66,7 +66,7 @@ "lib/run.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 2, "nodes": 3, "edges": 2, diff --git a/projectgraph/testdata/roots/package-json-bin/indexes.json b/projectgraph/testdata/roots/package-json-bin/indexes.json index 36885795..3195991b 100644 --- a/projectgraph/testdata/roots/package-json-bin/indexes.json +++ b/projectgraph/testdata/roots/package-json-bin/indexes.json @@ -1,12 +1,12 @@ [ { - "version": "16", "path": "bin/cli.js", "language": "javascript", + "version": "17", "path": "bin/cli.js", "language": "javascript", "imports": [{"line": 1, "module": "../lib/run", "item": "run", "alias": "run", "kind": "static", "namespace": "..//lib//run//run"}], "definitions": [{"key": "bin/cli.js", "kind": "module", "name": "cli", "start_line": 1, "end_line": 2, "exported": true}], "calls": [{"caller": "bin/cli.js", "line": 2, "column": 1, "callee": "run", "targets": ["..//lib//run//run"], "kind": "call"}] }, { - "version": "16", "path": "lib/run.js", "language": "javascript", + "version": "17", "path": "lib/run.js", "language": "javascript", "exports": [{"name": "run", "target": "lib/run.js//run", "line": 1}], "definitions": [ {"key": "lib/run.js", "kind": "module", "name": "run", "start_line": 1, "end_line": 1, "exported": true}, diff --git a/projectgraph/testdata/roots/package-json-main/graph.golden.json b/projectgraph/testdata/roots/package-json-main/graph.golden.json index da558b55..7641722c 100644 --- a/projectgraph/testdata/roots/package-json-main/graph.golden.json +++ b/projectgraph/testdata/roots/package-json-main/graph.golden.json @@ -158,7 +158,7 @@ "parser.js": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 4, "nodes": 9, "edges": 3, diff --git a/projectgraph/testdata/roots/package-json-main/indexes.json b/projectgraph/testdata/roots/package-json-main/indexes.json index 074ce92e..a10ff344 100644 --- a/projectgraph/testdata/roots/package-json-main/indexes.json +++ b/projectgraph/testdata/roots/package-json-main/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", + "version": "17", "path": "cli.js", "language": "javascript", "exports": [ @@ -30,7 +30,7 @@ ] }, { - "version": "16", + "version": "17", "path": "fmt.js", "language": "javascript", "exports": [ @@ -60,7 +60,7 @@ ] }, { - "version": "16", + "version": "17", "path": "index.js", "language": "javascript", "exports": [ @@ -104,7 +104,7 @@ ] }, { - "version": "16", + "version": "17", "path": "parser.js", "language": "javascript", "exports": [ diff --git a/projectgraph/testdata/roots/procfile-dockerfile/graph.golden.json b/projectgraph/testdata/roots/procfile-dockerfile/graph.golden.json index 0dc095cf..74f3eac4 100644 --- a/projectgraph/testdata/roots/procfile-dockerfile/graph.golden.json +++ b/projectgraph/testdata/roots/procfile-dockerfile/graph.golden.json @@ -114,7 +114,7 @@ "worker.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 6, "nodes": 7, "edges": 0, diff --git a/projectgraph/testdata/roots/procfile-dockerfile/indexes.json b/projectgraph/testdata/roots/procfile-dockerfile/indexes.json index 2ae71e39..ea4ada6f 100644 --- a/projectgraph/testdata/roots/procfile-dockerfile/indexes.json +++ b/projectgraph/testdata/roots/procfile-dockerfile/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", + "version": "17", "path": "app/wsgi.py", "language": "python", "definitions": [ @@ -23,7 +23,7 @@ ] }, { - "version": "16", + "version": "17", "path": "app/cli.py", "language": "python", "definitions": [ @@ -38,7 +38,7 @@ ] }, { - "version": "16", + "version": "17", "path": "app/__init__.py", "language": "python", "definitions": [ @@ -53,7 +53,7 @@ ] }, { - "version": "16", + "version": "17", "path": "worker.py", "language": "python", "definitions": [ @@ -68,7 +68,7 @@ ] }, { - "version": "16", + "version": "17", "path": "ignored.py", "language": "python", "definitions": [ @@ -83,7 +83,7 @@ ] }, { - "version": "16", + "version": "17", "path": "web/server.js", "language": "javascript", "definitions": [ diff --git a/projectgraph/testdata/roots/pyproject-scripts/graph.golden.json b/projectgraph/testdata/roots/pyproject-scripts/graph.golden.json index f26e273c..1dcb3143 100644 --- a/projectgraph/testdata/roots/pyproject-scripts/graph.golden.json +++ b/projectgraph/testdata/roots/pyproject-scripts/graph.golden.json @@ -151,7 +151,7 @@ "src/app/tool.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 5, "nodes": 10, "edges": 0, diff --git a/projectgraph/testdata/roots/pyproject-scripts/indexes.json b/projectgraph/testdata/roots/pyproject-scripts/indexes.json index eb785ce6..acf09334 100644 --- a/projectgraph/testdata/roots/pyproject-scripts/indexes.json +++ b/projectgraph/testdata/roots/pyproject-scripts/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", + "version": "17", "path": "src/app/cli.py", "language": "python", "definitions": [ @@ -23,7 +23,7 @@ ] }, { - "version": "16", + "version": "17", "path": "src/app/tool.py", "language": "python", "definitions": [ @@ -46,7 +46,7 @@ ] }, { - "version": "16", + "version": "17", "path": "src/app/plugin.py", "language": "python", "definitions": [ @@ -78,7 +78,7 @@ ] }, { - "version": "16", + "version": "17", "path": "src/app/legacy.py", "language": "python", "definitions": [ @@ -93,7 +93,7 @@ ] }, { - "version": "16", + "version": "17", "path": "src/app/ignored.py", "language": "python", "definitions": [ diff --git a/projectgraph/testdata/roots/setup-py/graph.golden.json b/projectgraph/testdata/roots/setup-py/graph.golden.json index 54508411..88781e51 100644 --- a/projectgraph/testdata/roots/setup-py/graph.golden.json +++ b/projectgraph/testdata/roots/setup-py/graph.golden.json @@ -76,7 +76,7 @@ "tool/main.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 2, "nodes": 4, "edges": 0, diff --git a/projectgraph/testdata/roots/setup-py/indexes.json b/projectgraph/testdata/roots/setup-py/indexes.json index 1236d66c..6dd37436 100644 --- a/projectgraph/testdata/roots/setup-py/indexes.json +++ b/projectgraph/testdata/roots/setup-py/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", + "version": "17", "path": "tool/main.py", "language": "python", "definitions": [ @@ -23,7 +23,7 @@ ] }, { - "version": "16", + "version": "17", "path": "tool/admin.py", "language": "python", "definitions": [ diff --git a/projectgraph/testdata/roots/test-file/graph.golden.json b/projectgraph/testdata/roots/test-file/graph.golden.json index 9456863f..c368e2b9 100644 --- a/projectgraph/testdata/roots/test-file/graph.golden.json +++ b/projectgraph/testdata/roots/test-file/graph.golden.json @@ -69,7 +69,7 @@ "tests/test_app.py": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 1, "nodes": 4, "edges": 0, diff --git a/projectgraph/testdata/roots/test-file/indexes.json b/projectgraph/testdata/roots/test-file/indexes.json index 13953dcc..c413c0c1 100644 --- a/projectgraph/testdata/roots/test-file/indexes.json +++ b/projectgraph/testdata/roots/test-file/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "tests/test_app.py", "language": "python", + "version": "17", "path": "tests/test_app.py", "language": "python", "definitions": [ {"key": "tests/test_app.py", "kind": "module", "name": "test_app", "start_line": 1, "end_line": 6, "exported": true}, {"key": "tests/test_app.py//TestApp", "kind": "class", "name": "TestApp", "start_line": 1, "end_line": 4, "exported": true}, diff --git a/projectgraph/testdata/typescript/js-specifier/graph.golden.json b/projectgraph/testdata/typescript/js-specifier/graph.golden.json index de92b13e..c634afa5 100644 --- a/projectgraph/testdata/typescript/js-specifier/graph.golden.json +++ b/projectgraph/testdata/typescript/js-specifier/graph.golden.json @@ -84,7 +84,7 @@ "src/util.ts": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 2, "nodes": 3, "edges": 2, diff --git a/projectgraph/testdata/typescript/js-specifier/indexes.json b/projectgraph/testdata/typescript/js-specifier/indexes.json index 59cbdd59..b86f5f29 100644 --- a/projectgraph/testdata/typescript/js-specifier/indexes.json +++ b/projectgraph/testdata/typescript/js-specifier/indexes.json @@ -1,12 +1,12 @@ [ { - "version": "16", "path": "src/main.ts", "language": "typescript", + "version": "17", "path": "src/main.ts", "language": "typescript", "imports": [{"line": 1, "module": "./util.js", "item": "f", "alias": "f", "kind": "static", "namespace": ".//util.js//f"}], "definitions": [{"key": "src/main.ts", "kind": "module", "name": "main", "start_line": 1, "end_line": 2, "exported": true}], "calls": [{"caller": "src/main.ts", "line": 2, "column": 1, "callee": "f", "targets": [".//util.js//f"], "kind": "call"}] }, { - "version": "16", "path": "src/util.ts", "language": "typescript", + "version": "17", "path": "src/util.ts", "language": "typescript", "exports": [{"name": "f", "target": "src/util.ts//f", "line": 1}], "definitions": [ {"key": "src/util.ts", "kind": "module", "name": "util", "start_line": 1, "end_line": 1, "exported": true}, diff --git a/projectgraph/testdata/typescript/tsconfig-paths/graph.golden.json b/projectgraph/testdata/typescript/tsconfig-paths/graph.golden.json index 41601674..6fd6935f 100644 --- a/projectgraph/testdata/typescript/tsconfig-paths/graph.golden.json +++ b/projectgraph/testdata/typescript/tsconfig-paths/graph.golden.json @@ -141,7 +141,7 @@ "src/main.ts": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 5, "edges": 4, diff --git a/projectgraph/testdata/typescript/tsconfig-paths/indexes.json b/projectgraph/testdata/typescript/tsconfig-paths/indexes.json index 7418d5d8..aa2919d4 100644 --- a/projectgraph/testdata/typescript/tsconfig-paths/indexes.json +++ b/projectgraph/testdata/typescript/tsconfig-paths/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "src/main.ts", "language": "typescript", + "version": "17", "path": "src/main.ts", "language": "typescript", "imports": [ {"line": 1, "module": "@app/server", "item": "serve", "alias": "serve", "kind": "static", "namespace": "@app//server//serve"}, {"line": 2, "module": "src/config", "item": "cfg", "alias": "cfg", "kind": "static", "namespace": "src//config//cfg"} @@ -12,7 +12,7 @@ ] }, { - "version": "16", "path": "src/app/server.ts", "language": "typescript", + "version": "17", "path": "src/app/server.ts", "language": "typescript", "exports": [{"name": "serve", "target": "src/app/server.ts//serve", "line": 1}], "definitions": [ {"key": "src/app/server.ts", "kind": "module", "name": "server", "start_line": 1, "end_line": 1, "exported": true}, @@ -20,7 +20,7 @@ ] }, { - "version": "16", "path": "src/config.ts", "language": "typescript", + "version": "17", "path": "src/config.ts", "language": "typescript", "exports": [{"name": "cfg", "target": "src/config.ts//cfg", "line": 1}], "definitions": [ {"key": "src/config.ts", "kind": "module", "name": "config", "start_line": 1, "end_line": 1, "exported": true}, diff --git a/projectgraph/testdata/typescript/type-only/graph.golden.json b/projectgraph/testdata/typescript/type-only/graph.golden.json index 2b8a3af9..426e70e6 100644 --- a/projectgraph/testdata/typescript/type-only/graph.golden.json +++ b/projectgraph/testdata/typescript/type-only/graph.golden.json @@ -100,7 +100,7 @@ "src/types.ts": "indexed" }, "stats": { - "version": "19", + "version": "20", "files": 3, "nodes": 5, "edges": 2, diff --git a/projectgraph/testdata/typescript/type-only/indexes.json b/projectgraph/testdata/typescript/type-only/indexes.json index 22df4bbc..30b979da 100644 --- a/projectgraph/testdata/typescript/type-only/indexes.json +++ b/projectgraph/testdata/typescript/type-only/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "16", "path": "src/main.ts", "language": "typescript", + "version": "17", "path": "src/main.ts", "language": "typescript", "imports": [ {"line": 1, "module": "./types", "item": "Opts", "alias": "Opts", "kind": "type-only", "namespace": ".//types//Opts"}, {"line": 2, "module": "./decl", "item": "decl", "alias": "decl", "kind": "static", "namespace": ".//decl//decl"} @@ -9,7 +9,7 @@ "calls": [{"caller": "src/main.ts", "line": 3, "column": 1, "callee": "decl", "targets": [".//decl//decl"], "kind": "call"}] }, { - "version": "16", "path": "src/types.ts", "language": "typescript", + "version": "17", "path": "src/types.ts", "language": "typescript", "exports": [{"name": "Opts", "target": "src/types.ts//Opts", "line": 1}], "definitions": [ {"key": "src/types.ts", "kind": "module", "name": "types", "start_line": 1, "end_line": 1, "exported": true}, @@ -17,7 +17,7 @@ ] }, { - "version": "16", "path": "src/decl.d.ts", "language": "typescript", + "version": "17", "path": "src/decl.d.ts", "language": "typescript", "exports": [{"name": "decl", "target": "src/decl.d.ts//decl", "line": 1}], "definitions": [ {"key": "src/decl.d.ts", "kind": "module", "name": "decl", "start_line": 1, "end_line": 1, "exported": true}, diff --git a/test/projectgraph/cases/go/dependency-build-files/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/dependency-build-files/summaries/go_github.com_acme_kit_v1.0.0.json index b87df19c..5337a2f6 100644 --- a/test/projectgraph/cases/go/dependency-build-files/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-build-files/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json b/test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json index 7bc7e100..074772c2 100644 --- a/test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/srv@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json b/test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json index e6392bf4..6f8105c5 100644 --- a/test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:example.com/markup@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-data-reads/summaries/go_std_go1.25.1.json b/test/projectgraph/cases/go/dependency-data-reads/summaries/go_std_go1.25.1.json index 12040ab2..c89c23bb 100644 --- a/test/projectgraph/cases/go/dependency-data-reads/summaries/go_std_go1.25.1.json +++ b/test/projectgraph/cases/go/dependency-data-reads/summaries/go_std_go1.25.1.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:std@go1.25.1", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json b/test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json index d9fa8639..3566de89 100644 --- a/test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/srv@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_a_v1.0.0.json b/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_a_v1.0.0.json index da9cc6d1..a9c224de 100644 --- a/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_a_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_a_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/a@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_b_v1.0.0.json b/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_b_v1.0.0.json index 84bf2d3d..6ad9c586 100644 --- a/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_b_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_b_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/b@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-generated-code/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/dependency-generated-code/summaries/go_github.com_acme_kit_v1.0.0.json index 989ceb3e..86f92fd5 100644 --- a/test/projectgraph/cases/go/dependency-generated-code/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-generated-code/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-generated-table/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/dependency-generated-table/summaries/go_github.com_acme_kit_v1.0.0.json index 69e5a026..e7e191ba 100644 --- a/test/projectgraph/cases/go/dependency-generated-table/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-generated-table/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-implements-interface/summaries/go_github.com_acme_dep_v1.0.0.json b/test/projectgraph/cases/go/dependency-implements-interface/summaries/go_github.com_acme_dep_v1.0.0.json index 9048c967..005492ee 100644 --- a/test/projectgraph/cases/go/dependency-implements-interface/summaries/go_github.com_acme_dep_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-implements-interface/summaries/go_github.com_acme_dep_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/dep@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-internal-call/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/dependency-internal-call/summaries/go_github.com_acme_kit_v1.0.0.json index b4d95adb..84318c82 100644 --- a/test/projectgraph/cases/go/dependency-internal-call/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-internal-call/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-internal-package/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/dependency-internal-package/summaries/go_github.com_acme_kit_v1.0.0.json index f2135c1e..22e76c32 100644 --- a/test/projectgraph/cases/go/dependency-internal-package/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-internal-package/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-module/summaries/go_github.com_acme_yamlish_v1.2.0.json b/test/projectgraph/cases/go/dependency-module/summaries/go_github.com_acme_yamlish_v1.2.0.json index b8911ade..0398174b 100644 --- a/test/projectgraph/cases/go/dependency-module/summaries/go_github.com_acme_yamlish_v1.2.0.json +++ b/test/projectgraph/cases/go/dependency-module/summaries/go_github.com_acme_yamlish_v1.2.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/yamlish@v1.2.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-no-unit/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/dependency-no-unit/summaries/go_github.com_acme_kit_v1.0.0.json index 222bc432..bdca26df 100644 --- a/test/projectgraph/cases/go/dependency-no-unit/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-no-unit/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-platform-files/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/dependency-platform-files/summaries/go_github.com_acme_kit_v1.0.0.json index 989ceb3e..86f92fd5 100644 --- a/test/projectgraph/cases/go/dependency-platform-files/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-platform-files/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-platform-results/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/dependency-platform-results/summaries/go_github.com_acme_kit_v1.0.0.json index 4504b1f5..c42f5344 100644 --- a/test/projectgraph/cases/go/dependency-platform-results/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-platform-results/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-result-types/summaries/go_example.com_markup_v1.0.0.json b/test/projectgraph/cases/go/dependency-result-types/summaries/go_example.com_markup_v1.0.0.json index f2a787c6..bfeea344 100644 --- a/test/projectgraph/cases/go/dependency-result-types/summaries/go_example.com_markup_v1.0.0.json +++ b/test/projectgraph/cases/go/dependency-result-types/summaries/go_example.com_markup_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:example.com/markup@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/dependency-result-types/summaries/go_std_go1.25.1.json b/test/projectgraph/cases/go/dependency-result-types/summaries/go_std_go1.25.1.json index 6ac542fd..b4fb1e85 100644 --- a/test/projectgraph/cases/go/dependency-result-types/summaries/go_std_go1.25.1.json +++ b/test/projectgraph/cases/go/dependency-result-types/summaries/go_std_go1.25.1.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:std@go1.25.1", "symbols": [ { diff --git a/test/projectgraph/cases/go/function-no-body/summaries/go_github.com_acme_fast_v1.0.0.json b/test/projectgraph/cases/go/function-no-body/summaries/go_github.com_acme_fast_v1.0.0.json index e9dbeab9..eabed758 100644 --- a/test/projectgraph/cases/go/function-no-body/summaries/go_github.com_acme_fast_v1.0.0.json +++ b/test/projectgraph/cases/go/function-no-body/summaries/go_github.com_acme_fast_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/fast@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/panic-value/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/panic-value/summaries/go_github.com_acme_kit_v1.0.0.json index c165d791..f88895b9 100644 --- a/test/projectgraph/cases/go/panic-value/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/panic-value/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/plugin-lookup-std/summaries/go_std_go1.25.1.json b/test/projectgraph/cases/go/plugin-lookup-std/summaries/go_std_go1.25.1.json index 5c50028c..ed364a81 100644 --- a/test/projectgraph/cases/go/plugin-lookup-std/summaries/go_std_go1.25.1.json +++ b/test/projectgraph/cases/go/plugin-lookup-std/summaries/go_std_go1.25.1.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:std@go1.25.1", "symbols": [ { diff --git a/test/projectgraph/cases/go/stdlib-callback-scope/summaries/go_github.com_acme_kit_v1.0.0.json b/test/projectgraph/cases/go/stdlib-callback-scope/summaries/go_github.com_acme_kit_v1.0.0.json index 08655323..9beca27c 100644 --- a/test/projectgraph/cases/go/stdlib-callback-scope/summaries/go_github.com_acme_kit_v1.0.0.json +++ b/test/projectgraph/cases/go/stdlib-callback-scope/summaries/go_github.com_acme_kit_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/kit@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/go/stdlib-callback/summaries/go_github.com_acme_ws_v1.0.0.json b/test/projectgraph/cases/go/stdlib-callback/summaries/go_github.com_acme_ws_v1.0.0.json index 27d42dbb..a559d532 100644 --- a/test/projectgraph/cases/go/stdlib-callback/summaries/go_github.com_acme_ws_v1.0.0.json +++ b/test/projectgraph/cases/go/stdlib-callback/summaries/go_github.com_acme_ws_v1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "go:github.com/acme/ws@v1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/javascript/builtin-chain/summaries/npm_plug_1.0.0.json b/test/projectgraph/cases/javascript/builtin-chain/summaries/npm_plug_1.0.0.json index c4c68898..3591fcec 100644 --- a/test/projectgraph/cases/javascript/builtin-chain/summaries/npm_plug_1.0.0.json +++ b/test/projectgraph/cases/javascript/builtin-chain/summaries/npm_plug_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "npm:plug@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/javascript/dependency-candidates/summaries/npm_store-kit_1.0.0.json b/test/projectgraph/cases/javascript/dependency-candidates/summaries/npm_store-kit_1.0.0.json index a52e9868..48500be2 100644 --- a/test/projectgraph/cases/javascript/dependency-candidates/summaries/npm_store-kit_1.0.0.json +++ b/test/projectgraph/cases/javascript/dependency-candidates/summaries/npm_store-kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "npm:store-kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/javascript/dependency-exports-import/summaries/npm_kit_1.0.0.json b/test/projectgraph/cases/javascript/dependency-exports-import/summaries/npm_kit_1.0.0.json index c59cf4f8..ef44014e 100644 --- a/test/projectgraph/cases/javascript/dependency-exports-import/summaries/npm_kit_1.0.0.json +++ b/test/projectgraph/cases/javascript/dependency-exports-import/summaries/npm_kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "npm:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/javascript/dependency-exports-require/summaries/npm_kit_1.0.0.json b/test/projectgraph/cases/javascript/dependency-exports-require/summaries/npm_kit_1.0.0.json index c59cf4f8..ef44014e 100644 --- a/test/projectgraph/cases/javascript/dependency-exports-require/summaries/npm_kit_1.0.0.json +++ b/test/projectgraph/cases/javascript/dependency-exports-require/summaries/npm_kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "npm:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/javascript/dependency-method-name/summaries/npm_plug_1.0.0.json b/test/projectgraph/cases/javascript/dependency-method-name/summaries/npm_plug_1.0.0.json index a14fade4..44fbae9f 100644 --- a/test/projectgraph/cases/javascript/dependency-method-name/summaries/npm_plug_1.0.0.json +++ b/test/projectgraph/cases/javascript/dependency-method-name/summaries/npm_plug_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "npm:plug@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/javascript/dependency-same-path/summaries/npm_util-kit_1.0.0.json b/test/projectgraph/cases/javascript/dependency-same-path/summaries/npm_util-kit_1.0.0.json index 2e8d0346..f4bb7739 100644 --- a/test/projectgraph/cases/javascript/dependency-same-path/summaries/npm_util-kit_1.0.0.json +++ b/test/projectgraph/cases/javascript/dependency-same-path/summaries/npm_util-kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "npm:util-kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/javascript/dependency-transitive/summaries/npm_lodash._reinterpolate_3.0.0.json b/test/projectgraph/cases/javascript/dependency-transitive/summaries/npm_lodash._reinterpolate_3.0.0.json index f3b26e6c..d2ad08c7 100644 --- a/test/projectgraph/cases/javascript/dependency-transitive/summaries/npm_lodash._reinterpolate_3.0.0.json +++ b/test/projectgraph/cases/javascript/dependency-transitive/summaries/npm_lodash._reinterpolate_3.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "npm:lodash._reinterpolate@3.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/javascript/dependency-transitive/summaries/npm_lodash_4.17.21.json b/test/projectgraph/cases/javascript/dependency-transitive/summaries/npm_lodash_4.17.21.json index 584208a0..7df354d3 100644 --- a/test/projectgraph/cases/javascript/dependency-transitive/summaries/npm_lodash_4.17.21.json +++ b/test/projectgraph/cases/javascript/dependency-transitive/summaries/npm_lodash_4.17.21.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "npm:lodash@4.17.21", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-compiled-module/summaries/pypi_speedy_1.0.0.json b/test/projectgraph/cases/python/dependency-compiled-module/summaries/pypi_speedy_1.0.0.json index 0d53d13f..5f6cd86a 100644 --- a/test/projectgraph/cases/python/dependency-compiled-module/summaries/pypi_speedy_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-compiled-module/summaries/pypi_speedy_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:speedy@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-dark-unit-record/summaries/pypi_PyYAML_6.0.1.json b/test/projectgraph/cases/python/dependency-dark-unit-record/summaries/pypi_PyYAML_6.0.1.json index 0a8677f0..5849dba5 100644 --- a/test/projectgraph/cases/python/dependency-dark-unit-record/summaries/pypi_PyYAML_6.0.1.json +++ b/test/projectgraph/cases/python/dependency-dark-unit-record/summaries/pypi_PyYAML_6.0.1.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:PyYAML@6.0.1", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-dark-unit/summaries/pypi_PyYAML_6.0.1.json b/test/projectgraph/cases/python/dependency-dark-unit/summaries/pypi_PyYAML_6.0.1.json index 71bf9342..516122d0 100644 --- a/test/projectgraph/cases/python/dependency-dark-unit/summaries/pypi_PyYAML_6.0.1.json +++ b/test/projectgraph/cases/python/dependency-dark-unit/summaries/pypi_PyYAML_6.0.1.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:PyYAML@6.0.1", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_badpkg_1.0.0.json b/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_badpkg_1.0.0.json index 65724707..f8a53885 100644 --- a/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_badpkg_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_badpkg_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:badpkg@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_okpkg_1.0.0.json b/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_okpkg_1.0.0.json index c6085da1..fe635f3d 100644 --- a/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_okpkg_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-failed-reached/summaries/pypi_okpkg_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:okpkg@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_badpkg_1.0.0.json b/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_badpkg_1.0.0.json index 65724707..f8a53885 100644 --- a/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_badpkg_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_badpkg_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:badpkg@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_okpkg_1.0.0.json b/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_okpkg_1.0.0.json index c6085da1..fe635f3d 100644 --- a/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_okpkg_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-failed-unreached/summaries/pypi_okpkg_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:okpkg@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-auth_2.0.0.json b/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-auth_2.0.0.json index 9a94e4f5..d43d7884 100644 --- a/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-auth_2.0.0.json +++ b/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-auth_2.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:google-auth@2.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-cloud-storage_2.1.0.json b/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-cloud-storage_2.1.0.json index 312a4125..ec782241 100644 --- a/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-cloud-storage_2.1.0.json +++ b/test/projectgraph/cases/python/dependency-namespace-two-dists/summaries/pypi_google-cloud-storage_2.1.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:google-cloud-storage@2.1.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-package-dir/summaries/pypi_outer_1.0.0.json b/test/projectgraph/cases/python/dependency-package-dir/summaries/pypi_outer_1.0.0.json index ec7f1191..4a512b50 100644 --- a/test/projectgraph/cases/python/dependency-package-dir/summaries/pypi_outer_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-package-dir/summaries/pypi_outer_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:outer@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-record-import-name/summaries/pypi_PyYAML_6.0.1.json b/test/projectgraph/cases/python/dependency-record-import-name/summaries/pypi_PyYAML_6.0.1.json index 71bf9342..516122d0 100644 --- a/test/projectgraph/cases/python/dependency-record-import-name/summaries/pypi_PyYAML_6.0.1.json +++ b/test/projectgraph/cases/python/dependency-record-import-name/summaries/pypi_PyYAML_6.0.1.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:PyYAML@6.0.1", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_requests_2.31.0.json b/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_requests_2.31.0.json index 18461637..e01c72f9 100644 --- a/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_requests_2.31.0.json +++ b/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_requests_2.31.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:requests@2.31.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_urllib3_2.0.7.json b/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_urllib3_2.0.7.json index 22b9b6ff..b9b5e47f 100644 --- a/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_urllib3_2.0.7.json +++ b/test/projectgraph/cases/python/dependency-site-packages/summaries/pypi_urllib3_2.0.7.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:urllib3@2.0.7", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-stdlib/summaries/python_std_3.12.3.json b/test/projectgraph/cases/python/dependency-stdlib/summaries/python_std_3.12.3.json index 720fcc94..717724a6 100644 --- a/test/projectgraph/cases/python/dependency-stdlib/summaries/python_std_3.12.3.json +++ b/test/projectgraph/cases/python/dependency-stdlib/summaries/python_std_3.12.3.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "python:std@3.12.3", "symbols": [ { diff --git a/test/projectgraph/cases/python/dependency-typed-return/summaries/pypi_dep_1.0.0.json b/test/projectgraph/cases/python/dependency-typed-return/summaries/pypi_dep_1.0.0.json index 4021d152..bf3c0a68 100644 --- a/test/projectgraph/cases/python/dependency-typed-return/summaries/pypi_dep_1.0.0.json +++ b/test/projectgraph/cases/python/dependency-typed-return/summaries/pypi_dep_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:dep@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/python/typed-base-subclass/summaries/pypi_dep_1.0.0.json b/test/projectgraph/cases/python/typed-base-subclass/summaries/pypi_dep_1.0.0.json index 4021d152..bf3c0a68 100644 --- a/test/projectgraph/cases/python/typed-base-subclass/summaries/pypi_dep_1.0.0.json +++ b/test/projectgraph/cases/python/typed-base-subclass/summaries/pypi_dep_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "pypi:dep@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/crate-bindings/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/crate-bindings/summaries/cargo_kit_1.0.0.json index 3e168d70..909d8788 100644 --- a/test/projectgraph/cases/rust/crate-bindings/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/crate-bindings/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/dependency-crate/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/dependency-crate/summaries/cargo_kit_1.0.0.json index 1ef40a7f..577a7214 100644 --- a/test/projectgraph/cases/rust/dependency-crate/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/dependency-crate/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/dependency-internal-call/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/dependency-internal-call/summaries/cargo_kit_1.0.0.json index d5029bbf..50b47245 100644 --- a/test/projectgraph/cases/rust/dependency-internal-call/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/dependency-internal-call/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/dependency-renamed/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/dependency-renamed/summaries/cargo_kit_1.0.0.json index d5029bbf..50b47245 100644 --- a/test/projectgraph/cases/rust/dependency-renamed/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/dependency-renamed/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/glob-use-dependency-module/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/glob-use-dependency-module/summaries/cargo_dep_1.2.0.json index 9e9f91b1..289d7721 100644 --- a/test/projectgraph/cases/rust/glob-use-dependency-module/summaries/cargo_dep_1.2.0.json +++ b/test/projectgraph/cases/rust/glob-use-dependency-module/summaries/cargo_dep_1.2.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:dep@1.2.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/glob-use/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/glob-use/summaries/cargo_dep_1.2.0.json index 9e9f91b1..289d7721 100644 --- a/test/projectgraph/cases/rust/glob-use/summaries/cargo_dep_1.2.0.json +++ b/test/projectgraph/cases/rust/glob-use/summaries/cargo_dep_1.2.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:dep@1.2.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/into-dependency/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/into-dependency/summaries/cargo_kit_1.0.0.json index 61ee1f2d..8f5ad891 100644 --- a/test/projectgraph/cases/rust/into-dependency/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/into-dependency/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/library-build-script/summaries/cargo_kit_1.0.0.json b/test/projectgraph/cases/rust/library-build-script/summaries/cargo_kit_1.0.0.json index 892b7283..3404902f 100644 --- a/test/projectgraph/cases/rust/library-build-script/summaries/cargo_kit_1.0.0.json +++ b/test/projectgraph/cases/rust/library-build-script/summaries/cargo_kit_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:kit@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/macro-export-module/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/macro-export-module/summaries/cargo_dep_1.2.0.json index f6470bbb..22af2b66 100644 --- a/test/projectgraph/cases/rust/macro-export-module/summaries/cargo_dep_1.2.0.json +++ b/test/projectgraph/cases/rust/macro-export-module/summaries/cargo_dep_1.2.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:dep@1.2.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/macro-import/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/macro-import/summaries/cargo_dep_1.2.0.json index 58d56bc3..b76022eb 100644 --- a/test/projectgraph/cases/rust/macro-import/summaries/cargo_dep_1.2.0.json +++ b/test/projectgraph/cases/rust/macro-import/summaries/cargo_dep_1.2.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:dep@1.2.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/trait-object-shadowed/summaries/cargo_dep_1.2.0.json index a0c68e22..80f550ed 100644 --- a/test/projectgraph/cases/rust/trait-object-shadowed/summaries/cargo_dep_1.2.0.json +++ b/test/projectgraph/cases/rust/trait-object-shadowed/summaries/cargo_dep_1.2.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:dep@1.2.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/trait-object/summaries/cargo_dep_1.2.0.json b/test/projectgraph/cases/rust/trait-object/summaries/cargo_dep_1.2.0.json index 37ffff09..9512f1ef 100644 --- a/test/projectgraph/cases/rust/trait-object/summaries/cargo_dep_1.2.0.json +++ b/test/projectgraph/cases/rust/trait-object/summaries/cargo_dep_1.2.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:dep@1.2.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/use-binding-path/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/use-binding-path/summaries/cargo_dep_1.0.0.json index 5bdf2f40..538baa79 100644 --- a/test/projectgraph/cases/rust/use-binding-path/summaries/cargo_dep_1.0.0.json +++ b/test/projectgraph/cases/rust/use-binding-path/summaries/cargo_dep_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:dep@1.0.0", "symbols": [ { diff --git a/test/projectgraph/cases/rust/use-dependency-item/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/use-dependency-item/summaries/cargo_dep_1.0.0.json index ed1c0b55..80465b0d 100644 --- a/test/projectgraph/cases/rust/use-dependency-item/summaries/cargo_dep_1.0.0.json +++ b/test/projectgraph/cases/rust/use-dependency-item/summaries/cargo_dep_1.0.0.json @@ -1,7 +1,7 @@ { "version": "3", - "index_version": "16", - "link_version": "19", + "index_version": "17", + "link_version": "20", "unit": "cargo:dep@1.0.0", "symbols": [ { diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index b65348dc..6b3bcc34 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -34,6 +34,7 @@ var graphHashes = map[string]string{ "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", "16/18": "bb92620bf9903e8d19b52b62198a0bfd69d67856f9db226112b2cedf537b312b", "16/19": "51ba3e925c628602b3a53c1a6e68b379022a410bb5b749062b3045fef082ac8f", + "17/20": "248b1b914d23ae12a707d447987524d999a65e6b15a524573ea8a32dc89437cd", } // resultHashes records the hash of the reach results of every case for @@ -52,6 +53,7 @@ var resultHashes = map[string]string{ "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", "16/18/5": "e0c6093df3a85353ee15c8a90ef7c2a441f23348ce80c9d49793c256bddeffc5", "16/19/5": "a935650f8e4d01c0abdceeefb3332bf779927693a1c48b28263da5365db752cd", + "17/20/5": "8beb5e0ae492aca9dfe0d79dba1e27ceaa5fd8d4850fe1b9cd97169d403f6e67", } // summaryHashes records the hash of the golden summaries of the cases for @@ -71,6 +73,7 @@ var summaryHashes = map[string]string{ "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", "16/18/3": "694efb5f72075356359031d449d7b4ecbdca2ddd5099dacbcd2adad745ac6e19", "16/19/3": "13c72bf9eb60fc0a38bc600906fc02028d5cb60cb2aefbec0f0721779c429d80", + "17/20/3": "187eb978ad6c7ea7d5d82177a57b3e7c097e38b6b1e313a5d88dcca221191ced", } func TestGraphHashFollowsVersion(t *testing.T) { From 5fc7953e8b3ac15d0428f89287968e9645d47516 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:36:43 +0000 Subject: [PATCH 21/86] fix(indexer): stop the grace timer when the store write returns The timer of graceContext kept running for its whole grace after the write returned. It now stops when the returned context ends. A store error of a run whose context is still live now comes back as it is, and only a write that fails after the deadline joins the error of the context. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/indexer.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/plugin/indexer/indexer.go b/plugin/indexer/indexer.go index aa69afbb..b7bb9c7d 100644 --- a/plugin/indexer/indexer.go +++ b/plugin/indexer/indexer.go @@ -368,7 +368,10 @@ func (r *run) flush(ctx context.Context, batch []pending) error { saveCtx, cancel := graceContext(ctx, saveGrace) defer cancel() if err := r.save(saveCtx, put, made); err != nil { - return errors.Join(err, ctx.Err()) + if ctx.Err() != nil { + return errors.Join(err, ctx.Err()) + } + return err } return parseErr } @@ -377,10 +380,20 @@ func (r *run) flush(ctx context.Context, batch []pending) error { // run ends. var saveGrace = 10 * time.Second -// graceContext returns a context that ends grace after ctx ends. +// graceContext returns a context that ends grace after ctx ends, or grace +// after the call if ctx has ended before it. The timer stops when the +// returned context ends, so a write that returns early holds nothing. func graceContext(ctx context.Context, grace time.Duration) (context.Context, context.CancelFunc) { out, cancel := context.WithCancel(context.WithoutCancel(ctx)) - stop := context.AfterFunc(ctx, func() { time.AfterFunc(grace, cancel) }) + stop := context.AfterFunc(ctx, func() { + t := time.NewTimer(grace) + defer t.Stop() + select { + case <-t.C: + cancel() + case <-out.Done(): + } + }) return out, func() { stop() cancel() From ad7b5e13b02586771503e50025c0a11c1877ed25 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:38:15 +0000 Subject: [PATCH 22/86] fix(golang): keep the type of a name that a function binds once The binding check turned a call into a record when the typed local came from an assignment and the binding in scope was a declaration with no type, as cl in cl := o.c; cl = &client{}; cl.do() or a named result. The record gave name edges to the methods of other types, so a target behind one of them read found. A function that binds the name once has one variable, so the assignment types it, and the call keeps its member edge. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/govalues.go | 27 +++++++++----- .../cases/go/typed-local-shadow/expect.yaml | 35 ++++++++++++++++++- .../go/typed-local-shadow/project/main.go | 18 ++++++++++ 3 files changed, 71 insertions(+), 9 deletions(-) diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 473e419c..700ca23a 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -241,6 +241,19 @@ func (p *projection) goLocal(caller, name string, at uint32) bool { // scope at the byte. An inner scope starts after an outer one that holds // it, so the scope with the last start is the binding. func (p *projection) goBinding(caller, name string, at uint32) (goScope, bool) { + var binding goScope + found := false + for _, s := range p.goScopesOf(caller)[name] { + if s.from <= at && at < s.to && (!found || s.from > binding.from) { + binding, found = s, true + } + } + return binding, found +} + +// goScopesOf returns the scopes of each local name of the caller, as +// goScopes gives them, once per caller. +func (p *projection) goScopesOf(caller string) map[string][]goScope { if p.goScoped == nil { p.goScoped = map[string]map[string][]goScope{} } @@ -249,14 +262,7 @@ func (p *projection) goBinding(caller, name string, at uint32) (goScope, bool) { locals = p.goScopes(p.syntax[caller]) p.goScoped[caller] = locals } - var binding goScope - found := false - for _, s := range locals[name] { - if s.from <= at && at < s.to && (!found || s.from > binding.from) { - binding, found = s, true - } - } - return binding, found + return locals } // goTypedAt reports whether the binding of a typed name in scope at the @@ -294,6 +300,11 @@ func (p *projection) goTypedAt(caller, name string, at uint32) bool { return origin == goAssignedOrigin case binding.from == origin: return true + case origin == goAssignedOrigin && len(p.goScopesOf(caller)[name]) == 1: + // The function binds the name once, so the assignment types that + // one variable, as cl in cl := o.c; cl = &client{} or a named + // result c in c = &client{}. + return true } for i, t := range def.Locals { if t.Name == name && i < len(origins) && origins[i] == binding.from { diff --git a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml index 42d1c2de..33b900f2 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml +++ b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml @@ -6,7 +6,10 @@ description: >- of x in scope at the call is the binding whose type the link step reads. Here it is not, so the call is an unknown-receiver record with the method name run, which gets a name edge to remote.run. The method value x.stop is a record of the same kind, with the name stop. A type of the first x gave not-found for - os/exec.LookPath. The call x.run() before the block keeps its member edge to local.run. + os/exec.LookPath. The call x.run() before the block keeps its member edge to local.run. pick binds r + once and types it by the assignment r = &remote{}, and named types its named result l by an assignment. + Each function binds the name once, so the assignment types that variable, and the call keeps its member + edge. A record there gave a name edge to the run of the other type, and a target behind it read found. roots: - key: main.go kind: main @@ -30,9 +33,23 @@ edges: kind: name confidence: low site: main.go:24 + - from: main.go//pick + to: main.go//remote//run + kind: member + confidence: high + site: main.go:38 + - from: main.go//named + to: main.go//local//run + kind: member + confidence: high + site: main.go:43 no_edges: - from: main.go//local//run to: os//exec//LookPath + - from: main.go//pick + to: main.go//local//run + - from: main.go//named + to: main.go//remote//run unresolved: - caller: main.go//main reason: no-dependency-source @@ -110,3 +127,19 @@ results: - caller: main.go//remote//stop site: main.go:16 reached: true + - target: os//Getwd + sources: [main.go//pick] + sources_only: true + status: not-found + call_sites: + - caller: main.go//local//run + site: main.go:10 + reached: false + - target: os//exec//LookPath + sources: [main.go//named] + sources_only: true + status: not-found + call_sites: + - caller: main.go//remote//run + site: main.go:14 + reached: false diff --git a/test/projectgraph/cases/go/typed-local-shadow/project/main.go b/test/projectgraph/cases/go/typed-local-shadow/project/main.go index 49e65bc8..807fb50c 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/project/main.go +++ b/test/projectgraph/cases/go/typed-local-shadow/project/main.go @@ -24,4 +24,22 @@ func main() { f := x.stop f() } + pick(opts{}) + named() +} + +type opts struct{ r *remote } + +func pick(o opts) { + r := o.r + if r == nil { + r = &remote{} + } + r.run() +} + +func named() (l *local) { + l = &local{} + l.run() + return } From 415c60eade48197e8f96036b801156c6e205d0e3 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:42:17 +0000 Subject: [PATCH 23/86] fix(golang): narrow the function-value records of fields and closures The link step counted every field name of every language as a member that can hold a function, so a call as n.Name() on a range variable made each target uncertain from a source when a struct had a field Name string. The Go index now marks a field of a type of data with Data, and only a Go field that can hold a function counts. A call of a local that the function binds once to a function literal, as f() after f := func() {...}, runs code that the index gives to the function already. The index now drops that call, so it has no record. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/calls.go | 3 ++ plugin/indexer/golang.go | 39 +++++++++++++- plugin/indexer/golang_test.go | 4 +- plugin/indexer/govalues.go | 51 +++++++++++++++++++ plugin/indexer/languages.go | 4 ++ plugin/indexer/testdata/golden/app.go.json | 12 +++-- projectgraph/values.go | 7 +-- .../cases/go/source-local-func/expect.yaml | 46 ++++++++++++++++- .../go/source-local-func/project/main.go | 18 +++++++ 9 files changed, 173 insertions(+), 11 deletions(-) diff --git a/plugin/indexer/calls.go b/plugin/indexer/calls.go index 0a60714a..220ef764 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -112,6 +112,9 @@ func (p *projection) addCalls(ctx context.Context) { Kind: kind, Args: p.arguments(key, ref.Site), } + if literal := p.rules().literalCall; literal != nil && literal(p, call) { + continue + } p.retargetTyped(ctx, &call, untyped[[2]uint32{ref.Site.StartLine, ref.Site.StartColumn}]) if shadowed := p.rules().shadowed; shadowed != nil && !shadowed(p, &call, ref.Site) { p.addRecord(call, fileindex.UnresolvedUnknownReceiver) diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 928f6af8..2d70482e 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -26,6 +26,7 @@ var goRules = languageRules{ exported: goExported, definitions: goDefinitions, typedAt: goTypedCall, + literalCall: goLiteralCall, visitors: []func(p *projection) visitor{referenceVisitor}, references: &referenceSyntax{ holders: map[string]string{ @@ -130,13 +131,49 @@ func (p *projection) goTypes(statement *sitter.Node) { continue } for _, field := range p.goStructFields(t) { + data := p.goDataField(field.ChildByFieldName("type")) for _, name := range childrenByField(field, "name") { - p.addDefinition(fileindex.Join(key, p.text(name)), fileindex.DefField, field) + p.addDefinition(fileindex.Join(key, p.text(name)), fileindex.DefField, field).Data = data } } } } +// goDataField reports whether a field of the type holds no function: a +// type that goDataType reads as data, or a type of the file whose +// declaration is such a type. A call x.f() can run the function value of a +// field f, so the link step reads the fields that can hold one. +func (p *projection) goDataField(t *sitter.Node) bool { + if t == nil { + return false + } + if p.goDataType(t) { + return true + } + declared := p.goFileType(t) + return declared != nil && p.goDataType(declared) +} + +// goFileType returns the type that a type declaration of the file gives a +// type name, as struct{...} for Config in type Config struct{...}. +func (p *projection) goFileType(t *sitter.Node) *sitter.Node { + if t.Type() != "type_identifier" { + return nil + } + name := p.text(t) + for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { + if statement.Type() != "type_declaration" { + continue + } + for _, spec := range ts.NamedChildren(statement) { + if spec.Type() == "type_spec" && p.text(spec.ChildByFieldName("name")) == name { + return spec.ChildByFieldName("type") + } + } + } + return nil +} + // goVarSpecs returns the specs of a var declaration. A declaration with // parentheses holds them in a list. func goVarSpecs(statement *sitter.Node) []*sitter.Node { diff --git a/plugin/indexer/golang_test.go b/plugin/indexer/golang_test.go index 11851842..32bd4055 100644 --- a/plugin/indexer/golang_test.go +++ b/plugin/indexer/golang_test.go @@ -125,8 +125,8 @@ func TestFileIndexGoValues(t *testing.T) { data bool values []string }{ - {key: "shop.go//Node//Data", kind: fileindex.DefField}, - {key: "shop.go//Node//kind", kind: fileindex.DefField}, + {key: "shop.go//Node//Data", kind: fileindex.DefField, data: true}, + {key: "shop.go//Node//kind", kind: fileindex.DefField, data: true}, {key: "shop.go//Max", kind: fileindex.DefConstant}, {key: "shop.go//Min", kind: fileindex.DefConstant}, {key: "shop.go//Next", kind: fileindex.DefConstant}, diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 700ca23a..61d95389 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -325,6 +325,57 @@ func goTypedCall(p *projection, call fileindex.Call) bool { return true } +// goLiteralCall reports whether a call names a local that the caller +// binds once, to a function literal, and never assigns again, as f in +// f := func() {...}; f(). The index gives the calls of the literal to the +// caller, so the call runs no code that the caller does not hold. An +// assignment or an address of the name can give it another function. +func goLiteralCall(p *projection, call fileindex.Call) bool { + name := call.Callee + if call.Receiver != "" || !isIdentifier(name) || len(p.goScopesOf(call.Caller)[name]) != 1 { + return false + } + if _, bound := p.goBinding(call.Caller, name, p.byteAt(call.Line, call.Column)); !bound { + return false + } + fn := p.syntax[call.Caller] + if fn == nil { + return false + } + literal, other, declared := false, false, 0 + walkBody(fn.ChildByFieldName("body"), nil, func(node *sitter.Node) { + switch node.Type() { + case "short_var_declaration", "var_spec": + left, right := node.ChildByFieldName("left"), node.ChildByFieldName("right") + var names []*sitter.Node + if node.Type() == "var_spec" { + names, right = childrenByField(node, "name"), node.ChildByFieldName("value") + } else if left != nil { + names = ts.NamedChildren(left) + } + var values []*sitter.Node + if right != nil { + values = ts.NamedChildren(right) + } + for i, n := range names { + if p.text(n) == name { + declared++ + literal = len(values) == len(names) && values[i].Type() == "func_literal" + } + } + case "assignment_statement", "inc_statement", "dec_statement": + if left := node.ChildByFieldName("left"); left != nil && slices.ContainsFunc(ts.NamedChildren(left), func(n *sitter.Node) bool { return p.text(n) == name }) { + other = true + } + case "unary_expression": + if operand := node.ChildByFieldName("operand"); goOperator(node) == "&" && operand != nil && p.text(operand) == name { + other = true + } + } + }) + return literal && !other && declared == 1 +} + // byteAt returns the start of the innermost named node at a line and a // column of a site, as a call or a record holds them. func (p *projection) byteAt(line, column int) uint32 { diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index d74eaca5..20cfe544 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -50,6 +50,10 @@ type languageRules struct { // the typed locals of a function with no extent, so an inner block can // bind the name of the receiver again. typedAt func(p *projection, call fileindex.Call) bool + // literalCall reports whether a call runs a function literal whose + // calls the index gives to the caller already, as f() after + // f := func() {...} in Go. Such a call needs no edge and no record. + literalCall func(p *projection, call fileindex.Call) bool // packageOf returns the package or the namespace of the file, which the // files of one unit share. diff --git a/plugin/indexer/testdata/golden/app.go.json b/plugin/indexer/testdata/golden/app.go.json index 3422b16a..5d23cafc 100644 --- a/plugin/indexer/testdata/golden/app.go.json +++ b/plugin/indexer/testdata/golden/app.go.json @@ -60,7 +60,8 @@ "start_line": 21, "end_line": 21, "exported": false, - "parent": "app.go//Server" + "parent": "app.go//Server", + "data": true }, { "key": "app.go//Server//name", @@ -69,7 +70,8 @@ "start_line": 21, "end_line": 21, "exported": false, - "parent": "app.go//Server" + "parent": "app.go//Server", + "data": true }, { "key": "app.go//Server//db", @@ -78,7 +80,8 @@ "start_line": 22, "end_line": 22, "exported": false, - "parent": "app.go//Server" + "parent": "app.go//Server", + "data": true }, { "key": "app.go//Server//cache", @@ -87,7 +90,8 @@ "start_line": 23, "end_line": 23, "exported": false, - "parent": "app.go//Server" + "parent": "app.go//Server", + "data": true }, { "key": "app.go//Box", diff --git a/projectgraph/values.go b/projectgraph/values.go index e3a0a6cc..d39cb65e 100644 --- a/projectgraph/values.go +++ b/projectgraph/values.go @@ -58,15 +58,16 @@ func (b *builder) recordValueCalls() { // callsValue reports whether a call with an unknown receiver can call a // function value: a name with no receiver, or a member that a field of the -// graph names. +// graph names. Only a field of a language with functionValues counts, and +// not one whose type the index shows to hold data, as Name string. func (b *builder) callsValue(u UnresolvedSite) bool { if u.Receiver == "" { return isIdentifier(u.Text) } if b.fieldNames == nil { b.fieldNames = map[string]bool{} - for _, def := range b.defs { - if def.Kind == fileindex.DefField { + for key, def := range b.defs { + if def.Kind == fileindex.DefField && !def.Data && profileOf(b.languageOf(key)).functionValues { b.fieldNames[def.Name] = true } } diff --git a/test/projectgraph/cases/go/source-local-func/expect.yaml b/test/projectgraph/cases/go/source-local-func/expect.yaml index 1d57a194..37db4f55 100644 --- a/test/projectgraph/cases/go/source-local-func/expect.yaml +++ b/test/projectgraph/cases/go/source-local-func/expect.yaml @@ -7,7 +7,11 @@ description: >- each function as a value, so os.Remove is found through the reference. A search from run, each, fire, call or pick alone does not reach main. Each call there runs a function value that the search does not reach, so the call is a function-value record, which concerns every target in a search with a source. - Each target is then uncertain. A record with the name alone, as f or fn, gave not-found there. + Each target is then uncertain. A record with the name alone, as f or fn, gave not-found there. show + calls Name on a range variable of an interface. The field Name of user holds a string, so the call runs + no function value, and os.Getwd is not found from show. closure calls its local f, which holds a + function literal and nothing else. The index gives the calls of the literal to closure, so the call of f + is no record, and os.Getwd is not found from closure. roots: - key: main.go kind: main @@ -33,6 +37,9 @@ unresolved: - caller: main.go//run reason: no-dependency-source callback: true + - caller: main.go//show + reason: no-dependency-source + callback: true - caller: os//Args reason: no-dependency-source callback: true @@ -45,6 +52,9 @@ unresolved: - caller: os//Getwd reason: no-dependency-source callback: true + - caller: os//Hostname + reason: no-dependency-source + callback: true - caller: os//Remove reason: no-dependency-source callback: true @@ -82,6 +92,10 @@ unresolved: site: main.go:54 reason: unknown-receiver method: m + - caller: main.go//show + site: main.go:65 + reason: unknown-receiver + method: Name results: - target: os//Remove status: found @@ -154,3 +168,33 @@ results: uncertainties: - kind: unresolved-call site: main.go:54 + - target: os//Getwd + sources: [main.go//show] + sources_only: true + status: not-found + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false + - target: os//Hostname + sources: [main.go//closure] + sources_only: true + status: found + path: + - key: main.go//closure + - key: os//Hostname + site: main.go:70 + kind: direct + confidence: high + call_sites: + - caller: main.go//closure + site: main.go:70 + reached: true + - target: os//Getwd + sources: [main.go//closure] + sources_only: true + status: not-found + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false diff --git a/test/projectgraph/cases/go/source-local-func/project/main.go b/test/projectgraph/cases/go/source-local-func/project/main.go index a61e47ee..6aa69dd4 100644 --- a/test/projectgraph/cases/go/source-local-func/project/main.go +++ b/test/projectgraph/cases/go/source-local-func/project/main.go @@ -53,3 +53,21 @@ func chdir() { os.Chdir("/") } func pick(m map[string]func(), name string) { m[name]() } + +type user struct { + Name string +} + +type namer interface{ Name() string } + +func show(ns []namer) { + for _, n := range ns { + _ = n.Name() + } +} + +func closure() { + f := func() { os.Hostname() } + f() + defer f() +} From b0b9c747edeb462d3f09b5a09e8d360f43495fe1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:44:39 +0000 Subject: [PATCH 24/86] fix(golang): record a range over a function value of a declared type A range over a parameter, a var or a field of a function type calls the function value, as a call seq(yield) does. It had no record, so a search from a source read not-found where the call read uncertain. The index now gives such a range a record, which the link step makes a function-value record. The docs also give the field and closure rules of the previous commit. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 27 +++-- plugin/indexer/golang.go | 2 +- plugin/indexer/govalues.go | 110 ++++++++++++++++-- .../cases/go/range-over-func/expect.yaml | 41 ++++++- .../cases/go/range-over-func/project/main.go | 15 +++ 5 files changed, 173 insertions(+), 22 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 8c6c5266..50e7a88a 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -1078,11 +1078,21 @@ definition of the name reaches. A local of a declared function type, as `g` in `var g F = h`, has no record of its own. The function value has another name, so in Go the link step also gives each such site a `function-value` record. It does the same for a call of a member whose -receiver it does not know, as `h.fn()`, when a field of a type of the graph -has the name. In a search from the roots, the `reference` edge of the stored -function covers the call, and the `function-value` record concerns no -target. In a search with a source and in a summary, it concerns every -target. The case `go/source-local-func` shows each form. +receiver it does not know, as `h.fn()`, when a Go field of the graph has the +name and can hold a function. The index marks a field of a type of data, as +`Name string`, with `Data`, and such a field does not count. In a search +from the roots, the `reference` edge of the stored function covers the +call, and the `function-value` record concerns no target. In a search with +a source and in a summary, it concerns every target. + +A local that the function binds once, to a function literal, and never +assigns again, as `f` in `f := func() {...}; f()`, runs only the literal. +The index gives the calls of the literal to the function, so the call of +`f` is no call of the index and has no record. A range over a parameter, a +local or a field of a declared function type, as `seq` in +`func consume(seq func(func(int) bool)) { for range seq {} }`, calls the +function value, so it gets a `function-value` record too. The case +`go/source-local-func` shows each form. Three gaps stay. In each of them, a search with a source can read `not-found`. @@ -1090,9 +1100,10 @@ Three gaps stay. In each of them, a search with a source can read - A field of a type of a package that no unit gives can hold a function, and no definition names that field. So a call of it keeps only the record with the name. -- A range over a parameter or a local that holds a function, as - `for x := range seq`, has no record. The index does not know whether - `seq` holds a function or data. +- A range over a value whose type the index does not see as a function + type has no record: a local that a call gives, as `seq := all()`, or a + field that no struct of the file declares. The index does not know + whether the value is a function or data. - In Rust, a call of a parameter or a local keeps only the record with the name. diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 2d70482e..4761df6f 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -27,7 +27,7 @@ var goRules = languageRules{ definitions: goDefinitions, typedAt: goTypedCall, literalCall: goLiteralCall, - visitors: []func(p *projection) visitor{referenceVisitor}, + visitors: []func(p *projection) visitor{referenceVisitor, goRangeVisitor}, references: &referenceSyntax{ holders: map[string]string{ "argument_list": "", diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 61d95389..d9c70cf8 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -3,9 +3,11 @@ package indexer import ( "slices" + "github.com/safedep/code/core/ast" "github.com/safedep/code/fileindex" "github.com/safedep/code/langcode" "github.com/safedep/code/pkg/ts" + "github.com/safedep/code/plugin/callgraph" sitter "github.com/smacker/go-tree-sitter" ) @@ -215,6 +217,8 @@ func (p *projection) goPredeclared(name string) bool { // function is in scope, and the start of the block that declares it. type goScope struct { from, to, block uint32 + // typ is the declared type of a parameter or a var, or nil. + typ *sitter.Node } // goBlocks are the nodes whose extent is the scope of a name that a @@ -325,6 +329,96 @@ func goTypedCall(p *projection, call fileindex.Call) bool { return true } +// goRangeVisitor records each range over a function value whose code +// the index cannot name, as seq in func consume(seq func(func(int) bool)) +// { for range seq {} }. The range calls the value, as seq(yield) does. A +// search from the roots reaches the reference to the function that the +// value holds. A search from a source can miss it, so the range gets a +// record, which the link step makes a function-value record. The index +// reads a parameter or a var of a declared function type, and a field +// that a struct of the file declares with a function type. +func goRangeVisitor(p *projection) visitor { + return func(node *sitter.Node, kind string) { + if kind != "range_clause" { + return + } + value := node.ChildByFieldName("right") + for value != nil && value.Type() == "parenthesized_expression" { + value = value.NamedChild(0) + } + if value == nil || !p.goRangesValue(value) { + return + } + site := ast.GetNodePosition(value) + p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ + Caller: p.caller(p.innermost(value.StartByte()), site), + Line: int(site.StartLine), + Column: int(site.StartColumn), + Text: callgraph.ClipText(p.text(value)), + Reason: fileindex.UnresolvedComputedName, + }) + } +} + +// goRangesValue reports whether a range expression holds a function value +// of a declared function type: a local of the caller, or a field. +func (p *projection) goRangesValue(value *sitter.Node) bool { + switch value.Type() { + case "identifier": + binding, ok := p.goBinding(p.innermost(value.StartByte()), p.text(value), value.StartByte()) + return ok && binding.typ != nil && p.goFuncType(binding.typ, 0) + case "selector_expression": + return p.goFuncField(p.text(value.ChildByFieldName("field"))) + } + return false +} + +// goFuncType reports whether a type is a function type: a literal +// function type, a type of the package iter, as iter.Seq[int], or a type +// of the file whose declaration is a function type. +func (p *projection) goFuncType(t *sitter.Node, depth int) bool { + if t == nil || depth > maxReferenceSegments { + return false + } + switch t.Type() { + case "function_type": + return true + case "parenthesized_type": + return p.goFuncType(t.NamedChild(0), depth+1) + case "generic_type": + return p.goFuncType(t.ChildByFieldName("type"), depth+1) + case "qualified_type": + return p.goImportPath(p.text(t.ChildByFieldName("package"))) == "iter" + case "type_identifier": + return p.goFuncType(p.goFileType(t), depth+1) + } + return false +} + +// goFuncField reports whether a struct of the file declares a field of the +// name with a function type. +func (p *projection) goFuncField(name string) bool { + for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { + if statement.Type() != "type_declaration" { + continue + } + for _, spec := range ts.NamedChildren(statement) { + t := spec.ChildByFieldName("type") + if spec.Type() != "type_spec" || t == nil || t.Type() != "struct_type" { + continue + } + for _, field := range p.goStructFields(t) { + for _, n := range childrenByField(field, "name") { + if p.text(n) == name && p.goFuncType(field.ChildByFieldName("type"), 0) { + return true + } + } + } + } + } + return false +} + // goLiteralCall reports whether a call names a local that the caller // binds once, to a function literal, and never assigns again, as f in // f := func() {...}; f(). The index gives the calls of the literal to the @@ -414,7 +508,7 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { if function == nil || function.Type() != "function_declaration" && function.Type() != "method_declaration" { return locals } - add := func(nodes []*sitter.Node, from uint32, block *sitter.Node, redeclares bool) { + add := func(nodes []*sitter.Node, from uint32, block, typ *sitter.Node, redeclares bool) { start := goBlockStart(block) for _, n := range nodes { if n.Type() != "identifier" { @@ -426,7 +520,7 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { if redeclares && slices.ContainsFunc(locals[name], func(s goScope) bool { return s.block == start && s.to == block.EndByte() }) { continue } - locals[name] = append(locals[name], goScope{from: from, to: block.EndByte(), block: start}) + locals[name] = append(locals[name], goScope{from: from, to: block.EndByte(), block: start, typ: typ}) } } var walk func(node, block *sitter.Node, depth int) @@ -444,20 +538,20 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { } switch node.Type() { case "parameter_declaration", "variadic_parameter_declaration": - add(childrenByField(node, "name"), goParamOrigin(block), block, false) + add(childrenByField(node, "name"), goParamOrigin(block), block, node.ChildByFieldName("type"), false) case "short_var_declaration": - add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, true) + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, nil, true) case "var_spec", "const_spec": - add(childrenByField(node, "name"), node.EndByte(), block, false) + add(childrenByField(node, "name"), node.EndByte(), block, node.ChildByFieldName("type"), false) case "type_spec", "type_alias": - add(childrenByField(node, "name"), node.StartByte(), block, false) + add(childrenByField(node, "name"), node.StartByte(), block, nil, false) case "range_clause", "receive_statement": if goDeclares(node) { - add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, false) + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, nil, false) } case "type_switch_statement": if alias := node.ChildByFieldName("alias"); alias != nil { - add(ts.NamedChildren(alias), alias.EndByte(), block, false) + add(ts.NamedChildren(alias), alias.EndByte(), block, nil, false) } } for _, child := range ts.NamedChildren(node) { diff --git a/test/projectgraph/cases/go/range-over-func/expect.yaml b/test/projectgraph/cases/go/range-over-func/expect.yaml index 6785767f..4730f455 100644 --- a/test/projectgraph/cases/go/range-over-func/expect.yaml +++ b/test/projectgraph/cases/go/range-over-func/expect.yaml @@ -1,10 +1,13 @@ source: safedep:hand description: >- - main ranges over the function walk and over the method value t.All, as Go 1.23 allows. The range - calls the function, and the function calls the body of the loop through yield. So the range - expression is a reference, as an argument is, and os.Remove and os/exec.LookPath are found through it. A - range clause with no reference lost both. The range over a slice names no function. No code uses unused, - so os.Getwd is not found. + main ranges over the function walk and over the method value t.All, as Go 1.23 allows. The range calls + the function, and the function calls the body of the loop through yield. So the range expression is a + reference, as an argument is, and os.Remove and os/exec.LookPath are found through it. A range clause + with no reference lost both. The range over a slice names no function. No code uses unused, so os.Getwd + is not found. consume ranges over its parameter seq of a function type, and drain over the field each of + a function type. Each range calls a function value that a search from a source does not reach, so it is + a function-value record, and each target is uncertain from consume and from drain. A range with no + record read not-found there. roots: - key: main.go kind: main @@ -60,6 +63,12 @@ unresolved: site: main.go:17 reason: unknown-receiver method: yield + - caller: main.go//consume + site: main.go:41 + reason: function-value + - caller: main.go//drain + site: main.go:46 + reason: function-value results: - target: os//Remove status: found @@ -99,3 +108,25 @@ results: - caller: main.go//unused site: main.go:21 reached: false + - target: os//Remove + sources: [main.go//consume] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//walk + site: main.go:9 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:41 + - target: os//Getwd + sources: [main.go//drain] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//unused + site: main.go:21 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:46 diff --git a/test/projectgraph/cases/go/range-over-func/project/main.go b/test/projectgraph/cases/go/range-over-func/project/main.go index e4fb7dea..ed8522c7 100644 --- a/test/projectgraph/cases/go/range-over-func/project/main.go +++ b/test/projectgraph/cases/go/range-over-func/project/main.go @@ -32,3 +32,18 @@ func main() { _ = s } } + +type source struct { + each func(func(string) bool) +} + +func consume(seq func(func(string) bool)) { + for range seq { + } +} + +func drain(s source) { + for x := range s.each { + _ = x + } +} From 7a795cc97ca0a5a66e63d2b9246b86ec884a0b35 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:40:56 +0000 Subject: [PATCH 25/86] fix(projectgraph): link Python instance methods from the calls that make an instance A reference edge joined each Python class to its __call__, __getattr__, __getattribute__ and __get__. Any reference to the class reached them, as isinstance(x, Proxy) or a call of a static method, so a target that only these methods call read as found when no code made an instance. The edge now goes from each edge that makes an instance: a call of the class, through export hops and the package edge of a unit. A reference to a metaclass, a class with the written base type, makes an instance at the class statement, so it gets the edges too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 22 +-- projectgraph/definitions.go | 100 ++++++++++++++ projectgraph/link.go | 1 + projectgraph/profile.go | 8 ++ projectgraph/python.go | 29 +--- .../instance-methods-no-instance/expect.yaml | 125 ++++++++++++++++++ .../project/main.py | 47 +++++++ .../name-ambiguous-callable/expect.yaml | 10 +- .../python/name-ambiguous-getattr/expect.yaml | 10 +- .../unknown-receiver-callable/expect.yaml | 25 ++-- .../unknown-receiver-getattr/expect.yaml | 20 ++- 11 files changed, 323 insertions(+), 74 deletions(-) create mode 100644 test/projectgraph/cases/python/instance-methods-no-instance/expect.yaml create mode 100644 test/projectgraph/cases/python/instance-methods-no-instance/project/main.py diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 50e7a88a..fd4de097 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -288,14 +288,20 @@ call of the instance runs `__call__`. A read of an attribute runs define, and `__get__` of a descriptor in a class attribute. The code that calls or reads the instance often has an unknown receiver, or is code outside the graph. So a `reference` edge with the rule `implicit` joins -each Python class to the method of each of these names that an instance -of the class runs, from the class or from a base in its MRO. The code that -makes the instance reaches the class, so a search from the roots reaches -the method, as it reaches a stored function value. A call with an unknown -receiver keeps its record with the method name. The target is then -found. The edge proves only that an instance exists, and not that code -calls it. The cases -`python/unknown-receiver-callable` and `python/unknown-receiver-getattr` +each call that makes an instance of a Python class to the method of each +of these names that the instance runs, from the class or from a base in +its MRO. The call can reach the class through an export hop or the +`package` edge of a unit. The code that makes the instance holds it, so a +search from the roots reaches the method, as it reaches a stored function +value. A reference to the class alone, as in `isinstance(x, Proxy)`, or a +call of a static method, makes no instance and gets no edge. A class +statement with `metaclass=Meta` makes an instance of `Meta`, so a +reference to a class with the written base `type` gets the edges too. A +call with an unknown receiver keeps its record with the method name. The +target is then found. The edge proves only that an instance exists, and +not that code calls it. The cases `python/unknown-receiver-callable`, +`python/unknown-receiver-getattr` and +`python/instance-methods-no-instance` show it. A function that code binds to a module attribute, as `hooks.handler = wipe`, has a `reference` edge from that code, and the assignment has the flag `monkey-patch`, so it needs no rule of its own. diff --git a/projectgraph/definitions.go b/projectgraph/definitions.go index e4d86376..a2dd864b 100644 --- a/projectgraph/definitions.go +++ b/projectgraph/definitions.go @@ -2,6 +2,7 @@ package projectgraph import ( "cmp" + "maps" "slices" "strings" @@ -127,6 +128,105 @@ func (b *builder) linkConstructors(ix *fileindex.Index) { } } +// linkInstances adds a reference edge from each edge that makes an +// instance of a class to the method of each instance method name that the +// instance runs, from the class or from a base in its MRO. The code that +// makes the instance holds it, so a search reaches the method as it +// reaches a stored function value. A reference to the class alone, as in +// isinstance(x, C), makes no instance and gets no edge. An edge reaches the +// class through export hops and the package edge of a unit. +func (b *builder) linkInstances() { + str := b.edges.symbols.strings + hops := map[string][]string{} + for slot, v := range b.edges.slots { + if k := EdgeKind(str[v.kind]); k == EdgeExport || k == EdgePackage { + hops[str[slot.from]] = append(hops[str[slot.from]], str[slot.to]) + } + } + memo := map[symbol]instanceCalls{} + var out []Edge + for slot, v := range b.edges.slots { + kind := EdgeKind(str[v.kind]) + if kind == EdgeImport || kind == EdgeExport || kind == EdgePackage { + continue + } + m, ok := memo[slot.to] + if !ok { + m = b.instanceMethods(str[slot.to], hops) + memo[slot.to] = m + } + methods := m.call + if kind == EdgeReference { + methods = m.ref + } + site := Site{Path: str[slot.path], Line: int(slot.line), Column: int(slot.column)} + for _, key := range methods { + out = append(out, newEdge(str[slot.from], key, site, EdgeReference, resolverName(b.profileAt(key).code, ruleImplicit))) + } + } + slices.SortFunc(out, func(x, y Edge) int { + return cmp.Or(cmp.Compare(x.From, y.From), cmp.Compare(x.Site.Path, y.Site.Path), cmp.Compare(x.Site.Line, y.Site.Line), + cmp.Compare(x.Site.Column, y.Site.Column), cmp.Compare(x.To, y.To)) + }) + for _, e := range out { + b.addEdge(e) + } +} + +// instanceCalls holds the instance methods that a call of a key runs, and +// the ones that a reference to the key runs. Only a metaclass gives the +// second kind, because a class statement that names it makes an instance. +type instanceCalls struct{ call, ref []string } + +// instanceMethods returns the instance methods of each class that a key +// names through its hops. +func (b *builder) instanceMethods(key string, hops map[string][]string) (m instanceCalls) { + seen := map[string]bool{} + next := []string{key} + for depth := 0; len(next) > 0 && depth <= b.opts.MaxExportHops; depth++ { + var later []string + for _, k := range next { + if seen[k] { + continue + } + seen[k] = true + later = append(later, hops[k]...) + p := b.profileAt(k) + if b.kindOf(k) != NodeClass || len(p.instanceMethods) == 0 { + continue + } + meta := b.isMetaclass(k, p.metaclass) + for _, name := range slices.Sorted(maps.Keys(p.instanceMethods)) { + if method, ok := b.ownMethod(k, name); ok && b.isMethod(method) { + m.call = append(m.call, method) + if meta { + m.ref = append(m.ref, method) + } + } + } + } + next = later + } + return m +} + +// isMetaclass reports whether a class of its MRO names the metaclass base +// as written, as class Meta(type). The builtin base binds no name, so the +// written name tells it. +func (b *builder) isMetaclass(class, metaclass string) bool { + if metaclass == "" { + return false + } + for _, c := range b.mro(class) { + for _, rec := range b.records(c) { + if slices.Contains(rec.class.Bases, metaclass) { + return true + } + } + } + return false +} + // linkOverrides adds a reference edge from each class with a base of a // dependency to the method that an instance of the class runs for each // method name of the first-party classes of its MRO, as run of a subclass diff --git a/projectgraph/link.go b/projectgraph/link.go index a5ff34fb..32bac68e 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -419,6 +419,7 @@ func (b *builder) linkAll(ctx context.Context) error { } b.checkUnits() b.linkFields() + b.linkInstances() return nil } diff --git a/projectgraph/profile.go b/projectgraph/profile.go index 5473158a..0e03d158 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -179,6 +179,14 @@ type profile struct { // implicitMethods name the methods that the language calls with no // call of the name, as an operator of Kotlin. implicitMethods map[string]bool + // instanceMethods name the methods that an instance runs with no call + // of the name, as __call__ of Python. A call that makes an instance + // links them, and a reference to the class alone does not. + instanceMethods map[string]bool + // metaclass is the base of a class whose instances are classes, as + // type of Python. A class statement makes an instance of such a class, + // so each edge to it makes an instance. + metaclass string // forwardMethod names the method of a class that forwards a call that // the class has no method for to another type, as deref of Rust. A // declared type with it does not narrow the receiver. diff --git a/projectgraph/python.go b/projectgraph/python.go index 2a8f6dbb..f94ea897 100644 --- a/projectgraph/python.go +++ b/projectgraph/python.go @@ -32,34 +32,17 @@ var pythonProfile = profile{ importBindsRoot: true, moduleNames: true, starImports: true, - implicitMethods: pythonImplicit, - link: (*builder).linkInstanceMethods, + instanceMethods: pythonInstanceMethods, + metaclass: "type", } -// pythonImplicit names the methods that Python runs on an instance with no -// call of the name. A call of the instance runs __call__. A read of an -// attribute runs __getattribute__, __getattr__ for a name that the class +// pythonInstanceMethods names the methods that Python runs on an instance +// with no call of the name. A call of the instance runs __call__. A read of +// an attribute runs __getattribute__, __getattr__ for a name that the class // does not define, and __get__ of a descriptor in a class attribute. The // code that calls or reads the instance often has an unknown receiver, or // it is code outside the graph. No call edge then names these methods. -var pythonImplicit = map[string]bool{"__call__": true, "__getattr__": true, "__getattribute__": true, "__get__": true} - -// linkInstanceMethods adds a reference edge from each class to the method -// of each implicit name that an instance of the class runs, from the class -// or from a base in its MRO. Code that makes an instance reaches the class, -// so a search reaches these methods as it reaches a stored function value. -// Without the edge, a target that only such a method calls reads as -// not-found, as for a class attribute that holds an object with __call__. -func (b *builder) linkInstanceMethods(ix *fileindex.Index) { - names := slices.Sorted(maps.Keys(profileOf(ix.Language).implicitMethods)) - for _, c := range ix.Classes { - for _, name := range names { - if m, ok := b.ownMethod(c.Key, name); ok && b.isMethod(m) { - b.link(ix.Language, c.Key, m, b.siteOf(m), EdgeReference, ruleImplicit) - } - } - } -} +var pythonInstanceMethods = map[string]bool{"__call__": true, "__getattr__": true, "__getattribute__": true, "__get__": true} // pythonDependencies returns the requirements of the Python manifests of // the project. diff --git a/test/projectgraph/cases/python/instance-methods-no-instance/expect.yaml b/test/projectgraph/cases/python/instance-methods-no-instance/expect.yaml new file mode 100644 index 00000000..8af4de05 --- /dev/null +++ b/test/projectgraph/cases/python/instance-methods-no-instance/expect.yaml @@ -0,0 +1,125 @@ +source: safedep:hand +description: >- + Proxy defines __call__ and __getattr__, and Desc defines __get__. No code makes an instance of them: + check names them only in isinstance, and main calls a static method and reads a class attribute. So no + edge leads to these methods, and their targets are not-found. Made names the metaclass Meta, so its + class statement makes an instance of Meta. A call of Made runs Meta.__call__, so the reference to Meta + at the class statement leads to it and tempfile.mkdtemp is found. +roots: + - key: main.py + kind: main + rule: python.main-guard +exact_edges: true +edges: + - from: main.py + to: main.py//Meta + kind: reference + confidence: low + site: main.py:31 + - from: main.py + to: main.py//Meta//__call__ + kind: reference + confidence: low + site: main.py:31 + - from: main.py + to: main.py//main + kind: direct + confidence: high + site: main.py:47 + - from: main.py//Desc//__get__ + to: subprocess//run + kind: direct + confidence: high + site: main.py:23 + - from: main.py//Meta//__call__ + to: tempfile//mkdtemp + kind: direct + confidence: high + site: main.py:28 + - from: main.py//Proxy//__call__ + to: os//remove + kind: direct + confidence: high + site: main.py:11 + - from: main.py//Proxy//__getattr__ + to: shutil//rmtree + kind: direct + confidence: high + site: main.py:14 + - from: main.py//check + to: isinstance + kind: direct + confidence: high + site: main.py:36 + - from: main.py//check + to: main.py//Proxy + kind: reference + confidence: low + site: main.py:36 + - from: main.py//check + to: isinstance + kind: direct + confidence: high + site: main.py:36 + - from: main.py//check + to: main.py//Desc + kind: reference + confidence: low + site: main.py:36 + - from: main.py//main + to: main.py//check + kind: direct + confidence: high + site: main.py:40 + - from: main.py//main + to: main.py//Proxy//helper + kind: member + confidence: high + site: main.py:41 + - from: main.py//main + to: print + kind: direct + confidence: high + site: main.py:42 + - from: main.py//main + to: main.py//Made + kind: direct + confidence: high + site: main.py:43 +unresolved: [] +results: + - target: os//remove + status: not-found + call_sites: + - caller: main.py//Proxy//__call__ + site: main.py:11 + reached: false + - target: shutil//rmtree + status: not-found + call_sites: + - caller: main.py//Proxy//__getattr__ + site: main.py:14 + reached: false + - target: subprocess//run + status: not-found + call_sites: + - caller: main.py//Desc//__get__ + site: main.py:23 + reached: false + - target: tempfile//mkdtemp + status: found + path: + - key: main.py + root: main + - key: main.py//Meta//__call__ + site: main.py:31 + kind: reference + confidence: low + - key: tempfile//mkdtemp + site: main.py:28 + kind: direct + confidence: high + call_sites: + - caller: main.py//Meta//__call__ + site: main.py:28 + reached: true diff --git a/test/projectgraph/cases/python/instance-methods-no-instance/project/main.py b/test/projectgraph/cases/python/instance-methods-no-instance/project/main.py new file mode 100644 index 00000000..758e8d0c --- /dev/null +++ b/test/projectgraph/cases/python/instance-methods-no-instance/project/main.py @@ -0,0 +1,47 @@ +import os +import shutil +import subprocess +import tempfile + + +class Proxy: + LIMIT = 3 + + def __call__(self): + os.remove("x") + + def __getattr__(self, name): + shutil.rmtree("y") + + @staticmethod + def helper(): + return 1 + + +class Desc: + def __get__(self, obj, typ=None): + subprocess.run("z") + + +class Meta(type): + def __call__(cls, *a): + tempfile.mkdtemp() + + +class Made(metaclass=Meta): + pass + + +def check(x): + return isinstance(x, Proxy) or isinstance(x, Desc) + + +def main(): + check(1) + Proxy.helper() + print(Proxy.LIMIT) + Made() + + +if __name__ == "__main__": + main() diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml index 60539cb6..a13f5ee4 100644 --- a/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml +++ b/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml @@ -3,8 +3,8 @@ description: >- dispatch calls handle on the parameter handler. 24 classes define a method handle, so the site is over the cap of 20 and is ambiguous. H0 holds an object of Cb in its class attribute handle. main passes an H0, so the call runs Cb.__call__, which calls os.remove. That code is no definition of the name handle, so the site - keeps no method name and concerns every target. The class body of H0 makes the object, and a call of an - object of Cb runs Cb.__call__, so a reference edge joins Cb to Cb.__call__ and os.remove is found. No code + keeps no method name and concerns every target. The class body of H0 makes the object, so a reference edge + joins that call to Cb.__call__ and os.remove is found. No code calls shutil.rmtree, and the ambiguous site makes it uncertain and not not-found. roots: - key: main.py @@ -26,12 +26,8 @@ results: site: main.py:1 kind: import confidence: high - - key: handlers.py//Cb - site: handlers.py:8 - kind: direct - confidence: high - key: handlers.py//Cb//__call__ - site: handlers.py:4 + site: handlers.py:8 kind: reference confidence: low - key: os//remove diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml index d5d16ebf..c9a9ca8e 100644 --- a/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml @@ -3,8 +3,8 @@ description: >- dispatch calls handle on the parameter handler. 24 classes define a method handle, so the site is over the cap of 20 and is ambiguous. main passes an H0, which defines no handle. Its __getattr__ gives cleanup, which calls os.remove. That code is no definition of the name handle, so the site keeps no method name and - concerns every target. A read of an attribute of an H0 can run H0.__getattr__, so a reference edge joins H0 - to H0.__getattr__ and os.remove is found. No code calls shutil.rmtree, and the ambiguous site makes it + concerns every target. A read of an attribute of an H0 can run H0.__getattr__, so a reference edge joins the + call that makes the H0 to H0.__getattr__ and os.remove is found. No code calls shutil.rmtree, and the ambiguous site makes it uncertain and not not-found. roots: - key: main.py @@ -22,12 +22,8 @@ results: status: found path: - key: main.py - - key: handlers.py//H0 - site: main.py:6 - kind: direct - confidence: high - key: handlers.py//H0//__getattr__ - site: handlers.py:7 + site: main.py:6 kind: reference confidence: low - key: handlers.py//cleanup diff --git a/test/projectgraph/cases/python/unknown-receiver-callable/expect.yaml b/test/projectgraph/cases/python/unknown-receiver-callable/expect.yaml index 9035da73..a0c1f6a2 100644 --- a/test/projectgraph/cases/python/unknown-receiver-callable/expect.yaml +++ b/test/projectgraph/cases/python/unknown-receiver-callable/expect.yaml @@ -3,34 +3,29 @@ description: >- The class attribute handle of Handler holds an object of Cleaner, which inherits __call__ from Base. dispatch calls handle on its parameter, so the receiver is unknown and the site is a record with the method name handle. No definition of the name handle exists, and the call runs Base.__call__, which calls - os.remove. The class body of Handler makes the object, so the search reaches Cleaner, and a reference - edge joins Cleaner to the __call__ of its MRO. + os.remove. The class body of Handler makes the object, so a reference edge joins that call to the + __call__ of the MRO of Cleaner. roots: - key: main.py kind: manifest rule: procfile exact_edges: true edges: + - from: handlers.py + to: handlers.py//Base//__call__ + kind: reference + confidence: low + site: handlers.py:14 - from: handlers.py to: handlers.py//Cleaner kind: direct confidence: high site: handlers.py:14 - - from: handlers.py//Base - to: handlers.py//Base//__call__ - kind: reference - confidence: low - site: handlers.py:5 - from: handlers.py//Base//__call__ to: os//remove kind: direct confidence: high site: handlers.py:6 - - from: handlers.py//Cleaner - to: handlers.py//Base//__call__ - kind: reference - confidence: low - site: handlers.py:5 - from: main.py to: handlers.py kind: import @@ -60,12 +55,8 @@ results: site: main.py:1 kind: import confidence: high - - key: handlers.py//Cleaner - site: handlers.py:14 - kind: direct - confidence: high - key: handlers.py//Base//__call__ - site: handlers.py:5 + site: handlers.py:14 kind: reference confidence: low - key: os//remove diff --git a/test/projectgraph/cases/python/unknown-receiver-getattr/expect.yaml b/test/projectgraph/cases/python/unknown-receiver-getattr/expect.yaml index 97511892..aea0625b 100644 --- a/test/projectgraph/cases/python/unknown-receiver-getattr/expect.yaml +++ b/test/projectgraph/cases/python/unknown-receiver-getattr/expect.yaml @@ -3,19 +3,14 @@ description: >- Handler defines no handle, and its __getattr__ gives cleanup for any name. dispatch calls handle on its parameter, so the receiver is unknown and the site is a record with the method name handle. No definition of the name handle exists, and the call runs cleanup, which calls os.remove. main makes a Handler, and a - read of an attribute of a Handler can run Handler.__getattr__, so a reference edge joins Handler to - Handler.__getattr__. The function that __getattr__ returns is a reference of it. + read of an attribute of it can run Handler.__getattr__, so a reference edge joins the call that makes + the Handler to Handler.__getattr__. The function that __getattr__ returns is a reference of it. roots: - key: main.py kind: manifest rule: procfile exact_edges: true edges: - - from: handlers.py//Handler - to: handlers.py//Handler//__getattr__ - kind: reference - confidence: low - site: handlers.py:9 - from: handlers.py//Handler//__getattr__ to: handlers.py//cleanup kind: reference @@ -41,6 +36,11 @@ edges: kind: direct confidence: high site: main.py:8 + - from: main.py + to: handlers.py//Handler//__getattr__ + kind: reference + confidence: low + site: main.py:8 unresolved: - caller: main.py//dispatch site: main.py:5 @@ -51,12 +51,8 @@ results: status: found path: - key: main.py - - key: handlers.py//Handler - site: main.py:8 - kind: direct - confidence: high - key: handlers.py//Handler//__getattr__ - site: handlers.py:9 + site: main.py:8 kind: reference confidence: low - key: handlers.py//cleanup From ac0889135b7b32371e87683872c6269afab733dd Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:45:41 +0000 Subject: [PATCH 26/86] fix(rootmatch): reach the actions of a SvelteKit route and resolve $lib A route file of SvelteKit exports an object of actions, and SvelteKit calls each function of it by its name. The path rule marked the object, and no edge led from it to its functions, so their targets read as not-found. A path rule now marks each function under an exported object of its files. An import of $lib gave an external key with no record. The resolver now maps $lib to src/lib of the nearest package that depends on @sveltejs/kit. A svelte.config file can move the directory, so an import of $lib that names no file stays first party and gets a record. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 14 ++- projectgraph/modules/javascript.go | 26 +++++ projectgraph/modules/resolver.go | 7 +- projectgraph/rootmatch/framework.go | 19 +++- .../sveltekit-actions-lib/expect.yaml | 105 ++++++++++++++++++ .../project/package.json | 5 + .../sveltekit-actions-lib/project/server.js | 3 + .../project/src/lib/helper.js | 5 + .../project/src/routes/api/+server.js | 5 + .../project/src/routes/form/+page.server.ts | 10 ++ .../sveltekit-lib-moved/expect.yaml | 46 ++++++++ .../sveltekit-lib-moved/project/package.json | 5 + .../sveltekit-lib-moved/project/server.js | 3 + .../project/src/library/helper.js | 5 + .../project/src/routes/api/+server.js | 5 + .../project/svelte.config.js | 1 + 16 files changed, 259 insertions(+), 5 deletions(-) create mode 100644 test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml create mode 100644 test/projectgraph/cases/javascript/sveltekit-actions-lib/project/package.json create mode 100644 test/projectgraph/cases/javascript/sveltekit-actions-lib/project/server.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/lib/helper.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/api/+server.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/form/+page.server.ts create mode 100644 test/projectgraph/cases/javascript/sveltekit-lib-moved/expect.yaml create mode 100644 test/projectgraph/cases/javascript/sveltekit-lib-moved/project/package.json create mode 100644 test/projectgraph/cases/javascript/sveltekit-lib-moved/project/server.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-lib-moved/project/src/library/helper.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-lib-moved/project/src/routes/api/+server.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-lib-moved/project/svelte.config.js diff --git a/docs/projectgraph.md b/docs/projectgraph.md index fd4de097..339d926e 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -707,13 +707,25 @@ reads. The rules `node.sveltekit.route` and `node.astro.page` mark the route files of SvelteKit and the pages of Astro, as `+page.ts`, `+server.ts` and `src/pages/api/data.ts`, with their exports. They keep the fallback on too, because a framework can load a file by a convention -that a rule does not list. Nuxt gets no rule of its own: it imports the +that a rule does not list. Nuxt gets no rule of its own. It imports the code of `composables` and `utils` with no import statement, so no list of paths is complete. The cases `javascript/vue-route-dynamic-import`, `javascript/vue-alias-import`, `javascript/vue-import-meta-glob`, `javascript/nuxt-pages`, `javascript/sveltekit-routes`, `javascript/astro-pages` and `javascript/vue-vite-fallback` show each form. +A path rule also marks each function under an exported object of its +files, as `default` and `save` of `export const actions = {...}` in a +`+page.server.ts`. The framework calls such a function by its name, and no +edge leads from the object to it. A destructured export, as +`export const { GET } = handlers`, gives no export record in the index, so +its function is no root. The resolver maps the alias `$lib` of SvelteKit +to `src/lib` of the nearest package that depends on `@sveltejs/kit`. It +does not read `svelte.config.js`, which can move the directory. So an +import of `$lib` that names no file is first party, and it is a record +with the reason `no-definition`. The cases `javascript/sveltekit-actions-lib` +and `javascript/sveltekit-lib-moved` show both. + ## Sources The rules decide the roots by default. An engineer who knows the entry diff --git a/projectgraph/modules/javascript.go b/projectgraph/modules/javascript.go index 52124b24..59eabf45 100644 --- a/projectgraph/modules/javascript.go +++ b/projectgraph/modules/javascript.go @@ -13,6 +13,10 @@ import ( var ( jsExtensions = []string{".js", ".mjs", ".cjs", ".jsx", ".json"} tsExtensions = []string{".ts", ".tsx", ".mts", ".cts", ".d.ts", ".js", ".mjs", ".cjs", ".jsx", ".json"} + // svelteKit is the package of SvelteKit, and svelteKitLib its alias of + // the library directory of a project. + svelteKit = "@sveltejs/kit" + svelteKitLib = "$lib" tsRewrites = map[string][]string{".js": {".ts", ".tsx"}, ".jsx": {".tsx"}, ".mjs": {".mts"}, ".cjs": {".cts"}} ) @@ -91,6 +95,9 @@ func (j *JavaScript) Resolve(importer, spec string, typescript bool, form Form) } } } + if file, ok := j.svelteKitLib(importer, spec, typescript); ok { + return []string{file}, "sveltekit-lib", true + } name, sub := SplitPackage(spec) if pj := j.packages[name]; pj != nil { return j.Entries(pj, sub, typescript, form) @@ -98,6 +105,25 @@ func (j *JavaScript) Resolve(importer, spec string, typescript bool, form Form) return nil, "", false } +// svelteKitLib resolves the alias $lib of SvelteKit to src/lib of the +// nearest package that depends on @sveltejs/kit. A svelte.config file can +// move the directory, and the resolver does not read it. FirstParty then +// keeps the import first party, so its calls get a record. +func (j *JavaScript) svelteKitLib(importer, spec string, typescript bool) (string, bool) { + rest, ok := strings.CutPrefix(spec, svelteKitLib) + if !ok || rest != "" && !strings.HasPrefix(rest, "/") { + return "", false + } + for d := Dir(importer); ; d = Dir(d) { + if pj := j.manifests[d]; pj != nil && slices.Contains(pj.DependencyNames(), svelteKit) { + return j.relativeFile(path.Join(d, "src", "lib"), "."+rest, typescript) + } + if d == "" { + return "", false + } + } +} + // Package returns the files of a subpath of the package at the root of // the file set, as "." or "./fp", for the form, and how the resolver found // them. A root with no package.json gives its index file and its files by diff --git a/projectgraph/modules/resolver.go b/projectgraph/modules/resolver.go index d157c670..3761bca8 100644 --- a/projectgraph/modules/resolver.go +++ b/projectgraph/modules/resolver.go @@ -94,9 +94,10 @@ func (r jsResolver) Modules(importer, spec string, form Form) ([]string, string, return r.j.Resolve(importer, spec, r.typescript, form) } -// FirstParty reports whether a module is relative or starts with the name -// of a file or a directory of the project. +// FirstParty reports whether a module is relative, starts with the name of +// a file or a directory of the project, or starts with the alias $lib of +// SvelteKit, which names the library directory of the project. func (r jsResolver) FirstParty(spec string) bool { first, _, _ := strings.Cut(spec, "/") - return strings.HasPrefix(spec, ".") || r.j.files.HasName(first) + return strings.HasPrefix(spec, ".") || first == svelteKitLib || r.j.files.HasName(first) } diff --git a/projectgraph/rootmatch/framework.go b/projectgraph/rootmatch/framework.go index c47465f2..f5db183c 100644 --- a/projectgraph/rootmatch/framework.go +++ b/projectgraph/rootmatch/framework.go @@ -22,9 +22,26 @@ func (f *finder) matchPath(r rules.Rule) { if r.Kind == rules.Main { continue } - for _, key := range f.Exports(ix.Path) { + exports := f.Exports(ix.Path) + for _, key := range exports { f.add(r, key) } + f.addMembers(r, ix, exports) + } +} + +// addMembers marks each function under an exported object of the file. The +// framework calls a function of the object by its name, as the actions of +// a SvelteKit page, and no edge of the graph leads from the object to it. +func (f *finder) addMembers(r rules.Rule, ix *fileindex.Index, exports []string) { + objects := slices.DeleteFunc(slices.Clone(exports), func(key string) bool { + def, ok := f.defs[key] + return !ok || def.Kind != fileindex.DefVariable + }) + for def := range ix.CodeDefinitions() { + if slices.ContainsFunc(objects, func(key string) bool { return def.Key != key && fileindex.HasKeyPrefix(def.Key, key) }) { + f.add(r, def.Key) + } } } diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml new file mode 100644 index 00000000..4eb564ca --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml @@ -0,0 +1,105 @@ +source: safedep:hand +description: >- + SvelteKit calls each function of the exported object actions of a +page.server.ts by its name, and no + call of the graph names them. The rule node.sveltekit.route marks each function under an exported object + of a route file, so a1 and a2 are found. The API route imports helper by the alias $lib, which SvelteKit + maps to src/lib of the package that depends on @sveltejs/kit, so the call of helper reaches c1. +roots: + - key: server.js + kind: manifest + rule: node.package-json + - key: src/routes/api/+server.js + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/+server.js//POST + kind: framework + rule: node.sveltekit.route + - key: src/routes/form/+page.server.ts + kind: framework + rule: node.sveltekit.route + - key: src/routes/form/+page.server.ts//actions + kind: framework + rule: node.sveltekit.route + - key: src/routes/form/+page.server.ts//actions//default + kind: framework + rule: node.sveltekit.route + - key: src/routes/form/+page.server.ts//actions//save + kind: framework + rule: node.sveltekit.route +exact_edges: true +edges: + - from: server.js + to: plug//listen + kind: direct + confidence: high + site: server.js:3 + - from: src/lib/helper.js//helper + to: plug//c1 + kind: direct + confidence: high + site: src/lib/helper.js:4 + - from: src/routes/api/+server.js + to: src/lib/helper.js + kind: import + confidence: high + site: src/routes/api/+server.js:1 + - from: src/routes/api/+server.js//POST + to: src/lib/helper.js//helper + kind: direct + confidence: high + site: src/routes/api/+server.js:4 + - from: src/routes/form/+page.server.ts//actions//default + to: plug//a1 + kind: direct + confidence: high + site: src/routes/form/+page.server.ts:5 + - from: src/routes/form/+page.server.ts//actions//save + to: plug//a2 + kind: direct + confidence: high + site: src/routes/form/+page.server.ts:8 +unresolved: [] +results: + - target: plug//a1 + status: found + path: + - key: src/routes/form/+page.server.ts//actions//default + root: framework + - key: plug//a1 + site: src/routes/form/+page.server.ts:5 + kind: direct + confidence: high + call_sites: + - caller: src/routes/form/+page.server.ts//actions//default + site: src/routes/form/+page.server.ts:5 + reached: true + - target: plug//a2 + status: found + path: + - key: src/routes/form/+page.server.ts//actions//save + root: framework + - key: plug//a2 + site: src/routes/form/+page.server.ts:8 + kind: direct + confidence: high + call_sites: + - caller: src/routes/form/+page.server.ts//actions//save + site: src/routes/form/+page.server.ts:8 + reached: true + - target: plug//c1 + status: found + path: + - key: src/routes/api/+server.js//POST + root: framework + - key: src/lib/helper.js//helper + site: src/routes/api/+server.js:4 + kind: direct + confidence: high + - key: plug//c1 + site: src/lib/helper.js:4 + kind: direct + confidence: high + call_sites: + - caller: src/lib/helper.js//helper + site: src/lib/helper.js:4 + reached: true diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/package.json b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/package.json new file mode 100644 index 00000000..455a2c4b --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "sveltekit-actions-lib", + "main": "server.js", + "devDependencies": { "@sveltejs/kit": "^2.5.0" } +} diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/server.js b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/server.js new file mode 100644 index 00000000..fc1cd6f8 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/server.js @@ -0,0 +1,3 @@ +import { listen } from 'plug'; + +listen(3000); diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/lib/helper.js b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/lib/helper.js new file mode 100644 index 00000000..099b4c6b --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/lib/helper.js @@ -0,0 +1,5 @@ +import { c1 } from 'plug'; + +export function helper() { + return c1(); +} diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/api/+server.js b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/api/+server.js new file mode 100644 index 00000000..50167b76 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/api/+server.js @@ -0,0 +1,5 @@ +import { helper } from '$lib/helper'; + +export function POST() { + return helper(); +} diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/form/+page.server.ts b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/form/+page.server.ts new file mode 100644 index 00000000..b32490d7 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/form/+page.server.ts @@ -0,0 +1,10 @@ +import { a1, a2 } from 'plug'; + +export const actions = { + default: async ({ request }) => { + a1(request); + }, + save({ request }) { + a2(request); + }, +}; diff --git a/test/projectgraph/cases/javascript/sveltekit-lib-moved/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-lib-moved/expect.yaml new file mode 100644 index 00000000..dbd11fe4 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-lib-moved/expect.yaml @@ -0,0 +1,46 @@ +source: safedep:hand +description: >- + svelte.config.js moves the library directory of SvelteKit to src/library, and the resolver does not read + it. So $lib/helper names no file. The alias $lib names a directory of the project, so the import is a + record with the reason no-definition and not an external key, and c1 reads uncertain and not not-found. +roots: + - key: server.js + kind: manifest + rule: node.package-json + - key: src/routes/api/+server.js + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/+server.js//POST + kind: framework + rule: node.sveltekit.route +exact_edges: true +edges: + - from: server.js + to: plug//listen + kind: direct + confidence: high + site: server.js:3 + - from: src/library/helper.js//helper + to: plug//c1 + kind: direct + confidence: high + site: src/library/helper.js:4 + - from: src/routes/api/+server.js//POST + to: $lib//helper//helper + kind: direct + confidence: high + site: src/routes/api/+server.js:4 +unresolved: + - caller: src/routes/api/+server.js + site: src/routes/api/+server.js:1 + reason: no-definition +results: + - target: plug//c1 + status: uncertain + call_sites: + - caller: src/library/helper.js//helper + site: src/library/helper.js:4 + reached: false + uncertainties: + - kind: unresolved-call + site: src/routes/api/+server.js:1 diff --git a/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/package.json b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/package.json new file mode 100644 index 00000000..444b1f88 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "sveltekit-lib-moved", + "main": "server.js", + "devDependencies": { "@sveltejs/kit": "^2.5.0" } +} diff --git a/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/server.js b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/server.js new file mode 100644 index 00000000..fc1cd6f8 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/server.js @@ -0,0 +1,3 @@ +import { listen } from 'plug'; + +listen(3000); diff --git a/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/src/library/helper.js b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/src/library/helper.js new file mode 100644 index 00000000..099b4c6b --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/src/library/helper.js @@ -0,0 +1,5 @@ +import { c1 } from 'plug'; + +export function helper() { + return c1(); +} diff --git a/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/src/routes/api/+server.js b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/src/routes/api/+server.js new file mode 100644 index 00000000..50167b76 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/src/routes/api/+server.js @@ -0,0 +1,5 @@ +import { helper } from '$lib/helper'; + +export function POST() { + return helper(); +} diff --git a/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/svelte.config.js b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/svelte.config.js new file mode 100644 index 00000000..43fcf186 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-lib-moved/project/svelte.config.js @@ -0,0 +1 @@ +export default { kit: { files: { lib: 'src/library' } } }; From 7451215eb1c38c3a0cadef013fb9344f1aae0952 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:49:20 +0000 Subject: [PATCH 27/86] fix(indexer): record the exports of a JavaScript destructuring declaration export const { GET } = handlers gave no export record, so a route rule marked no root for GET, and a target that only its function calls read as not-found. Each name of the pattern now gets an export record whose target is the member that it takes, as handlers//GET, also through a nested pattern and a rename. A name of an array pattern or a rest element takes the object that holds it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 7 +- plugin/indexer/exports.go | 54 +++++++++ .../sveltekit-destructured-export/expect.yaml | 107 ++++++++++++++++++ .../project/package.json | 5 + .../project/server.js | 3 + .../project/src/routes/api/+server.ts | 13 +++ 6 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 test/projectgraph/cases/javascript/sveltekit-destructured-export/expect.yaml create mode 100644 test/projectgraph/cases/javascript/sveltekit-destructured-export/project/package.json create mode 100644 test/projectgraph/cases/javascript/sveltekit-destructured-export/project/server.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-destructured-export/project/src/routes/api/+server.ts diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 339d926e..5acefbcd 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -718,8 +718,11 @@ A path rule also marks each function under an exported object of its files, as `default` and `save` of `export const actions = {...}` in a `+page.server.ts`. The framework calls such a function by its name, and no edge leads from the object to it. A destructured export, as -`export const { GET } = handlers`, gives no export record in the index, so -its function is no root. The resolver maps the alias `$lib` of SvelteKit +`export const { GET } = handlers`, gives an export record whose target is +the member that the name takes, as `handlers//GET`, also through a nested +pattern and a rename. A name of an array pattern or a rest element takes +the object that holds it. The case `javascript/sveltekit-destructured-export` +shows each form. The resolver maps the alias `$lib` of SvelteKit to `src/lib` of the nearest package that depends on `@sveltejs/kit`. It does not read `svelte.config.js`, which can move the directory. So an import of `$lib` that names no file is first party, and it is a record diff --git a/plugin/indexer/exports.go b/plugin/indexer/exports.go index 7c0bc910..cb242ce0 100644 --- a/plugin/indexer/exports.go +++ b/plugin/indexer/exports.go @@ -1,6 +1,8 @@ package indexer import ( + "slices" + "github.com/safedep/code/fileindex" "github.com/safedep/code/pkg/ts" sitter "github.com/smacker/go-tree-sitter" @@ -77,6 +79,7 @@ func (p *projection) addExportStatement(statement *sitter.Node) { for _, name := range p.declaredNames(declaration) { p.export(named(name), name, line) } + p.exportPatterns(declaration, line) return } if value := statement.ChildByFieldName("value"); value != nil { @@ -141,6 +144,57 @@ func (p *projection) declaredNames(declaration *sitter.Node) []string { return nil } +// exportPatterns adds the exports of the names that a destructuring +// declaration binds, as GET in export const { GET } = handlers. The +// target of a name is the member of the object that it takes, as +// handlers//GET. A framework calls the function of an exported name, and +// without the record no root names the function. A name of an array +// pattern or a rest element takes no member that a key names, so its +// target is the object that holds it. A value that is no name of the file +// gives no target, and the call of the value holds its code. +func (p *projection) exportPatterns(declaration *sitter.Node, line int) { + if t := declaration.Type(); t != "lexical_declaration" && t != "variable_declaration" { + return + } + for _, declarator := range ts.NamedChildren(declaration) { + pattern, value := declarator.ChildByFieldName("name"), declarator.ChildByFieldName("value") + if pattern == nil || pattern.Type() == "identifier" || value == nil || value.Type() != "identifier" { + continue + } + p.exportPattern(pattern, []string{p.text(value)}, line) + } +} + +// exportPattern adds the exports of the names of a pattern whose value is +// the member at the path. A name of an object pattern takes the member of +// its own name, as GET in { GET }. +func (p *projection) exportPattern(pattern *sitter.Node, path []string, line int) { + switch pattern.Type() { + case "identifier": + p.export(p.text(pattern), fileindex.Join(path[0], path[1:]...), line) + case "shorthand_property_identifier_pattern": + name := p.text(pattern) + p.export(name, fileindex.Join(path[0], append(slices.Clip(path[1:]), name)...), line) + case "assignment_pattern", "object_assignment_pattern": + if left := pattern.ChildByFieldName("left"); left != nil { + p.exportPattern(left, path, line) + } + case "pair_pattern": + key, value := pattern.ChildByFieldName("key"), pattern.ChildByFieldName("value") + if key == nil || value == nil { + return + } + if key.Type() == "property_identifier" { + path = append(slices.Clip(path), p.text(key)) + } + p.exportPattern(value, path, line) + case "object_pattern", "array_pattern", "rest_pattern": + for _, child := range ts.NamedChildren(pattern) { + p.exportPattern(child, path, line) + } + } +} + // addExportAssignment adds the exports of a CommonJS assignment, as // module.exports = f, module.exports = { f } and exports.f = f. func (p *projection) addExportAssignment(statement, assignment *sitter.Node) { diff --git a/test/projectgraph/cases/javascript/sveltekit-destructured-export/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-destructured-export/expect.yaml new file mode 100644 index 00000000..2a647d36 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-destructured-export/expect.yaml @@ -0,0 +1,107 @@ +source: safedep:hand +description: >- + The route file exports GET, PUT and POST by a destructuring declaration from the object handlers, and + fallback from the array fallbacks. The index gives each name an export record whose target is the member + that it takes, as handlers//rest//PUT for PUT in { rest: { PUT } }, and POST for the rename post: POST. + A name of an array pattern takes the array. So the rule node.sveltekit.route marks each function, and + a1, a2 and a3 are found. The function of fallbacks runs its call in the module top level, so a4 is found. +roots: + - key: server.js + kind: manifest + rule: node.package-json + - key: src/routes/api/+server.ts + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/+server.ts//fallbacks + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/+server.ts//handlers//GET + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/+server.ts//handlers//post + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/+server.ts//handlers//rest//PUT + kind: framework + rule: node.sveltekit.route +exact_edges: true +edges: + - from: server.js + to: plug//listen + kind: direct + confidence: high + site: server.js:3 + - from: src/routes/api/+server.ts + to: plug//a4 + kind: direct + confidence: high + site: src/routes/api/+server.ts:10 + - from: src/routes/api/+server.ts//handlers//GET + to: plug//a1 + kind: direct + confidence: high + site: src/routes/api/+server.ts:4 + - from: src/routes/api/+server.ts//handlers//post + to: plug//a3 + kind: direct + confidence: high + site: src/routes/api/+server.ts:7 + - from: src/routes/api/+server.ts//handlers//rest//PUT + to: plug//a2 + kind: direct + confidence: high + site: src/routes/api/+server.ts:5 +unresolved: [] +results: + - target: plug//a1 + status: found + path: + - key: src/routes/api/+server.ts//handlers//GET + root: framework + - key: plug//a1 + site: src/routes/api/+server.ts:4 + kind: direct + confidence: high + call_sites: + - caller: src/routes/api/+server.ts//handlers//GET + site: src/routes/api/+server.ts:4 + reached: true + - target: plug//a2 + status: found + path: + - key: src/routes/api/+server.ts//handlers//rest//PUT + root: framework + - key: plug//a2 + site: src/routes/api/+server.ts:5 + kind: direct + confidence: high + call_sites: + - caller: src/routes/api/+server.ts//handlers//rest//PUT + site: src/routes/api/+server.ts:5 + reached: true + - target: plug//a3 + status: found + path: + - key: src/routes/api/+server.ts//handlers//post + root: framework + - key: plug//a3 + site: src/routes/api/+server.ts:7 + kind: direct + confidence: high + call_sites: + - caller: src/routes/api/+server.ts//handlers//post + site: src/routes/api/+server.ts:7 + reached: true + - target: plug//a4 + status: found + path: + - key: src/routes/api/+server.ts + root: framework + - key: plug//a4 + site: src/routes/api/+server.ts:10 + kind: direct + confidence: high + call_sites: + - caller: src/routes/api/+server.ts + site: src/routes/api/+server.ts:10 + reached: true diff --git a/test/projectgraph/cases/javascript/sveltekit-destructured-export/project/package.json b/test/projectgraph/cases/javascript/sveltekit-destructured-export/project/package.json new file mode 100644 index 00000000..068b7a75 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-destructured-export/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "sveltekit-destructured-export", + "main": "server.js", + "devDependencies": { "@sveltejs/kit": "^2.5.0" } +} diff --git a/test/projectgraph/cases/javascript/sveltekit-destructured-export/project/server.js b/test/projectgraph/cases/javascript/sveltekit-destructured-export/project/server.js new file mode 100644 index 00000000..fc1cd6f8 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-destructured-export/project/server.js @@ -0,0 +1,3 @@ +import { listen } from 'plug'; + +listen(3000); diff --git a/test/projectgraph/cases/javascript/sveltekit-destructured-export/project/src/routes/api/+server.ts b/test/projectgraph/cases/javascript/sveltekit-destructured-export/project/src/routes/api/+server.ts new file mode 100644 index 00000000..c8eccbe1 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-destructured-export/project/src/routes/api/+server.ts @@ -0,0 +1,13 @@ +import { a1, a2, a3, a4 } from 'plug'; + +const handlers = { + GET: async () => a1(), + rest: { PUT: async () => a2() }, + post() { + a3(); + }, +}; +const fallbacks = [async () => a4()]; + +export const { GET, rest: { PUT }, post: POST } = handlers; +export const [fallback] = fallbacks; From e93a29d5086b64fd75e61b5fd0119cf090edce13 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:38:06 +0000 Subject: [PATCH 28/86] fix(indexer): keep a Rust call after a second let of the same type A let counted from its start, so c.with_retry() in the value of let c = c.with_retry() read as a call after a second let and became a record. So did c.send() after it, though both lets hold a Client. The records gave the low name edges of send, and a target behind another send read found. A let now counts once its statement ends, and lets whose values have one type keep the target of the call graph. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 5 +- plugin/indexer/rust.go | 101 +++++++++++++++--- .../cases/rust/let-same-type/expect.yaml | 44 ++++++++ .../rust/let-same-type/project/Cargo.toml | 4 + .../rust/let-same-type/project/src/main.rs | 43 ++++++++ 5 files changed, 182 insertions(+), 15 deletions(-) create mode 100644 test/projectgraph/cases/rust/let-same-type/expect.yaml create mode 100644 test/projectgraph/cases/rust/let-same-type/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/let-same-type/project/src/main.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 5acefbcd..74043f8a 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -408,7 +408,10 @@ Rust follows the same rules, with these additions. before the call is a record with the reason `unknown-receiver`, and so is a call whose declared type does not hold. The call graph binds a name to the value of its first `let`, so a call after a second `let` of the name - is a record too, as in the case `rust/shadowed-let-type`. A method of the + with another type is a record too, as in the case + `rust/shadowed-let-type`. A `let` counts once its statement ends, and + `let`s of one type keep the target, as `c.send()` after + `let c = c.with_retry();` in the case `rust/let-same-type`. A method of the real type then reads as `uncertain` and not as `not-found`. - The link step resolves a type that a parameter, a `let`, a field or the result of a function declares at the root of the file, or as a struct, diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index b7560c20..437eec2e 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -5,6 +5,7 @@ import ( "maps" "slices" "strings" + "unicode" "github.com/safedep/code/core/ast" "github.com/safedep/code/fileindex" @@ -535,32 +536,104 @@ func rustDeclaredAt(ctx context.Context, p *projection, call fileindex.Call, nam } // rustRebound reports whether the function binds a name before a call by a -// binder that the call graph does not read, or by a second let. A static or -// a const binds its name in the whole block, also before it. The call graph -// binds a name to the value of its first let, so it sends t.run() after -// let t = B to A::run when let t = A comes first. A function whose walk -// stopped early can bind any name. +// binder that the call graph does not read, or by a second let of another +// type. A static or a const binds its name in the whole block, also before +// it. The call graph binds a name to the value of its first let, so it sends +// t.run() after let t = B to A::run when let t = A comes first. A let counts +// once its statement ends, because the call in its value, as c.with_retry() +// in let c = c.with_retry(), still sees the binding before it. Lets of one +// type, as let c = c.with_retry() after let c = Client::new(), keep the +// target. A function whose walk stopped early can bind any name. func rustRebound(ctx context.Context, p *projection, call fileindex.Call, name string) bool { binders, ok := p.rustBinders(ctx, call.Caller) if !ok { return true } - lets := 0 - return slices.ContainsFunc(binders[name], func(binder *sitter.Node) bool { + var lets []*sitter.Node + if slices.ContainsFunc(binders[name], func(binder *sitter.Node) bool { item := binder.Type() == "static_item" || binder.Type() == "const_item" - if !item && !startsBefore(binder, call) { + switch { + case !item && !startsBefore(binder, call): return false - } - if !rustPlainBinder(binder) { + case !rustPlainBinder(binder): return true + case binder.Type() == "let_declaration" && endsBefore(binder, call): + lets = append(lets, binder) } - if binder.Type() == "let_declaration" { - lets++ - } - return lets > 1 + return false + }) { + return true + } + if len(lets) < 2 { + return false + } + first := p.rustLetType(binders, lets[0], 0) + return first == "" || slices.ContainsFunc(lets[1:], func(let *sitter.Node) bool { + return p.rustLetType(binders, let, 0) != first }) } +// endsBefore reports whether a node ends before the site of a call. +func endsBefore(node *sitter.Node, call fileindex.Call) bool { + at := node.EndPoint() + line, column := int(at.Row)+1, int(at.Column)+1 + return line < call.Line || line == call.Line && column <= call.Column +} + +// rustMaxLetDepth bounds the lets that the type of one let reads, as c of +// let c = c.with_retry(). +const rustMaxLetDepth = 4 + +// rustLetType returns the name of the type of the value of a let, as the +// file writes it, or "" when the index does not know it: the declared type, +// a struct, a function or a method of the file with a result, or the let of +// a name that the value reads. +func (p *projection) rustLetType(binders map[string][]*sitter.Node, let *sitter.Node, depth int) string { + if typ := p.rustTypeName(let.ChildByFieldName("type"), nil); typ != "" { + return typ + } + return p.rustValueType(binders, let.ChildByFieldName("value"), depth) +} + +func (p *projection) rustValueType(binders map[string][]*sitter.Node, value *sitter.Node, depth int) string { + if value == nil || depth > rustMaxLetDepth { + return "" + } + switch value.Type() { + case "struct_expression": + return p.rustTypeName(value.ChildByFieldName("name"), nil) + case "identifier": + if name := p.text(value); name != "" && unicode.IsUpper(rune(name[0])) { + return name + } + if let := rustLetOf(binders[p.text(value)], value); let != nil { + return p.rustLetType(binders, let, depth+1) + } + case "call_expression": + fn := value.ChildByFieldName("function") + if method := rustMethod(fn); method != nil { + if typ := p.rustValueType(binders, method.ChildByFieldName("value"), depth+1); typ != "" { + return p.rustResultOf(typ, p.text(method.ChildByFieldName("field"))) + } + return "" + } + if fn != nil && fn.Type() == "scoped_identifier" { + return p.rustResultOf(p.text(fn.ChildByFieldName("path")), p.text(fn.ChildByFieldName("name"))) + } + } + return "" +} + +// rustResultOf returns the result type of a function of a type of the +// file, with no Option or Result around it. +func (p *projection) rustResultOf(typ, name string) string { + def := p.defs[fileindex.Join(p.cg.FileName, append(strings.Split(typ, "::"), name)...)] + if def == nil || def.Wraps != "" { + return "" + } + return def.Returns +} + // startsBefore reports whether a node starts before the site of a call. func startsBefore(node *sitter.Node, call fileindex.Call) bool { at := node.StartPoint() diff --git a/test/projectgraph/cases/rust/let-same-type/expect.yaml b/test/projectgraph/cases/rust/let-same-type/expect.yaml new file mode 100644 index 00000000..8cf21099 --- /dev/null +++ b/test/projectgraph/cases/rust/let-same-type/expect.yaml @@ -0,0 +1,44 @@ +source: safedep:hand +description: >- + builder binds c to Client::new(), then binds c again to c.with_retry(), which returns a Client, and calls + c.send(). branch binds c to a Client and again to a Client in a block. Each let of c holds a Client, so each + call runs a method of Client. The call c.with_retry() sits in the value of the second let, where the first + let still binds c. An index that counts a let before its statement ends, or that makes each call after a + second let a record, gives the calls the low name edges of send, and std::fs::create_dir of Other::send reads + found. The calls keep their targets, so std::fs::create_dir is not-found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//builder + to: src/main.rs//Client//with_retry + kind: member + confidence: high + site: src/main.rs:27 + - from: src/main.rs//builder + to: src/main.rs//Client//send + kind: member + confidence: high + site: src/main.rs:28 + - from: src/main.rs//branch + to: src/main.rs//Client//send + kind: member + confidence: high + site: src/main.rs:35 +no_edges: + - from: src/main.rs//builder + to: src/main.rs//Other//send + - from: src/main.rs//branch + to: src/main.rs//Other//send +unresolved: [] +results: + - target: std//fs//create_dir + status: not-found + call_sites: + - caller: src/main.rs//Other//send + site: src/main.rs:21 + reached: false diff --git a/test/projectgraph/cases/rust/let-same-type/project/Cargo.toml b/test/projectgraph/cases/rust/let-same-type/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/let-same-type/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/let-same-type/project/src/main.rs b/test/projectgraph/cases/rust/let-same-type/project/src/main.rs new file mode 100644 index 00000000..964e1dbc --- /dev/null +++ b/test/projectgraph/cases/rust/let-same-type/project/src/main.rs @@ -0,0 +1,43 @@ +struct Client; + +impl Client { + fn new() -> Client { + Client + } + + fn with_retry(self) -> Client { + self + } + + fn send(&self) { + let _ = std::fs::write("a", "b"); + } +} + +struct Other; + +impl Other { + fn send(&self) { + let _ = std::fs::create_dir("c"); + } +} + +fn builder() { + let c = Client::new(); + let c = c.with_retry(); + c.send(); +} + +fn branch(flag: bool) { + let c = Client::new(); + if flag { + let c = Client; + c.send(); + } + let _ = c; +} + +fn main() { + builder(); + branch(true); +} From cdb47d5b765091a90116ab84cc4508c7f1da7cf2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:41:53 +0000 Subject: [PATCH 29/86] fix(indexer): give a Rust type and a method call the path of a use A type that a field, a result or a let names through a use of an inline module, as Item after use crate::store::Item, was dropped. The call on the value then got a record and the low name edges of each method of the name. The index now writes the path of the use, and the link step reads such a path of a declared type as the alias of a type does. The call graph sends x.save() after let x = Item::new() to the Item of an outer module that it finds before the use. The index now gives such a method call the path of the use of the type name, so the save of the real Item is no longer not-found. The receiver row of a use of an inline module now reads found. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 9 ++- plugin/indexer/rust.go | 79 +++++++++++++++---- plugin/indexer/rust_test.go | 4 +- projectgraph/declared.go | 3 + .../cases/rust/use-type-receiver/expect.yaml | 58 ++++++++++++++ .../rust/use-type-receiver/project/Cargo.toml | 4 + .../use-type-receiver/project/src/main.rs | 6 ++ .../use-type-receiver/project/src/store.rs | 39 +++++++++ .../cases/rust/use-type-result/expect.yaml | 39 +++++++++ .../rust/use-type-result/project/Cargo.toml | 4 + .../rust/use-type-result/project/src/main.rs | 6 ++ .../rust/use-type-result/project/src/store.rs | 38 +++++++++ test/projectgraph/receivers_test.go | 2 +- 13 files changed, 271 insertions(+), 20 deletions(-) create mode 100644 test/projectgraph/cases/rust/use-type-receiver/expect.yaml create mode 100644 test/projectgraph/cases/rust/use-type-receiver/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-type-receiver/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-type-receiver/project/src/store.rs create mode 100644 test/projectgraph/cases/rust/use-type-result/expect.yaml create mode 100644 test/projectgraph/cases/rust/use-type-result/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-type-result/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-type-result/project/src/store.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 74043f8a..c3e7d4ee 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -428,7 +428,10 @@ Rust follows the same rules, with these additions. with a `struct Local` at the root, `t.inspect()` takes no method of the root `Local`. The call keeps the target that the call graph gives, or a record with the reason `unknown-receiver`. A type parameter with a bound, - as `S` in `fn run(s: S)`, names its bound. The case + as `S` in `fn run(s: S)`, names its bound. A `use` that + declares the name gives the path of the `use` in place of the type, as + `crate::store::Item` for `Item` after `use crate::store::Item;` in an + inline module, as in the case `rust/use-type-result`. The case `rust/inline-module-types` shows the types of an inline module, and the case `rust/generic-field-type` a type parameter of a struct and of an `impl`. @@ -462,6 +465,10 @@ Rust follows the same rules, with these additions. the call, and reads no `use` and no type parameter. A `use` in a block or in an inline module can bind the name again, and the index then gives the call the path of that `use`, as in the case `rust/use-shadows-module`. + A method call whose target the call graph finds as a method of a type of + an outer scope gets the path of a `use` of the type name the same way, + as `x.save()` after `let x = Item::new();` in the case + `rust/use-type-receiver`. A type parameter of the name names a type that the caller gives, so the call is a record with the reason `unknown-receiver`, as in the case `rust/generic-shadows-type`. diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 437eec2e..598ba8ef 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -309,8 +309,10 @@ func rustSignature(p *projection, def *fileindex.Definition, node *sitter.Node) } } result := node.ChildByFieldName("return_type") - if def.Returns, def.Wraps = p.rustResult(result, bounds); def.Returns != "" && p.rustScopedType(result, def.Returns) == "" { - def.Returns, def.Wraps = "", "" + if def.Returns, def.Wraps = p.rustResult(result, bounds); def.Returns != "" { + if def.Returns = p.rustScopedType(result, def.Returns); def.Returns == "" { + def.Wraps = "" + } } def.Locals = p.rustLocals(node.ChildByFieldName("body"), bounds) if impl := rustImpl(node); impl != nil && !def.Static && !p.rustDefinesType(impl) { @@ -361,24 +363,38 @@ func (p *projection) rustDeclaredType(binder *sitter.Node, bounds map[string]str return p.rustScopedType(binder, p.rustTypeName(binder.ChildByFieldName("type"), bounds)) } -// rustScopedType returns a type that a node names, or "" when a scope that -// holds the node declares the first name of the type. The link step -// resolves a type at the root of the file, or as a type of an inline module -// that holds the node. So Local in fn run() { type Local = dep::Thing; -// let t: Local = ... } resolved to a struct Local of the root. A call on t -// then took the method of that struct with no record, and a method of -// dep::Thing read as not-found. Without the type, the call keeps the target -// of the call graph or its record. +// rustScopedType returns a type that a node names, as the link step reads +// it. The link step resolves a type at the root of the file, or as a type of +// an inline module that holds the node. So Local in fn run() { type Local = +// dep::Thing; let t: Local = ... } resolved to a struct Local of the +// root. A call on t then took the method of that struct with no record, and +// a method of dep::Thing read as not-found. So when a scope that holds the +// node declares the first name of the type, the type is "", and the call +// keeps the target of the call graph or its record. A use that declares the +// name gives its path instead, as crate::store::Item for Item after use +// crate::store::Item in an inline module. func (p *projection) rustScopedType(node *sitter.Node, typ string) string { - first, _, _ := strings.Cut(typ, "::") - if typ == "" || p.rustScopeDeclares(node, first) { + first, rest, nested := strings.Cut(typ, "::") + if typ == "" || !p.rustScopeDeclares(node, nil, first) { + return typ + } + use, isUse := p.rustRebinder(node, nil, first) + if !isUse || p.rustScopeDeclares(node, use.Parent(), first) { return "" } - return typ + namespace, ok := p.rustUseNamespace(use, first) + if !ok { + return "" + } + path := strings.ReplaceAll(namespace, fileindex.Separator, "::") + if nested { + path += "::" + rest + } + return path } // rustScopeDeclares reports whether a scope between a node and the root of -// the file can give a name another meaning than the root gives it: an item +// the file, or a stop node, can give a name another meaning than the root gives it: an item // or a use of a block or of an inline module, or a type parameter of a // function, an impl, a trait or a type. A glob use can declare any name, // except use super::* in a module of the root, which reads the names of the @@ -386,8 +402,8 @@ func (p *projection) rustScopedType(node *sitter.Node, typ string) string { // root. A struct, an enum, a union or a trait of the inline module that // declares the name first does not count, because the link step looks for // a type of the name in the inline modules that hold the node. -func (p *projection) rustScopeDeclares(node *sitter.Node, name string) bool { - for scope := node.Parent(); scope != nil; scope = scope.Parent() { +func (p *projection) rustScopeDeclares(node, stop *sitter.Node, name string) bool { + for scope := node.Parent(); scope != nil && (stop == nil || !ts.SameNode(scope, stop)); scope = scope.Parent() { switch scope.Type() { case "block": if p.rustItemsDeclare(scope, name, false) { @@ -780,6 +796,9 @@ func rustBoundName(pattern *sitter.Node) *sitter.Node { // record. func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) bool { node := p.descendant(callee) + if node != nil && rustMethod(node) != nil { + return p.rustShadowedMethod(call, node) + } if node == nil || node.Type() != "identifier" && node.Type() != "scoped_identifier" { return true } @@ -810,6 +829,34 @@ func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) return true } +// rustShadowedMethod fixes a target of a method call that the call graph +// finds as a method of a type of an outer scope, when a scope between the +// call and that scope binds the name of the type again. So x.save() after +// let x = Item::new() and use crate::store::Item in mod deep went to the +// Item of the parent of deep, and the real save read as not-found. +func (p *projection) rustShadowedMethod(call *fileindex.Call, node *sitter.Node) bool { + for i, target := range call.Targets { + class := fileindex.Parent(target) + if def := p.defs[class]; def == nil || def.Kind != fileindex.DefClass || !fileindex.HasKeyPrefix(class, p.cg.FileName) { + continue + } + name := fileindex.Last(class) + binder, use := p.rustRebinder(node, p.syntax[fileindex.Parent(class)], name) + switch { + case binder == nil: + case !use: + return false + default: + namespace, ok := p.rustUseNamespace(binder, name) + if !ok { + return false + } + call.Targets[i] = fileindex.Join(namespace, fileindex.Last(target)) + } + } + return true +} + // rustRebinder returns the innermost use or item with type parameters // between a node and the definition of a scope that binds a name, and // whether it is a use. A nil scope is the file. diff --git a/plugin/indexer/rust_test.go b/plugin/indexer/rust_test.go index bb5ea023..df269c07 100644 --- a/plugin/indexer/rust_test.go +++ b/plugin/indexer/rust_test.go @@ -156,9 +156,9 @@ func TestFileIndexRustDeclaredTypes(t *testing.T) { {key: "main.rs//plain", locals: store}, {key: "main.rs//sibling", locals: store}, {key: "main.rs//alias"}, - {key: "main.rs//after"}, + {key: "main.rs//after", locals: []fileindex.Typed{{Name: "t", Type: "dep::Thing"}}}, {key: "main.rs//outer"}, - {key: "main.rs//listed"}, + {key: "main.rs//listed", locals: []fileindex.Typed{{Name: "t", Type: "dep::store::Store"}}}, {key: "main.rs//globbed"}, {key: "main.rs//crated"}, {key: "main.rs//generic"}, diff --git a/projectgraph/declared.go b/projectgraph/declared.go index 6d774041..c5a44ad1 100644 --- a/projectgraph/declared.go +++ b/projectgraph/declared.go @@ -127,6 +127,9 @@ func (b *builder) resolveType(sc *scope, from, text string) (string, bool) { return "", false } key, ok := b.resolveNameIn(sc, from, text) + if !ok && sc.profile.typePath != nil && strings.Contains(text, "::") { + key, ok = sc.profile.typePath(b, sc, text) + } trusted := sc.profile.implicitInterfaces || sc.profile.unitTypes || sc.profile.declaredTypes switch { case !ok: diff --git a/test/projectgraph/cases/rust/use-type-receiver/expect.yaml b/test/projectgraph/cases/rust/use-type-receiver/expect.yaml new file mode 100644 index 00000000..966e6962 --- /dev/null +++ b/test/projectgraph/cases/rust/use-type-receiver/expect.yaml @@ -0,0 +1,58 @@ +source: safedep:hand +description: >- + src/store.rs declares the struct Item, and its inline module cache declares another Item. The inline module + cache::deep has use crate::store::Item, so x in by_new and in by_value holds the Item of the root, and x.save() + runs Item::save of the root, which calls std::fs::write. The call graph reads no use, so it finds the Item of + cache for Item::new() and for Item, and sends x.save() to cache::Item::save, which calls std::fs::create_dir. + std::fs::write then reads not-found. The index gives the call the path of the use. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/store.rs//cache//deep//by_new + to: src/store.rs//Item//save + kind: member + confidence: high + site: src/store.rs:31 + - from: src/store.rs//cache//deep//by_value + to: src/store.rs//Item//save + kind: member + confidence: high + site: src/store.rs:36 +no_edges: + - from: src/store.rs//cache//deep//by_new + to: src/store.rs//cache//Item//save + - from: src/store.rs//cache//deep//by_value + to: src/store.rs//cache//Item//save +unresolved: [] +results: + - target: std//fs//write + status: found + path: + - key: src/main.rs//main + - key: src/store.rs//cache//deep//by_new + site: src/main.rs:4 + kind: direct + confidence: high + - key: src/store.rs//Item//save + site: src/store.rs:31 + kind: member + confidence: high + - key: std//fs//write + site: src/store.rs:5 + kind: direct + confidence: high + call_sites: + - caller: src/store.rs//Item//save + site: src/store.rs:5 + reached: true + - target: std//fs//create_dir + status: not-found + call_sites: + - caller: src/store.rs//cache//Item//save + site: src/store.rs:18 + reached: false diff --git a/test/projectgraph/cases/rust/use-type-receiver/project/Cargo.toml b/test/projectgraph/cases/rust/use-type-receiver/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/use-type-receiver/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/use-type-receiver/project/src/main.rs b/test/projectgraph/cases/rust/use-type-receiver/project/src/main.rs new file mode 100644 index 00000000..8f8af11f --- /dev/null +++ b/test/projectgraph/cases/rust/use-type-receiver/project/src/main.rs @@ -0,0 +1,6 @@ +mod store; + +fn main() { + store::cache::deep::by_new(); + store::cache::deep::by_value(); +} diff --git a/test/projectgraph/cases/rust/use-type-receiver/project/src/store.rs b/test/projectgraph/cases/rust/use-type-receiver/project/src/store.rs new file mode 100644 index 00000000..efdedfbd --- /dev/null +++ b/test/projectgraph/cases/rust/use-type-receiver/project/src/store.rs @@ -0,0 +1,39 @@ +pub struct Item; + +impl Item { + pub fn save(&self) { + let _ = std::fs::write("top", "x"); + } + + pub fn new() -> Item { + Item + } +} + +pub mod cache { + pub struct Item; + + impl Item { + pub fn save(&self) { + let _ = std::fs::create_dir("cache"); + } + + pub fn new() -> Item { + Item + } + } + + pub mod deep { + use crate::store::Item; + + pub fn by_new() { + let x = Item::new(); + x.save(); + } + + pub fn by_value() { + let x = Item; + x.save(); + } + } +} diff --git a/test/projectgraph/cases/rust/use-type-result/expect.yaml b/test/projectgraph/cases/rust/use-type-result/expect.yaml new file mode 100644 index 00000000..96fd4bd4 --- /dev/null +++ b/test/projectgraph/cases/rust/use-type-result/expect.yaml @@ -0,0 +1,39 @@ +source: safedep:hand +description: >- + src/store.rs declares the struct Item, and its inline module cache declares another Item. The inline module + cache::deep has use crate::store::Item, so the result of make and the field item of Holder hold the Item of the + root. make().save() and h.item.save() run Item::save of the root, which calls std::fs::write. The save of + cache::Item calls std::fs::create_dir and nothing calls it. An index that drops a type that a use declares + gives each call a record and the low name edges of both save methods, so std::fs::create_dir reads found. The + index writes the path of the use, crate::store::Item, so each call keeps one edge. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/store.rs//cache//deep//by_result + to: src/store.rs//Item//save + kind: member + confidence: high + site: src/store.rs:30 + - from: src/store.rs//cache//deep//by_field + to: src/store.rs//Item//save + kind: member + confidence: high + site: src/store.rs:35 +no_edges: + - from: src/store.rs//cache//deep//by_result + to: src/store.rs//cache//Item//save + - from: src/store.rs//cache//deep//by_field + to: src/store.rs//cache//Item//save +unresolved: [] +results: + - target: std//fs//create_dir + status: not-found + call_sites: + - caller: src/store.rs//cache//Item//save + site: src/store.rs:14 + reached: false diff --git a/test/projectgraph/cases/rust/use-type-result/project/Cargo.toml b/test/projectgraph/cases/rust/use-type-result/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/use-type-result/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/use-type-result/project/src/main.rs b/test/projectgraph/cases/rust/use-type-result/project/src/main.rs new file mode 100644 index 00000000..650de6e4 --- /dev/null +++ b/test/projectgraph/cases/rust/use-type-result/project/src/main.rs @@ -0,0 +1,6 @@ +mod store; + +fn main() { + store::cache::deep::by_result(); + store::cache::deep::by_field(); +} diff --git a/test/projectgraph/cases/rust/use-type-result/project/src/store.rs b/test/projectgraph/cases/rust/use-type-result/project/src/store.rs new file mode 100644 index 00000000..4bb2846b --- /dev/null +++ b/test/projectgraph/cases/rust/use-type-result/project/src/store.rs @@ -0,0 +1,38 @@ +pub struct Item; + +impl Item { + pub fn save(&self) { + let _ = std::fs::write("top", "x"); + } +} + +pub mod cache { + pub struct Item; + + impl Item { + pub fn save(&self) { + let _ = std::fs::create_dir("cache"); + } + } + + pub mod deep { + use crate::store::Item; + + pub struct Holder { + pub item: Item, + } + + pub fn make() -> Item { + Item + } + + pub fn by_result() { + make().save(); + } + + pub fn by_field() { + let h = Holder { item: Item }; + h.item.save(); + } + } +} diff --git a/test/projectgraph/receivers_test.go b/test/projectgraph/receivers_test.go index 47be09f3..7683413b 100644 --- a/test/projectgraph/receivers_test.go +++ b/test/projectgraph/receivers_test.go @@ -346,7 +346,7 @@ var rustReceiverRows = []receiverRow{ name: "parameter of a use of an inline module", source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nmod inner {\n use dep::Thing as Local;\n pub fn go(t: Local) {\n t.inspect();\n }\n}\n\nfn run() {\n inner::go(dep::new_thing());\n}", target: "dep//Thing//inspect", - want: "uncertain/unknown-receiver", + want: "found", }, { name: "parameter of a type alias of the outer function", From 2d2bcb83d63301abb71d4c8c89b5dfd5d976f1df Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:49:11 +0000 Subject: [PATCH 30/86] fix(projectgraph): follow a Rust glob of a unit into a third crate A name that the module of a glob use of a unit brings from another crate, by pub use inner::* or pub use inner::tools, or a child module that it brings by its own pub use crate::sub::*, named nothing. The path then read as a crate, got an external key with no record, and its target read not-found. A unit now gives such a name as the external key of the third crate, and a glob of a module of a unit brings its child modules and the names of its uses. Two crates can bring each other's names by glob uses, so a guard cuts the second read of a name. A provision that a guard cut is now not kept for the build, because the same path can resolve on another way in. Each cycle guard counts its cuts for that check. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 8 +- projectgraph/link.go | 4 + projectgraph/names.go | 8 +- projectgraph/namespaces.go | 5 + projectgraph/packages.go | 10 +- projectgraph/resolve.go | 6 +- projectgraph/results.go | 1 + projectgraph/rust.go | 68 ++++++++++++++ projectgraph/walk.go | 1 + .../rust/glob-use-crate-cycle/expect.yaml | 49 ++++++++++ .../glob-use-crate-cycle/project/Cargo.toml | 8 ++ .../glob-use-crate-cycle/project/src/a.rs | 5 + .../glob-use-crate-cycle/project/src/b.rs | 5 + .../glob-use-crate-cycle/project/src/main.rs | 7 ++ .../project/vendor/dep_a/Cargo.toml | 7 ++ .../project/vendor/dep_a/src/lib.rs | 3 + .../project/vendor/dep_b/Cargo.toml | 8 ++ .../project/vendor/dep_b/src/lib.rs | 4 + .../project/vendor/dep_c/Cargo.toml | 4 + .../project/vendor/dep_c/src/lib.rs | 1 + .../project/vendor/dep_c/src/n.rs | 1 + .../summaries/cargo_dep_a_1.0.0.json | 16 ++++ .../summaries/cargo_dep_b_1.0.0.json | 16 ++++ .../summaries/cargo_dep_c_1.0.0.json | 16 ++++ .../cases/rust/glob-use-reexport/expect.yaml | 91 +++++++++++++++++++ .../rust/glob-use-reexport/project/Cargo.toml | 7 ++ .../rust/glob-use-reexport/project/src/a.rs | 6 ++ .../rust/glob-use-reexport/project/src/b.rs | 5 + .../glob-use-reexport/project/src/main.rs | 7 ++ .../project/vendor/dep/Cargo.toml | 7 ++ .../project/vendor/dep/src/lib.rs | 5 + .../project/vendor/dep/src/prelude.rs | 1 + .../project/vendor/dep/src/sub/leaf.rs | 1 + .../project/vendor/dep/src/sub/mod.rs | 1 + .../project/vendor/inner/Cargo.toml | 4 + .../project/vendor/inner/src/lib.rs | 2 + .../project/vendor/inner/src/text.rs | 1 + .../project/vendor/inner/src/tools.rs | 1 + .../summaries/cargo_dep_1.0.0.json | 16 ++++ .../summaries/cargo_inner_1.0.0.json | 23 +++++ 40 files changed, 434 insertions(+), 5 deletions(-) create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/a.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/b.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_a/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_a/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_b/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_b/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/src/n.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_a_1.0.0.json create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_b_1.0.0.json create mode 100644 test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_c_1.0.0.json create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/src/a.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/src/b.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/prelude.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/sub/leaf.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/sub/mod.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/text.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/tools.rs create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/summaries/cargo_dep_1.0.0.json create mode 100644 test/projectgraph/cases/rust/glob-use-reexport/summaries/cargo_inner_1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index c3e7d4ee..f5c6e483 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -494,7 +494,13 @@ Rust follows the same rules, with these additions. and `html::render()` after `use dep::*;` names it too, and not a crate `html`, as in the case `rust/glob-use-dependency-module`. A glob `use` of a crate that no unit gives can bring any name, so the path resolves - as if the glob `use` were not there. A local closure or a parameter can + as if the glob `use` were not there. The unit can bring the name from a + third crate by a `use` or a glob `use`, or a child module by a glob + `use` of its own crate, as `text` after `pub use inner::*;` and `leaf` + after `pub use crate::sub::*;` in the case `rust/glob-use-reexport`. Two + crates can bring each other's names, so a cycle guard cuts the read, and + the link step keeps no provision of a unit that a cut read gives, as the + case `rust/glob-use-crate-cycle` shows. A local closure or a parameter can bind a single name, so such a call keeps its record too. - `#[macro_export]` puts a macro at the root of its crate, from any module of the crate. So `dep::wipe` names the macro `wipe` of a private module diff --git a/projectgraph/link.go b/projectgraph/link.go index 32bac68e..1751849c 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -188,6 +188,10 @@ type builder struct { // tentative holds an undo step for each memo write made while a read // of wildcard imports is in progress, as wildcardModules describes. tentative []func() + // cuts counts the answers that a cycle guard or the hop limit cut + // short. A memo that a read with a cut gives can lack an answer that + // another way in finds. + cuts int children map[string][]string // aliases holds the definitions of each class that the language joins // to one class, the class too. diff --git a/projectgraph/names.go b/projectgraph/names.go index b8dc2e20..d6861d15 100644 --- a/projectgraph/names.go +++ b/projectgraph/names.go @@ -17,9 +17,11 @@ func (b *builder) resolveName(module, name string, hops int) (string, *failure) return r.key, r.fail } if b.visiting[k] { + b.cuts++ return "", &failure{reason: fileindex.UnresolvedNoDefinition, name: name, cut: true} } if hops > b.opts.MaxExportHops { + b.cuts++ path := strings.TrimSuffix(module, "/") b.truncations[Truncation{Kind: TruncationExportHops, Key: fileindex.Join(path, name), Site: Site{Path: path}}] = true return "", &failure{reason: fileindex.UnresolvedLimit, name: name, cut: true} @@ -205,7 +207,11 @@ func (b *builder) bound(key string) (string, bool) { return target, true } def := b.defs[key] - if def == nil || def.Kind != fileindex.DefAlias || b.reading[key] { + if def == nil || def.Kind != fileindex.DefAlias { + return "", false + } + if b.reading[key] { + b.cuts++ return "", false } b.reading[key] = true diff --git a/projectgraph/namespaces.go b/projectgraph/namespaces.go index 10c5ac00..4eee1c03 100644 --- a/projectgraph/namespaces.go +++ b/projectgraph/namespaces.go @@ -114,11 +114,16 @@ func (b *builder) unitMember(key, name string) (string, bool) { if r := b.walk(mod, lead); r.fail == nil { return r.key, true } + } else if key, ok := b.rustCrateMember(sc, joinPath(module, lead)); ok { + return key, true } } if keys := b.wildcardImported(sc, name); len(keys) > 0 { return keys[0], true } + if key, ok := b.rustGlobMember(sc, name); ok { + return key, true + } } return "", false } diff --git a/projectgraph/packages.go b/projectgraph/packages.go index 37b7fcff..5711c974 100644 --- a/projectgraph/packages.go +++ b/projectgraph/packages.go @@ -364,6 +364,7 @@ func (b *builder) providers(o origin) []provision { if out, ok := b.provisions[k]; ok { return out } + cuts := b.cuts var out []provision for _, sp := range b.ordered { if sp.unit != "" { @@ -373,8 +374,13 @@ func (b *builder) providers(o origin) []provision { if narrow := profileOf(family).narrowProviders; narrow != nil { out = narrow(out) } - b.provisions[k] = out - b.memo(func() { delete(b.provisions, k) }) + // A cycle guard of an outer resolution can cut the read of the unit, + // and the same module can resolve on another way in. So only an answer + // that no guard cut stays. + if b.cuts == cuts { + b.provisions[k] = out + b.memo(func() { delete(b.provisions, k) }) + } return out } diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 9b93450f..ed6372b3 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -487,8 +487,12 @@ func (b *builder) wildcardImported(sc *scope, name string) []string { var keys []string for _, mod := range b.wildcardModules(sc) { switch { - case b.units[mod] != nil: + case b.units[mod] != nil && len(b.units[mod].names[name]) > 0: keys = append(keys, b.units[mod].names[name]...) + case b.units[mod] != nil && sc.profile.unitImports: + if key, ok := b.rustGlobUnitMember(mod, name); ok { + keys = append(keys, key) + } case b.isType(mod): if key, fail := b.method(mod, name); fail == nil { keys = append(keys, key) diff --git a/projectgraph/results.go b/projectgraph/results.go index 1aaa64ad..b6dc73e7 100644 --- a/projectgraph/results.go +++ b/projectgraph/results.go @@ -122,6 +122,7 @@ func (b *builder) unitDefinitions(origins []origin) ([]string, bool) { } k := nameKey{module: pv.module, name: fileindex.Join("", o.names...)} if b.stepping[k] { + b.cuts++ continue } b.stepping[k] = true diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 5b4943d3..f43d1107 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -157,6 +157,8 @@ type rustState struct { globs map[string][]string macros map[*space]map[string]string nested map[string]bool + globbing map[[2]string]bool + members map[[2]string]bool } // crateRoute is the path in another crate that a path of a file names, and @@ -194,6 +196,8 @@ func (b *builder) addRustUnits() { s.routing = map[[2]string]bool{} s.routes = map[[2]string]crateRoute{} s.globs = map[string][]string{} + s.globbing = map[[2]string]bool{} + s.members = map[[2]string]bool{} files := map[string][]string{} for _, p := range b.paths { if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust { @@ -503,6 +507,7 @@ func (b *builder) resolveRust(importer, written string) (string, bool) { b.asked[k] = true } if s.resolving[k] { + b.cuts++ return "", false } s.resolving[k] = true @@ -623,6 +628,7 @@ func (b *builder) rustCratePath(importer, written string) (string, string, bool) return r.path, r.file, r.ok } if s.routing[k] { + b.cuts++ return "", "", false } outer := len(s.routing) == 0 @@ -829,6 +835,10 @@ func (b *builder) rustMacroCrate(ix *fileindex.Index, name string) (string, bool // of a crate that no unit gives can bring any name, and the path then // resolves as before. A glob use of an inline module stays out, as // rustGlobModules says. +// +// A unit can bring the name by a glob use of a third crate, so the read of +// the unit can ask for the same path again. The guard cuts such a cycle, +// and providers keeps no answer that a cut gave. func (b *builder) rustGlobPath(importer, written string) (string, string, bool) { sc := b.scopes[importer] first := rustFirstName(written) @@ -839,6 +849,14 @@ func (b *builder) rustGlobPath(importer, written string) (string, string, bool) case "crate", "self", "super": return "", "", false } + s := b.rustState() + k := [2]string{importer, first} + if s.globbing[k] { + b.cuts++ + return "", "", false + } + s.globbing[k] = true + defer delete(s.globbing, k) for _, imp := range sc.wildcards { if def := b.defs[sc.enclosing(imp.Line)]; def != nil && def.Kind == fileindex.DefClass { continue @@ -876,6 +894,56 @@ func (b *builder) rustNestedName(name string) bool { return s.nested[name] } +// rustCrateMember returns the external key of a path of another crate that +// a use of a file of a unit binds, as inner::text of pub use inner::text in +// the prelude of dep. The walk then goes on in the unit of that crate. +// Without the key, dep::prelude::text named nothing, and text::fmt after +// use dep::prelude::* read as a crate text. +func (b *builder) rustCrateMember(sc *scope, path string) (string, bool) { + if !b.rustUnitFile(sc.ix) || b.rustLocalRoot(sc.ix.Path, path) { + return "", false + } + return b.rustCrateKey(sc.ix.Path, path), true +} + +// rustGlobMember returns the external key of a name that a glob use of a +// file of a unit brings from another crate, as inner::ifn for ifn after +// pub use inner::*. A file of the project reads such a name in rustPath, +// which keeps the record of a single name. +func (b *builder) rustGlobMember(sc *scope, name string) (string, bool) { + if !b.rustUnitFile(sc.ix) { + return "", false + } + path, file, ok := b.rustGlobPath(sc.ix.Path, name) + if !ok { + return "", false + } + return b.rustCrateKey(file, path), true +} + +// rustGlobUnitMember returns what a glob use of a module brings for a name +// that no definition of the module has: a child module, or the target of a +// use or of a glob use of the module, as leaf for use crate::sub::* with +// pub mod leaf in sub. Two modules can bring each other with glob uses, so +// the guard cuts a read that asks for the same name again. +func (b *builder) rustGlobUnitMember(mod, name string) (string, bool) { + s := b.rustState() + k := [2]string{mod, name} + if s.members[k] { + b.cuts++ + return "", false + } + s.members[k] = true + defer delete(s.members, k) + return b.unitMember(mod, name) +} + +// rustUnitFile reports whether a file is Rust code of a unit of dependency +// code. +func (b *builder) rustUnitFile(ix *fileindex.Index) bool { + return ix.Language == langcode.Rust && ix.Unit != "" +} + // joinImplBlocks makes an impl block of a type of another module one class // with the type, as impl Store for FileStore after use crate::FileStore. func (b *builder) joinImplBlocks() { diff --git a/projectgraph/walk.go b/projectgraph/walk.go index 46e4bf69..06e506d6 100644 --- a/projectgraph/walk.go +++ b/projectgraph/walk.go @@ -321,6 +321,7 @@ func (b *builder) defaultValue(module, name string) (string, bool) { // variable that holds a variable of another file walks on to that value. func (b *builder) variableValue(key string) (string, bool) { if b.reading[key] { + b.cuts++ return "", false } b.reading[key] = true diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/expect.yaml b/test/projectgraph/cases/rust/glob-use-crate-cycle/expect.yaml new file mode 100644 index 00000000..57aec1e4 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/expect.yaml @@ -0,0 +1,49 @@ +source: safedep:hand +description: >- + src/a.rs has use dep_b::* and src/b.rs has use dep_a::*, and both call n::f. dep_b has pub use dep_a::* and then + pub use dep_c::*, dep_a has pub use dep_b::*, and n is a module of dep_c. Cargo rejects such a cycle of crates, + but the read of a glob use must still end and give each path its answer. The read of n for src/a.rs asks dep_a, + which asks dep_b again, and the cycle guard cuts that inner read. A memo that keeps the answer of the cut read + for dep_a gives src/b.rs no module n, so its call reads n as a crate, and dep_c//n//f reads not-found from it. +units: + vendor/dep_a: cargo:dep_a@1.0.0 + vendor/dep_b: cargo:dep_b@1.0.0 + vendor/dep_c: cargo:dep_c@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/a.rs//go + to: dep_b//n//f + kind: direct + confidence: high + site: src/a.rs:4 + - from: src/b.rs//go + to: dep_a//n//f + kind: direct + confidence: high + site: src/b.rs:4 +unresolved: [] +results: + - target: dep_c//n//f + status: found + sources: [src/b.rs//go] + sources_only: true + path: + - key: src/b.rs//go + - key: dep_a//n//f + site: src/b.rs:4 + kind: direct + confidence: high + - key: dep_b//n//f + site: cargo:dep_a@1.0.0 + kind: package + confidence: high + - key: dep_c//n//f + site: cargo:dep_b@1.0.0 + kind: package + confidence: high diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/Cargo.toml new file mode 100644 index 00000000..bf121dd4 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep_a = "1.0.0" +dep_b = "1.0.0" diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/a.rs b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/a.rs new file mode 100644 index 00000000..ae913539 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/a.rs @@ -0,0 +1,5 @@ +use dep_b::*; + +pub fn go() { + n::f(); +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/b.rs b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/b.rs new file mode 100644 index 00000000..d0d2e32b --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/b.rs @@ -0,0 +1,5 @@ +use dep_a::*; + +pub fn go() { + n::f(); +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/main.rs new file mode 100644 index 00000000..efffd961 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/src/main.rs @@ -0,0 +1,7 @@ +mod a; +mod b; + +fn main() { + a::go(); + b::go(); +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_a/Cargo.toml b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_a/Cargo.toml new file mode 100644 index 00000000..2316a378 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_a/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "dep_a" +version = "1.0.0" +edition = "2021" + +[dependencies] +dep_b = "1.0.0" diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_a/src/lib.rs b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_a/src/lib.rs new file mode 100644 index 00000000..ae4fd6ef --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_a/src/lib.rs @@ -0,0 +1,3 @@ +pub use dep_b::*; + +pub fn a() {} diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_b/Cargo.toml b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_b/Cargo.toml new file mode 100644 index 00000000..3ada9cc0 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "dep_b" +version = "1.0.0" +edition = "2021" + +[dependencies] +dep_a = "1.0.0" +dep_c = "1.0.0" diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_b/src/lib.rs b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_b/src/lib.rs new file mode 100644 index 00000000..611ff513 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_b/src/lib.rs @@ -0,0 +1,4 @@ +pub use dep_a::*; +pub use dep_c::*; + +pub fn b() {} diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/Cargo.toml b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/Cargo.toml new file mode 100644 index 00000000..70c42fb8 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep_c" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/src/lib.rs b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/src/lib.rs new file mode 100644 index 00000000..698957ad --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/src/lib.rs @@ -0,0 +1 @@ +pub mod n; diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/src/n.rs b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/src/n.rs new file mode 100644 index 00000000..cdc6c27d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/project/vendor/dep_c/src/n.rs @@ -0,0 +1 @@ +pub fn f() {} diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_a_1.0.0.json b/test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_a_1.0.0.json new file mode 100644 index 00000000..7fcb9e7f --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_a_1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep_a@1.0.0", + "symbols": [ + { + "key": "dep_a//a", + "definitions": [ + "cargo:dep_a@1.0.0//src/lib.rs//a" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_b_1.0.0.json b/test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_b_1.0.0.json new file mode 100644 index 00000000..b20e638c --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_b_1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep_b@1.0.0", + "symbols": [ + { + "key": "dep_b//b", + "definitions": [ + "cargo:dep_b@1.0.0//src/lib.rs//b" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_c_1.0.0.json b/test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_c_1.0.0.json new file mode 100644 index 00000000..b3fa4f57 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-cycle/summaries/cargo_dep_c_1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep_c@1.0.0", + "symbols": [ + { + "key": "dep_c//n//f", + "definitions": [ + "cargo:dep_c@1.0.0//src/n.rs//f" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/glob-use-reexport/expect.yaml b/test/projectgraph/cases/rust/glob-use-reexport/expect.yaml new file mode 100644 index 00000000..b81af259 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/expect.yaml @@ -0,0 +1,91 @@ +source: safedep:hand +description: >- + The crate dep, the unit cargo:dep@1.0.0, has pub use inner::* and pub use crate::sub::* at its root, and its + module prelude has pub use inner::tools. inner is the unit cargo:inner@1.0.0. src/a.rs has use dep::*, so text + names the module text of inner and leaf names the module sub::leaf of dep. src/b.rs has use dep::prelude::*, so + tools names the module tools of inner. A link step that reads no name that a unit brings from another crate or + by its own glob use reads each first name as a crate, gives each call an external key with no record, and + each target reads not-found. +units: + vendor/dep: cargo:dep@1.0.0 + vendor/inner: cargo:inner@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +no_edges: + - from: src/a.rs//go + to: text//fmt + - from: src/a.rs//go + to: leaf//leaffn + - from: src/b.rs//go + to: tools//run +edges: + - from: src/a.rs//go + to: dep//text//fmt + kind: direct + confidence: high + site: src/a.rs:4 + - from: src/a.rs//go + to: dep//leaf//leaffn + kind: direct + confidence: high + site: src/a.rs:5 + - from: src/b.rs//go + to: dep//prelude//tools//run + kind: direct + confidence: high + site: src/b.rs:4 +unresolved: [] +results: + - target: inner//text//fmt + status: found + path: + - key: src/main.rs//main + - key: src/a.rs//go + site: src/main.rs:5 + kind: direct + confidence: high + - key: dep//text//fmt + site: src/a.rs:4 + kind: direct + confidence: high + - key: inner//text//fmt + site: cargo:dep@1.0.0 + kind: package + confidence: high + - target: dep//sub//leaf//leaffn + status: found + path: + - key: src/main.rs//main + - key: src/a.rs//go + site: src/main.rs:5 + kind: direct + confidence: high + - key: dep//leaf//leaffn + site: src/a.rs:5 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/sub/leaf.rs//leaffn + site: cargo:dep@1.0.0//src/sub/leaf.rs:1 + kind: package + confidence: high + - target: inner//tools//run + status: found + path: + - key: src/main.rs//main + - key: src/b.rs//go + site: src/main.rs:6 + kind: direct + confidence: high + - key: dep//prelude//tools//run + site: src/b.rs:4 + kind: direct + confidence: high + - key: inner//tools//run + site: cargo:dep@1.0.0 + kind: package + confidence: high diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-reexport/project/Cargo.toml new file mode 100644 index 00000000..730014fa --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.0.0" diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/src/a.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/src/a.rs new file mode 100644 index 00000000..a5c145db --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/src/a.rs @@ -0,0 +1,6 @@ +use dep::*; + +pub fn go() { + text::fmt(); + leaf::leaffn(); +} diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/src/b.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/src/b.rs new file mode 100644 index 00000000..947ff664 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/src/b.rs @@ -0,0 +1,5 @@ +use dep::prelude::*; + +pub fn go() { + tools::run(); +} diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/src/main.rs new file mode 100644 index 00000000..efffd961 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/src/main.rs @@ -0,0 +1,7 @@ +mod a; +mod b; + +fn main() { + a::go(); + b::go(); +} diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..aa3a92c9 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "dep" +version = "1.0.0" +edition = "2021" + +[dependencies] +inner = "1.0.0" diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..e8734edf --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/lib.rs @@ -0,0 +1,5 @@ +pub use inner::*; +pub use crate::sub::*; + +pub mod prelude; +pub mod sub; diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/prelude.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/prelude.rs new file mode 100644 index 00000000..73518714 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/prelude.rs @@ -0,0 +1 @@ +pub use inner::tools; diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/sub/leaf.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/sub/leaf.rs new file mode 100644 index 00000000..5dc8e4c7 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/sub/leaf.rs @@ -0,0 +1 @@ +pub fn leaffn() {} diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/sub/mod.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/sub/mod.rs new file mode 100644 index 00000000..dccc7636 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/dep/src/sub/mod.rs @@ -0,0 +1 @@ +pub mod leaf; diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/Cargo.toml b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/Cargo.toml new file mode 100644 index 00000000..639ae42c --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "inner" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/lib.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/lib.rs new file mode 100644 index 00000000..a82a680d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/lib.rs @@ -0,0 +1,2 @@ +pub mod text; +pub mod tools; diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/text.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/text.rs new file mode 100644 index 00000000..5bab887c --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/text.rs @@ -0,0 +1 @@ +pub fn fmt() {} diff --git a/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/tools.rs b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/tools.rs new file mode 100644 index 00000000..fec4af15 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/project/vendor/inner/src/tools.rs @@ -0,0 +1 @@ +pub fn run() {} diff --git a/test/projectgraph/cases/rust/glob-use-reexport/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/glob-use-reexport/summaries/cargo_dep_1.0.0.json new file mode 100644 index 00000000..dc24eb1d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//sub//leaf//leaffn", + "definitions": [ + "cargo:dep@1.0.0//src/sub/leaf.rs//leaffn" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/glob-use-reexport/summaries/cargo_inner_1.0.0.json b/test/projectgraph/cases/rust/glob-use-reexport/summaries/cargo_inner_1.0.0.json new file mode 100644 index 00000000..e3881cb5 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-reexport/summaries/cargo_inner_1.0.0.json @@ -0,0 +1,23 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:inner@1.0.0", + "symbols": [ + { + "key": "inner//text//fmt", + "definitions": [ + "cargo:inner@1.0.0//src/text.rs//fmt" + ], + "reaches": [] + }, + { + "key": "inner//tools//run", + "definitions": [ + "cargo:inner@1.0.0//src/tools.rs//run" + ], + "reaches": [] + } + ], + "targets": [] +} From c90243816af210a2562582d7a8bfa85be47fc8fd Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:55:58 +0000 Subject: [PATCH 31/86] perf(projectgraph): keep the answer of a Rust glob path that no guard cut A unit asks for the glob path of each name that its module does not define, and each ask read every glob use of the file and asked providers again. That made the link step of the Rust corpus two fifths slower. The answer now stays for the file and the name when no cycle guard cut its read, and a glob pass that does not settle undoes it as it does providers. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/rust.go | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/projectgraph/rust.go b/projectgraph/rust.go index f43d1107..8f1bc552 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -158,6 +158,7 @@ type rustState struct { macros map[*space]map[string]string nested map[string]bool globbing map[[2]string]bool + globPaths map[[2]string]globPath members map[[2]string]bool } @@ -197,6 +198,7 @@ func (b *builder) addRustUnits() { s.routes = map[[2]string]crateRoute{} s.globs = map[string][]string{} s.globbing = map[[2]string]bool{} + s.globPaths = map[[2]string]globPath{} s.members = map[[2]string]bool{} files := map[string][]string{} for _, p := range b.paths { @@ -851,12 +853,42 @@ func (b *builder) rustGlobPath(importer, written string) (string, string, bool) } s := b.rustState() k := [2]string{importer, first} + if g, ok := s.globPaths[k]; ok { + return g.join(written) + } if s.globbing[k] { b.cuts++ return "", "", false } s.globbing[k] = true - defer delete(s.globbing, k) + cuts := b.cuts + g := b.findGlobPath(sc, first) + delete(s.globbing, k) + // The answer reads providers, so it follows the rule of that memo. + if b.cuts == cuts { + s.globPaths[k] = g + b.memo(func() { delete(s.globPaths, k) }) + } + return g.join(written) +} + +// globPath is the path of the module of another crate that a glob use of +// a file brings a name from, and the file that names the crate. +type globPath struct { + path, file string + ok bool +} + +// join returns the path of a written path under the module. +func (g globPath) join(written string) (string, string, bool) { + if !g.ok { + return "", "", false + } + return joinPath(g.path, strings.Split(written, "::")), g.file, true +} + +func (b *builder) findGlobPath(sc *scope, first string) globPath { + importer := sc.ix.Path for _, imp := range sc.wildcards { if def := b.defs[sc.enclosing(imp.Line)]; def != nil && def.Kind == fileindex.DefClass { continue @@ -872,10 +904,10 @@ func (b *builder) rustGlobPath(importer, written string) (string, string, bool) ix := b.byPath[file] o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, joinPath(path, []string{first})), form: modules.FormImport} if slices.ContainsFunc(b.providers(o), func(pv provision) bool { return pv.module != "" }) { - return joinPath(path, strings.Split(written, "::")), file, true + return globPath{path: path, file: file, ok: true} } } - return "", "", false + return globPath{} } // rustNestedName reports whether an inline module of the graph defines a From 7172aa69ad331f8924f71c15b1d9ae5a4c8b29ba Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 18:58:26 +0000 Subject: [PATCH 32/86] test(projectgraph): update the hash rows of fileindex 17 and projectgraph 20 The review fixes add cases and change the Go and Rust index output. The rows of fileindex 17 and projectgraph 20 exist only on this branch, so they change in place. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/plugin_test.go | 2 +- test/projectgraph/version_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin/indexer/plugin_test.go b/plugin/indexer/plugin_test.go index 979574c5..fd149838 100644 --- a/plugin/indexer/plugin_test.go +++ b/plugin/indexer/plugin_test.go @@ -107,7 +107,7 @@ var goldenHashes = map[string]string{ "14": "26f22c80fd4120cd04f228a53188accb1695434d0ed1ff5654b110981f8f44dd", "15": "c4b949e72be5b2b4474e06f20ae18b0cc4e080b6c5fe20ccaf712c3714bf2a12", "16": "235591d02eeb5e1bb771fbacf0ba6db228146a6a70c55670de6cb93392d75d81", - "17": "400a131636f20520cfe73263a2f96f0072137282b0339b101315b2b10b46b4d9", + "17": "a55a77ef5cb3c57064683cd88239c7edacc3f7bad6d61b9c89430d290ecd9b84", } func TestFileIndexGoldenHashFollowsVersion(t *testing.T) { diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index 6b3bcc34..e233cef5 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -34,7 +34,7 @@ var graphHashes = map[string]string{ "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", "16/18": "bb92620bf9903e8d19b52b62198a0bfd69d67856f9db226112b2cedf537b312b", "16/19": "51ba3e925c628602b3a53c1a6e68b379022a410bb5b749062b3045fef082ac8f", - "17/20": "248b1b914d23ae12a707d447987524d999a65e6b15a524573ea8a32dc89437cd", + "17/20": "5cc59915552880540c96950363befc72a16fb1ebb7a0830a523cd559dff097b7", } // resultHashes records the hash of the reach results of every case for @@ -53,7 +53,7 @@ var resultHashes = map[string]string{ "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", "16/18/5": "e0c6093df3a85353ee15c8a90ef7c2a441f23348ce80c9d49793c256bddeffc5", "16/19/5": "a935650f8e4d01c0abdceeefb3332bf779927693a1c48b28263da5365db752cd", - "17/20/5": "8beb5e0ae492aca9dfe0d79dba1e27ceaa5fd8d4850fe1b9cd97169d403f6e67", + "17/20/5": "b5e6219c16179c37bb4e5d01c4dbe762a50fa6e2e5d087849d69cfd45ac65ba7", } // summaryHashes records the hash of the golden summaries of the cases for @@ -73,7 +73,7 @@ var summaryHashes = map[string]string{ "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", "16/18/3": "694efb5f72075356359031d449d7b4ecbdca2ddd5099dacbcd2adad745ac6e19", "16/19/3": "13c72bf9eb60fc0a38bc600906fc02028d5cb60cb2aefbec0f0721779c429d80", - "17/20/3": "187eb978ad6c7ea7d5d82177a57b3e7c097e38b6b1e313a5d88dcca221191ced", + "17/20/3": "56789fdacb1789e19480212dfc6d426dde138b39bc40ff3b9b9c71105989e0f4", } func TestGraphHashFollowsVersion(t *testing.T) { From c2b4470530b54fbcb074dae4fda05387e884a356 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:26:42 +0000 Subject: [PATCH 33/86] fix(golang): close the gaps of a second review of function values - A range over a member that the index cannot name gets no record with the method name, so it gets no name edge to a method of the name. - The binding check reads the first name of the receiver too, for a parameter of a function literal that the call graph keeps raw. - An assignment types a variable that the function binds once only when each write gives that type, or the declared type is that type. - A range over a value that the source does not show to be data gets a function-value record. The link step drops the record of a member whose fields hold data, or that a reference names as a function. - A call of a field of an anonymous struct, of a dereference or of a receive, and a call of a local of a function type of another package, get a function-value record. - A linkname into a package of the graph gets an edge to its function, and a path with no dot is the standard library only when no package of the graph gives it. - A local that a range or a receive assigns keeps its call. - A binding in scope with a type of its own gives the call the target of that binding under an alias. - A record of a method value has no receiver, so the link step does not take it for a call of a field. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 68 +++- fileindex/index.go | 5 +- plugin/indexer/calls.go | 9 +- plugin/indexer/golang.go | 4 +- plugin/indexer/govalues.go | 371 ++++++++++++++---- plugin/indexer/languages.go | 9 +- plugin/indexer/project.go | 3 + plugin/indexer/references.go | 18 +- projectgraph/golang.go | 40 +- projectgraph/link.go | 22 +- projectgraph/resolve.go | 12 +- projectgraph/values.go | 32 +- .../cases/go/function-no-body/expect.yaml | 42 +- .../cases/go/function-no-body/project/main.go | 7 + .../cases/go/range-over-func/expect.yaml | 50 ++- .../cases/go/range-over-func/project/main.go | 26 ++ .../cases/go/range-over-func/project/types.go | 11 + .../cases/go/source-local-func/expect.yaml | 95 ++++- .../go/source-local-func/project/other.go | 32 ++ .../cases/go/typed-local-shadow/expect.yaml | 95 ++++- .../go/typed-local-shadow/project/main.go | 29 ++ 21 files changed, 826 insertions(+), 154 deletions(-) create mode 100644 test/projectgraph/cases/go/range-over-func/project/types.go create mode 100644 test/projectgraph/cases/go/source-local-func/project/other.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index f5c6e483..771cd3d9 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -344,11 +344,24 @@ inner block can bind the name again, as `x` in take a parameter of the name. So the index checks each call and each method value on a typed name. The binding of the name in scope there must be the binding whose type the link step reads, or a binding of the same -type. Otherwise the site is a record with the reason `unknown-receiver` -and the method name, and the method of the real type gets a `name` edge. A -short variable declaration that assigns a name of its own block, as `err` -in `n, err := f()` after `m, err := g()`, binds no new name. The case -`go/typed-local-shadow` shows it. +type. A binding in scope with a type of its own, as `x := remote{}` in the +block, gives the call the target of a typed local under an alias, as +`x#120`, so the link step reads that type. Otherwise the site is a record +with the reason `unknown-receiver` and the method name, and the method of +the real type gets a `name` edge. The index checks the first name of the +receiver too, because the call graph can keep a raw target, as `x//run` for +a parameter `x` of a function literal. A short variable declaration that +assigns a name of its own block, as `err` in `n, err := f()` after +`m, err := g()`, binds no new name. + +An assignment can type a variable that the function binds once, as `r` in +`r := o.r; if r == nil { r = &remote{} }`. The link step reads that type +only when it is the type of the variable. A declared type must be that +type, and with no declared type each write must give it, as the field `r` +of the struct `opts` of the file does. A variable that can hold an +interface, as `r` of a field `r runner`, a range variable or `l := ls[0]`, +can hold another type, so its call is a record. The case +`go/typed-local-shadow` shows each form. Rust follows the same rules, with these additions. @@ -1125,11 +1138,32 @@ a source and in a summary, it concerns every target. A local that the function binds once, to a function literal, and never assigns again, as `f` in `f := func() {...}; f()`, runs only the literal. The index gives the calls of the literal to the function, so the call of -`f` is no call of the index and has no record. A range over a parameter, a -local or a field of a declared function type, as `seq` in -`func consume(seq func(func(int) bool)) { for range seq {} }`, calls the -function value, so it gets a `function-value` record too. The case -`go/source-local-func` shows each form. +`f` is no call of the index and has no record. A range, an assignment in a +range or a receive, or an address of `f` can give it another function, so +its call keeps its records. + +The index also gives a `function-value` record to code that calls a +function value with no call record of its own: a call of a field of an +anonymous struct, as `s.act()`, a call of a dereference or a receive, as +`(*p)()` or `(<-ch)()`, and a call of a local whose declared type is a +function type of another package, as `h` in `var h http.HandlerFunc`. A +record of a method value that a reference reads, as `g := h.fn`, has no +receiver, so the link step does not take it for a call of a field. The +case `go/source-local-func` shows each form. + +A range over a function calls it, as `seq(yield)` does. So a range over a +value that the source does not show to be data gets a `function-value` +record: a parameter of a function type of another file, as `seq` in +`func consume(seq Seq) { for range seq {} }`, a local that a field gives, +an element of a slice, or the result of a call. A literal, a value of a +type of data and a local whose declaration gives such a value need no +record. A range over a member keeps the member name for the link step. The +link step drops the record when each field of the name in the graph holds +data, as `Attr []string`, or when a reference at the site names a function +or a method, as `t.All`. The index gives a range over a member no record +with the method name, because such a range mostly reads a slice or a map, +and a `name` edge to a method of the name would make a target behind it +`found`. The case `go/range-over-func` shows each form. Three gaps stay. In each of them, a search with a source can read `not-found`. @@ -1137,10 +1171,8 @@ Three gaps stay. In each of them, a search with a source can read - A field of a type of a package that no unit gives can hold a function, and no definition names that field. So a call of it keeps only the record with the name. -- A range over a value whose type the index does not see as a function - type has no record: a local that a call gives, as `seq := all()`, or a - field that no struct of the file declares. The index does not know - whether the value is a function or data. +- A field of an anonymous struct of another file can hold a function, and + no definition names it. - In Rust, a call of a parameter or a local keeps only the record with the name. @@ -1224,11 +1256,15 @@ A Go function with no body runs code that the graph does not hold too: a stub of assembly, as `func Sum(xs []int) int` with a `sum_amd64.s` file, or a function that `//go:linkname` binds to a function of another package. The index gives such a function a record with the reason -`no-dependency-source`, and the link step gives it a scope. +`no-dependency-source`. When a package of the graph holds the function of +a linkname, as `main.aliased` or `app/internal/impl.secret`, the link step +joins the stub to it with an edge in place of the record. Otherwise it +gives the record a scope. - A linkname into the standard library, or a stub of assembly in a unit of the standard library, runs code of the standard library. So its record is - a callback, as above. + a callback, as above. A path with no dot names the standard library only + when no package of the graph gives it. - The assembly of a package of a unit calls code of its package and of the runtime. So its record concerns what the code of its unit reaches. - A linkname into another package and the assembly of the project can call diff --git a/fileindex/index.go b/fileindex/index.go index c6bd98bd..ca8e4388 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -119,7 +119,10 @@ const ( // UnresolvedFunctionValue is a call of a function value, as f(x)(y) or // handlers[i](c), in a language whose function values the link step // joins to the code that holds them. The link step gives the reason - // in place of computed-name, and the record names no target. + // in place of computed-name, and the record names no target. The Go + // index gives it to a range over a member, with the member name in + // Method. The link step drops that record when each field of the name + // holds data, and clears the name otherwise. UnresolvedFunctionValue UnresolvedReason = "function-value" // UnresolvedMarkup is the markup of a component file, as the template // of a Vue component, which the index does not parse. The framework diff --git a/plugin/indexer/calls.go b/plugin/indexer/calls.go index 220ef764..c738dec8 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -32,7 +32,7 @@ func (p *projection) retargetTyped(ctx context.Context, call *fileindex.Call, un } // bindsAsWritten reports whether the call graph binds the names of a call -// as the code does, in a language with typedAt or declaredAt. The call +// as the code does, in a language with declaredAt. The call // graph reads // only some binders of a name, so it can bind a receiver to a parameter or // to the value of a let while a pattern binds the name again, as t in @@ -43,9 +43,6 @@ func (p *projection) retargetTyped(ctx context.Context, call *fileindex.Call, un // name must have its declared type at the call. func (p *projection) bindsAsWritten(ctx context.Context, call fileindex.Call) bool { rules := p.rules() - if rules.typedAt != nil && !rules.typedAt(p, call) { - return false - } if rules.declaredAt == nil { return true } @@ -120,6 +117,10 @@ func (p *projection) addCalls(ctx context.Context) { p.addRecord(call, fileindex.UnresolvedUnknownReceiver) continue } + if typed := p.rules().typedAt; typed != nil && !typed(p, &call) { + p.addRecord(call, fileindex.UnresolvedUnknownReceiver) + continue + } if !p.bindsAsWritten(ctx, call) { p.addRecord(call, fileindex.UnresolvedUnknownReceiver) continue diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 4761df6f..01b118f3 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -27,7 +27,7 @@ var goRules = languageRules{ definitions: goDefinitions, typedAt: goTypedCall, literalCall: goLiteralCall, - visitors: []func(p *projection) visitor{referenceVisitor, goRangeVisitor}, + visitors: []func(p *projection) visitor{referenceVisitor, goValueVisitor}, references: &referenceSyntax{ holders: map[string]string{ "argument_list": "", @@ -45,6 +45,8 @@ var goRules = languageRules{ // grammar parses as a conversion. "type_conversion_expression": "operand", }, + namedOnly: map[string]bool{"range_clause": true}, + bareValues: true, member: "selector_expression", object: "operand", property: "field", diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index d9c70cf8..92c9e0ec 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -1,7 +1,9 @@ package indexer import ( + "fmt" "slices" + "strings" "github.com/safedep/code/core/ast" "github.com/safedep/code/fileindex" @@ -217,8 +219,9 @@ func (p *projection) goPredeclared(name string) bool { // function is in scope, and the start of the block that declares it. type goScope struct { from, to, block uint32 - // typ is the declared type of a parameter or a var, or nil. - typ *sitter.Node + // typ is the declared type of a parameter or a var, or nil. value is + // the value that a declaration gives the name, or nil. + typ, value *sitter.Node } // goBlocks are the nodes whose extent is the scope of a name that a @@ -255,6 +258,109 @@ func (p *projection) goBinding(caller, name string, at uint32) (goScope, bool) { return binding, found } +// goAssignedType reports whether the one variable of a name has the type +// that an assignment gives it, as the link step reads it. A declared type +// is the type of the variable, so it must be that type: a declared +// interface holds any type that implements it. With no declared type, +// each write of the variable must give that type, as the declaration +// cl := o.c with a field c *client and cl = &client{}. A write whose type +// the index does not know, as a range or l := ls[0], can give another type. +func (p *projection) goAssignedType(caller, name string, binding goScope, read fileindex.Typed) bool { + if read.Result { + return false + } + if binding.typ != nil { + return p.goTypeName(binding.typ) == read.Type + } + fn := p.syntax[caller] + if fn == nil { + return false + } + agree := true + walkBody(fn.ChildByFieldName("body"), nil, func(node *sitter.Node) { + var names []*sitter.Node + var right *sitter.Node + switch node.Type() { + case "short_var_declaration", "assignment_statement": + if left := node.ChildByFieldName("left"); left != nil { + names = ts.NamedChildren(left) + } + right = node.ChildByFieldName("right") + case "var_spec": + names, right = childrenByField(node, "name"), node.ChildByFieldName("value") + case "range_clause", "receive_statement": + if left := node.ChildByFieldName("left"); left != nil { + names = ts.NamedChildren(left) + } + } + i := slices.IndexFunc(names, func(n *sitter.Node) bool { return p.text(n) == name }) + if i < 0 { + return + } + var values []*sitter.Node + if right != nil && node.Type() != "range_clause" && node.Type() != "receive_statement" { + values = ts.NamedChildren(right) + } + if len(values) != len(names) { + agree = false + return + } + t, ok := p.goValueType(values[i]) + if !ok { + t, ok = p.goFieldValueType(caller, values[i]) + } + agree = agree && ok && !t.Result && t.Type == read.Type + }) + return agree +} + +// goFieldValueType returns the declared type of a field that a value +// reads, as client for o.c with a parameter o opts and a field c *client +// of the struct opts of the file. +func (p *projection) goFieldValueType(caller string, value *sitter.Node) (fileindex.Typed, bool) { + def := p.defs[caller] + if def == nil || value.Type() != "selector_expression" { + return fileindex.Typed{}, false + } + operand, field := value.ChildByFieldName("operand"), value.ChildByFieldName("field") + if operand == nil || field == nil || operand.Type() != "identifier" { + return fileindex.Typed{}, false + } + i := slices.IndexFunc(def.Params, func(t fileindex.Typed) bool { return t.Name == p.text(operand) }) + if i < 0 { + return fileindex.Typed{}, false + } + t := p.goFileField(def.Params[i].Type, p.text(field)) + if typ := p.goTypeName(t); typ != "" { + return fileindex.Typed{Type: typ}, true + } + return fileindex.Typed{}, false +} + +// goFileField returns the type of a field of a struct that the file +// declares, or nil. +func (p *projection) goFileField(structName, field string) *sitter.Node { + for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { + if statement.Type() != "type_declaration" { + continue + } + for _, spec := range ts.NamedChildren(statement) { + t := spec.ChildByFieldName("type") + if spec.Type() != "type_spec" || t == nil || t.Type() != "struct_type" || p.text(spec.ChildByFieldName("name")) != structName { + continue + } + for _, f := range p.goStructFields(t) { + for _, n := range childrenByField(f, "name") { + if p.text(n) == field { + return f.ChildByFieldName("type") + } + } + } + } + } + return nil +} + // goScopesOf returns the scopes of each local name of the caller, as // goScopes gives them, once per caller. func (p *projection) goScopesOf(caller string) map[string][]goScope { @@ -307,8 +413,9 @@ func (p *projection) goTypedAt(caller, name string, at uint32) bool { case origin == goAssignedOrigin && len(p.goScopesOf(caller)[name]) == 1: // The function binds the name once, so the assignment types that // one variable, as cl in cl := o.c; cl = &client{} or a named - // result c in c = &client{}. - return true + // result c in c = &client{}, if the type of the variable is that + // type. + return p.goAssignedType(caller, name, binding, read) } for i, t := range def.Locals { if t.Name == name && i < len(origins) && origins[i] == binding.from { @@ -319,111 +426,193 @@ func (p *projection) goTypedAt(caller, name string, at uint32) bool { } // goTypedCall reports whether the type that the link step reads for each -// typed name that starts a target of a call holds at the call. -func goTypedCall(p *projection, call fileindex.Call) bool { - for _, target := range call.Targets { - if name, ok := p.typedName(call.Caller, target); ok && !p.goTypedAt(call.Caller, name, p.byteAt(call.Line, call.Column)) { - return false +// typed name that starts a target or the receiver of a call holds at the +// call. The call graph can keep a raw target for a name that a function +// literal binds, as x//run for x.run() in go func(x B) { x.run() }(b), +// and the link step then reads the type of the parameter x of the caller. +// When the binding in scope has a type of its own, as file in a second +// file, err := os.Create(p) of a sibling block, the call gets the target +// of that binding under its alias. +func goTypedCall(p *projection, call *fileindex.Call) bool { + at := p.byteAt(call.Line, call.Column) + names := strings.Split(call.Receiver, ".") + name := names[0] + if !isIdentifier(name) || p.goTypedAt(call.Caller, name, at) { + for _, target := range call.Targets { + if n, ok := p.typedName(call.Caller, target); ok && n != name && !p.goTypedAt(call.Caller, n, at) { + return false + } } + return true } + alias, ok := p.goAlias(call.Caller, name, at) + if !ok { + return false + } + call.Targets = []string{fileindex.Join(call.Caller, append(append([]string{alias}, names[1:]...), call.Method)...)} return true } -// goRangeVisitor records each range over a function value whose code -// the index cannot name, as seq in func consume(seq func(func(int) bool)) -// { for range seq {} }. The range calls the value, as seq(yield) does. A -// search from the roots reaches the reference to the function that the -// value holds. A search from a source can miss it, so the range gets a -// record, which the link step makes a function-value record. The index -// reads a parameter or a var of a declared function type, and a field -// that a struct of the file declares with a function type. -func goRangeVisitor(p *projection) visitor { - return func(node *sitter.Node, kind string) { - if kind != "range_clause" { - return +// goAlias returns a name under which the link step reads the type of the +// binding of a name in scope at the byte, when that binding has a type of +// its own. The index adds a typed local of the alias to the caller once. +func (p *projection) goAlias(caller, name string, at uint32) (string, bool) { + def := p.defs[caller] + binding, bound := p.goBinding(caller, name, at) + if def == nil || !bound { + return "", false + } + origins := p.goOrigins[caller] + i := slices.IndexFunc(def.Locals, func(t fileindex.Typed) bool { return t.Name == name }) + for j := i; i >= 0 && j < len(def.Locals) && j < len(origins); j++ { + t := def.Locals[j] + if t.Name != name || origins[j] != binding.from { + continue } - value := node.ChildByFieldName("right") - for value != nil && value.Type() == "parenthesized_expression" { - value = value.NamedChild(0) + alias := fmt.Sprintf("%s#%d", name, binding.from) + if !slices.ContainsFunc(def.Locals, func(l fileindex.Typed) bool { return l.Name == alias }) { + t.Name = alias + def.Locals = append(def.Locals, t) + p.goOrigins[caller] = append(origins, binding.from) } - if value == nil || !p.goRangesValue(value) { - return + return alias, true + } + return "", false +} + +// goValueVisitor records the Go code that calls a function value with no +// call record of its own. The link step makes each record a function-value +// record, which concerns every target in a search with a source. +// +// - A range over a value that the source does not show to be data, as +// seq in func consume(seq func(func(int) bool)) { for range seq {} }. +// The range calls the value, as seq(yield) does. A range over a member +// keeps the member name in Method, and the link step drops the record +// when each field of the name holds data. +// - A call of a field of an anonymous struct of the file, as s.run() in +// for _, s := range []struct{ run func() }{...}. No definition names +// such a field, so the link step cannot see it. +// - A call of a dereference or a receive, as (*p)() or (<-ch)(), which +// the call graph does not record. +func goValueVisitor(p *projection) visitor { + return func(node *sitter.Node, kind string) { + switch kind { + case "range_clause": + value := goUnparen(node.ChildByFieldName("right")) + if value == nil { + return + } + if method, ok := p.goRangeRecord(value); ok { + p.goValueRecord(value, method) + } + case "call_expression": + function := goUnparen(node.ChildByFieldName("function")) + switch { + case function == nil: + case function.Type() == "unary_expression" && (goOperator(function) == "*" || goOperator(function) == "<-"): + p.goValueRecord(node, "") + case function.Type() == "selector_expression" && p.goAnonFields()[p.text(function.ChildByFieldName("field"))]: + p.goValueRecord(node, "") + } } - site := ast.GetNodePosition(value) - p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ - Caller: p.caller(p.innermost(value.StartByte()), site), - Line: int(site.StartLine), - Column: int(site.StartColumn), - Text: callgraph.ClipText(p.text(value)), - Reason: fileindex.UnresolvedComputedName, - }) } } -// goRangesValue reports whether a range expression holds a function value -// of a declared function type: a local of the caller, or a field. -func (p *projection) goRangesValue(value *sitter.Node) bool { +// goValueRecord adds the record of a call of a function value at the node. +// A record with a member name is a candidate that the link step checks. +func (p *projection) goValueRecord(node *sitter.Node, member string) { + site := ast.GetNodePosition(node) + u := fileindex.Unresolved{ + Caller: p.caller(p.innermost(node.StartByte()), site), + Line: int(site.StartLine), + Column: int(site.StartColumn), + Text: callgraph.ClipText(p.text(node)), + Reason: fileindex.UnresolvedComputedName, + } + if member != "" { + u.Receiver = callgraph.ClipText(p.text(node.ChildByFieldName("operand"))) + u.Method, u.Reason = member, fileindex.UnresolvedFunctionValue + } + p.ix.Unresolved = append(p.ix.Unresolved, u) +} + +// goRangeRecord reports whether a range over a value needs a record, and +// the member name that the link step checks. A value that the source +// shows to be data needs none: a literal, a value of a type of data, or a +// local whose declaration gives it such a value. A name of the package +// reaches its value through the load code of the package. A member is a +// candidate that the link step checks against the fields and the +// references of the graph. +func (p *projection) goRangeRecord(value *sitter.Node) (string, bool) { + caller := p.innermost(value.StartByte()) + if p.goDataValue(value) { + return "", false + } switch value.Type() { case "identifier": - binding, ok := p.goBinding(p.innermost(value.StartByte()), p.text(value), value.StartByte()) - return ok && binding.typ != nil && p.goFuncType(binding.typ, 0) + binding, ok := p.goBinding(caller, p.text(value), value.StartByte()) + switch { + case !ok: + return "", false + case binding.typ != nil: + return "", !p.goDataField(binding.typ) + case binding.value != nil: + return "", !p.goDataValue(goUnparen(binding.value)) + } + return "", true case "selector_expression": - return p.goFuncField(p.text(value.ChildByFieldName("field"))) + return p.text(value.ChildByFieldName("field")), true + case "type_assertion_expression": + return "", !p.goDataField(value.ChildByFieldName("type")) } - return false + return "", true } -// goFuncType reports whether a type is a function type: a literal -// function type, a type of the package iter, as iter.Seq[int], or a type -// of the file whose declaration is a function type. -func (p *projection) goFuncType(t *sitter.Node, depth int) bool { - if t == nil || depth > maxReferenceSegments { - return false - } - switch t.Type() { - case "function_type": - return true - case "parenthesized_type": - return p.goFuncType(t.NamedChild(0), depth+1) - case "generic_type": - return p.goFuncType(t.ChildByFieldName("type"), depth+1) - case "qualified_type": - return p.goImportPath(p.text(t.ChildByFieldName("package"))) == "iter" - case "type_identifier": - return p.goFuncType(p.goFileType(t), depth+1) +// goUnparen returns the expression in parentheses. +func goUnparen(node *sitter.Node) *sitter.Node { + for depth := 0; node != nil && node.Type() == "parenthesized_expression" && depth < maxReferenceSegments; depth++ { + node = node.NamedChild(0) } - return false + return node } -// goFuncField reports whether a struct of the file declares a field of the -// name with a function type. -func (p *projection) goFuncField(name string) bool { - for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { - if statement.Type() != "type_declaration" { - continue +// goAnonFields returns the names of the fields of each anonymous struct of +// the file that can hold a function. +func (p *projection) goAnonFields() map[string]bool { + if p.anonFields != nil { + return p.anonFields + } + p.anonFields = map[string]bool{} + var walk func(node *sitter.Node, depth int) + walk = func(node *sitter.Node, depth int) { + if depth > maxLocalDepth { + return } - for _, spec := range ts.NamedChildren(statement) { - t := spec.ChildByFieldName("type") - if spec.Type() != "type_spec" || t == nil || t.Type() != "struct_type" { - continue - } - for _, field := range p.goStructFields(t) { - for _, n := range childrenByField(field, "name") { - if p.text(n) == name && p.goFuncType(field.ChildByFieldName("type"), 0) { - return true + if node.Type() == "struct_type" { + if parent := node.Parent(); parent == nil || parent.Type() != "type_spec" { + for _, field := range p.goStructFields(node) { + if !p.goDataField(field.ChildByFieldName("type")) { + for _, name := range childrenByField(field, "name") { + p.anonFields[p.text(name)] = true + } } } } } + for _, child := range ts.NamedChildren(node) { + walk(child, depth+1) + } } - return false + walk(p.tree.Tree().RootNode(), 0) + return p.anonFields } // goLiteralCall reports whether a call names a local that the caller // binds once, to a function literal, and never assigns again, as f in // f := func() {...}; f(). The index gives the calls of the literal to the // caller, so the call runs no code that the caller does not hold. An -// assignment or an address of the name can give it another function. +// assignment, a range or a receive into the name, or an address of it, can +// give it another function. func goLiteralCall(p *projection, call fileindex.Call) bool { name := call.Callee if call.Receiver != "" || !isIdentifier(name) || len(p.goScopesOf(call.Caller)[name]) != 1 { @@ -457,7 +646,7 @@ func goLiteralCall(p *projection, call fileindex.Call) bool { literal = len(values) == len(names) && values[i].Type() == "func_literal" } } - case "assignment_statement", "inc_statement", "dec_statement": + case "assignment_statement", "inc_statement", "dec_statement", "range_clause", "receive_statement": if left := node.ChildByFieldName("left"); left != nil && slices.ContainsFunc(ts.NamedChildren(left), func(n *sitter.Node) bool { return p.text(n) == name }) { other = true } @@ -508,9 +697,13 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { if function == nil || function.Type() != "function_declaration" && function.Type() != "method_declaration" { return locals } - add := func(nodes []*sitter.Node, from uint32, block, typ *sitter.Node, redeclares bool) { + add := func(nodes []*sitter.Node, from uint32, block, typ *sitter.Node, redeclares bool, list *sitter.Node) { start := goBlockStart(block) - for _, n := range nodes { + var values []*sitter.Node + if list != nil { + values = ts.NamedChildren(list) + } + for i, n := range nodes { if n.Type() != "identifier" { continue } @@ -520,7 +713,11 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { if redeclares && slices.ContainsFunc(locals[name], func(s goScope) bool { return s.block == start && s.to == block.EndByte() }) { continue } - locals[name] = append(locals[name], goScope{from: from, to: block.EndByte(), block: start, typ: typ}) + scope := goScope{from: from, to: block.EndByte(), block: start, typ: typ} + if len(values) == len(nodes) { + scope.value = values[i] + } + locals[name] = append(locals[name], scope) } } var walk func(node, block *sitter.Node, depth int) @@ -538,20 +735,20 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { } switch node.Type() { case "parameter_declaration", "variadic_parameter_declaration": - add(childrenByField(node, "name"), goParamOrigin(block), block, node.ChildByFieldName("type"), false) + add(childrenByField(node, "name"), goParamOrigin(block), block, node.ChildByFieldName("type"), false, nil) case "short_var_declaration": - add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, nil, true) + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, nil, true, node.ChildByFieldName("right")) case "var_spec", "const_spec": - add(childrenByField(node, "name"), node.EndByte(), block, node.ChildByFieldName("type"), false) + add(childrenByField(node, "name"), node.EndByte(), block, node.ChildByFieldName("type"), false, node.ChildByFieldName("value")) case "type_spec", "type_alias": - add(childrenByField(node, "name"), node.StartByte(), block, nil, false) + add(childrenByField(node, "name"), node.StartByte(), block, nil, false, nil) case "range_clause", "receive_statement": if goDeclares(node) { - add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, nil, false) + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, nil, false, nil) } case "type_switch_statement": if alias := node.ChildByFieldName("alias"); alias != nil { - add(ts.NamedChildren(alias), alias.EndByte(), block, nil, false) + add(ts.NamedChildren(alias), alias.EndByte(), block, nil, false, nil) } } for _, child := range ts.NamedChildren(node) { diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 20cfe544..0c9f3e97 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -46,10 +46,11 @@ type languageRules struct { // the value of another binding. rebound func(ctx context.Context, p *projection, call fileindex.Call, name string) bool // typedAt reports whether the declared type that the link step reads - // for the receiver of a call holds at the call. The Go index records - // the typed locals of a function with no extent, so an inner block can - // bind the name of the receiver again. - typedAt func(p *projection, call fileindex.Call) bool + // for the receiver of a call holds at the call, and can retarget the + // call to the binding in scope. The Go index records the typed locals + // of a function with no extent, so an inner block can bind the name of + // the receiver again. A false result makes the call a record. + typedAt func(p *projection, call *fileindex.Call) bool // literalCall reports whether a call runs a function literal whose // calls the index gives to the caller already, as f() after // f := func() {...} in Go. Such a call needs no edge and no record. diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index 0b9412e6..c3b550a0 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -65,6 +65,9 @@ type projection struct { // linknames holds the remote name of each local name that a // //go:linkname directive of a Go file binds. linknames map[string]string + // anonFields holds the names of the fields of the anonymous structs of + // a Go file that can hold a function. + anonFields map[string]bool // rustMacros holds the names that a macro_rules! or a use of a Rust // file binds, once a call of a macro reads them. rustMacros map[string]bool diff --git a/plugin/indexer/references.go b/plugin/indexer/references.go index cd20b424..f64c4f86 100644 --- a/plugin/indexer/references.go +++ b/plugin/indexer/references.go @@ -43,6 +43,16 @@ type referenceSyntax struct { // files of a unit with no import, as C#. A name that the file does not // bind keeps its text, and the link step resolves it or drops it. rawNames bool + // namedOnly holds the holders whose value counts only when the index + // names its function, as the expression of a Go range. A range over a + // member that the index cannot name, as n.Attr, mostly reads a slice or + // a map, and a record with the method name would give it a name edge + // to each method of the name. + namedOnly map[string]bool + // bareValues marks a language whose record of a method value keeps no + // receiver, as Go. The link step then tells it from a call, which can + // run a function value of a field. + bareValues bool // value replaces reference for a language whose function values take // more forms than a name or a member chain, as Go. It gives the member // node of a method value whose receiver the index cannot name, and the @@ -120,7 +130,7 @@ func referenceVisitor(p *projection) visitor { case seen[k]: case target != "": p.addReference(value, caller, p.targets([]string{target})) - default: + case !syntax.namedOnly[kind]: p.addValueRecord(member, caller) } seen[k] = true @@ -202,12 +212,16 @@ func (p *projection) addValueRecord(member *sitter.Node, caller string) { syntax := p.rules().references name := member.ChildByFieldName(syntax.property) site := ast.GetNodePosition(name) + receiver := callgraph.ClipText(p.text(member.ChildByFieldName(syntax.object))) + if syntax.bareValues { + receiver = "" + } p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ Caller: p.caller(caller, site), Line: int(site.StartLine), Column: int(site.StartColumn), Text: callgraph.ClipText(p.text(member)), - Receiver: callgraph.ClipText(p.text(member.ChildByFieldName(syntax.object))), + Receiver: receiver, Method: p.text(name), Reason: fileindex.UnresolvedUnknownReceiver, }) diff --git a/projectgraph/golang.go b/projectgraph/golang.go index fd419cc1..f5049181 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -447,21 +447,53 @@ func (b *builder) goOutsideRecords(key string) []UnresolvedSite { return []UnresolvedSite{{Caller: key, Text: key, Reason: fileindex.UnresolvedNoDependencySource}} } +// linkGoStub joins a Go function with no body to the function that a +// //go:linkname binds it to, when a package of the graph holds that +// function, and reports whether it did. The receiver of the record names +// the package of the linkname and the text its full name, as +// app/internal/impl.secret, or main.target for the package of the file. +func (b *builder) linkGoStub(ix *fileindex.Index, site UnresolvedSite) bool { + pkg := site.Receiver + name, ok := strings.CutPrefix(site.Text, pkg+".") + if pkg == "" || !ok || !isIdentifier(name) { + return false + } + var keys []string + if pkg == ix.Package { + if key, ok := b.resolveScopeName(b.scopes[ix.Path], name); ok && !b.g.IsExternal(key) { + keys = append(keys, key) + } + } + for _, m := range b.resolveModules(ix, pkg, modules.FormImport) { + if r := b.walk(m.key, []string{name}); r.fail == nil && !b.g.IsExternal(r.key) { + keys = append(keys, r.key) + } + } + for _, key := range keys { + b.link(ix.Language, site.Caller, key, site.Site, EdgeDirect, ruleLocal) + } + return len(keys) > 0 +} + // scopeGoStub gives the record of a Go function with no body the scope of -// the code that runs in its place. The receiver of the record names the -// package of a //go:linkname target. +// the code that runs in its place, when no package of the graph holds +// that code. The receiver of the record names the package of a +// //go:linkname target. // // - Code of the standard library calls back only a method of an exported // name, as goOutsideRecords argues. So a linkname into the standard -// library, or a stub of assembly in it, gets the callback. +// library, or a stub of assembly in it, gets the callback. A path with +// no dot can also name a package of a module, as app/internal/impl in +// the module app, so the path must name no package of the graph. // - The assembly of a package of a unit calls code of its package and of // the runtime. So its record concerns what the code of its unit reaches. // - A linkname into another package and the assembly of the project can // call any function, so their records concern every target. func (b *builder) scopeGoStub(ix *fileindex.Index, site *UnresolvedSite) { unit, _ := fileindex.ParseUnit(ix.Unit) + std := site.Receiver != "" && site.Receiver != ix.Package && modules.IsStdPath(site.Receiver) && len(b.resolveModules(ix, site.Receiver, modules.FormImport)) == 0 switch { - case site.Receiver != "" && modules.IsStdPath(site.Receiver), site.Receiver == "" && unit.Ecosystem == "go" && unit.Name == modules.StdModule: + case std, site.Receiver == "" && unit.Ecosystem == "go" && unit.Name == modules.StdModule: site.Callback = true case site.Receiver == "" && ix.Unit != "": site.Unit = ix.Unit diff --git a/projectgraph/link.go b/projectgraph/link.go index 1751849c..31280fd3 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -136,9 +136,13 @@ type builder struct { bindings map[string]string classes map[string]classRecord // defs holds the definition records by key, and fieldNames the names - // of the fields among them. + // of the Go fields among them, true for a name that a field which can + // hold a function has. defs map[string]*fileindex.Definition fieldNames map[string]bool + // valueSites holds the sites of the references to a function or a + // method. + valueSites map[Site]bool // reading holds the variables whose value a walk reads now, so a cycle // of variables across files stops. stepping holds the names that a // walk of a unit reads now for the result type of an external key. @@ -191,8 +195,8 @@ type builder struct { // cuts counts the answers that a cycle guard or the hop limit cut // short. A memo that a read with a cut gives can lack an answer that // another way in finds. - cuts int - children map[string][]string + cuts int + children map[string][]string // aliases holds the definitions of each class that the language joins // to one class, the class too. aliases map[string][]string @@ -300,6 +304,7 @@ func newTables(indexes []*fileindex.Index, opts Options) *builder { scopes: make(map[string]*scope, len(indexes)), edges: newEdgeSet(sites), unresolved: map[UnresolvedSite]bool{}, + valueSites: map[Site]bool{}, truncations: map[Truncation]bool{}, names: map[nameKey]resolution{}, visiting: map[nameKey]bool{}, @@ -513,10 +518,21 @@ func (b *builder) copyUnresolved(ix *fileindex.Index) { if u.Reason == fileindex.UnresolvedComputedName && profileOf(ix.Language).functionValues { site.Reason = fileindex.UnresolvedFunctionValue } + // The Go index gives a range over a member the member name, and + // the fields of the graph decide whether the range reads data. + if u.Reason == fileindex.UnresolvedFunctionValue && u.Method != "" { + if b.rangesNoValue(u, at) { + continue + } + site.Method = "" + } if u.Reason == fileindex.UnresolvedDynamicImport { site.ImportPrefix = b.importPrefix(ix, u.Line) } if u.Reason == fileindex.UnresolvedNoDependencySource && ix.Language == langcode.Go { + if b.linkGoStub(ix, site) { + continue + } b.scopeGoStub(ix, &site) } b.addUnresolved(site) diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index ed6372b3..2504b77c 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -96,10 +96,11 @@ func (b *builder) linkCalls(ix *fileindex.Index) { } continue } - // A local of a declared function type, as g in var g F = h, - // holds a function value. The walk ends at the type F, and no - // edge names the function. - if r.fail == nil && r.declared && call.Receiver == "" && sc.profile.functionValues && b.isType(r.key) { + // A local of a declared function type, as g in var g F = h or + // h in var h http.HandlerFunc = m["a"], holds a function value. + // The walk ends at the type, a node of the graph or an external + // key, and no edge names the function. + if r.fail == nil && r.declared && call.Receiver == "" && sc.profile.functionValues && !b.isFunction(r.key) { b.addUnresolved(UnresolvedSite{Caller: from, Site: site, Text: call.Callee, Reason: fileindex.UnresolvedFunctionValue}) } if r.unbound && sc.profile.thisCalls { @@ -367,6 +368,9 @@ func (b *builder) linkReferences(ix *fileindex.Index) { if sc.profile.typeConversions && b.isType(r.key) || b.holdsData(r.key) { continue } + if b.isFunction(r.key) { + b.valueSites[site] = true + } b.link(ix.Language, from, r.key, site, EdgeReference, ruleReference) } } diff --git a/projectgraph/values.go b/projectgraph/values.go index d39cb65e..6687d573 100644 --- a/projectgraph/values.go +++ b/projectgraph/values.go @@ -58,21 +58,43 @@ func (b *builder) recordValueCalls() { // callsValue reports whether a call with an unknown receiver can call a // function value: a name with no receiver, or a member that a field of the -// graph names. Only a field of a language with functionValues counts, and -// not one whose type the index shows to hold data, as Name string. +// graph names and that can hold a function. A record of a method value, as +// the index gives it for x.f in g(x.f), has no receiver, so only a call +// counts. func (b *builder) callsValue(u UnresolvedSite) bool { if u.Receiver == "" { return isIdentifier(u.Text) } + holds, _ := b.valueField(u.Method) + return holds +} + +// valueField reports whether a field of the name in a language with +// functionValues can hold a function, and whether the graph has a field +// of the name at all. The index marks a field of a type of data, as +// Name string, with Data. +func (b *builder) valueField(name string) (holds, known bool) { if b.fieldNames == nil { b.fieldNames = map[string]bool{} for key, def := range b.defs { - if def.Kind == fileindex.DefField && !def.Data && profileOf(b.languageOf(key)).functionValues { - b.fieldNames[def.Name] = true + if def.Kind == fileindex.DefField && profileOf(b.languageOf(key)).functionValues { + b.fieldNames[def.Name] = b.fieldNames[def.Name] || !def.Data } } } - return b.fieldNames[u.Method] + holds, known = b.fieldNames[name] + return holds, known +} + +// rangesNoValue reports whether the index record of a range over a member +// needs no record: each field of the member name in the graph holds data, +// or the member is a function or a method with a reference edge at the +// site, as t.All in for x := range t.All. A member that no field of the +// graph names, as a field of a type of a package that no unit gives, can +// hold a function. +func (b *builder) rangesNoValue(u fileindex.Unresolved, at Site) bool { + holds, known := b.valueField(u.Method) + return known && !holds || b.valueSites[at] } // reflection is a package of a language that gives or runs a function diff --git a/test/projectgraph/cases/go/function-no-body/expect.yaml b/test/projectgraph/cases/go/function-no-body/expect.yaml index 847d0e55..b0f02ce8 100644 --- a/test/projectgraph/cases/go/function-no-body/expect.yaml +++ b/test/projectgraph/cases/go/function-no-body/expect.yaml @@ -1,15 +1,18 @@ source: safedep:hand description: >- - Three functions have no body. add of the project and Sum of the unit fast are stubs of assembly, and - the assembly calls slow and fallback. now is a function that //go:linkname binds to runtime.nanotime. - The code of each one is not Go source of the graph, so the index gives each stub a record with the - reason no-dependency-source. The assembly of the project can call any function, so its record concerns - every target, and os.Remove and os.Getwd are uncertain from viaAsm. The assembly of a unit calls the - code of its package and of the runtime, so its record concerns what the code of the unit reaches. + Three functions have no body. add of the project and Sum of the unit fast are stubs of assembly, and the + assembly calls slow and fallback. now is a function that //go:linkname binds to runtime.nanotime. The + code of each one is not Go source of the graph, so the index gives each stub a record with the reason + no-dependency-source. The assembly of the project can call any function, so its record concerns every + target, and os.Remove and os.Getwd are uncertain from viaAsm. The assembly of a unit calls the code of + its package and of the runtime, so its record concerns what the code of the unit reaches. os/exec.LookPath is uncertain from viaUnit, and os.Getwd is not found from it. The code of the standard library calls back only a method of an exported name, so the record of now is a callback. os.Chdir, which closer.Close calls, is uncertain from viaLinkname, and os.Getwd is not found from it. With no - record, each search ended at the stub, and each target read not-found. + record, each search ended at the stub, and each target read not-found. alias is bound by //go:linkname + to aliased of its own package main. The path main has no dot, as a path of the standard library, but a + package of the graph gives it. So the link step joins alias to aliased with an edge, and os.Mkdir is + found from viaAlias. A callback there read not-found. units: gomod/github.com/acme/fast@v1.0.0: go:github.com/acme/fast@v1.0.0 roots: @@ -29,6 +32,9 @@ unresolved: - caller: os//Getwd reason: no-dependency-source callback: true + - caller: os//Mkdir + reason: no-dependency-source + callback: true - caller: os//Remove reason: no-dependency-source callback: true @@ -103,3 +109,25 @@ results: - caller: main.go//unused site: main.go:17 reached: false + - target: os//Mkdir + sources: [main.go//viaAlias] + sources_only: true + status: found + path: + - key: main.go//viaAlias + - key: main.go//alias + site: main.go:36 + kind: direct + confidence: high + - key: main.go//aliased + site: main.go:32 + kind: direct + confidence: high + - key: os//Mkdir + site: main.go:34 + kind: direct + confidence: high + call_sites: + - caller: main.go//aliased + site: main.go:34 + reached: true diff --git a/test/projectgraph/cases/go/function-no-body/project/main.go b/test/projectgraph/cases/go/function-no-body/project/main.go index 28e86437..5fae7f5c 100644 --- a/test/projectgraph/cases/go/function-no-body/project/main.go +++ b/test/projectgraph/cases/go/function-no-body/project/main.go @@ -27,3 +27,10 @@ func main() { viaUnit() viaLinkname() } + +//go:linkname alias main.aliased +func alias() + +func aliased() { os.Mkdir("d", 0o755) } + +func viaAlias() { alias() } diff --git a/test/projectgraph/cases/go/range-over-func/expect.yaml b/test/projectgraph/cases/go/range-over-func/expect.yaml index 4730f455..9581049d 100644 --- a/test/projectgraph/cases/go/range-over-func/expect.yaml +++ b/test/projectgraph/cases/go/range-over-func/expect.yaml @@ -7,7 +7,13 @@ description: >- is not found. consume ranges over its parameter seq of a function type, and drain over the field each of a function type. Each range calls a function value that a search from a source does not reach, so it is a function-value record, and each target is uncertain from consume and from drain. A range with no - record read not-found there. + record read not-found there. attrs ranges over the field Attr of a node, which holds a slice. A range + over a member that the index cannot name keeps no record with the method name, so it gets no name edge + to element.Attr, and os.Chdir is not found from attrs. The link step drops the record of the range, + because each field Attr of the graph holds data. typed ranges over a parameter of the type Seq of + another file, and viaLocal over a local that holds the field Each of a holder. The index cannot show + that either value is data, so each range is a function-value record, and os.Remove is uncertain from + each. roots: - key: main.go kind: main @@ -15,6 +21,9 @@ roots: - key: main.go//main kind: main rule: go.main + - key: types.go + kind: main + rule: go.main edges: - from: main.go//main to: main.go//walk @@ -40,6 +49,9 @@ unresolved: - caller: main.go//walk reason: no-dependency-source callback: true + - caller: os//Chdir + reason: no-dependency-source + callback: true - caller: os//Getwd reason: no-dependency-source callback: true @@ -69,6 +81,12 @@ unresolved: - caller: main.go//drain site: main.go:46 reason: function-value + - caller: main.go//typed + site: main.go:67 + reason: function-value + - caller: main.go//viaLocal + site: main.go:73 + reason: function-value results: - target: os//Remove status: found @@ -130,3 +148,33 @@ results: uncertainties: - kind: unresolved-call site: main.go:46 + - target: os//Chdir + sources: [main.go//attrs] + sources_only: true + status: not-found + call_sites: + - caller: main.go//element//Attr + site: main.go:54 + reached: false + - target: os//Remove + sources: [main.go//typed] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//walk + site: main.go:9 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:67 + - target: os//Remove + sources: [main.go//viaLocal] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//walk + site: main.go:9 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:73 diff --git a/test/projectgraph/cases/go/range-over-func/project/main.go b/test/projectgraph/cases/go/range-over-func/project/main.go index ed8522c7..979d7cc5 100644 --- a/test/projectgraph/cases/go/range-over-func/project/main.go +++ b/test/projectgraph/cases/go/range-over-func/project/main.go @@ -47,3 +47,29 @@ func drain(s source) { _ = x } } + +type element struct{} + +func (element) Attr() string { + os.Chdir("/") + return "" +} + +func attrs(ns []*node) { + for _, n := range ns { + for _, a := range n.Attr { + _ = a + } + } +} + +func typed(seq Seq) { + for range seq { + } +} + +func viaLocal(h holder) { + seq := h.Each + for range seq { + } +} diff --git a/test/projectgraph/cases/go/range-over-func/project/types.go b/test/projectgraph/cases/go/range-over-func/project/types.go new file mode 100644 index 00000000..95174a18 --- /dev/null +++ b/test/projectgraph/cases/go/range-over-func/project/types.go @@ -0,0 +1,11 @@ +package main + +type node struct { + Attr []string +} + +type Seq func(func(string) bool) + +type holder struct { + Each func(func(string) bool) +} diff --git a/test/projectgraph/cases/go/source-local-func/expect.yaml b/test/projectgraph/cases/go/source-local-func/expect.yaml index 37db4f55..3e5e62a1 100644 --- a/test/projectgraph/cases/go/source-local-func/expect.yaml +++ b/test/projectgraph/cases/go/source-local-func/expect.yaml @@ -11,7 +11,12 @@ description: >- calls Name on a range variable of an interface. The field Name of user holds a string, so the call runs no function value, and os.Getwd is not found from show. closure calls its local f, which holds a function literal and nothing else. The index gives the calls of the literal to closure, so the call of f - is no record, and os.Getwd is not found from closure. + is no record, and os.Getwd is not found from closure. In other.go, serveLocal calls a local of the type + http.HandlerFunc, rangeAssign calls a local that a range assigns after the literal, anonSteps calls the + field act of an anonymous struct, and deref calls (*p). Each call runs a function value, so each is a + function-value record, and os.Getwd is uncertain from each. keepRead reads the field fn of a hook and + calls nothing. The read is a record with no receiver, so the link step does not take it for a call of a + field, and os.Getwd is not found from keepRead. roots: - key: main.go kind: main @@ -19,6 +24,9 @@ roots: - key: main.go//main kind: main rule: go.main + - key: other.go + kind: main + rule: go.main no_edges: - from: main.go//run to: main.go//remove @@ -61,6 +69,15 @@ unresolved: - caller: os//exec//LookPath reason: no-dependency-source callback: true + - caller: other.go//anonSteps + reason: no-dependency-source + callback: true + - caller: other.go//keepRead + reason: no-dependency-source + callback: true + - caller: other.go//rangeAssign + reason: no-dependency-source + callback: true - caller: main.go//run site: main.go:23 reason: function-value @@ -96,6 +113,30 @@ unresolved: site: main.go:65 reason: unknown-receiver method: Name + - caller: other.go//serveLocal + site: other.go:7 + reason: function-value + - caller: other.go//rangeAssign + site: other.go:14 + reason: function-value + - caller: other.go//rangeAssign + site: other.go:14 + reason: unknown-receiver + method: f + - caller: other.go//anonSteps + site: other.go:19 + reason: unknown-receiver + method: act + - caller: other.go//anonSteps + site: other.go:19 + reason: function-value + - caller: other.go//deref + site: other.go:24 + reason: function-value + - caller: other.go//keepRead + site: other.go:29 + reason: unknown-receiver + method: fn results: - target: os//Remove status: found @@ -198,3 +239,55 @@ results: - caller: main.go//getwd site: main.go:18 reached: false + - target: os//Getwd + sources: [other.go//serveLocal] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false + uncertainties: + - kind: unresolved-call + site: other.go:7 + - target: os//Getwd + sources: [other.go//rangeAssign] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false + uncertainties: + - kind: unresolved-call + site: other.go:14 + - target: os//Getwd + sources: [other.go//anonSteps] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false + uncertainties: + - kind: unresolved-call + site: other.go:19 + - target: os//Getwd + sources: [other.go//deref] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false + uncertainties: + - kind: unresolved-call + site: other.go:24 + - target: os//Getwd + sources: [other.go//keepRead] + sources_only: true + status: not-found + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false diff --git a/test/projectgraph/cases/go/source-local-func/project/other.go b/test/projectgraph/cases/go/source-local-func/project/other.go new file mode 100644 index 00000000..d522c285 --- /dev/null +++ b/test/projectgraph/cases/go/source-local-func/project/other.go @@ -0,0 +1,32 @@ +package main + +import "net/http" + +func serveLocal(m map[string]http.HandlerFunc) { + var h http.HandlerFunc = m["a"] + h(nil, nil) +} + +func rangeAssign(fs []func()) { + f := func() {} + for _, f = range fs { + } + f() +} + +func anonSteps(steps []struct{ act func() }) { + for _, s := range steps { + s.act() + } +} + +func deref(p *func()) { + (*p)() +} + +func keepRead(hs []hook) { + for _, h := range hs { + g := h.fn + _ = g + } +} diff --git a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml index 33b900f2..800bf386 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml +++ b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml @@ -3,13 +3,19 @@ description: >- main declares x as a local, and an inner block declares x again as a remote. The call x.run() in the block runs remote.run, which calls os/exec.LookPath. The index records the typed locals of a function with no extent, and the link step reads the type of the first one. So the index checks that the binding - of x in scope at the call is the binding whose type the link step reads. Here it is not, so the call is - an unknown-receiver record with the method name run, which gets a name edge to remote.run. The method - value x.stop is a record of the same kind, with the name stop. A type of the first x gave not-found for - os/exec.LookPath. The call x.run() before the block keeps its member edge to local.run. pick binds r - once and types it by the assignment r = &remote{}, and named types its named result l by an assignment. - Each function binds the name once, so the assignment types that variable, and the call keeps its member - edge. A record there gave a name edge to the run of the other type, and a target behind it read found. + of x in scope at the call is the binding whose type the link step reads. Here it is not. The method + value x.stop is an unknown-receiver record with the method name stop, which gets a name edge to + remote.stop. A type of the first x gave not-found for os/exec.LookPath. The call x.run() before the + block keeps its member edge to local.run. pick binds r once and types it by the assignment r = + &remote{}, and named types its named result l by an assignment. Each function binds the name once, so + the assignment types that variable, and the call keeps its member edge. A record there gave a name edge + to the run of the other type, and a target behind it read found. The binding of x in the block of main + has a type of its own, so the index gives the call the target of that binding under an alias, and the + call gets a member edge to remote.run. siblings does the same in each of two blocks. viaLit calls run on + the parameter x of a function literal, which no type of the index names, so the call is a record. + pickIface types r by an assignment, but r holds the interface runner from the field r, so the writes do + not agree and the call is a record too. A type of local for each of the two gave not-found for + os/exec.LookPath. roots: - key: main.go kind: main @@ -24,10 +30,25 @@ edges: confidence: high site: main.go:20 - from: main.go//main + to: main.go//remote//run + kind: member + confidence: high + site: main.go:23 + - from: main.go//siblings + to: main.go//remote//run + kind: member + confidence: high + site: main.go:72 + - from: main.go//local//viaLit to: main.go//remote//run kind: name confidence: low - site: main.go:23 + site: main.go:49 + - from: main.go//pickIface + to: main.go//remote//run + kind: name + confidence: low + site: main.go:62 - from: main.go//main to: main.go//remote//stop kind: name @@ -51,9 +72,15 @@ no_edges: - from: main.go//named to: main.go//remote//run unresolved: + - caller: main.go//local//viaLit + reason: no-dependency-source + callback: true - caller: main.go//main reason: no-dependency-source callback: true + - caller: main.go//pickIface + reason: no-dependency-source + callback: true - caller: os//Getwd reason: no-dependency-source callback: true @@ -63,10 +90,6 @@ unresolved: - caller: os//exec//LookPath reason: no-dependency-source callback: true - - caller: main.go//main - site: main.go:23 - reason: unknown-receiver - method: run - caller: main.go//main site: main.go:24 reason: unknown-receiver @@ -78,6 +101,14 @@ unresolved: site: main.go:25 reason: unknown-receiver method: f + - caller: main.go//local//viaLit + site: main.go:49 + reason: unknown-receiver + method: run + - caller: main.go//pickIface + site: main.go:62 + reason: unknown-receiver + method: run results: - target: os//Getwd status: found @@ -101,8 +132,8 @@ results: - key: main.go//main - key: main.go//remote//run site: main.go:23 - kind: name - confidence: low + kind: member + confidence: high - key: os//exec//LookPath site: main.go:14 kind: direct @@ -143,3 +174,39 @@ results: - caller: main.go//remote//run site: main.go:14 reached: false + - target: os//exec//LookPath + sources: [main.go//local//viaLit] + sources_only: true + status: found + path: + - key: main.go//local//viaLit + - key: main.go//remote//run + site: main.go:49 + kind: name + confidence: low + - key: os//exec//LookPath + site: main.go:14 + kind: direct + confidence: high + call_sites: + - caller: main.go//remote//run + site: main.go:14 + reached: true + - target: os//exec//LookPath + sources: [main.go//pickIface] + sources_only: true + status: found + path: + - key: main.go//pickIface + - key: main.go//remote//run + site: main.go:62 + kind: name + confidence: low + - key: os//exec//LookPath + site: main.go:14 + kind: direct + confidence: high + call_sites: + - caller: main.go//remote//run + site: main.go:14 + reached: true diff --git a/test/projectgraph/cases/go/typed-local-shadow/project/main.go b/test/projectgraph/cases/go/typed-local-shadow/project/main.go index 807fb50c..4456e216 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/project/main.go +++ b/test/projectgraph/cases/go/typed-local-shadow/project/main.go @@ -43,3 +43,32 @@ func named() (l *local) { l.run() return } + +func (x local) viaLit(r remote) { + go func(x remote) { + x.run() + }(r) +} + +type runner interface{ run() } + +type optsIface struct{ r runner } + +func pickIface(o optsIface) { + r := o.r + if r == nil { + r = &local{} + } + r.run() +} + +func siblings() { + { + x := local{} + x.run() + } + { + x := remote{} + x.run() + } +} From b437769787d62e865f9fcf0c53765dffba108a16 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:18:14 +0000 Subject: [PATCH 34/86] fix(rootmatch): skip test files in path rules and scope markup by language A path rule marked a component under a test path as a root of the program, and the glob *.vue also matched a directory name, as weird.vue. A path rule now skips a test file, and the component rule matches the name of a file. The markup record of a component concerned every target, so one Vue file in a Python project made each Python target uncertain. The markup runs only JavaScript code, so the reach query now scopes the record by its language: it concerns a target that code of the family defines or calls, and a target with no node. The Astro rule also marks the actions, the content config and astro.config. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 29 ++++--- fileindex/roots/rules.yaml | 4 +- projectgraph/reach/reach.go | 8 +- projectgraph/reach/uncertainty.go | 57 +++++++++++++- projectgraph/rootmatch/framework.go | 4 +- .../cases/javascript/astro-pages/expect.yaml | 77 ++++++++++++++++++- .../astro-pages/project/astro.config.mjs | 3 + .../astro-pages/project/src/actions/index.ts | 3 + .../astro-pages/project/src/content/config.ts | 3 + .../component-test-path/expect.yaml | 54 +++++++++++++ .../component-test-path/project/package.json | 5 ++ .../component-test-path/project/server.js | 3 + .../project/tests/fixtures/T.vue | 9 +++ .../project/weird.vue/helper.js | 3 + .../python/component-in-frontend/expect.yaml | 46 +++++++++++ .../component-in-frontend/project/Procfile | 1 + .../project/frontend/package.json | 1 + .../project/frontend/src/App.vue | 9 +++ .../component-in-frontend/project/main.py | 9 +++ 19 files changed, 309 insertions(+), 19 deletions(-) create mode 100644 test/projectgraph/cases/javascript/astro-pages/project/astro.config.mjs create mode 100644 test/projectgraph/cases/javascript/astro-pages/project/src/actions/index.ts create mode 100644 test/projectgraph/cases/javascript/astro-pages/project/src/content/config.ts create mode 100644 test/projectgraph/cases/javascript/component-test-path/expect.yaml create mode 100644 test/projectgraph/cases/javascript/component-test-path/project/package.json create mode 100644 test/projectgraph/cases/javascript/component-test-path/project/server.js create mode 100644 test/projectgraph/cases/javascript/component-test-path/project/tests/fixtures/T.vue create mode 100644 test/projectgraph/cases/javascript/component-test-path/project/weird.vue/helper.js create mode 100644 test/projectgraph/cases/python/component-in-frontend/expect.yaml create mode 100644 test/projectgraph/cases/python/component-in-frontend/project/Procfile create mode 100644 test/projectgraph/cases/python/component-in-frontend/project/frontend/package.json create mode 100644 test/projectgraph/cases/python/component-in-frontend/project/frontend/src/App.vue create mode 100644 test/projectgraph/cases/python/component-in-frontend/project/main.py diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 771cd3d9..455b3a7a 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -103,12 +103,17 @@ the project calls them, also in a file with no template. So the index gives each component file one record with the reason `markup`, at the first byte outside the code that is no space. The record -has no method name, so it concerns every target of a search that reaches -the file. A narrower scope, as the targets that the names of the script -reach, would miss a name that the app registers elsewhere, and a target -that only such a name reaches would read `not-found`. A project of -component files thus reads `uncertain` for each target that it does not -find, as it did when the index did not read component files. +has no method name. A narrower scope, as the targets that the names of the +script reach, would miss a name that the app registers elsewhere, and a +target that only such a name reaches would read `not-found`. The markup +runs only code of the JavaScript family, so the reach query scopes the +record by its language with `Uncertainty.Language`. It concerns a target +that a definition of the family is, or that an edge from a file of the +family leads to, and a target with no node. A JavaScript project of +component files thus reads `uncertain` for each JavaScript target that it +does not find. A target that only the Python code of a project calls does +not, as in the case `python/component-in-frontend`. A span and a summary +still count the record for every target. A JSX element renders the component that its name holds, so the name is a reference, as a function value is. `` gives a `reference` edge @@ -727,14 +732,18 @@ A framework loads a component file of Vue, Svelte or Astro in ways that no static import shows: a route table with `() => import('./views/X.vue')`, an alias of the bundler as `@/`, `import.meta.glob`, or a file-based router, as the `pages` of Nuxt. So the rule `node.component` marks each -component file as a root, with no `requires`. A component file holds a -`markup` record, so this root makes each target that the search does not -find read `uncertain` in a project with a component file. A static import +component file as a root, with no `requires`. A path rule skips a test +file, which runs only under a test, and the rule matches the name of a +file, not of a directory, as the case `javascript/component-test-path` +shows. A component file holds a `markup` record, so this root makes each +JavaScript target that the search does not find read `uncertain`. A static import of a component file already did that in most such projects. The rule keeps the fallback on, because a Vite app starts at `index.html`, which no rule reads. The rules `node.sveltekit.route` and `node.astro.page` mark the route files of SvelteKit and the pages of Astro, as `+page.ts`, -`+server.ts` and `src/pages/api/data.ts`, with their exports. They keep +`+server.ts` and `src/pages/api/data.ts`, with their exports. The Astro +rule also marks the middleware, the actions, the content config and +`astro.config.*`. They keep the fallback on too, because a framework can load a file by a convention that a rule does not list. Nuxt gets no rule of its own. It imports the code of `composables` and `utils` with no import statement, so no list of diff --git a/fileindex/roots/rules.yaml b/fileindex/roots/rules.yaml index c8d44e21..8d89db4b 100644 --- a/fileindex/roots/rules.yaml +++ b/fileindex/roots/rules.yaml @@ -98,7 +98,7 @@ rules: language: javascript kind: framework keeps_fallback: true - match: { path: ["*.vue", "*.svelte", "*.astro"] } + match: { path: ["**/*.vue", "**/*.svelte", "**/*.astro"] } - id: node.sveltekit.route language: javascript kind: framework @@ -110,7 +110,7 @@ rules: kind: framework requires: [astro] keeps_fallback: true - match: { path: ["**/src/pages/**", "**/src/middleware.*", "**/src/middleware/index.*"] } + match: { path: ["**/src/pages/**", "**/src/middleware.*", "**/src/middleware/index.*", "**/src/actions/index.*", "**/src/content/config.*", "**/src/content.config.*", "**/astro.config.*"] } - id: node.serverless.handler language: javascript kind: framework diff --git a/projectgraph/reach/reach.go b/projectgraph/reach/reach.go index d53f0c55..fff1a65f 100644 --- a/projectgraph/reach/reach.go +++ b/projectgraph/reach/reach.go @@ -14,6 +14,7 @@ import ( "strings" "github.com/safedep/code/fileindex" + "github.com/safedep/code/langcode" "github.com/safedep/code/projectgraph" ) @@ -107,8 +108,10 @@ type CallSite struct { // method that the graph names for it reaches. Such an uncertainty has no // key and counts once per search. The code of a Method, a Unit or a // Callback scope can hold uncertainties of its own, and the scope also -// concerns what each of them concerns. With none of the fields, the -// uncertainty concerns every target. +// concerns what each of them concerns. Language scopes the markup of a +// component file to the targets that code of the language family calls, +// because the markup runs only code of that family. With none of the +// fields, the uncertainty concerns every target. type Uncertainty struct { Kind UncertaintyKind `json:"kind"` Key string `json:"key,omitempty"` @@ -118,6 +121,7 @@ type Uncertainty struct { ImportPrefix string `json:"import_prefix,omitempty"` Unit string `json:"unit,omitempty"` Callback bool `json:"callback,omitempty"` + Language langcode.Code `json:"language,omitempty"` } // Searched is what the search did. Version is the Version of the search diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index 41f1281b..ea1c9c3c 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -7,6 +7,7 @@ import ( "slices" "github.com/safedep/code/fileindex" + "github.com/safedep/code/langcode" "github.com/safedep/code/projectgraph" ) @@ -27,6 +28,11 @@ type found struct { methods []string prefixed []prefixed byUnit []Uncertainty + // byLanguage holds the markup records, which concern the targets that + // code of their language family calls. + byLanguage []Uncertainty + // speaks reports whether code of a language family calls a target. + speaks func(lang langcode.Code, target Target) bool callback *Uncertainty // concerns reports whether the span of a scope concerns a target. concerns func(k scopeKey, target Target) bool @@ -39,6 +45,7 @@ type found struct { // sources holds the records, the import prefixes and the truncations of a // graph by node, so a search and a set computation read them once. type sources struct { + g *projectgraph.Graph unresolved map[string][]projectgraph.UnresolvedSite prefixes map[projectgraph.Site]string truncated map[string][]projectgraph.Truncation @@ -67,7 +74,7 @@ type sources struct { } func newSources(g *projectgraph.Graph, valueEdges bool) *sources { - s := &sources{valueEdges: valueEdges, unresolved: map[string][]projectgraph.UnresolvedSite{}, prefixes: map[projectgraph.Site]string{}, truncated: map[string][]projectgraph.Truncation{}, budgets: map[string][]Uncertainty{}, testBudgets: map[string][]Uncertainty{}, callbacks: map[string]bool{}} + s := &sources{g: g, valueEdges: valueEdges, unresolved: map[string][]projectgraph.UnresolvedSite{}, prefixes: map[projectgraph.Site]string{}, truncated: map[string][]projectgraph.Truncation{}, budgets: map[string][]Uncertainty{}, testBudgets: map[string][]Uncertainty{}, callbacks: map[string]bool{}} for _, u := range g.Unresolved { if u.Callback { s.callbacks[u.Caller] = true @@ -172,6 +179,8 @@ func (s *sources) at(key string, n *projectgraph.Node) []Uncertainty { } u := Uncertainty{Kind: UnresolvedCall, Key: key, Site: r.Site, Text: fmt.Sprintf("%s (%s)", r.Text, r.Reason)} switch { + case r.Reason == fileindex.UnresolvedMarkup: + u.Language = s.language(r.Site.Path) case r.Unit != "": u.Unit = r.Unit case r.ImportPrefix != "": @@ -207,7 +216,7 @@ func (s *sources) unitsOf(n *projectgraph.Node, key string) []string { func (a *adjacency) uncertainties(s *search, maxDepth int) *found { g := a.g src := a.sources() - f := &found{byMethod: map[string][]Uncertainty{}, concerns: a.concerns, units: src.budgets, entered: map[string]bool{}} + f := &found{byMethod: map[string][]Uncertainty{}, concerns: a.concerns, speaks: a.speaks, units: src.budgets, entered: map[string]bool{}} for _, u := range src.global { f.add(u) } @@ -254,6 +263,8 @@ func (f *found) add(u Uncertainty) { if f.callback == nil { f.callback = &u } + case u.Language != "": + f.byLanguage = append(f.byLanguage, u) case u.Unit != "": f.byUnit = append(f.byUnit, u) case u.ImportPrefix != "": @@ -289,6 +300,7 @@ func (f *found) all() []Uncertainty { out = append(out, p.u) } out = append(out, f.byUnit...) + out = append(out, f.byLanguage...) if f.callback != nil { out = append(out, *f.callback) } @@ -310,6 +322,11 @@ func (f *found) forTarget(target Target) ([]Uncertainty, []Uncertainty, int) { all = append(all, u) } } + for _, u := range f.byLanguage { + if f.speaks(u.Language, target) { + all = append(all, u) + } + } if f.callback != nil && f.concerns(scopeKey{callback: true}, target) { all = append(all, *f.callback) } @@ -335,3 +352,39 @@ func (f *found) forTarget(target Target) ([]Uncertainty, []Uncertainty, int) { } return capped, setAside, len(all) } + +// language returns the language family of the file at the path, or "" for +// a file with no node. +func (s *sources) language(path string) langcode.Code { + if n := s.g.Nodes[path]; n != nil && n.Language != "" { + return n.Language.Family() + } + return "" +} + +// speaks reports whether code of a language family calls a target: a node +// of the target is a definition in a file of the family, or an edge from +// such a file leads to it. A target with no node keeps every uncertainty, +// because no edge tells who calls it. +func (a *adjacency) speaks(lang langcode.Code, target Target) bool { + ids := a.match(target) + if len(ids) == 0 { + return true + } + src := a.sources() + of := func(key string) bool { + n := a.g.Nodes[key] + return n != nil && n.Path != "" && src.language(n.Path) == lang + } + for _, id := range ids { + if of(a.keys[id]) { + return true + } + for _, e := range a.in[id] { + if of(a.g.Edges[e].From) { + return true + } + } + } + return false +} diff --git a/projectgraph/rootmatch/framework.go b/projectgraph/rootmatch/framework.go index f5db183c..e2a86658 100644 --- a/projectgraph/rootmatch/framework.go +++ b/projectgraph/rootmatch/framework.go @@ -15,7 +15,9 @@ import ( // script, and only its top level runs. func (f *finder) matchPath(r rules.Rule) { for _, ix := range f.applying(r) { - if !pathglob.MatchAny(r.Match.Path, ix.Path) { + // A test file runs only under a test, so a path convention of the + // program does not make it a root of the program. + if !pathglob.MatchAny(r.Match.Path, ix.Path) || f.IsTest(ix.Path) { continue } f.add(r, ix.Path) diff --git a/test/projectgraph/cases/javascript/astro-pages/expect.yaml b/test/projectgraph/cases/javascript/astro-pages/expect.yaml index ae9554e3..a4be8854 100644 --- a/test/projectgraph/cases/javascript/astro-pages/expect.yaml +++ b/test/projectgraph/cases/javascript/astro-pages/expect.yaml @@ -1,12 +1,31 @@ source: safedep:hand description: >- Astro loads each file of src/pages by its path, and no file imports it. Each component file is a root - of the rule node.component, and the rule node.astro.page marks each file of src/pages and its exports. - So danger of index.astro and store of the endpoint GET of api/data.ts are found. + of the rule node.component. The rule node.astro.page marks each file of src/pages with its exports, the + actions, the content config and astro.config.mjs. So danger of index.astro, store of the endpoint GET of + api/data.ts, act of an action, collect of the content config and integrate of the config are found. roots: + - key: astro.config.mjs + kind: framework + rule: node.astro.page - key: server.js kind: manifest rule: node.package-json + - key: src/actions/index.ts + kind: framework + rule: node.astro.page + - key: src/actions/index.ts//server + kind: framework + rule: node.astro.page + - key: src/actions/index.ts//server//go//handler + kind: framework + rule: node.astro.page + - key: src/content/config.ts + kind: framework + rule: node.astro.page + - key: src/content/config.ts//collections + kind: framework + rule: node.astro.page - key: src/pages/api/data.ts kind: framework rule: node.astro.page @@ -21,11 +40,26 @@ roots: rule: node.component exact_edges: true edges: + - from: astro.config.mjs + to: plug//integrate + kind: direct + confidence: high + site: astro.config.mjs:3 - from: server.js to: plug//listen kind: direct confidence: high site: server.js:3 + - from: src/actions/index.ts//server//go//handler + to: plug//act + kind: direct + confidence: high + site: src/actions/index.ts:3 + - from: src/content/config.ts + to: plug//collect + kind: direct + confidence: high + site: src/content/config.ts:3 - from: src/pages/api/data.ts//GET to: plug//store kind: direct @@ -67,3 +101,42 @@ results: - caller: src/pages/api/data.ts//GET site: src/pages/api/data.ts:4 reached: true + - target: plug//integrate + status: found + path: + - key: astro.config.mjs + root: framework + - key: plug//integrate + site: astro.config.mjs:3 + kind: direct + confidence: high + call_sites: + - caller: astro.config.mjs + site: astro.config.mjs:3 + reached: true + - target: plug//collect + status: found + path: + - key: src/content/config.ts + root: framework + - key: plug//collect + site: src/content/config.ts:3 + kind: direct + confidence: high + call_sites: + - caller: src/content/config.ts + site: src/content/config.ts:3 + reached: true + - target: plug//act + status: found + path: + - key: src/actions/index.ts//server//go//handler + root: framework + - key: plug//act + site: src/actions/index.ts:3 + kind: direct + confidence: high + call_sites: + - caller: src/actions/index.ts//server//go//handler + site: src/actions/index.ts:3 + reached: true diff --git a/test/projectgraph/cases/javascript/astro-pages/project/astro.config.mjs b/test/projectgraph/cases/javascript/astro-pages/project/astro.config.mjs new file mode 100644 index 00000000..ab5551ed --- /dev/null +++ b/test/projectgraph/cases/javascript/astro-pages/project/astro.config.mjs @@ -0,0 +1,3 @@ +import { integrate } from 'plug'; + +export default { integrations: [integrate()] }; diff --git a/test/projectgraph/cases/javascript/astro-pages/project/src/actions/index.ts b/test/projectgraph/cases/javascript/astro-pages/project/src/actions/index.ts new file mode 100644 index 00000000..5dfe8073 --- /dev/null +++ b/test/projectgraph/cases/javascript/astro-pages/project/src/actions/index.ts @@ -0,0 +1,3 @@ +import { act } from 'plug'; + +export const server = { go: { handler: async () => act() } }; diff --git a/test/projectgraph/cases/javascript/astro-pages/project/src/content/config.ts b/test/projectgraph/cases/javascript/astro-pages/project/src/content/config.ts new file mode 100644 index 00000000..2ff173a2 --- /dev/null +++ b/test/projectgraph/cases/javascript/astro-pages/project/src/content/config.ts @@ -0,0 +1,3 @@ +import { collect } from 'plug'; + +export const collections = { blog: collect() }; diff --git a/test/projectgraph/cases/javascript/component-test-path/expect.yaml b/test/projectgraph/cases/javascript/component-test-path/expect.yaml new file mode 100644 index 00000000..cf1bd8dc --- /dev/null +++ b/test/projectgraph/cases/javascript/component-test-path/expect.yaml @@ -0,0 +1,54 @@ +source: safedep:hand +description: >- + tests/fixtures/T.vue is a component under a test path, so it runs only under a test. The rule + node.component skips a test file, so fixture is not-found in the program and found only on a test path. + The directory weird.vue holds a plain module that no file imports. The rule matches the name of a file + and not of a directory, so helper is not-found. +test_paths: [tests] +roots: + - key: server.js + kind: manifest + rule: node.package-json + - key: tests/fixtures/T.vue + kind: test + rule: test.files +exact_edges: true +edges: + - from: server.js + to: plug//listen + kind: direct + confidence: high + site: server.js:3 + - from: tests/fixtures/T.vue + to: plug//fixture + kind: direct + confidence: high + site: tests/fixtures/T.vue:4 + - from: weird.vue/helper.js + to: plug//helper + kind: direct + confidence: high + site: weird.vue/helper.js:3 +unresolved: + - caller: tests/fixtures/T.vue + site: tests/fixtures/T.vue:1 + reason: markup +results: + - target: plug//fixture + status: not-found + test_path: + - key: tests/fixtures/T.vue + - key: plug//fixture + site: tests/fixtures/T.vue:4 + kind: direct + confidence: high + call_sites: + - caller: tests/fixtures/T.vue + site: tests/fixtures/T.vue:4 + reached: false + - target: plug//helper + status: not-found + call_sites: + - caller: weird.vue/helper.js + site: weird.vue/helper.js:3 + reached: false diff --git a/test/projectgraph/cases/javascript/component-test-path/project/package.json b/test/projectgraph/cases/javascript/component-test-path/project/package.json new file mode 100644 index 00000000..bc2c5d36 --- /dev/null +++ b/test/projectgraph/cases/javascript/component-test-path/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "component-test-path", + "main": "server.js", + "dependencies": { "vue": "^3.4.0" } +} diff --git a/test/projectgraph/cases/javascript/component-test-path/project/server.js b/test/projectgraph/cases/javascript/component-test-path/project/server.js new file mode 100644 index 00000000..fc1cd6f8 --- /dev/null +++ b/test/projectgraph/cases/javascript/component-test-path/project/server.js @@ -0,0 +1,3 @@ +import { listen } from 'plug'; + +listen(3000); diff --git a/test/projectgraph/cases/javascript/component-test-path/project/tests/fixtures/T.vue b/test/projectgraph/cases/javascript/component-test-path/project/tests/fixtures/T.vue new file mode 100644 index 00000000..7edffc79 --- /dev/null +++ b/test/projectgraph/cases/javascript/component-test-path/project/tests/fixtures/T.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/javascript/component-test-path/project/weird.vue/helper.js b/test/projectgraph/cases/javascript/component-test-path/project/weird.vue/helper.js new file mode 100644 index 00000000..14496ca9 --- /dev/null +++ b/test/projectgraph/cases/javascript/component-test-path/project/weird.vue/helper.js @@ -0,0 +1,3 @@ +import { helper } from 'plug'; + +helper(); diff --git a/test/projectgraph/cases/python/component-in-frontend/expect.yaml b/test/projectgraph/cases/python/component-in-frontend/expect.yaml new file mode 100644 index 00000000..2bad80c6 --- /dev/null +++ b/test/projectgraph/cases/python/component-in-frontend/expect.yaml @@ -0,0 +1,46 @@ +source: safedep:hand +description: >- + A Python service ships a Vue frontend. App.vue is a root of the rule node.component, and its markup + record runs only JavaScript code. So the record concerns the targets that code of the JavaScript family + calls, and os.remove, which only dead Python code calls, stays not-found. plug.other has no node, so no + edge tells who calls it, and the record keeps it uncertain. +roots: + - key: frontend/src/App.vue + kind: framework + rule: node.component + - key: main.py + kind: manifest + rule: procfile +exact_edges: true +edges: + - from: frontend/src/App.vue + to: plug//render + kind: direct + confidence: high + site: frontend/src/App.vue:4 + - from: main.py + to: shutil//rmtree + kind: direct + confidence: high + site: main.py:9 + - from: main.py//unused + to: os//remove + kind: direct + confidence: high + site: main.py:6 +unresolved: + - caller: frontend/src/App.vue + site: frontend/src/App.vue:1 + reason: markup +results: + - target: os//remove + status: not-found + call_sites: + - caller: main.py//unused + site: main.py:6 + reached: false + - target: plug//other + status: uncertain + uncertainties: + - kind: unresolved-call + site: frontend/src/App.vue:1 diff --git a/test/projectgraph/cases/python/component-in-frontend/project/Procfile b/test/projectgraph/cases/python/component-in-frontend/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/component-in-frontend/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/component-in-frontend/project/frontend/package.json b/test/projectgraph/cases/python/component-in-frontend/project/frontend/package.json new file mode 100644 index 00000000..be960f5a --- /dev/null +++ b/test/projectgraph/cases/python/component-in-frontend/project/frontend/package.json @@ -0,0 +1 @@ +{ "name": "frontend", "dependencies": { "vue": "^3.4.0" } } diff --git a/test/projectgraph/cases/python/component-in-frontend/project/frontend/src/App.vue b/test/projectgraph/cases/python/component-in-frontend/project/frontend/src/App.vue new file mode 100644 index 00000000..ebaae70d --- /dev/null +++ b/test/projectgraph/cases/python/component-in-frontend/project/frontend/src/App.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/python/component-in-frontend/project/main.py b/test/projectgraph/cases/python/component-in-frontend/project/main.py new file mode 100644 index 00000000..37b51646 --- /dev/null +++ b/test/projectgraph/cases/python/component-in-frontend/project/main.py @@ -0,0 +1,9 @@ +import os +import shutil + + +def unused(): + os.remove("x") + + +shutil.rmtree("y") From 035d6e51303689b56ae840e671c4cdb6678df097 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:23:42 +0000 Subject: [PATCH 35/86] fix(projectgraph): link Python instance methods from each site that can make an instance Only a call of a class linked its __call__, __getattr__, __getattribute__ and __get__. Code that gets the class as a value can make an instance too, as functools.partial(C), map(C, xs) or field(default_factory=C), and so can cls() in a classmethod, so their targets read as not-found. A reference to a class now links these methods, unless the index marks it with Check: a class that code only tests a value against, as in isinstance(x, C), or the metaclass of a class statement. A call of cls in a classmethod links the methods of the class and of its subclasses. The index records the metaclass of a class, and a call of the class links __call__ of its metaclass, so a reference to a metaclass alone no longer does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 32 ++-- fileindex/index.go | 19 +- plugin/indexer/languages.go | 5 +- plugin/indexer/project.go | 2 +- plugin/indexer/python.go | 47 +++++ plugin/indexer/references.go | 9 +- plugin/indexer/types.go | 9 + projectgraph/definitions.go | 117 +++++++---- projectgraph/link.go | 6 +- projectgraph/profile.go | 10 +- projectgraph/python.go | 3 +- projectgraph/reach/uncertainty.go | 2 +- projectgraph/resolve.go | 3 + .../instance-made-by-reference/expect.yaml | 181 ++++++++++++++++++ .../project/main.py | 50 +++++ .../instance-methods-no-instance/expect.yaml | 23 ++- 16 files changed, 436 insertions(+), 82 deletions(-) create mode 100644 test/projectgraph/cases/python/instance-made-by-reference/expect.yaml create mode 100644 test/projectgraph/cases/python/instance-made-by-reference/project/main.py diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 455b3a7a..37a5c02c 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -293,20 +293,24 @@ call of the instance runs `__call__`. A read of an attribute runs define, and `__get__` of a descriptor in a class attribute. The code that calls or reads the instance often has an unknown receiver, or is code outside the graph. So a `reference` edge with the rule `implicit` joins -each call that makes an instance of a Python class to the method of each -of these names that the instance runs, from the class or from a base in -its MRO. The call can reach the class through an export hop or the -`package` edge of a unit. The code that makes the instance holds it, so a -search from the roots reaches the method, as it reaches a stored function -value. A reference to the class alone, as in `isinstance(x, Proxy)`, or a -call of a static method, makes no instance and gets no edge. A class -statement with `metaclass=Meta` makes an instance of `Meta`, so a -reference to a class with the written base `type` gets the edges too. A -call with an unknown receiver keeps its record with the method name. The -target is then found. The edge proves only that an instance exists, and -not that code calls it. The cases `python/unknown-receiver-callable`, -`python/unknown-receiver-getattr` and -`python/instance-methods-no-instance` +each edge that can make an instance of a Python class to the method of +each of these names that the instance runs, from the class or from a base +in its MRO. A call of the class makes an instance. So does code that gets +the class as a value, as `functools.partial(C)`, `map(C, xs)` or +`field(default_factory=C)`, and a call of `cls` in a classmethod, which +holds the class of the method or a subclass. The edge can reach the class +through an export hop or the `package` edge of a unit. The code that +makes the instance holds it, so a search from the roots reaches the +method, as it reaches a stored function value. A class that code only +tests a value against, as in `isinstance(x, Proxy)`, makes no instance. +The index marks such a reference with `Check`, and it gets no edge. A +call of a class also runs `__call__` of its metaclass, as `Meta` in +`class Made(metaclass=Meta)`, which `Class.Metaclass` records. A call with +an unknown receiver keeps its record with the method name. The target is +then found. The edge proves only that an instance can exist, and not that +code calls it. The cases `python/unknown-receiver-callable`, +`python/unknown-receiver-getattr`, `python/instance-methods-no-instance` +and `python/instance-made-by-reference` show it. A function that code binds to a module attribute, as `hooks.handler = wipe`, has a `reference` edge from that code, and the assignment has the flag `monkey-patch`, so it needs no rule of its own. diff --git a/fileindex/index.go b/fileindex/index.go index ca8e4388..e4b1493a 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -294,13 +294,15 @@ func (d Definition) HoldsData() bool { // keys. Fields are the fields that declare the type of a class, in source // order. Abstract are the methods that a Go interface declares, in source // order. They have no definition, and a type implements the interface when -// its methods hold them. +// its methods hold them. Metaclass is the metaclass of a Python class as +// written, as M in class A(metaclass=M). type Class struct { - Key string `json:"key"` - Bases []string `json:"bases,omitempty"` - Methods []string `json:"methods,omitempty"` - Fields []Typed `json:"fields,omitempty"` - Abstract []Signature `json:"abstract,omitempty"` + Key string `json:"key"` + Bases []string `json:"bases,omitempty"` + Metaclass string `json:"metaclass,omitempty"` + Methods []string `json:"methods,omitempty"` + Fields []Typed `json:"fields,omitempty"` + Abstract []Signature `json:"abstract,omitempty"` } // Signature is a method that an interface declares: its name, the count of @@ -357,12 +359,16 @@ type Arg struct { // uses as a value and does not call, as yaml.load in run(yaml.load). The // code that gets the value can call it, so the link step counts the target // as possibly called. Targets are per-file resolutions, as in a Call. +// Check marks a class that the code only tests a value against, as the +// second argument of isinstance in Python, or names as the metaclass of a +// class statement. Such a use makes no instance that code can call. type Reference struct { Caller string `json:"caller"` Line int `json:"line"` Column int `json:"column"` Text string `json:"text"` Targets []string `json:"targets"` + Check bool `json:"check,omitempty"` } // Unresolved is a call site whose callee the index does not know in full. @@ -688,6 +694,7 @@ func (ix *Index) EachString(fn func(string) string) { c := &ix.Classes[i] kind(&c.Key) strs(c.Bases) + kind(&c.Metaclass) strs(c.Methods) typed(c.Fields) for j := range c.Abstract { diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 0c9f3e97..220da094 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -64,7 +64,10 @@ type languageRules struct { kindOf func(p *projection, node *sitter.Node, kind fileindex.DefKind) fileindex.DefKind // bases returns the classes and the interfaces that a class or an // interface names as its bases, as written. - bases func(p *projection, class *sitter.Node) []string + // metaclass returns the metaclass of a class as written, as M in + // class A(metaclass=M). + metaclass func(p *projection, class *sitter.Node) string + bases func(p *projection, class *sitter.Node) []string // fields returns the fields of a class that declare a type. fields func(p *projection, class *sitter.Node) []fileindex.Typed // abstract returns the methods that an interface declares. diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index c3b550a0..ea44f562 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -379,7 +379,7 @@ func (p *projection) addClasses(_ context.Context) error { case node == nil: p.ix.Classes = append(p.ix.Classes, fileindex.Class{Key: key, Methods: methods[key]}) default: - p.ix.Classes = append(p.ix.Classes, fileindex.Class{Key: key, Bases: p.bases(node), Methods: methods[key], Fields: p.fields(node)}) + p.ix.Classes = append(p.ix.Classes, fileindex.Class{Key: key, Bases: p.bases(node), Metaclass: p.metaclass(node), Methods: methods[key], Fields: p.fields(node)}) } } return nil diff --git a/plugin/indexer/python.go b/plugin/indexer/python.go index 6ced7d44..f7d8b5be 100644 --- a/plugin/indexer/python.go +++ b/plugin/indexer/python.go @@ -20,6 +20,7 @@ var pythonRules = languageRules{ constructor: "__init__", typedReceivers: true, bases: pythonBases, + metaclass: pythonMetaclass, fields: pythonFields, decorators: decorators, signature: pythonSignature, @@ -65,6 +66,7 @@ var pythonRules = languageRules{ identifiers: map[string]bool{"identifier": true}, instances: map[string]bool{"self": true, "cls": true}, bases: map[string]string{"class_definition": "superclasses"}, + check: pythonCheck, }, arguments: &argumentSyntax{ calls: map[string]string{"call": "function"}, @@ -95,6 +97,51 @@ func pythonBases(p *projection, class *sitter.Node) []string { return bases } +// pythonChecks name the builtins that test a value against a class. +var pythonChecks = map[string]bool{"isinstance": true, "issubclass": true} + +// pythonCheck reports whether a holder holds classes that the code only +// tests a value against or names as a metaclass: the arguments of +// isinstance or issubclass, a tuple among them, or metaclass=M in a class +// statement. The class statement makes an instance of M, and a call of the +// class that it makes runs M.__call__, so the call of that class owns it. +func pythonCheck(p *projection, holder *sitter.Node) bool { + switch holder.Type() { + case "keyword_argument": + list := holder.Parent() + name := holder.ChildByFieldName("name") + return name != nil && p.text(name) == "metaclass" && list != nil && list.Parent() != nil && list.Parent().Type() == "class_definition" + case "tuple": + if holder = holder.Parent(); holder == nil || holder.Type() != "argument_list" { + return false + } + case "argument_list": + default: + return false + } + call := holder.Parent() + if call == nil || call.Type() != "call" { + return false + } + function := call.ChildByFieldName("function") + return function != nil && pythonChecks[p.text(function)] +} + +// pythonMetaclass returns the metaclass of a Python class as written, as M +// in class A(Base, metaclass=M), or "". +func pythonMetaclass(p *projection, class *sitter.Node) string { + list := class.ChildByFieldName("superclasses") + if class.Type() != "class_definition" || list == nil { + return "" + } + for _, arg := range ts.NamedChildren(list) { + if name := arg.ChildByFieldName("name"); arg.Type() == "keyword_argument" && name != nil && p.text(name) == "metaclass" { + return p.text(arg.ChildByFieldName("value")) + } + } + return "" +} + // pythonAssigned returns the name of a top-level assignment, as x in x = f(). func pythonAssigned(_ *projection, statement *sitter.Node) []*sitter.Node { if statement.Type() != "expression_statement" || statement.NamedChildCount() == 0 { diff --git a/plugin/indexer/references.go b/plugin/indexer/references.go index f64c4f86..599b880d 100644 --- a/plugin/indexer/references.go +++ b/plugin/indexer/references.go @@ -53,6 +53,9 @@ type referenceSyntax struct { // receiver, as Go. The link step then tells it from a call, which can // run a function value of a field. bareValues bool + // check reports whether the values of a holder are classes that the + // code only tests a value against, as the arguments of isinstance. + check func(p *projection, holder *sitter.Node) bool // value replaces reference for a language whose function values take // more forms than a name or a member chain, as Go. It gives the member // node of a method value whose receiver the index cannot name, and the @@ -110,6 +113,7 @@ func referenceVisitor(p *projection) visitor { values = []*sitter.Node{node.ChildByFieldName(field)} } caller := stack[len(stack)-1].key + check := syntax.check != nil && syntax.check(p, node) for _, value := range syntax.unlist(values) { if spread, ok := syntax.spreads[kind]; ok && value.Type() == spread { p.addSpread(*syntax, caller, value) @@ -129,7 +133,7 @@ func referenceVisitor(p *projection) visitor { switch { case seen[k]: case target != "": - p.addReference(value, caller, p.targets([]string{target})) + p.addReference(value, caller, p.targets([]string{target})).Check = check case !syntax.namedOnly[kind]: p.addValueRecord(member, caller) } @@ -143,7 +147,7 @@ func referenceVisitor(p *projection) visitor { // node, and a static initializer or a property function that the call // graph does not know takes it from there, as a call does. The text is // bounded as the text of a record is. -func (p *projection) addReference(node *sitter.Node, caller string, targets []string) { +func (p *projection) addReference(node *sitter.Node, caller string, targets []string) *fileindex.Reference { site := ast.GetNodePosition(node) p.ix.References = append(p.ix.References, fileindex.Reference{ Caller: p.caller(caller, site), @@ -152,6 +156,7 @@ func (p *projection) addReference(node *sitter.Node, caller string, targets []st Text: callgraph.ClipText(p.text(node)), Targets: targets, }) + return &p.ix.References[len(p.ix.References)-1] } // addSpread adds the functions of the object that a spread passes, as diff --git a/plugin/indexer/types.go b/plugin/indexer/types.go index c8e3be5f..78b94424 100644 --- a/plugin/indexer/types.go +++ b/plugin/indexer/types.go @@ -121,6 +121,15 @@ func (p *projection) bases(node *sitter.Node) []string { return nil } +// metaclass returns the metaclass of a class as written, by the rules of +// the language. +func (p *projection) metaclass(node *sitter.Node) string { + if rule := p.rules().metaclass; rule != nil { + return rule(p, node) + } + return "" +} + // abstract returns the methods that an interface declares, by the rules of // the language. func (p *projection) abstract(node *sitter.Node) []fileindex.Signature { diff --git a/projectgraph/definitions.go b/projectgraph/definitions.go index a2dd864b..30e76f61 100644 --- a/projectgraph/definitions.go +++ b/projectgraph/definitions.go @@ -128,11 +128,13 @@ func (b *builder) linkConstructors(ix *fileindex.Index) { } } -// linkInstances adds a reference edge from each edge that makes an +// linkInstances adds a reference edge from each edge that can make an // instance of a class to the method of each instance method name that the -// instance runs, from the class or from a base in its MRO. The code that -// makes the instance holds it, so a search reaches the method as it -// reaches a stored function value. A reference to the class alone, as in +// instance runs, from the class or from a base in its MRO. A call makes an +// instance, and so can the code that gets a reference to the class, as +// functools.partial(C) or map(C, xs). The code that makes the instance +// holds it, so a search reaches the method as it reaches a stored function +// value. A reference that only tests a value against the class, as in // isinstance(x, C), makes no instance and gets no edge. An edge reaches the // class through export hops and the package edge of a unit. func (b *builder) linkInstances() { @@ -143,25 +145,31 @@ func (b *builder) linkInstances() { hops[str[slot.from]] = append(hops[str[slot.from]], str[slot.to]) } } - memo := map[symbol]instanceCalls{} + memo := map[string][]string{} + methodsOf := func(key string) []string { + m, ok := memo[key] + if !ok { + m = b.instanceMethods(key, hops) + memo[key] = m + } + return m + } var out []Edge + add := func(from string, site Site, methods []string) { + for _, key := range methods { + out = append(out, newEdge(from, key, site, EdgeReference, resolverName(b.profileAt(key).code, ruleImplicit))) + } + } for slot, v := range b.edges.slots { kind := EdgeKind(str[v.kind]) - if kind == EdgeImport || kind == EdgeExport || kind == EdgePackage { + if kind == EdgeImport || kind == EdgeExport || kind == EdgePackage || b.checks[slot] { continue } - m, ok := memo[slot.to] - if !ok { - m = b.instanceMethods(str[slot.to], hops) - memo[slot.to] = m - } - methods := m.call - if kind == EdgeReference { - methods = m.ref - } - site := Site{Path: str[slot.path], Line: int(slot.line), Column: int(slot.column)} - for _, key := range methods { - out = append(out, newEdge(str[slot.from], key, site, EdgeReference, resolverName(b.profileAt(key).code, ruleImplicit))) + add(str[slot.from], Site{Path: str[slot.path], Line: int(slot.line), Column: int(slot.column)}, methodsOf(str[slot.to])) + } + for _, c := range b.classCalls() { + for _, class := range append([]string{c.class}, b.subclasses(c.class)...) { + add(c.from, c.site, methodsOf(class)) } } slices.SortFunc(out, func(x, y Edge) int { @@ -173,14 +181,11 @@ func (b *builder) linkInstances() { } } -// instanceCalls holds the instance methods that a call of a key runs, and -// the ones that a reference to the key runs. Only a metaclass gives the -// second kind, because a class statement that names it makes an instance. -type instanceCalls struct{ call, ref []string } - // instanceMethods returns the instance methods of each class that a key -// names through its hops. -func (b *builder) instanceMethods(key string, hops map[string][]string) (m instanceCalls) { +// names through its hops. A call of a class also runs __call__ of its +// metaclass, as M in class A(metaclass=M). +func (b *builder) instanceMethods(key string, hops map[string][]string) []string { + var out []string seen := map[string]bool{} next := []string{key} for depth := 0; len(next) > 0 && depth <= b.opts.MaxExportHops; depth++ { @@ -195,36 +200,66 @@ func (b *builder) instanceMethods(key string, hops map[string][]string) (m insta if b.kindOf(k) != NodeClass || len(p.instanceMethods) == 0 { continue } - meta := b.isMetaclass(k, p.metaclass) for _, name := range slices.Sorted(maps.Keys(p.instanceMethods)) { if method, ok := b.ownMethod(k, name); ok && b.isMethod(method) { - m.call = append(m.call, method) - if meta { - m.ref = append(m.ref, method) - } + out = append(out, method) + } + } + if meta, ok := b.metaclass(k); ok { + if method, ok := b.ownMethod(meta, p.metaclassCall); ok && b.isMethod(method) { + out = append(out, method) } } } next = later } - return m + return out } -// isMetaclass reports whether a class of its MRO names the metaclass base -// as written, as class Meta(type). The builtin base binds no name, so the -// written name tells it. -func (b *builder) isMetaclass(class, metaclass string) bool { - if metaclass == "" { - return false - } +// metaclass returns the class that a class of the MRO of a class names as +// its metaclass. +func (b *builder) metaclass(class string) (string, bool) { for _, c := range b.mro(class) { for _, rec := range b.records(c) { - if slices.Contains(rec.class.Bases, metaclass) { - return true + if rec.class.Metaclass == "" { + continue + } + key, ok := b.resolveNameIn(b.scopes[rec.ix.Path], fileindex.Parent(rec.class.Key), rec.class.Metaclass) + return key, ok && b.kindOf(key) == NodeClass + } + } + return "", false +} + +// classCall is a call of the class of a method from the method itself, as +// cls() in a classmethod of Python. +type classCall struct { + from, class string + site Site +} + +// classCalls returns each call of the first parameter of a classmethod, +// which holds the class of the method or a subclass of it. +func (b *builder) classCalls() []classCall { + var out []classCall + for _, p := range b.paths { + ix := b.byPath[p] + if ix == nil { + continue + } + param := profileOf(ix.Language).classParam + if param == "" { + continue + } + for _, call := range ix.Calls { + def := b.defs[call.Caller] + if call.Callee != param || def == nil || def.Parent == "" || !slices.Contains(def.Decorators, "@classmethod") { + continue } + out = append(out, classCall{from: b.runner(call.Caller), class: def.Parent, site: Site{Path: ix.Path, Line: call.Line, Column: call.Column}}) } } - return false + return out } // linkOverrides adds a reference edge from each class with a base of a diff --git a/projectgraph/link.go b/projectgraph/link.go index 31280fd3..6d09dd4b 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -126,7 +126,10 @@ type builder struct { // form. provisions map[provisionKey][]provision - edges *edgeSet + edges *edgeSet + // checks holds the reference edges that only test a value against a + // class, as isinstance(x, C). They make no instance. + checks map[edgeSlot]bool unresolved map[UnresolvedSite]bool truncations map[Truncation]bool problems []modules.Problem @@ -303,6 +306,7 @@ func newTables(indexes []*fileindex.Index, opts Options) *builder { byPath: make(map[string]*fileindex.Index, len(indexes)), scopes: make(map[string]*scope, len(indexes)), edges: newEdgeSet(sites), + checks: map[edgeSlot]bool{}, unresolved: map[UnresolvedSite]bool{}, valueSites: map[Site]bool{}, truncations: map[Truncation]bool{}, diff --git a/projectgraph/profile.go b/projectgraph/profile.go index 0e03d158..7c594f25 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -183,10 +183,12 @@ type profile struct { // of the name, as __call__ of Python. A call that makes an instance // links them, and a reference to the class alone does not. instanceMethods map[string]bool - // metaclass is the base of a class whose instances are classes, as - // type of Python. A class statement makes an instance of such a class, - // so each edge to it makes an instance. - metaclass string + // metaclassCall names the method of a metaclass that a call of a class + // runs, as __call__ of Python. + metaclassCall string + // classParam names the first parameter of a class method, which holds + // the class, as cls of Python. A call of it makes an instance. + classParam string // forwardMethod names the method of a class that forwards a call that // the class has no method for to another type, as deref of Rust. A // declared type with it does not narrow the receiver. diff --git a/projectgraph/python.go b/projectgraph/python.go index f94ea897..4e6c303f 100644 --- a/projectgraph/python.go +++ b/projectgraph/python.go @@ -33,7 +33,8 @@ var pythonProfile = profile{ moduleNames: true, starImports: true, instanceMethods: pythonInstanceMethods, - metaclass: "type", + metaclassCall: "__call__", + classParam: "cls", } // pythonInstanceMethods names the methods that Python runs on an instance diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index ea1c9c3c..fe085326 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -32,7 +32,7 @@ type found struct { // code of their language family calls. byLanguage []Uncertainty // speaks reports whether code of a language family calls a target. - speaks func(lang langcode.Code, target Target) bool + speaks func(lang langcode.Code, target Target) bool callback *Uncertainty // concerns reports whether the span of a scope concerns a target. concerns func(k scopeKey, target Target) bool diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 2504b77c..f104037f 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -372,6 +372,9 @@ func (b *builder) linkReferences(ix *fileindex.Index) { b.valueSites[site] = true } b.link(ix.Language, from, r.key, site, EdgeReference, ruleReference) + if ref.Check { + b.checks[b.edges.slot(Edge{From: from, To: r.key, Site: site})] = true + } } } } diff --git a/test/projectgraph/cases/python/instance-made-by-reference/expect.yaml b/test/projectgraph/cases/python/instance-made-by-reference/expect.yaml new file mode 100644 index 00000000..6127238d --- /dev/null +++ b/test/projectgraph/cases/python/instance-made-by-reference/expect.yaml @@ -0,0 +1,181 @@ +source: safedep:hand +description: >- + Each class makes an instance in a way other than a call of its name. cls() in the classmethod create + calls the class of the method. functools.partial and field(default_factory=...) get the class as a + value and call it. So each of these sites links the __call__ or __getattr__ of the class, and os.remove, + shutil.rmtree and subprocess.run are found. check names Meta only in isinstance, which makes no + instance, and no class names Meta as its metaclass, so tempfile.mkdtemp in Meta.__call__ is not-found. +roots: + - key: main.py + kind: main + rule: python.main-guard +exact_edges: true +edges: + - from: main.py + to: dataclasses//dataclass + kind: direct + confidence: high + site: main.py:29 + - from: main.py + to: dataclasses//field + kind: direct + confidence: high + site: main.py:30 + - from: main.py + to: main.py//ByDefault + kind: reference + confidence: low + site: main.py:30 + - from: main.py + to: main.py//ByDefault//__getattr__ + kind: reference + confidence: low + site: main.py:30 + - from: main.py + to: main.py//run + kind: direct + confidence: high + site: main.py:47 + - from: main.py + to: main.py//ByClassmethod//create + kind: member + confidence: high + site: main.py:47 + - from: main.py + to: main.py//run + kind: direct + confidence: high + site: main.py:48 + - from: main.py + to: functools//partial + kind: direct + confidence: high + site: main.py:48 + - from: main.py + to: main.py//ByPartial + kind: reference + confidence: low + site: main.py:48 + - from: main.py + to: main.py//ByPartial//__call__ + kind: reference + confidence: low + site: main.py:48 + - from: main.py + to: print + kind: direct + confidence: high + site: main.py:49 + - from: main.py + to: main.py//Holder + kind: direct + confidence: high + site: main.py:49 + - from: main.py + to: main.py//check + kind: direct + confidence: high + site: main.py:50 + - from: main.py//ByClassmethod//__call__ + to: os//remove + kind: direct + confidence: high + site: main.py:11 + - from: main.py//ByClassmethod//create + to: main.py//ByClassmethod//__call__ + kind: reference + confidence: low + site: main.py:15 + - from: main.py//ByDefault//__getattr__ + to: subprocess//run + kind: direct + confidence: high + site: main.py:25 + - from: main.py//ByPartial//__call__ + to: shutil//rmtree + kind: direct + confidence: high + site: main.py:20 + - from: main.py//Meta//__call__ + to: tempfile//mkdtemp + kind: direct + confidence: high + site: main.py:35 + - from: main.py//check + to: isinstance + kind: direct + confidence: high + site: main.py:43 + - from: main.py//check + to: main.py//Meta + kind: reference + confidence: low + site: main.py:43 +unresolved: + - caller: main.py//ByClassmethod//create + site: main.py:15 + reason: unknown-receiver + method: cls + - caller: main.py//run + site: main.py:39 + reason: unknown-receiver + method: f +results: + - target: os//remove + status: found + path: + - key: main.py + - key: main.py//ByClassmethod//create + site: main.py:47 + kind: member + confidence: high + - key: main.py//ByClassmethod//__call__ + site: main.py:15 + kind: reference + confidence: low + - key: os//remove + site: main.py:11 + kind: direct + confidence: high + call_sites: + - caller: main.py//ByClassmethod//__call__ + site: main.py:11 + reached: true + - target: shutil//rmtree + status: found + path: + - key: main.py + - key: main.py//ByPartial//__call__ + site: main.py:48 + kind: reference + confidence: low + - key: shutil//rmtree + site: main.py:20 + kind: direct + confidence: high + call_sites: + - caller: main.py//ByPartial//__call__ + site: main.py:20 + reached: true + - target: subprocess//run + status: found + path: + - key: main.py + - key: main.py//ByDefault//__getattr__ + site: main.py:30 + kind: reference + confidence: low + - key: subprocess//run + site: main.py:25 + kind: direct + confidence: high + call_sites: + - caller: main.py//ByDefault//__getattr__ + site: main.py:25 + reached: true + - target: tempfile//mkdtemp + status: not-found + call_sites: + - caller: main.py//Meta//__call__ + site: main.py:35 + reached: false diff --git a/test/projectgraph/cases/python/instance-made-by-reference/project/main.py b/test/projectgraph/cases/python/instance-made-by-reference/project/main.py new file mode 100644 index 00000000..08915d0c --- /dev/null +++ b/test/projectgraph/cases/python/instance-made-by-reference/project/main.py @@ -0,0 +1,50 @@ +import functools +import os +import shutil +import subprocess +import tempfile +from dataclasses import dataclass, field + + +class ByClassmethod: + def __call__(self): + os.remove("a") + + @classmethod + def create(cls): + return cls() + + +class ByPartial: + def __call__(self): + shutil.rmtree("b") + + +class ByDefault: + def __getattr__(self, name): + subprocess.run("c") + + +@dataclass +class Holder: + d: ByDefault = field(default_factory=ByDefault) + + +class Meta(type): + def __call__(cls, *args): + tempfile.mkdtemp() + + +def run(f): + f() + + +def check(x): + return isinstance(x, Meta) + + +if __name__ == "__main__": + run(ByClassmethod.create()) + run(functools.partial(ByPartial)()) + print(Holder().d.anything) + check(1) diff --git a/test/projectgraph/cases/python/instance-methods-no-instance/expect.yaml b/test/projectgraph/cases/python/instance-methods-no-instance/expect.yaml index 8af4de05..f7346200 100644 --- a/test/projectgraph/cases/python/instance-methods-no-instance/expect.yaml +++ b/test/projectgraph/cases/python/instance-methods-no-instance/expect.yaml @@ -2,9 +2,9 @@ source: safedep:hand description: >- Proxy defines __call__ and __getattr__, and Desc defines __get__. No code makes an instance of them: check names them only in isinstance, and main calls a static method and reads a class attribute. So no - edge leads to these methods, and their targets are not-found. Made names the metaclass Meta, so its - class statement makes an instance of Meta. A call of Made runs Meta.__call__, so the reference to Meta - at the class statement leads to it and tempfile.mkdtemp is found. + edge leads to these methods, and their targets are not-found. Made names the metaclass Meta, and main + calls Made, which runs Meta.__call__. So the call of Made leads to Meta.__call__, and tempfile.mkdtemp + is found. roots: - key: main.py kind: main @@ -16,11 +16,6 @@ edges: kind: reference confidence: low site: main.py:31 - - from: main.py - to: main.py//Meta//__call__ - kind: reference - confidence: low - site: main.py:31 - from: main.py to: main.py//main kind: direct @@ -86,6 +81,11 @@ edges: kind: direct confidence: high site: main.py:43 + - from: main.py//main + to: main.py//Meta//__call__ + kind: reference + confidence: low + site: main.py:43 unresolved: [] results: - target: os//remove @@ -110,9 +110,12 @@ results: status: found path: - key: main.py - root: main + - key: main.py//main + site: main.py:47 + kind: direct + confidence: high - key: main.py//Meta//__call__ - site: main.py:31 + site: main.py:43 kind: reference confidence: low - key: tempfile//mkdtemp From d6de58f112e369240d3b3c57292d87de287582fb Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:28:32 +0000 Subject: [PATCH 36/86] fix(rootmatch): reach the route exports that are no object literal A route root reached no function of an export whose value is no object literal, so its targets read as not-found. export const handle = sequence(first) recorded no reference to first, because the index took each name in an export statement as an export name. A default of a pattern, as { PUT = fb } = h, and an export of a name that holds another name gave no root of the function either. The index now records an argument of a call in an export statement as a reference, gives a pattern default and an aliased name an export record of their own, and marks an export whose value is no literal as Opaque. A path rule marks each export record that resolves, and gives an opaque export a computed-name record in place of a root, so its targets read uncertain. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 13 +- fileindex/index.go | 4 + plugin/indexer/exports.go | 123 +++++++++-- plugin/indexer/references.go | 18 +- projectgraph/rootmatch/framework.go | 16 ++ projectgraph/rootmatch/match.go | 5 + projectgraph/roots.go | 29 ++- .../sveltekit-opaque-exports/expect.yaml | 207 ++++++++++++++++++ .../project/package.json | 5 + .../project/server.js | 3 + .../project/src/hooks.server.ts | 14 ++ .../project/src/routes/a/+server.ts | 12 + .../project/src/routes/b/+page.server.ts | 12 + 13 files changed, 438 insertions(+), 23 deletions(-) create mode 100644 test/projectgraph/cases/javascript/sveltekit-opaque-exports/expect.yaml create mode 100644 test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/package.json create mode 100644 test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/server.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/hooks.server.ts create mode 100644 test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/routes/a/+server.ts create mode 100644 test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/routes/b/+page.server.ts diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 37a5c02c..001f7430 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -764,7 +764,18 @@ edge leads from the object to it. A destructured export, as the member that the name takes, as `handlers//GET`, also through a nested pattern and a rename. A name of an array pattern or a rest element takes the object that holds it. The case `javascript/sveltekit-destructured-export` -shows each form. The resolver maps the alias `$lib` of SvelteKit +shows each form. +A default of a pattern, as `fb` in `{ PUT = fb } = h`, is a second export +record of the name, and a path rule marks each record that resolves. An +export of a name that holds another name, as `export const GET = handler`, +gives that name first. An exported name whose value is no literal, as the +result of a call in `export const actions = makeActions()`, is `Opaque` in +the index. The framework calls the functions of such a value, and no edge +names them, so a path rule gives the export a `computed-name` record in +place of a root. The index records a function that an argument of a call +in an export statement names, as `first` in +`export const handle = sequence(first)`, as a reference. The case +`javascript/sveltekit-opaque-exports` shows each form. The resolver maps the alias `$lib` of SvelteKit to `src/lib` of the nearest package that depends on `@sveltejs/kit`. It does not read `svelte.config.js`, which can move the directory. So an import of `$lib` that names no file is first party, and it is a record diff --git a/fileindex/index.go b/fileindex/index.go index e4b1493a..d98b6c25 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -234,6 +234,10 @@ type Export struct { From string `json:"from,omitempty"` Item string `json:"item,omitempty"` Line int `json:"line"` + // Opaque marks a name whose value the index does not follow, as the + // result of a call in export const actions = makeActions(). Code that + // calls a function of the value can run any code of it. + Opaque bool `json:"opaque,omitempty"` } // Definition is one module, function, method, class, constructor, module diff --git a/plugin/indexer/exports.go b/plugin/indexer/exports.go index cb242ce0..5110f2b7 100644 --- a/plugin/indexer/exports.go +++ b/plugin/indexer/exports.go @@ -76,10 +76,13 @@ func (p *projection) addExportStatement(statement *sitter.Node) { } if declaration := statement.ChildByFieldName("declaration"); declaration != nil { + if t := declaration.Type(); t == "lexical_declaration" || t == "variable_declaration" { + p.exportDeclarators(declaration, line) + return + } for _, name := range p.declaredNames(declaration) { p.export(named(name), name, line) } - p.exportPatterns(declaration, line) return } if value := statement.ChildByFieldName("value"); value != nil { @@ -109,7 +112,7 @@ func (p *projection) addExportStatement(statement *sitter.Node) { if source != "" { p.reexport(name, source, local, line) } else { - p.export(name, local, line) + p.exportValue(name, local, p.topValue(local), line) } } case "namespace_export": @@ -144,25 +147,100 @@ func (p *projection) declaredNames(declaration *sitter.Node) []string { return nil } -// exportPatterns adds the exports of the names that a destructuring -// declaration binds, as GET in export const { GET } = handlers. The -// target of a name is the member of the object that it takes, as -// handlers//GET. A framework calls the function of an exported name, and -// without the record no root names the function. A name of an array -// pattern or a rest element takes no member that a key names, so its -// target is the object that holds it. A value that is no name of the file -// gives no target, and the call of the value holds its code. -func (p *projection) exportPatterns(declaration *sitter.Node, line int) { - if t := declaration.Type(); t != "lexical_declaration" && t != "variable_declaration" { - return - } +// exportDeclarators adds the exports of the names that an exported +// variable declaration binds. A name of a destructuring pattern, as GET in +// export const { GET } = handlers, takes the member of the value that it +// names, as handlers//GET. A framework calls the function of an exported +// name, and without the record no root names the function. A name of an +// array pattern or a rest element takes no member that a key names, so +// its target is the object that holds it. +func (p *projection) exportDeclarators(declaration *sitter.Node, line int) { for _, declarator := range ts.NamedChildren(declaration) { pattern, value := declarator.ChildByFieldName("name"), declarator.ChildByFieldName("value") - if pattern == nil || pattern.Type() == "identifier" || value == nil || value.Type() != "identifier" { + switch { + case pattern == nil: + case pattern.Type() == "identifier": + name := p.text(pattern) + p.exportValue(name, name, value, line) + case value != nil && value.Type() == "identifier" && !opaque(p.topValue(p.text(value))): + p.exportPattern(pattern, []string{p.text(value)}, line) + default: + for _, name := range patternNames(p, pattern) { + p.export(name, name, line) + p.ix.Exports[len(p.ix.Exports)-1].Opaque = true + } + } + } +} + +// exportValue adds the export of a local name that holds a value. A name +// that holds another name gives that name first, as handler for GET in +// export const GET = handler, so the export reaches its function. A value +// that is no literal, as the result of a call, is opaque. +func (p *projection) exportValue(name, local string, value *sitter.Node, line int) { + if value != nil && value.Type() == "identifier" && p.text(value) != local { + p.export(name, p.text(value), line) + } + p.export(name, local, line) + if value != nil && value.Type() != "identifier" && opaque(value) { + p.ix.Exports[len(p.ix.Exports)-1].Opaque = true + } +} + +// opaque reports whether a value runs code that the index does not follow +// to a definition, as a call. A nil value is a name that no top-level +// declaration of the file binds, as an import. +func opaque(value *sitter.Node) bool { + return value != nil && value.Type() != "identifier" && !functionValues[value.Type()] && !literalValues[value.Type()] +} + +// literalValues are the JavaScript values that run no code of their own +// when code reads them. A function of an object or an array is a +// definition of the file. +var literalValues = map[string]bool{ + "string": true, "template_string": true, "number": true, "true": true, "false": true, "null": true, + "undefined": true, "regex": true, "object": true, "array": true, "class": true, +} + +// topValue returns the value of a top-level variable of the file, or nil. +func (p *projection) topValue(name string) *sitter.Node { + for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { + if statement.Type() == "export_statement" { + statement = statement.ChildByFieldName("declaration") + } + if statement == nil || statement.Type() != "lexical_declaration" && statement.Type() != "variable_declaration" { continue } - p.exportPattern(pattern, []string{p.text(value)}, line) + for _, declarator := range ts.NamedChildren(statement) { + if n := declarator.ChildByFieldName("name"); n != nil && n.Type() == "identifier" && p.text(n) == name { + return declarator.ChildByFieldName("value") + } + } } + return nil +} + +// patternNames returns the names that a destructuring pattern binds. +func patternNames(p *projection, pattern *sitter.Node) []string { + switch pattern.Type() { + case "identifier", "shorthand_property_identifier_pattern": + return []string{p.text(pattern)} + case "assignment_pattern", "object_assignment_pattern": + if left := pattern.ChildByFieldName("left"); left != nil { + return patternNames(p, left) + } + case "pair_pattern": + if value := pattern.ChildByFieldName("value"); value != nil { + return patternNames(p, value) + } + case "object_pattern", "array_pattern", "rest_pattern": + var names []string + for _, child := range ts.NamedChildren(pattern) { + names = append(names, patternNames(p, child)...) + } + return names + } + return nil } // exportPattern adds the exports of the names of a pattern whose value is @@ -176,8 +254,17 @@ func (p *projection) exportPattern(pattern *sitter.Node, path []string, line int name := p.text(pattern) p.export(name, fileindex.Join(path[0], append(slices.Clip(path[1:]), name)...), line) case "assignment_pattern", "object_assignment_pattern": - if left := pattern.ChildByFieldName("left"); left != nil { - p.exportPattern(left, path, line) + // A default holds the value when the member is undefined, as fb + // for PUT in { PUT = fb } = h. + left, right := pattern.ChildByFieldName("left"), pattern.ChildByFieldName("right") + if left == nil { + return + } + p.exportPattern(left, path, line) + if right != nil && right.Type() == "identifier" { + for _, name := range patternNames(p, left) { + p.export(name, p.text(right), line) + } } case "pair_pattern": key, value := pattern.ChildByFieldName("key"), pattern.ChildByFieldName("value") diff --git a/plugin/indexer/references.go b/plugin/indexer/references.go index 599b880d..e55b8b9a 100644 --- a/plugin/indexer/references.go +++ b/plugin/indexer/references.go @@ -261,7 +261,7 @@ func (syntax referenceSyntax) unlist(values []*sitter.Node) []*sitter.Node { func (p *projection) reference(syntax referenceSyntax, caller string, value *sitter.Node) (string, bool) { // A name in the body of a function that an export statement declares // is a value of that function, not the export. - if value == nil || caller == p.cg.FileName && p.inExport(value) && syntax.identifiers[value.Type()] { + if value == nil || caller == p.cg.FileName && p.exportsName(value) && syntax.identifiers[value.Type()] { return "", false } var names []string @@ -322,6 +322,22 @@ func (p *projection) valueOf(syntax referenceSyntax, caller, name string) (strin return "", false } +// exportsName reports whether a name in an export statement or a CommonJS +// export assignment is a name that the export gives, as a in export { a }. +// An argument of a call in the statement is a value that the callee gets, +// as g in export const handle = sequence(g), so it is no export name. +func (p *projection) exportsName(node *sitter.Node) bool { + if !p.inExport(node) { + return false + } + for n := node.Parent(); n != nil; n = n.Parent() { + if n.Type() == "arguments" { + return false + } + } + return true +} + // inExport reports whether the node is part of an export statement or a // CommonJS export assignment. func (p *projection) inExport(node *sitter.Node) bool { diff --git a/projectgraph/rootmatch/framework.go b/projectgraph/rootmatch/framework.go index e2a86658..2ec8e96a 100644 --- a/projectgraph/rootmatch/framework.go +++ b/projectgraph/rootmatch/framework.go @@ -29,7 +29,23 @@ func (f *finder) matchPath(r rules.Rule) { f.add(r, key) } f.addMembers(r, ix, exports) + for _, ex := range ix.Exports { + if ex.Opaque { + f.addOpaque(r, ix.Path, ex) + } + } + } +} + +// addOpaque marks an export whose value the index does not follow. +func (f *finder) addOpaque(r rules.Rule, path string, ex fileindex.Export) { + root := Root{Key: path, Kind: r.Kind, Rule: r.ID, Opaque: ex.Name} + if f.seen[root] { + return } + f.seen[root] = true + root.Site = Site{Path: path, Line: ex.Line} + f.roots = append(f.roots, root) } // addMembers marks each function under an exported object of the file. The diff --git a/projectgraph/rootmatch/match.go b/projectgraph/rootmatch/match.go index 71c628a2..78ccb403 100644 --- a/projectgraph/rootmatch/match.go +++ b/projectgraph/rootmatch/match.go @@ -30,6 +30,11 @@ type Root struct { Kind rules.Kind Rule string Site Site + // Opaque names an export of the file of Key whose value the index + // does not follow, as the result of a call. The framework calls the + // functions of the value, so the link step gives the site a record in + // place of a root. + Opaque string } // Site is a place in a file. Lines and columns are 1-based. diff --git a/projectgraph/roots.go b/projectgraph/roots.go index 6077f93f..9eab8dc7 100644 --- a/projectgraph/roots.go +++ b/projectgraph/roots.go @@ -55,6 +55,13 @@ func (b *builder) markRoots(ctx context.Context) error { } entry := false for _, r := range found { + // The framework calls the functions of the value of an opaque + // export, and no edge names them. The record keeps a target that + // only they call uncertain and not not-found. + if r.Opaque != "" { + b.addUnresolved(UnresolvedSite{Caller: r.Key, Site: Site(r.Site), Text: "export " + r.Opaque, Reason: fileindex.UnresolvedComputedName}) + continue + } b.addRootAt(r.Key, r.Kind, r.Rule, Site(r.Site)) helper := b.profileAt(r.Key).helperProgram entry = entry || r.Kind != RootTest && !keeps[r.Rule] && (helper == nil || !helper(fileindex.Unit(r.Key))) @@ -162,9 +169,25 @@ func (b *builder) exportKeys(p string, seen map[string]bool) []string { } } } - for name := range sc.exports { - if key, fail := b.resolveName(p, name, 0); fail == nil && b.kindOf(b.deref(key)) != NodeInterface { - keys = append(keys, key) + // A name can have more than one export record, as PUT in + // export const { PUT = fb } = h, which is h.PUT or fb. Each record that + // resolves gives a key. + add := func(key string) bool { + if b.kindOf(b.deref(key)) == NodeInterface { + return false + } + keys = append(keys, key) + return true + } + for name, exs := range sc.exports { + found := false + for _, ex := range exs { + if key, fail := b.followExport(sc, ex, 0); fail == nil { + found = add(key) || found + } + } + if key, fail := b.resolveName(p, name, 0); !found && fail == nil { + add(key) } } for _, st := range sc.stars { diff --git a/test/projectgraph/cases/javascript/sveltekit-opaque-exports/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/expect.yaml new file mode 100644 index 00000000..b17afefc --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/expect.yaml @@ -0,0 +1,207 @@ +source: safedep:hand +description: >- + The route files export values that are no object literal. handle is the result of sequence, and the + index now records the arguments first and second of the call in the export statement as references, so + h1 and h2 are found. other names the variable named, which holds table, so the export reaches table and + the rule marks save. PUT takes the default fallback, because handlers has no PUT. GET and actions are + results of calls, and the functions that the framework calls on them are no definition that an edge + names. So each of the two exports gets a computed-name record in place of a root, and a1 and a4 read + uncertain and not not-found. +roots: + - key: server.js + kind: manifest + rule: node.package-json + - key: src/hooks.server.ts + kind: framework + rule: node.sveltekit.route + - key: src/hooks.server.ts//handle + kind: framework + rule: node.sveltekit.route + - key: src/routes/a/+server.ts + kind: framework + rule: node.sveltekit.route + - key: src/routes/a/+server.ts//fallback + kind: framework + rule: node.sveltekit.route + - key: src/routes/b/+page.server.ts + kind: framework + rule: node.sveltekit.route + - key: src/routes/b/+page.server.ts//actions + kind: framework + rule: node.sveltekit.route + - key: src/routes/b/+page.server.ts//named + kind: framework + rule: node.sveltekit.route + - key: src/routes/b/+page.server.ts//table + kind: framework + rule: node.sveltekit.route + - key: src/routes/b/+page.server.ts//table//save + kind: framework + rule: node.sveltekit.route +exact_edges: true +edges: + - from: server.js + to: plug//listen + kind: direct + confidence: high + site: server.js:3 + - from: src/hooks.server.ts + to: "@sveltejs//kit//hooks//sequence" + kind: direct + confidence: high + site: src/hooks.server.ts:14 + - from: src/hooks.server.ts + to: src/hooks.server.ts//first + kind: reference + confidence: low + site: src/hooks.server.ts:14 + - from: src/hooks.server.ts + to: src/hooks.server.ts//second + kind: reference + confidence: low + site: src/hooks.server.ts:14 + - from: src/hooks.server.ts//first + to: plug//h1 + kind: direct + confidence: high + site: src/hooks.server.ts:5 + - from: src/hooks.server.ts//second + to: plug//h2 + kind: direct + confidence: high + site: src/hooks.server.ts:10 + - from: src/routes/a/+server.ts + to: src/routes/a/+server.ts//make + kind: direct + confidence: high + site: src/routes/a/+server.ts:7 + - from: src/routes/a/+server.ts//fallback + to: plug//a3 + kind: direct + confidence: high + site: src/routes/a/+server.ts:9 + - from: src/routes/a/+server.ts//make//GET + to: plug//a1 + kind: direct + confidence: high + site: src/routes/a/+server.ts:4 + - from: src/routes/b/+page.server.ts + to: src/routes/b/+page.server.ts//makeActions + kind: direct + confidence: high + site: src/routes/b/+page.server.ts:7 + - from: src/routes/b/+page.server.ts//makeActions//default + to: plug//a4 + kind: direct + confidence: high + site: src/routes/b/+page.server.ts:4 + - from: src/routes/b/+page.server.ts//table//save + to: plug//a5 + kind: direct + confidence: high + site: src/routes/b/+page.server.ts:9 +unresolved: + - caller: src/hooks.server.ts//first + site: src/hooks.server.ts:6 + reason: unknown-receiver + method: resolve + - caller: src/hooks.server.ts//second + site: src/hooks.server.ts:11 + reason: unknown-receiver + method: resolve + - caller: src/hooks.server.ts + site: src/hooks.server.ts:14 + reason: computed-name + - caller: src/routes/a/+server.ts + site: src/routes/a/+server.ts:7 + reason: computed-name + - caller: src/routes/b/+page.server.ts + site: src/routes/b/+page.server.ts:7 + reason: computed-name +results: + - target: plug//h1 + status: found + path: + - key: src/hooks.server.ts + root: framework + - key: src/hooks.server.ts//first + site: src/hooks.server.ts:14 + kind: reference + confidence: low + - key: plug//h1 + site: src/hooks.server.ts:5 + kind: direct + confidence: high + call_sites: + - caller: src/hooks.server.ts//first + site: src/hooks.server.ts:5 + reached: true + - target: plug//h2 + status: found + path: + - key: src/hooks.server.ts + root: framework + - key: src/hooks.server.ts//second + site: src/hooks.server.ts:14 + kind: reference + confidence: low + - key: plug//h2 + site: src/hooks.server.ts:10 + kind: direct + confidence: high + call_sites: + - caller: src/hooks.server.ts//second + site: src/hooks.server.ts:10 + reached: true + - target: plug//a1 + status: uncertain + call_sites: + - caller: src/routes/a/+server.ts//make//GET + site: src/routes/a/+server.ts:4 + reached: false + uncertainties: + - kind: unresolved-call + site: src/hooks.server.ts:14 + - kind: unresolved-call + site: src/routes/a/+server.ts:7 + - kind: unresolved-call + site: src/routes/b/+page.server.ts:7 + - target: plug//a3 + status: found + path: + - key: src/routes/a/+server.ts//fallback + root: framework + - key: plug//a3 + site: src/routes/a/+server.ts:9 + kind: direct + confidence: high + call_sites: + - caller: src/routes/a/+server.ts//fallback + site: src/routes/a/+server.ts:9 + reached: true + - target: plug//a4 + status: uncertain + call_sites: + - caller: src/routes/b/+page.server.ts//makeActions//default + site: src/routes/b/+page.server.ts:4 + reached: false + uncertainties: + - kind: unresolved-call + site: src/hooks.server.ts:14 + - kind: unresolved-call + site: src/routes/a/+server.ts:7 + - kind: unresolved-call + site: src/routes/b/+page.server.ts:7 + - target: plug//a5 + status: found + path: + - key: src/routes/b/+page.server.ts//table//save + root: framework + - key: plug//a5 + site: src/routes/b/+page.server.ts:9 + kind: direct + confidence: high + call_sites: + - caller: src/routes/b/+page.server.ts//table//save + site: src/routes/b/+page.server.ts:9 + reached: true diff --git a/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/package.json b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/package.json new file mode 100644 index 00000000..6d911bbb --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/package.json @@ -0,0 +1,5 @@ +{ + "name": "sveltekit-opaque-exports", + "main": "server.js", + "devDependencies": { "@sveltejs/kit": "^2.5.0" } +} diff --git a/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/server.js b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/server.js new file mode 100644 index 00000000..fc1cd6f8 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/server.js @@ -0,0 +1,3 @@ +import { listen } from 'plug'; + +listen(3000); diff --git a/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/hooks.server.ts b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/hooks.server.ts new file mode 100644 index 00000000..37132cb0 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/hooks.server.ts @@ -0,0 +1,14 @@ +import { sequence } from '@sveltejs/kit/hooks'; +import { h1, h2 } from 'plug'; + +const first = async ({ event, resolve }) => { + h1(); + return resolve(event); +}; + +async function second({ event, resolve }) { + h2(); + return resolve(event); +} + +export const handle = sequence(first, second); diff --git a/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/routes/a/+server.ts b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/routes/a/+server.ts new file mode 100644 index 00000000..46e7b0cc --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/routes/a/+server.ts @@ -0,0 +1,12 @@ +import { a1, a3 } from 'plug'; + +function make() { + return { GET() { a1(); } }; +} + +export const { GET } = make(); + +const fallback = () => a3(); +const handlers = {}; + +export const { PUT = fallback } = handlers; diff --git a/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/routes/b/+page.server.ts b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/routes/b/+page.server.ts new file mode 100644 index 00000000..fbffba0a --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/project/src/routes/b/+page.server.ts @@ -0,0 +1,12 @@ +import { a4, a5 } from 'plug'; + +function makeActions() { + return { default: async () => a4() }; +} + +export const actions = makeActions(); + +const table = { save: async () => a5() }; +const named = table; + +export { named as other }; From c175d38a21e59e0f1cfc0a35490d95c146b980a2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:33:35 +0000 Subject: [PATCH 37/86] perf(projectgraph): read each Rust glob module once per lookup of a name rustGlobUnitMember guarded only the path of the current lookup, so a lookup read a module once for each path through the glob uses. Nine files that glob use each other took minutes, and a diamond of twenty levels took seconds. One lookup now reads each module once: a module that it reads again counts as a cut. An answer that no cut gave stays for the build, with an undo step for a glob pass that does not settle. uncut now counts the guards of a Rust glob path and of a glob member, so a wildcard read under them keeps no list of modules. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/resolve.go | 5 ++- projectgraph/rust.go | 35 ++++++++++++++-- projectgraph/rust_test.go | 19 +++++++++ test/projectgraph/deadline_test.go | 67 ++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 5 deletions(-) diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index f104037f..83257c74 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -622,12 +622,15 @@ func (b *builder) undo(mark int) { // path in progress was set before the read, and the guard cut the read if // the read asked for it. // +// The guards of a Rust glob path and of a member of a glob module cut a +// read too, so a read under them is no whole read. +// // uncut compares the paths of the resolving guard only. On the Rust // corpus, the reads under any other guard cost less than 3% of the read // time. So any other state in progress fails the check. func (b *builder) uncut(asked map[[2]string]bool) bool { s := b.rustState() - if len(b.expanding)+len(b.visiting)+len(b.reading)+len(b.stepping)+len(b.findingBases)+len(s.routing)+b.ordering > 0 { + if len(b.expanding)+len(b.visiting)+len(b.reading)+len(b.stepping)+len(b.findingBases)+len(s.routing)+len(s.globbing)+len(s.members)+b.ordering > 0 { return false } for k := range s.resolving { diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 8f1bc552..12651122 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -160,6 +160,9 @@ type rustState struct { globbing map[[2]string]bool globPaths map[[2]string]globPath members map[[2]string]bool + // memberAnswers holds the answers of rustGlobUnitMember that no cut + // gave. + memberAnswers map[[2]string]memberAnswer } // crateRoute is the path in another crate that a path of a file names, and @@ -200,6 +203,7 @@ func (b *builder) addRustUnits() { s.globbing = map[[2]string]bool{} s.globPaths = map[[2]string]globPath{} s.members = map[[2]string]bool{} + s.memberAnswers = map[[2]string]memberAnswer{} files := map[string][]string{} for _, p := range b.paths { if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust { @@ -956,18 +960,41 @@ func (b *builder) rustGlobMember(sc *scope, name string) (string, bool) { // rustGlobUnitMember returns what a glob use of a module brings for a name // that no definition of the module has: a child module, or the target of a // use or of a glob use of the module, as leaf for use crate::sub::* with -// pub mod leaf in sub. Two modules can bring each other with glob uses, so -// the guard cuts a read that asks for the same name again. +// pub mod leaf in sub. +// +// Modules can bring each other with glob uses, in a diamond or in a cycle. +// A walk of each path through them grows with the number of paths. So one +// lookup reads each module once: a module that the lookup reads again +// counts as a cut, and the lookup keeps no answer that a cut gave. An +// answer that no cut gave stays for the build, so a diamond reads each +// module once in all. func (b *builder) rustGlobUnitMember(mod, name string) (string, bool) { s := b.rustState() k := [2]string{mod, name} + if m, ok := s.memberAnswers[k]; ok { + return m.key, m.ok + } if s.members[k] { b.cuts++ return "", false } + if len(s.members) == 0 { + defer clear(s.members) + } s.members[k] = true - defer delete(s.members, k) - return b.unitMember(mod, name) + cuts := b.cuts + key, ok := b.unitMember(mod, name) + if b.cuts == cuts { + s.memberAnswers[k] = memberAnswer{key: key, ok: ok} + b.memo(func() { delete(s.memberAnswers, k) }) + } + return key, ok +} + +// memberAnswer is the answer of rustGlobUnitMember for a module and a name. +type memberAnswer struct { + key string + ok bool } // rustUnitFile reports whether a file is Rust code of a unit of dependency diff --git a/projectgraph/rust_test.go b/projectgraph/rust_test.go index d50164d8..69c4b26c 100644 --- a/projectgraph/rust_test.go +++ b/projectgraph/rust_test.go @@ -103,3 +103,22 @@ func TestRustHelperProgram(t *testing.T) { }) } } + +// TestUncutCountsTheRustGlobGuards checks that a read of the wildcard +// imports of a scope under the guard of a Rust glob path or of a glob +// member is no whole read. Such a guard cuts the read of a module, and a +// list of modules that a cut read gave stayed for the build. +func TestUncutCountsTheRustGlobGuards(t *testing.T) { + for name, set := range map[string]func(s *rustState){ + "globbing": func(s *rustState) { s.globbing = map[[2]string]bool{{"src/a.rs", "x"}: true} }, + "members": func(s *rustState) { s.members = map[[2]string]bool{{"rust:app::c", "x"}: true} }, + } { + t.Run(name, func(t *testing.T) { + b, err := newBuilder(nil, Options{}) + require.NoError(t, err) + require.True(t, b.uncut(nil)) + set(b.rustState()) + assert.False(t, b.uncut(nil)) + }) + } +} diff --git a/test/projectgraph/deadline_test.go b/test/projectgraph/deadline_test.go index 4dd72c1c..134c6787 100644 --- a/test/projectgraph/deadline_test.go +++ b/test/projectgraph/deadline_test.go @@ -12,6 +12,7 @@ import ( codefs "github.com/safedep/code/fs" "github.com/safedep/code/plugin/indexer" "github.com/safedep/code/projectgraph" + "github.com/safedep/code/projectgraph/reach" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -54,3 +55,69 @@ func TestBuildStopsAtTheDeadline(t *testing.T) { assert.ErrorIs(t, err, context.DeadlineExceeded) assert.Less(t, time.Since(start), 10*time.Second) } + +// TestBuildReadsEachGlobModuleOncePerName links two layouts of Rust glob +// uses. In the first, nine files each glob use every other file. In the +// second, a chain of twenty levels of two files each glob uses both files of +// the next level, and only one file of the last level defines leaf. A +// lookup that reads a module once per path through the glob uses grows +// with the number of paths, and the build did not end within minutes. +func TestBuildReadsEachGlobModuleOncePerName(t *testing.T) { + complete := map[string]string{} + var main strings.Builder + for i := range 9 { + fmt.Fprintf(&main, "mod m%d;\n", i) + var file strings.Builder + for j := range 9 { + if j != i { + fmt.Fprintf(&file, "pub use crate::m%d::*;\n", j) + } + } + fmt.Fprintf(&file, "\npub fn f%d() {\n let _ = std::fs::remove_file(\"a\");\n}\n", i) + complete[fmt.Sprintf("src/m%d.rs", i)] = file.String() + } + main.WriteString("\nuse m0::*;\n\nfn main() {\n f0();\n gone();\n}\n") + complete["src/main.rs"] = main.String() + + const levels = 20 + diamond := map[string]string{} + main.Reset() + for i := range levels { + for _, side := range []string{"a", "b"} { + fmt.Fprintf(&main, "mod l%d%s;\n", i, side) + var file strings.Builder + if i+1 < levels { + fmt.Fprintf(&file, "pub use crate::l%da::*;\npub use crate::l%db::*;\n", i+1, i+1) + } else if side == "a" { + file.WriteString("pub fn leaf() {\n let _ = std::fs::remove_file(\"a\");\n}\n") + } + fmt.Fprintf(&file, "\npub fn own_%d%s() {}\n", i, side) + diamond[fmt.Sprintf("src/l%d%s.rs", i, side)] = file.String() + } + } + main.WriteString("\nuse l0a::*;\n\nfn main() {\n leaf();\n gone();\n}\n") + diamond["src/main.rs"] = main.String() + + for name, files := range map[string]map[string]string{"complete": complete, "diamond": diamond} { + t.Run(name, func(t *testing.T) { + dir := t.TempDir() + files["Cargo.toml"] = "[package]\nname = \"app\"\nversion = \"0.1.0\"\n" + for p, text := range files { + require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(dir, p)), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, p), []byte(text), 0o644)) + } + fsys, err := codefs.NewLocalFileSystem(codefs.LocalFileSystemConfig{AppDirectories: []string{dir}}) + require.NoError(t, err) + indexed := indexProject(t, fsys, indexer.Options{Languages: caseLanguages(t)}) + + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + g, err := projectgraph.Build(ctx, indexed.Indexes, projectgraph.Options{Root: os.DirFS(dir), Files: indexed.Statuses(), Languages: indexed.Languages}) + require.NoError(t, err) + results, err := reach.Reach(ctx, g, reach.Query{Targets: targetsOf(t, "std//fs//remove_file")}) + require.NoError(t, err) + require.Len(t, results, 1) + assert.Equal(t, reach.Found, results[0].Status) + }) + } +} From 5a8c0f0e1166066d68c61bd460e2c589a18a625f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:34:18 +0000 Subject: [PATCH 38/86] fix(projectgraph): join a Rust impl block only through a use of the root joinImplBlocks read the binding of a name from the scope of the file, which holds the uses of the inline modules and of the blocks too. So use crate::other::A in mod deep joined a struct A of the root with the A of src/other.rs. A call of run on the other A then went to the run of the root A with no record, and the real target read not-found. A use at the root of the file now joins alone. The root cannot define a type of a name that such a use binds, so the class is an impl block. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/rust.go | 20 +++++-- .../rust/use-shadow-root-type/expect.yaml | 54 +++++++++++++++++++ .../use-shadow-root-type/project/Cargo.toml | 4 ++ .../use-shadow-root-type/project/src/main.rs | 47 ++++++++++++++++ .../use-shadow-root-type/project/src/other.rs | 11 ++++ 5 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 test/projectgraph/cases/rust/use-shadow-root-type/expect.yaml create mode 100644 test/projectgraph/cases/rust/use-shadow-root-type/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-shadow-root-type/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-shadow-root-type/project/src/other.rs diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 12651122..926ad787 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -1005,6 +1005,12 @@ func (b *builder) rustUnitFile(ix *fileindex.Index) bool { // joinImplBlocks makes an impl block of a type of another module one class // with the type, as impl Store for FileStore after use crate::FileStore. +// Only a use at the root of the file counts. The root cannot define a type +// of a name that such a use binds, so the class is an impl block. A use of +// an inline module, as use crate::other::A in mod deep, binds A in deep +// only. Read for the root, it joined a struct A of the root with the A of +// the other module, and a call of a method of the root A went to the other +// A with no record. func (b *builder) joinImplBlocks() { var groups [][]string for _, p := range b.paths { @@ -1014,13 +1020,17 @@ func (b *builder) joinImplBlocks() { } sc := b.scopes[p] for _, c := range ix.Classes { - imp, ok := sc.bindings[fileindex.Last(c.Key)] - if !ok || len(fileindex.Segments(c.Key)) != 1 { + if len(fileindex.Segments(c.Key)) != 1 { continue } - module, _ := importedModule(ix.Language, imp) - if key, _, found := b.resolveModule(ix, module); found && key != c.Key && b.isType(key) { - groups = append(groups, []string{key, c.Key}) + for _, imp := range rustBindings(sc, fileindex.Last(c.Key)) { + if imp.Item == "" && imp.Alias == "" || sc.enclosing(imp.Line) != p { + continue + } + module, _ := importedModule(ix.Language, imp) + if key, _, found := b.resolveModule(ix, module); found && key != c.Key && b.isType(key) { + groups = append(groups, []string{key, c.Key}) + } } } } diff --git a/test/projectgraph/cases/rust/use-shadow-root-type/expect.yaml b/test/projectgraph/cases/rust/use-shadow-root-type/expect.yaml new file mode 100644 index 00000000..f42088d6 --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadow-root-type/expect.yaml @@ -0,0 +1,54 @@ +source: safedep:hand +description: >- + src/main.rs declares a struct A at its root, and src/other.rs declares another A. The inline module deep has + use crate::other::A, and the function by_block has the same use in its block. So the field a of Holder, the + parameter x of by_param, the let x of by_new and the calls in by_block hold the A of src/other.rs, whose run + calls std::fs::remove_file. Nothing calls run of the root A, which calls std::fs::remove_dir. A link step that + reads a use of an inline module or of a block for the root of the file joins the root A with the A of + src/other.rs as one class, so each call goes to the run of the root A, std::fs::remove_file reads not-found + and std::fs::remove_dir reads found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +no_edges: + - from: src/main.rs//deep//by_field + to: src/main.rs//A//run + - from: src/main.rs//deep//by_param + to: src/main.rs//A//run + - from: src/main.rs//deep//by_new + to: src/main.rs//A//run + - from: src/main.rs//by_block + to: src/main.rs//A//run +edges: + - from: src/main.rs//deep//by_field + to: src/other.rs//A//run + kind: member + confidence: high + site: src/main.rs:23 + - from: src/main.rs//deep//by_param + to: src/other.rs//A//run + kind: member + confidence: high + site: src/main.rs:27 + - from: src/main.rs//deep//by_new + to: src/other.rs//A//run + kind: member + confidence: high + site: src/main.rs:32 + - from: src/main.rs//by_block + to: src/other.rs//A//run + kind: member + confidence: high + site: src/main.rs:38 +unresolved: [] +results: + - target: std//fs//remove_dir + status: not-found + call_sites: + - caller: src/main.rs//A//run + site: src/main.rs:11 + reached: false diff --git a/test/projectgraph/cases/rust/use-shadow-root-type/project/Cargo.toml b/test/projectgraph/cases/rust/use-shadow-root-type/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadow-root-type/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/use-shadow-root-type/project/src/main.rs b/test/projectgraph/cases/rust/use-shadow-root-type/project/src/main.rs new file mode 100644 index 00000000..d6abed54 --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadow-root-type/project/src/main.rs @@ -0,0 +1,47 @@ +mod other; + +pub struct A; + +impl A { + pub fn new() -> A { + A + } + + pub fn run(&self) { + let _ = std::fs::remove_dir("a"); + } +} + +mod deep { + use crate::other::A; + + pub struct Holder { + pub a: A, + } + + pub fn by_field(h: &Holder) { + h.a.run(); + } + + pub fn by_param(x: A) { + x.run(); + } + + pub fn by_new() { + let x = A::new(); + x.run(); + } +} + +fn by_block() { + use crate::other::A; + A::new().run(); +} + +fn main() { + deep::by_field(&deep::Holder { a: other::A::new() }); + deep::by_param(other::A::new()); + deep::by_new(); + by_block(); + let _ = A::new(); +} diff --git a/test/projectgraph/cases/rust/use-shadow-root-type/project/src/other.rs b/test/projectgraph/cases/rust/use-shadow-root-type/project/src/other.rs new file mode 100644 index 00000000..f20cbee7 --- /dev/null +++ b/test/projectgraph/cases/rust/use-shadow-root-type/project/src/other.rs @@ -0,0 +1,11 @@ +pub struct A; + +impl A { + pub fn new() -> A { + A + } + + pub fn run(&self) { + let _ = std::fs::remove_file("a"); + } +} From a40bf41c8d93d228446edd5fbc2476d1530446de Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:34:46 +0000 Subject: [PATCH 39/86] fix(indexer): record a Rust call whose type name a glob use can bring The call graph finds the first name of a path in the scopes that hold the call, and reads no glob use. So A::new() and a.run() in mod n with use crate::other::* went to a struct A of the parent mod m, which n does not see, and the run of the A of src/other.rs read not-found. A glob use between the call and the scope of the target now makes the call a record. A glob of super, self, std, core or alloc keeps the target, because it brings no other item of the crate. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 5 +- plugin/indexer/rust.go | 26 +++++++++- .../rust/glob-shadows-outer-type/expect.yaml | 49 +++++++++++++++++++ .../project/Cargo.toml | 4 ++ .../project/src/main.rs | 28 +++++++++++ .../project/src/other.rs | 11 +++++ 6 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 test/projectgraph/cases/rust/glob-shadows-outer-type/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-shadows-outer-type/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-shadows-outer-type/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-shadows-outer-type/project/src/other.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 001f7430..eb138667 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -490,7 +490,10 @@ Rust follows the same rules, with these additions. A method call whose target the call graph finds as a method of a type of an outer scope gets the path of a `use` of the type name the same way, as `x.save()` after `let x = Item::new();` in the case - `rust/use-type-receiver`. + `rust/use-type-receiver`. A glob `use` between the call and that scope + can bring the name too, unless it reads `super`, `self` or a crate of + the toolchain, so the call is a record, as in the case + `rust/glob-shadows-outer-type`. A type parameter of the name names a type that the caller gives, so the call is a record with the reason `unknown-receiver`, as in the case `rust/generic-shadows-type`. diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 598ba8ef..c9326bf4 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -864,13 +864,21 @@ func (p *projection) rustRebinder(node, scope *sitter.Node, name string) (*sitte for n := node.Parent(); n != nil && (scope == nil || !ts.SameNode(n, scope)); n = n.Parent() { switch n.Type() { case "block", "declaration_list": + var globbed *sitter.Node for _, item := range ts.NamedChildren(n) { if item.Type() != "use_declaration" { continue } - if names, _ := p.rustUseNames(item.ChildByFieldName("argument"), nil); slices.Contains(names, name) { + names, glob := p.rustUseNames(item.ChildByFieldName("argument"), nil) + if slices.Contains(names, name) { return item, true } + if glob && !p.rustOuterGlob(item) { + globbed = item + } + } + if globbed != nil { + return globbed, false } case "function_item", "impl_item", "trait_item": if slices.ContainsFunc(ts.NamedChildren(n.ChildByFieldName("type_parameters")), func(param *sitter.Node) bool { @@ -883,6 +891,22 @@ func (p *projection) rustRebinder(node, scope *sitter.Node, name string) (*sitte return nil, false } +// rustOuterGlob reports whether a glob use brings no name that the call +// graph would find in an outer scope instead: a glob of super or self, which +// brings the names of the scopes that hold it, or of std, core or alloc, +// which define no item of the crate. Any other glob use can bring the name, +// as A of use crate::other::* in mod n inside mod m with a struct A. +func (p *projection) rustOuterGlob(use *sitter.Node) bool { + first := rustFirstName(p.text(use.ChildByFieldName("argument"))) + return first == "super" || first == "self" || first == "std" || first == "core" || first == "alloc" +} + +// rustFirstName returns the first name of a path, as std for ::std::io. +func rustFirstName(path string) string { + first, _, _ := strings.Cut(strings.TrimPrefix(path, "::"), "::") + return first +} + // rustUseNamespace returns the namespace of the import of a use that binds // a name. func (p *projection) rustUseNamespace(use *sitter.Node, name string) (string, bool) { diff --git a/test/projectgraph/cases/rust/glob-shadows-outer-type/expect.yaml b/test/projectgraph/cases/rust/glob-shadows-outer-type/expect.yaml new file mode 100644 index 00000000..60808ff4 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-shadows-outer-type/expect.yaml @@ -0,0 +1,49 @@ +source: safedep:hand +description: >- + The inline module m declares a struct A, and its inline module n has use crate::other::*. n sees no item of m + with no use, so A in f names the A of src/other.rs, whose run calls std::fs::remove_file. The call graph + reads no use and finds the A of m, whose run calls std::fs::remove_dir. A call of A::new() and of run in f + then goes to m::A with no record, and std::fs::remove_file reads not-found. The index makes each such call a + record, so the target reads found through the low name edges of run. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//m//n//f + to: src/other.rs//A//run + kind: name + confidence: low + site: src/main.rs:21 +unresolved: + - caller: src/main.rs//m//n//f + site: src/main.rs:20 + reason: unknown-receiver + - caller: src/main.rs//m//n//f + site: src/main.rs:21 + reason: unknown-receiver + method: run +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//m//n//f + site: src/main.rs:27 + kind: direct + confidence: high + - key: src/other.rs//A//run + site: src/main.rs:21 + kind: name + confidence: low + - key: std//fs//remove_file + site: src/other.rs:9 + kind: direct + confidence: high + call_sites: + - caller: src/other.rs//A//run + site: src/other.rs:9 + reached: true diff --git a/test/projectgraph/cases/rust/glob-shadows-outer-type/project/Cargo.toml b/test/projectgraph/cases/rust/glob-shadows-outer-type/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/glob-shadows-outer-type/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-shadows-outer-type/project/src/main.rs b/test/projectgraph/cases/rust/glob-shadows-outer-type/project/src/main.rs new file mode 100644 index 00000000..e28b4226 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-shadows-outer-type/project/src/main.rs @@ -0,0 +1,28 @@ +mod other; + +mod m { + pub struct A; + + impl A { + pub fn new() -> A { + A + } + + pub fn run(&self) { + let _ = std::fs::remove_dir("a"); + } + } + + pub mod n { + use crate::other::*; + + pub fn f() { + let a = A::new(); + a.run(); + } + } +} + +fn main() { + m::n::f(); +} diff --git a/test/projectgraph/cases/rust/glob-shadows-outer-type/project/src/other.rs b/test/projectgraph/cases/rust/glob-shadows-outer-type/project/src/other.rs new file mode 100644 index 00000000..f20cbee7 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-shadows-outer-type/project/src/other.rs @@ -0,0 +1,11 @@ +pub struct A; + +impl A { + pub fn new() -> A { + A + } + + pub fn run(&self) { + let _ = std::fs::remove_file("a"); + } +} From 47438adf20ecc59d3a81096f2039a7dfe114ead5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:36:38 +0000 Subject: [PATCH 40/86] fix(projectgraph): read only pub items of a Rust glob use, in its own scope A glob use of a module of another crate took any item of the unit, also a private one. So render() after use dep::html::* and use other::* went to the private render of dep::html, and util::run() to its private module util, and the code of other that runs read not-found. A glob use now brings only a pub item. A glob use at the root of the file also gave a name to a call in an inline module, which reads its own uses. Such a call now reads no glob use of the root, and keeps its record. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 5 +- projectgraph/rust.go | 65 +++++++++++++++++-- .../rust/glob-use-inline-scope/expect.yaml | 54 +++++++++++++++ .../glob-use-inline-scope/project/Cargo.toml | 8 +++ .../glob-use-inline-scope/project/src/main.rs | 14 ++++ .../project/vendor/dep/Cargo.toml | 4 ++ .../project/vendor/dep/src/html.rs | 5 ++ .../project/vendor/dep/src/lib.rs | 1 + .../project/vendor/other/Cargo.toml | 4 ++ .../project/vendor/other/src/lib.rs | 3 + .../summaries/cargo_dep_1.0.0.json | 23 +++++++ .../summaries/cargo_other_1.0.0.json | 16 +++++ .../rust/glob-use-private-item/expect.yaml | 60 +++++++++++++++++ .../glob-use-private-item/project/Cargo.toml | 8 +++ .../glob-use-private-item/project/src/main.rs | 7 ++ .../project/vendor/dep/Cargo.toml | 4 ++ .../project/vendor/dep/src/html.rs | 11 ++++ .../project/vendor/dep/src/lib.rs | 1 + .../project/vendor/other/Cargo.toml | 4 ++ .../project/vendor/other/src/lib.rs | 5 ++ .../project/vendor/other/src/util.rs | 3 + .../summaries/cargo_dep_1.0.0.json | 23 +++++++ .../summaries/cargo_other_1.0.0.json | 23 +++++++ 23 files changed, 344 insertions(+), 7 deletions(-) create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/src/html.rs create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/other/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/other/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/summaries/cargo_dep_1.0.0.json create mode 100644 test/projectgraph/cases/rust/glob-use-inline-scope/summaries/cargo_other_1.0.0.json create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/src/html.rs create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/src/util.rs create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/summaries/cargo_dep_1.0.0.json create mode 100644 test/projectgraph/cases/rust/glob-use-private-item/summaries/cargo_other_1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index eb138667..31df3d36 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -526,7 +526,10 @@ Rust follows the same rules, with these additions. crates can bring each other's names, so a cycle guard cuts the read, and the link step keeps no provision of a unit that a cut read gives, as the case `rust/glob-use-crate-cycle` shows. A local closure or a parameter can - bind a single name, so such a call keeps its record too. + bind a single name, so such a call keeps its record too. A glob `use` + brings only a `pub` item, as in the case `rust/glob-use-private-item`. + A glob `use` of the root of the file brings no name into an inline module + with uses of its own, as in the case `rust/glob-use-inline-scope`. - `#[macro_export]` puts a macro at the root of its crate, from any module of the crate. So `dep::wipe` names the macro `wipe` of a private module of the unit, as in the case `rust/macro-export-module`. A call of a diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 926ad787..3eed451d 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -163,6 +163,9 @@ type rustState struct { // memberAnswers holds the answers of rustGlobUnitMember that no cut // gave. memberAnswers map[[2]string]memberAnswer + // useScopes holds the classes of each file that hold a use, which are + // inline modules. + useScopes map[string]map[string]bool } // crateRoute is the path in another crate that a path of a file names, and @@ -204,6 +207,7 @@ func (b *builder) addRustUnits() { s.globPaths = map[[2]string]globPath{} s.members = map[[2]string]bool{} s.memberAnswers = map[[2]string]memberAnswer{} + s.useScopes = map[string]map[string]bool{} files := map[string][]string{} for _, p := range b.paths { if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust { @@ -572,15 +576,24 @@ type rustTarget struct { // rustPath resolves a path that a file writes: in the project, or else // through a name that a module binds to another crate. func (b *builder) rustPath(importer, written string) (rustTarget, bool) { + if t, ok := b.rustLocalPath(importer, written); ok { + return t, true + } + if path, file, ok := b.rustGlobPath(importer, written); ok { + return rustTarget{path: path, file: file}, true + } + return rustTarget{}, false +} + +// rustLocalPath resolves a path that a file writes as rustPath does, with +// no glob use of another crate. +func (b *builder) rustLocalPath(importer, written string) (rustTarget, bool) { if key, ok := b.resolveRust(importer, written); ok { return rustTarget{key: key}, true } if path, file, ok := b.rustCratePath(importer, written); ok { return rustTarget{path: path, file: file}, true } - if path, file, ok := b.rustGlobPath(importer, written); ok { - return rustTarget{path: path, file: file}, true - } return rustTarget{}, false } @@ -783,10 +796,15 @@ func (r rustResolver) FirstPartyFrom(importer, spec string) bool { // a local closure or a parameter can bind it. A single name that a glob use // of another crate or an exported macro of a dependency gives also gets the // edge to that item. -func (b *builder) rustScopeFallback(sc *scope, _, first string, rest []string, target string) []resolution { +func (b *builder) rustScopeFallback(sc *scope, caller, first string, rest []string, target string) []resolution { written := strings.Join(append([]string{first}, rest...), "::") record := resolution{fail: unknownReceiver(first)} - t, ok := b.rustPath(sc.ix.Path, written) + // A glob use of the file brings no name into an inline module, which + // reads its own uses. + t, ok := b.rustLocalPath(sc.ix.Path, written) + if !ok && !b.rustInUseScope(sc, caller) { + t, ok = b.rustPath(sc.ix.Path, written) + } switch { case ok && t.key != "": return []resolution{b.walkNamespace(t.key)} @@ -806,6 +824,30 @@ func (b *builder) rustScopeFallback(sc *scope, _, first string, rest []string, t return nil } +// rustInUseScope reports whether a key of a file sits in an inline module +// with a use of its own. The index keeps the uses of a file in one scope, +// and an impl block holds no use, so a class of the file that holds a use +// is an inline module. +func (b *builder) rustInUseScope(sc *scope, key string) bool { + s := b.rustState() + scopes, ok := s.useScopes[sc.ix.Path] + if !ok { + scopes = map[string]bool{} + for _, imp := range sc.imports { + if k := sc.enclosing(imp.Line); k != sc.ix.Path && b.kindOf(k) == NodeClass { + scopes[k] = true + } + } + s.useScopes[sc.ix.Path] = scopes + } + for k := fileindex.Parent(key); k != sc.ix.Path && fileindex.HasKeyPrefix(k, sc.ix.Path); k = fileindex.Parent(k) { + if scopes[k] { + return true + } + } + return false +} + // rustMacroCrate returns a dependency of the crate of a file whose unit // exports a macro of the name with #[macro_export], as dep for // render_now!() after #[macro_use] extern crate dep. The index does not @@ -876,6 +918,17 @@ func (b *builder) rustGlobPath(importer, written string) (string, string, bool) return g.join(written) } +// rustPublic reports whether a provision gives an item that a glob use of +// another crate brings: a module, a key of a third crate, or a pub item. A +// private item stays out, as fn render of dep::html for use dep::html::*. +func (b *builder) rustPublic(pv provision) bool { + if pv.module == "" { + return false + } + def := b.defs[pv.module] + return def == nil || def.Exported +} + // globPath is the path of the module of another crate that a glob use of // a file brings a name from, and the file that names the crate. type globPath struct { @@ -907,7 +960,7 @@ func (b *builder) findGlobPath(sc *scope, first string) globPath { } ix := b.byPath[file] o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, joinPath(path, []string{first})), form: modules.FormImport} - if slices.ContainsFunc(b.providers(o), func(pv provision) bool { return pv.module != "" }) { + if slices.ContainsFunc(b.providers(o), b.rustPublic) { return globPath{path: path, file: file, ok: true} } } diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/expect.yaml b/test/projectgraph/cases/rust/glob-use-inline-scope/expect.yaml new file mode 100644 index 00000000..ad3616db --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/expect.yaml @@ -0,0 +1,54 @@ +source: safedep:hand +description: >- + main.rs has use dep::html::* at its root, and the inline module inner has use other::*. A glob use of the root + brings no name into inner, so render() in inner::go runs render of other, which calls std::fs::remove_file. + A link step that reads the glob use of the root for inner gives the call the edge to dep//html//render, so + dep//html//render reads found from inner::go. The call keeps its record of render, so both targets read + uncertain from inner::go. +units: + vendor/dep: cargo:dep@1.0.0 + vendor/other: cargo:other@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//main + to: dep//html//page + kind: direct + confidence: high + site: src/main.rs:13 +no_edges: + - from: src/main.rs//inner//go + to: dep//html//render +unresolved: + - caller: src/main.rs//inner//go + site: src/main.rs:7 + reason: unknown-receiver + method: render + - caller: src/main.rs//main + site: src/main.rs:13 + reason: unknown-receiver + method: page +results: + - target: dep//html//render + status: uncertain + sources: [src/main.rs//inner//go] + sources_only: true + call_sites: + - caller: cargo:dep@1.0.0//src/html.rs//page + site: cargo:dep@1.0.0//src/html.rs:4 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:7 + - target: other//render + status: uncertain + sources: [src/main.rs//inner//go] + sources_only: true + uncertainties: + - kind: unresolved-call + site: src/main.rs:7 diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-inline-scope/project/Cargo.toml new file mode 100644 index 00000000..9fdab07a --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.0.0" +other = "1.0.0" diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-inline-scope/project/src/main.rs new file mode 100644 index 00000000..3f9d3111 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/project/src/main.rs @@ -0,0 +1,14 @@ +use dep::html::*; + +mod inner { + use other::*; + + pub fn go() { + render(); + } +} + +fn main() { + inner::go(); + page(); +} diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..537b4e47 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/src/html.rs b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/src/html.rs new file mode 100644 index 00000000..867bd09d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/src/html.rs @@ -0,0 +1,5 @@ +pub fn render() {} + +pub fn page() { + render(); +} diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..3d4613dd --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/dep/src/lib.rs @@ -0,0 +1 @@ +pub mod html; diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/other/Cargo.toml b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/other/Cargo.toml new file mode 100644 index 00000000..bdb8f41b --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/other/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "other" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/other/src/lib.rs b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/other/src/lib.rs new file mode 100644 index 00000000..df4d9c96 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/project/vendor/other/src/lib.rs @@ -0,0 +1,3 @@ +pub fn render() { + let _ = std::fs::remove_file("a"); +} diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/glob-use-inline-scope/summaries/cargo_dep_1.0.0.json new file mode 100644 index 00000000..7b8cd127 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,23 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//html//page", + "definitions": [ + "cargo:dep@1.0.0//src/html.rs//page" + ], + "reaches": [] + }, + { + "key": "dep//html//render", + "definitions": [ + "cargo:dep@1.0.0//src/html.rs//render" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/glob-use-inline-scope/summaries/cargo_other_1.0.0.json b/test/projectgraph/cases/rust/glob-use-inline-scope/summaries/cargo_other_1.0.0.json new file mode 100644 index 00000000..42a951d8 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-inline-scope/summaries/cargo_other_1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:other@1.0.0", + "symbols": [ + { + "key": "other//render", + "definitions": [ + "cargo:other@1.0.0//src/lib.rs//render" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/glob-use-private-item/expect.yaml b/test/projectgraph/cases/rust/glob-use-private-item/expect.yaml new file mode 100644 index 00000000..5cce1f4e --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/expect.yaml @@ -0,0 +1,60 @@ +source: safedep:hand +description: >- + main.rs has use dep::html::* and use other::*, and calls render() and util::run(). dep::html defines render + and the module util, both private, so the glob use of dep::html brings neither. other gives the pub function + render and the pub module util. So both calls run code of other: render calls std::fs::remove_file and + util::run calls std::fs::remove_dir. A link step that takes a private item of a unit for a name of a glob use + sends render() to dep//html//render and util::run() to dep//html//util//run, so std::fs::remove_dir reads + not-found. render() keeps its record, because a closure or a parameter can bind a single name. +units: + vendor/dep: cargo:dep@1.0.0 + vendor/other: cargo:other@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//main + to: other//render + kind: direct + confidence: high + site: src/main.rs:5 + - from: src/main.rs//main + to: other//util//run + kind: direct + confidence: high + site: src/main.rs:6 +no_edges: + - from: src/main.rs//main + to: dep//html//render + - from: src/main.rs//main + to: dep//html//util//run +unresolved: + - caller: src/main.rs//main + site: src/main.rs:5 + reason: unknown-receiver + method: render +results: + - target: std//fs//remove_dir + status: found + path: + - key: src/main.rs//main + - key: other//util//run + site: src/main.rs:6 + kind: direct + confidence: high + - key: cargo:other@1.0.0//src/util.rs//run + site: cargo:other@1.0.0//src/util.rs:1 + kind: package + confidence: high + - key: std//fs//remove_dir + site: cargo:other@1.0.0//src/util.rs:2 + kind: direct + confidence: high + call_sites: + - caller: cargo:other@1.0.0//src/util.rs//run + site: cargo:other@1.0.0//src/util.rs:2 + reached: true diff --git a/test/projectgraph/cases/rust/glob-use-private-item/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-private-item/project/Cargo.toml new file mode 100644 index 00000000..9fdab07a --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.0.0" +other = "1.0.0" diff --git a/test/projectgraph/cases/rust/glob-use-private-item/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-private-item/project/src/main.rs new file mode 100644 index 00000000..65967194 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/project/src/main.rs @@ -0,0 +1,7 @@ +use dep::html::*; +use other::*; + +fn main() { + render(); + util::run(); +} diff --git a/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..537b4e47 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/src/html.rs b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/src/html.rs new file mode 100644 index 00000000..7f5fde2d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/src/html.rs @@ -0,0 +1,11 @@ +mod util { + pub fn run() {} +} + +fn render() { + util::run(); +} + +pub fn page() { + render(); +} diff --git a/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..3d4613dd --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/dep/src/lib.rs @@ -0,0 +1 @@ +pub mod html; diff --git a/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/Cargo.toml b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/Cargo.toml new file mode 100644 index 00000000..bdb8f41b --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "other" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/src/lib.rs b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/src/lib.rs new file mode 100644 index 00000000..37d3042b --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/src/lib.rs @@ -0,0 +1,5 @@ +pub mod util; + +pub fn render() { + let _ = std::fs::remove_file("a"); +} diff --git a/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/src/util.rs b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/src/util.rs new file mode 100644 index 00000000..3b282c10 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/project/vendor/other/src/util.rs @@ -0,0 +1,3 @@ +pub fn run() { + let _ = std::fs::remove_dir("a"); +} diff --git a/test/projectgraph/cases/rust/glob-use-private-item/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/glob-use-private-item/summaries/cargo_dep_1.0.0.json new file mode 100644 index 00000000..8ecf12d4 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,23 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//html//page", + "definitions": [ + "cargo:dep@1.0.0//src/html.rs//page" + ], + "reaches": [] + }, + { + "key": "dep//html//util//run", + "definitions": [ + "cargo:dep@1.0.0//src/html.rs//util//run" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/glob-use-private-item/summaries/cargo_other_1.0.0.json b/test/projectgraph/cases/rust/glob-use-private-item/summaries/cargo_other_1.0.0.json new file mode 100644 index 00000000..5992e56d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-private-item/summaries/cargo_other_1.0.0.json @@ -0,0 +1,23 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:other@1.0.0", + "symbols": [ + { + "key": "other//render", + "definitions": [ + "cargo:other@1.0.0//src/lib.rs//render" + ], + "reaches": [] + }, + { + "key": "other//util//run", + "definitions": [ + "cargo:other@1.0.0//src/util.rs//run" + ], + "reaches": [] + } + ], + "targets": [] +} From 302193b6bb40d8432485a8a482c962feb0950c43 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:38:46 +0000 Subject: [PATCH 41/86] fix(projectgraph): let a Rust macro of the crate shadow a macro of a dependency A call of a single name that no item binds got the edge to a macro that a dependency exports. A macro_rules! of the crate, as log in a module with #[macro_use], shadows that macro, and the call then ran the local macro while the code of the dependency read found. The index does not tell a macro from a function, so a definition of the name in the space of the file now keeps the call a record alone. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/rust.go | 27 +++++++++++++ .../expect.yaml | 40 +++++++++++++++++++ .../project/Cargo.toml | 7 ++++ .../project/src/job.rs | 3 ++ .../project/src/macros.rs | 5 +++ .../project/src/main.rs | 13 ++++++ .../project/vendor/dep/Cargo.toml | 4 ++ .../project/vendor/dep/src/lib.rs | 10 +++++ .../summaries/cargo_dep_1.0.0.json | 16 ++++++++ 9 files changed, 125 insertions(+) create mode 100644 test/projectgraph/cases/rust/macro-local-shadows-dependency/expect.yaml create mode 100644 test/projectgraph/cases/rust/macro-local-shadows-dependency/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/job.rs create mode 100644 test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/macros.rs create mode 100644 test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/macro-local-shadows-dependency/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-local-shadows-dependency/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/macro-local-shadows-dependency/summaries/cargo_dep_1.0.0.json diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 3eed451d..1dc200c6 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -166,6 +166,8 @@ type rustState struct { // useScopes holds the classes of each file that hold a use, which are // inline modules. useScopes map[string]map[string]bool + // spaceNames holds the names of the definitions of each space. + spaceNames map[*space]map[string]bool } // crateRoute is the path in another crate that a path of a file names, and @@ -208,6 +210,7 @@ func (b *builder) addRustUnits() { s.members = map[[2]string]bool{} s.memberAnswers = map[[2]string]memberAnswer{} s.useScopes = map[string]map[string]bool{} + s.spaceNames = map[*space]map[string]bool{} files := map[string][]string{} for _, p := range b.paths { if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust { @@ -824,6 +827,24 @@ func (b *builder) rustScopeFallback(sc *scope, caller, first string, rest []stri return nil } +// rustSpaceNames returns the last name of each definition of a space. +func (b *builder) rustSpaceNames(sp *space) map[string]bool { + s := b.rustState() + if names, ok := s.spaceNames[sp]; ok { + return names + } + names := map[string]bool{} + for _, p := range b.paths { + if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust && b.spaceOf(p) == sp { + for _, def := range ix.Definitions { + names[def.Name] = true + } + } + } + s.spaceNames[sp] = names + return names +} + // rustInUseScope reports whether a key of a file sits in an inline module // with a use of its own. The index keeps the uses of a file in one scope, // and an impl block holds no use, so a class of the file that holds a use @@ -854,6 +875,12 @@ func (b *builder) rustInUseScope(sc *scope, key string) bool { // tell a call of a macro from a call of a function, so the caller keeps // the record of the name too. func (b *builder) rustMacroCrate(ix *fileindex.Index, name string) (string, bool) { + // A macro of the crate of the file shadows a macro of a dependency. + // The index does not tell a macro from a function, so a definition of + // the name in the space of the file counts. + if b.rustSpaceNames(b.spaceOf(ix.Path))[name] { + return "", false + } exported := false for _, macros := range b.rustMacros() { _, ok := macros[name] diff --git a/test/projectgraph/cases/rust/macro-local-shadows-dependency/expect.yaml b/test/projectgraph/cases/rust/macro-local-shadows-dependency/expect.yaml new file mode 100644 index 00000000..d6172ab1 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-local-shadows-dependency/expect.yaml @@ -0,0 +1,40 @@ +source: safedep:hand +description: >- + main.rs has #[macro_use] extern crate dep and #[macro_use] mod macros. The crate dep exports a macro log with + #[macro_export], and src/macros.rs defines its own macro_rules! log, which comes later in the text and so + shadows the macro of dep. log!() in src/job.rs runs the local macro, which calls crate::flush. A link step + that gives the call the edge to the macro of dep reads dep//sink as found. The call keeps its record alone, so + dep//sink reads uncertain. +units: + vendor/dep: cargo:dep@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/job.rs//go + to: src/macros.rs//log + kind: name + confidence: low + site: src/job.rs:2 +no_edges: + - from: src/job.rs//go + to: dep//log +unresolved: + - caller: src/job.rs//go + site: src/job.rs:2 + reason: unknown-receiver + method: log +results: + - target: dep//sink + status: uncertain + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//log + site: cargo:dep@1.0.0//src/lib.rs:4 + reached: false + uncertainties: + - kind: unresolved-call + site: src/job.rs:2 diff --git a/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/Cargo.toml b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/Cargo.toml new file mode 100644 index 00000000..c76f4fc8 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2018" + +[dependencies] +dep = "1.0.0" diff --git a/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/job.rs b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/job.rs new file mode 100644 index 00000000..1b8fe745 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/job.rs @@ -0,0 +1,3 @@ +pub fn go() { + log!(); +} diff --git a/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/macros.rs b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/macros.rs new file mode 100644 index 00000000..374dbd39 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/macros.rs @@ -0,0 +1,5 @@ +macro_rules! log { + () => { + $crate::flush() + }; +} diff --git a/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/main.rs b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/main.rs new file mode 100644 index 00000000..54d47873 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/src/main.rs @@ -0,0 +1,13 @@ +#[macro_use] +extern crate dep; +#[macro_use] +mod macros; +mod job; + +fn main() { + job::go(); +} + +pub fn flush() { + let _ = std::fs::remove_dir("a"); +} diff --git a/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..fb6107f0 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.0.0" +edition = "2018" diff --git a/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..bb64c2eb --- /dev/null +++ b/test/projectgraph/cases/rust/macro-local-shadows-dependency/project/vendor/dep/src/lib.rs @@ -0,0 +1,10 @@ +#[macro_export] +macro_rules! log { + () => { + $crate::sink() + }; +} + +pub fn sink() { + let _ = std::fs::remove_file("a"); +} diff --git a/test/projectgraph/cases/rust/macro-local-shadows-dependency/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/macro-local-shadows-dependency/summaries/cargo_dep_1.0.0.json new file mode 100644 index 00000000..2885322b --- /dev/null +++ b/test/projectgraph/cases/rust/macro-local-shadows-dependency/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//sink", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//sink" + ], + "reaches": [] + } + ], + "targets": [] +} From 932854dcee00974096bbcca7ddfce372f75657c2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:38:47 +0000 Subject: [PATCH 42/86] fix(indexer): read two Rust lets that call one function of a type as one type rustRebound keeps a call after a second let when both lets hold one type, and it knew the result of a function only in the file of the call. So t.run() after let t = A::new() twice, with A of another file, became a record, and the run of another type read found through the low name edges. Two lets whose values call the same function of the same type now hold one type. A trait of std that gives an inferred type, as Default, names no type. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/rust.go | 22 ++++++++++++- .../rust/let-same-constructor/expect.yaml | 31 +++++++++++++++++++ .../let-same-constructor/project/Cargo.toml | 4 +++ .../let-same-constructor/project/src/main.rs | 15 +++++++++ .../let-same-constructor/project/src/types.rs | 22 +++++++++++++ 5 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 test/projectgraph/cases/rust/let-same-constructor/expect.yaml create mode 100644 test/projectgraph/cases/rust/let-same-constructor/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/let-same-constructor/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/let-same-constructor/project/src/types.rs diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index c9326bf4..3c4f52b6 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -634,12 +634,32 @@ func (p *projection) rustValueType(binders map[string][]*sitter.Node, value *sit return "" } if fn != nil && fn.Type() == "scoped_identifier" { - return p.rustResultOf(p.text(fn.ChildByFieldName("path")), p.text(fn.ChildByFieldName("name"))) + path := p.text(fn.ChildByFieldName("path")) + if typ := p.rustResultOf(path, p.text(fn.ChildByFieldName("name"))); typ != "" { + return typ + } + return rustConstructorPath(path, p.text(fn)) } } return "" } +// rustGenericTraits are the traits of std whose associated function gives +// whatever type the caller infers, as Default::default(). +var rustGenericTraits = map[string]bool{"Default": true, "From": true, "Into": true, "FromStr": true, "TryFrom": true, "FromIterator": true} + +// rustConstructorPath returns a function of a type as a type of its own, +// as A::new for let t = A::new() when A is a type of another file. Two lets +// whose values call the same function of the same type hold one type. A +// trait of std that gives an inferred type names no type. +func rustConstructorPath(typ, fn string) string { + last := rustLastName(typ) + if last == "" || !unicode.IsUpper(rune(last[0])) || rustGenericTraits[last] { + return "" + } + return fn + "()" +} + // rustResultOf returns the result type of a function of a type of the // file, with no Option or Result around it. func (p *projection) rustResultOf(typ, name string) string { diff --git a/test/projectgraph/cases/rust/let-same-constructor/expect.yaml b/test/projectgraph/cases/rust/let-same-constructor/expect.yaml new file mode 100644 index 00000000..83153a2d --- /dev/null +++ b/test/projectgraph/cases/rust/let-same-constructor/expect.yaml @@ -0,0 +1,31 @@ +source: safedep:hand +description: >- + go binds t to A::new(), drops it, binds t again to A::new() and calls t.run(). A and B are types of + src/types.rs. Both lets call the same function of the same type, so t holds an A at the call, and the call + keeps the target A::run of the call graph. An index that knows the result of a function only in the file of + the call makes the call a record, and B::run, whose std::fs::remove_dir nothing calls, reads found through the + low name edges of run. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//go + to: src/types.rs//A//run + kind: member + confidence: high + site: src/main.rs:9 +no_edges: + - from: src/main.rs//go + to: src/types.rs//B//run +unresolved: [] +results: + - target: std//fs//remove_dir + status: not-found + call_sites: + - caller: src/types.rs//B//run + site: src/types.rs:20 + reached: false diff --git a/test/projectgraph/cases/rust/let-same-constructor/project/Cargo.toml b/test/projectgraph/cases/rust/let-same-constructor/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/let-same-constructor/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/let-same-constructor/project/src/main.rs b/test/projectgraph/cases/rust/let-same-constructor/project/src/main.rs new file mode 100644 index 00000000..d7982161 --- /dev/null +++ b/test/projectgraph/cases/rust/let-same-constructor/project/src/main.rs @@ -0,0 +1,15 @@ +mod types; + +use types::A; + +fn go() { + let t = A::new(); + drop(t); + let t = A::new(); + t.run(); +} + +fn main() { + go(); + let _ = types::B::new(); +} diff --git a/test/projectgraph/cases/rust/let-same-constructor/project/src/types.rs b/test/projectgraph/cases/rust/let-same-constructor/project/src/types.rs new file mode 100644 index 00000000..2f48e0ea --- /dev/null +++ b/test/projectgraph/cases/rust/let-same-constructor/project/src/types.rs @@ -0,0 +1,22 @@ +pub struct A; +pub struct B; + +impl A { + pub fn new() -> A { + A + } + + pub fn run(&self) { + let _ = std::fs::remove_file("a"); + } +} + +impl B { + pub fn new() -> B { + B + } + + pub fn run(&self) { + let _ = std::fs::remove_dir("b"); + } +} From 013d6834ba314c224cad3a3c66273f05042c6067 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:38:47 +0000 Subject: [PATCH 43/86] fix(callgraph): record a Rust macro rule that calls a metavariable $f() in a rule of a macro calls the function that a call of the macro passes. The tokens do not name it, and the call graph read no call there, so the function read not-found. Such a call is now a record with the reason computed-name. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/callgraph/rust.go | 12 ++++++- .../rust/macro-metavariable-call/expect.yaml | 33 +++++++++++++++++++ .../project/Cargo.toml | 4 +++ .../project/src/main.rs | 13 ++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 test/projectgraph/cases/rust/macro-metavariable-call/expect.yaml create mode 100644 test/projectgraph/cases/rust/macro-metavariable-call/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-metavariable-call/project/src/main.rs diff --git a/plugin/callgraph/rust.go b/plugin/callgraph/rust.go index c2dfb3f5..24fd282e 100644 --- a/plugin/callgraph/rust.go +++ b/plugin/callgraph/rust.go @@ -83,7 +83,17 @@ func rustTokenTreeProcessor(node *sitter.Node, treeData []byte, currentNamespace continue } rustTokenTreeProcessor(token, treeData, currentNamespace, callGraph, metadata) - if i == 0 || tokens[i-1].Type() != "identifier" { + if i == 0 { + continue + } + // A metavariable before a group in parentheses, as $f() in a rule + // of a macro, calls whatever function the call of the macro + // passes. The tokens do not name it, so the call is a record. + if prev := tokens[i-1]; prev.Type() == "metavariable" && !rustPathToken(prev, treeData) && strings.HasPrefix(token.Content(treeData), "(") { + callGraph.addRecord(currentNamespace, prev, "", "", treeData, UnresolvedComputedName) + continue + } + if tokens[i-1].Type() != "identifier" { continue } creation := strings.HasPrefix(token.Content(treeData), "{") diff --git a/test/projectgraph/cases/rust/macro-metavariable-call/expect.yaml b/test/projectgraph/cases/rust/macro-metavariable-call/expect.yaml new file mode 100644 index 00000000..601e4fcd --- /dev/null +++ b/test/projectgraph/cases/rust/macro-metavariable-call/expect.yaml @@ -0,0 +1,33 @@ +source: safedep:hand +description: >- + The rule of the macro call has $f(), which calls the function that a call of the macro passes. main calls + call!(target), so target runs and calls std::fs::remove_dir. The tokens of the rule do not name the function, + and a macro that reads $f() as no call gives no edge and no record, so std::fs::remove_dir reads not-found. + The call is a record with the reason computed-name, so the target reads uncertain. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//main + to: src/main.rs//call + kind: direct + confidence: high + site: src/main.rs:12 +unresolved: + - caller: src/main.rs//call + site: src/main.rs:3 + reason: computed-name +results: + - target: std//fs//remove_dir + status: uncertain + call_sites: + - caller: src/main.rs//target + site: src/main.rs:8 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:3 diff --git a/test/projectgraph/cases/rust/macro-metavariable-call/project/Cargo.toml b/test/projectgraph/cases/rust/macro-metavariable-call/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/macro-metavariable-call/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/macro-metavariable-call/project/src/main.rs b/test/projectgraph/cases/rust/macro-metavariable-call/project/src/main.rs new file mode 100644 index 00000000..85ffcc10 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-metavariable-call/project/src/main.rs @@ -0,0 +1,13 @@ +macro_rules! call { + ($f:ident) => { + $f() + }; +} + +fn target() { + let _ = std::fs::remove_dir("a"); +} + +fn main() { + call!(target); +} From 9ea9225cbc69a69e3ec093051e3223cde3a357ad Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:38:57 +0000 Subject: [PATCH 44/86] docs(projectgraph): describe the Rust macro and let rules of the second review Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 31df3d36..8479bb13 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -433,7 +433,9 @@ Rust follows the same rules, with these additions. with another type is a record too, as in the case `rust/shadowed-let-type`. A `let` counts once its statement ends, and `let`s of one type keep the target, as `c.send()` after - `let c = c.with_retry();` in the case `rust/let-same-type`. A method of the + `let c = c.with_retry();` in the case `rust/let-same-type`. Two `let`s + that call one function of one type hold one type, also when the type is + in another file, as in the case `rust/let-same-constructor`. A method of the real type then reads as `uncertain` and not as `not-found`. - The link step resolves a type that a parameter, a `let`, a field or the result of a function declares at the root of the file, or as a struct, @@ -537,8 +539,13 @@ Rust follows the same rules, with these additions. `#[macro_use] extern crate dep;`, gets the edge to such a macro of a dependency of the crate, as in the case `rust/macro-import`. The index does not tell a call of a macro from a call of a function value, so the - call keeps its record. A macro reads `$crate::sink` as `crate::sink` of - the crate of the macro. + call keeps its record. A definition of the name in the crate of the file, + as a `macro_rules!` of a module with `#[macro_use]`, shadows such a + macro, so the call keeps its record alone, as in the case + `rust/macro-local-shadows-dependency`. A macro reads `$crate::sink` as + `crate::sink` of the crate of the macro. A call of a metavariable, as + `$f()`, is a record with the reason `computed-name`, as in the case + `rust/macro-metavariable-call`. - The index records a type alias, as `pub type Image

= ImageBuffer

;`, as a definition of the kind `alias`. The link step resolves the type that it names, as the file of the alias names it, when a walk first From 28cc26bd5b3e490e8414b584dd97e57e28157e64 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:52:52 +0000 Subject: [PATCH 45/86] fix(projectgraph): keep no Rust glob list that a cut of an outer lookup gave A read of the glob uses of a scope under the guard of a glob path or of a glob member could get no module from a path that the guard cut, and the list of that read stayed for the build without the module. The guards now give the depth of each lookup that they cut, and a read keeps its list only when each cut hit a lookup that the read began, as the low link of Tarjan tells. A glob member keeps its answer for the build by the same rule, and gives the lookup in progress its first answer when it reads the module again. A check of any glob lookup in progress made the link step of the Rust corpus a third slower. The depth check costs nothing on it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/resolve.go | 14 ++++-- projectgraph/rust.go | 96 ++++++++++++++++++++++++++++++--------- projectgraph/rust_test.go | 58 ++++++++++++++++------- 3 files changed, 127 insertions(+), 41 deletions(-) diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 83257c74..bf060c02 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -563,6 +563,7 @@ func (b *builder) wildcardModules(sc *scope) []string { // A read inside another read adds the paths that it asks for to the // record of the outer one. outer := b.wildcardMods != nil && b.asked == nil + whole := b.globRead() if outer { b.asked = map[[2]string]bool{} } @@ -589,8 +590,9 @@ func (b *builder) wildcardModules(sc *scope) []string { clear(b.tentative) b.tentative = b.tentative[:0] } + readWhole := whole() if outer { - if b.uncut(b.asked) { + if readWhole && b.uncut(b.asked) { b.wildcardMods[sc] = &wildcardMods{mods: r.mods, asked: b.asked} } b.asked = nil @@ -622,15 +624,19 @@ func (b *builder) undo(mark int) { // path in progress was set before the read, and the guard cut the read if // the read asked for it. // -// The guards of a Rust glob path and of a member of a glob module cut a -// read too, so a read under them is no whole read. +// The guards of a Rust glob path and of a glob member tell globRead the +// depth of each read that they cut. A read with a cut of a lookup that +// began before the read is no whole read, so wildcardModules keeps no list +// of it. A cut of a lookup that the read began leaves out no path. A check +// of any glob lookup in progress kept no list under a lookup, and made the +// link step of the Rust corpus a third slower. // // uncut compares the paths of the resolving guard only. On the Rust // corpus, the reads under any other guard cost less than 3% of the read // time. So any other state in progress fails the check. func (b *builder) uncut(asked map[[2]string]bool) bool { s := b.rustState() - if len(b.expanding)+len(b.visiting)+len(b.reading)+len(b.stepping)+len(b.findingBases)+len(s.routing)+len(s.globbing)+len(s.members)+b.ordering > 0 { + if len(b.expanding)+len(b.visiting)+len(b.reading)+len(b.stepping)+len(b.findingBases)+len(s.routing)+b.ordering > 0 { return false } for k := range s.resolving { diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 1dc200c6..3b1c0e61 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -2,6 +2,7 @@ package projectgraph import ( "cmp" + "math" "slices" "strings" @@ -157,17 +158,26 @@ type rustState struct { globs map[string][]string macros map[*space]map[string]string nested map[string]bool - globbing map[[2]string]bool globPaths map[[2]string]globPath - members map[[2]string]bool - // memberAnswers holds the answers of rustGlobUnitMember that no cut - // gave. + // globbing and members hold the depth of each glob path and glob + // member that a lookup reads now. depth is the depth of the last one, + // and low the least depth that a cut of the read in progress hit. + globbing map[[2]string]int + members map[[2]string]int + depth int + low int + // memberAnswers holds the answers of rustGlobUnitMember that no cut of + // an outer read gave, and visited the other answers of the lookup in + // progress. memberAnswers map[[2]string]memberAnswer + visited map[[2]string]memberAnswer // useScopes holds the classes of each file that hold a use, which are // inline modules. useScopes map[string]map[string]bool // spaceNames holds the names of the definitions of each space. spaceNames map[*space]map[string]bool + // cuts counts the cuts of the glob guards, a part of b.cuts. + cuts int } // crateRoute is the path in another crate that a path of a file names, and @@ -205,9 +215,11 @@ func (b *builder) addRustUnits() { s.routing = map[[2]string]bool{} s.routes = map[[2]string]crateRoute{} s.globs = map[string][]string{} - s.globbing = map[[2]string]bool{} + s.globbing = map[[2]string]int{} + s.low = math.MaxInt + s.visited = map[[2]string]memberAnswer{} s.globPaths = map[[2]string]globPath{} - s.members = map[[2]string]bool{} + s.members = map[[2]string]int{} s.memberAnswers = map[[2]string]memberAnswer{} s.useScopes = map[string]map[string]bool{} s.spaceNames = map[*space]map[string]bool{} @@ -929,14 +941,16 @@ func (b *builder) rustGlobPath(importer, written string) (string, string, bool) if g, ok := s.globPaths[k]; ok { return g.join(written) } - if s.globbing[k] { - b.cuts++ + if d, ok := s.globbing[k]; ok { + b.globCut(d) return "", "", false } - s.globbing[k] = true + s.depth++ + s.globbing[k] = s.depth cuts := b.cuts g := b.findGlobPath(sc, first) delete(s.globbing, k) + s.depth-- // The answer reads providers, so it follows the rule of that memo. if b.cuts == cuts { s.globPaths[k] = g @@ -1044,37 +1058,77 @@ func (b *builder) rustGlobMember(sc *scope, name string) (string, bool) { // // Modules can bring each other with glob uses, in a diamond or in a cycle. // A walk of each path through them grows with the number of paths. So one -// lookup reads each module once: a module that the lookup reads again -// counts as a cut, and the lookup keeps no answer that a cut gave. An -// answer that no cut gave stays for the build, so a diamond reads each -// module once in all. +// lookup reads each module once, and a module that it reads again gives +// its first answer. A read of a module that the lookup reads now is a cut. +// An answer stays for the build when each cut under it hit the module or a +// module under it, as the low link of Tarjan tells. Such a cut leaves out +// no path, because the module in progress reads that path itself. func (b *builder) rustGlobUnitMember(mod, name string) (string, bool) { s := b.rustState() k := [2]string{mod, name} if m, ok := s.memberAnswers[k]; ok { return m.key, m.ok } - if s.members[k] { - b.cuts++ + if d, ok := s.members[k]; ok { + b.globCut(d) return "", false } + if m, ok := s.visited[k]; ok { + b.globCut(m.low) + return m.key, m.ok + } if len(s.members) == 0 { - defer clear(s.members) + defer clear(s.visited) } - s.members[k] = true - cuts := b.cuts + s.depth++ + d := s.depth + s.members[k] = d + low, cuts := s.low, b.cuts-s.cuts + s.low = math.MaxInt key, ok := b.unitMember(mod, name) - if b.cuts == cuts { - s.memberAnswers[k] = memberAnswer{key: key, ok: ok} + m := memberAnswer{key: key, ok: ok, low: s.low} + s.low = min(low, s.low) + delete(s.members, k) + s.depth-- + if m.low >= d && b.cuts-s.cuts == cuts { + s.memberAnswers[k] = m b.memo(func() { delete(s.memberAnswers, k) }) + } else { + s.visited[k] = m } return key, ok } -// memberAnswer is the answer of rustGlobUnitMember for a module and a name. +// globCut counts a read that the guard of a glob path or of a glob member +// cuts at the depth. +func (b *builder) globCut(depth int) { + s := b.rustState() + b.cuts++ + s.cuts++ + s.low = min(s.low, depth) +} + +// globRead starts a read that the cuts of the glob guards can make +// incomplete. The function that it returns ends the read and reports +// whether no cut of the read hit a module that a lookup read before the +// read began. +func (b *builder) globRead() func() bool { + s := b.rustState() + depth, low := s.depth, s.low + s.low = math.MaxInt + return func() bool { + whole := s.low > depth + s.low = min(low, s.low) + return whole + } +} + +// memberAnswer is the answer of rustGlobUnitMember for a module and a name, +// with the least depth that a cut under it hit. type memberAnswer struct { key string ok bool + low int } // rustUnitFile reports whether a file is Rust code of a unit of dependency diff --git a/projectgraph/rust_test.go b/projectgraph/rust_test.go index 69c4b26c..dd97e334 100644 --- a/projectgraph/rust_test.go +++ b/projectgraph/rust_test.go @@ -2,9 +2,15 @@ package projectgraph import ( "context" + "os" + "path/filepath" "testing" "testing/fstest" + codefs "github.com/safedep/code/fs" + "github.com/safedep/code/lang" + "github.com/safedep/code/plugin/indexer" + "github.com/safedep/code/fileindex" "github.com/safedep/code/langcode" "github.com/stretchr/testify/assert" @@ -104,21 +110,41 @@ func TestRustHelperProgram(t *testing.T) { } } -// TestUncutCountsTheRustGlobGuards checks that a read of the wildcard -// imports of a scope under the guard of a Rust glob path or of a glob -// member is no whole read. Such a guard cuts the read of a module, and a -// list of modules that a cut read gave stayed for the build. -func TestUncutCountsTheRustGlobGuards(t *testing.T) { - for name, set := range map[string]func(s *rustState){ - "globbing": func(s *rustState) { s.globbing = map[[2]string]bool{{"src/a.rs", "x"}: true} }, - "members": func(s *rustState) { s.members = map[[2]string]bool{{"rust:app::c", "x"}: true} }, - } { - t.Run(name, func(t *testing.T) { - b, err := newBuilder(nil, Options{}) - require.NoError(t, err) - require.True(t, b.uncut(nil)) - set(b.rustState()) - assert.False(t, b.uncut(nil)) - }) +// TestBuildRustKeepsNoGlobListThatACutGave links src/other.rs, which glob +// uses crate::sub::x and crate::c. sub gives x only through its own glob use +// of other, so the read of the glob uses of other asks for other again +// under the guard of a glob member. That read gets no module for +// crate::sub::x. A list of the modules of other that such a read gave +// stayed for the build without the module c::x, and a name of c::x read +// missing for every later lookup. +func TestBuildRustKeepsNoGlobListThatACutGave(t *testing.T) { + files := map[string]string{ + "Cargo.toml": "[package]\nname = \"app\"\nversion = \"1.0.0\"\nedition = \"2021\"\n", + "src/main.rs": "mod c;\nmod other;\nmod sub;\n\nuse sub::*;\n\nfn main() {\n x::g();\n other::run();\n}\n", + "src/c/mod.rs": "pub mod x;\n", + "src/c/x.rs": "pub fn g() {}\n", + "src/other.rs": "use crate::sub::x::*;\npub use crate::c::*;\n\npub fn run() {\n g();\n}\n", + "src/sub.rs": "pub use crate::other::*;\n", + } + dir := t.TempDir() + for p, text := range files { + require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(dir, p)), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, p), []byte(text), 0o644)) + } + languages, err := lang.AllLanguages() + require.NoError(t, err) + fsys, err := codefs.NewLocalFileSystem(codefs.LocalFileSystemConfig{AppDirectories: []string{dir}}) + require.NoError(t, err) + ix, err := indexer.New(indexer.Options{Languages: languages}) + require.NoError(t, err) + indexed, err := ix.Run(context.Background(), fsys) + require.NoError(t, err) + b, err := newBuilder(indexed.Indexes, Options{Root: os.DirFS(dir), Files: indexed.Statuses(), Languages: indexed.Languages}) + require.NoError(t, err) + require.NoError(t, b.linkAll(context.Background())) + + w := b.wildcardMods[b.scopes["src/other.rs"]] + if w != nil { + assert.Equal(t, []string{"rust:app::c::x", "rust:app::c"}, w.mods) } } From 376da5dc730311e913d33298ba8158272f64b0c9 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:54:32 +0000 Subject: [PATCH 46/86] fix(projectgraph): keep no external base that a cut search gave findExternalBase guards against a class whose base resolves through the class itself. A search that this guard cut can miss a base, and externalBase kept the answer for the build. The guard now counts in b.cuts, and externalBase keeps an answer only when no cut happened under it, as providers does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/cha.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/projectgraph/cha.go b/projectgraph/cha.go index 219483e8..45d5afe0 100644 --- a/projectgraph/cha.go +++ b/projectgraph/cha.go @@ -182,9 +182,14 @@ func (b *builder) externalBase(class string) (string, bool) { } base, ok := b.externalBases[class] if !ok { + // A search that the guard of an outer search cut can miss a base, + // so only an uncut answer stays for the build. + cuts := b.cuts base, _ = b.findExternalBase(class) - b.externalBases[class] = base - b.memo(func() { delete(b.externalBases, class) }) + if b.cuts == cuts { + b.externalBases[class] = base + b.memo(func() { delete(b.externalBases, class) }) + } } return base, base != "" } @@ -196,6 +201,7 @@ func (b *builder) externalBase(class string) (string, bool) { // that the search reads now has no external base, so the search ends. func (b *builder) findExternalBase(class string) (string, bool) { if b.findingBases[class] { + b.cuts++ return "", false } b.findingBases[class] = true From 7773305585b0444c200f182e609fb23036432ddb Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 19:55:38 +0000 Subject: [PATCH 47/86] chore(reach): bump reach to 6 and update the hash rows A markup record now concerns only the targets of the JavaScript family, so the reach rules change. The rows of fileindex 17 and projectgraph 20 exist only on this branch, so they change in place, and the row of reach 5 for that pair goes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/reach/reach.go | 2 +- test/projectgraph/version_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/projectgraph/reach/reach.go b/projectgraph/reach/reach.go index fff1a65f..850470a2 100644 --- a/projectgraph/reach/reach.go +++ b/projectgraph/reach/reach.go @@ -24,7 +24,7 @@ import ( // change. Version 2 added the root kind to the first hop of a path. // Version 3 matches a target against the external key of a definition of a // unit of dependency code. -const Version = "5" +const Version = "6" const ( defaultMaxDepth = 64 diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index e233cef5..360c9a02 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -34,7 +34,7 @@ var graphHashes = map[string]string{ "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", "16/18": "bb92620bf9903e8d19b52b62198a0bfd69d67856f9db226112b2cedf537b312b", "16/19": "51ba3e925c628602b3a53c1a6e68b379022a410bb5b749062b3045fef082ac8f", - "17/20": "5cc59915552880540c96950363befc72a16fb1ebb7a0830a523cd559dff097b7", + "17/20": "52b9259fdff358531e9a888ec28826f3ae43393d82d1c3eb947ab1d35b540ef9", } // resultHashes records the hash of the reach results of every case for @@ -53,7 +53,7 @@ var resultHashes = map[string]string{ "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", "16/18/5": "e0c6093df3a85353ee15c8a90ef7c2a441f23348ce80c9d49793c256bddeffc5", "16/19/5": "a935650f8e4d01c0abdceeefb3332bf779927693a1c48b28263da5365db752cd", - "17/20/5": "b5e6219c16179c37bb4e5d01c4dbe762a50fa6e2e5d087849d69cfd45ac65ba7", + "17/20/6": "b6a5dd163abb2704191cd194541aa67c17f41f22bb67e3a4414d9e5158258fe8", } // summaryHashes records the hash of the golden summaries of the cases for @@ -73,7 +73,7 @@ var summaryHashes = map[string]string{ "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", "16/18/3": "694efb5f72075356359031d449d7b4ecbdca2ddd5099dacbcd2adad745ac6e19", "16/19/3": "13c72bf9eb60fc0a38bc600906fc02028d5cb60cb2aefbec0f0721779c429d80", - "17/20/3": "56789fdacb1789e19480212dfc6d426dde138b39bc40ff3b9b9c71105989e0f4", + "17/20/3": "6b70d0caeec5a3a546bbc53d47a6beac138c77ab4532d8de033a004ae9c54424", } func TestGraphHashFollowsVersion(t *testing.T) { From e4170249e87ed1745d6a24c1c4b293c0afa19904 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 20:21:52 +0000 Subject: [PATCH 48/86] fix(golang): close the gaps of a third review of function values - A range over a member that a method of the graph names keeps a record with the method name, so a name edge leads to the method. A reference at the site that names data drops the record. - A receiver that binds no local at the call names the import, as url in url, err := url.Parse(raw), and the call keeps its target. - A local that shadows an import gets the target of the local in place of the target of the import. - A dereference of a type, as the conversion (*T)(p), gets no record. - A range over a slice of a value, an element of a map or a slice of a type of data, a value of a named type of data and the result of a function whose first result holds data needs no record. The index marks such types and functions with ValueData, and gives the link step a range-value record to decide. - The fields of an anonymous struct and of a struct type that a function declares get definitions, so a call of such a field in another file is a function-value record. - The field type of a parameter types an assignment only when the parameter is the binding in scope. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 67 +++-- fileindex/index.go | 17 +- plugin/indexer/golang.go | 16 +- plugin/indexer/govalues.go | 251 ++++++++++++++---- plugin/indexer/testdata/golden/app.go.json | 12 +- projectgraph/link.go | 18 +- projectgraph/resolve.go | 6 + projectgraph/values.go | 87 +++++- .../cases/go/range-over-func/expect.yaml | 72 ++++- .../cases/go/range-over-func/project/main.go | 21 ++ .../cases/go/range-over-func/project/types.go | 13 + .../cases/go/source-local-func/expect.yaml | 66 ++++- .../go/source-local-func/project/anon.go | 9 + .../go/source-local-func/project/conv.go | 17 ++ .../cases/go/typed-local-shadow/expect.yaml | 116 +++++++- .../go/typed-local-shadow/project/idiom.go | 31 +++ .../go/typed-local-shadow/project/main.go | 9 + 17 files changed, 717 insertions(+), 111 deletions(-) create mode 100644 test/projectgraph/cases/go/source-local-func/project/anon.go create mode 100644 test/projectgraph/cases/go/source-local-func/project/conv.go create mode 100644 test/projectgraph/cases/go/typed-local-shadow/project/idiom.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 8479bb13..f18ef214 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -359,15 +359,20 @@ block, gives the call the target of a typed local under an alias, as with the reason `unknown-receiver` and the method name, and the method of the real type gets a `name` edge. The index checks the first name of the receiver too, because the call graph can keep a raw target, as `x//run` for -a parameter `x` of a function literal. A short variable declaration that -assigns a name of its own block, as `err` in `n, err := f()` after -`m, err := g()`, binds no new name. +a parameter `x` of a function literal. A receiver that binds no local at +the call names the import, as `url` in `url, err := url.Parse(raw)`, whose +scope starts after the declaration, and the call keeps its target. A local +that shadows an import, as `log` in `for _, log := range ls { log.printf() +}`, gets the target of the local in place of the target of the import. A +short variable declaration that assigns a name of its own block, as `err` +in `n, err := f()` after `m, err := g()`, binds no new name. An assignment can type a variable that the function binds once, as `r` in `r := o.r; if r == nil { r = &remote{} }`. The link step reads that type only when it is the type of the variable. A declared type must be that type, and with no declared type each write must give it, as the field `r` -of the struct `opts` of the file does. A variable that can hold an +of the struct `opts` of the file does for a parameter `o opts`, and not for +a local that shadows the parameter. A variable that can hold an interface, as `r` of a field `r runner`, a range variable or `l := ls[0]`, can hold another type, so its call is a record. The case `go/typed-local-shadow` shows each form. @@ -1181,9 +1186,15 @@ its call keeps its records. The index also gives a `function-value` record to code that calls a function value with no call record of its own: a call of a field of an -anonymous struct, as `s.act()`, a call of a dereference or a receive, as -`(*p)()` or `(<-ch)()`, and a call of a local whose declared type is a -function type of another package, as `h` in `var h http.HandlerFunc`. A +anonymous struct, as `s.act()`, a call of a dereference of a value or a +receive, as `(*p)()` or `(<-ch)()`, and a call of a local whose declared +type is a function type of another package, as `h` in +`var h http.HandlerFunc`. A conversion, as `(*T)(p)`, dereferences a type +and calls nothing. The fields of an anonymous struct and of a struct type +that a function declares have no definition of their own type, so the index +gives each such field a definition under the key `struct` of its file, as +`anon.go//struct//exec`, and a call of such a field in another file is a +`function-value` record too. A record of a method value that a reference reads, as `g := h.fn`, has no receiver, so the link step does not take it for a call of a field. The case `go/source-local-func` shows each form. @@ -1192,27 +1203,43 @@ A range over a function calls it, as `seq(yield)` does. So a range over a value that the source does not show to be data gets a `function-value` record: a parameter of a function type of another file, as `seq` in `func consume(seq Seq) { for range seq {} }`, a local that a field gives, -an element of a slice, or the result of a call. A literal, a value of a -type of data and a local whose declaration gives such a value need no -record. A range over a member keeps the member name for the link step. The -link step drops the record when each field of the name in the graph holds -data, as `Attr []string`, or when a reference at the site names a function -or a method, as `t.All`. The index gives a range over a member no record -with the method name, because such a range mostly reads a slice or a map, -and a `name` edge to a method of the name would make a target behind it -`found`. The case `go/range-over-func` shows each form. - -Three gaps stay. In each of them, a search with a source can read +or an element of a slice of functions. A literal, a slice of a value, a +value of a type of data, an element of a map or a slice of a declared type +of data, and a local whose declaration gives such a value need no record. + +The index gives the link step what it needs to decide the other ranges, +with the reason `range-value`, and the link step never keeps that reason. + +- A member, as `n.Attr`. The record has no function-value record when a + reference at the site names a function or data, as `t.All` or `n.Attr` + of a typed `t` or `n`, or when each field of the name holds data and no + method has the name. A method of the name makes it a record with the + method name, as the range can call a method value, and a field of the + name that can hold a function makes it a `function-value` record. +- A value of a named type, as `l List` with `type List []string` in + another file. The index marks a named type whose values hold no function + with `ValueData`. +- The result of a call, as `names()` or `strings.Split(s, ",")`. The index + marks a function whose first result holds no function with `ValueData`. + Code outside the graph has its own record, which covers a function that + it makes. + +The case `go/range-over-func` shows each form. + +Two gaps stay. In each of them, a search with a source can read `not-found`. - A field of a type of a package that no unit gives can hold a function, and no definition names that field. So a call of it keeps only the record with the name. -- A field of an anonymous struct of another file can hold a function, and - no definition names it. - In Rust, a call of a parameter or a local keeps only the record with the name. +Some records cost precision only. A range over a value of a named type of a +package that no unit gives, as `http.Header`, or over an element of it, +keeps its record. So does a call of a local that a call into such a +package gives, as `cancel` of `context.WithTimeout`. + The rule does not apply to Rust. The Rust index records a function that an argument, a `let`, an assignment, a `return`, a field initializer or an array names. It does not record a function that the tail expression of a diff --git a/fileindex/index.go b/fileindex/index.go index d98b6c25..45175b41 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -119,11 +119,16 @@ const ( // UnresolvedFunctionValue is a call of a function value, as f(x)(y) or // handlers[i](c), in a language whose function values the link step // joins to the code that holds them. The link step gives the reason - // in place of computed-name, and the record names no target. The Go - // index gives it to a range over a member, with the member name in - // Method. The link step drops that record when each field of the name - // holds data, and clears the name otherwise. + // in place of computed-name, and the record names no target. UnresolvedFunctionValue UnresolvedReason = "function-value" + // UnresolvedRangeValue is a Go range over a value that the index cannot + // show to be data. A range over a function calls it. The link step + // makes the record a function-value record, or drops it when the graph + // shows that the value is data. Receiver and Method name a member, as + // n and Kids for n.Kids. Method alone names the type of the value, as + // List. With neither, the value is the result of the call at the site. + // The link step never keeps the reason. + UnresolvedRangeValue UnresolvedReason = "range-value" // UnresolvedMarkup is the markup of a component file, as the template // of a Vue component, which the index does not parse. The framework // runs the markup and calls the functions of the component, so the @@ -280,6 +285,10 @@ type Definition struct { Wraps string `json:"wraps,omitempty"` Static bool `json:"static,omitempty"` Data bool `json:"data,omitempty"` + // ValueData marks a Go function whose first result holds no function, + // and a Go named type whose values hold no function, as []string or a + // struct. A range over such a value reads data. + ValueData bool `json:"value_data,omitempty"` Flags []Flag `json:"flags,omitempty"` // Alias is the type that a DefAlias names, as written, with no type // arguments, as B for type A = B. diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 01b118f3..4dc07c66 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -108,6 +108,7 @@ func goDefinitions(p *projection) { for i, spec := range specs { p.goVariableValues(spec, vars[i]) } + p.goAnonFieldDefinitions() } // goTypes adds each named type of a type declaration. An interface is an @@ -127,7 +128,7 @@ func (p *projection) goTypes(statement *sitter.Node) { if t != nil && t.Type() == "interface_type" { kind = fileindex.DefInterface } - p.addDefinition(key, kind, spec) + p.addDefinition(key, kind, spec).ValueData = t != nil && kind == fileindex.DefClass && p.goDataField(t) p.addSyntax(key, spec) if t == nil || t.Type() != "struct_type" || spec.Type() != "type_spec" { continue @@ -404,6 +405,7 @@ func goSignature(p *projection, def *fileindex.Definition, node *sitter.Node) { var params []fileindex.Typed params, def.Arity, def.Variadic = p.goParams(node.ChildByFieldName("parameters"), p.goConstraints(node)) def.Params = append(def.Params, params...) + def.ValueData = p.goResultData(node.ChildByFieldName("result")) results := p.goResults(node.ChildByFieldName("result")) if len(results) > 0 { def.Returns = results[0] @@ -536,6 +538,18 @@ func (p *projection) goParams(list *sitter.Node, constraints map[string]string) return typed, arity, variadic } +// goResultData reports whether the first result of a function holds no +// function, as []string. +func (p *projection) goResultData(result *sitter.Node) bool { + if result != nil && result.Type() == "parameter_list" { + result = ts.ChildOfType(result, "parameter_declaration") + if result != nil { + result = result.ChildByFieldName("type") + } + } + return p.goDataField(result) +} + // goResults returns the type name of each result of a function, by // position. A result with no named type, as int or func(), gives "". func (p *projection) goResults(result *sitter.Node) []string { diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 92c9e0ec..b4082b45 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -330,6 +330,11 @@ func (p *projection) goFieldValueType(caller string, value *sitter.Node) (filein if i < 0 { return fileindex.Typed{}, false } + // A local can shadow the parameter, as o in o := other{} after + // func f(o opts), and hold a value of another type. + if binding, ok := p.goBinding(caller, p.text(operand), operand.StartByte()); !ok || binding.from != goParamOrigin(p.syntax[caller]) { + return fileindex.Typed{}, false + } t := p.goFileField(def.Params[i].Type, p.text(field)) if typ := p.goTypeName(t); typ != "" { return fileindex.Typed{Type: typ}, true @@ -430,29 +435,55 @@ func (p *projection) goTypedAt(caller, name string, at uint32) bool { // call. The call graph can keep a raw target for a name that a function // literal binds, as x//run for x.run() in go func(x B) { x.run() }(b), // and the link step then reads the type of the parameter x of the caller. -// When the binding in scope has a type of its own, as file in a second +// It can also bind a local that shadows an import to the import, as +// log//printf for log.printf() in for _, log := range ls. Such a call gets +// the target of the local, which the link step types or records. When the +// binding in scope has another type of its own, as file in a second // file, err := os.Create(p) of a sibling block, the call gets the target -// of that binding under its alias. +// of that binding under its alias. A receiver that binds no local at the +// call, as url in url, err := url.Parse(raw), names the import. func goTypedCall(p *projection, call *fileindex.Call) bool { at := p.byteAt(call.Line, call.Column) names := strings.Split(call.Receiver, ".") name := names[0] - if !isIdentifier(name) || p.goTypedAt(call.Caller, name, at) { - for _, target := range call.Targets { - if n, ok := p.typedName(call.Caller, target); ok && n != name && !p.goTypedAt(call.Caller, n, at) { - return false - } + if _, bound := p.goBinding(call.Caller, name, at); !isIdentifier(name) || !bound { + return p.goTypedTargets(call, at) + } + local := name + if p.goTypedAt(call.Caller, name, at) { + if !slices.ContainsFunc(call.Targets, func(t string) bool { return p.goImportTarget(name, t) }) { + return p.goTypedTargets(call, at) } - return true + } else { + alias, ok := p.goAlias(call.Caller, name, at) + if !ok { + return false + } + local = alias } - alias, ok := p.goAlias(call.Caller, name, at) - if !ok { - return false + call.Targets = []string{fileindex.Join(call.Caller, append(append([]string{local}, names[1:]...), call.Method)...)} + return true +} + +// goTypedTargets reports whether the type that the link step reads for +// each typed name that starts a target of a call holds at the call. +func (p *projection) goTypedTargets(call *fileindex.Call, at uint32) bool { + for _, target := range call.Targets { + if n, ok := p.typedName(call.Caller, target); ok && !p.goTypedAt(call.Caller, n, at) { + return false + } } - call.Targets = []string{fileindex.Join(call.Caller, append(append([]string{alias}, names[1:]...), call.Method)...)} return true } +// goImportTarget reports whether a target names a member of the import +// that binds the name. +func (p *projection) goImportTarget(name, target string) bool { + return slices.ContainsFunc(p.ix.Imports, func(imp fileindex.Import) bool { + return imp.Alias == name && imp.Namespace != "" && fileindex.HasKeyPrefix(target, imp.Namespace) + }) +} + // goAlias returns a name under which the link step reads the type of the // binding of a name in scope at the byte, when that binding has a type of // its own. The index adds a typed local of the alias to the caller once. @@ -502,14 +533,15 @@ func goValueVisitor(p *projection) visitor { if value == nil { return } - if method, ok := p.goRangeRecord(value); ok { - p.goValueRecord(value, method) + if r, ok := p.goRangeRecord(value, 0); ok { + p.goRangeValue(r) } case "call_expression": function := goUnparen(node.ChildByFieldName("function")) switch { case function == nil: - case function.Type() == "unary_expression" && (goOperator(function) == "*" || goOperator(function) == "<-"): + case function.Type() == "unary_expression" && goOperator(function) == "<-", + function.Type() == "unary_expression" && goOperator(function) == "*" && p.goValueOperand(function.ChildByFieldName("operand")): p.goValueRecord(node, "") case function.Type() == "selector_expression" && p.goAnonFields()[p.text(function.ChildByFieldName("field"))]: p.goValueRecord(node, "") @@ -518,54 +550,137 @@ func goValueVisitor(p *projection) visitor { } } +// goValueOperand reports whether the operand of a dereference in the +// function of a call is a value, as p in (*p)(), and no type, as T in the +// conversion (*T)(v) or reflect.SliceHeader in (*reflect.SliceHeader)(v). +// A value is a local, a variable of the file, or any expression other +// than a name or a member of an import. +func (p *projection) goValueOperand(operand *sitter.Node) bool { + operand = goUnparen(operand) + if operand == nil { + return false + } + switch operand.Type() { + case "identifier", "type_identifier": + name := p.text(operand) + if _, ok := p.goBinding(p.innermost(operand.StartByte()), name, operand.StartByte()); ok { + return true + } + def := p.defs[p.local(name)] + return def != nil && def.Kind == fileindex.DefVariable + case "selector_expression", "qualified_type": + object := operand.ChildByFieldName("operand") + if object == nil { + object = operand.ChildByFieldName("package") + } + return object == nil || p.goImportPath(p.text(object)) == "" + } + return !strings.HasSuffix(operand.Type(), "_type") +} + // goValueRecord adds the record of a call of a function value at the node. -// A record with a member name is a candidate that the link step checks. func (p *projection) goValueRecord(node *sitter.Node, member string) { - site := ast.GetNodePosition(node) - u := fileindex.Unresolved{ - Caller: p.caller(p.innermost(node.StartByte()), site), - Line: int(site.StartLine), - Column: int(site.StartColumn), - Text: callgraph.ClipText(p.text(node)), - Reason: fileindex.UnresolvedComputedName, - } - if member != "" { - u.Receiver = callgraph.ClipText(p.text(node.ChildByFieldName("operand"))) - u.Method, u.Reason = member, fileindex.UnresolvedFunctionValue - } - p.ix.Unresolved = append(p.ix.Unresolved, u) -} - -// goRangeRecord reports whether a range over a value needs a record, and -// the member name that the link step checks. A value that the source -// shows to be data needs none: a literal, a value of a type of data, or a -// local whose declaration gives it such a value. A name of the package -// reaches its value through the load code of the package. A member is a -// candidate that the link step checks against the fields and the -// references of the graph. -func (p *projection) goRangeRecord(value *sitter.Node) (string, bool) { - caller := p.innermost(value.StartByte()) + p.goRangeValue(goRange{node: node, reason: fileindex.UnresolvedComputedName, method: member}) +} + +// goRange is the record of a range over a value: its site and what the +// link step reads to decide it, as fileindex.UnresolvedRangeValue says. +type goRange struct { + node *sitter.Node + reason fileindex.UnresolvedReason + receiver, method string +} + +// goRangeValue adds the record of a range. +func (p *projection) goRangeValue(r goRange) { + site := ast.GetNodePosition(r.node) + p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ + Caller: p.caller(p.innermost(r.node.StartByte()), site), + Line: int(site.StartLine), + Column: int(site.StartColumn), + Text: callgraph.ClipText(p.text(r.node)), + Receiver: r.receiver, + Method: r.method, + Reason: r.reason, + }) +} + +// goRangeRecord returns the record of a range over a value, or false for +// a value that the source shows to be data: a literal, a slice of a value, +// a value of a type of data, an element of a map or a slice of a declared +// type of data, or a local whose declaration gives such a value. A name of +// the package reaches its value through the load code of the package. A +// member, a value of a named type and the result of a call are records of +// the reason range-value, which the link step decides from the fields, the +// types and the functions of the graph. A local that a call gives has the +// record at the call. +func (p *projection) goRangeRecord(value *sitter.Node, depth int) (goRange, bool) { + value = goUnparen(value) + plain := goRange{node: value, reason: fileindex.UnresolvedComputedName} + if value == nil || depth > maxReferenceSegments { + return plain, value != nil + } if p.goDataValue(value) { - return "", false + return goRange{}, false } switch value.Type() { + case "slice_expression": + return goRange{}, false case "identifier": - binding, ok := p.goBinding(caller, p.text(value), value.StartByte()) + binding, ok := p.goBinding(p.innermost(value.StartByte()), p.text(value), value.StartByte()) switch { case !ok: - return "", false + return goRange{}, false case binding.typ != nil: - return "", !p.goDataField(binding.typ) + return p.goRangeType(binding.typ, value) case binding.value != nil: - return "", !p.goDataValue(goUnparen(binding.value)) + return p.goRangeRecord(binding.value, depth+1) } - return "", true case "selector_expression": - return p.text(value.ChildByFieldName("field")), true + return goRange{node: value, reason: fileindex.UnresolvedRangeValue, receiver: callgraph.ClipText(p.text(value.ChildByFieldName("operand"))), method: p.text(value.ChildByFieldName("field"))}, true + case "index_expression": + if t := p.goElementType(value.ChildByFieldName("operand")); t != nil { + return p.goRangeType(t, value) + } case "type_assertion_expression": - return "", !p.goDataField(value.ChildByFieldName("type")) + return p.goRangeType(value.ChildByFieldName("type"), value) + case "call_expression": + return goRange{node: value, reason: fileindex.UnresolvedRangeValue}, true + } + return plain, true +} + +// goRangeType returns the record of a range over a value of a declared +// type, or false for a type of data. A named type that the file does not +// declare is a record with its name. +func (p *projection) goRangeType(t, at *sitter.Node) (goRange, bool) { + if p.goDataField(t) { + return goRange{}, false + } + if name := p.goTypeName(t); name != "" && t.Type() != "function_type" { + return goRange{node: at, reason: fileindex.UnresolvedRangeValue, method: name}, true + } + return goRange{node: at, reason: fileindex.UnresolvedComputedName}, true +} + +// goElementType returns the declared type of an element of a map, a slice +// or an array that a local of a declared type holds, or nil. +func (p *projection) goElementType(operand *sitter.Node) *sitter.Node { + operand = goUnparen(operand) + if operand == nil || operand.Type() != "identifier" { + return nil + } + binding, ok := p.goBinding(p.innermost(operand.StartByte()), p.text(operand), operand.StartByte()) + if !ok || binding.typ == nil { + return nil + } + switch binding.typ.Type() { + case "map_type": + return binding.typ.ChildByFieldName("value") + case "slice_type", "array_type": + return binding.typ.ChildByFieldName("element") } - return "", true + return nil } // goUnparen returns the expression in parentheses. @@ -577,7 +692,8 @@ func goUnparen(node *sitter.Node) *sitter.Node { } // goAnonFields returns the names of the fields of each anonymous struct of -// the file that can hold a function. +// the file and of each struct type that a function declares, true for a +// name that a field which can hold a function has. func (p *projection) goAnonFields() map[string]bool { if p.anonFields != nil { return p.anonFields @@ -588,14 +704,11 @@ func (p *projection) goAnonFields() map[string]bool { if depth > maxLocalDepth { return } - if node.Type() == "struct_type" { - if parent := node.Parent(); parent == nil || parent.Type() != "type_spec" { - for _, field := range p.goStructFields(node) { - if !p.goDataField(field.ChildByFieldName("type")) { - for _, name := range childrenByField(field, "name") { - p.anonFields[p.text(name)] = true - } - } + if node.Type() == "struct_type" && !goTopLevelType(node) { + for _, field := range p.goStructFields(node) { + holds := !p.goDataField(field.ChildByFieldName("type")) + for _, name := range childrenByField(field, "name") { + p.anonFields[p.text(name)] = p.anonFields[p.text(name)] || holds } } } @@ -607,6 +720,28 @@ func (p *projection) goAnonFields() map[string]bool { return p.anonFields } +// goTopLevelType reports whether a type is the type of a type declaration +// at the top level of the file, whose fields are definitions of their type. +func goTopLevelType(t *sitter.Node) bool { + spec := t.Parent() + if spec == nil || spec.Type() != "type_spec" { + return false + } + decl := spec.Parent() + return decl != nil && decl.Parent() != nil && decl.Parent().Type() == "source_file" +} + +// goAnonFieldDefinitions adds a field definition under the key struct of +// the file for each name of goAnonFields, as file.go//struct//run. The +// keyword struct names no type, so the key binds no name. The link step +// reads the fields of the graph by their names to tell a call of a field +// that can hold a function, as s.run(), from a call of a method. +func (p *projection) goAnonFieldDefinitions() { + for name, holds := range p.goAnonFields() { + p.addDefinition(fileindex.Join(p.cg.FileName, "struct", name), fileindex.DefField, p.tree.Tree().RootNode()).Data = !holds + } +} + // goLiteralCall reports whether a call names a local that the caller // binds once, to a function literal, and never assigns again, as f in // f := func() {...}; f(). The index gives the calls of the literal to the diff --git a/plugin/indexer/testdata/golden/app.go.json b/plugin/indexer/testdata/golden/app.go.json index 5d23cafc..77eac617 100644 --- a/plugin/indexer/testdata/golden/app.go.json +++ b/plugin/indexer/testdata/golden/app.go.json @@ -51,7 +51,8 @@ "name": "Server", "start_line": 18, "end_line": 24, - "exported": true + "exported": true, + "value_data": true }, { "key": "app.go//Server//addr", @@ -99,7 +100,8 @@ "name": "Box", "start_line": 26, "end_line": 26, - "exported": true + "exported": true, + "value_data": true }, { "key": "app.go//Box//v", @@ -139,7 +141,8 @@ "results": [ "Server", "" - ] + ], + "value_data": true }, { "key": "app.go//Server//Run", @@ -228,7 +231,8 @@ "name": "s", "type": "Server" } - ] + ], + "value_data": true } ], "classes": [ diff --git a/projectgraph/link.go b/projectgraph/link.go index 6d09dd4b..7239f073 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -143,9 +143,15 @@ type builder struct { // hold a function has. defs map[string]*fileindex.Definition fieldNames map[string]bool + // methodNames holds the names of the Go methods among them. + methodNames map[string]bool + // callees holds the definitions and the external keys that the calls + // at each site resolve to. + callees map[Site][]string // valueSites holds the sites of the references to a function or a - // method. + // method, and dataSites the sites of the references to data. valueSites map[Site]bool + dataSites map[Site]bool // reading holds the variables whose value a walk reads now, so a cycle // of variables across files stops. stepping holds the names that a // walk of a unit reads now for the result type of an external key. @@ -309,6 +315,8 @@ func newTables(indexes []*fileindex.Index, opts Options) *builder { checks: map[edgeSlot]bool{}, unresolved: map[UnresolvedSite]bool{}, valueSites: map[Site]bool{}, + dataSites: map[Site]bool{}, + callees: map[Site][]string{}, truncations: map[Truncation]bool{}, names: map[nameKey]resolution{}, visiting: map[nameKey]bool{}, @@ -524,11 +532,11 @@ func (b *builder) copyUnresolved(ix *fileindex.Index) { } // The Go index gives a range over a member the member name, and // the fields of the graph decide whether the range reads data. - if u.Reason == fileindex.UnresolvedFunctionValue && u.Method != "" { - if b.rangesNoValue(u, at) { - continue + if u.Reason == fileindex.UnresolvedRangeValue { + for _, r := range b.rangeRecords(ix, u, site) { + b.addUnresolved(r) } - site.Method = "" + continue } if u.Reason == fileindex.UnresolvedDynamicImport { site.ImportPrefix = b.importPrefix(ix, u.Line) diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index bf060c02..72f3b92c 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -156,6 +156,7 @@ func (b *builder) linkCalls(ix *fileindex.Index) { if r.kind == EdgeMember && r.declared { r.rule = ruleDeclaredType } + b.callees[site] = append(b.callees[site], r.key) b.link(ix.Language, from, r.key, site, r.kind, r.rule) } } @@ -366,6 +367,11 @@ func (b *builder) linkReferences(ix *fileindex.Index) { // A Go value of a named type is no function, so passing it // on calls nothing. Nor is a field or a constant. if sc.profile.typeConversions && b.isType(r.key) || b.holdsData(r.key) { + // A Go field holds data only when the index marks it, as + // a field of a function type can hold a function. + if def := b.defs[r.key]; b.holdsData(r.key) && (def == nil || def.Kind != fileindex.DefField || def.Data) { + b.dataSites[site] = true + } continue } if b.isFunction(r.key) { diff --git a/projectgraph/values.go b/projectgraph/values.go index 6687d573..9e2b2bed 100644 --- a/projectgraph/values.go +++ b/projectgraph/values.go @@ -86,15 +86,86 @@ func (b *builder) valueField(name string) (holds, known bool) { return holds, known } -// rangesNoValue reports whether the index record of a range over a member -// needs no record: each field of the member name in the graph holds data, -// or the member is a function or a method with a reference edge at the -// site, as t.All in for x := range t.All. A member that no field of the -// graph names, as a field of a type of a package that no unit gives, can -// hold a function. -func (b *builder) rangesNoValue(u fileindex.Unresolved, at Site) bool { +// rangeRecords returns the records of the index record of a range over a +// value that the index cannot show to be data, at the site. A record that +// the graph shows to read data has none. Otherwise it is a function-value +// record, as fileindex.UnresolvedRangeValue says. +func (b *builder) rangeRecords(ix *fileindex.Index, u fileindex.Unresolved, site UnresolvedSite) []UnresolvedSite { + site.Reason, site.Method, site.Receiver = fileindex.UnresolvedFunctionValue, "", "" + switch { + case u.Receiver != "" && u.Method != "": + return b.rangeMember(u, site) + case u.Method != "": + if key, ok := b.resolveType(b.scopes[ix.Path], u.Caller, u.Method); ok && b.valueData(key) { + return nil + } + case b.callsData(site.Site): + return nil + } + return []UnresolvedSite{site} +} + +// rangeMember returns the records of a range over a member. A field of the +// name that can hold a function, or no field and no method of the name, as +// a field of a type of a package that no unit gives, makes it a +// function-value record. A method of the name makes it a record with the +// method name too, so a name edge leads the search to each method of the +// name, as the range calls a method value. A reference at the site that +// names a function, as t.All of a typed t, or data, as n.Attr of a typed +// n, needs no record, and neither does a member whose fields hold data +// only. +func (b *builder) rangeMember(u fileindex.Unresolved, site UnresolvedSite) []UnresolvedSite { + if b.valueSites[site.Site] || b.dataSites[site.Site] { + return nil + } holds, known := b.valueField(u.Method) - return known && !holds || b.valueSites[at] + method := b.goMethodNamed(u.Method) + var out []UnresolvedSite + if holds || !known && !method { + out = append(out, site) + } + if method { + named := site + named.Reason, named.Receiver, named.Method = fileindex.UnresolvedUnknownReceiver, u.Receiver, u.Method + out = append(out, named) + } + return out +} + +// callsData reports whether each call at a site gives data: a function or +// a method whose first result holds no function, or code outside the graph, +// whose own record covers a function that it makes, as a function value +// that a package of the standard library with no unit gives. +func (b *builder) callsData(site Site) bool { + callees := b.callees[site] + return len(callees) > 0 && !slices.ContainsFunc(callees, func(key string) bool { + if b.kindOf(key) != NodeExternal { + return !b.valueData(key) + } + defs, provided := b.unitDefinitions(b.origins[key]) + return provided && slices.ContainsFunc(defs, func(d string) bool { return !b.valueData(d) }) + }) +} + +// valueData reports whether the definition of a key marks its values as +// data, as fileindex.Definition.ValueData says. +func (b *builder) valueData(key string) bool { + def := b.defs[key] + return def != nil && def.ValueData +} + +// goMethodNamed reports whether a method of a language with functionValues +// has the name. +func (b *builder) goMethodNamed(name string) bool { + if b.methodNames == nil { + b.methodNames = map[string]bool{} + for key, def := range b.defs { + if def.Kind == fileindex.DefMethod && profileOf(b.languageOf(key)).functionValues { + b.methodNames[def.Name] = true + } + } + } + return b.methodNames[name] } // reflection is a package of a language that gives or runs a function diff --git a/test/projectgraph/cases/go/range-over-func/expect.yaml b/test/projectgraph/cases/go/range-over-func/expect.yaml index 9581049d..9a09b00a 100644 --- a/test/projectgraph/cases/go/range-over-func/expect.yaml +++ b/test/projectgraph/cases/go/range-over-func/expect.yaml @@ -7,13 +7,21 @@ description: >- is not found. consume ranges over its parameter seq of a function type, and drain over the field each of a function type. Each range calls a function value that a search from a source does not reach, so it is a function-value record, and each target is uncertain from consume and from drain. A range with no - record read not-found there. attrs ranges over the field Attr of a node, which holds a slice. A range - over a member that the index cannot name keeps no record with the method name, so it gets no name edge - to element.Attr, and os.Chdir is not found from attrs. The link step drops the record of the range, - because each field Attr of the graph holds data. typed ranges over a parameter of the type Seq of - another file, and viaLocal over a local that holds the field Each of a holder. The index cannot show - that either value is data, so each range is a function-value record, and os.Remove is uncertain from - each. + record read not-found there. attrs ranges over the member Attr of a range variable n, whose type the + index does not know. The field Attr of node holds a slice, but the method element.Attr can make n.Attr a + method value. So the range is a record with the method name, and os.Chdir is found from attrs through + the name edge. attrsTyped ranges over n.Attr of a parameter n *node. The reference at the site names the + field, which holds data, so the range has no record, and os.Chdir is not found from it. typed ranges + over a parameter of the type Seq of another file, and viaLocal over a local that holds the field Each of + a holder. The index cannot show that either value is data, so each range is a function-value record, and + os.Remove is uncertain from each. The record of viaLocal is at the field that gives the local. + dataRanges ranges over a slice of a slice, a value of the type List of another file, an element of a map + of slices, the result of names and of node.Children, which give slices, and a local that holds such a + result. stdRange ranges over the result of strings.Fields, code outside the graph whose own record + covers a function that it makes. The link step reads the types and the results of the graph and drops + each record. So os.Remove is not found from dataRanges. From stdRange only the callback of + strings.Fields concerns it, because tree.All, a method of an exported name, holds a function-value + record. roots: - key: main.go kind: main @@ -43,6 +51,9 @@ no_edges: - from: main.go//main to: os//exec//LookPath unresolved: + - caller: main.go//attrs + reason: no-dependency-source + callback: true - caller: main.go//tree//All reason: no-dependency-source callback: true @@ -61,6 +72,9 @@ unresolved: - caller: os//exec//LookPath reason: no-dependency-source callback: true + - caller: strings//Fields + reason: no-dependency-source + callback: true - caller: main.go//walk site: main.go:10 reason: function-value @@ -81,11 +95,15 @@ unresolved: - caller: main.go//drain site: main.go:46 reason: function-value + - caller: main.go//attrs + site: main.go:60 + reason: unknown-receiver + method: Attr - caller: main.go//typed site: main.go:67 reason: function-value - caller: main.go//viaLocal - site: main.go:73 + site: main.go:72 reason: function-value results: - target: os//Remove @@ -151,6 +169,24 @@ results: - target: os//Chdir sources: [main.go//attrs] sources_only: true + status: found + path: + - key: main.go//attrs + - key: main.go//element//Attr + site: main.go:60 + kind: name + confidence: low + - key: os//Chdir + site: main.go:54 + kind: direct + confidence: high + call_sites: + - caller: main.go//element//Attr + site: main.go:54 + reached: true + - target: os//Chdir + sources: [main.go//attrsTyped] + sources_only: true status: not-found call_sites: - caller: main.go//element//Attr @@ -177,4 +213,22 @@ results: reached: false uncertainties: - kind: unresolved-call - site: main.go:73 + site: main.go:72 + - target: os//Remove + sources: [main.go//dataRanges] + sources_only: true + status: not-found + call_sites: + - caller: main.go//walk + site: main.go:9 + reached: false + - target: os//Remove + sources: [types.go//stdRange] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//walk + site: main.go:9 + reached: false + uncertainties: + - kind: unresolved-call diff --git a/test/projectgraph/cases/go/range-over-func/project/main.go b/test/projectgraph/cases/go/range-over-func/project/main.go index 979d7cc5..f4fc029e 100644 --- a/test/projectgraph/cases/go/range-over-func/project/main.go +++ b/test/projectgraph/cases/go/range-over-func/project/main.go @@ -73,3 +73,24 @@ func viaLocal(h holder) { for range seq { } } + +func attrsTyped(n *node) { + for range n.Attr { + } +} + +func dataRanges(b []byte, l List, m map[string][]string, n *node) { + for range b[:2] { + } + for range l { + } + for range m["a"] { + } + for range names() { + } + for range n.Children() { + } + kids := n.Children() + for range kids { + } +} diff --git a/test/projectgraph/cases/go/range-over-func/project/types.go b/test/projectgraph/cases/go/range-over-func/project/types.go index 95174a18..831eb78f 100644 --- a/test/projectgraph/cases/go/range-over-func/project/types.go +++ b/test/projectgraph/cases/go/range-over-func/project/types.go @@ -1,5 +1,7 @@ package main +import "strings" + type node struct { Attr []string } @@ -9,3 +11,14 @@ type Seq func(func(string) bool) type holder struct { Each func(func(string) bool) } + +type List []string + +func names() []string { return nil } + +func (*node) Children() []*node { return nil } + +func stdRange(s string) { + for range strings.Fields(s) { + } +} diff --git a/test/projectgraph/cases/go/source-local-func/expect.yaml b/test/projectgraph/cases/go/source-local-func/expect.yaml index 3e5e62a1..36a12165 100644 --- a/test/projectgraph/cases/go/source-local-func/expect.yaml +++ b/test/projectgraph/cases/go/source-local-func/expect.yaml @@ -16,8 +16,19 @@ description: >- field act of an anonymous struct, and deref calls (*p). Each call runs a function value, so each is a function-value record, and os.Getwd is uncertain from each. keepRead reads the field fn of a hook and calls nothing. The read is a record with no receiver, so the link step does not take it for a call of a - field, and os.Getwd is not found from keepRead. + field, and os.Getwd is not found from keepRead. conv.go converts pointers, as (*T)(p) and + *(*uintptr)(unsafe.Pointer(k)). The operand of each dereference is a type and no value, so no call runs + a function value, and os.Getwd is not found from convs. runTable calls the field exec of the anonymous + struct of table, which anon.go declares, and localJob calls the field trigger of a struct type that the + function declares. The index gives each such field a definition under the key struct of its file, so the + link step sees that the call can run a function value. os.Getwd is uncertain from each. roots: + - key: anon.go + kind: main + rule: go.main + - key: conv.go + kind: main + rule: go.main - key: main.go kind: main rule: go.main @@ -33,6 +44,12 @@ no_edges: - from: main.go//run to: os//Remove unresolved: + - caller: anon.go//localJob + reason: no-dependency-source + callback: true + - caller: conv.go//runTable + reason: no-dependency-source + callback: true - caller: main.go//each reason: no-dependency-source callback: true @@ -78,6 +95,23 @@ unresolved: - caller: other.go//rangeAssign reason: no-dependency-source callback: true + - caller: unsafe//Pointer + reason: no-dependency-source + callback: true + - caller: anon.go//localJob + site: anon.go:8 + reason: unknown-receiver + method: trigger + - caller: anon.go//localJob + site: anon.go:8 + reason: function-value + - caller: conv.go//runTable + site: conv.go:15 + reason: function-value + - caller: conv.go//runTable + site: conv.go:15 + reason: unknown-receiver + method: exec - caller: main.go//run site: main.go:23 reason: function-value @@ -291,3 +325,33 @@ results: - caller: main.go//getwd site: main.go:18 reached: false + - target: os//Getwd + sources: [conv.go//convs] + sources_only: true + status: not-found + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false + - target: os//Getwd + sources: [conv.go//runTable] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false + uncertainties: + - kind: unresolved-call + site: conv.go:15 + - target: os//Getwd + sources: [anon.go//localJob] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//getwd + site: main.go:18 + reached: false + uncertainties: + - kind: unresolved-call + site: anon.go:8 diff --git a/test/projectgraph/cases/go/source-local-func/project/anon.go b/test/projectgraph/cases/go/source-local-func/project/anon.go new file mode 100644 index 00000000..92e111ac --- /dev/null +++ b/test/projectgraph/cases/go/source-local-func/project/anon.go @@ -0,0 +1,9 @@ +package main + +var table []struct{ exec func() } + +func localJob(f func()) { + type job struct{ trigger func() } + j := job{trigger: f} + j.trigger() +} diff --git a/test/projectgraph/cases/go/source-local-func/project/conv.go b/test/projectgraph/cases/go/source-local-func/project/conv.go new file mode 100644 index 00000000..1f4808bf --- /dev/null +++ b/test/projectgraph/cases/go/source-local-func/project/conv.go @@ -0,0 +1,17 @@ +package main + +import "unsafe" + +type T struct{ n int } + +func convs(p unsafe.Pointer, k *uint32) uintptr { + _ = (*T)(p) + _ = (*T)(nil) + return *(*uintptr)(unsafe.Pointer(k)) +} + +func runTable() { + for _, t := range table { + t.exec() + } +} diff --git a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml index 800bf386..191f3194 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml +++ b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml @@ -15,8 +15,15 @@ description: >- the parameter x of a function literal, which no type of the index names, so the call is a record. pickIface types r by an assignment, but r holds the interface runner from the field r, so the writes do not agree and the call is a record too. A type of local for each of the two gave not-found for - os/exec.LookPath. + os/exec.LookPath. In idiom.go, parse declares url from url.Parse, and the local binds no name at the + call, so the call keeps its edge to the import. viaParam and viaRange bind log, which shadows the import + of log, so the index takes no target of the import. The parameter of the type logger gives a member edge + to logger.Println, and the range variable a record with a name edge. In shadowedParam of main.go a local o shadows + the parameter o, so the field r of the parameter type does not type x, and the call x.run() is a record. roots: + - key: idiom.go + kind: main + rule: go.main - key: main.go kind: main rule: go.main @@ -72,6 +79,12 @@ no_edges: - from: main.go//named to: main.go//remote//run unresolved: + - caller: idiom.go//viaRange + reason: no-dependency-source + callback: true + - caller: log//Println + reason: no-dependency-source + callback: true - caller: main.go//local//viaLit reason: no-dependency-source callback: true @@ -81,15 +94,32 @@ unresolved: - caller: main.go//pickIface reason: no-dependency-source callback: true + - caller: main.go//shadowedParam + reason: no-dependency-source + callback: true + - caller: net//url//Parse + reason: no-dependency-source + callback: true - caller: os//Getwd reason: no-dependency-source callback: true + - caller: os//Mkdir + reason: no-dependency-source + callback: true - caller: os//Remove reason: no-dependency-source callback: true - caller: os//exec//LookPath reason: no-dependency-source callback: true + - caller: idiom.go//parse + site: idiom.go:14 + reason: no-dependency-source + method: Host + - caller: idiom.go//viaRange + site: idiom.go:23 + reason: unknown-receiver + method: Println - caller: main.go//main site: main.go:24 reason: unknown-receiver @@ -109,6 +139,14 @@ unresolved: site: main.go:62 reason: unknown-receiver method: run + - caller: main.go//shadowedParam + site: main.go:79 + reason: unknown-receiver + method: r + - caller: main.go//shadowedParam + site: main.go:81 + reason: unknown-receiver + method: run results: - target: os//Getwd status: found @@ -210,3 +248,79 @@ results: - caller: main.go//remote//run site: main.go:14 reached: true + - target: net//url//Parse + sources: [idiom.go//parse] + sources_only: true + status: found + path: + - key: idiom.go//parse + - key: net//url//Parse + site: idiom.go:10 + kind: direct + confidence: high + call_sites: + - caller: idiom.go//parse + site: idiom.go:10 + reached: true + - target: os//Mkdir + sources: [idiom.go//viaParam] + sources_only: true + status: found + path: + - key: idiom.go//viaParam + - key: idiom.go//logger//Println + site: idiom.go:28 + kind: member + confidence: high + - key: os//Mkdir + site: idiom.go:19 + kind: direct + confidence: high + call_sites: + - caller: idiom.go//logger//Println + site: idiom.go:19 + reached: true + - target: log//Println + sources: [idiom.go//viaParam] + sources_only: true + status: not-found + call_sites: + - caller: idiom.go//keepLog + site: idiom.go:31 + reached: false + - target: os//Mkdir + sources: [idiom.go//viaRange] + sources_only: true + status: found + path: + - key: idiom.go//viaRange + - key: idiom.go//logger//Println + site: idiom.go:23 + kind: name + confidence: low + - key: os//Mkdir + site: idiom.go:19 + kind: direct + confidence: high + call_sites: + - caller: idiom.go//logger//Println + site: idiom.go:19 + reached: true + - target: os//Getwd + sources: [main.go//shadowedParam] + sources_only: true + status: found + path: + - key: main.go//shadowedParam + - key: main.go//local//run + site: main.go:81 + kind: name + confidence: low + - key: os//Getwd + site: main.go:10 + kind: direct + confidence: high + call_sites: + - caller: main.go//local//run + site: main.go:10 + reached: true diff --git a/test/projectgraph/cases/go/typed-local-shadow/project/idiom.go b/test/projectgraph/cases/go/typed-local-shadow/project/idiom.go new file mode 100644 index 00000000..dbceda54 --- /dev/null +++ b/test/projectgraph/cases/go/typed-local-shadow/project/idiom.go @@ -0,0 +1,31 @@ +package main + +import ( + "log" + "net/url" + "os" +) + +func parse(raw string) string { + url, err := url.Parse(raw) + if err != nil { + return "" + } + return url.Host +} + +type logger struct{} + +func (logger) Println() { os.Mkdir("d", 0o755) } + +func viaRange(ls []logger) { + for _, log := range ls { + log.Println() + } +} + +func viaParam(log logger) { + log.Println() +} + +func keepLog() { log.Println("x") } diff --git a/test/projectgraph/cases/go/typed-local-shadow/project/main.go b/test/projectgraph/cases/go/typed-local-shadow/project/main.go index 4456e216..81c99a53 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/project/main.go +++ b/test/projectgraph/cases/go/typed-local-shadow/project/main.go @@ -72,3 +72,12 @@ func siblings() { x.run() } } + +func shadowedParam(o opts, rs []runner) { + { + o := struct{ r runner }{rs[0]} + x := o.r + x = &remote{} + x.run() + } +} From 2001444b6930e434cd16c6f4c0e851ffe2b20f6e Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 20:14:35 +0000 Subject: [PATCH 49/86] fix(rootmatch): skip test paths only for the component rule A path rule skipped every file of a test path, so a route under a test path, as app/api/test/route.ts of Next.js or src/routes/tests of SvelteKit, lost its root and its targets read as not-found. A route rule names its files by their path in the app, so it keeps them. A new rule field skips_tests marks the rule that skips them, and only the component rule sets it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 11 ++- fileindex/roots/rules.go | 4 + fileindex/roots/rules.yaml | 2 + projectgraph/rootmatch/framework.go | 4 +- .../javascript/sveltekit-test-dir/expect.yaml | 76 +++++++++++++++++++ .../sveltekit-test-dir/project/package.json | 1 + .../sveltekit-test-dir/project/server.js | 3 + .../project/src/routes/api/test/+server.ts | 5 ++ .../project/src/routes/tests/+page.svelte | 6 ++ .../next-route-test-dir/expect.yaml | 71 +++++++++++++++++ .../project/app/api/test/route.ts | 5 ++ .../project/app/spec/page.tsx | 5 ++ .../next-route-test-dir/project/package.json | 1 + 13 files changed, 187 insertions(+), 7 deletions(-) create mode 100644 test/projectgraph/cases/javascript/sveltekit-test-dir/expect.yaml create mode 100644 test/projectgraph/cases/javascript/sveltekit-test-dir/project/package.json create mode 100644 test/projectgraph/cases/javascript/sveltekit-test-dir/project/server.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-test-dir/project/src/routes/api/test/+server.ts create mode 100644 test/projectgraph/cases/javascript/sveltekit-test-dir/project/src/routes/tests/+page.svelte create mode 100644 test/projectgraph/cases/typescript/next-route-test-dir/expect.yaml create mode 100644 test/projectgraph/cases/typescript/next-route-test-dir/project/app/api/test/route.ts create mode 100644 test/projectgraph/cases/typescript/next-route-test-dir/project/app/spec/page.tsx create mode 100644 test/projectgraph/cases/typescript/next-route-test-dir/project/package.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index f18ef214..5a04de58 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -754,10 +754,13 @@ A framework loads a component file of Vue, Svelte or Astro in ways that no static import shows: a route table with `() => import('./views/X.vue')`, an alias of the bundler as `@/`, `import.meta.glob`, or a file-based router, as the `pages` of Nuxt. So the rule `node.component` marks each -component file as a root, with no `requires`. A path rule skips a test -file, which runs only under a test, and the rule matches the name of a -file, not of a directory, as the case `javascript/component-test-path` -shows. A component file holds a `markup` record, so this root makes each +component file as a root, with no `requires`. The rule has +`skips_tests`, so it skips a component under a test path, which runs only +under a test, and it matches the name of a file, not of a directory, as +the case `javascript/component-test-path` shows. A route rule does not +skip a test path, because a route can have one, as `app/api/test`. The +cases `typescript/next-route-test-dir` and `javascript/sveltekit-test-dir` +show it. A component file holds a `markup` record, so this root makes each JavaScript target that the search does not find read `uncertain`. A static import of a component file already did that in most such projects. The rule keeps the fallback on, because a Vite app starts at `index.html`, which no rule diff --git a/fileindex/roots/rules.go b/fileindex/roots/rules.go index 3c1936ec..a43b589c 100644 --- a/fileindex/roots/rules.go +++ b/fileindex/roots/rules.go @@ -60,12 +60,16 @@ var manifestFields = map[string][]string{ // applies. KeepsFallback marks a rule whose roots do not turn off the // library-surface roots. Such a rule names files that a framework loads, // but not the entry that loads them, as the index.html of a Vite app. +// SkipsTests marks a rule that does not mark a file of a test path, as a +// component fixture of a test. A route rule names its files by their path +// in the app, and a route can have a test path, as app/api/test/route.ts. type Rule struct { ID string `yaml:"id"` Language string `yaml:"language"` Kind Kind `yaml:"kind"` Requires []string `yaml:"requires,omitempty"` KeepsFallback bool `yaml:"keeps_fallback,omitempty"` + SkipsTests bool `yaml:"skips_tests,omitempty"` Match Match `yaml:"match"` } diff --git a/fileindex/roots/rules.yaml b/fileindex/roots/rules.yaml index 8d89db4b..840d8e05 100644 --- a/fileindex/roots/rules.yaml +++ b/fileindex/roots/rules.yaml @@ -5,6 +5,7 @@ # rule, and the rule applies only to a project that depends on one of them. # keeps_fallback marks a rule whose roots do not turn off the library-surface # roots, because it names the files that a framework loads and not the entry. +# skips_tests marks a rule that does not mark a file of a test path. version: 1 rules: - id: go.main @@ -98,6 +99,7 @@ rules: language: javascript kind: framework keeps_fallback: true + skips_tests: true match: { path: ["**/*.vue", "**/*.svelte", "**/*.astro"] } - id: node.sveltekit.route language: javascript diff --git a/projectgraph/rootmatch/framework.go b/projectgraph/rootmatch/framework.go index 2ec8e96a..f0b8073c 100644 --- a/projectgraph/rootmatch/framework.go +++ b/projectgraph/rootmatch/framework.go @@ -15,9 +15,7 @@ import ( // script, and only its top level runs. func (f *finder) matchPath(r rules.Rule) { for _, ix := range f.applying(r) { - // A test file runs only under a test, so a path convention of the - // program does not make it a root of the program. - if !pathglob.MatchAny(r.Match.Path, ix.Path) || f.IsTest(ix.Path) { + if !pathglob.MatchAny(r.Match.Path, ix.Path) || r.SkipsTests && f.IsTest(ix.Path) { continue } f.add(r, ix.Path) diff --git a/test/projectgraph/cases/javascript/sveltekit-test-dir/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-test-dir/expect.yaml new file mode 100644 index 00000000..4dc87ae4 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-test-dir/expect.yaml @@ -0,0 +1,76 @@ +source: safedep:hand +description: >- + A route of the app has a path under a test path, as app/api/test or src/routes/tests. The route rule + names its files by their path in the app, so it marks them although they are test files, and probe + and render are found. Only the rule node.component skips a file of a test path. +test_paths: [test, tests, spec] +roots: + - key: server.js + kind: manifest + rule: node.package-json + - key: src/routes/api/test/+server.ts + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/test/+server.ts + kind: test + rule: test.files + - key: src/routes/api/test/+server.ts//GET + kind: framework + rule: node.sveltekit.route + - key: src/routes/api/test/+server.ts//GET + kind: test + rule: test.files + - key: src/routes/tests/+page.svelte + kind: framework + rule: node.sveltekit.route + - key: src/routes/tests/+page.svelte + kind: test + rule: test.files +exact_edges: true +edges: + - from: server.js + to: plug//listen + kind: direct + confidence: high + site: server.js:3 + - from: src/routes/api/test/+server.ts//GET + to: plug//probe + kind: direct + confidence: high + site: src/routes/api/test/+server.ts:4 + - from: src/routes/tests/+page.svelte + to: plug//render + kind: direct + confidence: high + site: src/routes/tests/+page.svelte:3 +unresolved: + - caller: src/routes/tests/+page.svelte + site: src/routes/tests/+page.svelte:1 + reason: markup +results: + - target: plug//probe + status: found + path: + - key: src/routes/api/test/+server.ts//GET + root: framework + - key: plug//probe + site: src/routes/api/test/+server.ts:4 + kind: direct + confidence: high + call_sites: + - caller: src/routes/api/test/+server.ts//GET + site: src/routes/api/test/+server.ts:4 + reached: true + - target: plug//render + status: found + path: + - key: src/routes/tests/+page.svelte + root: framework + - key: plug//render + site: src/routes/tests/+page.svelte:3 + kind: direct + confidence: high + call_sites: + - caller: src/routes/tests/+page.svelte + site: src/routes/tests/+page.svelte:3 + reached: true diff --git a/test/projectgraph/cases/javascript/sveltekit-test-dir/project/package.json b/test/projectgraph/cases/javascript/sveltekit-test-dir/project/package.json new file mode 100644 index 00000000..8a00bac4 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-test-dir/project/package.json @@ -0,0 +1 @@ +{ "name": "sveltekit-test-dir", "main": "server.js", "devDependencies": { "@sveltejs/kit": "^2.5.0" } } diff --git a/test/projectgraph/cases/javascript/sveltekit-test-dir/project/server.js b/test/projectgraph/cases/javascript/sveltekit-test-dir/project/server.js new file mode 100644 index 00000000..fc1cd6f8 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-test-dir/project/server.js @@ -0,0 +1,3 @@ +import { listen } from 'plug'; + +listen(3000); diff --git a/test/projectgraph/cases/javascript/sveltekit-test-dir/project/src/routes/api/test/+server.ts b/test/projectgraph/cases/javascript/sveltekit-test-dir/project/src/routes/api/test/+server.ts new file mode 100644 index 00000000..4dd9311e --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-test-dir/project/src/routes/api/test/+server.ts @@ -0,0 +1,5 @@ +import { probe } from 'plug'; + +export function GET() { + return probe(); +} diff --git a/test/projectgraph/cases/javascript/sveltekit-test-dir/project/src/routes/tests/+page.svelte b/test/projectgraph/cases/javascript/sveltekit-test-dir/project/src/routes/tests/+page.svelte new file mode 100644 index 00000000..875bb221 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-test-dir/project/src/routes/tests/+page.svelte @@ -0,0 +1,6 @@ + + +

tests

diff --git a/test/projectgraph/cases/typescript/next-route-test-dir/expect.yaml b/test/projectgraph/cases/typescript/next-route-test-dir/expect.yaml new file mode 100644 index 00000000..fed3c93d --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-test-dir/expect.yaml @@ -0,0 +1,71 @@ +source: safedep:hand +description: >- + A route of the app has a path under a test path, as app/api/test or src/routes/tests. The route rule + names its files by their path in the app, so it marks them although they are test files, and probe + and render are found. Only the rule node.component skips a file of a test path. +test_paths: [test, tests, spec] +roots: + - key: app/api/test/route.ts + kind: framework + rule: node.next.route + - key: app/api/test/route.ts + kind: test + rule: test.files + - key: app/api/test/route.ts//GET + kind: framework + rule: node.next.route + - key: app/api/test/route.ts//GET + kind: test + rule: test.files + - key: app/spec/page.tsx + kind: framework + rule: node.next.route + - key: app/spec/page.tsx + kind: test + rule: test.files + - key: app/spec/page.tsx//Page + kind: framework + rule: node.next.route + - key: app/spec/page.tsx//Page + kind: test + rule: test.files +exact_edges: true +edges: + - from: app/api/test/route.ts//GET + to: plug//probe + kind: direct + confidence: high + site: app/api/test/route.ts:4 + - from: app/spec/page.tsx//Page + to: plug//render + kind: direct + confidence: high + site: app/spec/page.tsx:4 +unresolved: [] +results: + - target: plug//probe + status: found + path: + - key: app/api/test/route.ts//GET + root: framework + - key: plug//probe + site: app/api/test/route.ts:4 + kind: direct + confidence: high + call_sites: + - caller: app/api/test/route.ts//GET + site: app/api/test/route.ts:4 + reached: true + - target: plug//render + status: found + path: + - key: app/spec/page.tsx//Page + root: framework + - key: plug//render + site: app/spec/page.tsx:4 + kind: direct + confidence: high + call_sites: + - caller: app/spec/page.tsx//Page + site: app/spec/page.tsx:4 + reached: true diff --git a/test/projectgraph/cases/typescript/next-route-test-dir/project/app/api/test/route.ts b/test/projectgraph/cases/typescript/next-route-test-dir/project/app/api/test/route.ts new file mode 100644 index 00000000..82804eb6 --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-test-dir/project/app/api/test/route.ts @@ -0,0 +1,5 @@ +import { probe } from 'plug'; + +export async function GET() { + return probe(); +} diff --git a/test/projectgraph/cases/typescript/next-route-test-dir/project/app/spec/page.tsx b/test/projectgraph/cases/typescript/next-route-test-dir/project/app/spec/page.tsx new file mode 100644 index 00000000..9a022ea4 --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-test-dir/project/app/spec/page.tsx @@ -0,0 +1,5 @@ +import { render } from 'plug'; + +export default function Page() { + return render(); +} diff --git a/test/projectgraph/cases/typescript/next-route-test-dir/project/package.json b/test/projectgraph/cases/typescript/next-route-test-dir/project/package.json new file mode 100644 index 00000000..ffe18ea4 --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-test-dir/project/package.json @@ -0,0 +1 @@ +{ "name": "next-route-test-dir", "dependencies": { "next": "^14.2.0" } } From 9962d577558c84b7e094babe635f37e3c9bcee46 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 20:22:37 +0000 Subject: [PATCH 50/86] fix(rootmatch): scope opaque route exports to JavaScript and narrow them An opaque export got a computed-name record with no scope, and any value that was no plain literal was opaque, so a line as revalidate = 60 * 60 or { ... } satisfies Metadata made each not-found of a Next.js project uncertain, the Python targets of a mixed repository too. The index now looks through satisfies, as, ! and parentheses, and takes a value with no call, new, await or read of a member as a literal. An object under satisfies keeps the names of its functions. The record of an opaque export has the scope of the markup record: the JavaScript family and the modules that its file imports, which Graph.Imports holds. So a markup record also concerns a target that only Python code calls when the component imports its module. The manifest rule now gives records as the path rule does, and so does an export of a default call and an export whose name no record resolves. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 29 +++++-- plugin/callgraph/javascript.go | 15 +++- plugin/indexer/exports.go | 48 +++++++++++- projectgraph/graph.go | 12 ++- projectgraph/link.go | 28 +++++++ projectgraph/reach/target.go | 10 +++ projectgraph/reach/uncertainty.go | 18 +++-- projectgraph/rootmatch/find_test.go | 10 +-- projectgraph/rootmatch/framework.go | 28 +++++-- projectgraph/rootmatch/match.go | 9 ++- projectgraph/roots.go | 4 +- .../library-opaque-export/expect.yaml | 38 +++++++++ .../project/package.json | 1 + .../project/src/index.js | 9 +++ .../sveltekit-actions-lib/expect.yaml | 31 +++++++- .../project/src/routes/items/+page.server.ts | 8 ++ .../python/component-in-frontend/expect.yaml | 26 +++++-- .../project/frontend/src/App.vue | 3 +- .../component-in-frontend/project/main.py | 3 + .../next-route-literals/expect.yaml | 60 +++++++++++++++ .../project/app/api/x/route.ts | 12 +++ .../next-route-literals/project/lib/db.ts | 5 ++ .../next-route-literals/project/package.json | 1 + .../typescript/next-route-opaque/expect.yaml | 77 +++++++++++++++++++ .../project/app/api/auth/route.ts | 4 + .../project/app/api/x/route.ts | 3 + .../next-route-opaque/project/lib/auth.ts | 7 ++ .../next-route-opaque/project/package.json | 1 + 28 files changed, 454 insertions(+), 46 deletions(-) create mode 100644 test/projectgraph/cases/javascript/library-opaque-export/expect.yaml create mode 100644 test/projectgraph/cases/javascript/library-opaque-export/project/package.json create mode 100644 test/projectgraph/cases/javascript/library-opaque-export/project/src/index.js create mode 100644 test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/items/+page.server.ts create mode 100644 test/projectgraph/cases/typescript/next-route-literals/expect.yaml create mode 100644 test/projectgraph/cases/typescript/next-route-literals/project/app/api/x/route.ts create mode 100644 test/projectgraph/cases/typescript/next-route-literals/project/lib/db.ts create mode 100644 test/projectgraph/cases/typescript/next-route-literals/project/package.json create mode 100644 test/projectgraph/cases/typescript/next-route-opaque/expect.yaml create mode 100644 test/projectgraph/cases/typescript/next-route-opaque/project/app/api/auth/route.ts create mode 100644 test/projectgraph/cases/typescript/next-route-opaque/project/app/api/x/route.ts create mode 100644 test/projectgraph/cases/typescript/next-route-opaque/project/lib/auth.ts create mode 100644 test/projectgraph/cases/typescript/next-route-opaque/project/package.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 5a04de58..e6e0189d 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -108,8 +108,11 @@ script reach, would miss a name that the app registers elsewhere, and a target that only such a name reaches would read `not-found`. The markup runs only code of the JavaScript family, so the reach query scopes the record by its language with `Uncertainty.Language`. It concerns a target -that a definition of the family is, or that an edge from a file of the -family leads to, and a target with no node. A JavaScript project of +under a module that the file imports from outside the project, as +`jsonschema` for `import { validate } from 'jsonschema'`, which +`Graph.Imports` holds. It also concerns a target that a definition of the +family is, or that an edge from a file of the family leads to, and a +target with no node. A JavaScript project of component files thus reads `uncertain` for each JavaScript target that it does not find. A target that only the Python code of a project calls does not, as in the case `python/component-in-frontend`. A span and a summary @@ -790,14 +793,24 @@ A default of a pattern, as `fb` in `{ PUT = fb } = h`, is a second export record of the name, and a path rule marks each record that resolves. An export of a name that holds another name, as `export const GET = handler`, gives that name first. An exported name whose value is no literal, as the -result of a call in `export const actions = makeActions()`, is `Opaque` in -the index. The framework calls the functions of such a value, and no edge -names them, so a path rule gives the export a `computed-name` record in -place of a root. The index records a function that an argument of a call +result of a call in `export const actions = makeActions()` or of +`export default NextAuth({...})`, is `Opaque` in the index. A value with no +call, `new`, `await` or read of a member, as `60 * 60` or +`'x' as const`, is a literal, and the index looks through `satisfies`, +`as`, `!` and parentheses. The framework calls the functions of an opaque +value, and no edge names them. So a path rule and the manifest rule give +the export a `computed-name` record in place of a root, and so does an +export whose name no record resolves, as GET of +`export const { GET } = handlers` for an imported `handlers`. The record +has the scope of the markup record. It concerns the targets of the +JavaScript family and of the modules that the file imports. The cases `typescript/next-route-literals`, +`typescript/next-route-opaque` and `javascript/library-opaque-export` +show it. The index records a function that an argument of a call in an export statement names, as `first` in `export const handle = sequence(first)`, as a reference. The case -`javascript/sveltekit-opaque-exports` shows each form. The resolver maps the alias `$lib` of SvelteKit -to `src/lib` of the nearest package that depends on `@sveltejs/kit`. It +`javascript/sveltekit-opaque-exports` shows each form. + +The resolver maps the alias `$lib` of SvelteKit to `src/lib` of the nearest package that depends on `@sveltejs/kit`. It does not read `svelte.config.js`, which can move the directory. So an import of `$lib` that names no file is first party, and it is a record with the reason `no-definition`. The cases `javascript/sveltekit-actions-lib` diff --git a/plugin/callgraph/javascript.go b/plugin/callgraph/javascript.go index 5e634aed..3aa6ea8a 100644 --- a/plugin/callgraph/javascript.go +++ b/plugin/callgraph/javascript.go @@ -504,7 +504,13 @@ func objectNames(object *sitter.Node, treeData []byte, depth int) []string { } return names case "object": - switch parent := object.Parent(); { + // A type of TypeScript, as { ... } satisfies Actions, or + // parentheses, wrap the object and leave its names. + parent := object.Parent() + for parent != nil && objectWrappers[parent.Type()] { + parent = parent.Parent() + } + switch { case parent == nil: case parent.Type() == "variable_declarator": if name := parent.ChildByFieldName("name"); name != nil && name.Type() == "identifier" { @@ -521,6 +527,13 @@ func objectNames(object *sitter.Node, treeData []byte, depth int) []string { return nil } +// objectWrappers hold an object with a type or a check of TypeScript, or +// in parentheses. +var objectWrappers = map[string]bool{ + "satisfies_expression": true, "as_expression": true, "non_null_expression": true, + "parenthesized_expression": true, "type_assertion": true, +} + // jsMethodDefinitionProcessor processes a method of a class or of an object // literal. A method of an object is keyed under the names that hold the // object, as api//run in const api = { run() {} }, as a function that a diff --git a/plugin/indexer/exports.go b/plugin/indexer/exports.go index 5110f2b7..dd6e9a79 100644 --- a/plugin/indexer/exports.go +++ b/plugin/indexer/exports.go @@ -91,6 +91,11 @@ func (p *projection) addExportStatement(statement *sitter.Node) { p.export(defaultExport, p.text(value), line) case functionValues[value.Type()] || value.Type() == "class": p.export(defaultExport, defaultExport, line) + case opaque(value): + // A default of a call, as export default NextAuth({...}), has + // no definition. The record keeps it for a rule that roots it. + p.export(defaultExport, defaultExport, line) + p.ix.Exports[len(p.ix.Exports)-1].Opaque = true } return } @@ -156,7 +161,7 @@ func (p *projection) declaredNames(declaration *sitter.Node) []string { // its target is the object that holds it. func (p *projection) exportDeclarators(declaration *sitter.Node, line int) { for _, declarator := range ts.NamedChildren(declaration) { - pattern, value := declarator.ChildByFieldName("name"), declarator.ChildByFieldName("value") + pattern, value := declarator.ChildByFieldName("name"), unwrap(declarator.ChildByFieldName("value")) switch { case pattern == nil: case pattern.Type() == "identifier": @@ -178,6 +183,7 @@ func (p *projection) exportDeclarators(declaration *sitter.Node, line int) { // export const GET = handler, so the export reaches its function. A value // that is no literal, as the result of a call, is opaque. func (p *projection) exportValue(name, local string, value *sitter.Node, line int) { + value = unwrap(value) if value != nil && value.Type() == "identifier" && p.text(value) != local { p.export(name, p.text(value), line) } @@ -187,11 +193,45 @@ func (p *projection) exportValue(name, local string, value *sitter.Node, line in } } +// typeWrappers hold a value with a type or a check of TypeScript, as +// x satisfies T, x as T and x!, or in parentheses. The value is the value +// of the wrapper. +var typeWrappers = map[string]bool{ + "satisfies_expression": true, "as_expression": true, "non_null_expression": true, + "parenthesized_expression": true, "type_assertion": true, +} + +// unwrap returns the value that type wrappers hold. +func unwrap(value *sitter.Node) *sitter.Node { + for value != nil && typeWrappers[value.Type()] { + value = value.NamedChild(0) + } + return value +} + +// opaqueParts are the expressions that can give a function that no +// definition of the file names: a call, a new, an await, and a read of a +// member, which can hold a function of another object. +var opaqueParts = map[string]bool{ + "call_expression": true, "new_expression": true, "await_expression": true, + "member_expression": true, "subscript_expression": true, +} + // opaque reports whether a value runs code that the index does not follow -// to a definition, as a call. A nil value is a name that no top-level -// declaration of the file binds, as an import. +// to a definition, as a call. A value with no call, new, await or read of +// a member, as 60 * 60 or 'x' as const, is a literal. A nil value is a +// name that no top-level declaration of the file binds, as an import. func opaque(value *sitter.Node) bool { - return value != nil && value.Type() != "identifier" && !functionValues[value.Type()] && !literalValues[value.Type()] + value = unwrap(value) + if value == nil || value.Type() == "identifier" || functionValues[value.Type()] || literalValues[value.Type()] { + return false + } + if opaqueParts[value.Type()] { + return true + } + return slices.ContainsFunc(ts.NamedChildren(value), func(child *sitter.Node) bool { + return opaqueParts[child.Type()] || opaque(child) + }) } // literalValues are the JavaScript values that run no code of their own diff --git a/projectgraph/graph.go b/projectgraph/graph.go index 6265c4ca..c1273ba6 100644 --- a/projectgraph/graph.go +++ b/projectgraph/graph.go @@ -206,7 +206,9 @@ type Edge struct { // search. Callback marks the site of an external key whose code can call // into the graph only through a method of a value that it holds, as the // Go standard library with no unit. The reach query applies it to the -// targets that a method of an exported name reaches. +// targets that a method of an exported name reaches. Language scopes a +// site to the targets that code of the language family calls, as a value +// of JavaScript that a framework calls, which runs only JavaScript code. type UnresolvedSite struct { Caller string `json:"caller"` Site Site `json:"site"` @@ -217,6 +219,7 @@ type UnresolvedSite struct { ImportPrefix string `json:"import_prefix,omitempty"` Unit string `json:"unit,omitempty"` Callback bool `json:"callback,omitempty"` + Language langcode.Code `json:"language,omitempty"` } // Root is a node that runs with no caller in the graph. Rule is the id of the @@ -264,7 +267,12 @@ type Graph struct { Files map[string]fileindex.FileStatus `json:"files"` Truncated []Truncation `json:"truncated,omitempty"` Problems []modules.Problem `json:"problems,omitempty"` - Stats Stats `json:"stats"` + // Imports holds the module keys that each file with a site of a + // Language scope imports from outside the project, as jsonschema. Such + // a site can run a name of the module that the file imports and no + // edge names, as a function that only the markup of the file calls. + Imports map[string][]string `json:"imports,omitempty"` + Stats Stats `json:"stats"` // edges is the set of Edges, which AddEdge makes on its first call and // keeps for the next ones. diff --git a/projectgraph/link.go b/projectgraph/link.go index 7239f073..bea41d90 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -444,6 +444,31 @@ func (b *builder) linkAll(ctx context.Context) error { return nil } +// scopeToLanguage scopes a site to the targets that code of the language +// family of its file calls, and keeps the modules that the file imports +// from outside the project, as the markup of a component runs only code +// of its script and of what the script imports. +func (b *builder) scopeToLanguage(ix *fileindex.Index, site *UnresolvedSite) { + site.Language = ix.Language.Family() + if _, done := b.g.Imports[ix.Path]; done { + return + } + var keys []string + for _, imp := range ix.Imports { + if imp.Kind == fileindex.ImportTypeOnly || imp.Namespace == "" || strings.HasPrefix(imp.Module, ".") { + continue + } + if _, _, ok := b.resolveModule(ix, imp.Module); !ok { + keys = append(keys, imp.Namespace) + } + } + slices.Sort(keys) + if b.g.Imports == nil { + b.g.Imports = map[string][]string{} + } + b.g.Imports[ix.Path] = slices.Compact(keys) +} + // CallbackMethod reports whether code outside the graph that a Callback // record stands for can call the definition of the key back, on a value // that it holds, as a Go method of an exported name. @@ -541,6 +566,9 @@ func (b *builder) copyUnresolved(ix *fileindex.Index) { if u.Reason == fileindex.UnresolvedDynamicImport { site.ImportPrefix = b.importPrefix(ix, u.Line) } + if u.Reason == fileindex.UnresolvedMarkup { + b.scopeToLanguage(ix, &site) + } if u.Reason == fileindex.UnresolvedNoDependencySource && ix.Language == langcode.Go { if b.linkGoStub(ix, site) { continue diff --git a/projectgraph/reach/target.go b/projectgraph/reach/target.go index 7b0ae3a6..67ae9016 100644 --- a/projectgraph/reach/target.go +++ b/projectgraph/reach/target.go @@ -94,6 +94,16 @@ func (t Target) hasMethod(method string) bool { return ok || fileindex.Last(t.key) == method } +// under reports whether the target is a key under a module, or a +// wildcard of a module that holds the module or that the module holds. +func (t Target) under(module string) bool { + if fileindex.HasKeyPrefix(t.key, module) { + return true + } + prefix, ok := t.wildcard() + return ok && (fileindex.HasKeyPrefix(prefix, module) || fileindex.HasKeyPrefix(module, prefix)) +} + // mayImport reports whether a computed import with a static prefix can load // the module of the target. A prefix longer than the module of a wildcard // must continue at a separator, so yamlx// does not load yaml//*. diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index fe085326..89b468c9 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -31,8 +31,9 @@ type found struct { // byLanguage holds the markup records, which concern the targets that // code of their language family calls. byLanguage []Uncertainty - // speaks reports whether code of a language family calls a target. - speaks func(lang langcode.Code, target Target) bool + // speaks reports whether the code of a site of a language scope can + // call a target. + speaks func(u Uncertainty, target Target) bool callback *Uncertainty // concerns reports whether the span of a scope concerns a target. concerns func(k scopeKey, target Target) bool @@ -179,6 +180,8 @@ func (s *sources) at(key string, n *projectgraph.Node) []Uncertainty { } u := Uncertainty{Kind: UnresolvedCall, Key: key, Site: r.Site, Text: fmt.Sprintf("%s (%s)", r.Text, r.Reason)} switch { + case r.Language != "": + u.Language = r.Language case r.Reason == fileindex.UnresolvedMarkup: u.Language = s.language(r.Site.Path) case r.Unit != "": @@ -323,7 +326,7 @@ func (f *found) forTarget(target Target) ([]Uncertainty, []Uncertainty, int) { } } for _, u := range f.byLanguage { - if f.speaks(u.Language, target) { + if f.speaks(u, target) { all = append(all, u) } } @@ -362,11 +365,16 @@ func (s *sources) language(path string) langcode.Code { return "" } -// speaks reports whether code of a language family calls a target: a node +// speaks reports whether the code of a site of a language scope can call +// a target: the file of the site imports the module of the target, a node // of the target is a definition in a file of the family, or an edge from // such a file leads to it. A target with no node keeps every uncertainty, // because no edge tells who calls it. -func (a *adjacency) speaks(lang langcode.Code, target Target) bool { +func (a *adjacency) speaks(u Uncertainty, target Target) bool { + lang := u.Language + if slices.ContainsFunc(a.g.Imports[u.Site.Path], func(module string) bool { return target.under(module) }) { + return true + } ids := a.match(target) if len(ids) == 0 { return true diff --git a/projectgraph/rootmatch/find_test.go b/projectgraph/rootmatch/find_test.go index dc4d2b72..2288423f 100644 --- a/projectgraph/rootmatch/find_test.go +++ b/projectgraph/rootmatch/find_test.go @@ -59,6 +59,7 @@ func testProject(root fstest.MapFS) Project { } defined := map[string]bool{} exports := map[string][]string{} + named := map[[2]string]string{} var paths []string for _, ix := range indexes { paths = append(paths, ix.Path) @@ -67,6 +68,7 @@ func testProject(root fstest.MapFS) Project { } for _, e := range ix.Exports { exports[ix.Path] = append(exports[ix.Path], e.Target) + named[[2]string{ix.Path, e.Name}] = e.Target } } files := modules.NewFiles(paths) @@ -83,12 +85,8 @@ func testProject(root fstest.MapFS) Project { Defined: func(key string) bool { return defined[key] }, Exports: func(p string) []string { return exports[p] }, Export: func(p, name string) (string, bool) { - for _, key := range exports[p] { - if fileindex.Last(key) == name { - return key, true - } - } - return "", false + key, ok := named[[2]string{p, name}] + return key, ok }, Resolve: func(_ *fileindex.Index, _ string, targets []string) []string { return targets }, ResolveName: func(p, name string) (string, bool) { diff --git a/projectgraph/rootmatch/framework.go b/projectgraph/rootmatch/framework.go index f0b8073c..b605c097 100644 --- a/projectgraph/rootmatch/framework.go +++ b/projectgraph/rootmatch/framework.go @@ -22,15 +22,27 @@ func (f *finder) matchPath(r rules.Rule) { if r.Kind == rules.Main { continue } - exports := f.Exports(ix.Path) - for _, key := range exports { - f.add(r, key) + f.addExports(r, ix) + } +} + +// addExports marks the exports of a file that a framework or a user of a +// library loads, the functions under an exported object, and each export +// whose value the index does not follow: an opaque value, as the result of +// a call, or a name that no record resolves, as GET of +// export const { GET } = handlers for an imported handlers. +func (f *finder) addExports(r rules.Rule, ix *fileindex.Index) { + exports := f.Exports(ix.Path) + for _, key := range exports { + f.add(r, key) + } + f.addMembers(r, ix, exports) + for _, ex := range ix.Exports { + if ex.From != "" { + continue } - f.addMembers(r, ix, exports) - for _, ex := range ix.Exports { - if ex.Opaque { - f.addOpaque(r, ix.Path, ex) - } + if _, ok := f.Export(ix.Path, ex.Name); ex.Opaque || !ok { + f.addOpaque(r, ix.Path, ex) } } } diff --git a/projectgraph/rootmatch/match.go b/projectgraph/rootmatch/match.go index 78ccb403..d141a93a 100644 --- a/projectgraph/rootmatch/match.go +++ b/projectgraph/rootmatch/match.go @@ -83,6 +83,7 @@ func Find(ctx context.Context, p Project, rs []rules.Rule) ([]Root, []modules.Pr f := &finder{ Project: p, languages: map[string]langcode.Code{}, + indexes: map[string]*fileindex.Index{}, seen: map[Root]bool{}, defs: map[string]*fileindex.Definition{}, calls: map[*fileindex.Call][]string{}, @@ -91,6 +92,7 @@ func Find(ctx context.Context, p Project, rs []rules.Rule) ([]Root, []modules.Pr } for _, ix := range p.Indexes { f.languages[ix.Path] = ix.Language + f.indexes[ix.Path] = ix for i := range ix.Definitions { f.defs[ix.Definitions[i].Key] = &ix.Definitions[i] } @@ -116,6 +118,7 @@ func state[T any](p Project, code langcode.Code) *T { type finder struct { Project languages map[string]langcode.Code + indexes map[string]*fileindex.Index defs map[string]*fileindex.Definition // calls holds the keys that each call resolves to. calls map[*fileindex.Call][]string @@ -314,10 +317,8 @@ func (f *finder) matchPackageJSON(r rules.Rule) { } for _, file := range f.entryFiles(js, pj.Dir, libraries) { f.addFile(r, file) - if lang, ok := f.languages[file]; ok && r.Applies(lang) { - for _, key := range f.Exports(file) { - f.add(r, key) - } + if ix := f.indexes[file]; ix != nil && r.Applies(ix.Language) { + f.addExports(r, ix) } } } diff --git a/projectgraph/roots.go b/projectgraph/roots.go index 9eab8dc7..b928c8a6 100644 --- a/projectgraph/roots.go +++ b/projectgraph/roots.go @@ -59,7 +59,9 @@ func (b *builder) markRoots(ctx context.Context) error { // export, and no edge names them. The record keeps a target that // only they call uncertain and not not-found. if r.Opaque != "" { - b.addUnresolved(UnresolvedSite{Caller: r.Key, Site: Site(r.Site), Text: "export " + r.Opaque, Reason: fileindex.UnresolvedComputedName}) + site := UnresolvedSite{Caller: r.Key, Site: Site(r.Site), Text: "export " + r.Opaque, Reason: fileindex.UnresolvedComputedName} + b.scopeToLanguage(b.byPath[r.Key], &site) + b.addUnresolved(site) continue } b.addRootAt(r.Key, r.Kind, r.Rule, Site(r.Site)) diff --git a/test/projectgraph/cases/javascript/library-opaque-export/expect.yaml b/test/projectgraph/cases/javascript/library-opaque-export/expect.yaml new file mode 100644 index 00000000..9413c513 --- /dev/null +++ b/test/projectgraph/cases/javascript/library-opaque-export/expect.yaml @@ -0,0 +1,38 @@ +source: safedep:hand +description: >- + The entry of the library exports api, which holds the result of the call makeApi. A user of the library + calls the functions of api, and no edge names them. So the manifest rule gives the export a + computed-name record of the JavaScript family, and a5 reads uncertain and not not-found. +roots: + - key: src/index.js + kind: manifest + rule: node.package-json + - key: src/index.js//api + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: src/index.js + to: src/index.js//makeApi + kind: direct + confidence: high + site: src/index.js:7 + - from: src/index.js//makeApi//get + to: plug//a5 + kind: direct + confidence: high + site: src/index.js:4 +unresolved: + - caller: src/index.js + site: src/index.js:9 + reason: computed-name +results: + - target: plug//a5 + status: uncertain + call_sites: + - caller: src/index.js//makeApi//get + site: src/index.js:4 + reached: false + uncertainties: + - kind: unresolved-call + site: src/index.js:9 diff --git a/test/projectgraph/cases/javascript/library-opaque-export/project/package.json b/test/projectgraph/cases/javascript/library-opaque-export/project/package.json new file mode 100644 index 00000000..624bc881 --- /dev/null +++ b/test/projectgraph/cases/javascript/library-opaque-export/project/package.json @@ -0,0 +1 @@ +{ "name": "library-opaque-export", "main": "src/index.js" } diff --git a/test/projectgraph/cases/javascript/library-opaque-export/project/src/index.js b/test/projectgraph/cases/javascript/library-opaque-export/project/src/index.js new file mode 100644 index 00000000..3ebbf0dd --- /dev/null +++ b/test/projectgraph/cases/javascript/library-opaque-export/project/src/index.js @@ -0,0 +1,9 @@ +import { a5 } from 'plug'; + +function makeApi() { + return { get() { a5(); } }; +} + +const api = makeApi(); + +export { api }; diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml index 4eb564ca..6fd9d475 100644 --- a/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml @@ -3,7 +3,9 @@ description: >- SvelteKit calls each function of the exported object actions of a +page.server.ts by its name, and no call of the graph names them. The rule node.sveltekit.route marks each function under an exported object of a route file, so a1 and a2 are found. The API route imports helper by the alias $lib, which SvelteKit - maps to src/lib of the package that depends on @sveltejs/kit, so the call of helper reaches c1. + maps to src/lib of the package that depends on @sveltejs/kit, so the call of helper reaches c1. The + object actions of items/+page.server.ts has a satisfies type, and its functions keep their names under + actions, so a6 is found. roots: - key: server.js kind: manifest @@ -26,6 +28,15 @@ roots: - key: src/routes/form/+page.server.ts//actions//save kind: framework rule: node.sveltekit.route + - key: src/routes/items/+page.server.ts + kind: framework + rule: node.sveltekit.route + - key: src/routes/items/+page.server.ts//actions + kind: framework + rule: node.sveltekit.route + - key: src/routes/items/+page.server.ts//actions//default + kind: framework + rule: node.sveltekit.route exact_edges: true edges: - from: server.js @@ -58,6 +69,11 @@ edges: kind: direct confidence: high site: src/routes/form/+page.server.ts:8 + - from: src/routes/items/+page.server.ts//actions//default + to: plug//a6 + kind: direct + confidence: high + site: src/routes/items/+page.server.ts:6 unresolved: [] results: - target: plug//a1 @@ -103,3 +119,16 @@ results: - caller: src/lib/helper.js//helper site: src/lib/helper.js:4 reached: true + - target: plug//a6 + status: found + path: + - key: src/routes/items/+page.server.ts//actions//default + root: framework + - key: plug//a6 + site: src/routes/items/+page.server.ts:6 + kind: direct + confidence: high + call_sites: + - caller: src/routes/items/+page.server.ts//actions//default + site: src/routes/items/+page.server.ts:6 + reached: true diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/items/+page.server.ts b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/items/+page.server.ts new file mode 100644 index 00000000..e0e18dd3 --- /dev/null +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/items/+page.server.ts @@ -0,0 +1,8 @@ +import type { Actions } from './$types'; +import { a6 } from 'plug'; + +export const actions = { + default: async () => { + a6(); + }, +} satisfies Actions; diff --git a/test/projectgraph/cases/python/component-in-frontend/expect.yaml b/test/projectgraph/cases/python/component-in-frontend/expect.yaml index 2bad80c6..53c85fe5 100644 --- a/test/projectgraph/cases/python/component-in-frontend/expect.yaml +++ b/test/projectgraph/cases/python/component-in-frontend/expect.yaml @@ -3,7 +3,9 @@ description: >- A Python service ships a Vue frontend. App.vue is a root of the rule node.component, and its markup record runs only JavaScript code. So the record concerns the targets that code of the JavaScript family calls, and os.remove, which only dead Python code calls, stays not-found. plug.other has no node, so no - edge tells who calls it, and the record keeps it uncertain. + edge tells who calls it, and the record keeps it uncertain. Only dead Python code calls + jsonschema.validate, but App.vue imports validate from jsonschema and its template calls it, so the + record keeps jsonschema.validate uncertain. roots: - key: frontend/src/App.vue kind: framework @@ -17,17 +19,22 @@ edges: to: plug//render kind: direct confidence: high - site: frontend/src/App.vue:4 + site: frontend/src/App.vue:5 - from: main.py to: shutil//rmtree kind: direct confidence: high - site: main.py:9 + site: main.py:12 - from: main.py//unused to: os//remove kind: direct confidence: high - site: main.py:6 + site: main.py:8 + - from: main.py//unused + to: jsonschema//validate + kind: direct + confidence: high + site: main.py:9 unresolved: - caller: frontend/src/App.vue site: frontend/src/App.vue:1 @@ -37,10 +44,19 @@ results: status: not-found call_sites: - caller: main.py//unused - site: main.py:6 + site: main.py:8 reached: false - target: plug//other status: uncertain uncertainties: - kind: unresolved-call site: frontend/src/App.vue:1 + - target: jsonschema//validate + status: uncertain + call_sites: + - caller: main.py//unused + site: main.py:9 + reached: false + uncertainties: + - kind: unresolved-call + site: frontend/src/App.vue:1 diff --git a/test/projectgraph/cases/python/component-in-frontend/project/frontend/src/App.vue b/test/projectgraph/cases/python/component-in-frontend/project/frontend/src/App.vue index ebaae70d..55a127b8 100644 --- a/test/projectgraph/cases/python/component-in-frontend/project/frontend/src/App.vue +++ b/test/projectgraph/cases/python/component-in-frontend/project/frontend/src/App.vue @@ -1,9 +1,10 @@ diff --git a/test/projectgraph/cases/python/component-in-frontend/project/main.py b/test/projectgraph/cases/python/component-in-frontend/project/main.py index 37b51646..dba04da7 100644 --- a/test/projectgraph/cases/python/component-in-frontend/project/main.py +++ b/test/projectgraph/cases/python/component-in-frontend/project/main.py @@ -1,9 +1,12 @@ import os import shutil +import jsonschema + def unused(): os.remove("x") + jsonschema.validate(1, {}) shutil.rmtree("y") diff --git a/test/projectgraph/cases/typescript/next-route-literals/expect.yaml b/test/projectgraph/cases/typescript/next-route-literals/expect.yaml new file mode 100644 index 00000000..0c5c0e2d --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-literals/expect.yaml @@ -0,0 +1,60 @@ +source: safedep:hand +description: >- + route.ts exports constants whose values run no call, as 60 * 60, 'force-dynamic' as const, -1, an + object with satisfies and an object that reads process.env inside a literal. They are no opaque exports + and get no record, so dead, which only the unused function of lib/db.ts calls, stays not-found. +roots: + - key: app/api/x/route.ts + kind: framework + rule: node.next.route + - key: app/api/x/route.ts//GET + kind: framework + rule: node.next.route + - key: app/api/x/route.ts//config + kind: framework + rule: node.next.route + - key: app/api/x/route.ts//dynamic + kind: framework + rule: node.next.route + - key: app/api/x/route.ts//maxDuration + kind: framework + rule: node.next.route + - key: app/api/x/route.ts//metadata + kind: framework + rule: node.next.route + - key: app/api/x/route.ts//revalidate + kind: framework + rule: node.next.route +exact_edges: true +edges: + - from: app/api/x/route.ts//GET + to: plug//a1 + kind: direct + confidence: high + site: app/api/x/route.ts:11 + - from: lib/db.ts//unused + to: plug//dead + kind: direct + confidence: high + site: lib/db.ts:4 +unresolved: [] +results: + - target: plug//a1 + status: found + path: + - key: app/api/x/route.ts//GET + root: framework + - key: plug//a1 + site: app/api/x/route.ts:11 + kind: direct + confidence: high + call_sites: + - caller: app/api/x/route.ts//GET + site: app/api/x/route.ts:11 + reached: true + - target: plug//dead + status: not-found + call_sites: + - caller: lib/db.ts//unused + site: lib/db.ts:4 + reached: false diff --git a/test/projectgraph/cases/typescript/next-route-literals/project/app/api/x/route.ts b/test/projectgraph/cases/typescript/next-route-literals/project/app/api/x/route.ts new file mode 100644 index 00000000..1b5c65d9 --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-literals/project/app/api/x/route.ts @@ -0,0 +1,12 @@ +import type { Metadata } from 'next'; +import { a1 } from 'plug'; + +export const revalidate = 60 * 60; +export const dynamic = 'force-dynamic' as const; +export const maxDuration = -1; +export const metadata = { title: 'x' } satisfies Metadata; +export const config = { api: { bodyParser: !process.env.RAW } }; + +export async function GET() { + a1(); +} diff --git a/test/projectgraph/cases/typescript/next-route-literals/project/lib/db.ts b/test/projectgraph/cases/typescript/next-route-literals/project/lib/db.ts new file mode 100644 index 00000000..096a7eb7 --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-literals/project/lib/db.ts @@ -0,0 +1,5 @@ +import { dead } from 'plug'; + +export function unused() { + dead(); +} diff --git a/test/projectgraph/cases/typescript/next-route-literals/project/package.json b/test/projectgraph/cases/typescript/next-route-literals/project/package.json new file mode 100644 index 00000000..5a4929a1 --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-literals/project/package.json @@ -0,0 +1 @@ +{ "name": "next-route-literals", "dependencies": { "next": "^14.2.0" } } diff --git a/test/projectgraph/cases/typescript/next-route-opaque/expect.yaml b/test/projectgraph/cases/typescript/next-route-opaque/expect.yaml new file mode 100644 index 00000000..d5f993bf --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-opaque/expect.yaml @@ -0,0 +1,77 @@ +source: safedep:hand +description: >- + app/api/auth/route.ts exports the result of the call NextAuth as its default, and app/api/x/route.ts + exports GET and POST from handlers, which lib/auth.ts makes by a call. Next.js calls the functions of + these values, and no edge names them. So each export gets a computed-name record of the JavaScript + family in place of a root, and a1 and a2 read uncertain and not not-found. +roots: + - key: app/api/auth/route.ts + kind: framework + rule: node.next.route + - key: app/api/x/route.ts + kind: framework + rule: node.next.route +exact_edges: true +edges: + - from: app/api/auth/route.ts + to: next-auth + kind: direct + confidence: high + site: app/api/auth/route.ts:4 + - from: app/api/auth/route.ts//callbacks//signIn + to: plug//a1 + kind: direct + confidence: high + site: app/api/auth/route.ts:4 + - from: app/api/x/route.ts + to: lib/auth.ts + kind: import + confidence: high + site: app/api/x/route.ts:1 + - from: lib/auth.ts + to: lib/auth.ts//makeAll + kind: direct + confidence: high + site: lib/auth.ts:7 + - from: lib/auth.ts//makeAll//GET + to: plug//a2 + kind: direct + confidence: high + site: lib/auth.ts:4 +unresolved: + - caller: app/api/auth/route.ts + site: app/api/auth/route.ts:4 + reason: computed-name + - caller: app/api/x/route.ts + site: app/api/x/route.ts:3 + reason: computed-name + - caller: app/api/x/route.ts + site: app/api/x/route.ts:3 + reason: computed-name +results: + - target: plug//a1 + status: uncertain + call_sites: + - caller: app/api/auth/route.ts//callbacks//signIn + site: app/api/auth/route.ts:4 + reached: false + uncertainties: + - kind: unresolved-call + site: app/api/auth/route.ts:4 + - kind: unresolved-call + site: app/api/x/route.ts:3 + - kind: unresolved-call + site: app/api/x/route.ts:3 + - target: plug//a2 + status: uncertain + call_sites: + - caller: lib/auth.ts//makeAll//GET + site: lib/auth.ts:4 + reached: false + uncertainties: + - kind: unresolved-call + site: app/api/auth/route.ts:4 + - kind: unresolved-call + site: app/api/x/route.ts:3 + - kind: unresolved-call + site: app/api/x/route.ts:3 diff --git a/test/projectgraph/cases/typescript/next-route-opaque/project/app/api/auth/route.ts b/test/projectgraph/cases/typescript/next-route-opaque/project/app/api/auth/route.ts new file mode 100644 index 00000000..3da95a6e --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-opaque/project/app/api/auth/route.ts @@ -0,0 +1,4 @@ +import NextAuth from 'next-auth'; +import { a1 } from 'plug'; + +export default NextAuth({ callbacks: { async signIn() { a1(); } } }); diff --git a/test/projectgraph/cases/typescript/next-route-opaque/project/app/api/x/route.ts b/test/projectgraph/cases/typescript/next-route-opaque/project/app/api/x/route.ts new file mode 100644 index 00000000..c7a254ca --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-opaque/project/app/api/x/route.ts @@ -0,0 +1,3 @@ +import { handlers } from '../../../lib/auth'; + +export const { GET, POST } = handlers; diff --git a/test/projectgraph/cases/typescript/next-route-opaque/project/lib/auth.ts b/test/projectgraph/cases/typescript/next-route-opaque/project/lib/auth.ts new file mode 100644 index 00000000..338cc5f5 --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-opaque/project/lib/auth.ts @@ -0,0 +1,7 @@ +import { a2 } from 'plug'; + +function makeAll() { + return { GET: async () => { a2(); } }; +} + +export const handlers = makeAll(); diff --git a/test/projectgraph/cases/typescript/next-route-opaque/project/package.json b/test/projectgraph/cases/typescript/next-route-opaque/project/package.json new file mode 100644 index 00000000..1f4c6a2f --- /dev/null +++ b/test/projectgraph/cases/typescript/next-route-opaque/project/package.json @@ -0,0 +1 @@ +{ "name": "next-route-opaque", "dependencies": { "next": "^14.2.0", "next-auth": "^4.24.0" } } From 3f47afa663b948c933b73e151c2c3905f7388581 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 20:26:12 +0000 Subject: [PATCH 51/86] fix(projectgraph): find more Python sites that make an instance, and fewer checks A classmethod whose first parameter is not cls, a call of cls in a function that the classmethod defines, self.__class__() and raise K made an instance that no edge named, so a target of __call__ or __getattr__ read as not-found. typing.cast(K, x), hasattr(K, "x") and a conditional in an isinstance argument linked the methods of K with no instance. The index records the first parameter of a classmethod as ClassParam, and the link step treats a call of it from the method or from a function that the method defines as a call of the class, and so a call of self.__class__ or type(self). raise K is a reference. The arguments of cast, hasattr and getattr, and a conditional in a check argument, are checks. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 12 +- fileindex/index.go | 12 +- plugin/indexer/python.go | 56 ++- projectgraph/definitions.go | 35 +- projectgraph/profile.go | 8 +- projectgraph/python.go | 2 +- .../instance-made-by-reference/expect.yaml | 325 ++++++++++++++++-- .../project/main.py | 58 ++++ 8 files changed, 444 insertions(+), 64 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index e6e0189d..5495160c 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -300,12 +300,18 @@ each edge that can make an instance of a Python class to the method of each of these names that the instance runs, from the class or from a base in its MRO. A call of the class makes an instance. So does code that gets the class as a value, as `functools.partial(C)`, `map(C, xs)` or -`field(default_factory=C)`, and a call of `cls` in a classmethod, which -holds the class of the method or a subclass. The edge can reach the class +`field(default_factory=C)`, and `raise C`. So does a call of the first +parameter of a classmethod, as `cls()` or `klass()`, also from a function +that the method defines, which holds the class of the method or a +subclass. The index records that parameter as `Definition.ClassParam`. A +call of `self.__class__` or `type(self)` makes an instance of the class of +the method or of a subclass too. The edge can reach the class through an export hop or the `package` edge of a unit. The code that makes the instance holds it, so a search from the roots reaches the method, as it reaches a stored function value. A class that code only -tests a value against, as in `isinstance(x, Proxy)`, makes no instance. +tests a value against or reads, as in `isinstance(x, Proxy)`, +`issubclass`, `typing.cast(Proxy, x)`, `hasattr(Proxy, "x")` or +`getattr`, also through a conditional, makes no instance. The index marks such a reference with `Check`, and it gets no edge. A call of a class also runs `__call__` of its metaclass, as `Meta` in `class Made(metaclass=Meta)`, which `Class.Metaclass` records. A call with diff --git a/fileindex/index.go b/fileindex/index.go index 45175b41..0adcd703 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -283,13 +283,16 @@ type Definition struct { Returns string `json:"returns,omitempty"` Results []string `json:"results,omitempty"` Wraps string `json:"wraps,omitempty"` - Static bool `json:"static,omitempty"` - Data bool `json:"data,omitempty"` + // ClassParam is the first parameter of a Python classmethod, which + // holds the class of the method or a subclass, as cls or klass. + ClassParam string `json:"class_param,omitempty"` + Static bool `json:"static,omitempty"` + Data bool `json:"data,omitempty"` // ValueData marks a Go function whose first result holds no function, // and a Go named type whose values hold no function, as []string or a // struct. A range over such a value reads data. - ValueData bool `json:"value_data,omitempty"` - Flags []Flag `json:"flags,omitempty"` + ValueData bool `json:"value_data,omitempty"` + Flags []Flag `json:"flags,omitempty"` // Alias is the type that a DefAlias names, as written, with no type // arguments, as B for type A = B. Alias string `json:"alias,omitempty"` @@ -696,6 +699,7 @@ func (ix *Index) EachString(fn func(string) string) { strs(def.Decorators) strs(def.Values) typed(def.Params) + kind(&def.ClassParam) typed(def.Locals) kind(&def.Returns) strs(def.Results) diff --git a/plugin/indexer/python.go b/plugin/indexer/python.go index f7d8b5be..a5771a04 100644 --- a/plugin/indexer/python.go +++ b/plugin/indexer/python.go @@ -59,6 +59,8 @@ var pythonRules = languageRules{ "conditional_expression": "", "boolean_operator": "", "parenthesized_expression": "", + // raise K makes an instance of K, as raise K() does. + "raise_statement": "", }, member: "attribute", object: "object", @@ -97,15 +99,29 @@ func pythonBases(p *projection, class *sitter.Node) []string { return bases } -// pythonChecks name the builtins that test a value against a class. -var pythonChecks = map[string]bool{"isinstance": true, "issubclass": true} +// pythonChecks name the functions that test a value against a class or +// read a class and make no instance of it: isinstance, issubclass, +// typing.cast, hasattr and getattr. +var pythonChecks = map[string]bool{ + "isinstance": true, "issubclass": true, "cast": true, "typing.cast": true, "hasattr": true, "getattr": true, +} + +// pythonCheckWrappers hold a value of an argument and give it on, as +// K if c else J or (K). +var pythonCheckWrappers = map[string]bool{"conditional_expression": true, "parenthesized_expression": true, "boolean_operator": true} // pythonCheck reports whether a holder holds classes that the code only -// tests a value against or names as a metaclass: the arguments of -// isinstance or issubclass, a tuple among them, or metaclass=M in a class -// statement. The class statement makes an instance of M, and a call of the -// class that it makes runs M.__call__, so the call of that class owns it. +// tests a value against or reads. Such a holder is the argument list of a +// function of pythonChecks, a tuple or a conditional in it, or +// metaclass=M in a class statement. A class statement makes an instance of +// M, and a call of the class that it makes runs M.__call__. So the call of +// that class owns the edge. func pythonCheck(p *projection, holder *sitter.Node) bool { + for pythonCheckWrappers[holder.Type()] { + if holder = holder.Parent(); holder == nil { + return false + } + } switch holder.Type() { case "keyword_argument": list := holder.Parent() @@ -205,6 +221,7 @@ func pythonSignature(p *projection, def *fileindex.Definition, node *sitter.Node return } def.Params = p.pythonParams(node.ChildByFieldName("parameters")) + def.ClassParam = p.pythonClassParam(node) if ret := node.ChildByFieldName("return_type"); ret != nil { def.Returns = p.pythonTypeName(ret) if results := p.pythonTupleTypes(ret); len(results) > 1 { @@ -214,6 +231,33 @@ func pythonSignature(p *projection, def *fileindex.Definition, node *sitter.Node def.Locals = p.pythonLocals(node.ChildByFieldName("body"), "") } +// pythonClassParam returns the name of the first parameter of a method +// that the decorator @classmethod marks, or "". +func (p *projection) pythonClassParam(node *sitter.Node) string { + decorated := node.Parent() + if decorated == nil || decorated.Type() != "decorated_definition" || !slices.ContainsFunc(ts.NamedChildren(decorated), func(d *sitter.Node) bool { + return d.Type() == "decorator" && p.text(d) == "@classmethod" + }) { + return "" + } + params := node.ChildByFieldName("parameters") + if params == nil || params.NamedChildCount() == 0 { + return "" + } + first := params.NamedChild(0) + switch first.Type() { + case "identifier": + return p.text(first) + case "typed_parameter": + if first.NamedChildCount() > 0 && first.NamedChild(0).Type() == "identifier" { + return p.text(first.NamedChild(0)) + } + case "default_parameter", "typed_default_parameter": + return p.text(first.ChildByFieldName("name")) + } + return "" +} + // pythonParams returns the parameters of a function that declare a type // name, as parser in run(parser: Parser) or run(parser: Parser = None). func (p *projection) pythonParams(list *sitter.Node) []fileindex.Typed { diff --git a/projectgraph/definitions.go b/projectgraph/definitions.go index 30e76f61..1bb3fe2d 100644 --- a/projectgraph/definitions.go +++ b/projectgraph/definitions.go @@ -238,30 +238,43 @@ type classCall struct { site Site } -// classCalls returns each call of the first parameter of a classmethod, -// which holds the class of the method or a subclass of it. +// classCalls returns each call that makes an instance of the class of the +// method that holds it, or of a subclass: a call of the first parameter of +// a classmethod, also from a function that the method defines, and a call +// of the class of the instance, as self.__class__() or type(self)(). func (b *builder) classCalls() []classCall { var out []classCall for _, p := range b.paths { ix := b.byPath[p] - if ix == nil { - continue - } - param := profileOf(ix.Language).classParam - if param == "" { + if ix == nil || len(profileOf(ix.Language).instanceClass) == 0 { continue } for _, call := range ix.Calls { - def := b.defs[call.Caller] - if call.Callee != param || def == nil || def.Parent == "" || !slices.Contains(def.Decorators, "@classmethod") { - continue + if class, ok := b.callsClass(ix, call); ok { + out = append(out, classCall{from: b.runner(call.Caller), class: class, site: Site{Path: ix.Path, Line: call.Line, Column: call.Column}}) } - out = append(out, classCall{from: b.runner(call.Caller), class: def.Parent, site: Site{Path: ix.Path, Line: call.Line, Column: call.Column}}) } } return out } +// callsClass returns the class that a call makes an instance of, by the +// method that holds the call. +func (b *builder) callsClass(ix *fileindex.Index, call fileindex.Call) (string, bool) { + instance := slices.Contains(profileOf(ix.Language).instanceClass, call.Callee) + for key := call.Caller; key != "" && key != ix.Path; key = fileindex.Parent(key) { + def := b.defs[key] + if def == nil || def.Parent == "" || b.kindOf(def.Parent) != NodeClass { + continue + } + if instance || def.ClassParam != "" && call.Callee == def.ClassParam { + return def.Parent, true + } + return "", false + } + return "", false +} + // linkOverrides adds a reference edge from each class with a base of a // dependency to the method that an instance of the class runs for each // method name of the first-party classes of its MRO, as run of a subclass diff --git a/projectgraph/profile.go b/projectgraph/profile.go index 7c594f25..45a00dd1 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -186,9 +186,11 @@ type profile struct { // metaclassCall names the method of a metaclass that a call of a class // runs, as __call__ of Python. metaclassCall string - // classParam names the first parameter of a class method, which holds - // the class, as cls of Python. A call of it makes an instance. - classParam string + // instanceClass names the callees that give the class of the instance + // of a method, as self.__class__ of Python. A call of one makes an + // instance, and so does a call of the class parameter of a class + // method, which the index records. + instanceClass []string // forwardMethod names the method of a class that forwards a call that // the class has no method for to another type, as deref of Rust. A // declared type with it does not narrow the receiver. diff --git a/projectgraph/python.go b/projectgraph/python.go index 4e6c303f..29958683 100644 --- a/projectgraph/python.go +++ b/projectgraph/python.go @@ -34,7 +34,7 @@ var pythonProfile = profile{ starImports: true, instanceMethods: pythonInstanceMethods, metaclassCall: "__call__", - classParam: "cls", + instanceClass: []string{"self.__class__", "type(self)"}, } // pythonInstanceMethods names the methods that Python runs on an instance diff --git a/test/projectgraph/cases/python/instance-made-by-reference/expect.yaml b/test/projectgraph/cases/python/instance-made-by-reference/expect.yaml index 6127238d..067b1445 100644 --- a/test/projectgraph/cases/python/instance-made-by-reference/expect.yaml +++ b/test/projectgraph/cases/python/instance-made-by-reference/expect.yaml @@ -2,9 +2,13 @@ source: safedep:hand description: >- Each class makes an instance in a way other than a call of its name. cls() in the classmethod create calls the class of the method. functools.partial and field(default_factory=...) get the class as a - value and call it. So each of these sites links the __call__ or __getattr__ of the class, and os.remove, - shutil.rmtree and subprocess.run are found. check names Meta only in isinstance, which makes no - instance, and no class names Meta as its metaclass, so tempfile.mkdtemp in Meta.__call__ is not-found. + value and call it. klass() in a function that the classmethod build defines calls its first parameter. + self.__class__() in clone and type(self)() in dup call the class of the instance, and copies calls + clone and dup on a parameter, and raise Raised makes an instance of + Raised. So each of these sites links the __call__ or __getattr__ of the class, and the os targets of + those classes are found. check names Meta only in isinstance, and check_more names Checked only in + typing.cast, hasattr and a conditional in isinstance, which make no instance. So tempfile.mkdtemp and + os.mkdir are not-found. roots: - key: main.py kind: main @@ -15,167 +19,416 @@ edges: to: dataclasses//dataclass kind: direct confidence: high - site: main.py:29 + site: main.py:30 - from: main.py to: dataclasses//field kind: direct confidence: high - site: main.py:30 + site: main.py:31 - from: main.py to: main.py//ByDefault kind: reference confidence: low - site: main.py:30 + site: main.py:31 - from: main.py to: main.py//ByDefault//__getattr__ kind: reference confidence: low - site: main.py:30 + site: main.py:31 - from: main.py to: main.py//run kind: direct confidence: high - site: main.py:47 + site: main.py:101 - from: main.py to: main.py//ByClassmethod//create kind: member confidence: high - site: main.py:47 + site: main.py:101 - from: main.py to: main.py//run kind: direct confidence: high - site: main.py:48 + site: main.py:102 - from: main.py to: functools//partial kind: direct confidence: high - site: main.py:48 + site: main.py:102 - from: main.py to: main.py//ByPartial kind: reference confidence: low - site: main.py:48 + site: main.py:102 - from: main.py to: main.py//ByPartial//__call__ kind: reference confidence: low - site: main.py:48 + site: main.py:102 - from: main.py to: print kind: direct confidence: high - site: main.py:49 + site: main.py:103 - from: main.py to: main.py//Holder kind: direct confidence: high - site: main.py:49 + site: main.py:103 - from: main.py to: main.py//check kind: direct confidence: high - site: main.py:50 + site: main.py:104 + - from: main.py + to: main.py//run + kind: direct + confidence: high + site: main.py:105 + - from: main.py + to: main.py//ByKlass//build + kind: member + confidence: high + site: main.py:105 + - from: main.py + to: main.py//copies + kind: direct + confidence: high + site: main.py:106 + - from: main.py + to: main.py//fail + kind: direct + confidence: high + site: main.py:107 + - from: main.py + to: main.py//check_more + kind: direct + confidence: high + site: main.py:108 - from: main.py//ByClassmethod//__call__ to: os//remove kind: direct confidence: high - site: main.py:11 + site: main.py:12 - from: main.py//ByClassmethod//create to: main.py//ByClassmethod//__call__ kind: reference confidence: low - site: main.py:15 + site: main.py:16 - from: main.py//ByDefault//__getattr__ to: subprocess//run kind: direct confidence: high - site: main.py:25 + site: main.py:26 + - from: main.py//ByInstanceClass//__getattr__ + to: os//rename + kind: direct + confidence: high + site: main.py:53 + - from: main.py//ByInstanceClass//clone + to: main.py//ByInstanceClass//__getattr__ + kind: reference + confidence: low + site: main.py:56 + - from: main.py//ByKlass//__call__ + to: os//chmod + kind: direct + confidence: high + site: main.py:41 + - from: main.py//ByKlass//build + to: main.py//ByKlass//build//inner + kind: direct + confidence: high + site: main.py:48 + - from: main.py//ByKlass//build//inner + to: main.py//ByKlass//__call__ + kind: reference + confidence: low + site: main.py:46 - from: main.py//ByPartial//__call__ to: shutil//rmtree kind: direct confidence: high - site: main.py:20 + site: main.py:21 + - from: main.py//ByType//__call__ + to: os//rmdir + kind: direct + confidence: high + site: main.py:61 + - from: main.py//ByType//dup + to: type + kind: direct + confidence: high + site: main.py:64 + - from: main.py//ByType//dup + to: main.py//ByType + kind: reference + confidence: low + site: main.py:64 + - from: main.py//ByType//dup + to: main.py//ByType//__call__ + kind: reference + confidence: low + site: main.py:64 + - from: main.py//Checked//__call__ + to: os//mkdir + kind: direct + confidence: high + site: main.py:74 - from: main.py//Meta//__call__ to: tempfile//mkdtemp kind: direct confidence: high - site: main.py:35 + site: main.py:36 + - from: main.py//Raised//__getattr__ + to: os//unlink + kind: direct + confidence: high + site: main.py:69 - from: main.py//check to: isinstance kind: direct confidence: high - site: main.py:43 + site: main.py:97 - from: main.py//check to: main.py//Meta kind: reference confidence: low - site: main.py:43 + site: main.py:97 + - from: main.py//check_more + to: typing//cast + kind: direct + confidence: high + site: main.py:91 + - from: main.py//check_more + to: main.py//Checked + kind: reference + confidence: low + site: main.py:91 + - from: main.py//check_more + to: hasattr + kind: direct + confidence: high + site: main.py:92 + - from: main.py//check_more + to: main.py//Checked + kind: reference + confidence: low + site: main.py:92 + - from: main.py//check_more + to: isinstance + kind: direct + confidence: high + site: main.py:93 + - from: main.py//check_more + to: main.py//Checked + kind: reference + confidence: low + site: main.py:93 + - from: main.py//check_more + to: main.py//Meta + kind: reference + confidence: low + site: main.py:93 + - from: main.py//copies + to: main.py//ByInstanceClass//clone + kind: name + confidence: low + site: main.py:86 + - from: main.py//copies + to: main.py//ByType//dup + kind: name + confidence: low + site: main.py:87 + - from: main.py//fail + to: main.py//Raised + kind: reference + confidence: low + site: main.py:82 + - from: main.py//fail + to: main.py//Raised//__getattr__ + kind: reference + confidence: low + site: main.py:82 unresolved: - caller: main.py//ByClassmethod//create - site: main.py:15 + site: main.py:16 reason: unknown-receiver method: cls + - caller: main.py//ByKlass//build//inner + site: main.py:46 + reason: unknown-receiver + method: klass + - caller: main.py//ByInstanceClass//clone + site: main.py:56 + reason: no-definition + method: __class__ - caller: main.py//run - site: main.py:39 + site: main.py:78 reason: unknown-receiver method: f + - caller: main.py//copies + site: main.py:86 + reason: unknown-receiver + method: clone + - caller: main.py//copies + site: main.py:87 + reason: unknown-receiver + method: dup results: - target: os//remove status: found path: - key: main.py - key: main.py//ByClassmethod//create - site: main.py:47 + site: main.py:101 kind: member confidence: high - key: main.py//ByClassmethod//__call__ - site: main.py:15 + site: main.py:16 kind: reference confidence: low - key: os//remove - site: main.py:11 + site: main.py:12 kind: direct confidence: high call_sites: - caller: main.py//ByClassmethod//__call__ - site: main.py:11 + site: main.py:12 reached: true - target: shutil//rmtree status: found path: - key: main.py - key: main.py//ByPartial//__call__ - site: main.py:48 + site: main.py:102 kind: reference confidence: low - key: shutil//rmtree - site: main.py:20 + site: main.py:21 kind: direct confidence: high call_sites: - caller: main.py//ByPartial//__call__ - site: main.py:20 + site: main.py:21 reached: true - target: subprocess//run status: found path: - key: main.py - key: main.py//ByDefault//__getattr__ - site: main.py:30 + site: main.py:31 kind: reference confidence: low - key: subprocess//run - site: main.py:25 + site: main.py:26 kind: direct confidence: high call_sites: - caller: main.py//ByDefault//__getattr__ - site: main.py:25 + site: main.py:26 reached: true - target: tempfile//mkdtemp status: not-found call_sites: - caller: main.py//Meta//__call__ - site: main.py:35 + site: main.py:36 + reached: false + - target: os//chmod + status: found + path: + - key: main.py + - key: main.py//ByKlass//build + site: main.py:105 + kind: member + confidence: high + - key: main.py//ByKlass//build//inner + site: main.py:48 + kind: direct + confidence: high + - key: main.py//ByKlass//__call__ + site: main.py:46 + kind: reference + confidence: low + - key: os//chmod + site: main.py:41 + kind: direct + confidence: high + call_sites: + - caller: main.py//ByKlass//__call__ + site: main.py:41 + reached: true + - target: os//rename + status: found + path: + - key: main.py + - key: main.py//copies + site: main.py:106 + kind: direct + confidence: high + - key: main.py//ByInstanceClass//clone + site: main.py:86 + kind: name + confidence: low + - key: main.py//ByInstanceClass//__getattr__ + site: main.py:56 + kind: reference + confidence: low + - key: os//rename + site: main.py:53 + kind: direct + confidence: high + call_sites: + - caller: main.py//ByInstanceClass//__getattr__ + site: main.py:53 + reached: true + - target: os//rmdir + status: found + path: + - key: main.py + - key: main.py//copies + site: main.py:106 + kind: direct + confidence: high + - key: main.py//ByType//dup + site: main.py:87 + kind: name + confidence: low + - key: main.py//ByType//__call__ + site: main.py:64 + kind: reference + confidence: low + - key: os//rmdir + site: main.py:61 + kind: direct + confidence: high + call_sites: + - caller: main.py//ByType//__call__ + site: main.py:61 + reached: true + - target: os//unlink + status: found + path: + - key: main.py + - key: main.py//fail + site: main.py:107 + kind: direct + confidence: high + - key: main.py//Raised//__getattr__ + site: main.py:82 + kind: reference + confidence: low + - key: os//unlink + site: main.py:69 + kind: direct + confidence: high + call_sites: + - caller: main.py//Raised//__getattr__ + site: main.py:69 + reached: true + - target: os//mkdir + status: not-found + call_sites: + - caller: main.py//Checked//__call__ + site: main.py:74 reached: false diff --git a/test/projectgraph/cases/python/instance-made-by-reference/project/main.py b/test/projectgraph/cases/python/instance-made-by-reference/project/main.py index 08915d0c..1fde5b54 100644 --- a/test/projectgraph/cases/python/instance-made-by-reference/project/main.py +++ b/test/projectgraph/cases/python/instance-made-by-reference/project/main.py @@ -3,6 +3,7 @@ import shutil import subprocess import tempfile +import typing from dataclasses import dataclass, field @@ -35,10 +36,63 @@ def __call__(cls, *args): tempfile.mkdtemp() +class ByKlass: + def __call__(self): + os.chmod("e", 0) + + @classmethod + def build(klass): + def inner(): + return klass() + + return inner() + + +class ByInstanceClass: + def __getattr__(self, name): + os.rename("f", "g") + + def clone(self): + return self.__class__() + + +class ByType: + def __call__(self): + os.rmdir("h") + + def dup(self): + return type(self)() + + +class Raised(Exception): + def __getattr__(self, name): + os.unlink("i") + + +class Checked: + def __call__(self): + os.mkdir("j") + + def run(f): f() +def fail(): + raise Raised + + +def copies(seed): + seed.clone() + seed.dup() + + +def check_more(x, c): + typing.cast(Checked, x) + hasattr(Checked, "x") + return isinstance(x, Checked if c else Meta) + + def check(x): return isinstance(x, Meta) @@ -48,3 +102,7 @@ def check(x): run(functools.partial(ByPartial)()) print(Holder().d.anything) check(1) + run(ByKlass.build()) + copies(None) + fail() + check_more(1, True) From 250b4d094c3b3de9cd9d2603f0244bd9d4d9d192 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 20:52:04 +0000 Subject: [PATCH 52/86] perf(projectgraph): keep the external base of a search that started idle externalBase kept an answer only when no cut happened under its search. On the cookie crate almost every search met a cut of its own guard, so almost no answer stayed, and the link step went from 0.1 s to 14 s. A search that starts with no cycle guard in progress meets only the guards that it sets itself, so a cut under it loses nothing of its answer. The answer of such a search now stays too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/cha.go | 7 ++++--- projectgraph/resolve.go | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/projectgraph/cha.go b/projectgraph/cha.go index 45d5afe0..f621d44b 100644 --- a/projectgraph/cha.go +++ b/projectgraph/cha.go @@ -183,10 +183,11 @@ func (b *builder) externalBase(class string) (string, bool) { base, ok := b.externalBases[class] if !ok { // A search that the guard of an outer search cut can miss a base, - // so only an uncut answer stays for the build. - cuts := b.cuts + // so only an uncut answer, or the answer of a search that started + // idle, stays for the build. + idle, cuts := b.idle(), b.cuts base, _ = b.findExternalBase(class) - if b.cuts == cuts { + if idle || b.cuts == cuts { b.externalBases[class] = base b.memo(func() { delete(b.externalBases, class) }) } diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 72f3b92c..915dd883 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -623,6 +623,16 @@ func (b *builder) undo(mark int) { b.tentative = b.tentative[:mark] } +// idle reports whether no cycle guard is in progress. A search that starts +// idle meets only the guards that it sets itself. A cut under it ends a +// cycle back through the search, and the answer of the search loses +// nothing by it. +func (b *builder) idle() bool { + s := b.rustState() + return len(b.expanding)+len(b.visiting)+len(b.reading)+len(b.stepping)+len(b.findingBases)+ + len(s.routing)+b.ordering+len(s.resolving)+len(s.members)+len(s.globbing) == 0 +} + // uncut reports whether a read of the wildcard imports of a scope that // asks the resolving guard for the paths gets no answer from a resolution // in progress. Such a read gives the same modules each time. The guards From d45b96d5065cec019a818011b0949120f40e1da3 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 21:09:23 +0000 Subject: [PATCH 53/86] fix(projectgraph): keep no stale Rust glob answer, and keep glob paths by low link A glob member that a cut of an outer lookup left incomplete stayed in the visited table after that lookup ended. A later read under another depth took its low for its own, passed the low link check, and kept an answer with no item for the build. A lookup that stays for the build now drops the visited answers under it, and keeps those that found their item. A lookup that does not stay lowers the low of the answers under it to its own. A glob path kept no answer under any cut, so a ring of crates that glob use each other read the ring once for each path round it, and six crates took minutes. Glob paths and glob members now share one lookup with the low link rule. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/resolve.go | 2 +- projectgraph/rust.go | 188 +++++++++++------- .../rust/glob-use-stale-visit/expect.yaml | 40 ++++ .../glob-use-stale-visit/project/Cargo.toml | 4 + .../glob-use-stale-visit/project/src/a.rs | 1 + .../glob-use-stale-visit/project/src/b.rs | 1 + .../glob-use-stale-visit/project/src/c.rs | 2 + .../glob-use-stale-visit/project/src/d.rs | 1 + .../glob-use-stale-visit/project/src/e.rs | 1 + .../glob-use-stale-visit/project/src/hub.rs | 2 + .../glob-use-stale-visit/project/src/leaf.rs | 3 + .../glob-use-stale-visit/project/src/main.rs | 13 ++ .../glob-use-stale-visit/project/src/other.rs | 3 + test/projectgraph/deadline_test.go | 58 ++++++ 14 files changed, 242 insertions(+), 77 deletions(-) create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/src/a.rs create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/src/b.rs create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/src/c.rs create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/src/d.rs create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/src/e.rs create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/src/hub.rs create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/src/leaf.rs create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use-stale-visit/project/src/other.rs diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 915dd883..afd243da 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -630,7 +630,7 @@ func (b *builder) undo(mark int) { func (b *builder) idle() bool { s := b.rustState() return len(b.expanding)+len(b.visiting)+len(b.reading)+len(b.stepping)+len(b.findingBases)+ - len(s.routing)+b.ordering+len(s.resolving)+len(s.members)+len(s.globbing) == 0 + len(s.routing)+b.ordering+len(s.resolving)+len(s.active) == 0 } // uncut reports whether a read of the wildcard imports of a scope that diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 3b1c0e61..b52fc116 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -158,19 +158,18 @@ type rustState struct { globs map[string][]string macros map[*space]map[string]string nested map[string]bool - globPaths map[[2]string]globPath - // globbing and members hold the depth of each glob path and glob - // member that a lookup reads now. depth is the depth of the last one, - // and low the least depth that a cut of the read in progress hit. - globbing map[[2]string]int - members map[[2]string]int - depth int - low int - // memberAnswers holds the answers of rustGlobUnitMember that no cut of - // an outer read gave, and visited the other answers of the lookup in - // progress. - memberAnswers map[[2]string]memberAnswer - visited map[[2]string]memberAnswer + // active holds the depth of each glob path and glob member that a + // lookup reads now. depth is the depth of the last one, and low the + // least depth that a cut of the read in progress hit. + active map[globKey]int + depth int + low int + // kept holds the answers of the glob lookups that no cut of an outer + // read gave, for the build. visited holds the other answers of the + // outermost lookup in progress, and visits the order of their reads. + kept map[globKey]globAnswer + visited map[globKey]globAnswer + visits []globKey // useScopes holds the classes of each file that hold a use, which are // inline modules. useScopes map[string]map[string]bool @@ -215,12 +214,10 @@ func (b *builder) addRustUnits() { s.routing = map[[2]string]bool{} s.routes = map[[2]string]crateRoute{} s.globs = map[string][]string{} - s.globbing = map[[2]string]int{} + s.active = map[globKey]int{} s.low = math.MaxInt - s.visited = map[[2]string]memberAnswer{} - s.globPaths = map[[2]string]globPath{} - s.members = map[[2]string]int{} - s.memberAnswers = map[[2]string]memberAnswer{} + s.kept = map[globKey]globAnswer{} + s.visited = map[globKey]globAnswer{} s.useScopes = map[string]map[string]bool{} s.spaceNames = map[*space]map[string]bool{} files := map[string][]string{} @@ -936,27 +933,10 @@ func (b *builder) rustGlobPath(importer, written string) (string, string, bool) case "crate", "self", "super": return "", "", false } - s := b.rustState() - k := [2]string{importer, first} - if g, ok := s.globPaths[k]; ok { - return g.join(written) - } - if d, ok := s.globbing[k]; ok { - b.globCut(d) - return "", "", false - } - s.depth++ - s.globbing[k] = s.depth - cuts := b.cuts - g := b.findGlobPath(sc, first) - delete(s.globbing, k) - s.depth-- - // The answer reads providers, so it follows the rule of that memo. - if b.cuts == cuts { - s.globPaths[k] = g - b.memo(func() { delete(s.globPaths, k) }) - } - return g.join(written) + a := b.globLookup(globKey{path: true, a: importer, b: first}, func() globAnswer { + return globAnswer{path: b.findGlobPath(sc, first)} + }) + return a.path.join(written) } // rustPublic reports whether a provision gives an item that a glob use of @@ -1055,48 +1035,112 @@ func (b *builder) rustGlobMember(sc *scope, name string) (string, bool) { // that no definition of the module has: a child module, or the target of a // use or of a glob use of the module, as leaf for use crate::sub::* with // pub mod leaf in sub. -// -// Modules can bring each other with glob uses, in a diamond or in a cycle. -// A walk of each path through them grows with the number of paths. So one -// lookup reads each module once, and a module that it reads again gives -// its first answer. A read of a module that the lookup reads now is a cut. -// An answer stays for the build when each cut under it hit the module or a -// module under it, as the low link of Tarjan tells. Such a cut leaves out -// no path, because the module in progress reads that path itself. func (b *builder) rustGlobUnitMember(mod, name string) (string, bool) { + a := b.globLookup(globKey{a: mod, b: name}, func() globAnswer { + key, ok := b.unitMember(mod, name) + return globAnswer{key: key, ok: ok} + }) + return a.key, a.ok +} + +// globKey names a glob lookup: the glob path of a file and a first name, or +// the glob member of a module and a name. +type globKey struct { + path bool + a, b string +} + +// globAnswer is the answer of a glob lookup, with the least depth that a cut +// under it hit. +type globAnswer struct { + key string + ok bool + path globPath + low int +} + +// found reports whether a glob lookup found its item. +func (a globAnswer) found() bool { + return a.ok || a.path.ok +} + +// keep keeps the answer of a glob lookup for the build. +func (b *builder) keep(k globKey, a globAnswer) { s := b.rustState() - k := [2]string{mod, name} - if m, ok := s.memberAnswers[k]; ok { - return m.key, m.ok + s.kept[k] = a + b.memo(func() { delete(s.kept, k) }) +} + +// globLookup reads a glob lookup once. Modules and crates can bring each +// other with glob uses, in a diamond or in a cycle, and a walk of each path +// through them grows with the number of paths. So the outermost lookup +// reads each lookup once, and a lookup that it reads again gives its first +// answer. A read of a lookup in progress is a cut. An answer stays for the +// build when each cut under it hit the lookup or a lookup under it, as the +// low link of Tarjan tells, and when no other guard cut it. Such a cut +// leaves out no path, because the lookup in progress reads that path +// itself. +// +// A visited answer is part of the lookup that read it. When that lookup +// stays for the build, a visited answer under it that found its item stays +// too, so a later read takes the same way through a cycle of crates. A +// later read of dep_3 of a cycle dep_0 to dep_3 with pub use dep_d::* in +// dep_3 took dep_0 first and sent the cycle round with no end. Any other +// visited answer under it goes, because a later read with another depth +// would take its low for its own, and it can lack an item that the lookup +// found after it. When the lookup does not stay, their low falls to its +// low, because they lack what it lacks. +func (b *builder) globLookup(k globKey, read func() globAnswer) globAnswer { + s := b.rustState() + if a, ok := s.kept[k]; ok { + return a } - if d, ok := s.members[k]; ok { + if d, ok := s.active[k]; ok { b.globCut(d) - return "", false + return globAnswer{} } - if m, ok := s.visited[k]; ok { - b.globCut(m.low) - return m.key, m.ok + if a, ok := s.visited[k]; ok { + b.globCut(a.low) + return a } - if len(s.members) == 0 { - defer clear(s.visited) + if len(s.active) == 0 { + defer func() { + clear(s.visited) + s.visits = s.visits[:0] + }() } s.depth++ d := s.depth - s.members[k] = d - low, cuts := s.low, b.cuts-s.cuts + s.active[k] = d + low, cuts, mark := s.low, b.cuts-s.cuts, len(s.visits) s.low = math.MaxInt - key, ok := b.unitMember(mod, name) - m := memberAnswer{key: key, ok: ok, low: s.low} + a := read() + a.low = s.low s.low = min(low, s.low) - delete(s.members, k) + delete(s.active, k) s.depth-- - if m.low >= d && b.cuts-s.cuts == cuts { - s.memberAnswers[k] = m - b.memo(func() { delete(s.memberAnswers, k) }) - } else { - s.visited[k] = m + under := s.visits[mark:] + if a.low >= d && b.cuts-s.cuts == cuts { + b.keep(k, a) + for _, v := range under { + if e, ok := s.visited[v]; ok && e.found() && e.low >= d { + b.keep(v, e) + } + delete(s.visited, v) + } + s.visits = s.visits[:mark] + return a } - return key, ok + for _, v := range under { + if e, ok := s.visited[v]; ok && e.low > a.low { + e.low = a.low + s.visited[v] = e + } + } + s.visited[k] = a + s.visits = append(s.visits, k) + b.memo(func() { delete(s.visited, k) }) + return a } // globCut counts a read that the guard of a glob path or of a glob member @@ -1123,14 +1167,6 @@ func (b *builder) globRead() func() bool { } } -// memberAnswer is the answer of rustGlobUnitMember for a module and a name, -// with the least depth that a cut under it hit. -type memberAnswer struct { - key string - ok bool - low int -} - // rustUnitFile reports whether a file is Rust code of a unit of dependency // code. func (b *builder) rustUnitFile(ix *fileindex.Index) bool { diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/expect.yaml b/test/projectgraph/cases/rust/glob-use-stale-visit/expect.yaml new file mode 100644 index 00000000..795d412d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/expect.yaml @@ -0,0 +1,40 @@ +source: safedep:hand +description: >- + The modules a, b, c, d, e and hub bring each other's names with glob uses, and leaf defines f. main first + calls e::f(), whose lookup reads c, hub, a, leaf and b, and then other::go() calls crate::d::f(). d brings f + through b, a and hub from leaf. The first lookup reads b while hub is in progress and finds no f there, so the + answer of b is not whole. A link step that keeps that answer of b for the lookup of d, under another depth, + keeps the lookup of d with no f for the build, and std::fs::remove_file reads not-found from other::go. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/other.rs//go + to: src/leaf.rs//f + kind: direct + confidence: high + site: src/other.rs:2 +unresolved: [] +results: + - target: std//fs//remove_file + status: found + sources: [src/other.rs//go] + sources_only: true + path: + - key: src/other.rs//go + - key: src/leaf.rs//f + site: src/other.rs:2 + kind: direct + confidence: high + - key: std//fs//remove_file + site: src/leaf.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/leaf.rs//f + site: src/leaf.rs:2 + reached: true diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-stale-visit/project/Cargo.toml new file mode 100644 index 00000000..305c41cb --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/a.rs b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/a.rs new file mode 100644 index 00000000..c96a8934 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/a.rs @@ -0,0 +1 @@ +pub use crate::hub::*; diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/b.rs b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/b.rs new file mode 100644 index 00000000..ecd3bddd --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/b.rs @@ -0,0 +1 @@ +pub use crate::a::*; diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/c.rs b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/c.rs new file mode 100644 index 00000000..9288b729 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/c.rs @@ -0,0 +1,2 @@ +pub use crate::hub::*; +pub use crate::b::*; diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/d.rs b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/d.rs new file mode 100644 index 00000000..e6586633 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/d.rs @@ -0,0 +1 @@ +pub use crate::b::*; diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/e.rs b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/e.rs new file mode 100644 index 00000000..a3122f94 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/e.rs @@ -0,0 +1 @@ +pub use crate::c::*; diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/hub.rs b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/hub.rs new file mode 100644 index 00000000..b40036e2 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/hub.rs @@ -0,0 +1,2 @@ +pub use crate::a::*; +pub use crate::leaf::*; diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/leaf.rs b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/leaf.rs new file mode 100644 index 00000000..723fa28f --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/leaf.rs @@ -0,0 +1,3 @@ +pub fn f() { + let _ = std::fs::remove_file("a"); +} diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/main.rs new file mode 100644 index 00000000..e5c84785 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/main.rs @@ -0,0 +1,13 @@ +mod a; +mod b; +mod c; +mod d; +mod e; +mod hub; +mod leaf; +mod other; + +fn main() { + e::f(); + other::go(); +} diff --git a/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/other.rs b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/other.rs new file mode 100644 index 00000000..ca65a72d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-stale-visit/project/src/other.rs @@ -0,0 +1,3 @@ +pub fn go() { + crate::d::f(); +} diff --git a/test/projectgraph/deadline_test.go b/test/projectgraph/deadline_test.go index 134c6787..362e03d1 100644 --- a/test/projectgraph/deadline_test.go +++ b/test/projectgraph/deadline_test.go @@ -121,3 +121,61 @@ func TestBuildReadsEachGlobModuleOncePerName(t *testing.T) { }) } } + +// TestBuildReadsEachGlobPathOnceInARingOfCrates links six crates of +// dependency code whose roots glob use the next crate, the last one back +// to the first, and a crate leaf that the last one also glob uses. Each +// file of the project glob uses one crate of the ring and calls n::f of +// leaf. A glob path that kept no answer under a cut read the ring once for +// each path round it, and the build did not end within minutes. +func TestBuildReadsEachGlobPathOnceInARingOfCrates(t *testing.T) { + const crates = 6 + dir := t.TempDir() + write := func(name, text string) { + require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(text), 0o644)) + } + manifest := func(name string, deps ...string) string { + var m strings.Builder + fmt.Fprintf(&m, "[package]\nname = \"%s\"\nversion = \"1.0.0\"\nedition = \"2021\"\n\n[dependencies]\n", name) + for _, d := range deps { + fmt.Fprintf(&m, "%s = \"1.0.0\"\n", d) + } + return m.String() + } + want := expectation{Units: map[string]string{"vendor/leaf": "cargo:leaf@1.0.0"}} + var main, deps []string + for i := range crates { + name, next := fmt.Sprintf("ring_%d", i), fmt.Sprintf("ring_%d", (i+1)%crates) + want.Units["vendor/"+name] = fmt.Sprintf("cargo:%s@1.0.0", name) + lib := "pub use " + next + "::*;\n" + uses := []string{next} + if i == crates-1 { + lib += "pub use leaf::*;\n" + uses = append(uses, "leaf") + } + write("vendor/"+name+"/Cargo.toml", manifest(name, uses...)) + write("vendor/"+name+"/src/lib.rs", lib) + write(fmt.Sprintf("src/c%d.rs", i), fmt.Sprintf("use %s::*;\n\npub fn go() {\n n::f();\n}\n", name)) + main = append(main, fmt.Sprintf("mod c%d;", i)) + deps = append(deps, name) + } + write("vendor/leaf/Cargo.toml", manifest("leaf")) + write("vendor/leaf/src/lib.rs", "pub mod n;\n") + write("vendor/leaf/src/n.rs", "pub fn f() {\n let _ = std::fs::remove_file(\"a\");\n}\n") + write("Cargo.toml", manifest("app", deps...)) + write("src/main.rs", strings.Join(main, "\n")+"\n\nfn main() {\n c0::go();\n}\n") + + cfg, roots := fileSystemConfig(dir, want) + fsys, err := codefs.NewLocalFileSystem(cfg) + require.NoError(t, err) + indexed := indexProject(t, fsys, indexer.Options{Languages: caseLanguages(t), Imports: true}) + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + g, err := projectgraph.Build(ctx, indexed.Indexes, projectgraph.Options{Root: os.DirFS(dir), Files: indexed.Statuses(), Languages: indexed.Languages, Units: roots}) + require.NoError(t, err) + results, err := reach.Reach(ctx, g, reach.Query{Targets: targetsOf(t, "leaf//n//f")}) + require.NoError(t, err) + require.Len(t, results, 1) + assert.Equal(t, reach.Found, results[0].Status) +} From 2cee839b6b6bce34ea46efcedb8c8269a7ff0b51 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 21:11:51 +0000 Subject: [PATCH 54/86] fix(projectgraph): bring only what another crate sees through a Rust glob use A glob use of a module of another crate took an item that is pub(crate) or pub(super), and a name that a private use or a pub(crate) use of the module binds. Another crate sees none of them, so a call of the name ran code of another crate while the code of the unit read found. The index now marks an item as exported only when it is pub, and marks a pub use with the new field exported of an import. The link step takes a name of a glob use of another crate only when the module gives it as pub: a pub item, a child module, or the binding of a pub use or of a pub glob use. A glob use in the crate itself still brings each name. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 7 +- fileindex/index.go | 2 + plugin/indexer/languages.go | 4 + plugin/indexer/project.go | 2 + plugin/indexer/rust.go | 46 ++++++++---- projectgraph/rust.go | 69 ++++++++++++++++- .../glob-use-crate-visibility/expect.yaml | 74 +++++++++++++++++++ .../project/Cargo.toml | 8 ++ .../project/src/main.rs | 8 ++ .../project/vendor/dep/Cargo.toml | 4 + .../project/vendor/dep/src/html.rs | 12 +++ .../project/vendor/dep/src/inner.rs | 7 ++ .../project/vendor/dep/src/lib.rs | 2 + .../project/vendor/other/Cargo.toml | 4 + .../project/vendor/other/src/lib.rs | 5 ++ .../summaries/cargo_dep_1.0.0.json | 30 ++++++++ .../summaries/cargo_other_1.0.0.json | 30 ++++++++ 17 files changed, 299 insertions(+), 15 deletions(-) create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/html.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/inner.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/other/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/other/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/summaries/cargo_dep_1.0.0.json create mode 100644 test/projectgraph/cases/rust/glob-use-crate-visibility/summaries/cargo_other_1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 5495160c..de846cf2 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -543,7 +543,12 @@ Rust follows the same rules, with these additions. the link step keeps no provision of a unit that a cut read gives, as the case `rust/glob-use-crate-cycle` shows. A local closure or a parameter can bind a single name, so such a call keeps its record too. A glob `use` - brings only a `pub` item, as in the case `rust/glob-use-private-item`. + brings only what another crate sees: a `pub` item, a child module, or the + binding of a `pub use`, as in the case `rust/glob-use-private-item`. An + item that is `pub(crate)`, `pub(super)` or `pub(in ..)`, and a `use` that + is not `pub`, stay out, as in the case `rust/glob-use-crate-visibility`. + The index marks an item as exported only when it is `pub`, and marks each + `pub use`. A glob `use` of the root of the file brings no name into an inline module with uses of its own, as in the case `rust/glob-use-inline-scope`. - `#[macro_export]` puts a macro at the root of its crate, from any module diff --git a/fileindex/index.go b/fileindex/index.go index 0adcd703..abcd6cca 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -225,6 +225,8 @@ type Import struct { Kind ImportKind `json:"kind"` Namespace string `json:"namespace"` Require bool `json:"require,omitempty"` + // Exported marks a Rust pub use, whose binding another crate can name. + Exported bool `json:"exported,omitempty"` } // Export is one export of the file. Name is "default" for a default export. diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 220da094..f478ab7b 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -6,6 +6,7 @@ import ( "github.com/safedep/code/core" "github.com/safedep/code/core/ast" "github.com/safedep/code/fileindex" + "github.com/safedep/code/plugin/callgraph" sitter "github.com/smacker/go-tree-sitter" ) @@ -99,6 +100,9 @@ type languageRules struct { // prune drops the definitions that hold no record, after the records. prune func(p *projection) + // importExported reports whether an import gives its binding on to + // other crates or modules, as pub use of Rust. + importExported func(p *projection, imp callgraph.Import) bool // shadowed fixes the targets of a call whose first name a scope between // the call and the definition of the target binds again, as a use in a // block of Rust. It reports false when the call is a record. diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index ea44f562..4516389c 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -154,9 +154,11 @@ func unitOf(name string) string { } func (p *projection) addImports() { + exported := p.rules().importExported for _, imp := range p.cg.Imports { kind, require := p.importForm(imp) p.ix.Imports = append(p.ix.Imports, fileindex.Import{ + Exported: exported != nil && exported(p, imp), Line: int(imp.Position.StartLine), Module: imp.Module, Item: imp.Item, diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 3c4f52b6..e13af31c 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -11,6 +11,7 @@ import ( "github.com/safedep/code/fileindex" "github.com/safedep/code/langcode" "github.com/safedep/code/pkg/ts" + "github.com/safedep/code/plugin/callgraph" sitter "github.com/smacker/go-tree-sitter" ) @@ -22,17 +23,18 @@ var rustRules = languageRules{ "mod_item": fileindex.DefClass, "trait_item": fileindex.DefInterface, }, - bases: rustBases, - fields: rustFields, - decorators: rustAttributes, - signature: rustSignature, - exported: rustPublic, - definitions: rustTypes, - markTries: rustMarkTries, - shadowed: rustShadowed, - visitors: []func(p *projection) visitor{referenceVisitor, rustVisitor}, - declaredAt: rustDeclaredAt, - rebound: rustRebound, + bases: rustBases, + fields: rustFields, + decorators: rustAttributes, + signature: rustSignature, + exported: rustPublic, + definitions: rustTypes, + markTries: rustMarkTries, + importExported: rustImportExported, + shadowed: rustShadowed, + visitors: []func(p *projection) visitor{referenceVisitor, rustVisitor}, + declaredAt: rustDeclaredAt, + rebound: rustRebound, // A Rust path to a function, as handler in get(handler) or // Self::visit in items.iter().for_each(Self::visit), is a value that // the code that gets it calls. @@ -1050,10 +1052,10 @@ func rustPublic(p *projection, def *fileindex.Definition) bool { } if node.Type() == "impl_item" { return slices.ContainsFunc(p.rustSiblings(node, p.rustItemName(node)), func(item *sitter.Node) bool { - return item.Type() != "impl_item" && ts.ChildOfType(item, "visibility_modifier") != nil + return item.Type() != "impl_item" && p.rustPub(item) }) } - if ts.ChildOfType(node, "visibility_modifier") != nil { + if p.rustPub(node) { return true } if impl := rustImpl(node); impl != nil { @@ -1063,6 +1065,24 @@ func rustPublic(p *projection, def *fileindex.Definition) bool { return parent != nil && parent.Type() == "trait_item" } +// rustPub reports whether an item is pub for every crate. pub(crate), +// pub(super) and pub(in path) keep the item in its crate, so another crate +// cannot name it, and a glob use of another crate does not bring it. +func (p *projection) rustPub(item *sitter.Node) bool { + return p.text(ts.ChildOfType(item, "visibility_modifier")) == "pub" +} + +// rustImportExported reports whether a use is pub, as pub use +// inner::render, so another crate can name what it binds. +func rustImportExported(p *projection, imp callgraph.Import) bool { + for node := p.descendant(imp.Position); node != nil; node = node.Parent() { + if node.Type() == "use_declaration" { + return p.rustPub(node) + } + } + return false +} + // rustVisitor marks an include! of a source file, whose code the index // does not read, as a computed import. It adds a reference to the type of a // let and to the error type of the function of a ?, which converts an diff --git a/projectgraph/rust.go b/projectgraph/rust.go index b52fc116..3b2b793c 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -981,13 +981,80 @@ func (b *builder) findGlobPath(sc *scope, first string) globPath { } ix := b.byPath[file] o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, joinPath(path, []string{first})), form: modules.FormImport} - if slices.ContainsFunc(b.providers(o), b.rustPublic) { + if slices.ContainsFunc(b.providers(o), b.rustPublic) && b.rustGlobGives(ix, path, first) { return globPath{path: path, file: file, ok: true} } } return globPath{} } +// rustGlobGives reports whether a glob use of the module of another crate +// at a path brings a name. Another crate sees only what is pub for every +// crate: a pub item of the module, a child module, or the binding of a pub +// use or of a pub glob use. A private use or a pub(crate) item of the module +// stays out, as render of use crate::inner::render in dep::html. A glob of +// what is no module, as an enum, brings the item that the provision gives. +func (b *builder) rustGlobGives(ix *fileindex.Index, path, name string) bool { + o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, path), form: modules.FormImport} + read := false + for _, pv := range b.providers(o) { + if b.units[pv.module] == nil { + continue + } + read = true + if b.rustExports(pv.module, name, map[string]bool{}) { + return true + } + } + return !read +} + +// rustExports reports whether a module of the project or of a unit gives a +// name to another crate, as rustGlobGives says. +func (b *builder) rustExports(mod, name string, seen map[string]bool) bool { + if seen[mod] { + return false + } + seen[mod] = true + u := b.units[mod] + if slices.ContainsFunc(u.names[name], func(k string) bool { return b.defs[k] != nil && b.defs[k].Exported }) { + return true + } + if _, pkg, ok := strings.Cut(mod, ":"); ok && b.namespaces[langcode.Rust].units[pkg+"::"+name] != nil { + return true + } + for _, p := range u.files { + sc := b.scopes[p] + if sc == nil { + continue + } + for _, imp := range sc.imports { + if !imp.Exported || sc.enclosing(imp.Line) != p { + continue + } + if !imp.Wildcard { + if bindingName(langcode.Rust, imp) == name { + return true + } + continue + } + if b.rustLocalRoot(p, imp.Module) { + if m, _, ok := b.resolveModule(sc.ix, imp.Module); ok && b.units[m] != nil && b.rustExports(m, name, seen) { + return true + } + continue + } + o := origin{lang: sc.ix.Language, unit: sc.ix.Unit, module: b.packageModule(sc.ix, imp.Module), form: modules.FormImport} + if slices.ContainsFunc(b.providers(o), func(pv provision) bool { + return b.units[pv.module] != nil && b.rustExports(pv.module, name, seen) + }) { + return true + } + } + } + return false +} + // rustNestedName reports whether an inline module of the graph defines a // type of the name. Most names in a Rust file name no such type, and a walk // up the classes that hold each name cost a fifth of the link step. diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/expect.yaml b/test/projectgraph/cases/rust/glob-use-crate-visibility/expect.yaml new file mode 100644 index 00000000..cfee0d4f --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/expect.yaml @@ -0,0 +1,74 @@ +source: safedep:hand +description: >- + main.rs has use dep::html::* and use other::*, and calls render(), save() and load(). In dep::html, render is + pub(crate), save comes from a private use and load from a pub(crate) use, so another crate sees none of them, + and the glob use of dep::html brings none. other gives the pub functions render, save and load, so each call + runs code of other. A link step that takes pub(crate) for pub, or a use for a pub use, gives the calls the edges + to dep, and the code of dep reads found. Each call keeps its record of the single name, so the code of dep + reads uncertain. +units: + vendor/dep: cargo:dep@1.0.0 + vendor/other: cargo:other@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//main + to: other//render + kind: direct + confidence: high + site: src/main.rs:5 + - from: src/main.rs//main + to: other//save + kind: direct + confidence: high + site: src/main.rs:6 + - from: src/main.rs//main + to: other//load + kind: direct + confidence: high + site: src/main.rs:7 +no_edges: + - from: src/main.rs//main + to: dep//html//render + - from: src/main.rs//main + to: dep//html//save + - from: src/main.rs//main + to: dep//html//load +unresolved: + - caller: src/main.rs//main + site: src/main.rs:5 + reason: unknown-receiver + method: render + - caller: src/main.rs//main + site: src/main.rs:6 + reason: unknown-receiver + method: save + - caller: src/main.rs//main + site: src/main.rs:7 + reason: unknown-receiver + method: load +results: + - target: std//fs//remove_dir + status: uncertain + call_sites: + - caller: cargo:dep@1.0.0//src/html.rs//render + site: cargo:dep@1.0.0//src/html.rs:5 + reached: false + - caller: cargo:dep@1.0.0//src/inner.rs//save + site: cargo:dep@1.0.0//src/inner.rs:2 + reached: false + - caller: cargo:dep@1.0.0//src/inner.rs//load + site: cargo:dep@1.0.0//src/inner.rs:6 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:5 + - kind: unresolved-call + site: src/main.rs:6 + - kind: unresolved-call + site: src/main.rs:7 diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/Cargo.toml new file mode 100644 index 00000000..9fdab07a --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.0.0" +other = "1.0.0" diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/src/main.rs new file mode 100644 index 00000000..7acb19ea --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/src/main.rs @@ -0,0 +1,8 @@ +use dep::html::*; +use other::*; + +fn main() { + render(); + save(); + load(); +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..537b4e47 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/html.rs b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/html.rs new file mode 100644 index 00000000..564ab4a9 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/html.rs @@ -0,0 +1,12 @@ +use crate::inner::save; +pub(crate) use crate::inner::load; + +pub(crate) fn render() { + let _ = std::fs::remove_dir("a"); +} + +pub fn page() { + render(); + save(); + load(); +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/inner.rs b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/inner.rs new file mode 100644 index 00000000..87b50228 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/inner.rs @@ -0,0 +1,7 @@ +pub fn save() { + let _ = std::fs::remove_dir("b"); +} + +pub fn load() { + let _ = std::fs::remove_dir("c"); +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..83e0a91d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/dep/src/lib.rs @@ -0,0 +1,2 @@ +pub mod html; +mod inner; diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/other/Cargo.toml b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/other/Cargo.toml new file mode 100644 index 00000000..bdb8f41b --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/other/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "other" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/other/src/lib.rs b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/other/src/lib.rs new file mode 100644 index 00000000..805ffddc --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/project/vendor/other/src/lib.rs @@ -0,0 +1,5 @@ +pub fn render() {} + +pub fn save() {} + +pub fn load() {} diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/glob-use-crate-visibility/summaries/cargo_dep_1.0.0.json new file mode 100644 index 00000000..cef61657 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,30 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//html//page", + "definitions": [ + "cargo:dep@1.0.0//src/html.rs//page" + ], + "reaches": [] + }, + { + "key": "dep//inner//load", + "definitions": [ + "cargo:dep@1.0.0//src/inner.rs//load" + ], + "reaches": [] + }, + { + "key": "dep//inner//save", + "definitions": [ + "cargo:dep@1.0.0//src/inner.rs//save" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/glob-use-crate-visibility/summaries/cargo_other_1.0.0.json b/test/projectgraph/cases/rust/glob-use-crate-visibility/summaries/cargo_other_1.0.0.json new file mode 100644 index 00000000..1b801bf5 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-crate-visibility/summaries/cargo_other_1.0.0.json @@ -0,0 +1,30 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:other@1.0.0", + "symbols": [ + { + "key": "other//load", + "definitions": [ + "cargo:other@1.0.0//src/lib.rs//load" + ], + "reaches": [] + }, + { + "key": "other//render", + "definitions": [ + "cargo:other@1.0.0//src/lib.rs//render" + ], + "reaches": [] + }, + { + "key": "other//save", + "definitions": [ + "cargo:other@1.0.0//src/lib.rs//save" + ], + "reaches": [] + } + ], + "targets": [] +} From 9dbc4d81b918054d79a3ca48db2743ccee0cb130 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 21:12:54 +0000 Subject: [PATCH 55/86] fix(projectgraph): let only a Rust macro_rules! shadow a macro of a dependency Any definition of the name in the crate kept a macro call from the edge to an exported macro of a dependency, as a function log in another file for log!(). A macro call reads macros only. The index now marks each macro_rules! with the decorator macro_rules!, and only such a definition of the name shadows the macro of the dependency. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 10 ++-- fileindex/index.go | 4 ++ plugin/indexer/rust.go | 6 ++- projectgraph/rust.go | 28 +++++----- .../expect.yaml | 51 +++++++++++++++++++ .../project/Cargo.toml | 7 +++ .../project/src/job.rs | 3 ++ .../project/src/main.rs | 9 ++++ .../project/src/util.rs | 9 ++++ .../project/vendor/dep/Cargo.toml | 4 ++ .../project/vendor/dep/src/lib.rs | 10 ++++ .../summaries/cargo_dep_1.0.0.json | 16 ++++++ 12 files changed, 140 insertions(+), 17 deletions(-) create mode 100644 test/projectgraph/cases/rust/macro-dependency-function-name/expect.yaml create mode 100644 test/projectgraph/cases/rust/macro-dependency-function-name/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-dependency-function-name/project/src/job.rs create mode 100644 test/projectgraph/cases/rust/macro-dependency-function-name/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/macro-dependency-function-name/project/src/util.rs create mode 100644 test/projectgraph/cases/rust/macro-dependency-function-name/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-dependency-function-name/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/macro-dependency-function-name/summaries/cargo_dep_1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index de846cf2..e083ef37 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -558,10 +558,12 @@ Rust follows the same rules, with these additions. `#[macro_use] extern crate dep;`, gets the edge to such a macro of a dependency of the crate, as in the case `rust/macro-import`. The index does not tell a call of a macro from a call of a function value, so the - call keeps its record. A definition of the name in the crate of the file, - as a `macro_rules!` of a module with `#[macro_use]`, shadows such a - macro, so the call keeps its record alone, as in the case - `rust/macro-local-shadows-dependency`. A macro reads `$crate::sink` as + call keeps its record. A `macro_rules!` of the name in the crate of the + file, as one of a module with `#[macro_use]`, shadows such a macro, so + the call keeps its record alone, as in the case + `rust/macro-local-shadows-dependency`. A function or a method of the name + does not, as in the case `rust/macro-dependency-function-name`. The index + marks a `macro_rules!` with the decorator `macro_rules!`. A macro reads `$crate::sink` as `crate::sink` of the crate of the macro. A call of a metavariable, as `$f()`, is a record with the reason `computed-name`, as in the case `rust/macro-metavariable-call`. diff --git a/fileindex/index.go b/fileindex/index.go index abcd6cca..e6c48f50 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -210,6 +210,10 @@ type Index struct { Truncated []Truncation `json:"truncated,omitempty"` } +// RustMacroRules is the decorator that marks a definition of a Rust +// macro_rules!, which the index otherwise gives the kind function. +const RustMacroRules = "macro_rules!" + // Import is one import of the file. Module is the module as written, as // ./utils or .foo, and the link step resolves it. Namespace is the per-file // "//" form of the import, which loses the leading dots of a relative module. diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index e13af31c..2cc91d63 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -274,7 +274,8 @@ func (p *projection) rustBounds(fn *sitter.Node) map[string]string { // rustAttributes returns the attributes before an item as written, with no // arguments, as @tokio::main for #[tokio::main] or @get for -// #[get("/")]. +// #[get("/")]. A macro_rules! gets the mark macro_rules! too, so the link +// step can tell a macro from a function of the name. func rustAttributes(p *projection, node *sitter.Node) []string { var attributes []string for sibling := node.PrevNamedSibling(); sibling != nil && sibling.Type() == "attribute_item"; sibling = sibling.PrevNamedSibling() { @@ -283,6 +284,9 @@ func rustAttributes(p *projection, node *sitter.Node) []string { } } slices.Reverse(attributes) + if node.Type() == "macro_definition" { + attributes = append(attributes, fileindex.RustMacroRules) + } return attributes } diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 3b2b793c..28381d06 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -173,8 +173,8 @@ type rustState struct { // useScopes holds the classes of each file that hold a use, which are // inline modules. useScopes map[string]map[string]bool - // spaceNames holds the names of the definitions of each space. - spaceNames map[*space]map[string]bool + // spaceMacros holds the names of the macro_rules! of each space. + spaceMacros map[*space]map[string]bool // cuts counts the cuts of the glob guards, a part of b.cuts. cuts int } @@ -219,7 +219,7 @@ func (b *builder) addRustUnits() { s.kept = map[globKey]globAnswer{} s.visited = map[globKey]globAnswer{} s.useScopes = map[string]map[string]bool{} - s.spaceNames = map[*space]map[string]bool{} + s.spaceMacros = map[*space]map[string]bool{} files := map[string][]string{} for _, p := range b.paths { if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust { @@ -836,21 +836,23 @@ func (b *builder) rustScopeFallback(sc *scope, caller, first string, rest []stri return nil } -// rustSpaceNames returns the last name of each definition of a space. -func (b *builder) rustSpaceNames(sp *space) map[string]bool { +// rustSpaceMacros returns the name of each macro_rules! of a space. +func (b *builder) rustSpaceMacros(sp *space) map[string]bool { s := b.rustState() - if names, ok := s.spaceNames[sp]; ok { + if names, ok := s.spaceMacros[sp]; ok { return names } names := map[string]bool{} for _, p := range b.paths { if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust && b.spaceOf(p) == sp { for _, def := range ix.Definitions { - names[def.Name] = true + if slices.Contains(def.Decorators, fileindex.RustMacroRules) { + names[def.Name] = true + } } } } - s.spaceNames[sp] = names + s.spaceMacros[sp] = names return names } @@ -884,10 +886,12 @@ func (b *builder) rustInUseScope(sc *scope, key string) bool { // tell a call of a macro from a call of a function, so the caller keeps // the record of the name too. func (b *builder) rustMacroCrate(ix *fileindex.Index, name string) (string, bool) { - // A macro of the crate of the file shadows a macro of a dependency. - // The index does not tell a macro from a function, so a definition of - // the name in the space of the file counts. - if b.rustSpaceNames(b.spaceOf(ix.Path))[name] { + // A macro_rules! of the crate of the file shadows a macro of a + // dependency. A function or a method of the name does not, because a + // macro call reads macros only. The rule does not read the textual + // scope of the macro, so a macro of the crate that the call cannot see + // shadows too. + if b.rustSpaceMacros(b.spaceOf(ix.Path))[name] { return "", false } exported := false diff --git a/test/projectgraph/cases/rust/macro-dependency-function-name/expect.yaml b/test/projectgraph/cases/rust/macro-dependency-function-name/expect.yaml new file mode 100644 index 00000000..0c8d95c7 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-dependency-function-name/expect.yaml @@ -0,0 +1,51 @@ +source: safedep:hand +description: >- + main.rs has #[macro_use] extern crate dep, and dep exports the macro log with #[macro_export]. src/job.rs calls + log!(), which runs that macro and so dep::sink. src/util.rs defines a function log and the struct Store a + method log. A macro call reads macros only, so neither shadows the macro of dep. A link step that takes any + definition of the name for a macro of the crate gives the call no edge, and dep//sink reads uncertain. +units: + vendor/dep: cargo:dep@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/job.rs//go + to: dep//log + kind: direct + confidence: high + site: src/job.rs:2 +unresolved: + - caller: src/job.rs//go + site: src/job.rs:2 + reason: unknown-receiver + method: log +results: + - target: dep//sink + status: found + path: + - key: src/main.rs//main + - key: src/job.rs//go + site: src/main.rs:8 + kind: direct + confidence: high + - key: dep//log + site: src/job.rs:2 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//log + site: cargo:dep@1.0.0//src/lib.rs:2 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//sink + site: cargo:dep@1.0.0//src/lib.rs:4 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//log + site: cargo:dep@1.0.0//src/lib.rs:4 + reached: true diff --git a/test/projectgraph/cases/rust/macro-dependency-function-name/project/Cargo.toml b/test/projectgraph/cases/rust/macro-dependency-function-name/project/Cargo.toml new file mode 100644 index 00000000..c76f4fc8 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-dependency-function-name/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2018" + +[dependencies] +dep = "1.0.0" diff --git a/test/projectgraph/cases/rust/macro-dependency-function-name/project/src/job.rs b/test/projectgraph/cases/rust/macro-dependency-function-name/project/src/job.rs new file mode 100644 index 00000000..1b8fe745 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-dependency-function-name/project/src/job.rs @@ -0,0 +1,3 @@ +pub fn go() { + log!(); +} diff --git a/test/projectgraph/cases/rust/macro-dependency-function-name/project/src/main.rs b/test/projectgraph/cases/rust/macro-dependency-function-name/project/src/main.rs new file mode 100644 index 00000000..90a688ea --- /dev/null +++ b/test/projectgraph/cases/rust/macro-dependency-function-name/project/src/main.rs @@ -0,0 +1,9 @@ +#[macro_use] +extern crate dep; +mod job; +mod util; + +fn main() { + util::log(1); + job::go(); +} diff --git a/test/projectgraph/cases/rust/macro-dependency-function-name/project/src/util.rs b/test/projectgraph/cases/rust/macro-dependency-function-name/project/src/util.rs new file mode 100644 index 00000000..3fbf3f5e --- /dev/null +++ b/test/projectgraph/cases/rust/macro-dependency-function-name/project/src/util.rs @@ -0,0 +1,9 @@ +pub fn log(n: u8) { + let _ = n; +} + +pub struct Store; + +impl Store { + pub fn log(&self) {} +} diff --git a/test/projectgraph/cases/rust/macro-dependency-function-name/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/macro-dependency-function-name/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..fb6107f0 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-dependency-function-name/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.0.0" +edition = "2018" diff --git a/test/projectgraph/cases/rust/macro-dependency-function-name/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/macro-dependency-function-name/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..bb64c2eb --- /dev/null +++ b/test/projectgraph/cases/rust/macro-dependency-function-name/project/vendor/dep/src/lib.rs @@ -0,0 +1,10 @@ +#[macro_export] +macro_rules! log { + () => { + $crate::sink() + }; +} + +pub fn sink() { + let _ = std::fs::remove_file("a"); +} diff --git a/test/projectgraph/cases/rust/macro-dependency-function-name/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/macro-dependency-function-name/summaries/cargo_dep_1.0.0.json new file mode 100644 index 00000000..2885322b --- /dev/null +++ b/test/projectgraph/cases/rust/macro-dependency-function-name/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//sink", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//sink" + ], + "reaches": [] + } + ], + "targets": [] +} From 11530b03799d9f9467c7366d11d047c4b765196a Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 21:19:09 +0000 Subject: [PATCH 56/86] perf(projectgraph): read the local Rust path once in the scope fallback rustScopeFallback read rustLocalPath and then rustPath, which reads rustLocalPath again. The fallback now reads only the glob path after the local path fails. The answer does not change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/rust.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 28381d06..634f5dc6 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -815,7 +815,7 @@ func (b *builder) rustScopeFallback(sc *scope, caller, first string, rest []stri // reads its own uses. t, ok := b.rustLocalPath(sc.ix.Path, written) if !ok && !b.rustInUseScope(sc, caller) { - t, ok = b.rustPath(sc.ix.Path, written) + t.path, t.file, ok = b.rustGlobPath(sc.ix.Path, written) } switch { case ok && t.key != "": From 5ec9470093a6e55161964305673586f5ae005908 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 21:52:12 +0000 Subject: [PATCH 57/86] test(projectgraph): update the hash rows after the third review The rows of fileindex 17, projectgraph 20 and reach 6 exist only on this branch, so they change in place. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/plugin_test.go | 2 +- test/projectgraph/version_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/plugin/indexer/plugin_test.go b/plugin/indexer/plugin_test.go index fd149838..c92f3a56 100644 --- a/plugin/indexer/plugin_test.go +++ b/plugin/indexer/plugin_test.go @@ -107,7 +107,7 @@ var goldenHashes = map[string]string{ "14": "26f22c80fd4120cd04f228a53188accb1695434d0ed1ff5654b110981f8f44dd", "15": "c4b949e72be5b2b4474e06f20ae18b0cc4e080b6c5fe20ccaf712c3714bf2a12", "16": "235591d02eeb5e1bb771fbacf0ba6db228146a6a70c55670de6cb93392d75d81", - "17": "a55a77ef5cb3c57064683cd88239c7edacc3f7bad6d61b9c89430d290ecd9b84", + "17": "76dafcdee7f32da7cfa59165c184dd180481efc557fb44d662bb9b20c8468baa", } func TestFileIndexGoldenHashFollowsVersion(t *testing.T) { diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index 360c9a02..cba3bcb9 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -34,7 +34,7 @@ var graphHashes = map[string]string{ "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", "16/18": "bb92620bf9903e8d19b52b62198a0bfd69d67856f9db226112b2cedf537b312b", "16/19": "51ba3e925c628602b3a53c1a6e68b379022a410bb5b749062b3045fef082ac8f", - "17/20": "52b9259fdff358531e9a888ec28826f3ae43393d82d1c3eb947ab1d35b540ef9", + "17/20": "e191f495166befe37bf0f4171cf1d2051e907be158353f5879fdb6c565434183", } // resultHashes records the hash of the reach results of every case for @@ -53,7 +53,7 @@ var resultHashes = map[string]string{ "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", "16/18/5": "e0c6093df3a85353ee15c8a90ef7c2a441f23348ce80c9d49793c256bddeffc5", "16/19/5": "a935650f8e4d01c0abdceeefb3332bf779927693a1c48b28263da5365db752cd", - "17/20/6": "b6a5dd163abb2704191cd194541aa67c17f41f22bb67e3a4414d9e5158258fe8", + "17/20/6": "b2b30afae4771b76016f6434442c835b4e5f051a4fbb7a16c1922bffc7bc8338", } // summaryHashes records the hash of the golden summaries of the cases for @@ -73,7 +73,7 @@ var summaryHashes = map[string]string{ "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", "16/18/3": "694efb5f72075356359031d449d7b4ecbdca2ddd5099dacbcd2adad745ac6e19", "16/19/3": "13c72bf9eb60fc0a38bc600906fc02028d5cb60cb2aefbec0f0721779c429d80", - "17/20/3": "6b70d0caeec5a3a546bbc53d47a6beac138c77ab4532d8de033a004ae9c54424", + "17/20/3": "70b8db79ef5d1b7c35e37b2abcc360ab8f8e7c6f0db13ad398c5b3bceefd5b96", } func TestGraphHashFollowsVersion(t *testing.T) { From 628568a91043a15dd8c491251c96c798da24a2c0 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:01:29 +0000 Subject: [PATCH 58/86] test(projectgraph): assert the outcome of the Rust glob tests The test of the Rust glob list builds the graph through Build and asserts the edge from run to g through the glob use. The glob tests of the deadline file get a budget of ten seconds, so a loaded runner does not fail them. One helper writes the files of a test project. The description of next-route-test-dir names the paths of its project. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/rust_test.go | 33 +++++----- .../next-route-test-dir/expect.yaml | 2 +- test/projectgraph/deadline_test.go | 60 ++++++++----------- test/projectgraph/helpers_test.go | 14 +++++ test/projectgraph/receivers_test.go | 6 +- 5 files changed, 58 insertions(+), 57 deletions(-) diff --git a/projectgraph/rust_test.go b/projectgraph/rust_test.go index dd97e334..2d20b4c4 100644 --- a/projectgraph/rust_test.go +++ b/projectgraph/rust_test.go @@ -114,23 +114,18 @@ func TestRustHelperProgram(t *testing.T) { // uses crate::sub::x and crate::c. sub gives x only through its own glob use // of other, so the read of the glob uses of other asks for other again // under the guard of a glob member. That read gets no module for -// crate::sub::x. A list of the modules of other that such a read gave -// stayed for the build without the module c::x, and a name of c::x read -// missing for every later lookup. +// crate::sub::x. The list of the glob modules of other that the build keeps +// must still hold c::x, so g() in run links g of src/c/x.rs through the +// glob use. func TestBuildRustKeepsNoGlobListThatACutGave(t *testing.T) { - files := map[string]string{ + dir := writeFiles(t, map[string]string{ "Cargo.toml": "[package]\nname = \"app\"\nversion = \"1.0.0\"\nedition = \"2021\"\n", "src/main.rs": "mod c;\nmod other;\nmod sub;\n\nuse sub::*;\n\nfn main() {\n x::g();\n other::run();\n}\n", "src/c/mod.rs": "pub mod x;\n", "src/c/x.rs": "pub fn g() {}\n", "src/other.rs": "use crate::sub::x::*;\npub use crate::c::*;\n\npub fn run() {\n g();\n}\n", "src/sub.rs": "pub use crate::other::*;\n", - } - dir := t.TempDir() - for p, text := range files { - require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(dir, p)), 0o755)) - require.NoError(t, os.WriteFile(filepath.Join(dir, p), []byte(text), 0o644)) - } + }) languages, err := lang.AllLanguages() require.NoError(t, err) fsys, err := codefs.NewLocalFileSystem(codefs.LocalFileSystemConfig{AppDirectories: []string{dir}}) @@ -139,12 +134,20 @@ func TestBuildRustKeepsNoGlobListThatACutGave(t *testing.T) { require.NoError(t, err) indexed, err := ix.Run(context.Background(), fsys) require.NoError(t, err) - b, err := newBuilder(indexed.Indexes, Options{Root: os.DirFS(dir), Files: indexed.Statuses(), Languages: indexed.Languages}) + g, err := Build(context.Background(), indexed.Indexes, Options{Root: os.DirFS(dir), Files: indexed.Statuses(), Languages: indexed.Languages}) require.NoError(t, err) - require.NoError(t, b.linkAll(context.Background())) + assert.Contains(t, edgeStrings(g), "src/other.rs//run -> src/c/x.rs//g direct rust.wildcard-import") +} - w := b.wildcardMods[b.scopes["src/other.rs"]] - if w != nil { - assert.Equal(t, []string{"rust:app::c::x", "rust:app::c"}, w.mods) +// writeFiles writes the files, by their slash paths, to a new temporary +// directory and returns the directory. +func writeFiles(t testing.TB, files map[string]string) string { + t.Helper() + dir := t.TempDir() + for p, text := range files { + file := filepath.Join(dir, filepath.FromSlash(p)) + require.NoError(t, os.MkdirAll(filepath.Dir(file), 0o755)) + require.NoError(t, os.WriteFile(file, []byte(text), 0o644)) } + return dir } diff --git a/test/projectgraph/cases/typescript/next-route-test-dir/expect.yaml b/test/projectgraph/cases/typescript/next-route-test-dir/expect.yaml index fed3c93d..7889edc1 100644 --- a/test/projectgraph/cases/typescript/next-route-test-dir/expect.yaml +++ b/test/projectgraph/cases/typescript/next-route-test-dir/expect.yaml @@ -1,6 +1,6 @@ source: safedep:hand description: >- - A route of the app has a path under a test path, as app/api/test or src/routes/tests. The route rule + A route of the app has a path under a test path, as app/api/test or app/spec. The route rule names its files by their path in the app, so it marks them although they are test files, and probe and render are found. Only the rule node.component skips a file of a test path. test_paths: [test, tests, spec] diff --git a/test/projectgraph/deadline_test.go b/test/projectgraph/deadline_test.go index 362e03d1..a2586d5c 100644 --- a/test/projectgraph/deadline_test.go +++ b/test/projectgraph/deadline_test.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "os" - "path/filepath" "strings" "testing" "time" @@ -23,26 +22,22 @@ import ( // work grows with the orders of the files. The build must end soon after // its deadline, as a scan gives the link step a budget. func TestBuildStopsAtTheDeadline(t *testing.T) { - const files = 10 - dir := t.TempDir() - write := func(name, text string) { - require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0o755)) - require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(text), 0o644)) - } - write("Cargo.toml", "[package]\nname = \"app\"\nversion = \"0.1.0\"\n") + const count = 10 + files := map[string]string{"Cargo.toml": "[package]\nname = \"app\"\nversion = \"0.1.0\"\n"} var lib strings.Builder - for i := range files { + for i := range count { fmt.Fprintf(&lib, "pub mod f%d;\n", i) var file strings.Builder - for j := range files { + for j := range count { if j != i { fmt.Fprintf(&file, "pub use crate::f%d::E::*;\n", j) } } file.WriteString("pub fn run() {\n missing();\n}\n") - write(fmt.Sprintf("src/f%d.rs", i), file.String()) + files[fmt.Sprintf("src/f%d.rs", i)] = file.String() } - write("src/lib.rs", lib.String()) + files["src/lib.rs"] = lib.String() + dir := writeFiles(t, files) fsys, err := codefs.NewLocalFileSystem(codefs.LocalFileSystemConfig{AppDirectories: []string{dir}}) require.NoError(t, err) @@ -61,7 +56,8 @@ func TestBuildStopsAtTheDeadline(t *testing.T) { // second, a chain of twenty levels of two files each glob uses both files of // the next level, and only one file of the last level defines leaf. A // lookup that reads a module once per path through the glob uses grows -// with the number of paths, and the build did not end within minutes. +// with the number of paths. The build must read each glob module once per +// name, so it ends well within the budget and reaches remove_file. func TestBuildReadsEachGlobModuleOncePerName(t *testing.T) { complete := map[string]string{} var main strings.Builder @@ -100,17 +96,13 @@ func TestBuildReadsEachGlobModuleOncePerName(t *testing.T) { for name, files := range map[string]map[string]string{"complete": complete, "diamond": diamond} { t.Run(name, func(t *testing.T) { - dir := t.TempDir() files["Cargo.toml"] = "[package]\nname = \"app\"\nversion = \"0.1.0\"\n" - for p, text := range files { - require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(dir, p)), 0o755)) - require.NoError(t, os.WriteFile(filepath.Join(dir, p), []byte(text), 0o644)) - } + dir := writeFiles(t, files) fsys, err := codefs.NewLocalFileSystem(codefs.LocalFileSystemConfig{AppDirectories: []string{dir}}) require.NoError(t, err) indexed := indexProject(t, fsys, indexer.Options{Languages: caseLanguages(t)}) - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() g, err := projectgraph.Build(ctx, indexed.Indexes, projectgraph.Options{Root: os.DirFS(dir), Files: indexed.Statuses(), Languages: indexed.Languages}) require.NoError(t, err) @@ -126,15 +118,11 @@ func TestBuildReadsEachGlobModuleOncePerName(t *testing.T) { // dependency code whose roots glob use the next crate, the last one back // to the first, and a crate leaf that the last one also glob uses. Each // file of the project glob uses one crate of the ring and calls n::f of -// leaf. A glob path that kept no answer under a cut read the ring once for -// each path round it, and the build did not end within minutes. +// leaf. The build must read each glob path of the ring once, so it ends +// well within the budget and reaches n::f. func TestBuildReadsEachGlobPathOnceInARingOfCrates(t *testing.T) { const crates = 6 - dir := t.TempDir() - write := func(name, text string) { - require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0o755)) - require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(text), 0o644)) - } + files := map[string]string{} manifest := func(name string, deps ...string) string { var m strings.Builder fmt.Fprintf(&m, "[package]\nname = \"%s\"\nversion = \"1.0.0\"\nedition = \"2021\"\n\n[dependencies]\n", name) @@ -154,23 +142,23 @@ func TestBuildReadsEachGlobPathOnceInARingOfCrates(t *testing.T) { lib += "pub use leaf::*;\n" uses = append(uses, "leaf") } - write("vendor/"+name+"/Cargo.toml", manifest(name, uses...)) - write("vendor/"+name+"/src/lib.rs", lib) - write(fmt.Sprintf("src/c%d.rs", i), fmt.Sprintf("use %s::*;\n\npub fn go() {\n n::f();\n}\n", name)) + files["vendor/"+name+"/Cargo.toml"] = manifest(name, uses...) + files["vendor/"+name+"/src/lib.rs"] = lib + files[fmt.Sprintf("src/c%d.rs", i)] = fmt.Sprintf("use %s::*;\n\npub fn go() {\n n::f();\n}\n", name) main = append(main, fmt.Sprintf("mod c%d;", i)) deps = append(deps, name) } - write("vendor/leaf/Cargo.toml", manifest("leaf")) - write("vendor/leaf/src/lib.rs", "pub mod n;\n") - write("vendor/leaf/src/n.rs", "pub fn f() {\n let _ = std::fs::remove_file(\"a\");\n}\n") - write("Cargo.toml", manifest("app", deps...)) - write("src/main.rs", strings.Join(main, "\n")+"\n\nfn main() {\n c0::go();\n}\n") - + files["vendor/leaf/Cargo.toml"] = manifest("leaf") + files["vendor/leaf/src/lib.rs"] = "pub mod n;\n" + files["vendor/leaf/src/n.rs"] = "pub fn f() {\n let _ = std::fs::remove_file(\"a\");\n}\n" + files["Cargo.toml"] = manifest("app", deps...) + files["src/main.rs"] = strings.Join(main, "\n") + "\n\nfn main() {\n c0::go();\n}\n" + dir := writeFiles(t, files) cfg, roots := fileSystemConfig(dir, want) fsys, err := codefs.NewLocalFileSystem(cfg) require.NoError(t, err) indexed := indexProject(t, fsys, indexer.Options{Languages: caseLanguages(t), Imports: true}) - ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() g, err := projectgraph.Build(ctx, indexed.Indexes, projectgraph.Options{Root: os.DirFS(dir), Files: indexed.Statuses(), Languages: indexed.Languages, Units: roots}) require.NoError(t, err) diff --git a/test/projectgraph/helpers_test.go b/test/projectgraph/helpers_test.go index 7af3ebca..14f5b9e5 100644 --- a/test/projectgraph/helpers_test.go +++ b/test/projectgraph/helpers_test.go @@ -3,6 +3,7 @@ package projectgraph import ( "context" "os" + "path/filepath" "testing" "github.com/safedep/code/core" @@ -47,3 +48,16 @@ func linkProject(t testing.TB, root string, indexed *indexer.Result, opts projec require.NoError(t, err) return g } + +// writeFiles writes the files, by their slash paths, to a new temporary +// directory and returns the directory. +func writeFiles(t testing.TB, files map[string]string) string { + t.Helper() + dir := t.TempDir() + for p, text := range files { + file := filepath.Join(dir, filepath.FromSlash(p)) + require.NoError(t, os.MkdirAll(filepath.Dir(file), 0o755)) + require.NoError(t, os.WriteFile(file, []byte(text), 0o644)) + } + return dir +} diff --git a/test/projectgraph/receivers_test.go b/test/projectgraph/receivers_test.go index 7683413b..64ed7bb0 100644 --- a/test/projectgraph/receivers_test.go +++ b/test/projectgraph/receivers_test.go @@ -554,11 +554,7 @@ func pythonReceiverProject(source string) map[string]string { // receiverAnswer links a project of the files with the dependency code of // the language and answers the target of the row. func receiverAnswer(t *testing.T, languages []core.Language, language core.LanguageCode, files map[string]string, row receiverRow) string { - dir := t.TempDir() - for p, text := range files { - require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(dir, p)), 0o755)) - require.NoError(t, os.WriteFile(filepath.Join(dir, p), []byte(text), 0o644)) - } + dir := writeFiles(t, files) cfg := codefs.LocalFileSystemConfig{AppDirectories: []string{dir}, ImportUnits: map[string]string{}} units := map[string]iofs.FS{} if !row.noDeps { From bb05738f4660a10076b3f73232eff0a4ee106d9e Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:07:51 +0000 Subject: [PATCH 59/86] refactor(projectgraph): share the guard, memo and origin code - guarded holds the sum of the cycle guards that idle and uncut read. - remember writes a memo answer and logs its undo, and cut counts a cut. - importOrigin makes the origin of an import of a file. - globMemo holds the glob lookups of Rust and their reads, and globKey names its fields. rustGlobPath returns a rustTarget. - One pass of the files reads the exported macros and the macro_rules! of Rust, and one pass of the definitions reads the field and method names of the languages with function values. docs/projectgraph.md holds the rules of the Rust glob lookups. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 28 ++++ projectgraph/cha.go | 8 +- projectgraph/imports.go | 2 +- projectgraph/link.go | 12 +- projectgraph/names.go | 12 +- projectgraph/packages.go | 12 +- projectgraph/resolve.go | 35 +++-- projectgraph/results.go | 2 +- projectgraph/rust.go | 298 ++++++++++++++++----------------------- projectgraph/values.go | 51 ++++--- projectgraph/walk.go | 2 +- 11 files changed, 232 insertions(+), 230 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index e083ef37..cb900faf 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -715,6 +715,34 @@ that only C gives, as `_thread`, stays an external key. The index step leaves out the test suite under `test`, the IDLE, the turtle demos and `lib2to3`, which no program imports. +### Rust glob lookups + +Rust reads a name of a glob `use` before a crate, and rejects a name that +two glob `use`s bring. A glob `use` of an inline module stays out of the +module of the file. Modules and crates can bring each other with glob +`use`s, in a diamond or in a cycle. A unit can bring a name by a glob +`use` of a third crate, so the read of the unit can ask for the same path +again. A walk of each path through such `use`s grows with the number of +paths. + +So the link step reads each glob lookup once: the glob path of a file and +a first name, or the glob member of a module and a name. The outermost +lookup reads each lookup once, and a lookup that it reads again gives its +first answer. A read of a lookup in progress is a cut. An answer stays for +the build when each cut under it hit the lookup or a lookup under it, as +the low link of Tarjan tells, and when no other guard cut it. Such a cut +leaves out no path, because the lookup in progress reads that path itself. +`providers` keeps no answer that a cut gave. + +A visited answer is part of the lookup that read it. When that lookup +stays for the build, a visited answer under it that found its item stays +too. The kept answers give each later read the same way through a cycle +of crates. Any other visited answer under it goes. A later read with +another depth would take its low for its own, and the answer can lack an +item that the lookup found after it. When the lookup does not stay, the +low of each visited answer under it falls to the low of the lookup, +because they lack what it lacks. + ## Overlay An overlay is a file of `llm` edges that resolve unresolved sites of a diff --git a/projectgraph/cha.go b/projectgraph/cha.go index f621d44b..82c35e1d 100644 --- a/projectgraph/cha.go +++ b/projectgraph/cha.go @@ -75,8 +75,7 @@ func (b *builder) mro(class string) []string { order = b.depthFirst(class, 0, map[string]bool{}) } b.ordering-- - b.mros[class] = order - b.memo(func() { delete(b.mros, class) }) + remember(b, b.mros, class, order) return order } @@ -188,8 +187,7 @@ func (b *builder) externalBase(class string) (string, bool) { idle, cuts := b.idle(), b.cuts base, _ = b.findExternalBase(class) if idle || b.cuts == cuts { - b.externalBases[class] = base - b.memo(func() { delete(b.externalBases, class) }) + remember(b, b.externalBases, class, base) } } return base, base != "" @@ -202,7 +200,7 @@ func (b *builder) externalBase(class string) (string, bool) { // that the search reads now has no external base, so the search ends. func (b *builder) findExternalBase(class string) (string, bool) { if b.findingBases[class] { - b.cuts++ + b.cut() return "", false } b.findingBases[class] = true diff --git a/projectgraph/imports.go b/projectgraph/imports.go index 3dac1de7..ff930621 100644 --- a/projectgraph/imports.go +++ b/projectgraph/imports.go @@ -106,7 +106,7 @@ func (b *builder) importedModules(ix *fileindex.Index, spec string, form modules } var mods []moduleRef var missing []provision - for _, pv := range b.providers(origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, spec), form: form}) { + for _, pv := range b.providers(b.importOrigin(ix, spec, form)) { if pv.module == "" { missing = append(missing, pv) continue diff --git a/projectgraph/link.go b/projectgraph/link.go index bea41d90..c0afb767 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -138,12 +138,12 @@ type builder struct { visiting map[nameKey]bool bindings map[string]string classes map[string]classRecord - // defs holds the definition records by key, and fieldNames the names - // of the Go fields among them, true for a name that a field which can - // hold a function has. - defs map[string]*fileindex.Definition - fieldNames map[string]bool - // methodNames holds the names of the Go methods among them. + // defs holds the definition records by key. fieldNames holds the + // names of the fields among them of the languages with functionValues, + // true for a name that a field which can hold a function has. + // methodNames holds the names of the methods of those languages. + defs map[string]*fileindex.Definition + fieldNames map[string]bool methodNames map[string]bool // callees holds the definitions and the external keys that the calls // at each site resolve to. diff --git a/projectgraph/names.go b/projectgraph/names.go index d6861d15..31f692a5 100644 --- a/projectgraph/names.go +++ b/projectgraph/names.go @@ -17,11 +17,11 @@ func (b *builder) resolveName(module, name string, hops int) (string, *failure) return r.key, r.fail } if b.visiting[k] { - b.cuts++ + b.cut() return "", &failure{reason: fileindex.UnresolvedNoDefinition, name: name, cut: true} } if hops > b.opts.MaxExportHops { - b.cuts++ + b.cut() path := strings.TrimSuffix(module, "/") b.truncations[Truncation{Kind: TruncationExportHops, Key: fileindex.Join(path, name), Site: Site{Path: path}}] = true return "", &failure{reason: fileindex.UnresolvedLimit, name: name, cut: true} @@ -30,8 +30,7 @@ func (b *builder) resolveName(module, name string, hops int) (string, *failure) key, fail := b.lookupName(module, name, hops) delete(b.visiting, k) if fail == nil || !fail.cut { - b.names[k] = resolution{key: key, fail: fail} - b.memo(func() { delete(b.names, k) }) + remember(b, b.names, k, resolution{key: key, fail: fail}) } return key, fail } @@ -211,7 +210,7 @@ func (b *builder) bound(key string) (string, bool) { return "", false } if b.reading[key] { - b.cuts++ + b.cut() return "", false } b.reading[key] = true @@ -229,8 +228,7 @@ func (b *builder) bound(key string) (string, bool) { if !ok || target == key { return "", false } - b.bindings[key] = target - b.memo(func() { delete(b.bindings, key) }) + remember(b, b.bindings, key, target) if b.g.Nodes[target] != nil { b.link(sc.ix.Language, key, target, b.siteOf(key), EdgeExport, ruleAlias) } diff --git a/projectgraph/packages.go b/projectgraph/packages.go index 5711c974..6013a202 100644 --- a/projectgraph/packages.go +++ b/projectgraph/packages.go @@ -298,12 +298,19 @@ func (b *builder) recordOrigin(ix *fileindex.Index, key, module string, form mod if !b.hasUnits() { return } - o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, module), form: form, names: slices.Clone(names)} + o := b.importOrigin(ix, module, form) + o.names = slices.Clone(names) if !slices.ContainsFunc(b.origins[key], o.equal) { b.origins[key] = append(b.origins[key], o) } } +// importOrigin returns the origin of an import of the file that names the +// module in the form. +func (b *builder) importOrigin(ix *fileindex.Index, module string, form modules.Form) origin { + return origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, module), form: form} +} + // packageModule returns the module of the package that an import of the // file names, by the profile of its family. func (b *builder) packageModule(ix *fileindex.Index, module string) string { @@ -378,8 +385,7 @@ func (b *builder) providers(o origin) []provision { // and the same module can resolve on another way in. So only an answer // that no guard cut stays. if b.cuts == cuts { - b.provisions[k] = out - b.memo(func() { delete(b.provisions, k) }) + remember(b, b.provisions, k, out) } return out } diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index afd243da..b9a81ed5 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -569,7 +569,7 @@ func (b *builder) wildcardModules(sc *scope) []string { // A read inside another read adds the paths that it asks for to the // record of the outer one. outer := b.wildcardMods != nil && b.asked == nil - whole := b.globRead() + whole := b.rustState().glob.read() if outer { b.asked = map[[2]string]bool{} } @@ -614,6 +614,18 @@ func (b *builder) memo(undo func()) { } } +// remember writes an answer to a memo table, and logs its undo as memo +// does. +func remember[K comparable, V any](b *builder, table map[K]V, k K, v V) { + table[k] = v + b.memo(func() { delete(table, k) }) +} + +// cut counts an answer that a cycle guard or the hop limit cuts. +func (b *builder) cut() { + b.cuts++ +} + // undo reverts the memo writes after the mark, newest first. func (b *builder) undo(mark int) { for i := len(b.tentative) - 1; i >= mark; i-- { @@ -629,8 +641,14 @@ func (b *builder) undo(mark int) { // nothing by it. func (b *builder) idle() bool { s := b.rustState() + return !b.guarded() && len(s.resolving)+len(s.glob.active) == 0 +} + +// guarded reports whether a cycle guard other than the resolving guard and +// the glob guards of Rust is in progress. +func (b *builder) guarded() bool { return len(b.expanding)+len(b.visiting)+len(b.reading)+len(b.stepping)+len(b.findingBases)+ - len(s.routing)+b.ordering+len(s.resolving)+len(s.active) == 0 + len(b.rustState().routing)+b.ordering > 0 } // uncut reports whether a read of the wildcard imports of a scope that @@ -640,22 +658,19 @@ func (b *builder) idle() bool { // path in progress was set before the read, and the guard cut the read if // the read asked for it. // -// The guards of a Rust glob path and of a glob member tell globRead the -// depth of each read that they cut. A read with a cut of a lookup that +// The guards of a Rust glob path and of a glob member tell globMemo.read +// the depth of each read that they cut. A read with a cut of a lookup that // began before the read is no whole read, so wildcardModules keeps no list -// of it. A cut of a lookup that the read began leaves out no path. A check -// of any glob lookup in progress kept no list under a lookup, and made the -// link step of the Rust corpus a third slower. +// of it. A cut of a lookup that the read began leaves out no path. // // uncut compares the paths of the resolving guard only. On the Rust // corpus, the reads under any other guard cost less than 3% of the read // time. So any other state in progress fails the check. func (b *builder) uncut(asked map[[2]string]bool) bool { - s := b.rustState() - if len(b.expanding)+len(b.visiting)+len(b.reading)+len(b.stepping)+len(b.findingBases)+len(s.routing)+b.ordering > 0 { + if b.guarded() { return false } - for k := range s.resolving { + for k := range b.rustState().resolving { if asked[k] { return false } diff --git a/projectgraph/results.go b/projectgraph/results.go index b6dc73e7..05e0a456 100644 --- a/projectgraph/results.go +++ b/projectgraph/results.go @@ -122,7 +122,7 @@ func (b *builder) unitDefinitions(origins []origin) ([]string, bool) { } k := nameKey{module: pv.module, name: fileindex.Join("", o.names...)} if b.stepping[k] { - b.cuts++ + b.cut() continue } b.stepping[k] = true diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 634f5dc6..57a81dee 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -158,25 +158,12 @@ type rustState struct { globs map[string][]string macros map[*space]map[string]string nested map[string]bool - // active holds the depth of each glob path and glob member that a - // lookup reads now. depth is the depth of the last one, and low the - // least depth that a cut of the read in progress hit. - active map[globKey]int - depth int - low int - // kept holds the answers of the glob lookups that no cut of an outer - // read gave, for the build. visited holds the other answers of the - // outermost lookup in progress, and visits the order of their reads. - kept map[globKey]globAnswer - visited map[globKey]globAnswer - visits []globKey + glob globMemo // useScopes holds the classes of each file that hold a use, which are // inline modules. useScopes map[string]map[string]bool // spaceMacros holds the names of the macro_rules! of each space. spaceMacros map[*space]map[string]bool - // cuts counts the cuts of the glob guards, a part of b.cuts. - cuts int } // crateRoute is the path in another crate that a path of a file names, and @@ -214,12 +201,8 @@ func (b *builder) addRustUnits() { s.routing = map[[2]string]bool{} s.routes = map[[2]string]crateRoute{} s.globs = map[string][]string{} - s.active = map[globKey]int{} - s.low = math.MaxInt - s.kept = map[globKey]globAnswer{} - s.visited = map[globKey]globAnswer{} + s.glob = globMemo{active: map[globKey]int{}, low: math.MaxInt, kept: map[globKey]globAnswer{}, visited: map[globKey]globAnswer{}} s.useScopes = map[string]map[string]bool{} - s.spaceMacros = map[*space]map[string]bool{} files := map[string][]string{} for _, p := range b.paths { if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust { @@ -391,39 +374,42 @@ func rustProvide(b *builder, sp *space, module string, _ modules.Form) []provisi if key, ok := b.resolveQualified(langcode.Rust, joinPath(sp.unit, parts[1:])); ok { return []provision{{sp: sp, module: key, via: "crate"}} } - if key, ok := b.rustExportedMacro(sp, parts[1:]); ok { - return []provision{{sp: sp, module: key, via: "crate"}} + // #[macro_export] puts a macro at the root of its crate, as from_sub of + // src/macros.rs for dep::from_sub. A walk of the modules of the crate + // does not find it. + if len(parts) == 2 { + exported, _ := b.rustMacros() + if key, ok := exported[sp][parts[1]]; ok { + return []provision{{sp: sp, module: key, via: "crate"}} + } } return []provision{{sp: sp}} } -// rustExportedMacro returns the macro of a unit that #[macro_export] puts -// at the root of its crate, as from_sub of src/macros.rs for -// dep::from_sub. A walk of the modules of the crate does not find it. -func (b *builder) rustExportedMacro(sp *space, names []string) (string, bool) { - if len(names) != 1 { - return "", false - } - key, ok := b.rustMacros()[sp][names[0]] - return key, ok -} - // rustMacros returns the macros that #[macro_export] puts at the root of -// the crate of each unit, by name. -func (b *builder) rustMacros() map[*space]map[string]string { +// the crate of each unit, by name, and the names of the macro_rules! of +// each space. +func (b *builder) rustMacros() (exported map[*space]map[string]string, rules map[*space]map[string]bool) { s := b.rustState() if s.macros != nil { - return s.macros + return s.macros, s.spaceMacros } s.macros = map[*space]map[string]string{} + s.spaceMacros = map[*space]map[string]bool{} for _, p := range b.paths { ix := b.byPath[p] - sp := b.spaces[fileindex.PackageOf(p)] - if ix == nil || ix.Language != langcode.Rust || sp == nil || sp.unit == "" { + if ix == nil || ix.Language != langcode.Rust { continue } + sp := b.spaceOf(p) for _, def := range ix.Definitions { - if !slices.Contains(def.Decorators, rustMacroExport) { + if slices.Contains(def.Decorators, fileindex.RustMacroRules) { + if s.spaceMacros[sp] == nil { + s.spaceMacros[sp] = map[string]bool{} + } + s.spaceMacros[sp][def.Name] = true + } + if sp.unit == "" || !slices.Contains(def.Decorators, rustMacroExport) { continue } if s.macros[sp] == nil { @@ -434,7 +420,7 @@ func (b *builder) rustMacros() map[*space]map[string]string { } } } - return s.macros + return s.macros, s.spaceMacros } // rustMacroExport is the attribute that puts a macro at the root of its @@ -529,7 +515,7 @@ func (b *builder) resolveRust(importer, written string) (string, bool) { b.asked[k] = true } if s.resolving[k] { - b.cuts++ + b.cut() return "", false } s.resolving[k] = true @@ -574,7 +560,7 @@ func (r rustResolver) Module(importer, spec string) (string, string, bool) { // is a module of a unit. func (b *builder) rustUnitModule(file, path string) bool { ix := b.byPath[file] - o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, path), form: modules.FormImport} + o := b.importOrigin(ix, path, modules.FormImport) return slices.ContainsFunc(b.providers(o), func(pv provision) bool { return b.units[pv.module] != nil }) } @@ -591,10 +577,7 @@ func (b *builder) rustPath(importer, written string) (rustTarget, bool) { if t, ok := b.rustLocalPath(importer, written); ok { return t, true } - if path, file, ok := b.rustGlobPath(importer, written); ok { - return rustTarget{path: path, file: file}, true - } - return rustTarget{}, false + return b.rustGlobPath(importer, written) } // rustLocalPath resolves a path that a file writes as rustPath does, with @@ -659,7 +642,7 @@ func (b *builder) rustCratePath(importer, written string) (string, string, bool) return r.path, r.file, r.ok } if s.routing[k] { - b.cuts++ + b.cut() return "", "", false } outer := len(s.routing) == 0 @@ -815,7 +798,7 @@ func (b *builder) rustScopeFallback(sc *scope, caller, first string, rest []stri // reads its own uses. t, ok := b.rustLocalPath(sc.ix.Path, written) if !ok && !b.rustInUseScope(sc, caller) { - t.path, t.file, ok = b.rustGlobPath(sc.ix.Path, written) + t, ok = b.rustGlobPath(sc.ix.Path, written) } switch { case ok && t.key != "": @@ -836,26 +819,6 @@ func (b *builder) rustScopeFallback(sc *scope, caller, first string, rest []stri return nil } -// rustSpaceMacros returns the name of each macro_rules! of a space. -func (b *builder) rustSpaceMacros(sp *space) map[string]bool { - s := b.rustState() - if names, ok := s.spaceMacros[sp]; ok { - return names - } - names := map[string]bool{} - for _, p := range b.paths { - if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust && b.spaceOf(p) == sp { - for _, def := range ix.Definitions { - if slices.Contains(def.Decorators, fileindex.RustMacroRules) { - names[def.Name] = true - } - } - } - } - s.spaceMacros[sp] = names - return names -} - // rustInUseScope reports whether a key of a file sits in an inline module // with a use of its own. The index keeps the uses of a file in one scope, // and an impl block holds no use, so a class of the file that holds a use @@ -891,20 +854,21 @@ func (b *builder) rustMacroCrate(ix *fileindex.Index, name string) (string, bool // macro call reads macros only. The rule does not read the textual // scope of the macro, so a macro of the crate that the call cannot see // shadows too. - if b.rustSpaceMacros(b.spaceOf(ix.Path))[name] { + exported, rules := b.rustMacros() + if rules[b.spaceOf(ix.Path)][name] { return "", false } - exported := false - for _, macros := range b.rustMacros() { + found := false + for _, macros := range exported { _, ok := macros[name] - exported = exported || ok + found = found || ok } - if !exported { + if !found { return "", false } for _, dep := range b.cargoOf(b.spaceOf(ix.Path)).Dependencies() { crate := strings.ReplaceAll(dep, "-", "_") - o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, crate+"::"+name), form: modules.FormImport} + o := b.importOrigin(ix, crate+"::"+name, modules.FormImport) if slices.ContainsFunc(b.providers(o), func(pv provision) bool { def := b.defs[pv.module] return def != nil && slices.Contains(def.Decorators, rustMacroExport) @@ -915,29 +879,23 @@ func (b *builder) rustMacroCrate(ix *fileindex.Index, name string) (string, bool return "", false } -// rustGlobPath returns the path in another crate that a path of a file +// rustGlobPath returns the target in another crate that a path of a file // names through a glob use of a module of that crate, as dep::html::render // for render after use dep::html::*, when a unit gives the crate and the -// first name of the path in the module. Rust reads a name of a glob use -// before a crate, and rejects a name that two glob uses bring. A glob use -// of a crate that no unit gives can bring any name, and the path then -// resolves as before. A glob use of an inline module stays out, as -// rustGlobModules says. -// -// A unit can bring the name by a glob use of a third crate, so the read of -// the unit can ask for the same path again. The guard cuts such a cycle, -// and providers keeps no answer that a cut gave. -func (b *builder) rustGlobPath(importer, written string) (string, string, bool) { +// first name of the path in the module. A glob use of a crate that no unit +// gives can bring any name, so the path then resolves with no glob path. +// docs/projectgraph.md has the rules of the cycles of glob uses. +func (b *builder) rustGlobPath(importer, written string) (rustTarget, bool) { sc := b.scopes[importer] first := rustFirstName(written) if sc == nil || len(sc.wildcards) == 0 || !b.hasUnits() || strings.HasPrefix(written, "::") { - return "", "", false + return rustTarget{}, false } switch first { case "crate", "self", "super": - return "", "", false + return rustTarget{}, false } - a := b.globLookup(globKey{path: true, a: importer, b: first}, func() globAnswer { + a := b.rustState().glob.lookup(b, globKey{path: true, scope: importer, name: first}, func() globAnswer { return globAnswer{path: b.findGlobPath(sc, first)} }) return a.path.join(written) @@ -961,12 +919,12 @@ type globPath struct { ok bool } -// join returns the path of a written path under the module. -func (g globPath) join(written string) (string, string, bool) { +// join returns the target of a written path under the module. +func (g globPath) join(written string) (rustTarget, bool) { if !g.ok { - return "", "", false + return rustTarget{}, false } - return joinPath(g.path, strings.Split(written, "::")), g.file, true + return rustTarget{path: joinPath(g.path, strings.Split(written, "::")), file: g.file}, true } func (b *builder) findGlobPath(sc *scope, first string) globPath { @@ -984,7 +942,7 @@ func (b *builder) findGlobPath(sc *scope, first string) globPath { path, file = imp.Module, importer } ix := b.byPath[file] - o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, joinPath(path, []string{first})), form: modules.FormImport} + o := b.importOrigin(ix, joinPath(path, []string{first}), modules.FormImport) if slices.ContainsFunc(b.providers(o), b.rustPublic) && b.rustGlobGives(ix, path, first) { return globPath{path: path, file: file, ok: true} } @@ -999,7 +957,7 @@ func (b *builder) findGlobPath(sc *scope, first string) globPath { // stays out, as render of use crate::inner::render in dep::html. A glob of // what is no module, as an enum, brings the item that the provision gives. func (b *builder) rustGlobGives(ix *fileindex.Index, path, name string) bool { - o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, path), form: modules.FormImport} + o := b.importOrigin(ix, path, modules.FormImport) read := false for _, pv := range b.providers(o) { if b.units[pv.module] == nil { @@ -1048,7 +1006,7 @@ func (b *builder) rustExports(mod, name string, seen map[string]bool) bool { } continue } - o := origin{lang: sc.ix.Language, unit: sc.ix.Unit, module: b.packageModule(sc.ix, imp.Module), form: modules.FormImport} + o := b.importOrigin(sc.ix, imp.Module, modules.FormImport) if slices.ContainsFunc(b.providers(o), func(pv provision) bool { return b.units[pv.module] != nil && b.rustExports(pv.module, name, seen) }) { @@ -1095,11 +1053,11 @@ func (b *builder) rustGlobMember(sc *scope, name string) (string, bool) { if !b.rustUnitFile(sc.ix) { return "", false } - path, file, ok := b.rustGlobPath(sc.ix.Path, name) + t, ok := b.rustGlobPath(sc.ix.Path, name) if !ok { return "", false } - return b.rustCrateKey(file, path), true + return b.rustCrateKey(t.file, t.path), true } // rustGlobUnitMember returns what a glob use of a module brings for a name @@ -1107,7 +1065,7 @@ func (b *builder) rustGlobMember(sc *scope, name string) (string, bool) { // use or of a glob use of the module, as leaf for use crate::sub::* with // pub mod leaf in sub. func (b *builder) rustGlobUnitMember(mod, name string) (string, bool) { - a := b.globLookup(globKey{a: mod, b: name}, func() globAnswer { + a := b.rustState().glob.lookup(b, globKey{scope: mod, name: name}, func() globAnswer { key, ok := b.unitMember(mod, name) return globAnswer{key: key, ok: ok} }) @@ -1117,8 +1075,8 @@ func (b *builder) rustGlobUnitMember(mod, name string) (string, bool) { // globKey names a glob lookup: the glob path of a file and a first name, or // the glob member of a module and a name. type globKey struct { - path bool - a, b string + path bool + scope, name string } // globAnswer is the answer of a glob lookup, with the least depth that a cut @@ -1135,105 +1093,99 @@ func (a globAnswer) found() bool { return a.ok || a.path.ok } -// keep keeps the answer of a glob lookup for the build. -func (b *builder) keep(k globKey, a globAnswer) { - s := b.rustState() - s.kept[k] = a - b.memo(func() { delete(s.kept, k) }) -} - -// globLookup reads a glob lookup once. Modules and crates can bring each -// other with glob uses, in a diamond or in a cycle, and a walk of each path -// through them grows with the number of paths. So the outermost lookup -// reads each lookup once, and a lookup that it reads again gives its first -// answer. A read of a lookup in progress is a cut. An answer stays for the -// build when each cut under it hit the lookup or a lookup under it, as the -// low link of Tarjan tells, and when no other guard cut it. Such a cut -// leaves out no path, because the lookup in progress reads that path -// itself. -// -// A visited answer is part of the lookup that read it. When that lookup -// stays for the build, a visited answer under it that found its item stays -// too, so a later read takes the same way through a cycle of crates. A -// later read of dep_3 of a cycle dep_0 to dep_3 with pub use dep_d::* in -// dep_3 took dep_0 first and sent the cycle round with no end. Any other -// visited answer under it goes, because a later read with another depth -// would take its low for its own, and it can lack an item that the lookup -// found after it. When the lookup does not stay, their low falls to its -// low, because they lack what it lacks. -func (b *builder) globLookup(k globKey, read func() globAnswer) globAnswer { - s := b.rustState() - if a, ok := s.kept[k]; ok { +// globMemo holds the glob lookups of Rust that a build reads now and has +// read, as lookup says. +type globMemo struct { + // active holds the depth of each glob path and glob member that a + // lookup reads now. depth is the depth of the last one, and low the + // least depth that a cut of the read in progress hit. + active map[globKey]int + depth int + low int + // kept holds the answers of the glob lookups that no cut of an outer + // read gave, for the build. visited holds the other answers of the + // outermost lookup in progress, and visits the order of their reads. + kept map[globKey]globAnswer + visited map[globKey]globAnswer + visits []globKey + // cuts counts the cuts of the glob guards, a part of b.cuts. + cuts int +} + +// lookup reads a glob lookup once. Modules and crates can bring each other +// with glob uses, so the outermost lookup reads each lookup once, and a +// lookup that it reads again gives its first answer. An answer stays for +// the build when each cut under it hit the lookup or a lookup under it, as +// the low link of Tarjan tells. docs/projectgraph.md has the rules for the +// visited answers under it. +func (g *globMemo) lookup(b *builder, k globKey, read func() globAnswer) globAnswer { + if a, ok := g.kept[k]; ok { return a } - if d, ok := s.active[k]; ok { - b.globCut(d) + if d, ok := g.active[k]; ok { + g.cut(b, d) return globAnswer{} } - if a, ok := s.visited[k]; ok { - b.globCut(a.low) + if a, ok := g.visited[k]; ok { + g.cut(b, a.low) return a } - if len(s.active) == 0 { + if len(g.active) == 0 { defer func() { - clear(s.visited) - s.visits = s.visits[:0] + clear(g.visited) + g.visits = g.visits[:0] }() } - s.depth++ - d := s.depth - s.active[k] = d - low, cuts, mark := s.low, b.cuts-s.cuts, len(s.visits) - s.low = math.MaxInt + g.depth++ + d := g.depth + g.active[k] = d + low, cuts, mark := g.low, b.cuts-g.cuts, len(g.visits) + g.low = math.MaxInt a := read() - a.low = s.low - s.low = min(low, s.low) - delete(s.active, k) - s.depth-- - under := s.visits[mark:] - if a.low >= d && b.cuts-s.cuts == cuts { - b.keep(k, a) + a.low = g.low + g.low = min(low, g.low) + delete(g.active, k) + g.depth-- + under := g.visits[mark:] + if a.low >= d && b.cuts-g.cuts == cuts { + remember(b, g.kept, k, a) for _, v := range under { - if e, ok := s.visited[v]; ok && e.found() && e.low >= d { - b.keep(v, e) + if e, ok := g.visited[v]; ok && e.found() && e.low >= d { + remember(b, g.kept, v, e) } - delete(s.visited, v) + delete(g.visited, v) } - s.visits = s.visits[:mark] + g.visits = g.visits[:mark] return a } for _, v := range under { - if e, ok := s.visited[v]; ok && e.low > a.low { + if e, ok := g.visited[v]; ok && e.low > a.low { e.low = a.low - s.visited[v] = e + g.visited[v] = e } } - s.visited[k] = a - s.visits = append(s.visits, k) - b.memo(func() { delete(s.visited, k) }) + g.visits = append(g.visits, k) + remember(b, g.visited, k, a) return a } -// globCut counts a read that the guard of a glob path or of a glob member -// cuts at the depth. -func (b *builder) globCut(depth int) { - s := b.rustState() - b.cuts++ - s.cuts++ - s.low = min(s.low, depth) +// cut counts a read that the guard of a glob path or of a glob member cuts +// at the depth. +func (g *globMemo) cut(b *builder, depth int) { + b.cut() + g.cuts++ + g.low = min(g.low, depth) } -// globRead starts a read that the cuts of the glob guards can make -// incomplete. The function that it returns ends the read and reports -// whether no cut of the read hit a module that a lookup read before the -// read began. -func (b *builder) globRead() func() bool { - s := b.rustState() - depth, low := s.depth, s.low - s.low = math.MaxInt +// read starts a read that the cuts of the glob guards can make incomplete. +// The function that it returns ends the read and reports whether no cut of +// the read hit a module that a lookup read before the read began. +func (g *globMemo) read() func() bool { + depth, low := g.depth, g.low + g.low = math.MaxInt return func() bool { - whole := s.low > depth - s.low = min(low, s.low) + whole := g.low > depth + g.low = min(low, g.low) return whole } } diff --git a/projectgraph/values.go b/projectgraph/values.go index 9e2b2bed..6722f4cd 100644 --- a/projectgraph/values.go +++ b/projectgraph/values.go @@ -74,18 +74,37 @@ func (b *builder) callsValue(u UnresolvedSite) bool { // of the name at all. The index marks a field of a type of data, as // Name string, with Data. func (b *builder) valueField(name string) (holds, known bool) { - if b.fieldNames == nil { - b.fieldNames = map[string]bool{} - for key, def := range b.defs { - if def.Kind == fileindex.DefField && profileOf(b.languageOf(key)).functionValues { - b.fieldNames[def.Name] = b.fieldNames[def.Name] || !def.Data - } - } - } + b.readValueNames() holds, known = b.fieldNames[name] return holds, known } +// valueMethod reports whether a method of a language with functionValues +// has the name. +func (b *builder) valueMethod(name string) bool { + b.readValueNames() + return b.methodNames[name] +} + +// readValueNames reads the names of the fields and of the methods of the +// languages with functionValues once. +func (b *builder) readValueNames() { + if b.fieldNames != nil { + return + } + b.fieldNames, b.methodNames = map[string]bool{}, map[string]bool{} + for key, def := range b.defs { + if def.Kind != fileindex.DefField && def.Kind != fileindex.DefMethod || !profileOf(b.languageOf(key)).functionValues { + continue + } + if def.Kind == fileindex.DefField { + b.fieldNames[def.Name] = b.fieldNames[def.Name] || !def.Data + } else { + b.methodNames[def.Name] = true + } + } +} + // rangeRecords returns the records of the index record of a range over a // value that the index cannot show to be data, at the site. A record that // the graph shows to read data has none. Otherwise it is a function-value @@ -119,7 +138,7 @@ func (b *builder) rangeMember(u fileindex.Unresolved, site UnresolvedSite) []Unr return nil } holds, known := b.valueField(u.Method) - method := b.goMethodNamed(u.Method) + method := b.valueMethod(u.Method) var out []UnresolvedSite if holds || !known && !method { out = append(out, site) @@ -154,20 +173,6 @@ func (b *builder) valueData(key string) bool { return def != nil && def.ValueData } -// goMethodNamed reports whether a method of a language with functionValues -// has the name. -func (b *builder) goMethodNamed(name string) bool { - if b.methodNames == nil { - b.methodNames = map[string]bool{} - for key, def := range b.defs { - if def.Kind == fileindex.DefMethod && profileOf(b.languageOf(key)).functionValues { - b.methodNames[def.Name] = true - } - } - } - return b.methodNames[name] -} - // reflection is a package of a language that gives or runs a function // that no reference names, and the methods of it that do it. type reflection struct { diff --git a/projectgraph/walk.go b/projectgraph/walk.go index 06e506d6..599266a9 100644 --- a/projectgraph/walk.go +++ b/projectgraph/walk.go @@ -321,7 +321,7 @@ func (b *builder) defaultValue(module, name string) (string, bool) { // variable that holds a variable of another file walks on to that value. func (b *builder) variableValue(key string) (string, bool) { if b.reading[key] { - b.cuts++ + b.cut() return "", false } b.reading[key] = true From c5ddb59222e379cf1f0b0e6d3949517d95844977 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:14:34 +0000 Subject: [PATCH 60/86] refactor(indexer): share the walks and the predicates of the Go, Rust and JavaScript code - goTypeSpecs walks the type declarations for goFileType and goFileField. - goAssignment gives the names and the values of a declaration to goAssignedType, goLiteralCall and goScopes, and goDeclaration holds the arguments of the add step of goScopes. - goValueCall names the record of a range and of a call of a function value. goValueRecord takes no member that every caller left empty. - rustUseTarget holds the switch of rustShadowed and rustShadowedMethod, and rustRebound reads its binders in a plain loop. - callgraph.IsTypeWrapper is the one table of the TypeScript wrappers. - export returns the record that it adds, so a caller marks it opaque. - pythonMetaclassArg tests metaclass= for both callers. - The hook typedCall and rustConstructorType have the names of what they hold. An unexported option holds the grace of a store write, so the test sets no package variable. - rootmatch reads the language of a file from its index. The long comments of goTypedCall, goValueVisitor and rustScopedType keep the reason, and docs/projectgraph.md keeps the cases. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/callgraph/javascript.go | 15 ++- plugin/indexer/calls.go | 5 +- plugin/indexer/exports.go | 37 +++---- plugin/indexer/golang.go | 29 +++-- plugin/indexer/govalues.go | 184 ++++++++++++++------------------ plugin/indexer/indexer.go | 11 +- plugin/indexer/indexer_test.go | 4 +- plugin/indexer/languages.go | 4 +- plugin/indexer/python.go | 11 +- plugin/indexer/rust.go | 94 ++++++++-------- projectgraph/rootmatch/match.go | 25 ++--- 11 files changed, 206 insertions(+), 213 deletions(-) diff --git a/plugin/callgraph/javascript.go b/plugin/callgraph/javascript.go index 3aa6ea8a..8927f652 100644 --- a/plugin/callgraph/javascript.go +++ b/plugin/callgraph/javascript.go @@ -507,7 +507,7 @@ func objectNames(object *sitter.Node, treeData []byte, depth int) []string { // A type of TypeScript, as { ... } satisfies Actions, or // parentheses, wrap the object and leave its names. parent := object.Parent() - for parent != nil && objectWrappers[parent.Type()] { + for parent != nil && typeWrappers[parent.Type()] { parent = parent.Parent() } switch { @@ -527,13 +527,20 @@ func objectNames(object *sitter.Node, treeData []byte, depth int) []string { return nil } -// objectWrappers hold an object with a type or a check of TypeScript, or -// in parentheses. -var objectWrappers = map[string]bool{ +// typeWrappers hold a value with a type or a check of TypeScript, as +// x satisfies T, x as T and x!, or in parentheses. The value is the value +// of the wrapper. +var typeWrappers = map[string]bool{ "satisfies_expression": true, "as_expression": true, "non_null_expression": true, "parenthesized_expression": true, "type_assertion": true, } +// IsTypeWrapper reports whether a JavaScript node of the type holds a value +// with a type or a check of TypeScript, or in parentheses. +func IsTypeWrapper(nodeType string) bool { + return typeWrappers[nodeType] +} + // jsMethodDefinitionProcessor processes a method of a class or of an object // literal. A method of an object is keyed under the names that hold the // object, as api//run in const api = { run() {} }, as a function that a diff --git a/plugin/indexer/calls.go b/plugin/indexer/calls.go index c738dec8..3ccf48a5 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -32,8 +32,7 @@ func (p *projection) retargetTyped(ctx context.Context, call *fileindex.Call, un } // bindsAsWritten reports whether the call graph binds the names of a call -// as the code does, in a language with declaredAt. The call -// graph reads +// as the code does, in a language with declaredAt. The call graph reads // only some binders of a name, so it can bind a receiver to a parameter or // to the value of a let while a pattern binds the name again, as t in // fn run(t: Local) { if let Some(t) = dep::try_thing() { t.inspect() } }. @@ -117,7 +116,7 @@ func (p *projection) addCalls(ctx context.Context) { p.addRecord(call, fileindex.UnresolvedUnknownReceiver) continue } - if typed := p.rules().typedAt; typed != nil && !typed(p, &call) { + if typed := p.rules().typedCall; typed != nil && !typed(p, &call) { p.addRecord(call, fileindex.UnresolvedUnknownReceiver) continue } diff --git a/plugin/indexer/exports.go b/plugin/indexer/exports.go index dd6e9a79..9f7e2099 100644 --- a/plugin/indexer/exports.go +++ b/plugin/indexer/exports.go @@ -5,6 +5,7 @@ import ( "github.com/safedep/code/fileindex" "github.com/safedep/code/pkg/ts" + "github.com/safedep/code/plugin/callgraph" sitter "github.com/smacker/go-tree-sitter" ) @@ -28,20 +29,22 @@ func (p *projection) local(name string) string { return fileindex.Join(p.cg.FileName, name) } -// export adds the export of a local name as name. A name that an import -// binds is given on, and Item holds the local name when it is not name. -func (p *projection) export(name, local string, line int) { +// export adds the export of a local name as name and returns it. A name +// that an import binds is given on, and Item holds the local name when it +// is not name. +func (p *projection) export(name, local string, line int) *fileindex.Export { if p.bindings[local] { item := local if item == name { item = "" } p.ix.Exports = append(p.ix.Exports, fileindex.Export{Name: name, Item: item, Line: line}) - return + } else { + target := p.local(local) + p.exports[target] = true + p.ix.Exports = append(p.ix.Exports, fileindex.Export{Name: name, Target: target, Line: line}) } - target := p.local(local) - p.exports[target] = true - p.ix.Exports = append(p.ix.Exports, fileindex.Export{Name: name, Target: target, Line: line}) + return &p.ix.Exports[len(p.ix.Exports)-1] } // reexport adds an export of item of the module from as name. An empty @@ -94,8 +97,7 @@ func (p *projection) addExportStatement(statement *sitter.Node) { case opaque(value): // A default of a call, as export default NextAuth({...}), has // no definition. The record keeps it for a rule that roots it. - p.export(defaultExport, defaultExport, line) - p.ix.Exports[len(p.ix.Exports)-1].Opaque = true + p.export(defaultExport, defaultExport, line).Opaque = true } return } @@ -171,8 +173,7 @@ func (p *projection) exportDeclarators(declaration *sitter.Node, line int) { p.exportPattern(pattern, []string{p.text(value)}, line) default: for _, name := range patternNames(p, pattern) { - p.export(name, name, line) - p.ix.Exports[len(p.ix.Exports)-1].Opaque = true + p.export(name, name, line).Opaque = true } } } @@ -187,23 +188,15 @@ func (p *projection) exportValue(name, local string, value *sitter.Node, line in if value != nil && value.Type() == "identifier" && p.text(value) != local { p.export(name, p.text(value), line) } - p.export(name, local, line) + export := p.export(name, local, line) if value != nil && value.Type() != "identifier" && opaque(value) { - p.ix.Exports[len(p.ix.Exports)-1].Opaque = true + export.Opaque = true } } -// typeWrappers hold a value with a type or a check of TypeScript, as -// x satisfies T, x as T and x!, or in parentheses. The value is the value -// of the wrapper. -var typeWrappers = map[string]bool{ - "satisfies_expression": true, "as_expression": true, "non_null_expression": true, - "parenthesized_expression": true, "type_assertion": true, -} - // unwrap returns the value that type wrappers hold. func unwrap(value *sitter.Node) *sitter.Node { - for value != nil && typeWrappers[value.Type()] { + for value != nil && callgraph.IsTypeWrapper(value.Type()) { value = value.NamedChild(0) } return value diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 4dc07c66..60159d34 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -1,6 +1,7 @@ package indexer import ( + "iter" "slices" "strings" "unicode" @@ -25,7 +26,7 @@ var goRules = languageRules{ signature: goSignature, exported: goExported, definitions: goDefinitions, - typedAt: goTypedCall, + typedCall: goTypedCall, literalCall: goLiteralCall, visitors: []func(p *projection) visitor{referenceVisitor, goValueVisitor}, references: &referenceSyntax{ @@ -164,17 +165,29 @@ func (p *projection) goFileType(t *sitter.Node) *sitter.Node { return nil } name := p.text(t) - for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { - if statement.Type() != "type_declaration" { - continue + for spec := range p.goTypeSpecs() { + if p.text(spec.ChildByFieldName("name")) == name { + return spec.ChildByFieldName("type") } - for _, spec := range ts.NamedChildren(statement) { - if spec.Type() == "type_spec" && p.text(spec.ChildByFieldName("name")) == name { - return spec.ChildByFieldName("type") + } + return nil +} + +// goTypeSpecs returns the type specs of the type declarations at the top +// level of the file. +func (p *projection) goTypeSpecs() iter.Seq[*sitter.Node] { + return func(yield func(*sitter.Node) bool) { + for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { + if statement.Type() != "type_declaration" { + continue + } + for _, spec := range ts.NamedChildren(statement) { + if spec.Type() == "type_spec" && !yield(spec) { + return + } } } } - return nil } // goVarSpecs returns the specs of a var declaration. A declaration with diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index b4082b45..5b2a7ce6 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -260,7 +260,7 @@ func (p *projection) goBinding(caller, name string, at uint32) (goScope, bool) { // goAssignedType reports whether the one variable of a name has the type // that an assignment gives it, as the link step reads it. A declared type -// is the type of the variable, so it must be that type: a declared +// is the type of the variable, so it must be that type. A declared // interface holds any type that implements it. With no declared type, // each write of the variable must give that type, as the declaration // cl := o.c with a field c *client and cl = &client{}. A write whose type @@ -278,29 +278,17 @@ func (p *projection) goAssignedType(caller, name string, binding goScope, read f } agree := true walkBody(fn.ChildByFieldName("body"), nil, func(node *sitter.Node) { - var names []*sitter.Node - var right *sitter.Node + var names, values []*sitter.Node switch node.Type() { - case "short_var_declaration", "assignment_statement": - if left := node.ChildByFieldName("left"); left != nil { - names = ts.NamedChildren(left) - } - right = node.ChildByFieldName("right") - case "var_spec": - names, right = childrenByField(node, "name"), node.ChildByFieldName("value") + case "short_var_declaration", "assignment_statement", "var_spec": + names, values = goAssignment(node) case "range_clause", "receive_statement": - if left := node.ChildByFieldName("left"); left != nil { - names = ts.NamedChildren(left) - } + names = ts.NamedChildren(node.ChildByFieldName("left")) } i := slices.IndexFunc(names, func(n *sitter.Node) bool { return p.text(n) == name }) if i < 0 { return } - var values []*sitter.Node - if right != nil && node.Type() != "range_clause" && node.Type() != "receive_statement" { - values = ts.NamedChildren(right) - } if len(values) != len(names) { agree = false return @@ -345,20 +333,15 @@ func (p *projection) goFieldValueType(caller string, value *sitter.Node) (filein // goFileField returns the type of a field of a struct that the file // declares, or nil. func (p *projection) goFileField(structName, field string) *sitter.Node { - for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { - if statement.Type() != "type_declaration" { + for spec := range p.goTypeSpecs() { + t := spec.ChildByFieldName("type") + if t == nil || t.Type() != "struct_type" || p.text(spec.ChildByFieldName("name")) != structName { continue } - for _, spec := range ts.NamedChildren(statement) { - t := spec.ChildByFieldName("type") - if spec.Type() != "type_spec" || t == nil || t.Type() != "struct_type" || p.text(spec.ChildByFieldName("name")) != structName { - continue - } - for _, f := range p.goStructFields(t) { - for _, n := range childrenByField(f, "name") { - if p.text(n) == field { - return f.ChildByFieldName("type") - } + for _, f := range p.goStructFields(t) { + for _, n := range childrenByField(f, "name") { + if p.text(n) == field { + return f.ChildByFieldName("type") } } } @@ -432,16 +415,10 @@ func (p *projection) goTypedAt(caller, name string, at uint32) bool { // goTypedCall reports whether the type that the link step reads for each // typed name that starts a target or the receiver of a call holds at the -// call. The call graph can keep a raw target for a name that a function -// literal binds, as x//run for x.run() in go func(x B) { x.run() }(b), -// and the link step then reads the type of the parameter x of the caller. -// It can also bind a local that shadows an import to the import, as -// log//printf for log.printf() in for _, log := range ls. Such a call gets -// the target of the local, which the link step types or records. When the -// binding in scope has another type of its own, as file in a second -// file, err := os.Create(p) of a sibling block, the call gets the target -// of that binding under its alias. A receiver that binds no local at the -// call, as url in url, err := url.Parse(raw), names the import. +// call. The call graph can keep a raw target, as x//run for a parameter x +// of a function literal, or bind a local that shadows an import to the +// import. Such a call gets the target of the local, under an alias when its +// binding has a type of its own. docs/projectgraph.md has the cases. func goTypedCall(p *projection, call *fileindex.Call) bool { at := p.byteAt(call.Line, call.Column) names := strings.Split(call.Receiver, ".") @@ -512,19 +489,10 @@ func (p *projection) goAlias(caller, name string, at uint32) (string, bool) { } // goValueVisitor records the Go code that calls a function value with no -// call record of its own. The link step makes each record a function-value -// record, which concerns every target in a search with a source. -// -// - A range over a value that the source does not show to be data, as -// seq in func consume(seq func(func(int) bool)) { for range seq {} }. -// The range calls the value, as seq(yield) does. A range over a member -// keeps the member name in Method, and the link step drops the record -// when each field of the name holds data. -// - A call of a field of an anonymous struct of the file, as s.run() in -// for _, s := range []struct{ run func() }{...}. No definition names -// such a field, so the link step cannot see it. -// - A call of a dereference or a receive, as (*p)() or (<-ch)(), which -// the call graph does not record. +// call record of its own: a range over a value that the source does not +// show to be data, a call of a field of an anonymous struct of the file, +// and a call of a dereference or a receive. The link step makes each record +// a function-value record. docs/projectgraph.md has the forms. func goValueVisitor(p *projection) visitor { return func(node *sitter.Node, kind string) { switch kind { @@ -534,7 +502,7 @@ func goValueVisitor(p *projection) visitor { return } if r, ok := p.goRangeRecord(value, 0); ok { - p.goRangeValue(r) + p.addValueCall(r) } case "call_expression": function := goUnparen(node.ChildByFieldName("function")) @@ -542,9 +510,9 @@ func goValueVisitor(p *projection) visitor { case function == nil: case function.Type() == "unary_expression" && goOperator(function) == "<-", function.Type() == "unary_expression" && goOperator(function) == "*" && p.goValueOperand(function.ChildByFieldName("operand")): - p.goValueRecord(node, "") + p.goValueRecord(node) case function.Type() == "selector_expression" && p.goAnonFields()[p.text(function.ChildByFieldName("field"))]: - p.goValueRecord(node, "") + p.goValueRecord(node) } } } @@ -579,20 +547,21 @@ func (p *projection) goValueOperand(operand *sitter.Node) bool { } // goValueRecord adds the record of a call of a function value at the node. -func (p *projection) goValueRecord(node *sitter.Node, member string) { - p.goRangeValue(goRange{node: node, reason: fileindex.UnresolvedComputedName, method: member}) +func (p *projection) goValueRecord(node *sitter.Node) { + p.addValueCall(goValueCall{node: node, reason: fileindex.UnresolvedComputedName}) } -// goRange is the record of a range over a value: its site and what the -// link step reads to decide it, as fileindex.UnresolvedRangeValue says. -type goRange struct { +// goValueCall is the record of a call of a function value, by a call or by +// a range over the value: its site and what the link step reads to decide +// it, as fileindex.UnresolvedRangeValue says. +type goValueCall struct { node *sitter.Node reason fileindex.UnresolvedReason receiver, method string } -// goRangeValue adds the record of a range. -func (p *projection) goRangeValue(r goRange) { +// addValueCall adds the record of a call of a function value. +func (p *projection) addValueCall(r goValueCall) { site := ast.GetNodePosition(r.node) p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ Caller: p.caller(p.innermost(r.node.StartByte()), site), @@ -614,30 +583,33 @@ func (p *projection) goRangeValue(r goRange) { // the reason range-value, which the link step decides from the fields, the // types and the functions of the graph. A local that a call gives has the // record at the call. -func (p *projection) goRangeRecord(value *sitter.Node, depth int) (goRange, bool) { +func (p *projection) goRangeRecord(value *sitter.Node, depth int) (goValueCall, bool) { value = goUnparen(value) - plain := goRange{node: value, reason: fileindex.UnresolvedComputedName} - if value == nil || depth > maxReferenceSegments { - return plain, value != nil + if value == nil { + return goValueCall{}, false + } + plain := goValueCall{node: value, reason: fileindex.UnresolvedComputedName} + if depth > maxReferenceSegments { + return plain, true } if p.goDataValue(value) { - return goRange{}, false + return goValueCall{}, false } switch value.Type() { case "slice_expression": - return goRange{}, false + return goValueCall{}, false case "identifier": binding, ok := p.goBinding(p.innermost(value.StartByte()), p.text(value), value.StartByte()) switch { case !ok: - return goRange{}, false + return goValueCall{}, false case binding.typ != nil: return p.goRangeType(binding.typ, value) case binding.value != nil: return p.goRangeRecord(binding.value, depth+1) } case "selector_expression": - return goRange{node: value, reason: fileindex.UnresolvedRangeValue, receiver: callgraph.ClipText(p.text(value.ChildByFieldName("operand"))), method: p.text(value.ChildByFieldName("field"))}, true + return goValueCall{node: value, reason: fileindex.UnresolvedRangeValue, receiver: callgraph.ClipText(p.text(value.ChildByFieldName("operand"))), method: p.text(value.ChildByFieldName("field"))}, true case "index_expression": if t := p.goElementType(value.ChildByFieldName("operand")); t != nil { return p.goRangeType(t, value) @@ -645,7 +617,7 @@ func (p *projection) goRangeRecord(value *sitter.Node, depth int) (goRange, bool case "type_assertion_expression": return p.goRangeType(value.ChildByFieldName("type"), value) case "call_expression": - return goRange{node: value, reason: fileindex.UnresolvedRangeValue}, true + return goValueCall{node: value, reason: fileindex.UnresolvedRangeValue}, true } return plain, true } @@ -653,14 +625,14 @@ func (p *projection) goRangeRecord(value *sitter.Node, depth int) (goRange, bool // goRangeType returns the record of a range over a value of a declared // type, or false for a type of data. A named type that the file does not // declare is a record with its name. -func (p *projection) goRangeType(t, at *sitter.Node) (goRange, bool) { +func (p *projection) goRangeType(t, at *sitter.Node) (goValueCall, bool) { if p.goDataField(t) { - return goRange{}, false + return goValueCall{}, false } if name := p.goTypeName(t); name != "" && t.Type() != "function_type" { - return goRange{node: at, reason: fileindex.UnresolvedRangeValue, method: name}, true + return goValueCall{node: at, reason: fileindex.UnresolvedRangeValue, method: name}, true } - return goRange{node: at, reason: fileindex.UnresolvedComputedName}, true + return goValueCall{node: at, reason: fileindex.UnresolvedComputedName}, true } // goElementType returns the declared type of an element of a map, a slice @@ -764,17 +736,7 @@ func goLiteralCall(p *projection, call fileindex.Call) bool { walkBody(fn.ChildByFieldName("body"), nil, func(node *sitter.Node) { switch node.Type() { case "short_var_declaration", "var_spec": - left, right := node.ChildByFieldName("left"), node.ChildByFieldName("right") - var names []*sitter.Node - if node.Type() == "var_spec" { - names, right = childrenByField(node, "name"), node.ChildByFieldName("value") - } else if left != nil { - names = ts.NamedChildren(left) - } - var values []*sitter.Node - if right != nil { - values = ts.NamedChildren(right) - } + names, values := goAssignment(node) for i, n := range names { if p.text(n) == name { declared++ @@ -794,6 +756,16 @@ func goLiteralCall(p *projection, call fileindex.Call) bool { return literal && !other && declared == 1 } +// goAssignment returns the names and the values of a short variable +// declaration, an assignment statement, or a var or a const spec. +func goAssignment(node *sitter.Node) (names, values []*sitter.Node) { + switch node.Type() { + case "var_spec", "const_spec": + return childrenByField(node, "name"), ts.NamedChildren(node.ChildByFieldName("value")) + } + return ts.NamedChildren(node.ChildByFieldName("left")), ts.NamedChildren(node.ChildByFieldName("right")) +} + // byteAt returns the start of the innermost named node at a line and a // column of a site, as a call or a record holds them. func (p *projection) byteAt(line, column int) uint32 { @@ -832,25 +804,21 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { if function == nil || function.Type() != "function_declaration" && function.Type() != "method_declaration" { return locals } - add := func(nodes []*sitter.Node, from uint32, block, typ *sitter.Node, redeclares bool, list *sitter.Node) { + add := func(block *sitter.Node, d goDeclaration) { start := goBlockStart(block) - var values []*sitter.Node - if list != nil { - values = ts.NamedChildren(list) - } - for i, n := range nodes { + for i, n := range d.names { if n.Type() != "identifier" { continue } name := p.text(n) // A short variable declaration assigns a name that its block // declares already, and binds no new variable. - if redeclares && slices.ContainsFunc(locals[name], func(s goScope) bool { return s.block == start && s.to == block.EndByte() }) { + if d.redeclares && slices.ContainsFunc(locals[name], func(s goScope) bool { return s.block == start && s.to == block.EndByte() }) { continue } - scope := goScope{from: from, to: block.EndByte(), block: start, typ: typ} - if len(values) == len(nodes) { - scope.value = values[i] + scope := goScope{from: d.from, to: block.EndByte(), block: start, typ: d.typ} + if len(d.values) == len(d.names) { + scope.value = d.values[i] } locals[name] = append(locals[name], scope) } @@ -870,20 +838,22 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { } switch node.Type() { case "parameter_declaration", "variadic_parameter_declaration": - add(childrenByField(node, "name"), goParamOrigin(block), block, node.ChildByFieldName("type"), false, nil) + add(block, goDeclaration{names: childrenByField(node, "name"), from: goParamOrigin(block), typ: node.ChildByFieldName("type")}) case "short_var_declaration": - add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, nil, true, node.ChildByFieldName("right")) + names, values := goAssignment(node) + add(block, goDeclaration{names: names, values: values, from: node.EndByte(), redeclares: true}) case "var_spec", "const_spec": - add(childrenByField(node, "name"), node.EndByte(), block, node.ChildByFieldName("type"), false, node.ChildByFieldName("value")) + names, values := goAssignment(node) + add(block, goDeclaration{names: names, values: values, from: node.EndByte(), typ: node.ChildByFieldName("type")}) case "type_spec", "type_alias": - add(childrenByField(node, "name"), node.StartByte(), block, nil, false, nil) + add(block, goDeclaration{names: childrenByField(node, "name"), from: node.StartByte()}) case "range_clause", "receive_statement": if goDeclares(node) { - add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block, nil, false, nil) + add(block, goDeclaration{names: ts.NamedChildren(node.ChildByFieldName("left")), from: node.EndByte()}) } case "type_switch_statement": if alias := node.ChildByFieldName("alias"); alias != nil { - add(ts.NamedChildren(alias), alias.EndByte(), block, nil, false, nil) + add(block, goDeclaration{names: ts.NamedChildren(alias), from: alias.EndByte()}) } } for _, child := range ts.NamedChildren(node) { @@ -894,6 +864,16 @@ func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { return locals } +// goDeclaration is a declaration that goScopes reads: the names, their +// values, the start of their scope, their declared type, and whether a +// short variable declaration declares them. +type goDeclaration struct { + names, values []*sitter.Node + from uint32 + typ *sitter.Node + redeclares bool +} + // goOperator returns the operator of a unary expression, as & or <-. func goOperator(node *sitter.Node) string { if op := node.ChildByFieldName("operator"); op != nil { diff --git a/plugin/indexer/indexer.go b/plugin/indexer/indexer.go index b7bb9c7d..ade54491 100644 --- a/plugin/indexer/indexer.go +++ b/plugin/indexer/indexer.go @@ -61,6 +61,10 @@ type Options struct { // because a key of dependency code needs its unit. The report lists // any other import file as skipped. Imports bool + + // saveGrace is the time that a store write has after the context of + // the run ends. Zero selects 10 seconds. + saveGrace time.Duration } // Result is what one run of an Indexer did. @@ -157,6 +161,7 @@ func New(opts Options) (*Indexer, error) { opts.Plugins = slices.Clone(opts.Plugins) opts.MaxFileBytes = cmp.Or(opts.MaxFileBytes, parser.DefaultMaxBytes) opts.Workers = cmp.Or(opts.Workers, runtime.GOMAXPROCS(0)) + opts.saveGrace = cmp.Or(opts.saveGrace, 10*time.Second) return &Indexer{opts: opts, byExtension: byExtension}, nil } @@ -365,7 +370,7 @@ func (r *run) flush(ctx context.Context, batch []pending) error { // with the same deadline does not parse them again. A store that hangs // must not hold the caller long past its budget, so the write ends // saveGrace after ctx. - saveCtx, cancel := graceContext(ctx, saveGrace) + saveCtx, cancel := graceContext(ctx, r.opts.saveGrace) defer cancel() if err := r.save(saveCtx, put, made); err != nil { if ctx.Err() != nil { @@ -376,10 +381,6 @@ func (r *run) flush(ctx context.Context, batch []pending) error { return parseErr } -// saveGrace is the time that a store write has after the context of the -// run ends. -var saveGrace = 10 * time.Second - // graceContext returns a context that ends grace after ctx ends, or grace // after the call if ctx has ended before it. The timer stops when the // returned context ends, so a write that returns early holds nothing. diff --git a/plugin/indexer/indexer_test.go b/plugin/indexer/indexer_test.go index 7d54f4f9..cada2dc0 100644 --- a/plugin/indexer/indexer_test.go +++ b/plugin/indexer/indexer_test.go @@ -191,9 +191,6 @@ func (s *hangingStore) Put(ctx context.Context, _ []*fileindex.Index) error { } func TestIndexerEndsAHangingStoreWriteAfterTheGrace(t *testing.T) { - defer func(grace time.Duration) { saveGrace = grace }(saveGrace) - saveGrace = 50 * time.Millisecond - dir := t.TempDir() writeFiles(t, dir, map[string]string{"a.js": "function a() {}\n"}) ctx, cancel := context.WithCancel(context.Background()) @@ -204,6 +201,7 @@ func TestIndexerEndsAHangingStoreWriteAfterTheGrace(t *testing.T) { _, err := runIndexer(t, ctx, appFileSystem(t, dir), Options{ Languages: languages(t, core.LanguageCodeJavascript), Store: &hangingStore{Store: memstore.New(), cancel: cancel}, + saveGrace: 50 * time.Millisecond, }) done <- err }() diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index f478ab7b..83f2dd0e 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -46,12 +46,12 @@ type languageRules struct { // if let Some(t) = x. The call graph then binds a call on the name to // the value of another binding. rebound func(ctx context.Context, p *projection, call fileindex.Call, name string) bool - // typedAt reports whether the declared type that the link step reads + // typedCall reports whether the declared type that the link step reads // for the receiver of a call holds at the call, and can retarget the // call to the binding in scope. The Go index records the typed locals // of a function with no extent, so an inner block can bind the name of // the receiver again. A false result makes the call a record. - typedAt func(p *projection, call *fileindex.Call) bool + typedCall func(p *projection, call *fileindex.Call) bool // literalCall reports whether a call runs a function literal whose // calls the index gives to the caller already, as f() after // f := func() {...} in Go. Such a call needs no edge and no record. diff --git a/plugin/indexer/python.go b/plugin/indexer/python.go index a5771a04..5ce83118 100644 --- a/plugin/indexer/python.go +++ b/plugin/indexer/python.go @@ -125,8 +125,7 @@ func pythonCheck(p *projection, holder *sitter.Node) bool { switch holder.Type() { case "keyword_argument": list := holder.Parent() - name := holder.ChildByFieldName("name") - return name != nil && p.text(name) == "metaclass" && list != nil && list.Parent() != nil && list.Parent().Type() == "class_definition" + return p.pythonMetaclassArg(holder) && list != nil && list.Parent() != nil && list.Parent().Type() == "class_definition" case "tuple": if holder = holder.Parent(); holder == nil || holder.Type() != "argument_list" { return false @@ -151,13 +150,19 @@ func pythonMetaclass(p *projection, class *sitter.Node) string { return "" } for _, arg := range ts.NamedChildren(list) { - if name := arg.ChildByFieldName("name"); arg.Type() == "keyword_argument" && name != nil && p.text(name) == "metaclass" { + if p.pythonMetaclassArg(arg) { return p.text(arg.ChildByFieldName("value")) } } return "" } +// pythonMetaclassArg reports whether an argument is metaclass=. +func (p *projection) pythonMetaclassArg(arg *sitter.Node) bool { + name := arg.ChildByFieldName("name") + return arg.Type() == "keyword_argument" && name != nil && p.text(name) == "metaclass" +} + // pythonAssigned returns the name of a top-level assignment, as x in x = f(). func pythonAssigned(_ *projection, statement *sitter.Node) []*sitter.Node { if statement.Type() != "expression_statement" || statement.NamedChildCount() == 0 { diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 2cc91d63..e42cc60f 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -370,15 +370,11 @@ func (p *projection) rustDeclaredType(binder *sitter.Node, bounds map[string]str } // rustScopedType returns a type that a node names, as the link step reads -// it. The link step resolves a type at the root of the file, or as a type of -// an inline module that holds the node. So Local in fn run() { type Local = -// dep::Thing; let t: Local = ... } resolved to a struct Local of the -// root. A call on t then took the method of that struct with no record, and -// a method of dep::Thing read as not-found. So when a scope that holds the -// node declares the first name of the type, the type is "", and the call -// keeps the target of the call graph or its record. A use that declares the -// name gives its path instead, as crate::store::Item for Item after use -// crate::store::Item in an inline module. +// it. The link step resolves Local at the root. A local type alias of the +// name, as in fn run() { type Local = dep::Thing; let t: Local }, must +// not take a struct Local of the root. So a type whose first name a scope +// of the node declares is "", or the path of a use that declares the name. +// docs/projectgraph.md has the rule. func (p *projection) rustScopedType(node *sitter.Node, typ string) string { first, rest, nested := strings.Cut(typ, "::") if typ == "" || !p.rustScopeDeclares(node, nil, first) { @@ -572,19 +568,16 @@ func rustRebound(ctx context.Context, p *projection, call fileindex.Call, name s return true } var lets []*sitter.Node - if slices.ContainsFunc(binders[name], func(binder *sitter.Node) bool { + for _, binder := range binders[name] { item := binder.Type() == "static_item" || binder.Type() == "const_item" switch { case !item && !startsBefore(binder, call): - return false + continue case !rustPlainBinder(binder): return true case binder.Type() == "let_declaration" && endsBefore(binder, call): lets = append(lets, binder) } - return false - }) { - return true } if len(lets) < 2 { return false @@ -644,7 +637,7 @@ func (p *projection) rustValueType(binders map[string][]*sitter.Node, value *sit if typ := p.rustResultOf(path, p.text(fn.ChildByFieldName("name"))); typ != "" { return typ } - return rustConstructorPath(path, p.text(fn)) + return rustConstructorType(path, p.text(fn)) } } return "" @@ -654,11 +647,11 @@ func (p *projection) rustValueType(binders map[string][]*sitter.Node, value *sit // whatever type the caller infers, as Default::default(). var rustGenericTraits = map[string]bool{"Default": true, "From": true, "Into": true, "FromStr": true, "TryFrom": true, "FromIterator": true} -// rustConstructorPath returns a function of a type as a type of its own, +// rustConstructorType returns a function of a type as a type of its own, // as A::new for let t = A::new() when A is a type of another file. Two lets // whose values call the same function of the same type hold one type. A // trait of std that gives an inferred type names no type. -func rustConstructorPath(typ, fn string) string { +func rustConstructorType(typ, fn string) string { last := rustLastName(typ) if last == "" || !unicode.IsUpper(rune(last[0])) || rustGenericTraits[last] { return "" @@ -814,12 +807,11 @@ func rustBoundName(pattern *sitter.Node) *sitter.Node { // rustShadowed fixes a target that the call graph finds for the path of a // call in an outer scope, when a scope between the call and that scope -// binds the first name of the path again. The call graph reads no use and -// no type parameter. So util::g() after use crate::deep::util in a block -// went to the module util of the file, and the real target read as -// not-found. A use gives the target through the path of the use. A type -// parameter names a type that the call does not know, so the call is a -// record. +// binds the first name of the path again, as util::g() after use +// crate::deep::util in a block. The call graph reads no use, so it gives +// util::g() the module util of the file. A use gives the target through +// the path of the use. A type parameter names a type that the call does not +// know, so the call is a record. func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) bool { node := p.descendant(callee) if node != nil && rustMethod(node) != nil { @@ -839,17 +831,12 @@ func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) if !ok || !fileindex.HasKeyPrefix(scope, p.cg.FileName) { continue } - binder, use := p.rustRebinder(node, p.syntax[scope], names[0]) - switch { - case binder == nil: - case !use: + rebound, ok := p.rustUseTarget(node, p.syntax[scope], names[0], names[1:]...) + if !ok { return false - default: - namespace, ok := p.rustUseNamespace(binder, names[0]) - if !ok { - return false - } - call.Targets[i] = fileindex.Join(namespace, names[1:]...) + } + if rebound != "" { + call.Targets[i] = rebound } } return true @@ -857,32 +844,45 @@ func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) // rustShadowedMethod fixes a target of a method call that the call graph // finds as a method of a type of an outer scope, when a scope between the -// call and that scope binds the name of the type again. So x.save() after -// let x = Item::new() and use crate::store::Item in mod deep went to the -// Item of the parent of deep, and the real save read as not-found. +// call and that scope binds the name of the type again, as x.save() after +// let x = Item::new() and use crate::store::Item in mod deep. The call graph +// reads no use, so it gives x.save() the Item of the parent of deep. func (p *projection) rustShadowedMethod(call *fileindex.Call, node *sitter.Node) bool { for i, target := range call.Targets { class := fileindex.Parent(target) if def := p.defs[class]; def == nil || def.Kind != fileindex.DefClass || !fileindex.HasKeyPrefix(class, p.cg.FileName) { continue } - name := fileindex.Last(class) - binder, use := p.rustRebinder(node, p.syntax[fileindex.Parent(class)], name) - switch { - case binder == nil: - case !use: + rebound, ok := p.rustUseTarget(node, p.syntax[fileindex.Parent(class)], fileindex.Last(class), fileindex.Last(target)) + if !ok { return false - default: - namespace, ok := p.rustUseNamespace(binder, name) - if !ok { - return false - } - call.Targets[i] = fileindex.Join(namespace, fileindex.Last(target)) + } + if rebound != "" { + call.Targets[i] = rebound } } return true } +// rustUseTarget returns the target of the names under a name that a use +// between a node and a scope binds again, or "" when no scope between them +// binds the name. It reports false when an item or a type parameter binds +// the name, because the call then names a type that it does not know. +func (p *projection) rustUseTarget(node, scope *sitter.Node, name string, rest ...string) (string, bool) { + binder, use := p.rustRebinder(node, scope, name) + switch { + case binder == nil: + return "", true + case !use: + return "", false + } + namespace, ok := p.rustUseNamespace(binder, name) + if !ok { + return "", false + } + return fileindex.Join(namespace, rest...), true +} + // rustRebinder returns the innermost use or item with type parameters // between a node and the definition of a scope that binds a name, and // whether it is a use. A nil scope is the file. diff --git a/projectgraph/rootmatch/match.go b/projectgraph/rootmatch/match.go index d141a93a..c6523fdb 100644 --- a/projectgraph/rootmatch/match.go +++ b/projectgraph/rootmatch/match.go @@ -81,17 +81,15 @@ type Project struct { // context ends. func Find(ctx context.Context, p Project, rs []rules.Rule) ([]Root, []modules.Problem, error) { f := &finder{ - Project: p, - languages: map[string]langcode.Code{}, - indexes: map[string]*fileindex.Index{}, - seen: map[Root]bool{}, - defs: map[string]*fileindex.Definition{}, - calls: map[*fileindex.Call][]string{}, - deps: map[langcode.Code]map[string]bool{}, - imported: map[langcode.Code]map[string]bool{}, + Project: p, + indexes: map[string]*fileindex.Index{}, + seen: map[Root]bool{}, + defs: map[string]*fileindex.Definition{}, + calls: map[*fileindex.Call][]string{}, + deps: map[langcode.Code]map[string]bool{}, + imported: map[langcode.Code]map[string]bool{}, } for _, ix := range p.Indexes { - f.languages[ix.Path] = ix.Language f.indexes[ix.Path] = ix for i := range ix.Definitions { f.defs[ix.Definitions[i].Key] = &ix.Definitions[i] @@ -117,9 +115,8 @@ func state[T any](p Project, code langcode.Code) *T { type finder struct { Project - languages map[string]langcode.Code - indexes map[string]*fileindex.Index - defs map[string]*fileindex.Definition + indexes map[string]*fileindex.Index + defs map[string]*fileindex.Definition // calls holds the keys that each call resolves to. calls map[*fileindex.Call][]string // deps holds the packages that the manifests declare, and imported the @@ -150,7 +147,7 @@ func (f *finder) addAt(r rules.Rule, key string, site Site) { // addFile marks the module top level of a file whose language the rule // takes. func (f *finder) addFile(r rules.Rule, file string) { - if lang, ok := f.languages[file]; ok && r.Applies(lang) { + if ix := f.indexes[file]; ix != nil && r.Applies(ix.Language) { f.add(r, file) } } @@ -358,7 +355,7 @@ func (f *finder) addPythonEntry(r rules.Rule, dir, entry string) { if !ok || strings.HasSuffix(file, "/") { return } - if lang, ok := f.languages[file]; !ok || !r.Applies(lang) { + if ix := f.indexes[file]; ix == nil || !r.Applies(ix.Language) { return } f.add(r, file) From cbe713dca99550977b3e4837d5baf50f67c00133 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:17:11 +0000 Subject: [PATCH 61/86] docs(projectgraph): state the comments and the case descriptions in the present tense The comments of the branch say what the code does and why, in the present tense, and keep to six lines. The detail of the Go stubs moves to docs/projectgraph.md, which holds it. A case description that names the result without its rule marks it with "Without the rule". The descriptions of the test-dir cases name the paths of their projects. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 2 +- lang/rust_resolvers.go | 4 +- plugin/callgraph/rust.go | 3 +- projectgraph/golang.go | 14 ++----- projectgraph/namespaces.go | 4 +- projectgraph/rust.go | 23 +++++------ .../cases/go/function-no-body/expect.yaml | 6 +-- .../cases/go/range-over-func/expect.yaml | 40 +++++++++---------- .../cases/go/source-local-func/expect.yaml | 30 +++++++------- .../cases/go/typed-local-shadow/expect.yaml | 39 +++++++++--------- .../sveltekit-opaque-exports/expect.yaml | 2 +- .../javascript/sveltekit-test-dir/expect.yaml | 6 +-- .../rust/generic-shadows-type/expect.yaml | 4 +- .../rust/glob-shadows-outer-type/expect.yaml | 6 +-- .../cases/rust/shadowed-let-type/expect.yaml | 8 ++-- .../cases/rust/use-shadows-module/expect.yaml | 4 +- .../cases/rust/use-type-receiver/expect.yaml | 2 +- 17 files changed, 92 insertions(+), 105 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index cb900faf..91e83bc0 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -806,7 +806,7 @@ skip a test path, because a route can have one, as `app/api/test`. The cases `typescript/next-route-test-dir` and `javascript/sveltekit-test-dir` show it. A component file holds a `markup` record, so this root makes each JavaScript target that the search does not find read `uncertain`. A static import -of a component file already did that in most such projects. The rule keeps +of a component file does that too in most such projects. The rule keeps the fallback on, because a Vite app starts at `index.html`, which no rule reads. The rules `node.sveltekit.route` and `node.astro.page` mark the route files of SvelteKit and the pages of Astro, as `+page.ts`, diff --git a/lang/rust_resolvers.go b/lang/rust_resolvers.go index e78816b2..7daed14e 100644 --- a/lang/rust_resolvers.go +++ b/lang/rust_resolvers.go @@ -113,8 +113,8 @@ func rustInlineModules(data *[]byte, node *sitter.Node) []string { // module as a path from the module of the file, as self::outer::x for // super::x in mod outer { mod inner { .. } }. The index keeps the uses of a // file in one scope, which reads such a path from the module of the file. -// So self::x in mod m named the x of the file, and a call through it lost -// its edge to m::x. +// A scope that reads self::x in mod m from the file names the x of the +// file, not m::x. func rustFromFile(path string, mods []string) string { parts := strings.Split(path, "::") ups := 0 diff --git a/plugin/callgraph/rust.go b/plugin/callgraph/rust.go index 24fd282e..b25055ef 100644 --- a/plugin/callgraph/rust.go +++ b/plugin/callgraph/rust.go @@ -128,8 +128,7 @@ func rustTokenTreeProcessor(node *sitter.Node, treeData []byte, currentNamespace // rustPathToken reports whether a token of a macro can start or continue a // path: a name, or $crate, which names the crate of the macro as crate does. -// Without $crate, $crate::sink() of a macro read as a call of the name sink, -// which the file of the macro does not bind. +// $crate::sink() names sink of the crate of the macro. func rustPathToken(token *sitter.Node, treeData []byte) bool { return token.Type() == "identifier" || token.Type() == "metavariable" && token.Content(treeData) == "$crate" } diff --git a/projectgraph/golang.go b/projectgraph/golang.go index f5049181..b564e4e5 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -478,17 +478,9 @@ func (b *builder) linkGoStub(ix *fileindex.Index, site UnresolvedSite) bool { // scopeGoStub gives the record of a Go function with no body the scope of // the code that runs in its place, when no package of the graph holds // that code. The receiver of the record names the package of a -// //go:linkname target. -// -// - Code of the standard library calls back only a method of an exported -// name, as goOutsideRecords argues. So a linkname into the standard -// library, or a stub of assembly in it, gets the callback. A path with -// no dot can also name a package of a module, as app/internal/impl in -// the module app, so the path must name no package of the graph. -// - The assembly of a package of a unit calls code of its package and of -// the runtime. So its record concerns what the code of its unit reaches. -// - A linkname into another package and the assembly of the project can -// call any function, so their records concern every target. +// //go:linkname target. A record into the standard library is a callback. +// The assembly of a unit concerns what its unit reaches. Any other record +// concerns every target. docs/projectgraph.md has the rules. func (b *builder) scopeGoStub(ix *fileindex.Index, site *UnresolvedSite) { unit, _ := fileindex.ParseUnit(ix.Unit) std := site.Receiver != "" && site.Receiver != ix.Package && modules.IsStdPath(site.Receiver) && len(b.resolveModules(ix, site.Receiver, modules.FormImport)) == 0 diff --git a/projectgraph/namespaces.go b/projectgraph/namespaces.go index 4eee1c03..87af0d25 100644 --- a/projectgraph/namespaces.go +++ b/projectgraph/namespaces.go @@ -61,8 +61,8 @@ func (b *builder) resolveQualified(family langcode.Code, name string) (string, b key, rest := u.key, parts[i:] // A Rust use can bind a module, as util of use deep::util. A walk // that ends on a unit fails, so the path through such a binding - // steps from unit to unit here. Without the step, a path of a - // chained use or of a glob use named no module. + // steps from unit to unit here. The step lets a path of a chained + // use or of a glob use name its module. for len(rest) > 0 && b.units[key] != nil && len(b.units[key].names[rest[0]]) == 0 { member, ok := b.unitMember(key, rest[0]) if !ok { diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 57a81dee..d981498f 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -545,10 +545,10 @@ func (r rustResolver) Module(importer, spec string) (string, string, bool) { } // A use can bind an item of the crate, as fl of use crate::ffi::flush // as fl after pub use dep as ffi. A call of fl links the key itself, - // so the key needs its node. Without it the edge reached no node, and - // the target read as not-found. A module of a unit, as html of - // dep::html, runs no code, and its node would get a record of the unit - // for a walk that ends on a module. + // so the key needs its node. An edge to a key with no node leaves the + // target not-found. A module of a unit, as html of dep::html, runs no + // code, and its node would get a record of the unit for a walk that ends + // on a module. key := r.b.rustCrateKey(t.file, t.path) if r.b.rustUnitModule(t.file, t.path) { return key, "crate", true @@ -1018,8 +1018,8 @@ func (b *builder) rustExports(mod, name string, seen map[string]bool) bool { } // rustNestedName reports whether an inline module of the graph defines a -// type of the name. Most names in a Rust file name no such type, and a walk -// up the classes that hold each name cost a fifth of the link step. +// type of the name. Most names name no such type, so the set skips the walk +// up the classes. func (b *builder) rustNestedName(name string) bool { s := b.rustState() if s.nested == nil { @@ -1035,9 +1035,8 @@ func (b *builder) rustNestedName(name string) bool { // rustCrateMember returns the external key of a path of another crate that // a use of a file of a unit binds, as inner::text of pub use inner::text in -// the prelude of dep. The walk then goes on in the unit of that crate. -// Without the key, dep::prelude::text named nothing, and text::fmt after -// use dep::prelude::* read as a crate text. +// the prelude of dep. The walk then goes on in the unit of that crate. The +// key lets dep::prelude::text name the item. func (b *builder) rustCrateMember(sc *scope, path string) (string, bool) { if !b.rustUnitFile(sc.ix) || b.rustLocalRoot(sc.ix.Path, path) { return "", false @@ -1200,10 +1199,8 @@ func (b *builder) rustUnitFile(ix *fileindex.Index) bool { // with the type, as impl Store for FileStore after use crate::FileStore. // Only a use at the root of the file counts. The root cannot define a type // of a name that such a use binds, so the class is an impl block. A use of -// an inline module, as use crate::other::A in mod deep, binds A in deep -// only. Read for the root, it joined a struct A of the root with the A of -// the other module, and a call of a method of the root A went to the other -// A with no record. +// an inline module, as use crate::other::A in mod deep, binds A in that +// module only. func (b *builder) joinImplBlocks() { var groups [][]string for _, p := range b.paths { diff --git a/test/projectgraph/cases/go/function-no-body/expect.yaml b/test/projectgraph/cases/go/function-no-body/expect.yaml index b0f02ce8..2f4bcb40 100644 --- a/test/projectgraph/cases/go/function-no-body/expect.yaml +++ b/test/projectgraph/cases/go/function-no-body/expect.yaml @@ -8,11 +8,11 @@ description: >- its package and of the runtime, so its record concerns what the code of the unit reaches. os/exec.LookPath is uncertain from viaUnit, and os.Getwd is not found from it. The code of the standard library calls back only a method of an exported name, so the record of now is a callback. os.Chdir, - which closer.Close calls, is uncertain from viaLinkname, and os.Getwd is not found from it. With no - record, each search ended at the stub, and each target read not-found. alias is bound by //go:linkname + which closer.Close calls, is uncertain from viaLinkname, and os.Getwd is not found from it. Without the + record, each search ends at the stub, and each target reads not-found. alias is bound by //go:linkname to aliased of its own package main. The path main has no dot, as a path of the standard library, but a package of the graph gives it. So the link step joins alias to aliased with an edge, and os.Mkdir is - found from viaAlias. A callback there read not-found. + found from viaAlias. Without the edge, a callback there gives not-found. units: gomod/github.com/acme/fast@v1.0.0: go:github.com/acme/fast@v1.0.0 roots: diff --git a/test/projectgraph/cases/go/range-over-func/expect.yaml b/test/projectgraph/cases/go/range-over-func/expect.yaml index 9a09b00a..c9006e4e 100644 --- a/test/projectgraph/cases/go/range-over-func/expect.yaml +++ b/test/projectgraph/cases/go/range-over-func/expect.yaml @@ -2,26 +2,26 @@ source: safedep:hand description: >- main ranges over the function walk and over the method value t.All, as Go 1.23 allows. The range calls the function, and the function calls the body of the loop through yield. So the range expression is a - reference, as an argument is, and os.Remove and os/exec.LookPath are found through it. A range clause - with no reference lost both. The range over a slice names no function. No code uses unused, so os.Getwd - is not found. consume ranges over its parameter seq of a function type, and drain over the field each of - a function type. Each range calls a function value that a search from a source does not reach, so it is - a function-value record, and each target is uncertain from consume and from drain. A range with no - record read not-found there. attrs ranges over the member Attr of a range variable n, whose type the - index does not know. The field Attr of node holds a slice, but the method element.Attr can make n.Attr a - method value. So the range is a record with the method name, and os.Chdir is found from attrs through - the name edge. attrsTyped ranges over n.Attr of a parameter n *node. The reference at the site names the - field, which holds data, so the range has no record, and os.Chdir is not found from it. typed ranges - over a parameter of the type Seq of another file, and viaLocal over a local that holds the field Each of - a holder. The index cannot show that either value is data, so each range is a function-value record, and - os.Remove is uncertain from each. The record of viaLocal is at the field that gives the local. - dataRanges ranges over a slice of a slice, a value of the type List of another file, an element of a map - of slices, the result of names and of node.Children, which give slices, and a local that holds such a - result. stdRange ranges over the result of strings.Fields, code outside the graph whose own record - covers a function that it makes. The link step reads the types and the results of the graph and drops - each record. So os.Remove is not found from dataRanges. From stdRange only the callback of - strings.Fields concerns it, because tree.All, a method of an exported name, holds a function-value - record. + reference, as an argument is, and os.Remove and os/exec.LookPath are found through it. Without the + reference, both read not-found. The range over a slice names no function. No code uses unused, so + os.Getwd is not found. consume ranges over its parameter seq of a function type, and drain over the + field each of a function type. Each range calls a function value that a search from a source does not + reach, so it is a function-value record, and each target is uncertain from consume and from drain. + Without the record, each target reads not-found there. attrs ranges over the member Attr of a range + variable n, whose type the index does not know. The field Attr of node holds a slice, but the method + element.Attr can make n.Attr a method value. So the range is a record with the method name, and os.Chdir + is found from attrs through the name edge. attrsTyped ranges over n.Attr of a parameter n *node. The + reference at the site names the field, which holds data, so the range has no record, and os.Chdir is not + found from it. typed ranges over a parameter of the type Seq of another file, and viaLocal over a local + that holds the field Each of a holder. The index cannot show that either value is data, so each range is + a function-value record, and os.Remove is uncertain from each. The record of viaLocal is at the field + that gives the local. dataRanges ranges over a slice of a slice, a value of the type List of another + file, an element of a map of slices, the result of names and of node.Children, which give slices, and a + local that holds such a result. stdRange ranges over the result of strings.Fields, code outside the + graph whose own record covers a function that it makes. The link step reads the types and the results of + the graph and drops each record. So os.Remove is not found from dataRanges. From stdRange only the + callback of strings.Fields concerns it, because tree.All, a method of an exported name, holds a + function-value record. roots: - key: main.go kind: main diff --git a/test/projectgraph/cases/go/source-local-func/expect.yaml b/test/projectgraph/cases/go/source-local-func/expect.yaml index 36a12165..cb768f54 100644 --- a/test/projectgraph/cases/go/source-local-func/expect.yaml +++ b/test/projectgraph/cases/go/source-local-func/expect.yaml @@ -7,21 +7,21 @@ description: >- each function as a value, so os.Remove is found through the reference. A search from run, each, fire, call or pick alone does not reach main. Each call there runs a function value that the search does not reach, so the call is a function-value record, which concerns every target in a search with a source. - Each target is then uncertain. A record with the name alone, as f or fn, gave not-found there. show - calls Name on a range variable of an interface. The field Name of user holds a string, so the call runs - no function value, and os.Getwd is not found from show. closure calls its local f, which holds a - function literal and nothing else. The index gives the calls of the literal to closure, so the call of f - is no record, and os.Getwd is not found from closure. In other.go, serveLocal calls a local of the type - http.HandlerFunc, rangeAssign calls a local that a range assigns after the literal, anonSteps calls the - field act of an anonymous struct, and deref calls (*p). Each call runs a function value, so each is a - function-value record, and os.Getwd is uncertain from each. keepRead reads the field fn of a hook and - calls nothing. The read is a record with no receiver, so the link step does not take it for a call of a - field, and os.Getwd is not found from keepRead. conv.go converts pointers, as (*T)(p) and - *(*uintptr)(unsafe.Pointer(k)). The operand of each dereference is a type and no value, so no call runs - a function value, and os.Getwd is not found from convs. runTable calls the field exec of the anonymous - struct of table, which anon.go declares, and localJob calls the field trigger of a struct type that the - function declares. The index gives each such field a definition under the key struct of its file, so the - link step sees that the call can run a function value. os.Getwd is uncertain from each. + Each target is then uncertain. Without the rule, a record with the name alone, as f or fn, gives + not-found there. show calls Name on a range variable of an interface. The field Name of user holds a + string, so the call runs no function value, and os.Getwd is not found from show. closure calls its local + f, which holds a function literal and nothing else. The index gives the calls of the literal to closure, + so the call of f is no record, and os.Getwd is not found from closure. In other.go, serveLocal calls a + local of the type http.HandlerFunc, rangeAssign calls a local that a range assigns after the literal, + anonSteps calls the field act of an anonymous struct, and deref calls (*p). Each call runs a function + value, so each is a function-value record, and os.Getwd is uncertain from each. keepRead reads the field + fn of a hook and calls nothing. The read is a record with no receiver, so the link step does not take it + for a call of a field, and os.Getwd is not found from keepRead. conv.go converts pointers, as (*T)(p) + and *(*uintptr)(unsafe.Pointer(k)). The operand of each dereference is a type and no value, so no call + runs a function value, and os.Getwd is not found from convs. runTable calls the field exec of the + anonymous struct of table, which anon.go declares, and localJob calls the field trigger of a struct type + that the function declares. The index gives each such field a definition under the key struct of its + file, so the link step sees that the call can run a function value. os.Getwd is uncertain from each. roots: - key: anon.go kind: main diff --git a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml index 191f3194..7ab790b5 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml +++ b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml @@ -1,25 +1,24 @@ source: safedep:hand description: >- - main declares x as a local, and an inner block declares x again as a remote. The call x.run() in the - block runs remote.run, which calls os/exec.LookPath. The index records the typed locals of a function - with no extent, and the link step reads the type of the first one. So the index checks that the binding - of x in scope at the call is the binding whose type the link step reads. Here it is not. The method - value x.stop is an unknown-receiver record with the method name stop, which gets a name edge to - remote.stop. A type of the first x gave not-found for os/exec.LookPath. The call x.run() before the - block keeps its member edge to local.run. pick binds r once and types it by the assignment r = - &remote{}, and named types its named result l by an assignment. Each function binds the name once, so - the assignment types that variable, and the call keeps its member edge. A record there gave a name edge - to the run of the other type, and a target behind it read found. The binding of x in the block of main - has a type of its own, so the index gives the call the target of that binding under an alias, and the - call gets a member edge to remote.run. siblings does the same in each of two blocks. viaLit calls run on - the parameter x of a function literal, which no type of the index names, so the call is a record. - pickIface types r by an assignment, but r holds the interface runner from the field r, so the writes do - not agree and the call is a record too. A type of local for each of the two gave not-found for - os/exec.LookPath. In idiom.go, parse declares url from url.Parse, and the local binds no name at the - call, so the call keeps its edge to the import. viaParam and viaRange bind log, which shadows the import - of log, so the index takes no target of the import. The parameter of the type logger gives a member edge - to logger.Println, and the range variable a record with a name edge. In shadowedParam of main.go a local o shadows - the parameter o, so the field r of the parameter type does not type x, and the call x.run() is a record. + main declares x as a local, and an inner block declares x again as a remote. The call x.run() in the block runs + remote.run, which calls os/exec.LookPath. The index records the typed locals of a function with no extent, and the + link step reads the type of the first one. So the index checks that the binding of x in scope at the call is the + binding whose type the link step reads. Here it is not. The method value x.stop is an unknown-receiver record with + the method name stop, which gets a name edge to remote.stop. Without the rule, a type of the first x gives + not-found for os/exec.LookPath. The call x.run() before the block keeps its member edge to local.run. pick binds r + once and types it by the assignment r = &remote{}, and named types its named result l by an assignment. Each + function binds the name once, so the assignment types that variable, and the call keeps its member edge. Without + the rule, a record there gives a name edge to the run of the other type, and a target behind it reads found. The + binding of x in the block of main has a type of its own, so the index gives the call the target of that binding + under an alias, and the call gets a member edge to remote.run. siblings does the same in each of two blocks. + viaLit calls run on the parameter x of a function literal, which no type of the index names, so the call is a + record. pickIface types r by an assignment, but r holds the interface runner from the field r, so the writes do + not agree and the call is a record too. Without the rule, a type of local for each of the two gives not-found for + os/exec.LookPath. In idiom.go, parse declares url from url.Parse, and the local binds no name at the call, so the + call keeps its edge to the import. viaParam and viaRange bind log, which shadows the import of log, so the index + takes no target of the import. The parameter of the type logger gives a member edge to logger.Println, and the + range variable a record with a name edge. In shadowedParam of main.go a local o shadows the parameter o, so the + field r of the parameter type does not type x, and the call x.run() is a record. roots: - key: idiom.go kind: main diff --git a/test/projectgraph/cases/javascript/sveltekit-opaque-exports/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/expect.yaml index b17afefc..bae1ff91 100644 --- a/test/projectgraph/cases/javascript/sveltekit-opaque-exports/expect.yaml +++ b/test/projectgraph/cases/javascript/sveltekit-opaque-exports/expect.yaml @@ -1,7 +1,7 @@ source: safedep:hand description: >- The route files export values that are no object literal. handle is the result of sequence, and the - index now records the arguments first and second of the call in the export statement as references, so + index records the arguments first and second of the call in the export statement as references, so h1 and h2 are found. other names the variable named, which holds table, so the export reaches table and the rule marks save. PUT takes the default fallback, because handlers has no PUT. GET and actions are results of calls, and the functions that the framework calls on them are no definition that an edge diff --git a/test/projectgraph/cases/javascript/sveltekit-test-dir/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-test-dir/expect.yaml index 4dc87ae4..0dc2b80e 100644 --- a/test/projectgraph/cases/javascript/sveltekit-test-dir/expect.yaml +++ b/test/projectgraph/cases/javascript/sveltekit-test-dir/expect.yaml @@ -1,8 +1,8 @@ source: safedep:hand description: >- - A route of the app has a path under a test path, as app/api/test or src/routes/tests. The route rule - names its files by their path in the app, so it marks them although they are test files, and probe - and render are found. Only the rule node.component skips a file of a test path. + A route of the app has a path under a test path, as src/routes/api/test or src/routes/tests. The + route rule names its files by their path in the app, so it marks them although they are test files, + and probe and render are found. Only the rule node.component skips a file of a test path. test_paths: [test, tests, spec] roots: - key: server.js diff --git a/test/projectgraph/cases/rust/generic-shadows-type/expect.yaml b/test/projectgraph/cases/rust/generic-shadows-type/expect.yaml index 53db1bba..bfdcfcc4 100644 --- a/test/projectgraph/cases/rust/generic-shadows-type/expect.yaml +++ b/test/projectgraph/cases/rust/generic-shadows-type/expect.yaml @@ -3,8 +3,8 @@ description: >- build has the type parameter Local with the bound Maker, which shadows the struct Local of the file. So Local::make() in build runs make of the type that the caller gives, Real here, whose make calls std::fs::create_dir. The make of the struct Local calls std::fs::remove_file and nothing calls it. The call - graph reads no type parameter, so it sends the call to Local::make of the struct, and std::fs::create_dir - reads not-found. The index makes the call a record with the reason unknown-receiver. + graph reads no type parameter, so it sends the call to Local::make of the struct. Without the rule, + std::fs::create_dir reads not-found. The index makes the call a record with the reason unknown-receiver. roots: - key: src/main.rs kind: main diff --git a/test/projectgraph/cases/rust/glob-shadows-outer-type/expect.yaml b/test/projectgraph/cases/rust/glob-shadows-outer-type/expect.yaml index 60808ff4..47dc95ac 100644 --- a/test/projectgraph/cases/rust/glob-shadows-outer-type/expect.yaml +++ b/test/projectgraph/cases/rust/glob-shadows-outer-type/expect.yaml @@ -2,9 +2,9 @@ source: safedep:hand description: >- The inline module m declares a struct A, and its inline module n has use crate::other::*. n sees no item of m with no use, so A in f names the A of src/other.rs, whose run calls std::fs::remove_file. The call graph - reads no use and finds the A of m, whose run calls std::fs::remove_dir. A call of A::new() and of run in f - then goes to m::A with no record, and std::fs::remove_file reads not-found. The index makes each such call a - record, so the target reads found through the low name edges of run. + reads no use and finds the A of m, whose run calls std::fs::remove_dir. Without the rule, a call of A::new() + and of run in f goes to m::A with no record, and std::fs::remove_file reads not-found. The index makes each + such call a record, so the target reads found through the low name edges of run. roots: - key: src/main.rs kind: main diff --git a/test/projectgraph/cases/rust/shadowed-let-type/expect.yaml b/test/projectgraph/cases/rust/shadowed-let-type/expect.yaml index 87f11ecd..dfbfa782 100644 --- a/test/projectgraph/cases/rust/shadowed-let-type/expect.yaml +++ b/test/projectgraph/cases/rust/shadowed-let-type/expect.yaml @@ -2,10 +2,10 @@ source: safedep:hand description: >- go binds t with a let to a Disk and calls t.save(), then binds t again with a second let to a Cloud and calls t.save() again. The second call runs Cloud::save, which calls std::fs::remove_dir_all. The call graph - binds a name to the value of its first let, so it sends both calls to Disk::save. The second call then gets - no record and no edge to Cloud::save, and std::fs::remove_dir_all reads not-found. The index keeps the first - call and makes the second one a record with the reason unknown-receiver, so the second call gets the low - name edges of save. + binds a name to the value of its first let, so it sends both calls to Disk::save. Without the rule, the + second call gets no record and no edge to Cloud::save, and std::fs::remove_dir_all reads not-found. The + index keeps the first call and makes the second one a record with the reason unknown-receiver, so the second + call gets the low name edges of save. roots: - key: src/main.rs kind: main diff --git a/test/projectgraph/cases/rust/use-shadows-module/expect.yaml b/test/projectgraph/cases/rust/use-shadows-module/expect.yaml index c3481f30..36f46030 100644 --- a/test/projectgraph/cases/rust/use-shadows-module/expect.yaml +++ b/test/projectgraph/cases/rust/use-shadows-module/expect.yaml @@ -4,8 +4,8 @@ description: >- The inline module worker has use crate::store::util, and the function flush has the same use in its block. So util::wipe in worker::go and in flush names the module src/store/util.rs, whose wipe calls std::fs::remove_dir_all. The call graph reads no use, so it finds util in the scope of the file and sends both - calls to the inline util. std::fs::remove_dir_all then reads not-found. The index gives each call the path - of its use. + calls to the inline util. Without the rule, std::fs::remove_dir_all reads not-found. The index gives each call + the path of its use. roots: - key: src/main.rs kind: main diff --git a/test/projectgraph/cases/rust/use-type-receiver/expect.yaml b/test/projectgraph/cases/rust/use-type-receiver/expect.yaml index 966e6962..a82157fd 100644 --- a/test/projectgraph/cases/rust/use-type-receiver/expect.yaml +++ b/test/projectgraph/cases/rust/use-type-receiver/expect.yaml @@ -4,7 +4,7 @@ description: >- cache::deep has use crate::store::Item, so x in by_new and in by_value holds the Item of the root, and x.save() runs Item::save of the root, which calls std::fs::write. The call graph reads no use, so it finds the Item of cache for Item::new() and for Item, and sends x.save() to cache::Item::save, which calls std::fs::create_dir. - std::fs::write then reads not-found. The index gives the call the path of the use. + Without the rule, std::fs::write reads not-found. The index gives the call the path of the use. roots: - key: src/main.rs kind: main From 5ced53be3415cda481e9fdbe52b2352cb4d2e0a6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:42:49 +0000 Subject: [PATCH 62/86] fix(projectgraph): decide the root fallback per language family The library-surface fallback applied only when no family had an entry root. A Python Lambda handler with no main guard, next to a JavaScript package with a main, then got no root, and since the markup record of a component concerns only JavaScript targets, its targets read as not-found. A family with no entry root of its own now gets the library-surface roots, whatever another family has. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 6 +- projectgraph/roots.go | 27 +++++--- .../python/component-in-frontend/expect.yaml | 9 +++ .../handler-beside-js-package/expect.yaml | 69 +++++++++++++++++++ .../project/handlers/api.py | 5 ++ .../project/ui/package.json | 1 + .../project/ui/src/App.vue | 9 +++ .../project/ui/src/index.js | 4 ++ 8 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 test/projectgraph/cases/python/handler-beside-js-package/expect.yaml create mode 100644 test/projectgraph/cases/python/handler-beside-js-package/project/handlers/api.py create mode 100644 test/projectgraph/cases/python/handler-beside-js-package/project/ui/package.json create mode 100644 test/projectgraph/cases/python/handler-beside-js-package/project/ui/src/App.vue create mode 100644 test/projectgraph/cases/python/handler-beside-js-package/project/ui/src/index.js diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 91e83bc0..9461059f 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -789,7 +789,11 @@ manifest of the project depends on the framework that it names, or when a file imports it. With no root of kind `main`, `manifest` or `framework`, every exported -definition is a root, and `Graph.Stats.RootFallback` says so. Each result +definition is a root, and `Graph.Stats.RootFallback` says so. The link +step decides it per language family. A family with no such root of its +own gets the library-surface roots, whatever another family has, as a +Python Lambda handler next to a JavaScript package with a `main`. The +case `python/handler-beside-js-package` shows it. Each result of a reach query then carries a `root-fallback` uncertainty. A root of a rule with `keeps_fallback` does not count here. Such a rule names files that a framework loads, but not the entry that loads them. diff --git a/projectgraph/roots.go b/projectgraph/roots.go index b928c8a6..6199b169 100644 --- a/projectgraph/roots.go +++ b/projectgraph/roots.go @@ -53,7 +53,11 @@ func (b *builder) markRoots(ctx context.Context) error { for _, r := range b.rules { keeps[r.ID] = r.KeepsFallback } - entry := false + // A family of languages with no entry root of its own gets the + // library-surface roots, whatever another family has. A Lambda handler + // in Python has no root that a rule marks, and the main of a + // JavaScript package next to it says nothing of the Python code. + entries := map[langcode.Code]bool{} for _, r := range found { // The framework calls the functions of the value of an opaque // export, and no edge names them. The record keeps a target that @@ -66,7 +70,9 @@ func (b *builder) markRoots(ctx context.Context) error { } b.addRootAt(r.Key, r.Kind, r.Rule, Site(r.Site)) helper := b.profileAt(r.Key).helperProgram - entry = entry || r.Kind != RootTest && !keeps[r.Rule] && (helper == nil || !helper(fileindex.Unit(r.Key))) + if r.Kind != RootTest && !keeps[r.Rule] && (helper == nil || !helper(fileindex.Unit(r.Key))) { + entries[b.familyOf(r.Key)] = true + } // A Go program initializes its package before main runs, so the // module top level of each file of the package is a root too. if u := b.unitOf[fileindex.Unit(r.Key)]; r.Kind == RootMain && u != nil && b.profileAt(r.Key).loadsPackages { @@ -75,15 +81,12 @@ func (b *builder) markRoots(ctx context.Context) error { } } } - if entry { - return nil - } - - b.g.Stats.RootFallback = true + b.g.Stats.RootFallback = len(entries) == 0 for _, ix := range indexes { - if b.test[ix.Path] || isDeclarationFile(ix.Path) { + if b.test[ix.Path] || isDeclarationFile(ix.Path) || entries[ix.Language.Family()] { continue } + b.g.Stats.RootFallback = true b.addRoot(ix.Path, RootLibrarySurface, string(RootLibrarySurface)) for def := range ix.CodeDefinitions() { if isSurfaceDefinition(def, ix.Path) { @@ -222,3 +225,11 @@ func (b *builder) rootLanguages() []langcode.Code { } return slices.Sorted(maps.Keys(set)) } + +// familyOf returns the language family of the file of a key. +func (b *builder) familyOf(key string) langcode.Code { + if ix := b.byPath[fileindex.Unit(key)]; ix != nil { + return ix.Language.Family() + } + return "" +} diff --git a/test/projectgraph/cases/python/component-in-frontend/expect.yaml b/test/projectgraph/cases/python/component-in-frontend/expect.yaml index 53c85fe5..0b02b9a0 100644 --- a/test/projectgraph/cases/python/component-in-frontend/expect.yaml +++ b/test/projectgraph/cases/python/component-in-frontend/expect.yaml @@ -6,10 +6,15 @@ description: >- edge tells who calls it, and the record keeps it uncertain. Only dead Python code calls jsonschema.validate, but App.vue imports validate from jsonschema and its template calls it, so the record keeps jsonschema.validate uncertain. + The frontend has no entry of its own, as main of a package.json, so its files get the library-surface + roots, and each result carries the root-fallback uncertainty. roots: - key: frontend/src/App.vue kind: framework rule: node.component + - key: frontend/src/App.vue + kind: library-surface + rule: library-surface - key: main.py kind: manifest rule: procfile @@ -46,11 +51,14 @@ results: - caller: main.py//unused site: main.py:8 reached: false + uncertainties: + - kind: root-fallback - target: plug//other status: uncertain uncertainties: - kind: unresolved-call site: frontend/src/App.vue:1 + - kind: root-fallback - target: jsonschema//validate status: uncertain call_sites: @@ -60,3 +68,4 @@ results: uncertainties: - kind: unresolved-call site: frontend/src/App.vue:1 + - kind: root-fallback diff --git a/test/projectgraph/cases/python/handler-beside-js-package/expect.yaml b/test/projectgraph/cases/python/handler-beside-js-package/expect.yaml new file mode 100644 index 00000000..6c5d2338 --- /dev/null +++ b/test/projectgraph/cases/python/handler-beside-js-package/expect.yaml @@ -0,0 +1,69 @@ +source: safedep:hand +description: >- + handlers/api.py holds a Lambda handler with no main guard, so no rule marks a root of Python. ui holds a + JavaScript package with a main. The fallback is decided per language family, so the Python file gets + the library-surface roots although JavaScript has an entry, and yaml.load is found. +roots: + - key: handlers/api.py + kind: library-surface + rule: library-surface + - key: handlers/api.py//handler + kind: library-surface + rule: library-surface + - key: ui/src/App.vue + kind: framework + rule: node.component + - key: ui/src/index.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: handlers/api.py//handler + to: yaml//load + kind: direct + confidence: high + site: handlers/api.py:5 + - from: ui/src/App.vue + to: plug//render + kind: direct + confidence: high + site: ui/src/App.vue:4 + - from: ui/src/index.js + to: ui/src/App.vue + kind: import + confidence: high + site: ui/src/index.js:2 + - from: ui/src/index.js + to: vue//createApp + kind: direct + confidence: high + site: ui/src/index.js:4 + - from: ui/src/index.js + to: vue//createApp//mount + kind: direct + confidence: high + site: ui/src/index.js:4 +unresolved: + - caller: ui/src/App.vue + site: ui/src/App.vue:1 + reason: markup + - caller: ui/src/index.js + site: ui/src/index.js:4 + reason: no-dependency-source + method: mount +results: + - target: yaml//load + status: found + path: + - key: handlers/api.py//handler + root: library-surface + - key: yaml//load + site: handlers/api.py:5 + kind: direct + confidence: high + call_sites: + - caller: handlers/api.py//handler + site: handlers/api.py:5 + reached: true + uncertainties: + - kind: root-fallback diff --git a/test/projectgraph/cases/python/handler-beside-js-package/project/handlers/api.py b/test/projectgraph/cases/python/handler-beside-js-package/project/handlers/api.py new file mode 100644 index 00000000..7dd6bde3 --- /dev/null +++ b/test/projectgraph/cases/python/handler-beside-js-package/project/handlers/api.py @@ -0,0 +1,5 @@ +import yaml + + +def handler(event, context): + return yaml.load(event["body"]) diff --git a/test/projectgraph/cases/python/handler-beside-js-package/project/ui/package.json b/test/projectgraph/cases/python/handler-beside-js-package/project/ui/package.json new file mode 100644 index 00000000..20852d71 --- /dev/null +++ b/test/projectgraph/cases/python/handler-beside-js-package/project/ui/package.json @@ -0,0 +1 @@ +{ "name": "ui", "main": "src/index.js", "dependencies": { "vue": "^3.4.0" } } diff --git a/test/projectgraph/cases/python/handler-beside-js-package/project/ui/src/App.vue b/test/projectgraph/cases/python/handler-beside-js-package/project/ui/src/App.vue new file mode 100644 index 00000000..ebaae70d --- /dev/null +++ b/test/projectgraph/cases/python/handler-beside-js-package/project/ui/src/App.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/python/handler-beside-js-package/project/ui/src/index.js b/test/projectgraph/cases/python/handler-beside-js-package/project/ui/src/index.js new file mode 100644 index 00000000..684d0421 --- /dev/null +++ b/test/projectgraph/cases/python/handler-beside-js-package/project/ui/src/index.js @@ -0,0 +1,4 @@ +import { createApp } from 'vue'; +import App from './App.vue'; + +createApp(App).mount('#app'); From 7b8583a6385673cceee994832ec5fbbfbe5a64ef Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:42:49 +0000 Subject: [PATCH 63/86] fix(reach): scope markup records by the imports of the whole family A language-scoped record concerned a target under a module that the file of the record imports. A component that imports a local module that gives on export * from 'plug' then left plug.t5 not-found when only Python code called it by an edge. The link step now keeps the modules that any file of the family imports or gives on from outside the project, by family. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 9 ++- projectgraph/graph.go | 9 +-- projectgraph/link.go | 40 ++++++++--- projectgraph/reach/uncertainty.go | 4 +- .../component-reexport-star/expect.yaml | 70 +++++++++++++++++++ .../project/package.json | 1 + .../project/src/App.vue | 7 ++ .../project/src/main.js | 4 ++ .../project/src/reexport.js | 1 + .../project/tests/test_api.py | 5 ++ 10 files changed, 131 insertions(+), 19 deletions(-) create mode 100644 test/projectgraph/cases/javascript/component-reexport-star/expect.yaml create mode 100644 test/projectgraph/cases/javascript/component-reexport-star/project/package.json create mode 100644 test/projectgraph/cases/javascript/component-reexport-star/project/src/App.vue create mode 100644 test/projectgraph/cases/javascript/component-reexport-star/project/src/main.js create mode 100644 test/projectgraph/cases/javascript/component-reexport-star/project/src/reexport.js create mode 100644 test/projectgraph/cases/javascript/component-reexport-star/project/tests/test_api.py diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 9461059f..9420153e 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -108,9 +108,12 @@ script reach, would miss a name that the app registers elsewhere, and a target that only such a name reaches would read `not-found`. The markup runs only code of the JavaScript family, so the reach query scopes the record by its language with `Uncertainty.Language`. It concerns a target -under a module that the file imports from outside the project, as -`jsonschema` for `import { validate } from 'jsonschema'`, which -`Graph.Imports` holds. It also concerns a target that a definition of the +under a module that a file of the family imports or gives on from outside +the project, as `jsonschema` for `import { validate } from 'jsonschema'` +or `plug` for `export * from 'plug'`, which `Graph.Imports` holds by +family. The case `javascript/component-reexport-star` shows it. The key +forms of npm and of Go coincide, so an import of `os` in JavaScript also +concerns a Go target under `os`. It also concerns a target that a definition of the family is, or that an edge from a file of the family leads to, and a target with no node. A JavaScript project of component files thus reads `uncertain` for each JavaScript target that it diff --git a/projectgraph/graph.go b/projectgraph/graph.go index c1273ba6..6bac6ebd 100644 --- a/projectgraph/graph.go +++ b/projectgraph/graph.go @@ -267,10 +267,11 @@ type Graph struct { Files map[string]fileindex.FileStatus `json:"files"` Truncated []Truncation `json:"truncated,omitempty"` Problems []modules.Problem `json:"problems,omitempty"` - // Imports holds the module keys that each file with a site of a - // Language scope imports from outside the project, as jsonschema. Such - // a site can run a name of the module that the file imports and no - // edge names, as a function that only the markup of the file calls. + // Imports holds, by language family, the module keys that the files + // of a family with a site of a Language scope import from outside the + // project, as jsonschema. Such a site can run a name of such a module + // that no edge names, as a function that only the markup of a + // component calls. Imports map[string][]string `json:"imports,omitempty"` Stats Stats `json:"stats"` diff --git a/projectgraph/link.go b/projectgraph/link.go index c0afb767..054cfd99 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -445,28 +445,48 @@ func (b *builder) linkAll(ctx context.Context) error { } // scopeToLanguage scopes a site to the targets that code of the language -// family of its file calls, and keeps the modules that the file imports -// from outside the project, as the markup of a component runs only code -// of its script and of what the script imports. +// family of its file calls, as the markup of a component runs only code of +// its family. It keeps the modules that the files of the family import or +// give on from outside the project, as plug for export * from 'plug'. Code +// of the family can call a name of such a module that no edge names. func (b *builder) scopeToLanguage(ix *fileindex.Index, site *UnresolvedSite) { - site.Language = ix.Language.Family() - if _, done := b.g.Imports[ix.Path]; done { + family := ix.Language.Family() + site.Language = family + if _, done := b.g.Imports[string(family)]; done { return } var keys []string - for _, imp := range ix.Imports { - if imp.Kind == fileindex.ImportTypeOnly || imp.Namespace == "" || strings.HasPrefix(imp.Module, ".") { + for _, p := range b.paths { + other := b.byPath[p] + if other == nil || other.Language.Family() != family { continue } - if _, _, ok := b.resolveModule(ix, imp.Module); !ok { - keys = append(keys, imp.Namespace) + for _, imp := range other.Imports { + if imp.Kind != fileindex.ImportTypeOnly { + keys = append(keys, b.outsideModules(other, imp.Module)...) + } + } + for _, ex := range other.Exports { + keys = append(keys, b.outsideModules(other, ex.From)...) } } slices.Sort(keys) if b.g.Imports == nil { b.g.Imports = map[string][]string{} } - b.g.Imports[ix.Path] = slices.Compact(keys) + b.g.Imports[string(family)] = slices.Compact(keys) +} + +// outsideModules returns the key of a module that a file names and that no +// file of the project or of a unit gives, as plug, or nothing. +func (b *builder) outsideModules(ix *fileindex.Index, module string) []string { + if module == "" || strings.HasPrefix(module, ".") { + return nil + } + if _, _, ok := b.resolveModule(ix, module); ok { + return nil + } + return []string{fileindex.Namespace(ix.Language, module)} } // CallbackMethod reports whether code outside the graph that a Callback diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index 89b468c9..71fc890a 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -366,13 +366,13 @@ func (s *sources) language(path string) langcode.Code { } // speaks reports whether the code of a site of a language scope can call -// a target: the file of the site imports the module of the target, a node +// a target: a file of the family imports the module of the target, a node // of the target is a definition in a file of the family, or an edge from // such a file leads to it. A target with no node keeps every uncertainty, // because no edge tells who calls it. func (a *adjacency) speaks(u Uncertainty, target Target) bool { lang := u.Language - if slices.ContainsFunc(a.g.Imports[u.Site.Path], func(module string) bool { return target.under(module) }) { + if slices.ContainsFunc(a.g.Imports[string(lang)], func(module string) bool { return target.under(module) }) { return true } ids := a.match(target) diff --git a/test/projectgraph/cases/javascript/component-reexport-star/expect.yaml b/test/projectgraph/cases/javascript/component-reexport-star/expect.yaml new file mode 100644 index 00000000..2e8d7f86 --- /dev/null +++ b/test/projectgraph/cases/javascript/component-reexport-star/expect.yaml @@ -0,0 +1,70 @@ +source: safedep:hand +description: >- + The template of App.vue calls api.t5, and api is the module reexport.js, which gives on each name of + plug. Only the Python test calls plug.t5 by an edge. A file of the JavaScript family gives on the module + plug, so the markup record concerns plug.t5, and it reads uncertain and not not-found. +test_paths: [tests] +roots: + - key: src/App.vue + kind: framework + rule: node.component + - key: src/main.js + kind: manifest + rule: node.package-json + - key: tests/test_api.py + kind: test + rule: test.files + - key: tests/test_api.py//test_t5 + kind: test + rule: test.files +exact_edges: true +edges: + - from: src/App.vue + to: src/reexport.js + kind: import + confidence: high + site: src/App.vue:2 + - from: src/main.js + to: src/App.vue + kind: import + confidence: high + site: src/main.js:2 + - from: src/main.js + to: vue//createApp + kind: direct + confidence: high + site: src/main.js:4 + - from: src/main.js + to: vue//createApp//mount + kind: direct + confidence: high + site: src/main.js:4 + - from: tests/test_api.py//test_t5 + to: plug//t5 + kind: direct + confidence: high + site: tests/test_api.py:5 +unresolved: + - caller: src/App.vue + site: src/App.vue:1 + reason: markup + - caller: src/main.js + site: src/main.js:4 + reason: no-dependency-source + method: mount +results: + - target: plug//t5 + status: uncertain + test_path: + - key: tests/test_api.py//test_t5 + - key: plug//t5 + site: tests/test_api.py:5 + kind: direct + confidence: high + call_sites: + - caller: tests/test_api.py//test_t5 + site: tests/test_api.py:5 + reached: false + uncertainties: + - kind: unresolved-call + site: src/App.vue:1 diff --git a/test/projectgraph/cases/javascript/component-reexport-star/project/package.json b/test/projectgraph/cases/javascript/component-reexport-star/project/package.json new file mode 100644 index 00000000..7044e959 --- /dev/null +++ b/test/projectgraph/cases/javascript/component-reexport-star/project/package.json @@ -0,0 +1 @@ +{ "name": "component-reexport-star", "main": "src/main.js", "dependencies": { "vue": "^3.4.0" } } diff --git a/test/projectgraph/cases/javascript/component-reexport-star/project/src/App.vue b/test/projectgraph/cases/javascript/component-reexport-star/project/src/App.vue new file mode 100644 index 00000000..30f23b41 --- /dev/null +++ b/test/projectgraph/cases/javascript/component-reexport-star/project/src/App.vue @@ -0,0 +1,7 @@ + + + diff --git a/test/projectgraph/cases/javascript/component-reexport-star/project/src/main.js b/test/projectgraph/cases/javascript/component-reexport-star/project/src/main.js new file mode 100644 index 00000000..684d0421 --- /dev/null +++ b/test/projectgraph/cases/javascript/component-reexport-star/project/src/main.js @@ -0,0 +1,4 @@ +import { createApp } from 'vue'; +import App from './App.vue'; + +createApp(App).mount('#app'); diff --git a/test/projectgraph/cases/javascript/component-reexport-star/project/src/reexport.js b/test/projectgraph/cases/javascript/component-reexport-star/project/src/reexport.js new file mode 100644 index 00000000..ded11f0d --- /dev/null +++ b/test/projectgraph/cases/javascript/component-reexport-star/project/src/reexport.js @@ -0,0 +1 @@ +export * from 'plug'; diff --git a/test/projectgraph/cases/javascript/component-reexport-star/project/tests/test_api.py b/test/projectgraph/cases/javascript/component-reexport-star/project/tests/test_api.py new file mode 100644 index 00000000..de124d17 --- /dev/null +++ b/test/projectgraph/cases/javascript/component-reexport-star/project/tests/test_api.py @@ -0,0 +1,5 @@ +import plug + + +def test_t5(): + plug.t5() From ced83dea479f7e144a2ca787d4d07e02c02c5a9f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:00:22 +0000 Subject: [PATCH 64/86] fix(callgraph): give a Rust value path its namespace as its value The call graph walked the segments of a value path, as config::LIMIT in let limit = config::LIMIT, as names. That bound config as a variable of the function. A later call config::load(limit) then read config as a receiver, got the record unknown-receiver and no edge, and a target under it read not-found. A path now gives the namespace of the path as its value, as the callee of a call does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/callgraph/rust.go | 1 + plugin/callgraph/shapes.go | 20 +++++++ .../cases/rust/value-path-module/expect.yaml | 59 +++++++++++++++++++ .../rust/value-path-module/project/Cargo.toml | 4 ++ .../value-path-module/project/src/config.rs | 13 ++++ .../value-path-module/project/src/main.rs | 8 +++ 6 files changed, 105 insertions(+) create mode 100644 test/projectgraph/cases/rust/value-path-module/expect.yaml create mode 100644 test/projectgraph/cases/rust/value-path-module/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/value-path-module/project/src/config.rs create mode 100644 test/projectgraph/cases/rust/value-path-module/project/src/main.rs diff --git a/plugin/callgraph/rust.go b/plugin/callgraph/rust.go index b25055ef..cef560cd 100644 --- a/plugin/callgraph/rust.go +++ b/plugin/callgraph/rust.go @@ -59,6 +59,7 @@ var rustProfile = shapedProfile(languageProfile{ "macro_definition": {name: named("name"), header: true, reachable: true}, }, skipped: []string{"use_declaration", "extern_crate_declaration", "attribute_item", "inner_attribute_item"}, + paths: []string{"scoped_identifier"}, values: map[string]childRef{"generic_function": named("function"), "type_cast_expression": named("value")}, constructorMethods: []string{"new", "default"}, classKeywords: []string{"Self"}, diff --git a/plugin/callgraph/shapes.go b/plugin/callgraph/shapes.go index 2a4fa2e9..1db36376 100644 --- a/plugin/callgraph/shapes.go +++ b/plugin/callgraph/shapes.go @@ -29,6 +29,10 @@ type syntaxShapes struct { // skipped are node types with no calls below them, such as imports. skipped []string + // paths are the node types that name a value by a path, as config::LIMIT + // in Rust. The value is the namespace of the path. + paths []string + // values are the node types whose value is the expression in one child: // the Rust turbofish, as in iter.collect::>(), and a cast. values map[string]childRef @@ -229,6 +233,9 @@ func shapedProfile(profile languageProfile, shapes syntaxShapes) languageProfile for _, nodeType := range shapes.skipped { processors[nodeType] = skippedProcessor } + for _, nodeType := range shapes.paths { + processors[nodeType] = p.path + } members := maps.Clone(profile.members) if members == nil { @@ -445,6 +452,19 @@ func (p *shapedProcessors) assignment(node *sitter.Node, treeData []byte, curren return newProcessorResult() } +// path gives the namespace of a path as its value. The first segment of a +// path names a module, a type or a crate, not a variable. A walk of the +// segments as names bound config of config::LIMIT as a variable of the +// function, and a later call config::load(t) then read config as a receiver +// and got no edge. +func (p *shapedProcessors) path(node *sitter.Node, treeData []byte, currentNamespace string, callGraph *CallGraph, metadata processorMetadata) processorResult { + namespace := p.resolve(node, treeData, currentNamespace, callGraph, metadata) + if namespace == "" { + return newProcessorResult() + } + return p.valueResult(namespace, callGraph) +} + // scope processes the body of a declaration in the namespace of its name. func (p *shapedProcessors) scope(node *sitter.Node, treeData []byte, currentNamespace string, callGraph *CallGraph, metadata processorMetadata) processorResult { shape := p.shapes.scopes[node.Type()] diff --git a/test/projectgraph/cases/rust/value-path-module/expect.yaml b/test/projectgraph/cases/rust/value-path-module/expect.yaml new file mode 100644 index 00000000..82c4eb98 --- /dev/null +++ b/test/projectgraph/cases/rust/value-path-module/expect.yaml @@ -0,0 +1,59 @@ +source: safedep:hand +description: >- + main reads the constant config::LIMIT and the variant config::Mode::Fast into variables, and then calls + config::load and config::apply. A path that names a value starts with a module, never with a variable. A call + graph that reads config of config::LIMIT as a variable of main binds config there, so config::load(limit) + reads config as a receiver and gets the record unknown-receiver with no edge. std::fs::remove_file and + std::fs::remove_dir then read not-found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/main.rs//main + to: src/config.rs//load + kind: direct + confidence: high + site: src/main.rs:5 + - from: src/main.rs//main + to: src/config.rs//apply + kind: direct + confidence: high + site: src/main.rs:7 +unresolved: [] +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/config.rs//load + site: src/main.rs:5 + kind: direct + confidence: high + - key: std//fs//remove_file + site: src/config.rs:8 + kind: direct + confidence: high + call_sites: + - caller: src/config.rs//load + site: src/config.rs:8 + reached: true + - target: std//fs//remove_dir + status: found + path: + - key: src/main.rs//main + - key: src/config.rs//apply + site: src/main.rs:7 + kind: direct + confidence: high + - key: std//fs//remove_dir + site: src/config.rs:12 + kind: direct + confidence: high + call_sites: + - caller: src/config.rs//apply + site: src/config.rs:12 + reached: true diff --git a/test/projectgraph/cases/rust/value-path-module/project/Cargo.toml b/test/projectgraph/cases/rust/value-path-module/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/value-path-module/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/value-path-module/project/src/config.rs b/test/projectgraph/cases/rust/value-path-module/project/src/config.rs new file mode 100644 index 00000000..c26dc8ab --- /dev/null +++ b/test/projectgraph/cases/rust/value-path-module/project/src/config.rs @@ -0,0 +1,13 @@ +pub const LIMIT: u32 = 3; + +pub enum Mode { + Fast, +} + +pub fn load(_limit: u32) { + let _ = std::fs::remove_file("x"); +} + +pub fn apply(_mode: Mode) { + let _ = std::fs::remove_dir("x"); +} diff --git a/test/projectgraph/cases/rust/value-path-module/project/src/main.rs b/test/projectgraph/cases/rust/value-path-module/project/src/main.rs new file mode 100644 index 00000000..9b69b09d --- /dev/null +++ b/test/projectgraph/cases/rust/value-path-module/project/src/main.rs @@ -0,0 +1,8 @@ +mod config; + +fn main() { + let limit = config::LIMIT; + config::load(limit); + let mode = config::Mode::Fast; + config::apply(mode); +} From e7cc6fbfac5d265bc3fe8ca12e6741c1ae210ffb Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:00:22 +0000 Subject: [PATCH 65/86] fix(indexer): read a Rust self or super path of an inline module from the file The call graph reads no inline module, and the link step reads a path that starts with self or super from the module of the file. So super::Item::new() in mod other went to the external key super//Item//new, and the Item::new of the file read as not-found. The index now writes such a target as a path from the module of the file, as the uses of an inline module already do. RustFromFile moves to langcode, so that lang and the indexer share it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- lang/rust_resolvers.go | 27 +------- langcode/rust.go | 29 ++++++++ .../rust_test.go | 4 +- plugin/indexer/rust.go | 32 ++++++++- .../rust/super-path-inline-module/expect.yaml | 69 +++++++++++++++++++ .../project/Cargo.toml | 4 ++ .../project/src/main.rs | 5 ++ .../project/src/store.rs | 21 ++++++ 8 files changed, 163 insertions(+), 28 deletions(-) create mode 100644 langcode/rust.go rename lang/rust_resolvers_test.go => langcode/rust_test.go (89%) create mode 100644 test/projectgraph/cases/rust/super-path-inline-module/expect.yaml create mode 100644 test/projectgraph/cases/rust/super-path-inline-module/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/super-path-inline-module/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/super-path-inline-module/project/src/store.rs diff --git a/lang/rust_resolvers.go b/lang/rust_resolvers.go index 7daed14e..bba8da65 100644 --- a/lang/rust_resolvers.go +++ b/lang/rust_resolvers.go @@ -4,12 +4,12 @@ import ( "context" "fmt" "slices" - "strings" "unicode" "unicode/utf8" "github.com/safedep/code/core" "github.com/safedep/code/core/ast" + "github.com/safedep/code/langcode" sitter "github.com/smacker/go-tree-sitter" ) @@ -50,7 +50,7 @@ func (r *rustResolvers) ResolveImports(ctx context.Context, tree core.ParseTree) uses := rustUseImports(data, node.ChildByFieldName("argument"), nil, nil) if mods := rustInlineModules(data, node); len(mods) > 0 { for _, imp := range uses { - imp.SetModuleName(rustFromFile(imp.ModuleName(), mods)) + imp.SetModuleName(langcode.RustFromFile(imp.ModuleName(), mods)) } } imports = append(imports, uses...) @@ -109,29 +109,6 @@ func rustInlineModules(data *[]byte, node *sitter.Node) []string { return mods } -// rustFromFile writes a path that starts with self or super in an inline -// module as a path from the module of the file, as self::outer::x for -// super::x in mod outer { mod inner { .. } }. The index keeps the uses of a -// file in one scope, which reads such a path from the module of the file. -// A scope that reads self::x in mod m from the file names the x of the -// file, not m::x. -func rustFromFile(path string, mods []string) string { - parts := strings.Split(path, "::") - ups := 0 - for ups < len(parts) && parts[ups] == "super" { - ups++ - } - switch { - case parts[0] == "self": - return strings.Join(slices.Concat([]string{"self"}, mods, parts[1:]), "::") - case ups == 0: - return path - case ups > len(mods): - return strings.Join(slices.Concat(slices.Repeat([]string{"super"}, ups-len(mods)), parts[ups:]), "::") - } - return strings.Join(slices.Concat([]string{"self"}, mods[:len(mods)-ups], parts[ups:]), "::") -} - // rustUseImports returns the imports of a use tree. The prefix is the path of // the outer use list and the module of each import. The nested path is the // path of a use list inside it. An import node has one node for each part, so diff --git a/langcode/rust.go b/langcode/rust.go new file mode 100644 index 00000000..5aa21027 --- /dev/null +++ b/langcode/rust.go @@ -0,0 +1,29 @@ +package langcode + +import ( + "slices" + "strings" +) + +// RustFromFile writes a path that starts with self or super in an inline +// module as a path from the module of the file, as self::outer::x for +// super::x in mod outer { mod inner { .. } }. mods holds the names of the +// inline modules, the outermost first. The index reads such a path from the +// module of the file. So self::x in mod m named the x of the file, and a +// call through it lost its edge to m::x. +func RustFromFile(path string, mods []string) string { + parts := strings.Split(path, "::") + ups := 0 + for ups < len(parts) && parts[ups] == "super" { + ups++ + } + switch { + case parts[0] == "self": + return strings.Join(slices.Concat([]string{"self"}, mods, parts[1:]), "::") + case ups == 0: + return path + case ups > len(mods): + return strings.Join(slices.Concat(slices.Repeat([]string{"super"}, ups-len(mods)), parts[ups:]), "::") + } + return strings.Join(slices.Concat([]string{"self"}, mods[:len(mods)-ups], parts[ups:]), "::") +} diff --git a/lang/rust_resolvers_test.go b/langcode/rust_test.go similarity index 89% rename from lang/rust_resolvers_test.go rename to langcode/rust_test.go index 93d91741..00ce3ba7 100644 --- a/lang/rust_resolvers_test.go +++ b/langcode/rust_test.go @@ -1,4 +1,4 @@ -package lang +package langcode import ( "testing" @@ -22,7 +22,7 @@ func TestRustFromFile(t *testing.T) { } for _, c := range cases { t.Run(c.path, func(t *testing.T) { - assert.Equal(t, c.want, rustFromFile(c.path, c.mods)) + assert.Equal(t, c.want, RustFromFile(c.path, c.mods)) }) } } diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index e42cc60f..0b03be66 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -822,7 +822,10 @@ func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) } names := strings.Split(p.text(node), "::") switch names[0] { - case "crate", "self", "super", "Self", "": + case "self", "super": + p.rustFromFile(call, node, names) + return true + case "crate", "Self", "": return true } written := strings.Join(names, fileindex.Separator) @@ -842,6 +845,33 @@ func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) return true } +// rustFromFile writes the target of a call whose path starts with self or +// super in an inline module as a path from the module of the file. The call +// graph reads no inline module, and the link step reads such a path from the +// module of the file. So super::Item::new() in mod other went to an external +// super//Item//new, and the real Item::new of the file read as not-found. +// Only the target that the call writes changes. A use already gives its +// target from the module of the file. +func (p *projection) rustFromFile(call *fileindex.Call, node *sitter.Node, names []string) { + var mods []string + for n := node.Parent(); n != nil; n = n.Parent() { + if n.Type() == "mod_item" { + mods = append(mods, p.text(n.ChildByFieldName("name"))) + } + } + if len(mods) == 0 { + return + } + slices.Reverse(mods) + written := strings.Join(names, fileindex.Separator) + path := langcode.RustFromFile(strings.Join(names, "::"), mods) + for i, target := range call.Targets { + if target == written { + call.Targets[i] = strings.ReplaceAll(path, "::", fileindex.Separator) + } + } +} + // rustShadowedMethod fixes a target of a method call that the call graph // finds as a method of a type of an outer scope, when a scope between the // call and that scope binds the name of the type again, as x.save() after diff --git a/test/projectgraph/cases/rust/super-path-inline-module/expect.yaml b/test/projectgraph/cases/rust/super-path-inline-module/expect.yaml new file mode 100644 index 00000000..44801d80 --- /dev/null +++ b/test/projectgraph/cases/rust/super-path-inline-module/expect.yaml @@ -0,0 +1,69 @@ +source: safedep:hand +description: >- + The inline module other of src/store.rs calls super::Item::new and self::inner::run. super of other is the + module store, and self is other. The call graph reads no inline module, and the link step reads such a path + from the module of the file. So super::Item::new goes to the external key super//Item//new, and + std::fs::write reads not-found. self::inner::run names an inner of the module store, which does not exist. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/store.rs//other//sib + to: src/store.rs//Item//new + kind: direct + confidence: high + site: src/store.rs:12 + - from: src/store.rs//other//sib + to: src/store.rs//other//inner//run + kind: direct + confidence: high + site: src/store.rs:13 +no_edges: + - from: src/store.rs//other//sib + to: super//Item//new +unresolved: [] +results: + - target: std//fs//write + status: found + path: + - key: src/main.rs//main + - key: src/store.rs//other//sib + site: src/main.rs:4 + kind: direct + confidence: high + - key: src/store.rs//Item//new + site: src/store.rs:12 + kind: direct + confidence: high + - key: std//fs//write + site: src/store.rs:5 + kind: direct + confidence: high + call_sites: + - caller: src/store.rs//Item//new + site: src/store.rs:5 + reached: true + - target: std//fs//create_dir + status: found + path: + - key: src/main.rs//main + - key: src/store.rs//other//sib + site: src/main.rs:4 + kind: direct + confidence: high + - key: src/store.rs//other//inner//run + site: src/store.rs:13 + kind: direct + confidence: high + - key: std//fs//create_dir + site: src/store.rs:18 + kind: direct + confidence: high + call_sites: + - caller: src/store.rs//other//inner//run + site: src/store.rs:18 + reached: true diff --git a/test/projectgraph/cases/rust/super-path-inline-module/project/Cargo.toml b/test/projectgraph/cases/rust/super-path-inline-module/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/super-path-inline-module/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/super-path-inline-module/project/src/main.rs b/test/projectgraph/cases/rust/super-path-inline-module/project/src/main.rs new file mode 100644 index 00000000..13c8ca1d --- /dev/null +++ b/test/projectgraph/cases/rust/super-path-inline-module/project/src/main.rs @@ -0,0 +1,5 @@ +mod store; + +fn main() { + store::other::sib(); +} diff --git a/test/projectgraph/cases/rust/super-path-inline-module/project/src/store.rs b/test/projectgraph/cases/rust/super-path-inline-module/project/src/store.rs new file mode 100644 index 00000000..94aa043b --- /dev/null +++ b/test/projectgraph/cases/rust/super-path-inline-module/project/src/store.rs @@ -0,0 +1,21 @@ +pub struct Item; + +impl Item { + pub fn new() -> Item { + let _ = std::fs::write("top", "x"); + Item + } +} + +pub mod other { + pub fn sib() { + super::Item::new(); + self::inner::run(); + } + + pub mod inner { + pub fn run() { + let _ = std::fs::create_dir("inner"); + } + } +} From d9f0f30d0421da653baf46c64b2a63c47b9b18db Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:00:22 +0000 Subject: [PATCH 66/86] fix(projectgraph): resolve a Rust path through a glob use of a module of the project crate::m0::f0 after pub use dep::m::* in m0 went to the external key crate//m0//f0 with no record, and dep::m::f0 read not-found. A glob member served only the files of a unit. A path through a module of the project now reads the glob uses of the module when a unit gives the crate. A file still reads a name of its own glob uses in rustScopeFallback, which keeps the record of a single name. A path that starts with crate, self or super and does not resolve is now a record, not an edge to an external key. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/rust.go | 81 +++++++++++++------ .../rust/glob-use-module-reexport/expect.yaml | 73 +++++++++++++++++ .../project/Cargo.toml | 8 ++ .../project/src/ac.rs | 4 + .../project/src/m0.rs | 1 + .../project/src/m1.rs | 1 + .../project/src/main.rs | 7 ++ .../project/vendor/dep/Cargo.toml | 4 + .../project/vendor/dep/src/lib.rs | 1 + .../project/vendor/dep/src/m.rs | 3 + .../summaries/cargo_dep_1.0.0.json | 16 ++++ 11 files changed, 175 insertions(+), 24 deletions(-) create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/project/src/ac.rs create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/project/src/m0.rs create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/project/src/m1.rs create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/src/m.rs create mode 100644 test/projectgraph/cases/rust/glob-use-module-reexport/summaries/cargo_dep_1.0.0.json diff --git a/projectgraph/rust.go b/projectgraph/rust.go index d981498f..b9cd2502 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -159,6 +159,8 @@ type rustState struct { macros map[*space]map[string]string nested map[string]bool glob globMemo + // asker is the file of the innermost resolveRust in progress. + asker string // useScopes holds the classes of each file that hold a use, which are // inline modules. useScopes map[string]map[string]bool @@ -303,7 +305,7 @@ func (b *builder) rustPaths(module, written string) []string { // not-found. func (b *builder) rustScopePaths(importer, written string) []string { out := b.rustPaths(b.rustState().modules[importer], written) - if first := rustFirstName(written); first == "crate" || first == "self" || first == "super" || strings.HasPrefix(written, "::") { + if rustCrateRoot(rustFirstName(written)) || strings.HasPrefix(written, "::") { return out } parts := strings.Split(written, "::") @@ -520,6 +522,8 @@ func (b *builder) resolveRust(importer, written string) (string, bool) { } s.resolving[k] = true defer delete(s.resolving, k) + defer func(asker string) { s.asker = asker }(s.asker) + s.asker = importer for _, p := range b.rustScopePaths(importer, written) { if key, ok := b.resolveQualified(langcode.Rust, p); ok { return key, true @@ -544,16 +548,22 @@ func (r rustResolver) Module(importer, spec string) (string, string, bool) { return t.key, "path", true } // A use can bind an item of the crate, as fl of use crate::ffi::flush - // as fl after pub use dep as ffi. A call of fl links the key itself, - // so the key needs its node. An edge to a key with no node leaves the - // target not-found. A module of a unit, as html of dep::html, runs no - // code, and its node would get a record of the unit for a walk that ends - // on a module. - key := r.b.rustCrateKey(t.file, t.path) - if r.b.rustUnitModule(t.file, t.path) { - return key, "crate", true + // as fl after pub use dep as ffi. + return r.b.rustCrateTarget(t.file, t.path), "crate", true +} + +// rustCrateTarget returns the key of a path of another crate that a use or +// a glob use of a file of the project binds. A call of the name links the +// key itself, so the key needs its node. An edge to a key with no node +// leaves the target not-found. A module of a unit, as html of dep::html, +// runs no code, and its node would get a record of the unit for a walk that +// ends on a module. +func (b *builder) rustCrateTarget(file, path string) string { + key := b.rustCrateKey(file, path) + if b.rustUnitModule(file, path) { + return key } - return r.b.external(key), "crate", true + return b.external(key) } // rustUnitModule reports whether a path of another crate that a file names @@ -739,13 +749,18 @@ func (b *builder) rustCrate(file, module string) (string, string, bool) { // the name does not tell it, because the module of that file is no child. func (b *builder) rustLocalRoot(file, path string) bool { first := rustFirstName(path) - switch first { - case "crate", "self", "super": + if rustCrateRoot(first) { return true } return b.cargoOf(b.spaceOf(file)).Has(first) || b.rustModuleUnit(file, first) != nil || b.rustModuleUnit(file, "crate::"+first) != nil } +// rustCrateRoot reports whether the first name of a path names the crate +// of the file or a module of it by a keyword: crate, self or super. +func rustCrateRoot(first string) bool { + return first == "crate" || first == "self" || first == "super" +} + // rustFirstName returns the first name of a path, as std for ::std::io. func rustFirstName(path string) string { first, _, _ := strings.Cut(strings.TrimPrefix(path, "::"), "::") @@ -768,7 +783,7 @@ func rustCrateNames(path string) (string, []string) { func (r rustResolver) FirstParty(spec string) bool { first := rustFirstName(spec) switch { - case first == "crate" || first == "self" || first == "super": + case rustCrateRoot(first): return true case rustRoots[first]: return false @@ -810,6 +825,12 @@ func (b *builder) rustScopeFallback(sc *scope, caller, first string, rest []stri return []resolution{r} } return []resolution{r, record} + case len(rest) > 0 && rustCrateRoot(first): + // crate, self or super names no other crate. A path of the crate + // that does not resolve, as crate::m0::f0 for a name of a glob use + // of a crate with no source, is a record and no edge to the + // external key crate//m0//f0. + return []resolution{record} case len(rest) > 0: return []resolution{b.importedWalk(sc.ix, target, first, modules.FormImport, rest)} } @@ -888,11 +909,7 @@ func (b *builder) rustMacroCrate(ix *fileindex.Index, name string) (string, bool func (b *builder) rustGlobPath(importer, written string) (rustTarget, bool) { sc := b.scopes[importer] first := rustFirstName(written) - if sc == nil || len(sc.wildcards) == 0 || !b.hasUnits() || strings.HasPrefix(written, "::") { - return rustTarget{}, false - } - switch first { - case "crate", "self", "super": + if sc == nil || len(sc.wildcards) == 0 || !b.hasUnits() || strings.HasPrefix(written, "::") || rustCrateRoot(first) { return rustTarget{}, false } a := b.rustState().glob.lookup(b, globKey{path: true, scope: importer, name: first}, func() globAnswer { @@ -1045,18 +1062,34 @@ func (b *builder) rustCrateMember(sc *scope, path string) (string, bool) { } // rustGlobMember returns the external key of a name that a glob use of a -// file of a unit brings from another crate, as inner::ifn for ifn after -// pub use inner::*. A file of the project reads such a name in rustPath, -// which keeps the record of a single name. +// file brings from another crate, as inner::ifn for ifn after pub use +// inner::*. A path through a module of the project reads it, as +// crate::m0::f0 after pub use dep::m::* in m0. Without it the path read as +// a crate of its own, crate//m0//f0. A file of the project reads a name of +// its own glob uses in rustScopeFallback, which keeps the record of a +// single name and knows the inline modules. func (b *builder) rustGlobMember(sc *scope, name string) (string, bool) { - if !b.rustUnitFile(sc.ix) { + unit := b.rustUnitFile(sc.ix) + if s := b.rustState(); !unit && sc.ix.Path == s.asker { + // A glob lookup keys its answer by the module, not by the asker, + // so it keeps no answer that this skip gave. + if len(s.glob.active) > 0 { + b.cut() + } return "", false } t, ok := b.rustGlobPath(sc.ix.Path, name) - if !ok { + switch { + case !ok: + return "", false + case unit: + return b.rustCrateKey(t.file, t.path), true + case !b.rustUnitModule(t.file, strings.TrimSuffix(t.path, "::"+name)): + // A glob use of a crate with no source can give any name, so a + // later glob use can give the item. The path stays a record. return "", false } - return b.rustCrateKey(t.file, t.path), true + return b.rustCrateTarget(t.file, t.path), true } // rustGlobUnitMember returns what a glob use of a module brings for a name diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/expect.yaml b/test/projectgraph/cases/rust/glob-use-module-reexport/expect.yaml new file mode 100644 index 00000000..68c296fd --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/expect.yaml @@ -0,0 +1,73 @@ +source: safedep:hand +description: >- + src/m0.rs has pub use dep::m::*, and src/m1.rs has pub use other::*. go calls crate::m0::run and + crate::m1::make. The unit cargo:dep@1.0.0 gives dep, and its module m defines run, so crate::m0::run names + dep::m::run. No unit gives other, so its glob use can bring any name, and crate::m1::make is a record. A link + step that reads no glob use of a module of the project for a path gives both calls the external keys + crate//m0//run and crate//m1//make with no record, and dep//m//run reads not-found. +units: + vendor/dep: cargo:dep@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +edges: + - from: src/ac.rs//go + to: dep//m//run + kind: direct + confidence: high + site: src/ac.rs:2 +no_edges: + - from: src/ac.rs//go + to: crate//m0//run + - from: src/ac.rs//go + to: crate//m1//make +unresolved: + - caller: src/ac.rs//go + site: src/ac.rs:3 + reason: unknown-receiver + method: crate +results: + - target: dep//m//run + status: found + path: + - key: src/main.rs//main + - key: src/ac.rs//go + site: src/main.rs:6 + kind: direct + confidence: high + - key: dep//m//run + site: src/ac.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/ac.rs//go + site: src/ac.rs:2 + reached: true + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/ac.rs//go + site: src/main.rs:6 + kind: direct + confidence: high + - key: dep//m//run + site: src/ac.rs:2 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/m.rs//run + site: cargo:dep@1.0.0//src/m.rs:1 + kind: package + confidence: high + - key: std//fs//remove_file + site: cargo:dep@1.0.0//src/m.rs:2 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/m.rs//run + site: cargo:dep@1.0.0//src/m.rs:2 + reached: true diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-module-reexport/project/Cargo.toml new file mode 100644 index 00000000..9fdab07a --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep = "1.0.0" +other = "1.0.0" diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/ac.rs b/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/ac.rs new file mode 100644 index 00000000..64c9477b --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/ac.rs @@ -0,0 +1,4 @@ +pub fn go() { + crate::m0::run(); + crate::m1::make(); +} diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/m0.rs b/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/m0.rs new file mode 100644 index 00000000..24412ca2 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/m0.rs @@ -0,0 +1 @@ +pub use dep::m::*; diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/m1.rs b/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/m1.rs new file mode 100644 index 00000000..6aecbee9 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/m1.rs @@ -0,0 +1 @@ +pub use other::*; diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/main.rs new file mode 100644 index 00000000..c2be9ba7 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/project/src/main.rs @@ -0,0 +1,7 @@ +mod ac; +mod m0; +mod m1; + +fn main() { + ac::go(); +} diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..537b4e47 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..ef2bf252 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/src/lib.rs @@ -0,0 +1 @@ +pub mod m; diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/src/m.rs b/test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/src/m.rs new file mode 100644 index 00000000..487b2a48 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/project/vendor/dep/src/m.rs @@ -0,0 +1,3 @@ +pub fn run() { + let _ = std::fs::remove_file("x"); +} diff --git a/test/projectgraph/cases/rust/glob-use-module-reexport/summaries/cargo_dep_1.0.0.json b/test/projectgraph/cases/rust/glob-use-module-reexport/summaries/cargo_dep_1.0.0.json new file mode 100644 index 00000000..7d589c81 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module-reexport/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//m//run", + "definitions": [ + "cargo:dep@1.0.0//src/m.rs//run" + ], + "reaches": [] + } + ], + "targets": [] +} From 5c8991050e00937fd155bcfbd3657bada497e561 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:14:22 +0000 Subject: [PATCH 67/86] fix(golang): type a write of an element of a map or a slice A variable that the function binds once takes the type of an assignment only when each write gives that type. A write of an element of a map or a slice, as call, dup := p.calls[key], had no known type, so the call on the variable became a record with a name edge to each method of the name. The index now reads the element type that the source declares for the map or the slice: a field of a struct of the file that a parameter reads, a parameter or a local in scope, or a variable of the file. The first name of the comma-ok form takes the type of the element. A write of unknown type still makes the call a record. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 7 +- plugin/indexer/govalues.go | 112 +++++++++++++++--- .../cases/go/typed-local-shadow/expect.yaml | 64 +++++++--- .../go/typed-local-shadow/project/main.go | 21 ++++ 4 files changed, 174 insertions(+), 30 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 9420153e..f0c552c1 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -384,7 +384,12 @@ An assignment can type a variable that the function binds once, as `r` in only when it is the type of the variable. A declared type must be that type, and with no declared type each write must give it, as the field `r` of the struct `opts` of the file does for a parameter `o opts`, and not for -a local that shadows the parameter. A variable that can hold an +a local that shadows the parameter. A write of an element of a map or a +slice gives the element type that the source declares for it, as +`call, dup := p.calls[key]` with a field `calls map[string]*remote`, or a +parameter, a local or a variable of the file of a map or slice type. A +write that constructs or asserts a type, as `&remote{}`, `new(remote)` or +`x.(*remote)`, gives that type. A variable that can hold an interface, as `r` of a field `r runner`, a range variable or `l := ls[0]`, can hold another type, so its call is a record. The case `go/typed-local-shadow` shows each form. diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 5b2a7ce6..689c67d2 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -289,45 +289,129 @@ func (p *projection) goAssignedType(caller, name string, binding goScope, read f if i < 0 { return } - if len(values) != len(names) { + var value *sitter.Node + switch { + case len(values) == len(names): + value = values[i] + case len(values) == 1 && len(names) == 2 && i == 0 && goCommaOk(values[0]): + // The first name of v, ok := m[k] or v, ok := x.(T) takes the + // value of the expression. + value = values[0] + default: agree = false return } - t, ok := p.goValueType(values[i]) - if !ok { - t, ok = p.goFieldValueType(caller, values[i]) - } + t, ok := p.goWriteType(caller, value) agree = agree && ok && !t.Result && t.Type == read.Type }) return agree } +// goCommaOk reports whether an expression gives a second value of the form +// ok when two names take it: an index of a map or a type assertion. +func goCommaOk(value *sitter.Node) bool { + value = goUnparen(value) + return value != nil && (value.Type() == "index_expression" || value.Type() == "type_assertion_expression") +} + +// goWriteType returns the type that a value written to a variable gives: +// the type that it constructs or asserts, the declared type of a field of a +// parameter that it reads, or the declared element type of a map or a slice +// that it indexes, as addConnCall for p.conns[key] with a field +// conns map[string]*addConnCall of the type of the receiver p. +func (p *projection) goWriteType(caller string, value *sitter.Node) (fileindex.Typed, bool) { + value = goUnparen(value) + if t, ok := p.goValueType(value); ok { + return t, true + } + if t, ok := p.goFieldValueType(caller, value); ok { + return t, true + } + if value.Type() != "index_expression" { + return fileindex.Typed{}, false + } + var elem *sitter.Node + switch container := p.goDeclaredType(caller, value.ChildByFieldName("operand")); { + case container == nil: + case container.Type() == "map_type": + elem = container.ChildByFieldName("value") + case container.Type() == "slice_type" || container.Type() == "array_type": + elem = container.ChildByFieldName("element") + } + if typ := p.goTypeName(elem); typ != "" { + return fileindex.Typed{Type: typ}, true + } + return fileindex.Typed{}, false +} + +// goDeclaredType returns the type node that the source declares for a +// value: a parameter or a local of a declared type in scope, a variable +// of the file with a declared type, or a field of a struct of the file +// that a parameter reads. It returns nil for any other value. +func (p *projection) goDeclaredType(caller string, value *sitter.Node) *sitter.Node { + value = goUnparen(value) + if value == nil { + return nil + } + switch value.Type() { + case "identifier": + if binding, ok := p.goBinding(caller, p.text(value), value.StartByte()); ok { + return binding.typ + } + return p.goFileVarType(p.text(value)) + case "selector_expression": + return p.goParamField(caller, value) + } + return nil +} + +// goFileVarType returns the declared type of a variable at the top level +// of the file, or nil. +func (p *projection) goFileVarType(name string) *sitter.Node { + for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { + if statement.Type() != "var_declaration" { + continue + } + for _, spec := range goVarSpecs(statement) { + if slices.ContainsFunc(goNames(spec), func(n *sitter.Node) bool { return p.text(n) == name }) { + return spec.ChildByFieldName("type") + } + } + } + return nil +} + // goFieldValueType returns the declared type of a field that a value // reads, as client for o.c with a parameter o opts and a field c *client // of the struct opts of the file. func (p *projection) goFieldValueType(caller string, value *sitter.Node) (fileindex.Typed, bool) { + if typ := p.goTypeName(p.goParamField(caller, value)); typ != "" { + return fileindex.Typed{Type: typ}, true + } + return fileindex.Typed{}, false +} + +// goParamField returns the declared type of a field of a struct of the file +// that a parameter of the caller reads, as the type of c in o.c, or nil. +func (p *projection) goParamField(caller string, value *sitter.Node) *sitter.Node { def := p.defs[caller] if def == nil || value.Type() != "selector_expression" { - return fileindex.Typed{}, false + return nil } operand, field := value.ChildByFieldName("operand"), value.ChildByFieldName("field") if operand == nil || field == nil || operand.Type() != "identifier" { - return fileindex.Typed{}, false + return nil } i := slices.IndexFunc(def.Params, func(t fileindex.Typed) bool { return t.Name == p.text(operand) }) if i < 0 { - return fileindex.Typed{}, false + return nil } // A local can shadow the parameter, as o in o := other{} after // func f(o opts), and hold a value of another type. if binding, ok := p.goBinding(caller, p.text(operand), operand.StartByte()); !ok || binding.from != goParamOrigin(p.syntax[caller]) { - return fileindex.Typed{}, false - } - t := p.goFileField(def.Params[i].Type, p.text(field)) - if typ := p.goTypeName(t); typ != "" { - return fileindex.Typed{Type: typ}, true + return nil } - return fileindex.Typed{}, false + return p.goFileField(def.Params[i].Type, p.text(field)) } // goFileField returns the type of a field of a struct that the file diff --git a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml index 7ab790b5..4b1f6d37 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml +++ b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml @@ -4,21 +4,25 @@ description: >- remote.run, which calls os/exec.LookPath. The index records the typed locals of a function with no extent, and the link step reads the type of the first one. So the index checks that the binding of x in scope at the call is the binding whose type the link step reads. Here it is not. The method value x.stop is an unknown-receiver record with - the method name stop, which gets a name edge to remote.stop. Without the rule, a type of the first x gives - not-found for os/exec.LookPath. The call x.run() before the block keeps its member edge to local.run. pick binds r - once and types it by the assignment r = &remote{}, and named types its named result l by an assignment. Each - function binds the name once, so the assignment types that variable, and the call keeps its member edge. Without - the rule, a record there gives a name edge to the run of the other type, and a target behind it reads found. The - binding of x in the block of main has a type of its own, so the index gives the call the target of that binding - under an alias, and the call gets a member edge to remote.run. siblings does the same in each of two blocks. - viaLit calls run on the parameter x of a function literal, which no type of the index names, so the call is a - record. pickIface types r by an assignment, but r holds the interface runner from the field r, so the writes do - not agree and the call is a record too. Without the rule, a type of local for each of the two gives not-found for - os/exec.LookPath. In idiom.go, parse declares url from url.Parse, and the local binds no name at the call, so the - call keeps its edge to the import. viaParam and viaRange bind log, which shadows the import of log, so the index - takes no target of the import. The parameter of the type logger gives a member edge to logger.Println, and the - range variable a record with a name edge. In shadowedParam of main.go a local o shadows the parameter o, so the - field r of the parameter type does not type x, and the call x.run() is a record. + the method name stop, which gets a name edge to remote.stop. Without the rule, a type of the first x gives not-found + for os/exec.LookPath. The call x.run() before the block keeps its member edge to local.run. pick binds r once and + types it by the assignment r = &remote{}, and named types its named result l by an assignment. Each function binds + the name once, so the assignment types that variable, and the call keeps its member edge. Without the rule, a record + there gives a name edge to the run of the other type, and a target behind it reads found. The binding of x in the + block of main has a type of its own, so the index gives the call the target of that binding under an alias, and the + call gets a member edge to remote.run. siblings does the same in each of two blocks. viaLit calls run on the + parameter x of a function literal, which no type of the index names, so the call is a record. pickIface types r by + an assignment, but r holds the interface runner from the field r, so the writes do not agree and the call is a + record too. Without the rule, a type of local for each of the two gives not-found for os/exec.LookPath. In idiom.go, + parse declares url from url.Parse, and the local binds no name at the call, so the call keeps its edge to the + import. viaParam and viaRange bind log, which shadows the import of log, so the index takes no target of the import. + The parameter of the type logger gives a member edge to logger.Println, and the range variable a record with a name + edge. In shadowedParam of main.go a local o shadows the parameter o, so the field r of the parameter type does not + type x, and the call x.run() is a record. pool.get declares call from an element of the field calls + map[string]*remote, in the form call, dup := p.calls[key], and fromSlice declares r from an element of a parameter + []*remote. The declared element type of the map and of the slice gives the write its type, so each write gives + remote, and each call keeps its member edge to remote.run. Without the rule, a write of no known type there makes + the call a record with a name edge to local.run. roots: - key: idiom.go kind: main @@ -70,7 +74,21 @@ edges: kind: member confidence: high site: main.go:43 + - from: main.go//pool//get + to: main.go//remote//run + kind: member + confidence: high + site: main.go:95 + - from: main.go//fromSlice + to: main.go//remote//run + kind: member + confidence: high + site: main.go:103 no_edges: + - from: main.go//pool//get + to: main.go//local//run + - from: main.go//fromSlice + to: main.go//local//run - from: main.go//local//run to: os//exec//LookPath - from: main.go//pick @@ -323,3 +341,19 @@ results: - caller: main.go//local//run site: main.go:10 reached: true + - target: os//Getwd + sources: [main.go//pool//get] + sources_only: true + status: not-found + call_sites: + - caller: main.go//local//run + site: main.go:10 + reached: false + - target: os//Getwd + sources: [main.go//fromSlice] + sources_only: true + status: not-found + call_sites: + - caller: main.go//local//run + site: main.go:10 + reached: false diff --git a/test/projectgraph/cases/go/typed-local-shadow/project/main.go b/test/projectgraph/cases/go/typed-local-shadow/project/main.go index 81c99a53..ee281e1b 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/project/main.go +++ b/test/projectgraph/cases/go/typed-local-shadow/project/main.go @@ -81,3 +81,24 @@ func shadowedParam(o opts, rs []runner) { x.run() } } + +type pool struct { + calls map[string]*remote +} + +func (p *pool) get(key string) { + call, dup := p.calls[key] + if !dup { + call = &remote{} + p.calls[key] = call + } + call.run() +} + +func fromSlice(rs []*remote, i int) { + r := rs[i] + if r == nil { + r = &remote{} + } + r.run() +} From 0d6b7eba0db41040bf0db993bc4223598d58e809 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 22:42:01 +0000 Subject: [PATCH 68/86] fix(golang): type a range variable and an element of a declared container A range variable and an element of a map or a slice had no type, so a call or a range over a member of such a value was a record with the method name, and its name edges reached methods of other types. The index now gives a range variable the key or the element type that the source declares for the map, the slice or the array, and gives the receiver ns[0] of ns []*node the element type. A member of such a receiver resolves through its type, and a field of data drops the range. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 4 +- plugin/indexer/golang.go | 37 +++++++++- plugin/indexer/govalues.go | 25 ++++--- .../cases/go/range-over-func/expect.yaml | 69 ++++++++----------- .../cases/go/range-over-func/project/main.go | 5 ++ .../cases/go/source-local-func/expect.yaml | 16 +---- .../stdlib-callback-method-record/expect.yaml | 31 ++++----- .../cases/go/typed-local-shadow/expect.yaml | 42 ++++++----- .../go/typed-local-shadow/project/main.go | 8 +++ 9 files changed, 138 insertions(+), 99 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index f0c552c1..59eb6c4f 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -389,7 +389,9 @@ slice gives the element type that the source declares for it, as `call, dup := p.calls[key]` with a field `calls map[string]*remote`, or a parameter, a local or a variable of the file of a map or slice type. A write that constructs or asserts a type, as `&remote{}`, `new(remote)` or -`x.(*remote)`, gives that type. A variable that can hold an +`x.(*remote)`, gives that type. The same element types type a range +variable, as `c` in `for _, c := range cs` with `cs []*Client`, and the +receiver of a member, as `ns[0]` in `ns[0].Attr` with `ns []*node`. A variable that can hold an interface, as `r` of a field `r runner`, a range variable or `l := ls[0]`, can hold another type, so its call is a record. The case `go/typed-local-shadow` shows each form. diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 60159d34..a6e7f55a 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -426,7 +426,7 @@ func goSignature(p *projection, def *fileindex.Definition, node *sitter.Node) { if len(results) > 1 { def.Results = results } - def.Locals, p.goOrigins[def.Key] = p.goLocals(node.ChildByFieldName("body")) + def.Locals, p.goOrigins[def.Key] = p.goLocals(def.Key, node.ChildByFieldName("body")) if node.ChildByFieldName("body") == nil { p.goStub(def, node) } @@ -592,7 +592,7 @@ func (p *projection) goResults(result *sitter.Node) []string { // the body shares the scope of the body, so its names count too. It also // returns the origin of each local: the start of its scope as goScopes // records it, or goAssignedOrigin for an assignment. -func (p *projection) goLocals(body *sitter.Node) ([]fileindex.Typed, []uint32) { +func (p *projection) goLocals(key string, body *sitter.Node) ([]fileindex.Typed, []uint32) { var locals []fileindex.Typed var origins []uint32 add := func(typed []fileindex.Typed, origin uint32) { @@ -620,11 +620,44 @@ func (p *projection) goLocals(body *sitter.Node) ([]fileindex.Typed, []uint32) { if left := node.ChildByFieldName("left"); left != nil { add(p.goAssigned(ts.NamedChildren(left), node.ChildByFieldName("right")), origin) } + case "range_clause": + if goDeclares(node) { + add(p.goRangeTyped(key, node), node.EndByte()) + } } }) return locals, origins } +// goRangeTyped returns the typed names that a range clause declares over a +// map, a slice or an array whose element type the source declares, as c +// in for _, c := range cs with cs []*Client. The key of a map takes the key +// type, and the value of a map or a slice the element type. +func (p *projection) goRangeTyped(key string, clause *sitter.Node) []fileindex.Typed { + left := clause.ChildByFieldName("left") + container := p.goDeclaredType(key, clause.ChildByFieldName("right")) + if left == nil || container == nil { + return nil + } + var types []*sitter.Node + switch container.Type() { + case "map_type": + types = []*sitter.Node{container.ChildByFieldName("key"), container.ChildByFieldName("value")} + case "slice_type", "array_type": + types = []*sitter.Node{nil, container.ChildByFieldName("element")} + } + var typed []fileindex.Typed + for i, name := range ts.NamedChildren(left) { + if i >= len(types) || name.Type() != "identifier" || p.text(name) == "_" { + continue + } + if typ := p.goTypeName(types[i]); typ != "" { + typed = append(typed, fileindex.Typed{Name: p.text(name), Type: typ}) + } + } + return typed +} + // goAssigned returns the typed names of an assignment. One call on the // right of more names gives each name the result at its position, and the // blank name _ takes none. diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 689c67d2..540a9851 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -115,6 +115,10 @@ func (p *projection) goOperand(caller string, node *sitter.Node) (string, bool) return p.goTypeTarget(caller, node.ChildByFieldName("type")) case "call_expression": return p.goResultOperand(caller, node) + case "index_expression": + // An element of a map or a slice of a declared type, as ns[0] of + // ns []*node, holds a value of the element type. + return p.goTypeTarget(caller, p.goElementOf(caller, node)) } return "", false } @@ -330,18 +334,23 @@ func (p *projection) goWriteType(caller string, value *sitter.Node) (fileindex.T if value.Type() != "index_expression" { return fileindex.Typed{}, false } - var elem *sitter.Node - switch container := p.goDeclaredType(caller, value.ChildByFieldName("operand")); { + if typ := p.goTypeName(p.goElementOf(caller, value)); typ != "" { + return fileindex.Typed{Type: typ}, true + } + return fileindex.Typed{}, false +} + +// goElementOf returns the element type that the source declares for the +// map or the slice that an index expression indexes, or nil. +func (p *projection) goElementOf(caller string, index *sitter.Node) *sitter.Node { + switch container := p.goDeclaredType(caller, index.ChildByFieldName("operand")); { case container == nil: case container.Type() == "map_type": - elem = container.ChildByFieldName("value") + return container.ChildByFieldName("value") case container.Type() == "slice_type" || container.Type() == "array_type": - elem = container.ChildByFieldName("element") - } - if typ := p.goTypeName(elem); typ != "" { - return fileindex.Typed{Type: typ}, true + return container.ChildByFieldName("element") } - return fileindex.Typed{}, false + return nil } // goDeclaredType returns the type node that the source declares for a diff --git a/test/projectgraph/cases/go/range-over-func/expect.yaml b/test/projectgraph/cases/go/range-over-func/expect.yaml index c9006e4e..cd1c512c 100644 --- a/test/projectgraph/cases/go/range-over-func/expect.yaml +++ b/test/projectgraph/cases/go/range-over-func/expect.yaml @@ -1,27 +1,25 @@ source: safedep:hand description: >- - main ranges over the function walk and over the method value t.All, as Go 1.23 allows. The range calls - the function, and the function calls the body of the loop through yield. So the range expression is a - reference, as an argument is, and os.Remove and os/exec.LookPath are found through it. Without the - reference, both read not-found. The range over a slice names no function. No code uses unused, so - os.Getwd is not found. consume ranges over its parameter seq of a function type, and drain over the - field each of a function type. Each range calls a function value that a search from a source does not - reach, so it is a function-value record, and each target is uncertain from consume and from drain. - Without the record, each target reads not-found there. attrs ranges over the member Attr of a range - variable n, whose type the index does not know. The field Attr of node holds a slice, but the method - element.Attr can make n.Attr a method value. So the range is a record with the method name, and os.Chdir - is found from attrs through the name edge. attrsTyped ranges over n.Attr of a parameter n *node. The - reference at the site names the field, which holds data, so the range has no record, and os.Chdir is not - found from it. typed ranges over a parameter of the type Seq of another file, and viaLocal over a local - that holds the field Each of a holder. The index cannot show that either value is data, so each range is - a function-value record, and os.Remove is uncertain from each. The record of viaLocal is at the field - that gives the local. dataRanges ranges over a slice of a slice, a value of the type List of another - file, an element of a map of slices, the result of names and of node.Children, which give slices, and a - local that holds such a result. stdRange ranges over the result of strings.Fields, code outside the - graph whose own record covers a function that it makes. The link step reads the types and the results of - the graph and drops each record. So os.Remove is not found from dataRanges. From stdRange only the - callback of strings.Fields concerns it, because tree.All, a method of an exported name, holds a - function-value record. + main ranges over the function walk and over the method value t.All, as Go 1.23 allows. The range calls the function, + and the function calls the body of the loop through yield. So the range expression is a reference, as an argument + is, and os.Remove and os/exec.LookPath are found through it. Without the reference, both read not-found. The range + over a slice names no function. No code uses unused, so os.Getwd is not found. consume ranges over its parameter seq + of a function type, and drain over the field each of a function type. Each range calls a function value that a + search from a source does not reach, so it is a function-value record, and each target is uncertain from consume and + from drain. Without the record, each target reads not-found there. attrs ranges over the member Attr of a range + variable n over ns []*node, and attrsIndexed over ns[0].Attr. The range clause and the index give n and ns[0] the + element type node, so the reference at each site names the field Attr of node, which holds a slice. Each range then + has no record, and os.Chdir, which the method element.Attr calls, is not found from attrs, attrsIndexed or + attrsTyped. Without the type, a range over a member keeps a record with the method name, which gives a name edge to + element.Attr. typed ranges over a parameter of the type Seq of another file, and viaLocal over a local that holds + the field Each of a holder. The index cannot show that either value is data, so each range is a function-value + record, and os.Remove is uncertain from each. The record of viaLocal is at the field that gives the local. + dataRanges ranges over a slice of a slice, a value of the type List of another file, an element of a map of slices, + the result of names and of node.Children, which give slices, and a local that holds such a result. stdRange ranges + over the result of strings.Fields, code outside the graph whose own record covers a function that it makes. The link + step reads the types and the results of the graph and drops each record. So os.Remove is not found from dataRanges. + From stdRange only the callback of strings.Fields concerns it, because tree.All, a method of an exported name, holds + a function-value record. roots: - key: main.go kind: main @@ -51,9 +49,6 @@ no_edges: - from: main.go//main to: os//exec//LookPath unresolved: - - caller: main.go//attrs - reason: no-dependency-source - callback: true - caller: main.go//tree//All reason: no-dependency-source callback: true @@ -95,10 +90,6 @@ unresolved: - caller: main.go//drain site: main.go:46 reason: function-value - - caller: main.go//attrs - site: main.go:60 - reason: unknown-receiver - method: Attr - caller: main.go//typed site: main.go:67 reason: function-value @@ -169,21 +160,19 @@ results: - target: os//Chdir sources: [main.go//attrs] sources_only: true - status: found - path: - - key: main.go//attrs - - key: main.go//element//Attr - site: main.go:60 - kind: name - confidence: low - - key: os//Chdir + status: not-found + call_sites: + - caller: main.go//element//Attr site: main.go:54 - kind: direct - confidence: high + reached: false + - target: os//Chdir + sources: [main.go//attrsIndexed] + sources_only: true + status: not-found call_sites: - caller: main.go//element//Attr site: main.go:54 - reached: true + reached: false - target: os//Chdir sources: [main.go//attrsTyped] sources_only: true diff --git a/test/projectgraph/cases/go/range-over-func/project/main.go b/test/projectgraph/cases/go/range-over-func/project/main.go index f4fc029e..3db12e5f 100644 --- a/test/projectgraph/cases/go/range-over-func/project/main.go +++ b/test/projectgraph/cases/go/range-over-func/project/main.go @@ -94,3 +94,8 @@ func dataRanges(b []byte, l List, m map[string][]string, n *node) { for range kids { } } + +func attrsIndexed(ns []*node) { + for range ns[0].Attr { + } +} diff --git a/test/projectgraph/cases/go/source-local-func/expect.yaml b/test/projectgraph/cases/go/source-local-func/expect.yaml index cb768f54..6ee55d63 100644 --- a/test/projectgraph/cases/go/source-local-func/expect.yaml +++ b/test/projectgraph/cases/go/source-local-func/expect.yaml @@ -53,9 +53,6 @@ unresolved: - caller: main.go//each reason: no-dependency-source callback: true - - caller: main.go//fire - reason: no-dependency-source - callback: true - caller: main.go//pick reason: no-dependency-source callback: true @@ -89,9 +86,6 @@ unresolved: - caller: other.go//anonSteps reason: no-dependency-source callback: true - - caller: other.go//keepRead - reason: no-dependency-source - callback: true - caller: other.go//rangeAssign reason: no-dependency-source callback: true @@ -129,10 +123,6 @@ unresolved: - caller: main.go//fire site: main.go:34 reason: function-value - - caller: main.go//fire - site: main.go:34 - reason: unknown-receiver - method: fn - caller: main.go//call site: main.go:40 reason: function-value @@ -145,7 +135,7 @@ unresolved: method: m - caller: main.go//show site: main.go:65 - reason: unknown-receiver + reason: interface-receiver method: Name - caller: other.go//serveLocal site: other.go:7 @@ -167,10 +157,6 @@ unresolved: - caller: other.go//deref site: other.go:24 reason: function-value - - caller: other.go//keepRead - site: other.go:29 - reason: unknown-receiver - method: fn results: - target: os//Remove status: found diff --git a/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml b/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml index feb9a2ec..7a318555 100644 --- a/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml +++ b/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml @@ -2,14 +2,17 @@ source: safedep:hand description: >- No unit gives the standard library. handler.ServeHTTP calls os.Remove, and each via function calls ListenAndServe of an http.Server, or of http, on a value whose type the link step does not know: a - package-level variable, an element of a slice, a range variable, a parameter of a first-party interface, a - method value, and a dot import. viaField calls a function value that a field holds. Each call is a record - with a method name, and such a call can run a method of the standard library, which calls back ServeHTTP. - So the caller of each record also has a callback record, and os.Remove is uncertain from each via - function. A call of a function value, as the field of viaField or a name that no definition of the file - binds, as ListenAndServe after the dot import, also concerns every target from a source. quiet.Idle calls nothing, and its package holds no load code, so os.Remove is not found from it. - A source in package main would enter the module top level of main.go, which holds http.ListenAndServe as a - value. + package-level variable, an element of a slice, a parameter of a first-party interface, a method value, + and a dot import. viaRange calls it on a range variable over a map of a declared type, so the variable + has the type http.Server, and the call names the external key Server.ListenAndServe. The callback record + of that key makes os.Remove uncertain from viaRange too. viaField calls a function value that a field + holds. Each call is a record with a method name, and such a call can run a method of the standard + library, which calls back ServeHTTP. So the caller of each record also has a callback record, and + os.Remove is uncertain from each via function. A call of a function value, as the field of viaField or a + name that no definition of the file binds, as ListenAndServe after the dot import, also concerns every + target from a source. quiet.Idle calls nothing, and its package holds no load code, so os.Remove is not + found from it. A source in package main would enter the module top level of main.go, which holds + http.ListenAndServe as a value. roots: - key: dot.go kind: main @@ -36,15 +39,15 @@ unresolved: - caller: main.go//viaMethodValue reason: no-dependency-source callback: true - - caller: main.go//viaRange - reason: no-dependency-source - callback: true - caller: main.go//viaVariable reason: no-dependency-source callback: true - caller: net//http//ListenAndServe reason: no-dependency-source callback: true + - caller: net//http//Server//ListenAndServe + reason: no-dependency-source + callback: true - caller: os//Remove reason: no-dependency-source callback: true @@ -63,10 +66,6 @@ unresolved: site: main.go:33 reason: unknown-receiver method: ListenAndServe - - caller: main.go//viaRange - site: main.go:38 - reason: unknown-receiver - method: ListenAndServe - caller: main.go//viaInterface site: main.go:43 reason: interface-receiver @@ -121,8 +120,6 @@ results: reached: false uncertainties: - kind: unresolved-call - site: main.go:38 - - kind: unresolved-call - target: os//Remove sources: [main.go//viaInterface] sources_only: true diff --git a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml index 4b1f6d37..4efe94c9 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/expect.yaml +++ b/test/projectgraph/cases/go/typed-local-shadow/expect.yaml @@ -16,13 +16,15 @@ description: >- record too. Without the rule, a type of local for each of the two gives not-found for os/exec.LookPath. In idiom.go, parse declares url from url.Parse, and the local binds no name at the call, so the call keeps its edge to the import. viaParam and viaRange bind log, which shadows the import of log, so the index takes no target of the import. - The parameter of the type logger gives a member edge to logger.Println, and the range variable a record with a name - edge. In shadowedParam of main.go a local o shadows the parameter o, so the field r of the parameter type does not - type x, and the call x.run() is a record. pool.get declares call from an element of the field calls - map[string]*remote, in the form call, dup := p.calls[key], and fromSlice declares r from an element of a parameter - []*remote. The declared element type of the map and of the slice gives the write its type, so each write gives - remote, and each call keeps its member edge to remote.run. Without the rule, a write of no known type there makes - the call a record with a name edge to local.run. + The parameter of the type logger and the range variable over ls []logger each give a member edge to logger.Println. + In shadowedParam of main.go a local o shadows the parameter o, so the field r of the parameter type does not type x, + and the call x.run() is a record. pool.get declares call from an element of the field calls map[string]*remote, in + the form call, dup := p.calls[key], and fromSlice declares r from an element of a parameter []*remote. The declared + element type of the map and of the slice gives the write its type, so each write gives remote, and each call keeps + its member edge to remote.run. Without the rule, a write of no known type there makes the call a record with a name + edge to local.run. loopThenVar calls run on a range variable over rs []*remote and declares r *local after the loop. + The range clause gives r the element type remote, so the call keeps its member edge to remote.run. Without the type, + the call is a record with a name edge to local.run. roots: - key: idiom.go kind: main @@ -84,7 +86,14 @@ edges: kind: member confidence: high site: main.go:103 + - from: main.go//loopThenVar + to: main.go//remote//run + kind: member + confidence: high + site: main.go:108 no_edges: + - from: main.go//loopThenVar + to: main.go//local//run - from: main.go//pool//get to: main.go//local//run - from: main.go//fromSlice @@ -96,9 +105,6 @@ no_edges: - from: main.go//named to: main.go//remote//run unresolved: - - caller: idiom.go//viaRange - reason: no-dependency-source - callback: true - caller: log//Println reason: no-dependency-source callback: true @@ -133,10 +139,6 @@ unresolved: site: idiom.go:14 reason: no-dependency-source method: Host - - caller: idiom.go//viaRange - site: idiom.go:23 - reason: unknown-receiver - method: Println - caller: main.go//main site: main.go:24 reason: unknown-receiver @@ -313,8 +315,8 @@ results: - key: idiom.go//viaRange - key: idiom.go//logger//Println site: idiom.go:23 - kind: name - confidence: low + kind: member + confidence: high - key: os//Mkdir site: idiom.go:19 kind: direct @@ -357,3 +359,11 @@ results: - caller: main.go//local//run site: main.go:10 reached: false + - target: os//Getwd + sources: [main.go//loopThenVar] + sources_only: true + status: not-found + call_sites: + - caller: main.go//local//run + site: main.go:10 + reached: false diff --git a/test/projectgraph/cases/go/typed-local-shadow/project/main.go b/test/projectgraph/cases/go/typed-local-shadow/project/main.go index ee281e1b..9cdb9452 100644 --- a/test/projectgraph/cases/go/typed-local-shadow/project/main.go +++ b/test/projectgraph/cases/go/typed-local-shadow/project/main.go @@ -102,3 +102,11 @@ func fromSlice(rs []*remote, i int) { } r.run() } + +func loopThenVar(rs []*remote) { + for _, r := range rs { + r.run() + } + var r *local + _ = r +} From 804f5b6094e1f9eaf47a51b02319ae647cffbe50 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:35:03 +0000 Subject: [PATCH 69/86] perf(indexer): read the Go top level and the struct types once per file The Go index walked the whole tree of each file for the struct types that no type spec of the top level declares, scanned the top level for each type check of a value, and scanned every definition node for the innermost definition of each site. The index now finds the struct types at the words struct of the source, keeps the types and the variables of the top level in a map, and keeps the definition nodes in start order. The index output does not change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/golang.go | 21 +++++-- plugin/indexer/govalues.go | 125 +++++++++++++++++++++++++++---------- plugin/indexer/project.go | 8 +++ plugin/indexer/types.go | 36 ++++++++--- 4 files changed, 145 insertions(+), 45 deletions(-) diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index a6e7f55a..2c77711f 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -164,13 +164,24 @@ func (p *projection) goFileType(t *sitter.Node) *sitter.Node { if t.Type() != "type_identifier" { return nil } - name := p.text(t) - for spec := range p.goTypeSpecs() { - if p.text(spec.ChildByFieldName("name")) == name { - return spec.ChildByFieldName("type") + return p.goFileTypes()[p.text(t)] +} + +// goFileTypes returns the type that the first type spec of each name at +// the top level of the file gives the name, read once per file. A type +// check of a value asks for it often, and each walk of the top level +// costs a call of the tree per statement. +func (p *projection) goFileTypes() map[string]*sitter.Node { + if p.fileTypes == nil { + p.fileTypes = map[string]*sitter.Node{} + for spec := range p.goTypeSpecs() { + name := p.text(spec.ChildByFieldName("name")) + if _, ok := p.fileTypes[name]; !ok { + p.fileTypes[name] = spec.ChildByFieldName("type") + } } } - return nil + return p.fileTypes } // goTypeSpecs returns the type specs of the type declarations at the top diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 540a9851..b224a8af 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -1,7 +1,9 @@ package indexer import ( + "bytes" "fmt" + "iter" "slices" "strings" @@ -377,17 +379,22 @@ func (p *projection) goDeclaredType(caller string, value *sitter.Node) *sitter.N // goFileVarType returns the declared type of a variable at the top level // of the file, or nil. func (p *projection) goFileVarType(name string) *sitter.Node { - for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { - if statement.Type() != "var_declaration" { - continue - } - for _, spec := range goVarSpecs(statement) { - if slices.ContainsFunc(goNames(spec), func(n *sitter.Node) bool { return p.text(n) == name }) { - return spec.ChildByFieldName("type") + if p.fileVars == nil { + p.fileVars = map[string]*sitter.Node{} + for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { + if statement.Type() != "var_declaration" { + continue + } + for _, spec := range goVarSpecs(statement) { + for _, n := range goNames(spec) { + if _, ok := p.fileVars[p.text(n)]; !ok { + p.fileVars[p.text(n)] = spec.ChildByFieldName("type") + } + } } } } - return nil + return p.fileVars[name] } // goFieldValueType returns the declared type of a field that a value @@ -426,16 +433,14 @@ func (p *projection) goParamField(caller string, value *sitter.Node) *sitter.Nod // goFileField returns the type of a field of a struct that the file // declares, or nil. func (p *projection) goFileField(structName, field string) *sitter.Node { - for spec := range p.goTypeSpecs() { - t := spec.ChildByFieldName("type") - if t == nil || t.Type() != "struct_type" || p.text(spec.ChildByFieldName("name")) != structName { - continue - } - for _, f := range p.goStructFields(t) { - for _, n := range childrenByField(f, "name") { - if p.text(n) == field { - return f.ChildByFieldName("type") - } + t := p.goFileTypes()[structName] + if t == nil || t.Type() != "struct_type" { + return nil + } + for _, f := range p.goStructFields(t) { + for _, n := range childrenByField(f, "name") { + if p.text(n) == field { + return f.ChildByFieldName("type") } } } @@ -758,31 +763,87 @@ func goUnparen(node *sitter.Node) *sitter.Node { // goAnonFields returns the names of the fields of each anonymous struct of // the file and of each struct type that a function declares, true for a -// name that a field which can hold a function has. +// name that a field which can hold a function has. Each struct type starts +// with the keyword struct, so the index finds the struct types at the words +// struct of the source, and a walk of the whole tree is not needed. A word +// in a comment or a string finds no struct type there. func (p *projection) goAnonFields() map[string]bool { if p.anonFields != nil { return p.anonFields } p.anonFields = map[string]bool{} - var walk func(node *sitter.Node, depth int) - walk = func(node *sitter.Node, depth int) { - if depth > maxLocalDepth { - return + root := p.tree.Tree().RootNode() + for at := range p.goStructWords() { + node := root.NamedDescendantForPointRange(p.pointAt(at), p.pointAt(at+uint32(len("struct")))) + if node == nil || node.Type() != "struct_type" || node.StartByte() != at || goTopLevelType(node) { + continue } - if node.Type() == "struct_type" && !goTopLevelType(node) { - for _, field := range p.goStructFields(node) { - holds := !p.goDataField(field.ChildByFieldName("type")) - for _, name := range childrenByField(field, "name") { - p.anonFields[p.text(name)] = p.anonFields[p.text(name)] || holds + for _, field := range p.goStructFields(node) { + holds := !p.goDataField(field.ChildByFieldName("type")) + for _, name := range childrenByField(field, "name") { + p.anonFields[p.text(name)] = p.anonFields[p.text(name)] || holds + } + } + } + return p.anonFields +} + +// goStructWords returns the byte of each word struct of the source that a +// brace with a field can follow, as struct{ run func() }. struct{} holds no +// field, so it is left out. +func (p *projection) goStructWords() iter.Seq[uint32] { + return func(yield func(uint32) bool) { + src := p.source + for i := 0; ; { + j := bytes.Index(src[i:], []byte("struct")) + if j < 0 { + return + } + at, end := i+j, i+j+len("struct") + i = end + if at > 0 && goWordByte(src[at-1]) || end < len(src) && goWordByte(src[end]) { + continue + } + k := end + for k < len(src) && (src[k] == ' ' || src[k] == '\t' || src[k] == '\n' || src[k] == '\r') { + k++ + } + if k < len(src) && src[k] == '{' { + k++ + for k < len(src) && (src[k] == ' ' || src[k] == '\t' || src[k] == '\n' || src[k] == '\r') { + k++ + } + if k < len(src) && src[k] == '}' { + continue } } + if !yield(uint32(at)) { + return + } } - for _, child := range ts.NamedChildren(node) { - walk(child, depth+1) + } +} + +// pointAt returns the row and the byte column of a byte of the source. +func (p *projection) pointAt(at uint32) sitter.Point { + if p.lineStarts == nil { + p.lineStarts = []uint32{0} + for i, b := range p.source { + if b == '\n' { + p.lineStarts = append(p.lineStarts, uint32(i+1)) + } } } - walk(p.tree.Tree().RootNode(), 0) - return p.anonFields + row, found := slices.BinarySearch(p.lineStarts, at) + if !found { + row-- + } + return sitter.Point{Row: uint32(row), Column: at - p.lineStarts[row]} +} + +// goWordByte reports whether a byte can be part of a Go identifier. +func goWordByte(b byte) bool { + return b == '_' || b >= '0' && b <= '9' || b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= 0x80 } // goTopLevelType reports whether a type is the type of a type declaration diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index 4516389c..5bd6ad0b 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -68,6 +68,14 @@ type projection struct { // anonFields holds the names of the fields of the anonymous structs of // a Go file that can hold a function. anonFields map[string]bool + // fileTypes and fileVars hold the declared type of each type and each + // variable at the top level of a Go file, read once. + fileTypes, fileVars map[string]*sitter.Node + // extents holds the bytes of each definition node in start order, for + // innermost. A new definition node clears it. + extents []extent + // lineStarts holds the byte where each line of the source starts. + lineStarts []uint32 // rustMacros holds the names that a macro_rules! or a use of a Rust // file binds, once a call of a macro reads them. rustMacros map[string]bool diff --git a/plugin/indexer/types.go b/plugin/indexer/types.go index 78b94424..6120ba78 100644 --- a/plugin/indexer/types.go +++ b/plugin/indexer/types.go @@ -1,6 +1,9 @@ package indexer import ( + "cmp" + "slices" + "github.com/safedep/code/fileindex" "github.com/safedep/code/pkg/ts" sitter "github.com/smacker/go-tree-sitter" @@ -29,6 +32,7 @@ var functionValues = map[string]bool{ // addSyntax records the tree node of a definition. func (p *projection) addSyntax(key string, node *sitter.Node) { p.syntax[key] = node + p.extents = nil if node != nil { p.spans[spanOf(node)] = key } @@ -90,17 +94,33 @@ func walkBody(body *sitter.Node, stop map[string]bool, visit func(node *sitter.N // innermost returns the key of the innermost definition that holds the // byte, or the module key. func (p *projection) innermost(at uint32) string { - var inner *sitter.Node - key := p.cg.FileName - for k, node := range p.syntax { - if node == nil || at < node.StartByte() || at >= node.EndByte() { - continue + if p.extents == nil { + p.extents = make([]extent, 0, len(p.syntax)) + for k, node := range p.syntax { + if node != nil { + p.extents = append(p.extents, extent{start: node.StartByte(), end: node.EndByte(), key: k}) + } } - if inner == nil || node.EndByte()-node.StartByte() < inner.EndByte()-inner.StartByte() { - inner, key = node, k + slices.SortFunc(p.extents, func(a, b extent) int { + return cmp.Or(cmp.Compare(a.start, b.start), cmp.Compare(b.end, a.end), cmp.Compare(a.key, b.key)) + }) + } + // The nodes of a tree nest or do not meet, so of the nodes that hold + // the byte the one that starts last is the innermost. The scan back + // from the last start before the byte passes only nodes inside it. + i, _ := slices.BinarySearchFunc(p.extents, at+1, func(e extent, t uint32) int { return cmp.Compare(e.start, t) }) + for i--; i >= 0; i-- { + if e := p.extents[i]; at < e.end { + return e.key } } - return key + return p.cg.FileName +} + +// extent is the bytes of the node of a definition, for innermost. +type extent struct { + start, end uint32 + key string } // fields returns the fields of a class that declare a type name, by the From 7fe82ce8ac474a55195ba7427ae299ff48ce84ae Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:39:14 +0000 Subject: [PATCH 70/86] fix(indexer): type a Rust value of a self or super path in an inline module let i = super::Item::new() in mod other gave i.save() the target super//Item//save. The link step read it from the module of the file, found no Item there, and gave low name edges to the save of each Item. So a target of the save of another Item read found. The index now writes the targets of a method call on such a value from the module of the file, as it does for the path of a call. A struct expression, as super::Item {}, gets the same fix. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/calls.go | 2 +- plugin/indexer/languages.go | 2 +- plugin/indexer/rust.go | 67 +++++++--- .../rust/super-path-inline-module/expect.yaml | 116 ++++++++++++++++-- .../project/src/main.rs | 2 + .../project/src/store.rs | 25 +++- 6 files changed, 186 insertions(+), 28 deletions(-) diff --git a/plugin/indexer/calls.go b/plugin/indexer/calls.go index 3ccf48a5..753d1cbb 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -112,7 +112,7 @@ func (p *projection) addCalls(ctx context.Context) { continue } p.retargetTyped(ctx, &call, untyped[[2]uint32{ref.Site.StartLine, ref.Site.StartColumn}]) - if shadowed := p.rules().shadowed; shadowed != nil && !shadowed(p, &call, ref.Site) { + if shadowed := p.rules().shadowed; shadowed != nil && !shadowed(ctx, p, &call, ref.Site) { p.addRecord(call, fileindex.UnresolvedUnknownReceiver) continue } diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 83f2dd0e..d6fa5220 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -106,7 +106,7 @@ type languageRules struct { // shadowed fixes the targets of a call whose first name a scope between // the call and the definition of the target binds again, as a use in a // block of Rust. It reports false when the call is a record. - shadowed func(p *projection, call *fileindex.Call, callee ast.NodePosition) bool + shadowed func(ctx context.Context, p *projection, call *fileindex.Call, callee ast.NodePosition) bool // markTries marks each ? in the targets of a call from the syntax of // its callee, as the ? of Rust in dep//load//?//run. The call graph // keeps no such mark. diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 0b03be66..d30e494d 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -812,18 +812,25 @@ func rustBoundName(pattern *sitter.Node) *sitter.Node { // util::g() the module util of the file. A use gives the target through // the path of the use. A type parameter names a type that the call does not // know, so the call is a record. -func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) bool { +func rustShadowed(ctx context.Context, p *projection, call *fileindex.Call, callee ast.NodePosition) bool { node := p.descendant(callee) - if node != nil && rustMethod(node) != nil { + if method := rustMethod(node); node != nil && method != nil { + if binders, ok := p.rustBinders(ctx, call.Caller); ok { + p.rustFromFile(call, node, p.rustReceiverPath(binders, method)) + } return p.rustShadowedMethod(call, node) } + if node != nil && node.Type() == "scoped_type_identifier" { + p.rustFromFile(call, node, p.text(node)) + return true + } if node == nil || node.Type() != "identifier" && node.Type() != "scoped_identifier" { return true } names := strings.Split(p.text(node), "::") switch names[0] { case "self", "super": - p.rustFromFile(call, node, names) + p.rustFromFile(call, node, p.text(node)) return true case "crate", "Self", "": return true @@ -845,14 +852,18 @@ func rustShadowed(p *projection, call *fileindex.Call, callee ast.NodePosition) return true } -// rustFromFile writes the target of a call whose path starts with self or -// super in an inline module as a path from the module of the file. The call -// graph reads no inline module, and the link step reads such a path from the -// module of the file. So super::Item::new() in mod other went to an external -// super//Item//new, and the real Item::new of the file read as not-found. -// Only the target that the call writes changes. A use already gives its -// target from the module of the file. -func (p *projection) rustFromFile(call *fileindex.Call, node *sitter.Node, names []string) { +// rustFromFile writes the targets of a call under a path that the call +// writes, when the path starts with self or super in an inline module, as a +// path from the module of the file. The call graph reads no inline module, +// and the link step reads such a path from the module of the file. So +// super::Item::new() in mod other went to an external super//Item//new, and +// the real Item::new of the file read as not-found. A method of a value of +// such a path, as i.save() after let i = super::Item::new(), gets the same +// fix. A use already gives its target from the module of the file. +func (p *projection) rustFromFile(call *fileindex.Call, node *sitter.Node, written string) { + if first, _, _ := strings.Cut(written, "::"); first != "self" && first != "super" { + return + } var mods []string for n := node.Parent(); n != nil; n = n.Parent() { if n.Type() == "mod_item" { @@ -863,13 +874,39 @@ func (p *projection) rustFromFile(call *fileindex.Call, node *sitter.Node, names return } slices.Reverse(mods) - written := strings.Join(names, fileindex.Separator) - path := langcode.RustFromFile(strings.Join(names, "::"), mods) + from := strings.ReplaceAll(written, "::", fileindex.Separator) + to := strings.ReplaceAll(langcode.RustFromFile(written, mods), "::", fileindex.Separator) for i, target := range call.Targets { - if target == written { - call.Targets[i] = strings.ReplaceAll(path, "::", fileindex.Separator) + if rest, ok := strings.CutPrefix(target, from); ok && (rest == "" || strings.HasPrefix(rest, fileindex.Separator)) { + call.Targets[i] = to + rest + } + } +} + +// rustReceiverPath returns the path of the type that the let of the +// receiver of a method call writes, as super::Item of let i = +// super::Item::new() or of let i = super::Item {}. The call graph types the +// receiver by that path. +func (p *projection) rustReceiverPath(binders map[string][]*sitter.Node, method *sitter.Node) string { + receiver := method.ChildByFieldName("value") + if receiver.Type() != "identifier" { + return "" + } + let := rustLetOf(binders[p.text(receiver)], receiver) + if let == nil { + return "" + } + value := let.ChildByFieldName("value") + switch { + case value == nil: + case value.Type() == "struct_expression": + return p.text(value.ChildByFieldName("name")) + case value.Type() == "call_expression": + if fn := value.ChildByFieldName("function"); fn != nil && fn.Type() == "scoped_identifier" { + return p.text(fn.ChildByFieldName("path")) } } + return "" } // rustShadowedMethod fixes a target of a method call that the call graph diff --git a/test/projectgraph/cases/rust/super-path-inline-module/expect.yaml b/test/projectgraph/cases/rust/super-path-inline-module/expect.yaml index 44801d80..4554620b 100644 --- a/test/projectgraph/cases/rust/super-path-inline-module/expect.yaml +++ b/test/projectgraph/cases/rust/super-path-inline-module/expect.yaml @@ -1,9 +1,13 @@ source: safedep:hand description: >- The inline module other of src/store.rs calls super::Item::new and self::inner::run. super of other is the - module store, and self is other. The call graph reads no inline module, and the link step reads such a path + module store, and self is other. other also makes values of super::Item with super::Item::new, + super::Item {} and crate::store::Item::new, and calls save on each. other defines an Item of its own, whose + save calls std::fs::remove_dir. The call graph reads no inline module, and the link step reads such a path from the module of the file. So super::Item::new goes to the external key super//Item//new, and std::fs::write reads not-found. self::inner::run names an inner of the module store, which does not exist. + i.save() and j.save() go to super//Item//save, which gets low name edges to the save of each Item, so + std::fs::remove_dir reads found. roots: - key: src/main.rs kind: main @@ -11,20 +15,88 @@ roots: - key: src/main.rs//main kind: main rule: rust.main +exact_edges: true edges: + - from: src/main.rs//main + to: src/store.rs//other//sib + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//main + to: src/store.rs//other//made + kind: direct + confidence: high + site: src/main.rs:5 + - from: src/main.rs//main + to: src/store.rs//other//rooted + kind: direct + confidence: high + site: src/main.rs:6 + - from: src/store.rs//Item//new + to: std//fs//write + kind: direct + confidence: high + site: src/store.rs:5 + - from: src/store.rs//Item//save + to: src/store.rs//Item + kind: direct + confidence: high + site: src/store.rs:9 + - from: src/store.rs//Item//save + to: std//fs//remove_file + kind: direct + confidence: high + site: src/store.rs:10 + - from: src/store.rs//other//Item//save + to: src/store.rs//other//Item + kind: direct + confidence: high + site: src/store.rs:18 + - from: src/store.rs//other//Item//save + to: std//fs//remove_dir + kind: direct + confidence: high + site: src/store.rs:19 + - from: src/store.rs//other//inner//run + to: std//fs//create_dir + kind: direct + confidence: high + site: src/store.rs:41 - from: src/store.rs//other//sib to: src/store.rs//Item//new kind: direct confidence: high - site: src/store.rs:12 + site: src/store.rs:24 + - from: src/store.rs//other//sib + to: src/store.rs//Item//save + kind: member + confidence: high + site: src/store.rs:25 - from: src/store.rs//other//sib to: src/store.rs//other//inner//run kind: direct confidence: high - site: src/store.rs:13 -no_edges: - - from: src/store.rs//other//sib - to: super//Item//new + site: src/store.rs:26 + - from: src/store.rs//other//made + to: src/store.rs//Item + kind: direct + confidence: high + site: src/store.rs:30 + - from: src/store.rs//other//made + to: src/store.rs//Item//save + kind: member + confidence: high + site: src/store.rs:31 + - from: src/store.rs//other//rooted + to: src/store.rs//Item//new + kind: direct + confidence: high + site: src/store.rs:35 + - from: src/store.rs//other//rooted + to: src/store.rs//Item//save + kind: member + confidence: high + site: src/store.rs:36 unresolved: [] results: - target: std//fs//write @@ -36,7 +108,7 @@ results: kind: direct confidence: high - key: src/store.rs//Item//new - site: src/store.rs:12 + site: src/store.rs:24 kind: direct confidence: high - key: std//fs//write @@ -47,6 +119,30 @@ results: - caller: src/store.rs//Item//new site: src/store.rs:5 reached: true + - target: std//fs//remove_file + status: found + sources: [src/store.rs//other//made] + sources_only: true + path: + - key: src/store.rs//other//made + - key: src/store.rs//Item//save + site: src/store.rs:31 + kind: member + confidence: high + - key: std//fs//remove_file + site: src/store.rs:10 + kind: direct + confidence: high + call_sites: + - caller: src/store.rs//Item//save + site: src/store.rs:10 + reached: true + - target: std//fs//remove_dir + status: not-found + call_sites: + - caller: src/store.rs//other//Item//save + site: src/store.rs:19 + reached: false - target: std//fs//create_dir status: found path: @@ -56,14 +152,14 @@ results: kind: direct confidence: high - key: src/store.rs//other//inner//run - site: src/store.rs:13 + site: src/store.rs:26 kind: direct confidence: high - key: std//fs//create_dir - site: src/store.rs:18 + site: src/store.rs:41 kind: direct confidence: high call_sites: - caller: src/store.rs//other//inner//run - site: src/store.rs:18 + site: src/store.rs:41 reached: true diff --git a/test/projectgraph/cases/rust/super-path-inline-module/project/src/main.rs b/test/projectgraph/cases/rust/super-path-inline-module/project/src/main.rs index 13c8ca1d..a6e06a13 100644 --- a/test/projectgraph/cases/rust/super-path-inline-module/project/src/main.rs +++ b/test/projectgraph/cases/rust/super-path-inline-module/project/src/main.rs @@ -2,4 +2,6 @@ mod store; fn main() { store::other::sib(); + store::other::made(); + store::other::rooted(); } diff --git a/test/projectgraph/cases/rust/super-path-inline-module/project/src/store.rs b/test/projectgraph/cases/rust/super-path-inline-module/project/src/store.rs index 94aa043b..58c60fd9 100644 --- a/test/projectgraph/cases/rust/super-path-inline-module/project/src/store.rs +++ b/test/projectgraph/cases/rust/super-path-inline-module/project/src/store.rs @@ -5,14 +5,37 @@ impl Item { let _ = std::fs::write("top", "x"); Item } + + pub fn save(&self) { + let _ = std::fs::remove_file("top"); + } } pub mod other { + pub struct Item; + + impl Item { + pub fn save(&self) { + let _ = std::fs::remove_dir("other"); + } + } + pub fn sib() { - super::Item::new(); + let i = super::Item::new(); + i.save(); self::inner::run(); } + pub fn made() { + let j = super::Item {}; + j.save(); + } + + pub fn rooted() { + let k = crate::store::Item::new(); + k.save(); + } + pub mod inner { pub fn run() { let _ = std::fs::create_dir("inner"); From 72053f2f0efffe9c6249ed18e03f7867ea1e7cd4 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 23:41:09 +0000 Subject: [PATCH 71/86] test(projectgraph): update the hash rows of fileindex 17 and projectgraph 20 The Go, Rust and JavaScript fixes add cases and change graphs. The rows of fileindex 17, projectgraph 20 and reach 6 exist only on this branch, so they change in place. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- test/projectgraph/version_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index cba3bcb9..1361860f 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -34,7 +34,7 @@ var graphHashes = map[string]string{ "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", "16/18": "bb92620bf9903e8d19b52b62198a0bfd69d67856f9db226112b2cedf537b312b", "16/19": "51ba3e925c628602b3a53c1a6e68b379022a410bb5b749062b3045fef082ac8f", - "17/20": "e191f495166befe37bf0f4171cf1d2051e907be158353f5879fdb6c565434183", + "17/20": "35035a564611d1a01733a44dd1985ae8053efc036c4a3012ff86cbea1668b4fa", } // resultHashes records the hash of the reach results of every case for @@ -53,7 +53,7 @@ var resultHashes = map[string]string{ "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", "16/18/5": "e0c6093df3a85353ee15c8a90ef7c2a441f23348ce80c9d49793c256bddeffc5", "16/19/5": "a935650f8e4d01c0abdceeefb3332bf779927693a1c48b28263da5365db752cd", - "17/20/6": "b2b30afae4771b76016f6434442c835b4e5f051a4fbb7a16c1922bffc7bc8338", + "17/20/6": "f4f845cd28c356e6e7db514e51b482a340b7d731d35f9c3f35bb66840a5e0694", } // summaryHashes records the hash of the golden summaries of the cases for @@ -73,7 +73,7 @@ var summaryHashes = map[string]string{ "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", "16/18/3": "694efb5f72075356359031d449d7b4ecbdca2ddd5099dacbcd2adad745ac6e19", "16/19/3": "13c72bf9eb60fc0a38bc600906fc02028d5cb60cb2aefbec0f0721779c429d80", - "17/20/3": "70b8db79ef5d1b7c35e37b2abcc360ab8f8e7c6f0db13ad398c5b3bceefd5b96", + "17/20/3": "197788451ef4b26f40f72434cec03e17e2b213893e4ea0127b9248f489c5262c", } func TestGraphHashFollowsVersion(t *testing.T) { From 7765b0fe40ae395bec6b7850399896afd74df38c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 00:16:02 +0000 Subject: [PATCH 72/86] fix(projectgraph): leave Node.js builtin modules out of the family imports A language-scoped record concerns a target under a module that a file of its family imports from outside the project. An import of a builtin module of Node.js, as crypto, then made a target that only Go code calls, as crypto//tls//Client, uncertain, because the two languages write the same key. The family imports now leave out the builtin modules of Node.js, with or without the prefix node:. An import of an npm package still counts. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 8 +- projectgraph/javascript.go | 16 ++++ projectgraph/link.go | 7 +- projectgraph/profile.go | 3 + .../cases/go/svelte-node-builtin/expect.yaml | 89 +++++++++++++++++++ .../go/svelte-node-builtin/project/go.mod | 3 + .../go/svelte-node-builtin/project/main.go | 15 ++++ .../project/web/package.json | 1 + .../project/web/src/App.svelte | 5 ++ .../project/web/src/hash.js | 5 ++ 10 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 test/projectgraph/cases/go/svelte-node-builtin/expect.yaml create mode 100644 test/projectgraph/cases/go/svelte-node-builtin/project/go.mod create mode 100644 test/projectgraph/cases/go/svelte-node-builtin/project/main.go create mode 100644 test/projectgraph/cases/go/svelte-node-builtin/project/web/package.json create mode 100644 test/projectgraph/cases/go/svelte-node-builtin/project/web/src/App.svelte create mode 100644 test/projectgraph/cases/go/svelte-node-builtin/project/web/src/hash.js diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 59eb6c4f..b73af6fe 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -111,9 +111,11 @@ record by its language with `Uncertainty.Language`. It concerns a target under a module that a file of the family imports or gives on from outside the project, as `jsonschema` for `import { validate } from 'jsonschema'` or `plug` for `export * from 'plug'`, which `Graph.Imports` holds by -family. The case `javascript/component-reexport-star` shows it. The key -forms of npm and of Go coincide, so an import of `os` in JavaScript also -concerns a Go target under `os`. It also concerns a target that a definition of the +family. The case `javascript/component-reexport-star` shows it. A builtin +module of Node.js, as `crypto` or `node:fs`, is not in that set. Its key +can name a module of Go, as `crypto//tls//Client`, so an import of it +does not make a target that only Go code calls uncertain, as the case +`go/svelte-node-builtin` shows. It also concerns a target that a definition of the family is, or that an edge from a file of the family leads to, and a target with no node. A JavaScript project of component files thus reads `uncertain` for each JavaScript target that it diff --git a/projectgraph/javascript.go b/projectgraph/javascript.go index 0fb8ac8f..4c182094 100644 --- a/projectgraph/javascript.go +++ b/projectgraph/javascript.go @@ -25,6 +25,7 @@ func ecmascriptProfile(code langcode.Code, typescript bool) profile { openBases: true, structuralTypes: true, memberFields: true, + builtinModules: nodeBuiltins, } // The hooks of a family live on the profile of the family. if !typescript { @@ -35,6 +36,21 @@ func ecmascriptProfile(code langcode.Code, typescript bool) profile { return p } +// nodeBuiltins are the modules that Node.js gives, with or without the +// prefix node:. A key under one of them can also be a key of another +// language, as crypto//tls//Client of Go, so an import of one tells +// nothing of a target that only another family calls. +var nodeBuiltins = map[string]bool{ + "assert": true, "async_hooks": true, "buffer": true, "child_process": true, "cluster": true, + "console": true, "constants": true, "crypto": true, "dgram": true, "diagnostics_channel": true, + "dns": true, "domain": true, "events": true, "fs": true, "http": true, "http2": true, "https": true, + "inspector": true, "module": true, "net": true, "os": true, "path": true, "perf_hooks": true, + "process": true, "punycode": true, "querystring": true, "readline": true, "repl": true, + "stream": true, "string_decoder": true, "sys": true, "timers": true, "tls": true, "trace_events": true, + "tty": true, "url": true, "util": true, "v8": true, "vm": true, "wasi": true, "worker_threads": true, + "zlib": true, "test": true, "sqlite": true, "sea": true, +} + // javascriptDependencies returns the packages that the package.json files // of the project depend on. func javascriptDependencies(b *builder) ([]string, []modules.Problem) { diff --git a/projectgraph/link.go b/projectgraph/link.go index 054cfd99..e0fd0932 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -478,9 +478,12 @@ func (b *builder) scopeToLanguage(ix *fileindex.Index, site *UnresolvedSite) { } // outsideModules returns the key of a module that a file names and that no -// file of the project or of a unit gives, as plug, or nothing. +// file of the project or of a unit gives, as plug, or nothing. A builtin +// module of the runtime gives nothing, because its key can name a module +// of another language, as crypto of Go. func (b *builder) outsideModules(ix *fileindex.Index, module string) []string { - if module == "" || strings.HasPrefix(module, ".") { + first, _, _ := strings.Cut(strings.TrimPrefix(module, "node:"), "/") + if module == "" || strings.HasPrefix(module, ".") || profileOf(ix.Language).builtinModules[first] { return nil } if _, _, ok := b.resolveModule(ix, module); ok { diff --git a/projectgraph/profile.go b/projectgraph/profile.go index 45a00dd1..5d670682 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -179,6 +179,9 @@ type profile struct { // implicitMethods name the methods that the language calls with no // call of the name, as an operator of Kotlin. implicitMethods map[string]bool + // builtinModules name the modules that the runtime of the language + // gives, as fs of Node.js, by their first segment. + builtinModules map[string]bool // instanceMethods name the methods that an instance runs with no call // of the name, as __call__ of Python. A call that makes an instance // links them, and a reference to the class alone does not. diff --git a/test/projectgraph/cases/go/svelte-node-builtin/expect.yaml b/test/projectgraph/cases/go/svelte-node-builtin/expect.yaml new file mode 100644 index 00000000..ea25d679 --- /dev/null +++ b/test/projectgraph/cases/go/svelte-node-builtin/expect.yaml @@ -0,0 +1,89 @@ +source: safedep:hand +description: >- + A Go service ships a Svelte page. hash.js imports crypto, a builtin module of Node.js, and only the dead + Go function unused calls crypto/tls.Client. The key of a Node.js builtin can name a module of Go, so the + import does not make the markup record of App.svelte concern crypto//tls//Client, and it stays + not-found. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main + - key: web/src/App.svelte + kind: framework + rule: node.component + - key: web/src/App.svelte + kind: library-surface + rule: library-surface + - key: web/src/hash.js + kind: library-surface + rule: library-surface + - key: web/src/hash.js//digest + kind: library-surface + rule: library-surface +exact_edges: true +edges: + - from: main.go//main + to: fmt//Println + kind: direct + confidence: high + site: main.go:10 + - from: main.go//unused + to: crypto//tls//Client + kind: direct + confidence: high + site: main.go:14 + - from: web/src/App.svelte + to: web/src/hash.js + kind: import + confidence: high + site: web/src/App.svelte:2 + - from: web/src/hash.js//digest + to: crypto//createHash + kind: direct + confidence: high + site: web/src/hash.js:4 + - from: web/src/hash.js//digest + to: crypto//createHash//update + kind: direct + confidence: high + site: web/src/hash.js:4 + - from: web/src/hash.js//digest + to: crypto//createHash//update//digest + kind: direct + confidence: high + site: web/src/hash.js:4 + - from: web/src/hash.js//digest + to: web/src/hash.js//digest + kind: field + confidence: low + site: web/src/hash.js:4 +unresolved: + - caller: crypto//tls//Client + reason: no-dependency-source + callback: true + - caller: fmt//Println + reason: no-dependency-source + callback: true + - caller: web/src/App.svelte + site: web/src/App.svelte:1 + reason: markup + - caller: web/src/hash.js//digest + site: web/src/hash.js:4 + reason: no-dependency-source + method: update + - caller: web/src/hash.js//digest + site: web/src/hash.js:4 + reason: no-dependency-source + method: digest +results: + - target: crypto//tls//Client + status: not-found + call_sites: + - caller: main.go//unused + site: main.go:14 + reached: false + uncertainties: + - kind: root-fallback diff --git a/test/projectgraph/cases/go/svelte-node-builtin/project/go.mod b/test/projectgraph/cases/go/svelte-node-builtin/project/go.mod new file mode 100644 index 00000000..3955fcdc --- /dev/null +++ b/test/projectgraph/cases/go/svelte-node-builtin/project/go.mod @@ -0,0 +1,3 @@ +module app + +go 1.22 diff --git a/test/projectgraph/cases/go/svelte-node-builtin/project/main.go b/test/projectgraph/cases/go/svelte-node-builtin/project/main.go new file mode 100644 index 00000000..39033321 --- /dev/null +++ b/test/projectgraph/cases/go/svelte-node-builtin/project/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "crypto/tls" + "fmt" + "net" +) + +func main() { + fmt.Println("serve") +} + +func unused(c net.Conn) { + tls.Client(c, nil) +} diff --git a/test/projectgraph/cases/go/svelte-node-builtin/project/web/package.json b/test/projectgraph/cases/go/svelte-node-builtin/project/web/package.json new file mode 100644 index 00000000..6a35c5fc --- /dev/null +++ b/test/projectgraph/cases/go/svelte-node-builtin/project/web/package.json @@ -0,0 +1 @@ +{ "name": "web", "dependencies": { "svelte": "^4.2.0" } } diff --git a/test/projectgraph/cases/go/svelte-node-builtin/project/web/src/App.svelte b/test/projectgraph/cases/go/svelte-node-builtin/project/web/src/App.svelte new file mode 100644 index 00000000..f9cd22c6 --- /dev/null +++ b/test/projectgraph/cases/go/svelte-node-builtin/project/web/src/App.svelte @@ -0,0 +1,5 @@ + + +

{digest('x')}

diff --git a/test/projectgraph/cases/go/svelte-node-builtin/project/web/src/hash.js b/test/projectgraph/cases/go/svelte-node-builtin/project/web/src/hash.js new file mode 100644 index 00000000..a85dbc55 --- /dev/null +++ b/test/projectgraph/cases/go/svelte-node-builtin/project/web/src/hash.js @@ -0,0 +1,5 @@ +import crypto from 'crypto'; + +export function digest(text) { + return crypto.createHash('sha256').update(text).digest('hex'); +} From b21feaad0372a15d51e9d4d2921d394d6e76c604 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 00:20:01 +0000 Subject: [PATCH 73/86] fix(projectgraph): link the key of a third crate that a walk into a unit ends on dep1::m2::f0 with pub use dep2::m4::f0 or pub use dep2::m4::* in the unit of dep1 walks to the key dep2//m4//f0. No call names that key, so it had no node, and linkProvision returned with no edge and no record. A target under it read not-found. The key now gets its node and the edge, and the next pass of linkPackages links it to the unit of dep2. A key of a module of a unit runs no code and gets no node. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/packages.go | 42 ++++++- .../rust/reexport-third-crate/expect.yaml | 114 ++++++++++++++++++ .../reexport-third-crate/project/Cargo.toml | 7 ++ .../reexport-third-crate/project/src/main.rs | 4 + .../project/vendor/dep1/Cargo.toml | 7 ++ .../project/vendor/dep1/src/bound.rs | 5 + .../project/vendor/dep1/src/glob.rs | 1 + .../project/vendor/dep1/src/lib.rs | 2 + .../project/vendor/dep2/Cargo.toml | 4 + .../project/vendor/dep2/src/lib.rs | 1 + .../project/vendor/dep2/src/store.rs | 7 ++ .../summaries/cargo_dep1_1.0.0.json | 16 +++ .../summaries/cargo_dep2_1.0.0.json | 23 ++++ 13 files changed, 231 insertions(+), 2 deletions(-) create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/expect.yaml create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/Cargo.toml create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/bound.rs create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/glob.rs create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/lib.rs create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/Cargo.toml create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/src/lib.rs create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/src/store.rs create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/summaries/cargo_dep1_1.0.0.json create mode 100644 test/projectgraph/cases/rust/reexport-third-crate/summaries/cargo_dep2_1.0.0.json diff --git a/projectgraph/packages.go b/projectgraph/packages.go index 6013a202..155782a3 100644 --- a/projectgraph/packages.go +++ b/projectgraph/packages.go @@ -472,14 +472,52 @@ func (b *builder) linkProvision(key string, o origin, pv provision) { if b.holdsData(res.key) { return } - n := b.g.Nodes[res.key] - if res.key == key || n == nil { + if res.key == key { return } + n := b.g.Nodes[res.key] + if n == nil { + // The walk can end on the key of a third crate, as dep2::m4::f0 + // of pub use dep2::m4::f0 in dep1::m2. That key has no node until + // a call names it, so the edge went nowhere, and a target under + // it read not-found. The key gets its node, and the next pass + // links it to the unit of its crate. A module of a unit runs no + // code, and a key that no origin names cannot link on. + if len(b.origins[res.key]) == 0 || b.unitModuleKey(res.key) { + return + } + n = b.g.Nodes[b.external(res.key)] + } b.link(o.lang, key, res.key, Site{Path: cmp.Or(n.Path, pv.sp.unit), Line: n.Line}, EdgePackage, r) b.linkPackageLoad(o.lang, key, pv.module) } +// unitModuleKey reports whether a unit gives an external key as a module, +// as inner//tools for the module tools of the crate inner. +func (b *builder) unitModuleKey(key string) bool { + for _, o := range b.origins[key] { + for _, pv := range b.providers(o) { + mod := pv.module + for _, name := range o.names { + if b.units[mod] == nil { + mod = "" + break + } + next, ok := b.unitMember(mod, name) + if !ok || b.units[next] == nil { + mod = "" + break + } + mod = next + } + if b.units[mod] != nil { + return true + } + } + } + return false +} + // linkPackageLoad adds an import edge from an external key to each module // top level of its package that runs code at load, in a language whose // program loads a package before a symbol of it runs. A search that enters diff --git a/test/projectgraph/cases/rust/reexport-third-crate/expect.yaml b/test/projectgraph/cases/rust/reexport-third-crate/expect.yaml new file mode 100644 index 00000000..43261b79 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/expect.yaml @@ -0,0 +1,114 @@ +source: safedep:hand +description: >- + main calls dep1::bound::save and dep1::glob::load. The unit cargo:dep1@1.0.0 has pub use dep2::store::save + in bound and pub use dep2::store::* in glob, and the unit cargo:dep2@1.0.0 defines save and load in store. + bound also defines audit, so the summary of dep1 has a symbol. The walk of each key of dep1 ends on the key + of dep2, which no call names, so it has no node. A link step that links only to a node gives the keys of + dep1 no edge and no record, and std::fs::remove_file and std::fs::remove_dir read not-found. +units: + vendor/dep1: cargo:dep1@1.0.0 + vendor/dep2: cargo:dep2@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +exact_edges: true +edges: + - from: src/main.rs//main + to: dep1//bound//save + kind: direct + confidence: high + site: src/main.rs:2 + - from: src/main.rs//main + to: dep1//glob//load + kind: direct + confidence: high + site: src/main.rs:3 + - from: dep1//bound//save + to: dep2//store//save + kind: package + confidence: high + site: cargo:dep1@1.0.0 + - from: dep1//glob//load + to: dep2//store//load + kind: package + confidence: high + site: cargo:dep1@1.0.0 + - from: dep2//store//save + to: cargo:dep2@1.0.0//src/store.rs//save + kind: package + confidence: high + site: cargo:dep2@1.0.0//src/store.rs:1 + - from: dep2//store//load + to: cargo:dep2@1.0.0//src/store.rs//load + kind: package + confidence: high + site: cargo:dep2@1.0.0//src/store.rs:5 + - from: cargo:dep2@1.0.0//src/store.rs//save + to: std//fs//remove_file + kind: direct + confidence: high + site: cargo:dep2@1.0.0//src/store.rs:2 + - from: cargo:dep2@1.0.0//src/store.rs//load + to: std//fs//remove_dir + kind: direct + confidence: high + site: cargo:dep2@1.0.0//src/store.rs:6 + - from: cargo:dep1@1.0.0//src/bound.rs//audit + to: std//fs//read + kind: direct + confidence: high + site: cargo:dep1@1.0.0//src/bound.rs:4 +unresolved: [] +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: dep1//bound//save + site: src/main.rs:2 + kind: direct + confidence: high + - key: dep2//store//save + site: cargo:dep1@1.0.0 + kind: package + confidence: high + - key: cargo:dep2@1.0.0//src/store.rs//save + site: cargo:dep2@1.0.0//src/store.rs:1 + kind: package + confidence: high + - key: std//fs//remove_file + site: cargo:dep2@1.0.0//src/store.rs:2 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep2@1.0.0//src/store.rs//save + site: cargo:dep2@1.0.0//src/store.rs:2 + reached: true + - target: std//fs//remove_dir + status: found + path: + - key: src/main.rs//main + - key: dep1//glob//load + site: src/main.rs:3 + kind: direct + confidence: high + - key: dep2//store//load + site: cargo:dep1@1.0.0 + kind: package + confidence: high + - key: cargo:dep2@1.0.0//src/store.rs//load + site: cargo:dep2@1.0.0//src/store.rs:5 + kind: package + confidence: high + - key: std//fs//remove_dir + site: cargo:dep2@1.0.0//src/store.rs:6 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep2@1.0.0//src/store.rs//load + site: cargo:dep2@1.0.0//src/store.rs:6 + reached: true diff --git a/test/projectgraph/cases/rust/reexport-third-crate/project/Cargo.toml b/test/projectgraph/cases/rust/reexport-third-crate/project/Cargo.toml new file mode 100644 index 00000000..d90723b1 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +dep1 = "1.0.0" diff --git a/test/projectgraph/cases/rust/reexport-third-crate/project/src/main.rs b/test/projectgraph/cases/rust/reexport-third-crate/project/src/main.rs new file mode 100644 index 00000000..c4bf9661 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/project/src/main.rs @@ -0,0 +1,4 @@ +fn main() { + dep1::bound::save(); + dep1::glob::load(); +} diff --git a/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/Cargo.toml b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/Cargo.toml new file mode 100644 index 00000000..053e0a3c --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "dep1" +version = "1.0.0" +edition = "2021" + +[dependencies] +dep2 = "1.0.0" diff --git a/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/bound.rs b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/bound.rs new file mode 100644 index 00000000..9cc08fe8 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/bound.rs @@ -0,0 +1,5 @@ +pub use dep2::store::save; + +pub fn audit() { + let _ = std::fs::read("audit"); +} diff --git a/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/glob.rs b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/glob.rs new file mode 100644 index 00000000..80ce2c21 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/glob.rs @@ -0,0 +1 @@ +pub use dep2::store::*; diff --git a/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/lib.rs b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/lib.rs new file mode 100644 index 00000000..c1d93582 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep1/src/lib.rs @@ -0,0 +1,2 @@ +pub mod bound; +pub mod glob; diff --git a/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/Cargo.toml b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/Cargo.toml new file mode 100644 index 00000000..f2e71301 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "dep2" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/src/lib.rs b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/src/lib.rs new file mode 100644 index 00000000..55c88cbf --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/src/lib.rs @@ -0,0 +1 @@ +pub mod store; diff --git a/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/src/store.rs b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/src/store.rs new file mode 100644 index 00000000..8672c123 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/project/vendor/dep2/src/store.rs @@ -0,0 +1,7 @@ +pub fn save() { + let _ = std::fs::remove_file("save"); +} + +pub fn load() { + let _ = std::fs::remove_dir("load"); +} diff --git a/test/projectgraph/cases/rust/reexport-third-crate/summaries/cargo_dep1_1.0.0.json b/test/projectgraph/cases/rust/reexport-third-crate/summaries/cargo_dep1_1.0.0.json new file mode 100644 index 00000000..dcff5789 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/summaries/cargo_dep1_1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep1@1.0.0", + "symbols": [ + { + "key": "dep1//bound//audit", + "definitions": [ + "cargo:dep1@1.0.0//src/bound.rs//audit" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/reexport-third-crate/summaries/cargo_dep2_1.0.0.json b/test/projectgraph/cases/rust/reexport-third-crate/summaries/cargo_dep2_1.0.0.json new file mode 100644 index 00000000..45616ab4 --- /dev/null +++ b/test/projectgraph/cases/rust/reexport-third-crate/summaries/cargo_dep2_1.0.0.json @@ -0,0 +1,23 @@ +{ + "version": "3", + "index_version": "17", + "link_version": "20", + "unit": "cargo:dep2@1.0.0", + "symbols": [ + { + "key": "dep2//store//load", + "definitions": [ + "cargo:dep2@1.0.0//src/store.rs//load" + ], + "reaches": [] + }, + { + "key": "dep2//store//save", + "definitions": [ + "cargo:dep2@1.0.0//src/store.rs//save" + ], + "reaches": [] + } + ], + "targets": [] +} From 42156cef687dc6b271735aeed15b74c6c6eccfcf Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 00:24:21 +0000 Subject: [PATCH 74/86] perf(indexer): read the Rust uses of a list once and the binders only for a self or super target rustRebinder read every item of each block and declaration list around each call. An impl body with many methods made that read grow with the square of the body. The uses of a list now stay in a table of the file. The walk of the binders of a function for a method call now runs only when a target starts with self or super, because no other target can change. The index does not change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/project.go | 3 ++ plugin/indexer/rust.go | 60 ++++++++++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index 5bd6ad0b..9efc6397 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -79,6 +79,9 @@ type projection struct { // rustMacros holds the names that a macro_rules! or a use of a Rust // file binds, once a call of a macro reads them. rustMacros map[string]bool + // rustUses holds the uses of each block and declaration list of a Rust + // file, by the start and the end of the list. + rustUses map[[2]uint32][]rustUse } // project builds the call graph of tree and projects it into an index. It diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index d30e494d..b54f1954 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -815,8 +815,12 @@ func rustBoundName(pattern *sitter.Node) *sitter.Node { func rustShadowed(ctx context.Context, p *projection, call *fileindex.Call, callee ast.NodePosition) bool { node := p.descendant(callee) if method := rustMethod(node); node != nil && method != nil { - if binders, ok := p.rustBinders(ctx, call.Caller); ok { - p.rustFromFile(call, node, p.rustReceiverPath(binders, method)) + // Only a target from a self or super path can change, so the walk + // of the binders of the function runs only for such a target. + if slices.ContainsFunc(call.Targets, rustFromModule) { + if binders, ok := p.rustBinders(ctx, call.Caller); ok { + p.rustFromFile(call, node, p.rustReceiverPath(binders, method)) + } } return p.rustShadowedMethod(call, node) } @@ -883,6 +887,11 @@ func (p *projection) rustFromFile(call *fileindex.Call, node *sitter.Node, writt } } +// rustFromModule reports whether a target starts with self or super. +func rustFromModule(target string) bool { + return strings.HasPrefix(target, "self"+fileindex.Separator) || strings.HasPrefix(target, "super"+fileindex.Separator) +} + // rustReceiverPath returns the path of the type that the let of the // receiver of a method call writes, as super::Item of let i = // super::Item::new() or of let i = super::Item {}. The call graph types the @@ -958,16 +967,12 @@ func (p *projection) rustRebinder(node, scope *sitter.Node, name string) (*sitte switch n.Type() { case "block", "declaration_list": var globbed *sitter.Node - for _, item := range ts.NamedChildren(n) { - if item.Type() != "use_declaration" { - continue + for _, u := range p.rustUsesOf(n) { + if slices.Contains(u.names, name) { + return u.item, true } - names, glob := p.rustUseNames(item.ChildByFieldName("argument"), nil) - if slices.Contains(names, name) { - return item, true - } - if glob && !p.rustOuterGlob(item) { - globbed = item + if u.glob { + globbed = u.item } } if globbed != nil { @@ -984,6 +989,39 @@ func (p *projection) rustRebinder(node, scope *sitter.Node, name string) (*sitte return nil, false } +// rustUse is a use of a block or a declaration list: the names that it +// binds, and whether it is a glob use that can bring a name of an outer +// scope, as rustOuterGlob says. +type rustUse struct { + item *sitter.Node + names []string + glob bool +} + +// rustUsesOf returns the uses of a block or a declaration list, read once +// for the file. rustRebinder reads them for each call in the list, and a +// list such as an impl body with many methods made that read grow with the +// square of the list. +func (p *projection) rustUsesOf(list *sitter.Node) []rustUse { + k := [2]uint32{list.StartByte(), list.EndByte()} + if uses, ok := p.rustUses[k]; ok { + return uses + } + var uses []rustUse + for _, item := range ts.NamedChildren(list) { + if item.Type() != "use_declaration" { + continue + } + names, glob := p.rustUseNames(item.ChildByFieldName("argument"), nil) + uses = append(uses, rustUse{item: item, names: names, glob: glob && !p.rustOuterGlob(item)}) + } + if p.rustUses == nil { + p.rustUses = map[[2]uint32][]rustUse{} + } + p.rustUses[k] = uses + return uses +} + // rustOuterGlob reports whether a glob use brings no name that the call // graph would find in an outer scope instead: a glob of super or self, which // brings the names of the scopes that hold it, or of std, core or alloc, From 0978580088ae3a70d6a4bcd1e87051cf42a62018 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 00:28:33 +0000 Subject: [PATCH 75/86] test(projectgraph): update the hash rows for the third-crate re-export case The rows of fileindex 17, projectgraph 20 and reach 6 exist only on this branch, so they change in place. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- test/projectgraph/version_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index 1361860f..d2742100 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -34,7 +34,7 @@ var graphHashes = map[string]string{ "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", "16/18": "bb92620bf9903e8d19b52b62198a0bfd69d67856f9db226112b2cedf537b312b", "16/19": "51ba3e925c628602b3a53c1a6e68b379022a410bb5b749062b3045fef082ac8f", - "17/20": "35035a564611d1a01733a44dd1985ae8053efc036c4a3012ff86cbea1668b4fa", + "17/20": "36ac2003628d1acf80d68c43ddef8a257608fda5f6850fdf55e77509e4d86fa0", } // resultHashes records the hash of the reach results of every case for @@ -53,7 +53,7 @@ var resultHashes = map[string]string{ "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", "16/18/5": "e0c6093df3a85353ee15c8a90ef7c2a441f23348ce80c9d49793c256bddeffc5", "16/19/5": "a935650f8e4d01c0abdceeefb3332bf779927693a1c48b28263da5365db752cd", - "17/20/6": "f4f845cd28c356e6e7db514e51b482a340b7d731d35f9c3f35bb66840a5e0694", + "17/20/6": "197c1d93f531ed68b8b6b69c3e5ec8847b5e7b45d463295fe145068e93323ed7", } // summaryHashes records the hash of the golden summaries of the cases for @@ -73,7 +73,7 @@ var summaryHashes = map[string]string{ "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", "16/18/3": "694efb5f72075356359031d449d7b4ecbdca2ddd5099dacbcd2adad745ac6e19", "16/19/3": "13c72bf9eb60fc0a38bc600906fc02028d5cb60cb2aefbec0f0721779c429d80", - "17/20/3": "197788451ef4b26f40f72434cec03e17e2b213893e4ea0127b9248f489c5262c", + "17/20/3": "aa91355f8b5c0b3f9d68b08f2532ea5f34ccd8595e304f6ed7f1b0afec8d31e0", } func TestGraphHashFollowsVersion(t *testing.T) { From 76106a47a4b07e8aab5cc62ba71dcd77782a5ed0 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 00:38:30 +0000 Subject: [PATCH 76/86] test(projectgraph): move the indexed Rust glob test out of the CGO-free package TestBuildRustKeepsNoGlobListThatACutGave indexes a crate, so it needs the tree-sitter parser and CGO. The package projectgraph builds without CGO, and the CGO-free job runs its tests. The test now sits in test/projectgraph, which holds the other tests that index a project. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/rust_test.go | 48 ---------------------------------- test/projectgraph/glob_test.go | 42 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 48 deletions(-) create mode 100644 test/projectgraph/glob_test.go diff --git a/projectgraph/rust_test.go b/projectgraph/rust_test.go index 2d20b4c4..d50164d8 100644 --- a/projectgraph/rust_test.go +++ b/projectgraph/rust_test.go @@ -2,15 +2,9 @@ package projectgraph import ( "context" - "os" - "path/filepath" "testing" "testing/fstest" - codefs "github.com/safedep/code/fs" - "github.com/safedep/code/lang" - "github.com/safedep/code/plugin/indexer" - "github.com/safedep/code/fileindex" "github.com/safedep/code/langcode" "github.com/stretchr/testify/assert" @@ -109,45 +103,3 @@ func TestRustHelperProgram(t *testing.T) { }) } } - -// TestBuildRustKeepsNoGlobListThatACutGave links src/other.rs, which glob -// uses crate::sub::x and crate::c. sub gives x only through its own glob use -// of other, so the read of the glob uses of other asks for other again -// under the guard of a glob member. That read gets no module for -// crate::sub::x. The list of the glob modules of other that the build keeps -// must still hold c::x, so g() in run links g of src/c/x.rs through the -// glob use. -func TestBuildRustKeepsNoGlobListThatACutGave(t *testing.T) { - dir := writeFiles(t, map[string]string{ - "Cargo.toml": "[package]\nname = \"app\"\nversion = \"1.0.0\"\nedition = \"2021\"\n", - "src/main.rs": "mod c;\nmod other;\nmod sub;\n\nuse sub::*;\n\nfn main() {\n x::g();\n other::run();\n}\n", - "src/c/mod.rs": "pub mod x;\n", - "src/c/x.rs": "pub fn g() {}\n", - "src/other.rs": "use crate::sub::x::*;\npub use crate::c::*;\n\npub fn run() {\n g();\n}\n", - "src/sub.rs": "pub use crate::other::*;\n", - }) - languages, err := lang.AllLanguages() - require.NoError(t, err) - fsys, err := codefs.NewLocalFileSystem(codefs.LocalFileSystemConfig{AppDirectories: []string{dir}}) - require.NoError(t, err) - ix, err := indexer.New(indexer.Options{Languages: languages}) - require.NoError(t, err) - indexed, err := ix.Run(context.Background(), fsys) - require.NoError(t, err) - g, err := Build(context.Background(), indexed.Indexes, Options{Root: os.DirFS(dir), Files: indexed.Statuses(), Languages: indexed.Languages}) - require.NoError(t, err) - assert.Contains(t, edgeStrings(g), "src/other.rs//run -> src/c/x.rs//g direct rust.wildcard-import") -} - -// writeFiles writes the files, by their slash paths, to a new temporary -// directory and returns the directory. -func writeFiles(t testing.TB, files map[string]string) string { - t.Helper() - dir := t.TempDir() - for p, text := range files { - file := filepath.Join(dir, filepath.FromSlash(p)) - require.NoError(t, os.MkdirAll(filepath.Dir(file), 0o755)) - require.NoError(t, os.WriteFile(file, []byte(text), 0o644)) - } - return dir -} diff --git a/test/projectgraph/glob_test.go b/test/projectgraph/glob_test.go new file mode 100644 index 00000000..542ccc04 --- /dev/null +++ b/test/projectgraph/glob_test.go @@ -0,0 +1,42 @@ +package projectgraph + +import ( + "context" + "fmt" + "os" + "testing" + + codefs "github.com/safedep/code/fs" + "github.com/safedep/code/plugin/indexer" + "github.com/safedep/code/projectgraph" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// TestBuildRustKeepsNoGlobListThatACutGave links src/other.rs, which glob +// uses crate::sub::x and crate::c. sub gives x only through its own glob use +// of other, so the read of the glob uses of other asks for other again +// under the guard of a glob member. That read gets no module for +// crate::sub::x. The list of the glob modules of other that the build keeps +// must still hold c::x, so g() in run links g of src/c/x.rs through the +// glob use. +func TestBuildRustKeepsNoGlobListThatACutGave(t *testing.T) { + dir := writeFiles(t, map[string]string{ + "Cargo.toml": "[package]\nname = \"app\"\nversion = \"1.0.0\"\nedition = \"2021\"\n", + "src/main.rs": "mod c;\nmod other;\nmod sub;\n\nuse sub::*;\n\nfn main() {\n x::g();\n other::run();\n}\n", + "src/c/mod.rs": "pub mod x;\n", + "src/c/x.rs": "pub fn g() {}\n", + "src/other.rs": "use crate::sub::x::*;\npub use crate::c::*;\n\npub fn run() {\n g();\n}\n", + "src/sub.rs": "pub use crate::other::*;\n", + }) + fsys, err := codefs.NewLocalFileSystem(codefs.LocalFileSystemConfig{AppDirectories: []string{dir}}) + require.NoError(t, err) + indexed := indexProject(t, fsys, indexer.Options{Languages: caseLanguages(t)}) + g, err := projectgraph.Build(context.Background(), indexed.Indexes, projectgraph.Options{Root: os.DirFS(dir), Files: indexed.Statuses(), Languages: indexed.Languages}) + require.NoError(t, err) + var edges []string + for _, e := range g.Edges { + edges = append(edges, fmt.Sprintf("%s -> %s %s %s", e.From, e.To, e.Kind, e.Resolver)) + } + assert.Contains(t, edges, "src/other.rs//run -> src/c/x.rs//g direct rust.wildcard-import") +} From c86e581df14ee7ace0c0f7e6137cdc4fb56c5d4c Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 00:41:38 +0000 Subject: [PATCH 77/86] fix(rootmatch): root no helper function inside a member of a route object A route rule marks each function under an exported object of the file, as the actions of a SvelteKit page. It also marked a function that a member defines inside its body. Such a helper runs only when a call reaches it, so a dead helper made its calls read found. A function under the object is a root now only when no other function of the file holds it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/rootmatch/framework.go | 19 ++++++++++++++++- .../sveltekit-actions-lib/expect.yaml | 21 +++++++++++++++---- .../project/src/routes/form/+page.server.ts | 5 ++++- test/projectgraph/version_test.go | 4 ++-- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/projectgraph/rootmatch/framework.go b/projectgraph/rootmatch/framework.go index b605c097..875023d0 100644 --- a/projectgraph/rootmatch/framework.go +++ b/projectgraph/rootmatch/framework.go @@ -61,13 +61,30 @@ func (f *finder) addOpaque(r rules.Rule, path string, ex fileindex.Export) { // addMembers marks each function under an exported object of the file. The // framework calls a function of the object by its name, as the actions of // a SvelteKit page, and no edge of the graph leads from the object to it. +// A function that another function of the object holds is a helper of +// that function, and it runs only when a call reaches it. func (f *finder) addMembers(r rules.Rule, ix *fileindex.Index, exports []string) { objects := slices.DeleteFunc(slices.Clone(exports), func(key string) bool { def, ok := f.defs[key] return !ok || def.Kind != fileindex.DefVariable }) + code := map[string]bool{} for def := range ix.CodeDefinitions() { - if slices.ContainsFunc(objects, func(key string) bool { return def.Key != key && fileindex.HasKeyPrefix(def.Key, key) }) { + code[def.Key] = true + } + member := func(key, object string) bool { + if key == object || !fileindex.HasKeyPrefix(key, object) { + return false + } + for p := fileindex.Parent(key); p != object; p = fileindex.Parent(p) { + if code[p] { + return false + } + } + return true + } + for def := range ix.CodeDefinitions() { + if slices.ContainsFunc(objects, func(object string) bool { return member(def.Key, object) }) { f.add(r, def.Key) } } diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml b/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml index 6fd9d475..8dc1d321 100644 --- a/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/expect.yaml @@ -5,7 +5,9 @@ description: >- of a route file, so a1 and a2 are found. The API route imports helper by the alias $lib, which SvelteKit maps to src/lib of the package that depends on @sveltejs/kit, so the call of helper reaches c1. The object actions of items/+page.server.ts has a satisfies type, and its functions keep their names under - actions, so a6 is found. + actions, so a6 is found. The function unused inside save is a helper of save, not a member of actions. + No code calls it, so a7 is not-found. Without the rule that skips such a helper, unused is a root and + a7 reads found. roots: - key: server.js kind: manifest @@ -39,6 +41,11 @@ roots: rule: node.sveltekit.route exact_edges: true edges: + - from: src/routes/form/+page.server.ts//actions//save//unused + to: plug//a7 + kind: direct + confidence: high + site: src/routes/form/+page.server.ts:9 - from: server.js to: plug//listen kind: direct @@ -68,7 +75,7 @@ edges: to: plug//a2 kind: direct confidence: high - site: src/routes/form/+page.server.ts:8 + site: src/routes/form/+page.server.ts:11 - from: src/routes/items/+page.server.ts//actions//default to: plug//a6 kind: direct @@ -95,12 +102,12 @@ results: - key: src/routes/form/+page.server.ts//actions//save root: framework - key: plug//a2 - site: src/routes/form/+page.server.ts:8 + site: src/routes/form/+page.server.ts:11 kind: direct confidence: high call_sites: - caller: src/routes/form/+page.server.ts//actions//save - site: src/routes/form/+page.server.ts:8 + site: src/routes/form/+page.server.ts:11 reached: true - target: plug//c1 status: found @@ -132,3 +139,9 @@ results: - caller: src/routes/items/+page.server.ts//actions//default site: src/routes/items/+page.server.ts:6 reached: true + - target: plug//a7 + status: not-found + call_sites: + - caller: src/routes/form/+page.server.ts//actions//save//unused + site: src/routes/form/+page.server.ts:9 + reached: false diff --git a/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/form/+page.server.ts b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/form/+page.server.ts index b32490d7..a313b363 100644 --- a/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/form/+page.server.ts +++ b/test/projectgraph/cases/javascript/sveltekit-actions-lib/project/src/routes/form/+page.server.ts @@ -1,10 +1,13 @@ -import { a1, a2 } from 'plug'; +import { a1, a2, a7 } from 'plug'; export const actions = { default: async ({ request }) => { a1(request); }, save({ request }) { + function unused() { + a7(); + } a2(request); }, }; diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index d2742100..bbd7e1cf 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -34,7 +34,7 @@ var graphHashes = map[string]string{ "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", "16/18": "bb92620bf9903e8d19b52b62198a0bfd69d67856f9db226112b2cedf537b312b", "16/19": "51ba3e925c628602b3a53c1a6e68b379022a410bb5b749062b3045fef082ac8f", - "17/20": "36ac2003628d1acf80d68c43ddef8a257608fda5f6850fdf55e77509e4d86fa0", + "17/20": "aeef882849539833bea2e9c584d02e9e81389539a61cee4384e0046e7c126796", } // resultHashes records the hash of the reach results of every case for @@ -53,7 +53,7 @@ var resultHashes = map[string]string{ "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", "16/18/5": "e0c6093df3a85353ee15c8a90ef7c2a441f23348ce80c9d49793c256bddeffc5", "16/19/5": "a935650f8e4d01c0abdceeefb3332bf779927693a1c48b28263da5365db752cd", - "17/20/6": "197c1d93f531ed68b8b6b69c3e5ec8847b5e7b45d463295fe145068e93323ed7", + "17/20/6": "a6d059a795ef42aa79d6f2a004d030330d8988e7384a0c28568ece8be2178294", } // summaryHashes records the hash of the golden summaries of the cases for From a7eab88bb2afba6a971f77bb44d87ed317b2c538 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 01:41:38 +0000 Subject: [PATCH 78/86] docs(projectgraph): keep the guide to the API and drop the rule-by-rule text The guide now tells a caller what to call, what each call gives and what the answers mean. It keeps the steps, a short example per step, the options that a caller sets, the statuses and the uncertainty kinds, the node and edge kinds, the roots and sources, the versions and the known limits. The code, its comments and the cases under test/projectgraph/cases record each rule. So the guide no longer copies the rules of each language. Each code comment that sent the reader to the guide for a rule now states the reason in place and names the case that pins it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 1593 ++++++------------------------------ plugin/indexer/govalues.go | 20 +- plugin/indexer/rust.go | 4 +- projectgraph/golang.go | 11 +- projectgraph/link.go | 9 +- projectgraph/rust.go | 10 +- 6 files changed, 271 insertions(+), 1376 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index b73af6fe..3c4c40b7 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -1,1452 +1,323 @@ # Project graph -The project graph is the call graph of a whole project, across its files -and into the code of its dependencies. It answers one question: does code -that runs reach an external symbol, as `yaml//load`? The answer is `found`, -`not-found` or `uncertain`, and the library never gives `not-found` when it +The project graph is the call graph of a whole project, across its files and +into the code of its dependencies. It answers one question. Does code that runs +reach an external symbol, as `yaml//load`? The answer is `found`, `not-found`, +`uncertain` or `no-such-target`. The library never gives `not-found` when it does not know. -Five calls make the graph. Each one reads only the output of the one -before it, so a job can run them at different times and keep the output of -each in a store. +This guide tells a caller what to call, what each call gives and what the +answers mean. The code, its comments and the cases under +`test/projectgraph/cases` record each rule. The `description` of each +`expect.yaml` tells in prose what its case pins. + +## Steps + +Each step reads only the output of the step before it. A job can run the steps +at different times and keep the output of each step in a store. | Step | Call | Reads | Gives | | --- | --- | --- | --- | -| Index | `indexer.New` and `Indexer.Run` | The source files, with tree-sitter | One `fileindex.Index` per file | +| Index | `indexer.New`, `Indexer.Run` | The source files, with tree-sitter | One `fileindex.Index` per file | | Link | `projectgraph.Build` | The indexes and the manifests | A `projectgraph.Graph` | -| Roots | `rootmatch.Find` | The indexes and the manifests | The roots of the graph. `Build` calls it | +| Roots | `rootmatch.Find` | The indexes and the manifests | The roots of the graph. `Build` calls it, so a caller does not | | Reach | `reach.Reach` | The graph | One `reach.Result` per target | -| Summary | `summary.Build` | The indexes of one package | A `summary.Summary` of the package | - -The index step is the only one that parses source. It needs CGO. The other -steps read records and build with `CGO_ENABLED=0`. +| Summary | `summary.Build` | The indexes of one package | A `summary.Result` that holds the `summary.Summary` of the package | -An [overlay](#overlay) is an optional step between the link step and the -reach query. It merges edges that a model proposed for the call sites -that the link step could not resolve. - -`examples/projectgraph` runs all five from the command line. +Only the index step parses source, and only the index step needs CGO. The other +steps build with `CGO_ENABLED=0`. An [overlay](#overlay) is an optional step +between the link step and the reach query. It merges edges that a model +proposed for the call sites that the link step did not resolve. +`examples/projectgraph` runs all the steps from the command line. ## Index ```go -ix, err := indexer.New(indexer.Options{ - Languages: languages, - Store: store, - Workers: 4, +languages, err := lang.AllLanguages() +fsys, err := fs.NewLocalFileSystem(fs.LocalFileSystemConfig{ + AppDirectories: []string{root}, + ImportDirectories: []string{lodashDir}, + ImportUnits: map[string]string{lodashDir: "npm:lodash@4.17.21"}, }) +ix, err := indexer.New(indexer.Options{Languages: languages, Store: memstore.New(), Imports: true}) indexed, err := ix.Run(ctx, fsys) ``` -`New` validates the options: a run needs a language, two languages -cannot share a file extension, and a size limit or a number of workers -cannot be negative. `Run` walks the file system, reads each file once and gives the tree of the -file to the index plugin. The index of a file holds what the link step -needs: the imports, the exports, the definitions, the classes with their -bases, the calls with their resolved targets, the references, the -unresolved call sites and the dynamic flags. It holds no tree-sitter node -and no source text past a short call text. - -`indexer.Options` has these fields. - -| Field | Meaning | +| `indexer.Options` field | Meaning | | --- | --- | | `Languages` | The languages to index. The run reads only the files that one of them parses | | `Store` | A `store.Store` that keeps the indexes between runs. A nil store parses every file | | `Plugins` | Extra plugins that run on the tree of each file | -| `MaxFileBytes` | The size limit of a file. A larger file fails. Zero selects 4 MiB | -| `Workers` | The number of files to parse at once. Zero selects the number of CPUs. The result does not depend on it | -| `Imports` | Index the files of the import directories too. Each one needs a package unit, as `npm:lodash@4.17.21` | - -`indexer.Result` holds the indexes in path order, the status of each file -(`indexed`, `cached`, `missing`, `failed`, `truncated`), the same for the -files of dependency code, and the report of the run. `Result.Unit` returns -the indexes and the files of one unit of dependency code. - -A generated Go file of a unit, with a `// Code generated ... DO NOT EDIT.` -line or a name that starts with `tables`, can hit a limit of the index, -as a table of thousands of entries. Its index then comes from the file -with each top-level var value that holds only data blanked. Function bodies -and values with a call or a function literal stay, so the calls of a -generated file, as a `.pb.go` file, stay in the graph. A var value that -names a function stays too, as a map of handlers, because a call of a -function value finds the function only through that reference. The index keeps the -hash of the file. If the skeleton still hits a limit, the file stays -truncated, and reach counts it as budget. - -The JavaScript language reads `.jsx` files with its grammar, which parses -JSX. It also reads the component files of Vue, Svelte and Astro (`.vue`, -`.svelte`, `.astro`). The parse reads only the `