From 20eaf5919a03fe69da06d2f72fb07c31806431f0 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 08:51:54 +0000 Subject: [PATCH 01/58] feat(lang): parse .jsx files and the scripts of Vue, Svelte and Astro files (FOU-576) The JavaScript language read .js, .mjs and .cjs files only. A React project of .jsx files gave no file to the index, so vet marked its npm packages unknown with no source files. The script blocks of .vue, .svelte and .astro files hold imports too, and the index missed them. The JavaScript language now lists .jsx, .vue, .svelte and .astro. Its grammar parses JSX. .tsx files already parse with the TSX grammar. A new optional interface, core.SourceSelector, picks the language of a file from its bytes. The JavaScript language implements it for the component files. It replaces each byte outside the script blocks with a space and keeps the line ends. Each import and call then keeps its line and column in the file. A block with lang="ts" or lang="typescript" makes the file TypeScript, and lang="tsx" makes it TSX. Astro reads the code between the two --- lines at the top, and every script, as TypeScript. A block with a src attribute holds no code. A block that does not close runs to the end of the file. The parser parses the source that the selector gives, and the tree keeps the bytes of the file, so the index keeps the hash of the file. The indexer picks the language before it makes the store key, so the key and the index agree and a warm run serves a TypeScript component file from the store. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- core/language.go | 11 ++ docs/projectgraph.md | 10 ++ lang/component.go | 216 +++++++++++++++++++++++++++++++++ lang/component_test.go | 169 ++++++++++++++++++++++++++ lang/factory_test.go | 4 + lang/javascript.go | 41 ++++++- parser/parser.go | 86 +++++++------ parser/parser_test.go | 32 +++++ plugin/indexer/indexer.go | 5 + plugin/indexer/indexer_test.go | 85 +++++++++++++ 10 files changed, 619 insertions(+), 40 deletions(-) create mode 100644 lang/component.go create mode 100644 lang/component_test.go diff --git a/core/language.go b/core/language.go index 854b19e6..c448e457 100644 --- a/core/language.go +++ b/core/language.go @@ -95,3 +95,14 @@ type Language interface { type GrammarSelector interface { LanguageForExtension(ext string) Language } + +// SourceSelector is an optional interface of a Language whose files of some +// extensions hold their code in blocks among markup, as the script blocks +// of a Vue component. Source returns the language of the code and the bytes +// that its grammar parses: data with each byte outside the code replaced by +// a space. The bytes keep the offsets and the line ends of data, so a node +// reports its line and column in the file, and its content in data. A file +// of another extension gives the Language and data. +type SourceSelector interface { + Source(ext string, data []byte) (Language, []byte) +} diff --git a/docs/projectgraph.md b/docs/projectgraph.md index e8c2e180..017b8b8d 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -72,6 +72,16 @@ generated file, as a `.pb.go` file, stay in the graph. 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 `\n\n\n"), + }, + }, + { + name: "lang ts gives TypeScript", + ext: ".svelte", + parts: []part{ + markup(`\n

{n}

\n"), + }, + dialect: dialectTypescript, + }, + { + name: "lang typescript gives TypeScript and a quoted > stays in the tag", + ext: ".vue", + parts: []part{ + markup(`"), + }, + dialect: dialectTypescript, + }, + { + name: "lang tsx gives TSX over the TypeScript of another block", + ext: ".vue", + parts: []part{ + markup(`\n\n"), + }, + dialect: dialectTSX, + }, + { + name: "a block with a src attribute gives no code and no dialect", + ext: ".vue", + parts: []part{ + markup(``), markup("\n\n"), + }, + }, + { + name: "a block that does not close runs to the end of the file", + ext: ".svelte", + parts: []part{ + markup("

hi

\n"), + }, + }, + { + name: "a longer tag name is not a script", + ext: ".vue", + parts: []part{ + markup("\nnot code\n\n"), + }, + }, + { + name: "Astro reads the fence and the scripts as TypeScript", + ext: ".astro", + parts: []part{ + markup("\n---\n"), code("import pad from 'left-pad'\nconst title: string = pad('a', 2)\n"), markup("---\n

{title}

\n\n"), + }, + dialect: dialectTypescript, + }, + { + name: "an Astro fence that does not close runs to the end of the file", + ext: ".astro", + parts: []part{ + markup("---\n"), code("import pad from 'left-pad'\n\n"), + }, + dialect: dialectTypescript, + }, + { + name: "an Astro file with no fence reads its scripts", + ext: ".astro", + parts: []part{ + markup("

---

\n"), + }, + dialect: dialectTypescript, + }, + { + name: "CRLF line ends stay in the markup", + ext: ".vue", + parts: []part{ + markup("\r\n\r\n"), + }, + }, + { + name: "an Astro fence with CRLF line ends", + ext: ".astro", + parts: []part{ + markup("---\r\n"), code("import pad from 'left-pad'\r\n"), markup("---\r\n

\r\n"), + }, + dialect: dialectTypescript, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + var data, want strings.Builder + for _, p := range tc.parts { + data.WriteString(p.text) + if p.code { + want.WriteString(p.text) + } else { + want.WriteString(blankExceptLineEnds(p.text)) + } + } + + source, dialect := componentSource(tc.ext, []byte(data.String())) + assert.Equal(t, want.String(), string(source)) + assert.Equal(t, tc.dialect, dialect) + }) + } +} + +// blankExceptLineEnds blanks each byte, so a rune of several bytes keeps +// its width in bytes, as the columns of tree-sitter count. +func blankExceptLineEnds(s string) string { + b := []byte(s) + for i, c := range b { + if c != '\n' && c != '\r' { + b[i] = ' ' + } + } + return string(b) +} diff --git a/lang/factory_test.go b/lang/factory_test.go index e67cacc8..cf77db39 100644 --- a/lang/factory_test.go +++ b/lang/factory_test.go @@ -18,6 +18,10 @@ var resolveLanguageTestcases = []struct { {filePath: "test.js", exists: true, expectedLanguageCode: core.LanguageCodeJavascript}, {filePath: "test.cjs", exists: true, expectedLanguageCode: core.LanguageCodeJavascript}, {filePath: "test.mjs", exists: true, expectedLanguageCode: core.LanguageCodeJavascript}, + {filePath: "App.jsx", exists: true, expectedLanguageCode: core.LanguageCodeJavascript}, + {filePath: "App.vue", exists: true, expectedLanguageCode: core.LanguageCodeJavascript}, + {filePath: "App.svelte", exists: true, expectedLanguageCode: core.LanguageCodeJavascript}, + {filePath: "page.astro", exists: true, expectedLanguageCode: core.LanguageCodeJavascript}, {filePath: "test.go", exists: true, expectedLanguageCode: core.LanguageCodeGo}, {filePath: "test.java", exists: true, expectedLanguageCode: core.LanguageCodeJava}, {filePath: "test.rs", exists: true, expectedLanguageCode: core.LanguageCodeRust}, diff --git a/lang/javascript.go b/lang/javascript.go index 269debea..73c67243 100644 --- a/lang/javascript.go +++ b/lang/javascript.go @@ -1,6 +1,8 @@ package lang import ( + "slices" + "github.com/safedep/code/core" sitter "github.com/smacker/go-tree-sitter" "github.com/smacker/go-tree-sitter/javascript" @@ -8,24 +10,40 @@ import ( const javascriptLanguageName = "javascript" -type javascriptLanguage struct{} +type javascriptLanguage struct { + // typescript and tsx parse the script blocks of a component file that + // name TypeScript. + typescript core.Language + tsx core.Language +} var _ core.Language = (*javascriptLanguage)(nil) +var _ core.SourceSelector = (*javascriptLanguage)(nil) func NewJavascriptLanguage() (*javascriptLanguage, error) { - return &javascriptLanguage{}, nil + typescript, err := NewTypescriptWithoutJSXLanguage() + if err != nil { + return nil, err + } + tsx, err := NewTypescriptLanguage() + if err != nil { + return nil, err + } + return &javascriptLanguage{typescript: typescript, tsx: tsx}, nil } func (l *javascriptLanguage) Name() string { return javascriptLanguageName } +// Meta lists .jsx with the JavaScript extensions, because the JavaScript +// grammar parses JSX. func (l *javascriptLanguage) Meta() core.LanguageMeta { return core.LanguageMeta{ Name: javascriptLanguageName, Code: core.LanguageCodeJavascript, ObjectOriented: true, - SourceFileExtensions: []string{".js", ".mjs", ".cjs"}, + SourceFileExtensions: append([]string{".js", ".mjs", ".cjs", ".jsx"}, componentExtensions...), } } @@ -36,3 +54,20 @@ func (l *javascriptLanguage) Language() *sitter.Language { func (l *javascriptLanguage) Resolvers() core.LanguageResolvers { return &javascriptResolvers{} } + +// Source gives the script blocks of a component file to the language of +// their dialect. It gives every other file to JavaScript. +func (l *javascriptLanguage) Source(ext string, data []byte) (core.Language, []byte) { + if !slices.Contains(componentExtensions, ext) { + return l, data + } + source, dialect := componentSource(ext, data) + switch dialect { + case dialectTSX: + return l.tsx, source + case dialectTypescript: + return l.typescript, source + default: + return l, source + } +} diff --git a/parser/parser.go b/parser/parser.go index c542ac8f..18a4525e 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -13,14 +13,10 @@ import ( // parserWrapper parses the files of the languages that it was built with. // It is not safe for concurrent use, because a sitter.Parser is not. type parserWrapper struct { - byExtension map[string]*grammar - maxBytes int -} - -// grammar is a language with the sitter parser that parses its files. -type grammar struct { - language core.Language - parser *sitter.Parser + byExtension map[string]core.Language + // parsers holds the sitter parser of each language that parsed a file. + parsers map[core.Language]*sitter.Parser + maxBytes int } type parseTree struct { @@ -35,30 +31,19 @@ var _ core.ParseTree = (*parseTree)(nil) // NewParser creates a parser for the given languages. It picks the language // of a file by the file extension. A language that implements -// core.GrammarSelector picks the language of each of its extensions. The -// caller closes the parser. +// core.GrammarSelector picks the language of each of its extensions, and a +// language that implements core.SourceSelector picks the language of the +// content. The caller closes the parser. func NewParser(languages []core.Language, opts ...Option) (*parserWrapper, error) { cfg := newConfig(opts) - fileLanguages, err := LanguagesByExtension(languages) + byExtension, err := LanguagesByExtension(languages) if err != nil { return nil, err } - byExtension := make(map[string]*grammar, len(fileLanguages)) - byLanguage := make(map[core.Language]*grammar) - for ext, language := range fileLanguages { - g, ok := byLanguage[language] - if !ok { - parser := sitter.NewParser() - parser.SetLanguage(language.Language()) - g = &grammar{language: language, parser: parser} - byLanguage[language] = g - } - byExtension[ext] = g - } - return &parserWrapper{ byExtension: byExtension, + parsers: map[core.Language]*sitter.Parser{}, maxBytes: cfg.maxBytes, }, nil } @@ -86,9 +71,19 @@ func LanguagesByExtension(languages []core.Language) (map[string]core.Language, // Close frees the sitter parsers. The trees that the parser made stay // valid. The parser must not parse after Close. func (p *parserWrapper) Close() { - for _, g := range p.byExtension { - g.parser.Close() + for _, sp := range p.parsers { + sp.Close() + } +} + +// Source returns the language that parses data as the content of the file +// of the name, and the bytes that its grammar parses. language is the +// language of the extension of the name. A core.SourceSelector picks both. +func Source(language core.Language, name string, data []byte) (core.Language, []byte) { + if selector, ok := language.(core.SourceSelector); ok { + return selector.Source(filepath.Ext(name), data) } + return language, data } // Parse parses the file with the language of its extension. It returns an @@ -105,7 +100,7 @@ func (p *parserWrapper) parse(ctx context.Context, file core.File) (core.ParseTr return nil, core.StageRead, err } - g, err := p.grammarOf(file) + language, err := p.languageOf(file) if err != nil { return nil, core.StageParse, err } @@ -115,7 +110,7 @@ func (p *parserWrapper) parse(ctx context.Context, file core.File) (core.ParseTr return nil, core.StageRead, err } - tree, err := p.parseContent(ctx, g, file, data) + tree, err := p.parseContent(ctx, language, file, data) return tree, core.StageParse, err } @@ -127,31 +122,48 @@ func (p *parserWrapper) ParseContent(ctx context.Context, file core.File, data [ if err := ctx.Err(); err != nil { return nil, err } - g, err := p.grammarOf(file) + language, err := p.languageOf(file) if err != nil { return nil, err } - return p.parseContent(ctx, g, file, data) + return p.parseContent(ctx, language, file, data) } -func (p *parserWrapper) grammarOf(file core.File) (*grammar, error) { - g, ok := p.byExtension[filepath.Ext(file.Name())] +func (p *parserWrapper) languageOf(file core.File) (core.Language, error) { + language, ok := p.byExtension[filepath.Ext(file.Name())] if !ok { return nil, fmt.Errorf("%w: %s", core.ErrNoLanguage, file.Name()) } - return g, nil + return language, nil +} + +// sitterParser returns the sitter parser of the language, and makes it at +// the first file of the language. +func (p *parserWrapper) sitterParser(language core.Language) *sitter.Parser { + sp, ok := p.parsers[language] + if !ok { + sp = sitter.NewParser() + sp.SetLanguage(language.Language()) + p.parsers[language] = sp + } + return sp } -func (p *parserWrapper) parseContent(ctx context.Context, g *grammar, file core.File, data []byte) (core.ParseTree, error) { +// parseContent parses the source of data. The tree keeps data, not the +// source, so the tree data is the bytes of the file. The source keeps the +// offsets of data, so a node has the same content in both. +func (p *parserWrapper) parseContent(ctx context.Context, language core.Language, file core.File, data []byte) (core.ParseTree, error) { if p.maxBytes > 0 && len(data) > p.maxBytes { return nil, fmt.Errorf("%w: %s is over %d bytes", ErrInputTooLarge, file.Name(), p.maxBytes) } - tree, err := g.parser.ParseCtx(ctx, nil, data) + language, source := Source(language, file.Name(), data) + sp := p.sitterParser(language) + tree, err := sp.ParseCtx(ctx, nil, source) if err != nil { // A sitter parser resumes a cancelled parse on its next call. // The reset makes the next file parse from its start. - g.parser.Reset() + sp.Reset() return nil, fmt.Errorf("failed to parse file: %w", err) } @@ -159,7 +171,7 @@ func (p *parserWrapper) parseContent(ctx context.Context, g *grammar, file core. tree: tree, data: &data, file: file, - lang: g.language, + lang: language, }, nil } diff --git a/parser/parser_test.go b/parser/parser_test.go index 3542a22a..26baaed3 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -361,3 +361,35 @@ func TestParseAfterADeadlineParsesTheNextFile(t *testing.T) { root := tree.Tree().RootNode() assert.Equal(t, "(module (expression_statement (call function: (identifier) arguments: (argument_list))))", root.String()) } + +func TestParseReadsJSXAndTheScriptsOfComponentFiles(t *testing.T) { + const vue = "\n\n" + cases := []struct { + file string + content string + code core.LanguageCode + }{ + {file: "App.jsx", content: "const e =

{x}
;\n", code: core.LanguageCodeJavascript}, + {file: "App.vue", content: vue, code: core.LanguageCodeTypescript}, + } + for _, tc := range cases { + t.Run(tc.file, func(t *testing.T) { + p, err := NewParser([]core.Language{javascript(t)}) + require.NoError(t, err) + t.Cleanup(p.Close) + + tree, err := p.Parse(t.Context(), &trackedFile{name: tc.file, content: tc.content}) + require.NoError(t, err) + defer tree.Close() + assert.False(t, tree.Tree().RootNode().HasError()) + + treeLanguage, err := tree.Language() + require.NoError(t, err) + assert.Equal(t, tc.code, treeLanguage.Meta().Code) + + data, err := tree.Data() + require.NoError(t, err) + assert.Equal(t, tc.content, string(*data), "the tree keeps the bytes of the file") + }) + } +} diff --git a/plugin/indexer/indexer.go b/plugin/indexer/indexer.go index 45e49c67..c1b7f816 100644 --- a/plugin/indexer/indexer.go +++ b/plugin/indexer/indexer.go @@ -295,6 +295,11 @@ func (r *run) index(ctx context.Context, files []core.File, byExtension map[stri r.exclude(file) continue } + // The script blocks of a component file name their language, and + // the key and the index of the file hold it. The parse picks the + // same language again from the same bytes. + language, _ = parser.Source(language, file.Name(), data) + r.result.Languages[file.Name()] = language.Meta().Code batch = append(batch, pending{ file: file, diff --git a/plugin/indexer/indexer_test.go b/plugin/indexer/indexer_test.go index 40c27a7c..a48ac8a6 100644 --- a/plugin/indexer/indexer_test.go +++ b/plugin/indexer/indexer_test.go @@ -471,3 +471,88 @@ func TestNewValidatesTheOptions(t *testing.T) { caller[0] = nil assert.Equal(t, languages, ix.opts.Languages, "a change of the caller's slice after New does not reach the indexer") } + +func TestIndexerReadsJSXAndTheScriptsOfComponentFiles(t *testing.T) { + type importAt struct { + Module string + Line int + } + type callAt struct { + Callee string + Line int + Column int + } + files := map[string]struct { + content string + language core.LanguageCode + imports []importAt + calls []callAt + }{ + "App.jsx": { + content: "import pad from 'left-pad'\n\nexport default function App() {\n return
{pad('a', 2)}
;\n}\n", + language: core.LanguageCodeJavascript, + imports: []importAt{{"left-pad", 1}}, + calls: []callAt{{"pad", 4, 16}}, + }, + "App.vue": { + content: "\n\n\n", + language: core.LanguageCodeJavascript, + imports: []importAt{{"left-pad", 6}}, + calls: []callAt{{"pad", 7, 13}}, + }, + "Widget.vue": { + content: "\n\r\n\r\n", + language: core.LanguageCodeTypescript, + imports: []importAt{{"vue", 2}, {"left-pad", 7}}, + calls: []callAt{{"defineComponent", 3, 16}, {"pad", 8, 19}}, + }, + "Page.astro": { + content: "---\nimport pad from 'left-pad'\nconst title: string = pad('a', 2)\n---\n

{title}

\n\n", + language: core.LanguageCodeTypescript, + imports: []importAt{{"left-pad", 2}, {"canvas-confetti", 7}}, + calls: []callAt{{"pad", 3, 23}, {"confetti", 8, 3}}, + }, + "Card.svelte": { + content: "

{label}

\n\n", + language: core.LanguageCodeJavascript, + imports: []importAt{{"left-pad", 3}}, + calls: []callAt{{"pad", 4, 15}}, + }, + } + dir := t.TempDir() + contents := map[string]string{} + for name, f := range files { + contents[name] = f.content + } + writeFiles(t, dir, contents) + fsys := appFileSystem(t, dir) + opts := Options{Languages: projectLanguages(t), Store: memstore.New()} + + cold, err := runIndexer(t, context.Background(), fsys, opts) + require.NoError(t, err) + assert.Empty(t, cold.Report.Failed) + require.Len(t, cold.Indexes, len(files)) + for _, ix := range cold.Indexes { + want := files[ix.Path] + t.Run(ix.Path, func(t *testing.T) { + assert.Equal(t, want.language, ix.Language) + assert.Equal(t, want.language, cold.Languages[ix.Path]) + assert.Equal(t, fileindex.FileIndexed, cold.Files[ix.Path]) + var imports []importAt + for _, imp := range ix.Imports { + imports = append(imports, importAt{imp.Module, imp.Line}) + } + assert.Equal(t, want.imports, imports) + var calls []callAt + for _, c := range ix.Calls { + calls = append(calls, callAt{c.Callee, c.Line, c.Column}) + } + assert.Equal(t, want.calls, calls) + }) + } + + warm, err := runIndexer(t, context.Background(), fsys, opts) + require.NoError(t, err) + assert.Empty(t, warm.Report.Visited) + assert.Equal(t, cold.Indexes, warm.Indexes) +} From 2a4877bd971c8255c4af7518338821db19496520 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 08:54:36 +0000 Subject: [PATCH 02/58] fix(indexer): store the finished files of a batch when the context ends (FOU-575) When the parse of a batch returned the error of the context, run.flush skipped Store.Put and Store.PutArtifacts for the whole batch. The files that finished before the deadline were lost too. A scan with the same time budget parsed the same partial batch again and never warmed the store past it. flush now stores the indexes and the artifacts of the files that finished, and then returns the error of the context. The store write ignores the end of the context, because a done context fails the write. A file that the deadline cut keeps no index and no artifact. A cut index has a deadline truncation and fails its file, as before. Two paths could still give a partial result as whole, so the change closes them: - skeletonIndex returned the skeleton index of a generated Go table with a deadline truncation and no error. It now fails the file, as the index plugin does. - A cached plugin can stop at the end of the context and return a partial artifact with no error. The run now stores an artifact only when the context is still live after the plugin returns. The next run parses that file again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 8 ++ plugin/indexer/cache.go | 3 +- plugin/indexer/cache_test.go | 165 ++++++++++++++++++++++++++++++++++ plugin/indexer/gounit.go | 3 + plugin/indexer/gounit_test.go | 35 ++++++++ plugin/indexer/indexer.go | 39 +++++--- plugin/indexer/plugin.go | 5 ++ plugin/indexer/plugin_test.go | 43 +++++---- 8 files changed, 273 insertions(+), 28 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index e8c2e180..2e569d66 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -84,6 +84,14 @@ as `missing` and returns the result so far with the error of the context. The link step takes the partial result, and the reach query reports each missing file as a budget cause. +Before it returns, `Run` puts the indexes and the artifacts of the files that +it finished in the store. A run with the same deadline then reads them from +the store and gets further into the project. The store gets no index and no +artifact of a file that the deadline cut. An index with a deadline +truncation fails its file. A cached plugin that returns after the end of +the context can have stopped early with no error, so the store gets no +artifact from such a call. The next run parses that file again. + ### Store A `store.Store` keeps each index by `fileindex.Key`: the hash of the bytes diff --git a/plugin/indexer/cache.go b/plugin/indexer/cache.go index 569c6708..e9488656 100644 --- a/plugin/indexer/cache.go +++ b/plugin/indexer/cache.go @@ -15,7 +15,8 @@ import ( // the run gives each plugin its stored artifact in place of a tree. The // run keys an artifact by the fileindex.Key of the file and the Name and // the Version of the plugin, so a new Version makes every older artifact -// miss. +// miss. The run stores no artifact that the plugin returns after ctx is +// done, because the end of ctx can have cut it. type CachedPlugin interface { core.Plugin diff --git a/plugin/indexer/cache_test.go b/plugin/indexer/cache_test.go index b62004f3..1598f915 100644 --- a/plugin/indexer/cache_test.go +++ b/plugin/indexer/cache_test.go @@ -4,13 +4,18 @@ import ( "bytes" "context" "errors" + "fmt" + "maps" + "slices" "strconv" "strings" "testing" "github.com/safedep/code/core" "github.com/safedep/code/fileindex" + "github.com/safedep/code/fileindex/store" "github.com/safedep/code/fileindex/store/memstore" + "github.com/safedep/code/langcode" "github.com/safedep/code/parser" "github.com/safedep/code/plugin/callgraph" "github.com/stretchr/testify/assert" @@ -210,3 +215,163 @@ func TestAnalyzeKeepsTheCallGraphOnlyForAGraphPlugin(t *testing.T) { assert.Equal(t, keep, o.graph != nil, "a run with no GraphPlugin drops the graph after the index") } } + +// storeContents returns the index that the store holds for each file, and +// the artifact of the plugin for each file. The files are JavaScript. +func storeContents(t *testing.T, s store.Store, files map[string]string, pl CachedPlugin) (map[string]*fileindex.Index, map[string]string) { + t.Helper() + var keys []fileindex.Key + var artifactKeys []store.ArtifactKey + for name, content := range files { + key := fileindex.NewKey(name, fileindex.HashContent([]byte(content)), langcode.Javascript) + keys = append(keys, key) + artifactKeys = append(artifactKeys, store.ArtifactKey{Key: key, Plugin: pl.Name(), Version: pl.Version()}) + } + stored, err := s.Get(context.Background(), keys) + require.NoError(t, err) + indexes := map[string]*fileindex.Index{} + for key, ix := range stored { + indexes[key.Path] = ix + } + data, err := s.GetArtifacts(context.Background(), artifactKeys) + require.NoError(t, err) + artifacts := map[string]string{} + for key, d := range data { + artifacts[key.Path] = string(d) + } + return indexes, artifacts +} + +// finished returns the files that a run indexed, by name. +func finished(r *Result) map[string]*fileindex.Index { + out := map[string]*fileindex.Index{} + for _, ix := range r.Indexes { + if r.Files[ix.Path] == fileindex.FileIndexed { + out[ix.Path] = ix + } + } + return out +} + +// cuttingPlugin is a countingPlugin that ends the run when it analyzes the +// file at. After the end of ctx it returns a partial artifact with no +// error, as a plugin that stops at the deadline can. +type cuttingPlugin struct { + *countingPlugin + at string + cancel context.CancelFunc +} + +func (p *cuttingPlugin) Analyze(ctx context.Context, file core.File, tree core.ParseTree) ([]byte, error) { + if file.Name() == p.at { + p.cancel() + } + if ctx.Err() != nil { + return []byte("partial"), nil + } + return p.countingPlugin.Analyze(ctx, file, tree) +} + +func TestIndexerStoresTheFinishedFilesOfACutBatch(t *testing.T) { + files := map[string]string{} + var names []string + for i := range 10 { + name := fmt.Sprintf("f%d.js", i) + files[name] = fmt.Sprintf("function f%d() {}\n", i) + names = append(names, name) + } + dir := t.TempDir() + writeFiles(t, dir, files) + fsys := appFileSystem(t, dir) + langs := languages(t, core.LanguageCodeJavascript) + + for _, workers := range []int{1, 4} { + t.Run(fmt.Sprintf("workers=%d", workers), func(t *testing.T) { + s := memstore.New() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + pl := &cuttingPlugin{countingPlugin: newCountingPlugin("1"), at: "f4.js", cancel: cancel} + cut, err := runIndexer(t, ctx, fsys, Options{Languages: langs, Store: s, Plugins: []core.Plugin{pl}, Workers: workers}) + require.ErrorIs(t, err, context.Canceled) + + done := finished(cut) + var rest []string + for _, name := range names { + if _, ok := done[name]; !ok { + assert.Equal(t, fileindex.FileMissing, cut.Files[name], name) + rest = append(rest, name) + } + } + assert.Subset(t, slices.Collect(maps.Keys(done)), names[:5], "the files up to the cut finish") + + indexes, artifacts := storeContents(t, s, files, pl) + assert.Equal(t, done, indexes, "the store holds the index of each finished file and of no other file") + assert.Equal(t, map[string]string{"f0.js": "1", "f1.js": "1", "f2.js": "1", "f3.js": "1"}, artifacts, + "the store holds no artifact that the plugin made after the cut") + + next, err := runIndexer(t, context.Background(), fsys, Options{Languages: langs, Store: s, Workers: workers}) + require.NoError(t, err) + assert.Equal(t, rest, next.Report.Visited, "the next run parses only the files that the cut stopped") + for name := range done { + assert.Equal(t, fileindex.FileCached, next.Files[name], name) + } + }) + } +} + +// graphKeepingPlugin is a GraphPlugin that keeps the number of lines of a +// file, or "partial" for a partial call graph, as a plugin that does not +// check the graph can. +type graphKeepingPlugin struct { + *countingPlugin +} + +func (p *graphKeepingPlugin) AnalyzeGraph(_ context.Context, _ core.File, tree core.ParseTree, cg *callgraph.CallGraph) ([]byte, error) { + for _, t := range cg.Truncations() { + if t.Kind == callgraph.TruncationDeadline { + return []byte("partial"), nil + } + } + return lineCount(tree) +} + +func TestIndexerStoresNoPartialIndexOrGraphArtifact(t *testing.T) { + var big strings.Builder + for i := range 20000 { + _, err := fmt.Fprintf(&big, "f%d();\n", i) + require.NoError(t, err) + } + files := map[string]string{ + "a.js": "function a() {}\n", + "b.js": "function b() {}\n", + // The deadline cuts the build of the call graph of this file. + "c.js": big.String(), + "d.js": "function d() {}\n", + } + dir := t.TempDir() + writeFiles(t, dir, files) + fsys := appFileSystem(t, dir) + + for _, workers := range []int{1, 4} { + t.Run(fmt.Sprintf("workers=%d", workers), func(t *testing.T) { + s := memstore.New() + pl := &graphKeepingPlugin{countingPlugin: newCountingPlugin("1")} + ctx := &cutContext{Context: context.Background(), at: []string{callGraphBuild}} + result, err := runIndexer(t, ctx, fsys, Options{Languages: languages(t, core.LanguageCodeJavascript), Store: s, Plugins: []core.Plugin{pl}, Workers: workers}) + require.ErrorIs(t, err, context.Canceled) + assert.Equal(t, fileindex.FileMissing, result.Files["c.js"]) + + indexes, artifacts := storeContents(t, s, files, pl) + assert.Equal(t, finished(result), indexes, "the store holds the index of each finished file and of no other file") + assert.NotContains(t, indexes, "c.js") + for name, data := range artifacts { + assert.Equal(t, "1", data, name) + assert.Contains(t, indexes, name, "an artifact comes with the index of its file") + } + if workers == 1 { + assert.Contains(t, indexes, "a.js", "one worker finishes the files before the cut") + assert.Contains(t, indexes, "b.js", "one worker finishes the files before the cut") + } + }) + } +} diff --git a/plugin/indexer/gounit.go b/plugin/indexer/gounit.go index 562b80a6..28fc0dc7 100644 --- a/plugin/indexer/gounit.go +++ b/plugin/indexer/gounit.go @@ -173,6 +173,9 @@ func skeletonIndex(ctx context.Context, p contentParser, file pending, tree core if err != nil { return nil, err } + if err := complete(ctx, out); err != nil { + return nil, err + } out.ContentHash = file.key.ContentHash return out, nil } diff --git a/plugin/indexer/gounit_test.go b/plugin/indexer/gounit_test.go index 24551c7e..378fd076 100644 --- a/plugin/indexer/gounit_test.go +++ b/plugin/indexer/gounit_test.go @@ -2,6 +2,7 @@ package indexer import ( "context" + "os" "path/filepath" "regexp" "strings" @@ -11,6 +12,7 @@ import ( "github.com/safedep/code/fileindex" "github.com/safedep/code/fileindex/store/memstore" "github.com/safedep/code/fs" + "github.com/safedep/code/langcode" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -98,6 +100,39 @@ func TestGoSkeletonKeepsTheHashOfTheFile(t *testing.T) { assert.Equal(t, fileindex.FileCached, result.Dependencies[goKitUnit+"//tables.go"], "the store serves the skeleton index on the next run") } +func TestIndexerStoresNoPartialSkeletonIndex(t *testing.T) { + dir := t.TempDir() + kit := filepath.Join(dir, "kit") + writeFiles(t, kit, goKitFiles()) + fsys, err := fs.NewLocalFileSystem(fs.LocalFileSystemConfig{AppDirectories: []string{dir}, ImportDirectories: []string{kit}, ImportUnits: map[string]string{kit: goKitUnit}, ExcludePatterns: []*regexp.Regexp{fs.SkipPattern(kit)}}) + require.NoError(t, err) + s := memstore.New() + // The deadline cuts the walk of the skeleton of tables.go, after the + // skeleton parsed. + ctx := &cutContext{Context: context.Background(), at: []string{"indexer.skeletonIndex", "indexer.(*projection).walkTree"}} + result, err := runIndexer(t, ctx, fsys, Options{Languages: languages(t, core.LanguageCodeGo), Store: s, Imports: true, Workers: 1}) + require.ErrorIs(t, err, context.Canceled) + + tables := goKitUnit + "//tables.go" + assert.Equal(t, fileindex.FileMissing, result.Dependencies[tables]) + var stored []string + for _, ix := range result.Indexes { + assert.NotEqual(t, tables, ix.Path) + got, err := s.Get(context.Background(), []fileindex.Key{ix.Key()}) + require.NoError(t, err) + if _, ok := got[ix.Key()]; ok { + stored = append(stored, ix.Path) + } + } + assert.ElementsMatch(t, []string{goKitUnit + "//kit.go", goKitUnit + "//kit_linux.go", goKitUnit + "//kit_windows.go"}, stored, "the files before the cut reach the store") + + data, err := os.ReadFile(filepath.Join(kit, "tables.go")) + require.NoError(t, err) + got, err := s.Get(context.Background(), []fileindex.Key{fileindex.NewKey(tables, fileindex.HashContent(data), langcode.Go)}) + require.NoError(t, err) + assert.Empty(t, got, "the partial skeleton index does not reach the store") +} + func TestIsGeneratedTable(t *testing.T) { for _, tt := range []struct { name string diff --git a/plugin/indexer/indexer.go b/plugin/indexer/indexer.go index 45e49c67..202132e1 100644 --- a/plugin/indexer/indexer.go +++ b/plugin/indexer/indexer.go @@ -194,8 +194,10 @@ type run struct { // that the store serves come first, in path order, and then the parsed // files, in path order. // -// When ctx is done, Run stops, marks each file that it did not index as -// missing, and returns the result so far with the error of ctx. Run fails +// When ctx is done, Run stops and puts the indexes and the artifacts of the +// files that it finished in the store. It marks each file that it did not +// index as missing, and returns the result so far with the error of ctx. +// A file that ctx cut keeps no index and no artifact. Run fails // when two files of fsys have one name, as the same relative path under // two app roots, because the index keys a file by its name. func (ix *Indexer) Run(ctx context.Context, fsys core.ImportAwareFileSystem) (*Result, error) { @@ -325,7 +327,7 @@ func read(file core.File, limit int) ([]byte, error) { } // flush serves the files of a batch from the store, parses the rest and -// puts their indexes. +// puts their indexes and artifacts. func (r *run) flush(ctx context.Context, batch []pending) error { if len(batch) == 0 { return nil @@ -352,20 +354,28 @@ func (r *run) flush(ctx context.Context, batch []pending) error { parse = append(parse, p) } - put, made, err := r.parse(ctx, parse) - if err != nil { + 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 } + return parseErr +} + +// 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 { return nil } - if len(put) > 0 { - if err := r.opts.Store.Put(ctx, put); err != nil { + if len(indexes) > 0 { + if err := r.opts.Store.Put(ctx, indexes); err != nil { return fmt.Errorf("failed to store file indexes: %w", err) } } - if len(made) > 0 { - if err := r.opts.Store.PutArtifacts(ctx, made); err != nil { + if len(artifacts) > 0 { + if err := r.opts.Store.PutArtifacts(ctx, artifacts); err != nil { return fmt.Errorf("failed to store plugin artifacts: %w", err) } } @@ -420,7 +430,8 @@ func (r *run) parse(ctx context.Context, files []pending) ([]*fileindex.Index, [ // collect records the outcome of one file, runs the extra plugins on its // tree and closes the tree. It returns the index of the file, or nil when -// the file failed, and the artifacts of the cached plugins. +// the file failed, and the artifacts of the cached plugins that finished +// before ctx was done. func (r *run) collect(ctx context.Context, p pending, o outcome) (*fileindex.Index, []store.Artifact) { if o.err != nil { r.fail(ctx, p.file, core.StageParse, o.err) @@ -447,9 +458,13 @@ func (r *run) collect(ctx context.Context, p pending, o outcome) (*fileindex.Ind continue } data, err := analyzeCached(ctx, cached, p.file, o) - if err != nil { + switch { + case err != nil: report.Failed = append(report.Failed, failure(p.file, pl, err)) - } else { + case ctx.Err() == nil: + // A plugin can stop at the end of ctx and return a partial + // artifact with no error. A live ctx after the return proves + // that the plugin saw no end of ctx. artifacts = append(artifacts, store.Artifact{Key: artifactKey(p, cached), Data: data}) } continue diff --git a/plugin/indexer/plugin.go b/plugin/indexer/plugin.go index 50792ae8..1388abef 100644 --- a/plugin/indexer/plugin.go +++ b/plugin/indexer/plugin.go @@ -50,7 +50,12 @@ func (p *fileIndexPlugin) AnalyzeTree(ctx context.Context, tree core.ParseTree) if err := p.callback(ctx, ix); err != nil { return err } + return complete(ctx, ix) +} +// complete fails on an index that the deadline cut, so the run does not +// store it as whole. +func complete(ctx context.Context, ix *fileindex.Index) error { if partial(ix) { return fmt.Errorf("index of %s is partial: %w", ix.Path, ctx.Err()) } diff --git a/plugin/indexer/plugin_test.go b/plugin/indexer/plugin_test.go index 14cdb360..1d9fc57b 100644 --- a/plugin/indexer/plugin_test.go +++ b/plugin/indexer/plugin_test.go @@ -9,8 +9,10 @@ import ( "os" "path/filepath" "runtime" + "slices" "strings" "sync" + "sync/atomic" "testing" "github.com/safedep/code/core" @@ -186,7 +188,7 @@ func TestFileIndexPartialAtTheDeadline(t *testing.T) { } tree := parseSource(t, core.LanguageCodeJavascript, "slow.js", source.String()) - ctx := &buildCutContext{Context: context.Background()} + ctx := &cutContext{Context: context.Background(), at: []string{callGraphBuild}} var got *fileindex.Index err := NewFileIndexPlugin(func(_ context.Context, ix *fileindex.Index) error { @@ -198,37 +200,48 @@ func TestFileIndexPartialAtTheDeadline(t *testing.T) { assert.True(t, partial(got)) } -// buildCutContext is done from the first check inside the build of the -// call graph on. The import queries before the build see it live, so the -// build starts and the deadline cuts it. It reads the stack, so it does not -// depend on the number of checks before the build. -type buildCutContext struct { +// callGraphBuild is the function that builds a call graph. +const callGraphBuild = "callgraph.(*CallGraph).build" + +// cutContext is done from the first check of it that runs inside every +// function of at. The checks before see it live, so the work starts and +// the deadline cuts it. It reads the stack, so it does not depend on the number of checks +// before the cut. The workers of a run check it at once, so the cut is +// atomic. +type cutContext struct { context.Context - cut bool + at []string + cut atomic.Bool } -func (c *buildCutContext) Err() error { - if !c.cut { - c.cut = insideBuild() +func (c *cutContext) Err() error { + if !c.cut.Load() && inside(c.at) { + c.cut.Store(true) } - if c.cut { + if c.cut.Load() { return context.Canceled } return nil } -func insideBuild() bool { +// inside reports whether the stack of the caller runs each function of fns. +func inside(fns []string) bool { pcs := make([]uintptr, 64) frames := runtime.CallersFrames(pcs[:runtime.Callers(2, pcs)]) + var stack []string for { frame, more := frames.Next() - if strings.HasSuffix(frame.Function, "callgraph.(*CallGraph).build") { - return true - } + stack = append(stack, frame.Function) if !more { + break + } + } + for _, fn := range fns { + if !slices.ContainsFunc(stack, func(f string) bool { return strings.HasSuffix(f, fn) }) { return false } } + return true } func parseSource(t *testing.T, code core.LanguageCode, name, source string) core.ParseTree { From c42c228f709dc253257a520cfea7efded406fbb0 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 08:57:12 +0000 Subject: [PATCH 03/58] test(projectgraph): link the Go standard library in the method measure (FOU-561) The method measure gave the Go modules of each project as units, but not the standard library. So a receiver of a standard library type had no known type, and a call on it was an unknown-receiver record. vet passes GOROOT as a unit, so the measure did not show what vet gives. scripts/fetch_method_corpus.sh now reads the go line of the go.mod of each Go project, fetches the src tree of that Go release from go.dev, and records the release in gostd/releases.txt. The measure links the tree as the unit go:std@. PROJECTGRAPH_METHOD_NO_STD=1 leaves the unit out, so a run shows its effect. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- scripts/fetch_method_corpus.sh | 50 ++++++++++++++++++++++++++++--- test/projectgraph/README.md | 9 ++++++ test/projectgraph/methods_test.go | 38 ++++++++++++++++++++++- 3 files changed, 92 insertions(+), 5 deletions(-) diff --git a/scripts/fetch_method_corpus.sh b/scripts/fetch_method_corpus.sh index ff722436..7d9c7136 100755 --- a/scripts/fetch_method_corpus.sh +++ b/scripts/fetch_method_corpus.sh @@ -2,7 +2,10 @@ # Fetch the projects and the dependencies of the method measure into the # directory of the first argument. Go modules come from the module proxy # into /gomod, crates from crates.io into /crates, and PyPI -# packages from pypi.org into /pypi/-. A dependency +# packages from pypi.org into /pypi/-. Each Go project +# also gets the src tree of the Go release of the go line of its go.mod +# into /gostd//src. The file +# /gostd/releases.txt gives the release of each Go project. A dependency # comes as a wheel, unpacked, so its directory has the layout of # site-packages with the .dist-info of the package. A project comes as its # sdist, so its directory has the manifests. The list is @@ -11,9 +14,16 @@ set -euo pipefail dir="${1:?usage: fetch_method_corpus.sh }" -list="$(dirname "$0")/../test/projectgraph/public/methods/corpus.txt" -mkdir -p "$dir/gomod" "$dir/crates" "$dir/pypi" +list="$(cd "$(dirname "$0")/.." && pwd)/test/projectgraph/public/methods/corpus.txt" +mkdir -p "$dir/gomod" "$dir/crates" "$dir/pypi" "$dir/gostd" dir="$(cd "$dir" && pwd)" +: >"$dir/gostd/releases.txt" + +# The go commands fill the module cache of the corpus. They run outside of +# any module, with the local toolchain and no workspace, so no go.mod or +# go.work of the caller changes what they fetch. +cd "$dir" +export GOMODCACHE="$dir/gomod" GOFLAGS=-modcacherw GOTOOLCHAIN=local GOWORK=off # pypi_url prints the download URL of a package version: the sdist for # the project, and for a dependency a pure wheel, else a manylinux x86_64 @@ -67,6 +77,35 @@ pypi_fetch() { rm -rf "$tmp" } +# go_release prints the Go release of the go line of a go.mod, as go1.16 +# for go 1.16 and go1.21.0 for go 1.21. The go command reads a go.mod with +# no go line as go 1.16. +go_release() { + local version minor + version="$(awk '$1 == "go" { print $2; exit }' "$1")" + version="${version:-1.16}" + minor="${version#1.}" + if [ "$minor" = "${minor%%.*}" ] && [ "$minor" -ge 21 ]; then + version="$version.0" + fi + echo "go$version" +} + +# go_project fetches the src tree of the Go release of a Go project, and +# records the release. +go_project() { + local project="$1" src release tmp + src="$(go mod download -json "$project" | jq -r .Dir)" + release="$(go_release "$src/go.mod")" + echo "$project $release" >>"$dir/gostd/releases.txt" + if [ ! -d "$dir/gostd/$release" ]; then + tmp="$(mktemp -d)" + curl -sSfL "https://go.dev/dl/$release.src.tar.gz" | tar -xz -C "$tmp" go/src + mv "$tmp/go" "$dir/gostd/$release" + rm -rf "$tmp" + fi +} + grep -v '^#' "$list" | while read -r language _ project deps; do if [ "$language" = python ]; then pypi_fetch "$project" project @@ -75,10 +114,13 @@ grep -v '^#' "$list" | while read -r language _ project deps; do done continue fi + if [ "$language" = go ]; then + go_project "$project" + fi for item in $project $deps; do case "$language" in go) - GOFLAGS=-modcacherw GOMODCACHE="$dir/gomod" go mod download "$item" + go mod download "$item" ;; rust) name="${item%@*}" diff --git a/test/projectgraph/README.md b/test/projectgraph/README.md index 2b7a473e..5701f035 100644 --- a/test/projectgraph/README.md +++ b/test/projectgraph/README.md @@ -148,6 +148,15 @@ crates.io, and a PyPI package from pypi.org: a dependency as its wheel, unpacked, so its directory has the layout of site-packages, and a project as its sdist, so its directory has the manifests. +The script also fetches the `src` tree of the Go release of each Go +project from go.dev into `gostd//src`, and writes the release of +each project to `gostd/releases.txt`. The release comes from the `go` line +of the go.mod of the project, as `go1.16` for `go 1.16`. The measure links +the tree as the unit `go:std@`, as vet links GOROOT. Set +`PROJECTGRAPH_METHOD_NO_STD=1` to leave the unit out and see what it +changes. + + `receivers_test.go` holds one row per form of the receiver of a method call, for Go, for Rust and for Python, against the small dependency units under `testdata/receivers`. It runs in `go test ./...`. A row that the link step diff --git a/test/projectgraph/methods_test.go b/test/projectgraph/methods_test.go index 4163f7ff..301a7474 100644 --- a/test/projectgraph/methods_test.go +++ b/test/projectgraph/methods_test.go @@ -24,6 +24,7 @@ import ( "github.com/safedep/code/langcode" "github.com/safedep/code/plugin/indexer" "github.com/safedep/code/projectgraph" + "github.com/safedep/code/projectgraph/modules" "github.com/safedep/code/projectgraph/reach" "github.com/stretchr/testify/require" "golang.org/x/mod/module" @@ -193,14 +194,38 @@ func ecosystemOf(language string) (string, langcode.Code) { return "cargo", langcode.Rust } +// methodNoStdEnv names a switch that leaves out the unit of the Go +// standard library, so a run shows what the unit changes. +const methodNoStdEnv = "PROJECTGRAPH_METHOD_NO_STD" + +// goReleases returns the Go release of each Go project of the corpus, as +// scripts/fetch_method_corpus.sh records it from the go line of the +// go.mod of the project. +func goReleases(t *testing.T, corpus string) map[string]string { + data, err := os.ReadFile(filepath.Join(corpus, "gostd", "releases.txt")) + require.NoError(t, err, "run scripts/fetch_method_corpus.sh again to fetch the Go standard library") + releases := map[string]string{} + for _, line := range strings.Split(strings.TrimSpace(string(data)), "\n") { + project, release, ok := strings.Cut(line, " ") + require.True(t, ok, line) + releases[project] = release + } + return releases +} + // methodDir returns the directory of a project or a dependency in the // corpus, and its unit. A PyPI dependency is an unpacked wheel, so its // directory has the layout of site-packages, and the unit is rooted there. +// The Go standard library is the dependency std@, as std@go1.16, +// and its directory is the src tree of the release. func methodDir(t *testing.T, corpus, language, item string) (string, string) { name, version, _ := strings.Cut(item, "@") eco, _ := ecosystemOf(language) switch eco { case "go": + if name == modules.StdModule { + return filepath.Join(corpus, "gostd", version, "src"), "go:" + item + } escaped, err := module.EscapePath(name) require.NoError(t, err) return filepath.Join(corpus, "gomod", filepath.FromSlash(escaped)+"@"+version), "go:" + item @@ -276,6 +301,11 @@ func TestMethodTargets(t *testing.T) { }) } projects, advisories := readMethodCorpus(t) + measureGo := slices.ContainsFunc(languages, func(l core.Language) bool { return l.Meta().Code == langcode.Go }) + var releases map[string]string + if measureGo && os.Getenv(methodNoStdEnv) == "" { + releases = goReleases(t, corpus) + } counts := methodCounts{} var lines, details []string @@ -284,7 +314,13 @@ func TestMethodTargets(t *testing.T) { cfg := codefs.LocalFileSystemConfig{AppDirectories: []string{dir}, ImportUnits: map[string]string{}, ExcludePatterns: codefs.DefaultSkipPatterns(dir)} units := map[string]iofs.FS{} var targets []methodTarget - for _, dep := range p.deps { + deps := p.deps + if p.language == "go" && releases != nil { + release, ok := releases[p.source] + require.True(t, ok, "gostd/releases.txt has no Go release of %s", p.source) + deps = append(slices.Clone(deps), modules.StdModule+"@"+release) + } + for _, dep := range deps { depDir, unit := methodDir(t, corpus, p.language, dep) cfg.ImportDirectories = append(cfg.ImportDirectories, depDir) cfg.ImportUnits[depDir] = unit From 32ad6c46249b99f878c696305252d7a937944509 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 08:57:26 +0000 Subject: [PATCH 04/58] test(projectgraph): check the Go answers of the method measure against govulncheck (FOU-563) The method measure counted the answers of reach, but nothing checked that a not-found answer is true. A false not-found hides a real vulnerability. With govulncheck v1.9.0 on the PATH, the measure now writes the Go advisories of advisories.json as a local vulnerability database, and runs govulncheck -mode source -json on a copy of each Go project with GOPROXY=off. The fetch script fills the module cache of the corpus with the build list of each Go project, so the run needs no network. The measure compares the answer of each target with the symbols that govulncheck reaches. A target that govulncheck reaches and that reach gives as not-found or no-such-target is a false negative, and the test fails. A target that reach finds and govulncheck does not is a review row. A listed symbol that govulncheck reaches and that is no target is an unmatched row, so a key that the two tools write in two ways cannot hide a false negative. TestVulnAgreement checks the comparison on fixed answers, and the CI job of the public sets runs it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- .github/workflows/ci.yml | 2 +- scripts/fetch_method_corpus.sh | 16 +- test/projectgraph/README.md | 27 ++ test/projectgraph/methods_govulncheck_test.go | 290 ++++++++++++++++++ test/projectgraph/methods_test.go | 21 +- 5 files changed, 348 insertions(+), 8 deletions(-) create mode 100644 test/projectgraph/methods_govulncheck_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eeab87c0..befd8781 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -80,7 +80,7 @@ jobs: env: PROJECTGRAPH_PUBLIC_REPORT: ${{ runner.temp }}/public.txt PROJECTGRAPH_GOVULNCHECK_REPORT: ${{ runner.temp }}/govulncheck.txt - run: go test -tags projectgraph -run 'TestPublicSets|TestGovulncheckTraces' -count=1 ./test/projectgraph/ + run: go test -tags projectgraph -run 'TestPublicSets|TestGovulncheckTraces|TestVulnAgreement' -count=1 ./test/projectgraph/ - name: Check the gates run: bash scripts/check_projectgraph_gates.sh "$RUNNER_TEMP/public.txt" "$RUNNER_TEMP/govulncheck.txt" pycg-micro=0.95 diff --git a/scripts/fetch_method_corpus.sh b/scripts/fetch_method_corpus.sh index 7d9c7136..688f3439 100755 --- a/scripts/fetch_method_corpus.sh +++ b/scripts/fetch_method_corpus.sh @@ -3,8 +3,9 @@ # directory of the first argument. Go modules come from the module proxy # into /gomod, crates from crates.io into /crates, and PyPI # packages from pypi.org into /pypi/-. Each Go project -# also gets the src tree of the Go release of the go line of its go.mod -# into /gostd//src. The file +# also gets the modules of its build list into /gomod, so govulncheck +# can load it with no network, and the src tree of the Go release of the +# go line of its go.mod into /gostd//src. The file # /gostd/releases.txt gives the release of each Go project. A dependency # comes as a wheel, unpacked, so its directory has the layout of # site-packages with the .dist-info of the package. A project comes as its @@ -91,8 +92,8 @@ go_release() { echo "go$version" } -# go_project fetches the src tree of the Go release of a Go project, and -# records the release. +# go_project fetches the build list of a Go project and the src tree of its +# Go release, and records the release. go_project() { local project="$1" src release tmp src="$(go mod download -json "$project" | jq -r .Dir)" @@ -104,6 +105,13 @@ go_project() { mv "$tmp/go" "$dir/gostd/$release" rm -rf "$tmp" fi + # The module cache holds the project read-only, and the go command can + # add missing go.sum lines, so it runs on a copy. + tmp="$(mktemp -d)" + cp -R "$src/." "$tmp" + chmod -R u+w "$tmp" + (cd "$tmp" && GOFLAGS="$GOFLAGS -mod=mod" go list -deps ./... >/dev/null && GOFLAGS="$GOFLAGS -mod=mod" go list -m all >/dev/null) + rm -rf "$tmp" } grep -v '^#' "$list" | while read -r language _ project deps; do diff --git a/test/projectgraph/README.md b/test/projectgraph/README.md index 5701f035..43875343 100644 --- a/test/projectgraph/README.md +++ b/test/projectgraph/README.md @@ -156,6 +156,33 @@ the tree as the unit `go:std@`, as vet links GOROOT. Set `PROJECTGRAPH_METHOD_NO_STD=1` to leave the unit out and see what it changes. +With govulncheck on the `PATH`, the measure also compares each answer of a +Go target with govulncheck. The script fetches the modules of the build +list of each Go project into the module cache of the corpus. The measure +writes the Go advisories of `advisories.json` as a vulnerability database +in the layout of `vuln.go.dev`, and runs +`govulncheck -mode source -json -db file:// ./...` on a copy of each +project, with `GOPROXY=off`. So the run needs no network, and govulncheck +checks exactly the advisories of the measure. Install the version that +`methods_govulncheck_test.go` pins: + +```bash +go install golang.org/x/vuln/cmd/govulncheck@v1.9.0 +export PATH="$PATH:$(go env GOPATH)/bin" +``` + +The block `set=govulncheck-agreement` counts the targets that govulncheck +reaches by the answer of reach (`found`, `uncertain`, or `false-negative` +for `not-found` and `no-such-target`), and the same for the targets that +govulncheck does not reach. A false negative fails the test. A `review` +row is a target that reach finds and govulncheck does not. It is not a +failure, because reach also starts from the library surface of the +project. An `unmatched` row is a symbol of an advisory with a symbol list +that govulncheck reaches and that is no target of the project: the corpus +does not list the module of the symbol, or the two tools write its key in +two ways. An advisory with no symbol list gives no target, so the +comparison leaves out its findings. `TestVulnAgreement` checks the +comparison on fixed answers and runs with the tag and no corpus. `receivers_test.go` holds one row per form of the receiver of a method call, for Go, for Rust and for Python, against the small dependency units under diff --git a/test/projectgraph/methods_govulncheck_test.go b/test/projectgraph/methods_govulncheck_test.go new file mode 100644 index 00000000..92cedccf --- /dev/null +++ b/test/projectgraph/methods_govulncheck_test.go @@ -0,0 +1,290 @@ +//go:build projectgraph + +package projectgraph + +import ( + "bytes" + "encoding/json" + "errors" + "io" + "maps" + "os" + "os/exec" + "path/filepath" + "slices" + "strconv" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// govulncheckVersion is the version of govulncheck that the method measure +// compares with. Install it with +// go install golang.org/x/vuln/cmd/govulncheck@v1.9.0. +const govulncheckVersion = "v1.9.0" + +// vulnCheck runs govulncheck on each Go project of the method measure and +// compares its findings with the answers of reach. A zero vulnCheck +// compares nothing. +type vulnCheck struct { + bin, db, gomod string + // listed holds the import paths of each advisory that list symbols. + // govulncheck reports any called symbol of a module or a package that + // an advisory names with no symbol list, and the measure has no target + // for such an advisory. + listed map[string]map[string]bool + agreement vulnAgreement +} + +// newVulnCheck finds govulncheck on the PATH and writes a vulnerability +// database of the Go advisories of the measure, so govulncheck checks +// exactly these advisories and needs no network. +func newVulnCheck(t *testing.T, corpus string, advisories map[string][]advisory, measureGo bool) *vulnCheck { + if !measureGo { + return &vulnCheck{} + } + bin, err := exec.LookPath("govulncheck") + if err != nil { + t.Logf("govulncheck is not on the PATH, so the measure does not compare with it: install govulncheck@%s", govulncheckVersion) + return &vulnCheck{} + } + db := t.TempDir() + writeVulnDB(t, db, advisories) + listed := map[string]map[string]bool{} + for _, list := range advisories { + for _, a := range list { + listed[a.ID] = map[string]bool{} + for _, imp := range a.Imports { + listed[a.ID][imp.Path] = len(imp.Symbols) > 0 + } + } + } + return &vulnCheck{bin: bin, db: db, gomod: filepath.Join(corpus, "gomod"), listed: listed} +} + +// compare runs govulncheck on the project in dir and adds the answers of +// reach for the project to the agreement. +func (v *vulnCheck) compare(t *testing.T, project, dir string, answers []methodAnswer) { + if v.bin == "" { + return + } + v.agreement.add(project, answers, v.reached(t, dir)) +} + +func (v *vulnCheck) rows() []string { + if v.bin == "" { + return nil + } + return v.agreement.rows() +} + +// reached returns the keys of the vulnerable symbols that govulncheck +// finds a call stack to in the project in dir. The module cache of the +// corpus holds the build list of the project, so the go command runs with +// no proxy. It may add go.sum lines, so it runs on a copy of the project. +func (v *vulnCheck) reached(t *testing.T, dir string) map[string]bool { + work := t.TempDir() + require.NoError(t, os.CopyFS(work, os.DirFS(dir))) + cmd := exec.Command(v.bin, "-mode", "source", "-json", "-db", "file://"+v.db, "./...") + cmd.Dir = work + cmd.Env = append(os.Environ(), "GOPROXY=off", "GOSUMDB=off", "GOFLAGS=-mod=mod", "GOMODCACHE="+v.gomod, "GOTOOLCHAIN=local", "GOWORK=off") + var stderr bytes.Buffer + cmd.Stderr = &stderr + out, err := cmd.Output() + require.NoError(t, err, stderr.String()) + return v.reachedIn(t, out) +} + +// reachedIn returns the keys of the vulnerable symbols that the findings +// of a govulncheck -json output reach with a call stack, for the advisories +// that list symbols. It fails when the output comes from another version +// of govulncheck, because the call graph of another version can reach +// other symbols. +func (v *vulnCheck) reachedIn(t *testing.T, out []byte) map[string]bool { + reached := map[string]bool{} + dec := json.NewDecoder(bytes.NewReader(out)) + for { + var m struct { + vulnMessage + Config *struct { + ScannerVersion string `json:"scanner_version"` + } `json:"config"` + } + err := dec.Decode(&m) + if errors.Is(err, io.EOF) { + return reached + } + require.NoError(t, err) + if m.Config != nil { + require.Equal(t, govulncheckVersion, m.Config.ScannerVersion, "install govulncheck@%s", govulncheckVersion) + } + if f := m.Finding; f != nil && len(f.Trace) > 0 && f.Trace[0].Function != "" && v.listed[f.OSV][f.Trace[0].Package] { + reached[frameKey(f.Trace[0])] = true + } + } +} + +// writeVulnDB writes the Go advisories of the measure as a vulnerability +// database in the layout of vuln.go.dev, which govulncheck -db file:// +// reads: index/db.json, index/modules.json and ID/.json. +func writeVulnDB(t *testing.T, dir string, advisories map[string][]advisory) { + modified := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC) + type vuln struct { + ID string `json:"id"` + Modified time.Time `json:"modified"` + } + type module struct { + Path string `json:"path"` + Vulns []vuln `json:"vulns"` + } + var index []module + entries := map[string][]any{} + for _, path := range slices.Sorted(maps.Keys(advisories)) { + m := module{Path: path} + for _, a := range advisories[path] { + m.Vulns = append(m.Vulns, vuln{ID: a.ID, Modified: modified}) + entries[a.ID] = append(entries[a.ID], map[string]any{ + "package": map[string]string{"name": path, "ecosystem": "Go"}, + "ranges": []any{map[string]any{"type": "SEMVER", "events": a.Ranges}}, + "ecosystem_specific": map[string]any{"imports": a.Imports}, + }) + } + index = append(index, m) + } + writeJSON(t, filepath.Join(dir, "index", "db.json"), map[string]time.Time{"modified": modified}) + writeJSON(t, filepath.Join(dir, "index", "modules.json"), index) + for id, affected := range entries { + writeJSON(t, filepath.Join(dir, "ID", id+".json"), map[string]any{ + "schema_version": "1.3.1", + "id": id, + "modified": modified, + "details": "", + "affected": affected, + }) + } +} + +func writeJSON(t *testing.T, file string, v any) { + data, err := json.Marshal(v) + require.NoError(t, err) + require.NoError(t, os.MkdirAll(filepath.Dir(file), 0o755)) + require.NoError(t, os.WriteFile(file, data, 0o644)) +} + +// The classes of a reach answer for the comparison with govulncheck. +const ( + answerFound = "found" + answerMissing = "not-found" + answerUncertain = "uncertain" +) + +// answerClass returns the class of an outcome of the measure. found/low +// is found, and no-such-target says that no path exists, as not-found does. +func answerClass(outcome string) string { + switch status, _, _ := strings.Cut(outcome, "/"); status { + case "found": + return answerFound + case "not-found", "no-such-target": + return answerMissing + } + return answerUncertain +} + +// vulnAgreement counts the answers of reach for the targets that +// govulncheck reaches and for the targets that it does not reach, by the +// class of the answer. A missing answer for a target that govulncheck +// reaches is a false negative. A found answer for a target that +// govulncheck does not reach is a row to review, because reach also starts +// from the library surface of the project. A symbol that govulncheck +// reaches and that is no target of the project shows a key that the two +// tools write in two ways, which would hide a false negative. +type vulnAgreement struct { + reached, quiet map[string]int + falseNegatives, review, unmatched []string +} + +func (a *vulnAgreement) add(project string, answers []methodAnswer, reached map[string]bool) { + if a.reached == nil { + a.reached, a.quiet = map[string]int{}, map[string]int{} + } + targets := map[string]bool{} + for _, ans := range answers { + targets[ans.key] = true + class := answerClass(ans.outcome) + switch { + case !reached[ans.key]: + a.quiet[class]++ + if class == answerFound { + a.review = append(a.review, ans.detail) + } + case class == answerMissing: + a.reached[class]++ + a.falseNegatives = append(a.falseNegatives, ans.detail) + default: + a.reached[class]++ + } + } + for _, key := range slices.Sorted(maps.Keys(reached)) { + if !targets[key] { + a.unmatched = append(a.unmatched, "project="+project+" symbol="+key) + } + } +} + +// rows writes the counts of each direction, the share of each direction +// on which the two tools agree, and one row per false negative, per +// answer to review and per unmatched symbol. +func (a vulnAgreement) rows() []string { + total := func(m map[string]int) int { + n := 0 + for _, c := range m { + n += c + } + return n + } + reached, quiet := total(a.reached), total(a.quiet) + rows := []string{ + "set=govulncheck-agreement language=go version=" + govulncheckVersion, + " govulncheck-reached=" + strconv.Itoa(reached) + " found=" + strconv.Itoa(a.reached[answerFound]) + " uncertain=" + strconv.Itoa(a.reached[answerUncertain]) + " false-negative=" + strconv.Itoa(len(a.falseNegatives)), + " govulncheck-not-reached=" + strconv.Itoa(quiet) + " not-found=" + strconv.Itoa(a.quiet[answerMissing]) + " uncertain=" + strconv.Itoa(a.quiet[answerUncertain]) + " found=" + strconv.Itoa(a.quiet[answerFound]), + " agree-reached=" + ratio{hit: a.reached[answerFound], all: reached}.String() + " agree-not-reached=" + ratio{hit: a.quiet[answerMissing], all: quiet}.String(), + " unmatched=" + strconv.Itoa(len(a.unmatched)), + } + for _, group := range []struct { + name string + lines []string + }{{"false-negative", a.falseNegatives}, {"review", a.review}, {"unmatched", a.unmatched}} { + for _, line := range group.lines { + rows = append(rows, " "+group.name+" "+line) + } + } + return rows +} + +func TestVulnAgreement(t *testing.T) { + answer := func(key, outcome string) methodAnswer { + return methodAnswer{key: key, outcome: outcome, detail: "target=" + key + " outcome=" + outcome} + } + answers := []methodAnswer{ + answer("x//Missed", "not-found"), + answer("x//Absent", "no-such-target"), + answer("x//Called", "found/low"), + answer("x//Unsure", "uncertain/ambiguous"), + answer("x//Extra", "found"), + answer("x//Quiet", "not-found"), + } + reached := map[string]bool{"x//Missed": true, "x//Absent": true, "x//Called": true, "x//Unsure": true, "x//Other": true} + + var a vulnAgreement + a.add("p", answers, reached) + + assert.Equal(t, []string{"target=x//Missed outcome=not-found", "target=x//Absent outcome=no-such-target"}, a.falseNegatives) + assert.Equal(t, []string{"target=x//Extra outcome=found"}, a.review) + assert.Equal(t, []string{"project=p symbol=x//Other"}, a.unmatched) + assert.Equal(t, map[string]int{answerMissing: 2, answerFound: 1, answerUncertain: 1}, a.reached) + assert.Equal(t, map[string]int{answerFound: 1, answerMissing: 1}, a.quiet) + assert.Contains(t, a.rows(), " govulncheck-reached=4 found=1 uncertain=1 false-negative=2") +} diff --git a/test/projectgraph/methods_test.go b/test/projectgraph/methods_test.go index 301a7474..ffbe958e 100644 --- a/test/projectgraph/methods_test.go +++ b/test/projectgraph/methods_test.go @@ -26,6 +26,7 @@ import ( "github.com/safedep/code/projectgraph" "github.com/safedep/code/projectgraph/modules" "github.com/safedep/code/projectgraph/reach" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "golang.org/x/mod/module" "golang.org/x/mod/semver" @@ -61,8 +62,8 @@ type advisory struct { ID string `json:"id"` Source string `json:"source,omitempty"` Ranges []struct { - Introduced string `json:"introduced"` - Fixed string `json:"fixed"` + Introduced string `json:"introduced,omitempty"` + Fixed string `json:"fixed,omitempty"` } `json:"ranges"` Imports []struct { Path string `json:"path"` @@ -306,6 +307,7 @@ func TestMethodTargets(t *testing.T) { if measureGo && os.Getenv(methodNoStdEnv) == "" { releases = goReleases(t, corpus) } + vulncheck := newVulnCheck(t, corpus, advisories["go"], measureGo) counts := methodCounts{} var lines, details []string @@ -343,11 +345,16 @@ func TestMethodTargets(t *testing.T) { require.NoError(t, err) methods := receiverReasons(t, g, nonTestRoots(g)) project := methodCounts{} + answers := make([]methodAnswer, len(results)) for i, r := range results { o := outcome(r, methods) counts.add(p.language, targets[i].kind, o) project.add(p.language, targets[i].kind, o) - details = append(details, methodDetail(p.name, targets[i].key, o, r)) + answers[i] = methodAnswer{key: targets[i].key, outcome: o, detail: methodDetail(p.name, targets[i].key, o, r)} + details = append(details, answers[i].detail) + } + if p.language == "go" { + vulncheck.compare(t, p.name, dir, answers) } lines = append(lines, "project="+p.name+" files="+strconv.Itoa(len(g.Files))+" truncated="+strconv.Itoa(len(g.Truncated))+" problems="+strconv.Itoa(len(g.Problems))) for _, row := range project.rows() { @@ -356,6 +363,7 @@ func TestMethodTargets(t *testing.T) { } rows := append([]string{"set=methods projects=" + strconv.Itoa(len(projects))}, counts.rows()...) rows = append(rows, lines...) + rows = append(rows, vulncheck.rows()...) t.Log("\n" + strings.Join(rows, "\n")) if out := os.Getenv("PROJECTGRAPH_METHOD_REPORT"); out != "" { require.NoError(t, os.WriteFile(out, []byte(strings.Join(rows, "\n")+"\n"), 0o644)) @@ -363,6 +371,13 @@ func TestMethodTargets(t *testing.T) { if out := os.Getenv(methodDetailEnv); out != "" { require.NoError(t, os.WriteFile(out, []byte(strings.Join(details, "\n")+"\n"), 0o644)) } + assert.Empty(t, vulncheck.agreement.falseNegatives, "govulncheck reaches these targets, and reach says that no path exists") +} + +// methodAnswer is the answer of reach for one target of a project, and +// the detail line of the target. +type methodAnswer struct { + key, outcome, detail string } // methodDetailEnv names the file that gets one line per target: the From 159784faa804bedde5b76656c40e71bcfdd72cf1 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 08:59:56 +0000 Subject: [PATCH 05/58] fix(projectgraph): record the Rust ? and skip builtins in the chained-call record (FOU-556) The Rust link step guessed a ? from a table of the methods of Option and Result. A name that the table did not hold took the type inside, so dep::try_thing().inspect(..) made an edge to Thing::inspect. The check also matched the whole path of the wrapper, so std::io::Result and anyhow::Result wrapped nothing. The Rust index now records each ? in the receiver of a method call as the name ? in the target, as dep//load//?//run for dep::load()?.run() and for t.run() after let t = dep::load()?. The walk of the link step reads ?, unwrap and expect as the value inside, and any other name on a wrapped result as a method of the wrapper, with no type. The table is gone. A wrapper type matches by the last name of its path. A call on the result of a call whose package no unit gives is a no-dependency-source record. The record also came for the builtins of a language, as Promise.resolve().then. The runtime calls other code only through a value that the call passes, and a reference edge holds it. So the record now skips the builtins of the language. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 17 +- fileindex/index.go | 5 + plugin/indexer/calls.go | 7 + plugin/indexer/languages.go | 6 + plugin/indexer/rust.go | 158 ++++++++++++++++-- plugin/indexer/rust_test.go | 35 ++++ projectgraph/resolve.go | 9 + projectgraph/results.go | 19 ++- projectgraph/rust.go | 42 +---- projectgraph/walk.go | 5 +- .../references-argument/expect.yaml | 8 +- test/projectgraph/receivers_test.go | 24 +++ .../testdata/receivers/rust/dep/src/lib.rs | 10 ++ 13 files changed, 278 insertions(+), 67 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index e8c2e180..8f8f6ddb 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -184,7 +184,11 @@ that a definition of the name reaches. 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 -`uncertain` and not `no-such-target`. +`uncertain` and not `no-such-target`. A call on the result of a builtin of +the language, as `Promise.resolve(x).then(f)`, makes no record. The code +of the runtime calls the code of the project or of a dependency only +through a value that the call passes, as `f`, and a `reference` edge holds +that value. ### Result types @@ -224,9 +228,14 @@ Rust follows the same rules, with these additions. `Self` as the type of its `impl`. A type drops `&`, `&mut`, `Box<..>`, `Rc<..>`, `Arc<..>` and its type arguments, so `&mut SmallVec<[u8; 4]>` is `SmallVec`. An `Option` or a `Result` result keeps `T` in - `Returns` and its wrapper in `Wraps`. `unwrap` and `expect` give `T`, - and so does a `?`, which the index does not record. A call of another - method of the wrapper, as `map`, has no type. + `Returns` and its wrapper in `Wraps`. A wrapper matches by the last name + of its path, so `std::io::Result` and `anyhow::Result` wrap `T`. + `unwrap` and `expect` give `T`, and so does a `?`. The index records each + `?` in the receiver of a method call as the name `?` in the target, as + `dep//load//?//run` for `dep::load()?.run()` and for `t.run()` after + `let t = dep::load()?`. A call of any other method of the wrapper, as + `map` or `inspect`, has no type, so the site is a record with the reason + `unknown-receiver`. - A declared type of a parameter, a field or a local that a unit gives binds the receiver. An associated function, as `Type::from_vec(v)`, binds the type that it returns, and `clone`, `to_owned`, `default` and diff --git a/fileindex/index.go b/fileindex/index.go index 0476dfdc..93882e54 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -59,6 +59,11 @@ const ( DefInterface DefKind = "interface" ) +// TryName is the name that a ? of Rust takes in a call target, as +// dep//load//?//run for dep::load()?.run(). The ? gives the value inside +// an Option or a Result, so the name after it is a name of that value. +const TryName = "?" + type CallKind string const ( diff --git a/plugin/indexer/calls.go b/plugin/indexer/calls.go index 828598e0..c4dbf087 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -31,6 +31,10 @@ func (p *projection) retargetTyped(call *fileindex.Call) { } func (p *projection) addCalls() { + var retarget func(call *fileindex.Call, callee ast.NodePosition) + if r := p.rules().retarget; r != nil { + retarget = r(p) + } for key, node := range p.cg.Nodes { for _, ref := range node.CallsTo { kind, ok := callKind(ref.Kind) @@ -64,6 +68,9 @@ func (p *projection) addCalls() { Args: p.arguments(key, ref.Site), } p.retargetTyped(&call) + if retarget != nil { + retarget(&call, ref.Site) + } p.ix.Calls = append(p.ix.Calls, call) } } diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 34e0dae0..f3d8b575 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -2,6 +2,7 @@ package indexer import ( "github.com/safedep/code/core" + "github.com/safedep/code/core/ast" "github.com/safedep/code/fileindex" sitter "github.com/smacker/go-tree-sitter" ) @@ -70,6 +71,11 @@ type languageRules struct { // prune drops the definitions that hold no record, after the records. prune func(p *projection) + // retarget returns the function that rewrites 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. + retarget func(p *projection) func(call *fileindex.Call, callee ast.NodePosition) + // visitors read the walk of the tree, as for the references and the // flags of the language, in this order. visitors []func(p *projection) visitor diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 610908d9..46cf0cca 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -4,6 +4,7 @@ import ( "slices" "strings" + "github.com/safedep/code/core/ast" "github.com/safedep/code/fileindex" "github.com/safedep/code/langcode" "github.com/safedep/code/pkg/ts" @@ -24,6 +25,7 @@ var rustRules = languageRules{ signature: rustSignature, exported: rustPublic, definitions: rustTypes, + retarget: rustRetarget, visitors: []func(p *projection) visitor{referenceVisitor, rustVisitor}, // 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 @@ -194,8 +196,9 @@ func (p *projection) rustTypeName(node *sitter.Node, bounds map[string]string) s return "" } -// rustWrappers are the types of std whose value ?, unwrap and expect give, -// as T of Option and of Result. +// rustWrappers are the types whose value ?, unwrap and expect give, as T +// of Option and of Result. A wrapper matches by the last name of +// its path, so std::io::Result and anyhow::Result wrap T too. var rustWrappers = []string{"Option", "Result"} // rustResult returns the type that a result names, and the Option or the @@ -203,6 +206,9 @@ var rustWrappers = []string{"Option", "Result"} func (p *projection) rustResult(node *sitter.Node, bounds map[string]string) (string, string) { if node != nil && node.Type() == "generic_type" { name := p.rustTypeName(node.ChildByFieldName("type"), bounds) + if i := strings.LastIndex(name, "::"); i >= 0 { + name = name[i+len("::"):] + } if args := ts.NamedChildren(node.ChildByFieldName("type_arguments")); slices.Contains(rustWrappers, name) && len(args) > 0 { return p.rustTypeName(args[0], bounds), name } @@ -267,11 +273,8 @@ func rustSignature(p *projection, def *fileindex.Definition, node *sitter.Node) if param.Type() != "parameter" { continue } - name := param.ChildByFieldName("pattern") - if name != nil && name.Type() == "mut_pattern" && name.NamedChildCount() > 0 { - name = name.NamedChild(0) - } - if typ := p.rustTypeName(param.ChildByFieldName("type"), bounds); typ != "" && name != nil && name.Type() == "identifier" { + name := rustBoundName(param.ChildByFieldName("pattern")) + if typ := p.rustTypeName(param.ChildByFieldName("type"), bounds); typ != "" && name != nil { def.Params = append(def.Params, fileindex.Typed{Name: p.text(name), Type: typ}) } } @@ -310,17 +313,148 @@ func (p *projection) rustLocals(body *sitter.Node, bounds map[string]string) []f if node.Type() != "let_declaration" { return } - name := node.ChildByFieldName("pattern") - if name != nil && name.Type() == "mut_pattern" && name.NamedChildCount() > 0 { - name = name.NamedChild(0) - } - if typ := p.rustTypeName(node.ChildByFieldName("type"), bounds); typ != "" && name != nil && name.Type() == "identifier" { + name := rustBoundName(node.ChildByFieldName("pattern")) + if typ := p.rustTypeName(node.ChildByFieldName("type"), bounds); typ != "" && name != nil { locals = append(locals, fileindex.Typed{Name: p.text(name), Type: typ}) } }) return locals } +// 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 { + if pattern != nil && pattern.Type() == "mut_pattern" && pattern.NamedChildCount() > 0 { + pattern = pattern.NamedChild(0) + } + if pattern == nil || pattern.Type() != "identifier" { + return nil + } + return pattern +} + +// rustRetarget 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 +// without the mark the link step cannot tell a method of the value inside +// an Option or a Result from a method of the wrapper, as inspect of +// Option. +func rustRetarget(p *projection) func(call *fileindex.Call, callee ast.NodePosition) { + var lets map[string][]*sitter.Node + letOf := func(name *sitter.Node) *sitter.Node { + if lets == nil { + lets = p.rustLets() + } + fn := rustFunction(name) + var out *sitter.Node + for _, let := range lets[p.text(name)] { + block := let.Parent() + if let.EndByte() <= name.StartByte() && block != nil && name.EndByte() <= block.EndByte() && rustFunction(let) == fn { + out = let + } + } + return out + } + var tries func(node *sitter.Node) []int + tries = func(node *sitter.Node) []int { + switch node.Type() { + case "try_expression": + return append(tries(node.NamedChild(0)), 0) + case "parenthesized_expression", "await_expression", "reference_expression": + return tries(node.NamedChild(0)) + case "identifier": + if let := letOf(node); let != nil && let.ChildByFieldName("value") != nil { + return tries(let.ChildByFieldName("value")) + } + case "call_expression": + if method := rustMethod(node.ChildByFieldName("function")); method != nil { + return rustAfter(tries(method.ChildByFieldName("value"))) + } + } + return nil + } + return func(call *fileindex.Call, callee ast.NodePosition) { + node := p.descendant(callee) + if node == nil || node.StartByte() != callee.StartByte || node.EndByte() != callee.EndByte { + return + } + if method := rustMethod(node); method != nil { + marks := rustAfter(tries(method.ChildByFieldName("value"))) + for i, target := range call.Targets { + call.Targets[i] = withTries(target, marks) + } + } + } +} + +// rustMethod returns the field expression of a method call, as +// t.run of t.run() or of t.run::(). Each method call adds one name to +// a target. A field that no call reads, as t.store in t.store.run(), keeps +// no ? of its value, so the walk of the link step gives no type there. +func rustMethod(fn *sitter.Node) *sitter.Node { + if fn != nil && fn.Type() == "generic_function" { + fn = fn.ChildByFieldName("function") + } + if fn == nil || fn.Type() != "field_expression" || fn.ChildByFieldName("value") == nil { + return nil + } + return fn +} + +// rustAfter moves the marks of a receiver past the name of the method that +// a call on it adds. +func rustAfter(marks []int) []int { + for i := range marks { + marks[i]++ + } + return marks +} + +// withTries puts a ? into a target before the count of names of each mark, +// counted from the end. +func withTries(target string, marks []int) string { + if len(marks) == 0 { + return target + } + names := strings.Split(target, fileindex.Separator) + n := len(names) + marks = slices.Clone(marks) + slices.Sort(marks) + for _, m := range slices.Compact(marks) { + if at := n - m; at > 0 { + names = slices.Insert(names, at, fileindex.TryName) + } + } + return strings.Join(names, fileindex.Separator) +} + +// rustLets returns the let declarations of the file that bind one name, by +// the name, in source order. +func (p *projection) rustLets() map[string][]*sitter.Node { + lets := map[string][]*sitter.Node{} + walkBody(p.tree.Tree().RootNode(), nil, func(node *sitter.Node) { + if node.Type() != "let_declaration" { + return + } + if name := rustBoundName(node.ChildByFieldName("pattern")); name != nil { + lets[p.text(name)] = append(lets[p.text(name)], node) + } + }) + return lets +} + +// rustFunction returns the start of the function that holds a node. A let +// binds its name for the rest of its block, and a closure in the block +// sees it, but a nested function does not. +func rustFunction(node *sitter.Node) uint32 { + for n := node.Parent(); n != nil; n = n.Parent() { + if n.Type() == "function_item" { + return n.StartByte() + } + } + return 0 +} + // rustPublic reports whether an item is pub. A method of a trait impl is // as public as the trait. func rustPublic(p *projection, def *fileindex.Definition) bool { diff --git a/plugin/indexer/rust_test.go b/plugin/indexer/rust_test.go index e1bc0960..9f27d006 100644 --- a/plugin/indexer/rust_test.go +++ b/plugin/indexer/rust_test.go @@ -61,3 +61,38 @@ func TestFileIndexRust(t *testing.T) { assert.Equal(t, []string{"Error", "Note"}, targets) }) } + +func TestFileIndexRustTry(t *testing.T) { + source := strings.Join([]string{ + "fn run() -> Result<(), Error> {", + " let t = dep::load()?;", + " t.save();", + " dep::load()?.check()?.save();", + " dep::find().inspect(|t| {});", + " Ok(())", + "}", + "fn read() -> std::io::Result { open() }", + }, "\n") + "\n" + ix := indexSource(t, core.LanguageCodeRust, "main.rs", source) + + targets := map[string][]string{} + for _, call := range ix.Calls { + targets[call.Callee] = call.Targets + } + tests := []struct { + callee string + want []string + }{ + {"t.save", []string{"dep//load//?//save"}}, + {"dep::load()?.check", []string{"dep//load//?//check"}}, + {"dep::load()?.check()?.save", []string{"dep//load//?//check//?//save"}}, + {"dep::find().inspect", []string{"dep//find//inspect"}}, + } + for _, tt := range tests { + assert.Equal(t, tt.want, targets[tt.callee], tt.callee) + } + + read := definitionOf(t, ix, "main.rs//read") + assert.Equal(t, "Job", read.Returns) + assert.Equal(t, "Result", read.Wraps) +} diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 0e76429a..ccdb91ac 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -159,6 +159,12 @@ func (b *builder) linkCalls(ix *fileindex.Index) { // right: new Yaml().load() in Java, or Yaml().load() in Python or Kotlin, // whose classes start with an upper case letter. A Go name starts with an // upper case letter when the package exports it, so it tells nothing. +// +// A builtin of the language, as Promise in Promise.resolve().then() or std +// in std::fs::read(p).map(f), is code of the runtime, which no unit gives. +// That code calls the code of the project or of a dependency only through +// a value that the call passes, and a reference edge holds each such +// value. So a call on its result makes no record. func (b *builder) onCallResult(p *profile, call fileindex.Call, r resolution) (fileindex.UnresolvedReason, bool) { called, ok := calledName(call.Receiver) if !ok || r.fail != nil || call.Method == "" || b.kindOf(r.key) != NodeExternal { @@ -171,6 +177,9 @@ func (b *builder) onCallResult(p *profile, call fileindex.Call, r resolution) (f return "", false } if _, provided := b.unitDefinitions(b.origins[r.key]); !provided { + if langcode.IsBuiltin(p.code, fileindex.Unit(r.key)) { + return "", false + } return fileindex.UnresolvedNoDependencySource, true } return fileindex.UnresolvedUnknownReceiver, true diff --git a/projectgraph/results.go b/projectgraph/results.go index ec2e09de..1aaa64ad 100644 --- a/projectgraph/results.go +++ b/projectgraph/results.go @@ -68,16 +68,21 @@ func (b *builder) oneResult(unit string, defs []string, pos int) (result, bool) } // unwrap returns how many of the names after a call a wrapped result -// takes: one for unwrap or expect, which give the value inside, and none -// for a name that the value inside takes after a ?, which the index does -// not record. A method of the wrapper itself, as map of Option, gives no -// type, so it reports false. A result with no wrapper takes no name. Only -// a Rust result has a wrapper. +// takes: one for unwrap, expect or a ?, which give the value inside. Any +// other name is a method of the wrapper itself, as map of Option, and +// gives no type, so it reports false. A result with no wrapper takes no +// name, and a ? on it gives no type, because the index knows no wrapper +// that holds the value. Only a Rust result has a wrapper. func unwrap(wraps string, next []string) (int, bool) { - if wraps == "" || len(next) == 0 { + switch { + case len(next) == 0: return 0, true + case wraps == "": + return 0, next[0] != fileindex.TryName + case rustUnwraps[next[0]]: + return 1, true } - return rustUnwrap(wraps, next[0]) + return 0, false } // resultText returns the type name of the result at a position of a diff --git a/projectgraph/rust.go b/projectgraph/rust.go index aacb25ea..0beeafc0 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -95,43 +95,11 @@ var rustImplicit = map[string]bool{ "call_once": true, "serialize": true, "deserialize": true, } -// rustUnwraps are the methods of Option and Result that give the value -// inside, and rustWrapperMethods the other methods of each one. A ? -// gives the value inside too, and the index records no ?, so a name that -// neither table holds takes the value inside. -var ( - rustUnwraps = map[string]bool{"unwrap": true, "expect": true, "unwrap_unchecked": true} - rustWrapperMethods = map[string]map[string]bool{ - "Option": setOf("and", "and_then", "as_deref", "as_deref_mut", "as_mut", "as_ref", "cloned", "copied", "filter", - "flatten", "get_or_insert", "get_or_insert_with", "insert", "is_none", "is_some", "is_some_and", "iter", - "iter_mut", "map", "map_or", "map_or_else", "ok_or", "ok_or_else", "or", "or_else", "replace", "take", - "unwrap_or", "unwrap_or_default", "unwrap_or_else", "xor", "zip"), - "Result": setOf("and", "and_then", "as_deref", "as_deref_mut", "as_mut", "as_ref", "cloned", "copied", "err", - "expect_err", "flatten", "is_err", "is_ok", "is_ok_and", "iter", "iter_mut", "map", "map_err", "map_or", - "map_or_else", "ok", "or", "or_else", "unwrap_err", "unwrap_or", "unwrap_or_default", "unwrap_or_else"), - } -) - -func setOf(names ...string) map[string]bool { - out := make(map[string]bool, len(names)) - for _, n := range names { - out[n] = true - } - return out -} - -// rustUnwrap returns how many names a Rust result in the wrapper takes -// before the next name: one for an unwrap, none for a name of the value -// inside, and false for a method of the wrapper itself. -func rustUnwrap(wraps, next string) (int, bool) { - switch { - case rustUnwraps[next]: - return 1, true - case rustWrapperMethods[wraps][next]: - return 0, false - } - return 0, true -} +// rustUnwraps are the names that give the value inside an Option or a +// Result: unwrap, expect and the ? that the index records in a target. Any +// other name on a wrapped result is a method of the wrapper itself, as map +// or inspect of Option, and the link step does not know its result. +var rustUnwraps = map[string]bool{"unwrap": true, "expect": true, "unwrap_unchecked": true, fileindex.TryName: true} // rustRoots are the first names of the paths of the crates of the // toolchain, which no crate of the project defines. diff --git a/projectgraph/walk.go b/projectgraph/walk.go index 9855829b..83a42752 100644 --- a/projectgraph/walk.go +++ b/projectgraph/walk.go @@ -2,6 +2,7 @@ package projectgraph import ( "cmp" + "slices" "strings" "github.com/safedep/code/fileindex" @@ -97,7 +98,9 @@ func (b *builder) walkFrom(r resolution, names []string) resolution { if r.virtual && i+1 == len(names) { r.class = r.key } - r.key = b.deriveExternal(r.key, names[i:]) + // A ? names no member, so the key keeps the names that the + // code writes. + r.key = b.deriveExternal(r.key, slices.DeleteFunc(slices.Clone(names[i:]), func(n string) bool { return n == fileindex.TryName })) r.rule = cmp.Or(r.rule, ruleExternal) return r case kind == NodeFunction || kind == NodeMethod || kind == NodeConstructor: diff --git a/test/projectgraph/cases/javascript/references-argument/expect.yaml b/test/projectgraph/cases/javascript/references-argument/expect.yaml index edc1f654..438d688b 100644 --- a/test/projectgraph/cases/javascript/references-argument/expect.yaml +++ b/test/projectgraph/cases/javascript/references-argument/expect.yaml @@ -1,5 +1,5 @@ source: safedep:hand -description: src/main.js passes the imported load to Array.map and the first-party parse to Promise.then. Each reference makes its function possibly called. The call of then is a call on the result of Promise.resolve, whose type the link step does not know with no code of Promise, so the site keeps a record with the reason no-dependency-source. +description: src/main.js passes the imported load to Array.map and the first-party parse to Promise.then. Each reference makes its function possibly called. The call of then is a call on the result of Promise.resolve. Promise is a builtin, whose code calls parse only through the reference, so the site has no record. roots: - key: src/main.js kind: manifest @@ -15,11 +15,7 @@ edges: kind: reference confidence: low site: src/main.js:5 -unresolved: - - caller: src/main.js - site: src/main.js:5 - reason: no-dependency-source - method: then +unresolved: [] results: - target: js-yaml//load status: found diff --git a/test/projectgraph/receivers_test.go b/test/projectgraph/receivers_test.go index 4c5fc597..96921656 100644 --- a/test/projectgraph/receivers_test.go +++ b/test/projectgraph/receivers_test.go @@ -168,6 +168,30 @@ var rustReceiverRows = []receiverRow{ target: "dep//Thing//insert", want: "found", }, + { + name: "anyhow result with ?", + source: "fn run() -> anyhow::Result<()> {\n let mut t = dep::parse_thing()?;\n t.insert(1);\n Ok(())\n}", + target: "dep//Thing//insert", + want: "found", + }, + { + name: "chained call after ? on a std::io::Result", + source: "fn run() -> std::io::Result<()> {\n dep::read_thing()?.insert(1);\n Ok(())\n}", + target: "dep//Thing//insert", + want: "found", + }, + { + name: "chained call after ? with a name of an Option method", + source: "fn run() -> Option<()> {\n dep::try_thing()?.insert(1);\n Some(())\n}", + target: "dep//Thing//insert", + want: "found", + }, + { + name: "option method with the name of a method of the value inside", + source: "fn run() {\n dep::try_thing().inspect(|t| {});\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, { name: "chained call", source: "fn run() {\n dep::new_thing().insert(1);\n}", diff --git a/test/projectgraph/testdata/receivers/rust/dep/src/lib.rs b/test/projectgraph/testdata/receivers/rust/dep/src/lib.rs index 33158560..c3af43f8 100644 --- a/test/projectgraph/testdata/receivers/rust/dep/src/lib.rs +++ b/test/projectgraph/testdata/receivers/rust/dep/src/lib.rs @@ -18,6 +18,8 @@ impl Thing { pub fn child(&self) -> &Thing { self } + + pub fn inspect(&self) {} } impl Clone for Thing { @@ -38,6 +40,14 @@ pub fn load_thing() -> Result, String> { Ok(Thing::new()) } +pub fn read_thing() -> std::io::Result> { + Ok(Thing::new()) +} + +pub fn parse_thing() -> anyhow::Result> { + Ok(Thing::new()) +} + pub trait Doer { fn run(&self); } From a31cf33ae068927711d0db8dd5201432f01b1c8b Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:00:37 +0000 Subject: [PATCH 06/58] fix(indexer): link a Rust method call on a boxed trait object (FOU-578) The reach query answered not-found for dep::Html::write when main called w.write() on a Box that held a dep::Html. The call graph took Box::new as a constructor method of Box. So w became an instance of Box, and the call linked to Box//write with high confidence and no record. The declared type of the let never reached the link step, because the index typed a Rust receiver only from a parameter. The call graph now takes the value of Box::new, Rc::new and Arc::new as the value of a call, as these pointers hold their argument. A call on such a value keeps an unknown-receiver record. The index of a Rust file now gives such a call the key of the name in its scope when a parameter or a typed let declares the name. The link step then reads the trait of the unit and adds a cha edge to each impl of the method. A value that the call graph makes, as String::new(), keeps its target. The declared type types the call only when the function binds the name once, before the call. A let, a match arm, an if let, a for or a closure can bind the name again with another type. Without that check, a call on the new value took the method of the old type and lost its record, and the case trait-object-shadowed read dep::Plain::write as not-found. The cases trait-object, glob-use and macro-import copy the cases of the vet reach corpus. trait-object is now found. glob-use and macro-import stay uncertain with an unresolved-call at the call site, as before. Method measure, PROJECTGRAPH_METHOD_LANGUAGES=rust, before and after: function targets=13 found=1 found/low=3 uncertain/no-definition=9 method targets=38 found/low=16 uncertain/budget=9 uncertain/no-definition=13 other targets=5 no-such-target=5 The corpus has no call on a boxed trait object on a target path, so no count changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 12 +++ langcode/builtins_rust.go | 11 +++ plugin/callgraph/rust.go | 1 + plugin/callgraph/shapes.go | 11 ++- plugin/indexer/calls.go | 52 ++++++++--- plugin/indexer/languages.go | 7 ++ plugin/indexer/rust.go | 67 ++++++++++++-- .../cases/rust/glob-use/expect.yaml | 29 ++++++ .../cases/rust/glob-use/project/Cargo.toml | 7 ++ .../cases/rust/glob-use/project/src/main.rs | 5 ++ .../glob-use/project/vendor/dep/Cargo.toml | 4 + .../glob-use/project/vendor/dep/src/html.rs | 3 + .../glob-use/project/vendor/dep/src/lib.rs | 1 + .../glob-use/summaries/cargo_dep_1.2.0.json | 23 +++++ .../cases/rust/macro-import/expect.yaml | 37 ++++++++ .../rust/macro-import/project/Cargo.toml | 7 ++ .../rust/macro-import/project/src/main.rs | 6 ++ .../project/vendor/dep/Cargo.toml | 4 + .../project/vendor/dep/src/lib.rs | 8 ++ .../summaries/cargo_dep_1.2.0.json | 16 ++++ .../rust/trait-object-shadowed/expect.yaml | 88 +++++++++++++++++++ .../trait-object-shadowed/project/Cargo.toml | 7 ++ .../trait-object-shadowed/project/src/main.rs | 10 +++ .../project/vendor/dep/Cargo.toml | 4 + .../project/vendor/dep/src/lib.rs | 19 ++++ .../summaries/cargo_dep_1.2.0.json | 58 ++++++++++++ .../cases/rust/trait-object/expect.yaml | 65 ++++++++++++++ .../rust/trait-object/project/Cargo.toml | 7 ++ .../rust/trait-object/project/src/main.rs | 6 ++ .../project/vendor/dep/Cargo.toml | 4 + .../project/vendor/dep/src/lib.rs | 9 ++ .../summaries/cargo_dep_1.2.0.json | 30 +++++++ test/projectgraph/receivers_test.go | 18 ++++ 33 files changed, 616 insertions(+), 20 deletions(-) create mode 100644 test/projectgraph/cases/rust/glob-use/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/glob-use/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/html.rs create mode 100644 test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/glob-use/summaries/cargo_dep_1.2.0.json create mode 100644 test/projectgraph/cases/rust/macro-import/expect.yaml create mode 100644 test/projectgraph/cases/rust/macro-import/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-import/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/macro-import/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/macro-import/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/macro-import/summaries/cargo_dep_1.2.0.json create mode 100644 test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml create mode 100644 test/projectgraph/cases/rust/trait-object-shadowed/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/trait-object-shadowed/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/trait-object-shadowed/summaries/cargo_dep_1.2.0.json create mode 100644 test/projectgraph/cases/rust/trait-object/expect.yaml create mode 100644 test/projectgraph/cases/rust/trait-object/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/trait-object/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/trait-object/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/trait-object/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/trait-object/summaries/cargo_dep_1.2.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index e8c2e180..8cc87a09 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -232,6 +232,18 @@ Rust follows the same rules, with these additions. binds the type that it returns, and `clone`, `to_owned`, `default` and `from` on a type of a unit keep the type also when the crate derives them. +- `Box::new`, `Rc::new` and `Arc::new` hold their argument, so their + value is no instance of the pointer. A method call on it runs a method of + the argument. With no declared type, as `let w = Box::new(Html {})`, the + call is a record with the reason `unknown-receiver`. A declared type of + the local, as `let w: Box = Box::new(Html {})`, binds the + receiver. A declared type binds the receiver only where the index of the + file knows no type of the value. So `let s: String = String::new()` keeps + the target `String//shout` of `s.shout()`. The function must also bind + the name once, as a parameter or a typed `let`, before the call. A `let`, + a `match` arm, an `if let`, a `for` or a closure can bind the name again + with another type. A call on such a name keeps its record, so a method of + the real type does not read as `not-found`. - A trait object or a generic bound `T: Trait` of a unit gets the `cha` edges to the implementations in the graph, through their external keys. With no implementation, the site is a record with the reason diff --git a/langcode/builtins_rust.go b/langcode/builtins_rust.go index ac6e2224..15988bf6 100644 --- a/langcode/builtins_rust.go +++ b/langcode/builtins_rust.go @@ -1,5 +1,7 @@ package langcode +import "slices" + // rustBuiltins are the macros of std and the names of the prelude, which are in scope in every module. var rustBuiltins = []string{ "println", "print", "eprintln", "eprint", "format", "format_args", "write", "writeln", @@ -10,3 +12,12 @@ var rustBuiltins = []string{ "Some", "None", "Ok", "Err", "drop", "Box", "Vec", "String", "Option", "Result", "std", "core", "alloc", } + +// rustPointers are the smart pointers of std. A method call on a pointer +// runs a method of the value that it holds, as s.save() on a Box. +var rustPointers = []string{"Box", "Rc", "Arc"} + +// IsRustPointer reports whether the name is a smart pointer of std. +func IsRustPointer(name string) bool { + return slices.Contains(rustPointers, name) +} diff --git a/plugin/callgraph/rust.go b/plugin/callgraph/rust.go index 38e50242..f43c2808 100644 --- a/plugin/callgraph/rust.go +++ b/plugin/callgraph/rust.go @@ -61,6 +61,7 @@ var rustProfile = shapedProfile(languageProfile{ skipped: []string{"use_declaration", "extern_crate_declaration", "attribute_item", "inner_attribute_item"}, values: map[string]childRef{"generic_function": named("function"), "type_cast_expression": named("value")}, constructorMethods: []string{"new", "default"}, + pointer: langcode.IsRustPointer, classKeywords: []string{"Self"}, computedCallees: map[string]bool{"index_expression": true}, }) diff --git a/plugin/callgraph/shapes.go b/plugin/callgraph/shapes.go index d2d87268..1a6d4433 100644 --- a/plugin/callgraph/shapes.go +++ b/plugin/callgraph/shapes.go @@ -35,6 +35,11 @@ type syntaxShapes struct { // receiver, as in OpenAI::Client.new. A variable that gets the result is // an instance of the receiver. constructorMethods []string + // pointer reports whether a type is a pointer whose constructor method + // holds its argument, as Box in Box::new(Html {}). A method call on the + // value runs a method of the argument, so the value is no instance of + // the pointer. + pointer func(name string) bool // classKeywords name the class that holds the code, as Self in Rust or // static in PHP. superKeywords name its base class, as parent in PHP. @@ -650,13 +655,17 @@ func cleanSegment(segment string) string { // call of its name makes in a language of class calls, as Parser(). func (p *shapedProcessors) instanceOf(callee string) (string, bool) { receiver, method, found := cutLast(callee, namespaceSeparator) - if found && slices.Contains(p.shapes.constructorMethods, method) { + if found && slices.Contains(p.shapes.constructorMethods, method) && !p.isPointer(lastOf(receiver)) { return receiver, true } first, _ := utf8.DecodeRuneInString(lastOf(callee)) return callee, p.shapes.classCalls && unicode.IsUpper(first) } +func (p *shapedProcessors) isPointer(name string) bool { + return p.shapes.pointer != nil && p.shapes.pointer(name) +} + // lastOf returns the last segment of a namespace. func lastOf(key string) string { _, last, found := cutLast(key, namespaceSeparator) diff --git a/plugin/indexer/calls.go b/plugin/indexer/calls.go index 828598e0..68bb1381 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -13,24 +13,25 @@ import ( // retargetTyped gives a call on a receiver that a declared type of the // scope names the key of the name in its scope as its target, in a language -// with typedReceivers. The call graph bound the receiver to the value of an -// assignment, as dep//new_thing for t = dep.new_thing(), and a call on that -// value, as dep//new_thing//do, names no method. The declared type or the -// result type of the name does. A call that the call graph bound to a -// method of a class of the file, as Parser//render for p = Parser(), keeps -// that target. -func (p *projection) retargetTyped(call *fileindex.Call) { +// with typedReceivers, or with typedResult at an untyped site. The call +// graph bound the receiver to the value of an assignment, as dep//new_thing +// for t = dep.new_thing(), and a call on that value, as dep//new_thing//do, +// names no method. The declared type or the result type of the name does. +// A call that the call graph bound to a method of a class of the file, as +// Parser//render for p = Parser(), keeps that target. +func (p *projection) retargetTyped(call *fileindex.Call, untyped bool) { for _, t := range call.Targets { if def, ok := p.defs[fileindex.Parent(t)]; ok && def.Kind == fileindex.DefClass { return } } - if target, ok := p.typedTarget(call.Caller, call.Receiver, call.Method); ok { + if target, ok := p.typedTarget(call.Caller, call.Receiver, call.Method, call.Line, call.Column, untyped); ok { call.Targets = []string{target} } } func (p *projection) addCalls() { + untyped := p.untypedSites() for key, node := range p.cg.Nodes { for _, ref := range node.CallsTo { kind, ok := callKind(ref.Kind) @@ -63,12 +64,28 @@ func (p *projection) addCalls() { Kind: kind, Args: p.arguments(key, ref.Site), } - p.retargetTyped(&call) + p.retargetTyped(&call, untyped[[2]uint32{ref.Site.StartLine, ref.Site.StartColumn}]) p.ix.Calls = append(p.ix.Calls, call) } } } +// untypedSites returns the line and the column of each call on a receiver +// whose value the call graph does not type, as the value of a call, in a +// language with typedResult. +func (p *projection) untypedSites() map[[2]uint32]bool { + if p.rules().typedResult == nil { + return nil + } + sites := map[[2]uint32]bool{} + for _, u := range p.cg.Unresolved() { + if u.Reason == callgraph.UnresolvedUnknownReceiver { + sites[[2]uint32{u.Site.StartLine, u.Site.StartColumn}] = true + } + } + return sites +} + // objectMember returns the definition that a JavaScript call of a member of // an object of the file names, as api//run for api.run() after // const api = { run() {} }. The call graph keeps no value of an object @@ -232,7 +249,8 @@ func (p *projection) addUnresolved() { continue } caller := p.caller(u.Caller, u.Site) - if target, ok := p.typedTarget(caller, u.Receiver, u.Method); ok && u.Reason == callgraph.UnresolvedUnknownReceiver { + untyped := u.Reason == callgraph.UnresolvedUnknownReceiver + if target, ok := p.typedTarget(caller, u.Receiver, u.Method, int(u.Site.StartLine), int(u.Site.StartColumn), untyped); ok && untyped { p.ix.Calls = append(p.ix.Calls, fileindex.Call{ Caller: caller, Line: int(u.Site.StartLine), @@ -259,10 +277,11 @@ func (p *projection) addUnresolved() { // typedTarget returns the target of a call on a receiver whose first name a // declared type of the scope chain of the caller types, as main.py//run//t//do -// for t.do() in run(t: Thing), in a language with typedReceivers. The link -// step reads the type of the name from the scope, as it does for Go. -func (p *projection) typedTarget(caller, receiver, method string) (string, bool) { - if !p.rules().typedReceivers || receiver == "" || method == "" { +// for t.do() in run(t: Thing), in a language with typedReceivers, or with +// typedResult at an untyped site. The link step reads the type of the name +// from the scope, as it does for Go. +func (p *projection) typedTarget(caller, receiver, method string, line, column int, untyped bool) (string, bool) { + if receiver == "" || method == "" { return "", false } names := strings.Split(receiver, ".") @@ -271,6 +290,11 @@ func (p *projection) typedTarget(caller, receiver, method string) (string, bool) return "", false } } + rules := p.rules() + typed := rules.typedReceivers || untyped && rules.typedResult != nil && rules.typedResult(p, caller, names[0], line, column) + if !typed { + return "", false + } scope, ok := p.typedScope(caller, names[0]) if !ok { return "", false diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 34e0dae0..ba9dd19c 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -29,6 +29,13 @@ type languageRules struct { // on such a receiver gets the key of the name in its scope as its // target, as main.py//run//t//do, so the link step reads the type. typedReceivers bool + // typedResult reports whether the declared type of a name types a call + // on it at a line and a column where the call graph knows no type of + // its value, as Rust for w.write() after + // let w: Box = Box::new(Html {}). A value that the call graph + // makes, as String::new(), is an instance of that type alone, so a call + // on it keeps its target. + typedResult func(p *projection, caller, name string, line, column int) bool // packageOf returns the package or the namespace of the file, which the // files of one unit share. diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 610908d9..38c520f3 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -1,6 +1,7 @@ package indexer import ( + "context" "slices" "strings" @@ -25,6 +26,7 @@ var rustRules = languageRules{ exported: rustPublic, definitions: rustTypes, visitors: []func(p *projection) visitor{referenceVisitor, rustVisitor}, + typedResult: rustDeclaredAt, // 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. @@ -50,10 +52,6 @@ var rustRules = languageRules{ // block of the same name holds. var rustTypeItems = []string{"struct_item", "enum_item", "union_item"} -// rustPointers hold a value whose methods a call reaches through them, as -// s.save() on a Box. -var rustPointers = []string{"Box", "Rc", "Arc"} - // rustTypes adds each struct, enum and union that no impl block of its // scope names, so that a path names the type. An impl block of the type // is the class of the type, and its node gives the methods. @@ -176,7 +174,7 @@ func (p *projection) rustTypeName(node *sitter.Node, bounds map[string]string) s return p.text(node) case "generic_type": name := p.rustTypeName(node.ChildByFieldName("type"), bounds) - if args := ts.NamedChildren(node.ChildByFieldName("type_arguments")); slices.Contains(rustPointers, name) && len(args) > 0 { + if args := ts.NamedChildren(node.ChildByFieldName("type_arguments")); langcode.IsRustPointer(name) && len(args) > 0 { return p.rustTypeName(args[0], bounds) } return name @@ -321,6 +319,65 @@ func (p *projection) rustLocals(body *sitter.Node, bounds map[string]string) []f return locals } +// rustDeclaredAt reports whether the declared type of a name of a function +// holds at a line and a column of the function: the function binds the name +// once, as a parameter or a let with a type, before that place. A let, a +// match arm, an if let, a for or a closure can bind the name again with +// another type. Without the check, a call on such a name took the method of +// the declared type and lost its record, so a method of the real type read +// as not-found. +func rustDeclaredAt(p *projection, caller, name string, line, column int) bool { + fn := p.syntax[caller] + if fn == nil || fn.Type() != "function_item" { + return false + } + var binders []*sitter.Node + err := ts.Walk(context.Background(), fn, ts.WalkOptions{}, func(node *sitter.Node) bool { + if node.Type() == "function_item" && !ts.SameNode(node, fn) { + return false + } + if (node.Type() == "identifier" || node.Type() == "shorthand_field_identifier") && p.text(node) == name { + if binder := rustBinder(node); binder != nil { + binders = append(binders, binder) + } + } + return true + }) + if err != nil || len(binders) != 1 { + return false + } + binder := binders[0] + if at := binder.StartPoint(); int(at.Row)+1 > line || int(at.Row)+1 == line && int(at.Column)+1 >= column { + return false + } + return binder.Type() == "parameter" || binder.Type() == "let_declaration" && binder.ChildByFieldName("type") != nil +} + +// rustBinder returns the node that binds an identifier of a pattern, as the +// let, the parameter or the match pattern, or nil for an identifier that is +// no binding. +func rustBinder(id *sitter.Node) *sitter.Node { + child, parent := id, id.Parent() + for parent != nil && strings.HasSuffix(parent.Type(), "_pattern") && parent.Type() != "match_pattern" { + if typ := parent.ChildByFieldName("type"); typ != nil && ts.SameNode(typ, child) { + return nil + } + child, parent = parent, parent.Parent() + } + if parent == nil { + return nil + } + switch parent.Type() { + case "match_pattern", "closure_parameters": + return parent + case "let_declaration", "for_expression", "let_condition", "parameter": + if pattern := parent.ChildByFieldName("pattern"); pattern != nil && ts.SameNode(pattern, child) { + return parent + } + } + return nil +} + // rustPublic reports whether an item is pub. A method of a trait impl is // as public as the trait. func rustPublic(p *projection, def *fileindex.Definition) bool { diff --git a/test/projectgraph/cases/rust/glob-use/expect.yaml b/test/projectgraph/cases/rust/glob-use/expect.yaml new file mode 100644 index 00000000..843d7711 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/expect.yaml @@ -0,0 +1,29 @@ +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. +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 +unresolved: + - caller: src/main.rs//main + site: src/main.rs:4 + reason: unknown-receiver + method: render +results: + - target: dep//html//render + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/main.rs:4 + - target: dep//html//escape + status: not-found diff --git a/test/projectgraph/cases/rust/glob-use/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/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/project/src/main.rs b/test/projectgraph/cases/rust/glob-use/project/src/main.rs new file mode 100644 index 00000000..e648a30d --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/project/src/main.rs @@ -0,0 +1,5 @@ +use dep::html::*; + +fn main() { + render(); +} diff --git a/test/projectgraph/cases/rust/glob-use/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/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/project/vendor/dep/src/html.rs b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/html.rs new file mode 100644 index 00000000..38c45e42 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/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/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..3d4613dd --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/project/vendor/dep/src/lib.rs @@ -0,0 +1 @@ +pub mod html; 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 new file mode 100644 index 00000000..12f7c4e6 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,23 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "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/macro-import/expect.yaml b/test/projectgraph/cases/rust/macro-import/expect.yaml new file mode 100644 index 00000000..97d9d859 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/expect.yaml @@ -0,0 +1,37 @@ +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. +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: cargo:dep@1.2.0//src/lib.rs//render_now + to: cargo:dep@1.2.0//src/lib.rs//render + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:6 +unresolved: + - caller: src/main.rs//main + site: src/main.rs:5 + reason: unknown-receiver + method: render_now +results: + - target: dep//render + status: uncertain + 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 diff --git a/test/projectgraph/cases/rust/macro-import/project/Cargo.toml b/test/projectgraph/cases/rust/macro-import/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/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-import/project/src/main.rs b/test/projectgraph/cases/rust/macro-import/project/src/main.rs new file mode 100644 index 00000000..9214cf3f --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/project/src/main.rs @@ -0,0 +1,6 @@ +#[macro_use] +extern crate dep; + +fn main() { + render_now!(); +} diff --git a/test/projectgraph/cases/rust/macro-import/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/macro-import/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/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-import/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/macro-import/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..f2ae6e99 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/project/vendor/dep/src/lib.rs @@ -0,0 +1,8 @@ +pub fn render() {} + +#[macro_export] +macro_rules! render_now { + () => { + $crate::render() + }; +} 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 new file mode 100644 index 00000000..e4d8fec0 --- /dev/null +++ b/test/projectgraph/cases/rust/macro-import/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,16 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "cargo:dep@1.2.0", + "symbols": [ + { + "key": "dep//render", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//render" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml b/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml new file mode 100644 index 00000000..d644ce21 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml @@ -0,0 +1,88 @@ +source: safedep:hand +description: >- + print takes a Box, then a let binds the name w again to the dep::Plain that dep::plain returns, and + calls write on it. The code of the crate dep is the unit cargo:dep@1.2.0. The declared type of the parameter does + not hold after the let, so the call keeps the result type of dep::plain and goes to dep//Plain//write. If the + declared type typed the call, it would get the default write of the trait and the cha edge to Html::write, and + dep//Plain//write would read as not-found. Nothing calls write on an Html, so dep//Html//write 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: cargo:dep@1.2.0//src/lib.rs//Html//write + to: cargo:dep@1.2.0//src/lib.rs//Html + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:8 + - from: cargo:dep@1.2.0//src/lib.rs//Plain//write + to: cargo:dep@1.2.0//src/lib.rs//Plain + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:14 + - from: cargo:dep@1.2.0//src/lib.rs//plain + to: cargo:dep@1.2.0//src/lib.rs//Plain + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:18 + - from: dep//Plain//write + to: cargo:dep@1.2.0//src/lib.rs//Plain//write + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:14 + - from: dep//plain + to: cargo:dep@1.2.0//src/lib.rs//plain + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:17 + - from: src/main.rs//main + to: src/main.rs//print + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//main + to: Box//new + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//main + to: dep//Html + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//print + to: dep//plain + kind: direct + confidence: high + site: src/main.rs:8 + - from: src/main.rs//print + to: dep//Plain//write + kind: direct + confidence: high + site: src/main.rs:9 +unresolved: [] +results: + - target: dep//Plain//write + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//print + site: src/main.rs:4 + kind: direct + confidence: high + - key: dep//Plain//write + site: src/main.rs:9 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//print + site: src/main.rs:9 + reached: true + - target: dep//Html//write + status: not-found diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/project/Cargo.toml b/test/projectgraph/cases/rust/trait-object-shadowed/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/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/trait-object-shadowed/project/src/main.rs b/test/projectgraph/cases/rust/trait-object-shadowed/project/src/main.rs new file mode 100644 index 00000000..74ece660 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/project/src/main.rs @@ -0,0 +1,10 @@ +use dep::{Html, Write}; + +fn main() { + print(Box::new(Html {})); +} + +fn print(w: Box) { + let w = dep::plain(); + w.write(); +} diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/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/trait-object-shadowed/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..29825dfa --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/project/vendor/dep/src/lib.rs @@ -0,0 +1,19 @@ +pub trait Write { + fn write(&self) {} +} + +pub struct Html {} + +impl Write for Html { + fn write(&self) {} +} + +pub struct Plain {} + +impl Plain { + pub fn write(&self) {} +} + +pub fn plain() -> Plain { + Plain {} +} 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 new file mode 100644 index 00000000..85672d3c --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object-shadowed/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,58 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "cargo:dep@1.2.0", + "symbols": [ + { + "key": "dep//Html", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Html" + ], + "reaches": [] + }, + { + "key": "dep//Html//write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Html//write" + ], + "reaches": [] + }, + { + "key": "dep//Plain", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Plain" + ], + "reaches": [] + }, + { + "key": "dep//Plain//write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Plain//write" + ], + "reaches": [] + }, + { + "key": "dep//Write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Write" + ], + "reaches": [] + }, + { + "key": "dep//Write//write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Write//write" + ], + "reaches": [] + }, + { + "key": "dep//plain", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//plain" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/rust/trait-object/expect.yaml b/test/projectgraph/cases/rust/trait-object/expect.yaml new file mode 100644 index 00000000..071baf45 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/expect.yaml @@ -0,0 +1,65 @@ +source: safedep:hand +description: >- + The case of the vet reach corpus rust/trait-object. main calls write on a Box that holds a dep::Html. The + code of the crate dep is the unit cargo:dep@1.2.0. Box::new holds its argument, so the value of the call is no + instance of Box, and the declared type of w gives the trait Write of the unit. Html implements Write, so the call + gets a cha edge to dep//Html//write. A type of another crate can implement Write too, so the site keeps its record. +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: cargo:dep@1.2.0//src/lib.rs//Html//write + to: cargo:dep@1.2.0//src/lib.rs//Html + kind: direct + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:8 + - from: dep//Html//write + to: cargo:dep@1.2.0//src/lib.rs//Html//write + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:8 + - from: src/main.rs//main + to: dep//Write + kind: reference + confidence: low + site: src/main.rs:4 + - from: src/main.rs//main + to: Box//new + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//main + to: dep//Html + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/main.rs//main + to: dep//Html//write + kind: cha + confidence: medium + site: src/main.rs:5 +unresolved: + - caller: src/main.rs//main + site: src/main.rs:5 + reason: no-definition + method: write +results: + - target: dep//Html//write + status: found + path: + - key: src/main.rs//main + - key: dep//Html//write + site: src/main.rs:5 + kind: cha + confidence: medium + call_sites: + - caller: src/main.rs//main + site: src/main.rs:5 + reached: true diff --git a/test/projectgraph/cases/rust/trait-object/project/Cargo.toml b/test/projectgraph/cases/rust/trait-object/project/Cargo.toml new file mode 100644 index 00000000..56e246be --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/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/trait-object/project/src/main.rs b/test/projectgraph/cases/rust/trait-object/project/src/main.rs new file mode 100644 index 00000000..82fb8f1f --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/project/src/main.rs @@ -0,0 +1,6 @@ +use dep::{Html, Write}; + +fn main() { + let w: Box = Box::new(Html {}); + w.write(); +} diff --git a/test/projectgraph/cases/rust/trait-object/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/trait-object/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..5352efc0 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/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/trait-object/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/trait-object/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..fa315abd --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/project/vendor/dep/src/lib.rs @@ -0,0 +1,9 @@ +pub trait Write { + fn write(&self); +} + +pub struct Html {} + +impl Write for Html { + fn write(&self) {} +} 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 new file mode 100644 index 00000000..f8846920 --- /dev/null +++ b/test/projectgraph/cases/rust/trait-object/summaries/cargo_dep_1.2.0.json @@ -0,0 +1,30 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "cargo:dep@1.2.0", + "symbols": [ + { + "key": "dep//Html", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Html" + ], + "reaches": [] + }, + { + "key": "dep//Html//write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Html//write" + ], + "reaches": [] + }, + { + "key": "dep//Write", + "definitions": [ + "cargo:dep@1.2.0//src/lib.rs//Write" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/receivers_test.go b/test/projectgraph/receivers_test.go index 4c5fc597..677a3099 100644 --- a/test/projectgraph/receivers_test.go +++ b/test/projectgraph/receivers_test.go @@ -198,6 +198,24 @@ var rustReceiverRows = []receiverRow{ target: "dep//Thing//run", want: "found", }, + { + name: "boxed trait object", + source: "fn run() {\n let d: Box = Box::new(Thing::::new());\n d.run();\n}", + target: "dep//Thing//run", + want: "found", + }, + { + name: "reference-counted trait object", + source: "use std::rc::Rc;\n\nfn run() {\n let d: Rc = Rc::new(Thing::::new());\n d.run();\n}", + target: "dep//Thing//run", + want: "found", + }, + { + name: "boxed value with no declared type", + source: "fn run() {\n let d = Box::new(Thing::::new());\n d.run();\n}", + target: "dep//Thing//run", + want: "uncertain/unknown-receiver", + }, { name: "generic bound", source: "fn run(d: &T) {\n d.run();\n}", From 4d2283e87c36bc1c39a491bcae9b4c818ba8af3c Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:09:34 +0000 Subject: [PATCH 07/58] docs(projectgraph): report the standard library unit and the govulncheck agreement (FOU-561, FOU-563) The report gives the Go rows of the method measure with and without the unit go:std@, the top sites of the targets that stay unknown-receiver, and the agreement of each Go target with govulncheck in each direction. The run gives 0 false negatives, because reach gives no Go target as not-found or no-such-target yet. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- test/projectgraph/report.md | 110 ++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/test/projectgraph/report.md b/test/projectgraph/report.md index f2d08968..e78a16de 100644 --- a/test/projectgraph/report.md +++ b/test/projectgraph/report.md @@ -1360,3 +1360,113 @@ the standard library to the project graph. The case that goes on to `os.execvp`. The reach result still reports the shorter path through the reference edge of the override rule, because the search returns a shortest path. + +### The Go standard library unit + +FOU-561 adds the `src` tree of the Go release of each Go project to the +corpus. The release comes from the `go` line of the go.mod of the +project: go1.12 for colly, go1.13 for goquery and hey, go1.14 for +go-grpc-middleware, go1.15 for sftp, go1.16 for bluemonday, go1.18 for +scany, go1.19 for melody and fiber, and go1.20 for go-rabbitmq. The +measure links the tree as the unit `go:std@`, as vet links +GOROOT. The advisories of the measure name no symbol of the standard +library, so the unit adds no target. It adds the code of the standard +library to each graph, and the types of its receivers. + +The rows come from two runs of the branch of FOU-561 and FOU-563, on +2026-11-14, on the machine above. The run "without" sets +`PROJECTGRAPH_METHOD_NO_STD=1`. The Python and Rust rows do not change. + +| Language | Kind | Run | Targets | found | not-found | no-such-target | unknown-receiver | no-definition | interface-receiver | no-dependency-source | budget | ambiguous | computed-name | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | +| Go | function | without | 256 | 51 | 0 | 0 | 3 | 5 | 0 | 2 | 39 | 106 | 50 | +| Go | function | with | 256 | 56 | 0 | 0 | 14 | 5 | 0 | 0 | 0 | 154 | 27 | +| Go | method | without | 1298 | 331 | 0 | 0 | 154 | 71 | 2 | 2 | 109 | 416 | 213 | +| Go | method | with | 1298 | 366 | 0 | 0 | 256 | 132 | 5 | 0 | 0 | 466 | 73 | + +| Run | Go method targets that end as unknown-receiver | Files of the 10 Go graphs | +| --- | --- | --- | +| without | 0.119 (154/1298) | 5,286 | +| with | 0.197 (256/1298) | 23,991 | + +- `found` moves from 331 to 366 Go method targets and from 51 to 56 Go + function targets. A call on a receiver of a standard library type now + has a type, and the graph holds the paths through the standard + library, as a handler that `net/http` calls. +- `unknown-receiver` grows from 154 to 256 Go method targets. The cause + counts a target when the search reaches any call site with an unknown + receiver and the method name of the target. The standard library adds + code that the search reaches, and more sites with the names `String`, + `RoundTrip`, `Close`, `Error` and `Write`. So the share rises, and it is + not a count of the receiver forms that the unit fixes. +- `budget` and `computed-name` fall, and `ambiguous` grows. The budget + records of scany and the computed names of hey are still in the graph. + The first uncertainty of these results is now an ambiguous call of the + standard library, as `regexp/regexp.go` of go1.18 for scany. +- The graphs hold 4.5 times the files. The run of all three languages + takes 356 seconds in place of 124 on this machine. + +These are the sites with an unknown receiver that the most +`unknown-receiver` targets reach. A site concerns each target whose +method name it has. + +| Site | Targets | Call | Receiver form | +| --- | --- | --- | --- | +| `net/http/server.go:3372` of go1.13 | 16 | `h.c.RemoteAddr().String()` | the result of a method of an interface, `net.Addr` | +| x/net `http2/frame.go:1553` | 14 | `hdec.Close()` | a local that a field of the receiver gives, `*hpack.Decoder` | +| x/net `websocket/hybi.go:318` and `:330` | 14 | `w.Close()` | the result of a method of an interface, `io.WriteCloser` | +| x/net `webdav/internal/xml/xml.go:1916` and `:1919` | 11 | `w.Write(...)` | an `io.Writer` parameter | +| x/net `webdav/internal/xml/marshal.go:220` to `:247` | 11 | `p.Write(t)` | a method of the embedded field `*bufio.Writer` | +| hey `requester/report.go:134` | 10 | `buf.String()` | a local of `&bytes.Buffer{}` | +| hey `requester/print.go:92` | 10 | `res.String()` | a local of `new(bytes.Buffer)` | +| `net/http/transport.go:533` of go1.13 | 10 | `pconn.alt.RoundTrip(req)` | an interface field, `http.RoundTripper` | +| `net/http/httputil/reverseproxy.go:249` of go1.13 | 10 | `transport.RoundTrip(outreq)` | an interface local | +| go-grpc-middleware `validator/validator.go:33` | 8 | `err.Error()` | the `error` interface | +| go-grpc-middleware `tracing/opentracing/server_interceptors.go:95` | 8 | `err.Error()` | the `error` interface | + +Of the 1,810 pairs of a target and such a site, 771 sites are in the +code of a module, 457 in the standard library and 582 in a project. +`Error` on the `error` interface and a method of an interface field or +parameter stay records until the graph has the implementations of the +method. A method on the result of an interface method, as +`RemoteAddr().String()`, needs the result type of the interface method. +The link step does not bind three other forms yet, also with the types +of the standard library: the two `bytes.Buffer` locals of hey, a method +of an embedded field, and a local that a field of the receiver gives. +Each one is a candidate for an issue. + +### Agreement with govulncheck + +FOU-563 runs govulncheck v1.9.0 on each Go project of the corpus, with no +network, against the Go advisories of `advisories.json`, and compares +each Go target. `README.md` says how. The rows come from the same runs +as the table above. + +| Run | govulncheck reaches | reach found | reach uncertain | false negative | govulncheck does not reach | reach not-found | reach uncertain | reach found (review) | unmatched | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | +| without | 115 | 60 | 55 | 0 | 1439 | 0 | 1117 | 322 | 1 | +| with | 115 | 62 | 53 | 0 | 1439 | 0 | 1079 | 360 | 1 | + +| Run | Agreement, targets that govulncheck reaches | Agreement, targets that govulncheck does not reach | +| --- | --- | --- | +| without | 0.522 (60/115) | 0.000 (0/1439) | +| with | 0.539 (62/115) | 0.000 (0/1439) | + +- No target is a false negative. Reach gives no Go target as `not-found` + or `no-such-target` yet, so the check has no negative answer to test. + The fixes of FOU-557, FOU-558, FOU-559, FOU-560 and FOU-562 turn + uncertain answers into definite ones, and the check then tests each of + them. +- 360 targets that reach finds are rows to review in the output of the + measure: 75 with a path of `high` and `medium` edges, and 285 with a + `low` hop. Reach starts from each exported symbol of a library project. + govulncheck starts from the packages of the project and follows the + calls that its call graph proves. +- The unmatched symbol is `utf16Decoder.Transform` of golang.org/x/text, + of GO-2020-0015. The build list of colly holds x/text at an affected + version, and `corpus.txt` does not list x/text for colly, so the measure + has no target for it. +- govulncheck reaches symbols of the module-wide advisory GO-2026-6372 of + amqp091-go in go-rabbitmq, as `Channel.NotifyClose`. The advisory has + no symbol list and gives no target, so the comparison leaves out its + findings. From 3291fc32298d9f65321efefbff827b5ac67e6a17 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:08:01 +0000 Subject: [PATCH 08/58] fix(projectgraph): keep the method name of an ambiguous call (FOU-557) linkFields cleared the method name of a site with more than 20 name candidates. The reach query scopes a record by its method name, and a record with no method name concerns every target. So one ambiguous call, as x.Len(), made every target of the project uncertain. An ambiguous call can run only a method of its name. The site now keeps the method name, so the reach query applies it to the targets with that last segment and to the targets that a definition of the name reaches. The name-ambiguous case adds a function of another name that no root calls. Its target is now not-found and was uncertain before the change. The Go method measure (PROJECTGRAPH_METHOD_LANGUAGES=go) gives: kind=method before: ambiguous=416 computed-name=213 no-definition=71 unknown-receiver=154 no-such-target=0 kind=method after: ambiguous=0 computed-name=579 no-definition=110 unknown-receiver=156 no-such-target=9 kind=function before: ambiguous=106 computed-name=50 no-definition=5 kind=function after: ambiguous=0 computed-name=139 no-definition=21 no-such-target=1 No target carries an ambiguous site any more. The targets fall to the next uncertainty that concerns every target, mostly computed-name. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 10 ++++++---- projectgraph/cha_test.go | 2 +- projectgraph/field.go | 4 +++- projectgraph/link_test.go | 2 +- .../cases/javascript/field-ambiguous/expect.yaml | 7 ++++++- .../cases/python/name-ambiguous/expect.yaml | 16 ++++++++++++++-- .../python/name-ambiguous/project/handlers.py | 5 +++++ 7 files changed, 36 insertions(+), 10 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index e8c2e180..9ac6d39f 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -176,10 +176,12 @@ reason: `computed-name`, `dynamic-import`, `unknown-receiver`, reach query can tell which targets it concerns. A site with more than 20 candidates of its method name, as `render` in a -Django project, is `ambiguous` and keeps no method name. The site gets no -edge, so the search cannot follow what the candidates call, and the record -concerns every target. A record with a method name concerns the targets -that a definition of the name reaches. +Django project, is `ambiguous`. The site gets no edge, so the search cannot +follow what the candidates call. The site keeps its method name, because +the call can run only a method of that name. A record with a method name +concerns the targets with that last segment and the targets that a +definition of the name reaches, so it concerns what each candidate +reaches. 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 diff --git a/projectgraph/cha_test.go b/projectgraph/cha_test.go index cf0136d8..9c909a7d 100644 --- a/projectgraph/cha_test.go +++ b/projectgraph/cha_test.go @@ -210,7 +210,7 @@ func TestBuildFieldEdges(t *testing.T) { assert.Equal(t, []string{ "main.js:1 main.js unknown-receiver parse", "main.js:2 main.js unknown-receiver parse", - "main.js:3 main.js ambiguous ", + "main.js:3 main.js ambiguous get", }, unresolvedStrings(g), "25 candidates make the site ambiguous") } diff --git a/projectgraph/field.go b/projectgraph/field.go index 1dceb6a4..e9996c2a 100644 --- a/projectgraph/field.go +++ b/projectgraph/field.go @@ -51,9 +51,11 @@ func (b *builder) linkFields() { } } } + // An ambiguous site keeps its method name. It can run only a + // method of that name, so the reach query scopes it by the name. if len(candidates) > maxNameCandidates { delete(b.unresolved, u) - u.Reason, u.Method = fileindex.UnresolvedAmbiguous, "" + u.Reason = fileindex.UnresolvedAmbiguous b.addUnresolved(u) continue } diff --git a/projectgraph/link_test.go b/projectgraph/link_test.go index bd2ca572..9df7832a 100644 --- a/projectgraph/link_test.go +++ b/projectgraph/link_test.go @@ -862,7 +862,7 @@ func TestBuildNameEdges(t *testing.T) { assert.Equal(t, []string{ "main.py:2 main.py unknown-receiver cb", "main.py:4 main.py unknown-receiver run", - "main.py:5 main.py ambiguous ", + "main.py:5 main.py ambiguous get", "main.py:6 main.py computed-name ", }, unresolvedStrings(g), "a bare name with no binding is a record, and too many candidates make the site ambiguous") assert.Contains(t, g.Nodes, "print", "a builtin stays external") diff --git a/test/projectgraph/cases/javascript/field-ambiguous/expect.yaml b/test/projectgraph/cases/javascript/field-ambiguous/expect.yaml index d2229db5..34f74fc9 100644 --- a/test/projectgraph/cases/javascript/field-ambiguous/expect.yaml +++ b/test/projectgraph/cases/javascript/field-ambiguous/expect.yaml @@ -1,5 +1,9 @@ source: safedep:hand -description: dispatch calls handle on the parameter handler, whose value the index does not know. 25 object properties hold a function named handle. That is over the cap of 20, so the site gets no field edge and is ambiguous. An ambiguous site can call any target, so the reach result for yaml.load is uncertain. +description: >- + dispatch calls handle on the parameter handler, whose value the index does not know. 25 object properties + hold a function named handle. That is over the cap of 20, so the site gets no field edge and is ambiguous. The + site keeps the method name handle, and h0.handle calls yaml.load, so the reach result for yaml.load is + uncertain. roots: - key: src/main.js kind: manifest @@ -17,6 +21,7 @@ unresolved: - caller: src/main.js//dispatch site: src/main.js:4 reason: ambiguous + method: handle results: - target: js-yaml//load status: uncertain diff --git a/test/projectgraph/cases/python/name-ambiguous/expect.yaml b/test/projectgraph/cases/python/name-ambiguous/expect.yaml index 43f10784..ac4b970c 100644 --- a/test/projectgraph/cases/python/name-ambiguous/expect.yaml +++ b/test/projectgraph/cases/python/name-ambiguous/expect.yaml @@ -1,5 +1,10 @@ source: safedep:hand -description: dispatch calls handle on the parameter handler, whose value the index does not know. 25 classes define handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. An ambiguous site can call any target, so the reach result for yaml.load is uncertain. +description: >- + dispatch calls handle on the parameter handler, whose value the index does not know. 25 classes define + handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. The site keeps the + method name handle. H0.handle calls yaml.load, so the reach result for yaml.load is uncertain. An ambiguous + call of handle runs no function of another name, so os.remove, which only the uncalled cleanup calls, is + not found. roots: - key: main.py kind: manifest @@ -17,13 +22,20 @@ unresolved: - caller: main.py//dispatch site: main.py:5 reason: ambiguous + method: handle results: - target: yaml//load status: uncertain call_sites: - caller: handlers.py//H0//handle - site: handlers.py:7 + site: handlers.py:8 reached: false uncertainties: - kind: unresolved-call site: main.py:5 + - target: os//remove + status: not-found + call_sites: + - caller: handlers.py//cleanup + site: handlers.py:132 + reached: false diff --git a/test/projectgraph/cases/python/name-ambiguous/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous/project/handlers.py index c48b244e..8bd7ab57 100644 --- a/test/projectgraph/cases/python/name-ambiguous/project/handlers.py +++ b/test/projectgraph/cases/python/name-ambiguous/project/handlers.py @@ -1,3 +1,4 @@ +import os import yaml @@ -125,3 +126,7 @@ def handle(self, text): class H24: def handle(self, text): return text + + +def cleanup(path): + os.remove(path) From b9adbc6e6817b622aec80f00a2feb018fa63ae1a Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:18:31 +0000 Subject: [PATCH 09/58] fix(projectgraph): index Go fields, constants and variables (FOU-558) The Go index recorded functions, methods and types, but not struct fields, constants or package variables. A read of n.Data or pgx.ErrNoRows in dependency code walked into the unit, found no definition and gave a no-definition record on the external node. That record made every target of the unit uncertain. A read of a field or a constant runs no code. The index now records each named struct field as a field, each constant as a constant and each package variable as a variable. A variable holds the target of a value that names a function, as run in var Run = run. The index marks a variable as data when its declared type or its value shows that it is no function. The link step gives a field, a constant and a data variable no node. A read of one gives no edge and no record. A call of any other variable keeps its edge or its unknown-receiver record, so a call of var Handler = func() {...} stays uncertain. A method that an embedded interface declares, as ssh.Client.Close through ssh.Conn, now resolves as a call on a value of the interface. The walk of an external key into a unit that ends at a method of an interface gives an interface-receiver record. The case go/dependency-data-reads covers a field, a constant, a data variable, a function value in a variable, a promoted method of an embedded struct and of an embedded interface. Method measure, Go only, against main: no-definition: function 5 -> 0, method 71 -> 19 not-found: 0 -> 19, all in melody. Its constant reads hid a gap of the missing GOROOT unit (FOU-561), as ioutil.ReadAll calls messageReader.Read. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 33 +++ fileindex/index.go | 20 +- plugin/indexer/golang.go | 237 +++++++++++++++-- plugin/indexer/golang_test.go | 68 +++++ plugin/indexer/project.go | 2 +- plugin/indexer/references.go | 4 +- plugin/indexer/testdata/golden/app.go.json | 45 ++++ projectgraph/definitions.go | 15 ++ projectgraph/golang.go | 25 ++ projectgraph/names.go | 3 + projectgraph/packages.go | 23 +- projectgraph/resolve.go | 8 +- projectgraph/rootmatch/match.go | 4 +- projectgraph/roots.go | 4 +- projectgraph/scope.go | 2 +- projectgraph/walk.go | 21 ++ .../go/dependency-data-reads/expect.yaml | 247 ++++++++++++++++++ .../go/dependency-data-reads/project/go.mod | 5 + .../gomod/example.com/markup@v1.0.0/client.go | 41 +++ .../gomod/example.com/markup@v1.0.0/go.mod | 3 + .../gomod/example.com/markup@v1.0.0/node.go | 41 +++ .../project/goroot/src/errors/errors.go | 9 + .../project/goroot/src/go.mod | 3 + .../project/goroot/src/os/exec/exec.go | 7 + .../project/goroot/src/os/file.go | 5 + .../go/dependency-data-reads/project/main.go | 28 ++ .../go_example.com_markup_v1.0.0.json | 143 ++++++++++ .../summaries/go_std_go1.25.1.json | 37 +++ 28 files changed, 1044 insertions(+), 39 deletions(-) create mode 100644 test/projectgraph/cases/go/dependency-data-reads/expect.yaml create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/go.mod create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/client.go create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/node.go create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/errors/errors.go create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/go.mod create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/exec/exec.go create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/file.go create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/main.go create mode 100644 test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json create mode 100644 test/projectgraph/cases/go/dependency-data-reads/summaries/go_std_go1.25.1.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index e8c2e180..d3fbb8fe 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -242,6 +242,39 @@ The standard library is a unit too. Pass `GOROOT/src` as the unit each import path whose first element has no dot, as `net/http`. The index step leaves out the `cmd` module of it, which no program imports. +### Go fields, constants and variables + +The Go index records each named field of a struct as a `field` under the +key of its type, as `html.go//Node//Data`, each constant as a `constant`, +and each package variable as a `variable`. A field and a constant hold no +code. A variable holds no code when the source shows that its value is no +function: its declared type is a predeclared type or a type literal that +is no function type, as `int` or `*Client`, or its value is a literal, a +composite literal, an arithmetic expression, a call of a builtin, or the +error of `errors.New` or `fmt.Errorf`. The index marks such a variable +with `data`. A variable whose value is a name, as `run` in +`var Run = run`, holds that value. + +Such a definition of data has no node. A read of it, as `n.Data` or +`pgx.ErrNoRows`, gives no edge and no record, in the project and in the +walk of an external key into a unit. A name on it, as `bufPool.Get()`, +is a record with the reason `unknown-receiver`, because the walk does not +know its type. + +A call of any other variable keeps its edge or its record. A variable that +holds a name gives the edge to that name, as a call of `Run` gives the +edge to `run`. A variable whose value can be a function that the index does +not know, as `var Handler = func() {...}` or `var h = makeHandler()`, gives +a record with the reason `unknown-receiver`. So a target that only such a +function reaches is `uncertain`, never `not-found`. + +A struct gets the methods of the types that it embeds. A method that an +embedded interface declares, as `Close` of `ssh.Client` that embeds the +interface `Conn`, runs on the value that the field holds, as a call on a +value of the interface does. The walk of an external key into a unit that +ends at such a method, or at a method of an interface of the unit, is a +record with the reason `interface-receiver`. + A `pypi:` unit is a distribution, as a site-packages directory or an unpacked wheel holds it. These rules apply to it. diff --git a/fileindex/index.go b/fileindex/index.go index 0476dfdc..2273a0ac 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -57,6 +57,11 @@ const ( // DefInterface is a TypeScript or Go interface. It has no code, but a // declared type can name it and a class can implement it. DefInterface DefKind = "interface" + // DefField is a named field of a Go struct, under the key of its type, + // as Node//Data. It holds data and no code. + DefField DefKind = "field" + // DefConstant is a constant of a Go package. It holds data and no code. + DefConstant DefKind = "constant" ) type CallKind string @@ -206,8 +211,8 @@ type Export struct { } // Definition is one module, function, method, class, constructor, module -// variable or interface. Key is its symbol key, as src/a.py//Parser//parse. -// Parent is the class key of a method. A JavaScript function that an object +// variable, interface, field or constant. Key is its symbol key, as +// src/a.py//Parser//parse. Parent is the class key of a method or a field. A JavaScript function that an object // property or a property assignment holds is a method with no parent. // Decorators are as written, in source order, and hold the annotations of // a Java definition. Values are the per-file targets that a variable @@ -223,7 +228,8 @@ type Export struct { // "" for a result with no named type. Wraps names the Option or the Result // that holds the result of a Rust function, and Returns then names the // type inside, as Thing for -> Option. Static marks a static method -// of Java. +// of Java. Data marks a Go variable whose value the source shows is no +// function, as var n int or var ErrX = errors.New("x"). type Definition struct { Key string `json:"key"` Kind DefKind `json:"kind"` @@ -242,9 +248,17 @@ type Definition struct { Results []string `json:"results,omitempty"` Wraps string `json:"wraps,omitempty"` Static bool `json:"static,omitempty"` + Data bool `json:"data,omitempty"` Flags []Flag `json:"flags,omitempty"` } +// HoldsData reports whether the definition holds data and no code: a +// field, a constant or a variable with Data. A read of it runs no code, and +// a call of it does not compile. +func (d Definition) HoldsData() bool { + return d.Kind == DefField || d.Kind == DefConstant || d.Data +} + // Class is one class or interface of the file. Bases are as written, in // source order, and the link step resolves them. Methods are definition // keys. Fields are the fields that declare the type of a class, in source diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 01b7d88d..1956e8fc 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -1,6 +1,7 @@ package indexer import ( + "slices" "unicode" "unicode/utf8" @@ -21,7 +22,7 @@ var goRules = languageRules{ abstract: goMethodElems, signature: goSignature, exported: goExported, - definitions: goTypes, + definitions: goDefinitions, visitors: []func(p *projection) visitor{referenceVisitor}, references: &referenceSyntax{ holders: map[string]string{ @@ -61,38 +62,226 @@ func goPackage(p *projection) string { return "" } -// goTypes adds each top-level named type of a Go file. An interface is an -// interface. Any other named type is a class, because it can have methods. -// The call graph has no node for a type, so the tree gives the types. -func goTypes(p *projection) { +// goDefinitions adds the top-level named types of a Go file with the fields +// of each struct, the constants and the package variables. The call graph +// has no node for them, so the tree gives them. +func goDefinitions(p *projection) { + var vars []*sitter.Node for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { - if statement.Type() != "type_declaration" { + switch statement.Type() { + case "type_declaration": + p.goTypes(statement) + case "const_declaration": + for _, spec := range ts.NamedChildren(statement) { + if spec.Type() == "const_spec" { + for _, name := range goNames(spec) { + p.goValue(name, spec, fileindex.DefConstant) + } + } + } + case "var_declaration": + vars = append(vars, goVarSpecs(statement)...) + } + } + for _, spec := range vars { + for _, name := range goNames(spec) { + p.goValue(name, spec, fileindex.DefVariable) + } + } + // A value can name a variable that the file declares after it. + for _, spec := range vars { + p.goVariableValues(spec) + } +} + +// goTypes adds each named type of a type declaration. An interface is an +// interface. Any other named type is a class, because it can have methods. +// A named field of a struct is a field of its class. +func (p *projection) goTypes(statement *sitter.Node) { + for _, spec := range ts.NamedChildren(statement) { + if spec.Type() != "type_spec" && spec.Type() != "type_alias" { continue } - for _, spec := range ts.NamedChildren(statement) { - if spec.Type() != "type_spec" && spec.Type() != "type_alias" { - continue - } - key := p.local(p.text(spec.ChildByFieldName("name"))) - if _, defined := p.defs[key]; defined { - continue - } - kind := fileindex.DefClass - if t := spec.ChildByFieldName("type"); t != nil && t.Type() == "interface_type" { - kind = fileindex.DefInterface + key := p.local(p.text(spec.ChildByFieldName("name"))) + if _, defined := p.defs[key]; defined { + continue + } + kind := fileindex.DefClass + t := spec.ChildByFieldName("type") + if t != nil && t.Type() == "interface_type" { + kind = fileindex.DefInterface + } + p.addGoDefinition(key, kind, spec) + p.addSyntax(key, spec) + if t == nil || t.Type() != "struct_type" || spec.Type() != "type_spec" { + continue + } + for _, field := range p.goStructFields(t) { + for _, name := range childrenByField(field, "name") { + p.addGoDefinition(fileindex.Join(key, p.text(name)), fileindex.DefField, field) } - p.defs[key] = &fileindex.Definition{ - Key: key, - Kind: kind, - Name: fileindex.Last(key), - StartLine: lineOf(spec), - EndLine: int(spec.EndPoint().Row) + 1, + } + } +} + +// goVarSpecs returns the specs of a var declaration. A declaration with +// parentheses holds them in a list. +func goVarSpecs(statement *sitter.Node) []*sitter.Node { + var specs []*sitter.Node + for _, child := range ts.NamedChildren(statement) { + switch child.Type() { + case "var_spec": + specs = append(specs, child) + case "var_spec_list": + specs = append(specs, goVarSpecs(child)...) + } + } + return specs +} + +// goNames returns the names that a const or a var spec declares. The commas +// between them have the name field too. +func goNames(spec *sitter.Node) []*sitter.Node { + return slices.DeleteFunc(childrenByField(spec, "name"), func(n *sitter.Node) bool { return n.Type() != "identifier" }) +} + +// goValue adds a constant or a package variable of a spec. The blank name +// _ declares nothing. +func (p *projection) goValue(name, spec *sitter.Node, kind fileindex.DefKind) { + if p.text(name) == "_" { + return + } + key := p.local(p.text(name)) + if _, defined := p.defs[key]; !defined { + p.addGoDefinition(key, kind, spec) + } +} + +func (p *projection) addGoDefinition(key string, kind fileindex.DefKind, node *sitter.Node) { + p.defs[key] = &fileindex.Definition{ + Key: key, + Kind: kind, + Name: fileindex.Last(key), + StartLine: lineOf(node), + EndLine: int(node.EndPoint().Row) + 1, + } +} + +// goVariableValues fills each package variable of a spec: the per-file +// target of a value that names a function or another value, as run for +// var Run = run, or the Data mark when the source shows that the value is +// no function. A variable with neither can hold a function that the index +// does not know, as the value of a function literal or of a call, so a +// call of it keeps its record. +func (p *projection) goVariableValues(spec *sitter.Node) { + names := goNames(spec) + var values []*sitter.Node + if list := spec.ChildByFieldName("value"); list != nil { + values = ts.NamedChildren(list) + } + typed := spec.ChildByFieldName("type") + for i, name := range names { + def := p.defs[p.local(p.text(name))] + if def == nil || def.Kind != fileindex.DefVariable || def.StartLine != lineOf(spec) { + continue + } + var value *sitter.Node + if len(values) == len(names) { + value = values[i] + } + switch { + case typed != nil && p.goDataType(typed): + def.Data = true + case value == nil: + case p.goDataValue(value): + def.Data = true + default: + if target, ok := p.goValueTarget(value); ok { + def.Values = []string{target} } - p.addSyntax(key, spec) } } } +// goDataType reports whether a value of the type is no function: a +// predeclared type, as int or error, or a type literal that is not a +// function type, as *T or []T. A named type can be a function type, as +// http.HandlerFunc. +func (p *projection) goDataType(t *sitter.Node) bool { + switch t.Type() { + case "type_identifier": + return langcode.IsBuiltin(langcode.Go, p.text(t)) && p.defs[p.local(p.text(t))] == nil + case "parenthesized_type": + return t.NamedChildCount() == 1 && p.goDataType(t.NamedChild(0)) + case "pointer_type", "slice_type", "array_type", "map_type", "channel_type", "struct_type", "interface_type": + return true + } + return false +} + +// goErrorConstructors are the functions of the standard library that give +// an error value, by import path and name. +var goErrorConstructors = map[[2]string]bool{{"errors", "New"}: true, {"fmt", "Errorf"}: true} + +// goDataValue reports whether the source shows that a value is no +// function: a literal, a composite literal, an arithmetic expression, a +// conversion to a type of data, a call of a builtin, or an error that +// errors.New or fmt.Errorf gives. +func (p *projection) goDataValue(value *sitter.Node) bool { + switch value.Type() { + case "interpreted_string_literal", "raw_string_literal", "int_literal", "float_literal", "imaginary_literal", "rune_literal", + "true", "false", "iota", "composite_literal", "binary_expression": + return true + case "parenthesized_expression": + return value.NamedChildCount() == 1 && p.goDataValue(value.NamedChild(0)) + case "unary_expression": + // A dereference of a pointer to a function or a receive from a + // channel of functions gives a function. + op := p.text(value.ChildByFieldName("operator")) + return op != "*" && op != "<-" + case "call_expression": + function := value.ChildByFieldName("function") + if function == nil { + return false + } + switch function.Type() { + case "identifier": + return langcode.IsBuiltin(langcode.Go, p.text(function)) && p.defs[p.local(p.text(function))] == nil + case "selector_expression": + operand, field := function.ChildByFieldName("operand"), function.ChildByFieldName("field") + return operand != nil && field != nil && goErrorConstructors[[2]string{p.importPath(p.text(operand)), p.text(field)}] + } + return p.goDataType(function) + } + return false +} + +// importPath returns the module of the import that binds the alias. +func (p *projection) importPath(alias string) string { + for _, imp := range p.ix.Imports { + if imp.Alias == alias { + return imp.Module + } + } + return "" +} + +// goValueTarget returns the per-file target of a value that names a +// function or another value: a definition of the file, a name of the +// package that the link step resolves, or a member of an import. +func (p *projection) goValueTarget(value *sitter.Node) (string, bool) { + switch value.Type() { + case "identifier": + if key := p.local(p.text(value)); p.defs[key] != nil { + return key, true + } + return p.text(value), true + case "selector_expression": + return p.reference(*p.rules().references, p.cg.FileName, value) + } + return "", false +} + // goBases returns the types that a Go type embeds, and the type that an // alias names. A method of such a type is a method of the type too. func goBases(p *projection, spec *sitter.Node) []string { diff --git a/plugin/indexer/golang_test.go b/plugin/indexer/golang_test.go index 0fcab12a..11851842 100644 --- a/plugin/indexer/golang_test.go +++ b/plugin/indexer/golang_test.go @@ -95,3 +95,71 @@ func TestFileIndexGo(t *testing.T) { } }) } + +func TestFileIndexGoValues(t *testing.T) { + source := strings.Join([]string{ + "package shop", + "import (", + " \"errors\"", + " \"example.com/shop/store\"", + ")", + "type Node struct { Data, kind string; *Base }", + "const ( Max, Min = 1, iota; Next )", + "var ErrNoRows = errors.New(\"no rows\")", + "var n, m int", + "var Default = &Node{}", + "var Handler = func() {}", + "var Hook = store.Hook()", + "var Run = run", + "var Open = store.Open", + "var Fn func()", + "var Conv = store.HandlerFunc(run)", + "var _ = run", + "func run() { use(ErrNoRows, Max, Run) }", + }, "\n") + "\n" + ix := indexSource(t, core.LanguageCodeGo, "shop.go", source) + + tests := []struct { + key string + kind fileindex.DefKind + data bool + values []string + }{ + {key: "shop.go//Node//Data", kind: fileindex.DefField}, + {key: "shop.go//Node//kind", kind: fileindex.DefField}, + {key: "shop.go//Max", kind: fileindex.DefConstant}, + {key: "shop.go//Min", kind: fileindex.DefConstant}, + {key: "shop.go//Next", kind: fileindex.DefConstant}, + {key: "shop.go//ErrNoRows", kind: fileindex.DefVariable, data: true}, + {key: "shop.go//n", kind: fileindex.DefVariable, data: true}, + {key: "shop.go//m", kind: fileindex.DefVariable, data: true}, + {key: "shop.go//Default", kind: fileindex.DefVariable, data: true}, + {key: "shop.go//Handler", kind: fileindex.DefVariable}, + {key: "shop.go//Hook", kind: fileindex.DefVariable}, + {key: "shop.go//Run", kind: fileindex.DefVariable, values: []string{"shop.go//run"}}, + {key: "shop.go//Open", kind: fileindex.DefVariable, values: []string{"example.com//shop//store//Open"}}, + {key: "shop.go//Fn", kind: fileindex.DefVariable}, + {key: "shop.go//Conv", kind: fileindex.DefVariable}, + } + for _, tt := range tests { + t.Run(tt.key, func(t *testing.T) { + def := definitionOf(t, ix, tt.key) + assert.Equal(t, tt.kind, def.Kind) + assert.Equal(t, tt.data, def.Data) + assert.Equal(t, tt.values, def.Values) + }) + } + t.Run("a field is no method of its class and the embedded type is a base", func(t *testing.T) { + node := classOf(t, ix, "shop.go//Node") + assert.Empty(t, node.Methods) + assert.Equal(t, []string{"Base"}, node.Bases) + assert.Equal(t, "shop.go//Node", definitionOf(t, ix, "shop.go//Node//Data").Parent) + }) + t.Run("a read of data is no reference and a read of a function value is one", func(t *testing.T) { + var texts []string + for _, ref := range ix.References { + texts = append(texts, ref.Text) + } + assert.Equal(t, []string{"run", "store.Open", "run", "run", "Run"}, texts) + }) +} diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index 874db990..57f89a03 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -344,7 +344,7 @@ func decorators(p *projection, node *sitter.Node) []string { func (p *projection) addClasses(_ context.Context) error { methods := map[string][]string{} for key, def := range p.defs { - if def.Parent != "" { + if def.Parent != "" && !def.HoldsData() { methods[def.Parent] = append(methods[def.Parent], key) } } diff --git a/plugin/indexer/references.go b/plugin/indexer/references.go index f85a2bd5..a577c566 100644 --- a/plugin/indexer/references.go +++ b/plugin/indexer/references.go @@ -89,8 +89,10 @@ func referenceVisitor(p *projection) visitor { } caller := stack[len(stack)-1].key for _, value := range syntax.unlist(values) { + // A read of a field, a constant or a variable of data runs + // no code and gives the reader no function. target, ok := p.reference(*syntax, caller, value) - if !ok { + if def := p.defs[target]; !ok || def != nil && def.HoldsData() { continue } at := ast.GetNodePosition(value) diff --git a/plugin/indexer/testdata/golden/app.go.json b/plugin/indexer/testdata/golden/app.go.json index e8cbe556..5d3938ae 100644 --- a/plugin/indexer/testdata/golden/app.go.json +++ b/plugin/indexer/testdata/golden/app.go.json @@ -53,6 +53,42 @@ "end_line": 24, "exported": true }, + { + "key": "app.go//Server//addr", + "kind": "field", + "name": "addr", + "start_line": 21, + "end_line": 21, + "exported": false, + "parent": "app.go//Server" + }, + { + "key": "app.go//Server//name", + "kind": "field", + "name": "name", + "start_line": 21, + "end_line": 21, + "exported": false, + "parent": "app.go//Server" + }, + { + "key": "app.go//Server//db", + "kind": "field", + "name": "db", + "start_line": 22, + "end_line": 22, + "exported": false, + "parent": "app.go//Server" + }, + { + "key": "app.go//Server//cache", + "kind": "field", + "name": "cache", + "start_line": 23, + "end_line": 23, + "exported": false, + "parent": "app.go//Server" + }, { "key": "app.go//Box", "kind": "class", @@ -61,6 +97,15 @@ "end_line": 26, "exported": true }, + { + "key": "app.go//Box//v", + "kind": "field", + "name": "v", + "start_line": 26, + "end_line": 26, + "exported": false, + "parent": "app.go//Box" + }, { "key": "app.go//Box//Get", "kind": "method", diff --git a/projectgraph/definitions.go b/projectgraph/definitions.go index 807c68c3..8e4af87c 100644 --- a/projectgraph/definitions.go +++ b/projectgraph/definitions.go @@ -26,6 +26,11 @@ func (b *builder) addDefinitions(ix *fileindex.Index) { for i := range ix.Definitions { def := &ix.Definitions[i] b.defs[def.Key] = def + // No edge goes to a definition of data, so it has no node. A walk + // finds it by its definition. + if def.HoldsData() { + continue + } if def.Key == ix.Path { module.Line = cmp.Or(def.StartLine, 1) module.Flags = append(module.Flags, def.Flags...) @@ -48,6 +53,9 @@ func (b *builder) addDefinitions(ix *fileindex.Index) { } } for _, def := range ix.Definitions { + if def.HoldsData() { + continue + } n := b.g.Nodes[def.Key] slices.SortFunc(n.Flags, fileindex.CompareFlags) n.Flags = slices.Compact(n.Flags) @@ -91,6 +99,13 @@ func nodeKind(k fileindex.DefKind) NodeKind { } } +// holdsData reports whether the key names a definition that holds data and +// no code, as a field or a constant of Go. +func (b *builder) holdsData(key string) bool { + def := b.defs[key] + return def != nil && def.HoldsData() +} + func (b *builder) addNode(n *Node) { if _, ok := b.g.Nodes[n.Key]; !ok { b.g.Nodes[n.Key] = n diff --git a/projectgraph/golang.go b/projectgraph/golang.go index c6ccc4eb..44199e4d 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -85,6 +85,7 @@ func goSurface(s *surfacer) { for _, def := range b.byPath[p].Definitions { segs := fileindex.Segments(def.Key) switch { + case def.HoldsData(): case len(segs) == 1 && isGoExported(segs[0]): s.add(langcode.Go, importPath, modules.FormImport, segs[0]) case len(segs) == 2 && def.Kind == fileindex.DefMethod && isGoExported(segs[0]) && isGoExported(segs[1]): @@ -332,3 +333,27 @@ func (b *builder) abstract(iface string, depth int) []fileindex.Signature { } return sigs } + +// goField returns the field of the name that a struct of the MRO of the +// class declares: the struct, or a struct that it embeds. +func (b *builder) goField(class, name string) (string, bool) { + for _, c := range b.mro(class) { + if key := fileindex.Join(c, name); b.defs[key] != nil && b.defs[key].Kind == fileindex.DefField { + return key, true + } + } + return "", false +} + +// declaringInterface returns the first-party interface of the MRO of the +// class that declares the method: the class itself, or an interface that +// it embeds, as Conn for Close of a struct that embeds Conn. +func (b *builder) declaringInterface(class, name string) (string, bool) { + for _, c := range b.mro(class) { + rec, ok := b.classes[c] + if ok && b.kindOf(c) == NodeInterface && slices.ContainsFunc(rec.class.Abstract, func(s fileindex.Signature) bool { return s.Name == name }) { + return c, true + } + } + return "", false +} diff --git a/projectgraph/names.go b/projectgraph/names.go index 79a8cb9b..73f461c1 100644 --- a/projectgraph/names.go +++ b/projectgraph/names.go @@ -47,6 +47,9 @@ func (b *builder) lookupName(module, name string, hops int) (string, *failure) { if n := b.g.Nodes[fileindex.Join(module, name)]; n != nil && n.Kind != NodeExport { return n.Key, nil } + if key := fileindex.Join(module, name); b.holdsData(key) { + return key, nil + } miss := noDefinition(name) try := func(fail *failure) bool { diff --git a/projectgraph/packages.go b/projectgraph/packages.go index b20d9570..b7e0df48 100644 --- a/projectgraph/packages.go +++ b/projectgraph/packages.go @@ -429,7 +429,11 @@ func (b *builder) linkProvision(key string, o origin, pv provision) { } res := b.walk(pv.module, names) if res.fail != nil { - fail(res.fail.reason) + fail(b.provisionReason(o.lang, res)) + return + } + // A field or a constant holds no code, so a read of it goes nowhere. + if b.holdsData(res.key) { return } n := b.g.Nodes[res.key] @@ -439,6 +443,23 @@ func (b *builder) linkProvision(key string, o origin, pv provision) { b.link(o.lang, key, res.key, Site{Path: cmp.Or(n.Path, pv.sp.unit), Line: n.Line}, EdgePackage, r) } +// provisionReason returns the reason of the record of a walk into a unit +// that fails. A method that an interface of the unit declares, as Close of +// a struct that embeds the interface Conn, has its code in the type that +// the interface value holds. The external key gets no edge to such a type, +// so its record is the one of a call on a value of an interface with no +// implementation. +func (b *builder) provisionReason(lang langcode.Code, res resolution) fileindex.UnresolvedReason { + reason, abstract := res.fail.reason, profileOf(lang).abstractReceiver + if reason != fileindex.UnresolvedNoDefinition || abstract == "" || res.class == "" { + return reason + } + if _, ok := b.declaringInterface(res.class, res.fail.name); ok { + return abstract + } + return reason +} + // addSurfaces links an external node to each exported symbol of each unit, // as an import of the package by its name would write it. func (b *builder) addSurfaces() { diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 0e76429a..bbf8f930 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -84,7 +84,7 @@ func (b *builder) linkCalls(ix *fileindex.Index) { continue } for _, r := range resolutions { - if r.unbound && sc.profile.literals[r.key] { + if r.unbound && sc.profile.literals[r.key] || r.fail == nil && b.holdsData(r.key) { continue } if r.unbound && sc.profile.thisCalls { @@ -339,8 +339,8 @@ func (b *builder) linkReferences(ix *fileindex.Index) { site := Site{Path: ix.Path, Line: ref.Line, Column: ref.Column} for _, r := range b.resolveTargets(sc, ref.Caller, ref.Targets) { // A Go value of a named type is no function, so passing it - // on calls nothing. - if r.fail != nil || r.unbound || sc.profile.typeConversions && b.isType(r.key) { + // on calls nothing. Nor is a field or a constant. + if r.fail != nil || r.unbound || sc.profile.typeConversions && b.isType(r.key) || b.holdsData(r.key) { continue } b.link(ix.Language, from, r.key, site, EdgeReference, ruleReference) @@ -540,7 +540,7 @@ func (b *builder) enclosingClass(key string) (string, bool) { // caller is a parameter or a local variable of the function, and its // declared type gives its class. func (b *builder) resolveLocal(sc *scope, caller, target string) resolution { - if b.g.Nodes[target] != nil { + if b.g.Nodes[target] != nil || b.holdsData(target) { return b.walkAs(target, nil, ruleLocal) } unit, segs := fileindex.Unit(target), fileindex.Segments(target) diff --git a/projectgraph/rootmatch/match.go b/projectgraph/rootmatch/match.go index 06c7f604..293bed12 100644 --- a/projectgraph/rootmatch/match.go +++ b/projectgraph/rootmatch/match.go @@ -204,7 +204,7 @@ func (f *finder) matchDefinition(r rules.Rule) { } for _, def := range ix.Definitions { switch { - case def.Name != m.Name: + case def.Name != m.Name || def.HoldsData(): case m.Parent != "" && fileindex.Last(def.Parent) != m.Parent: case m.InPackage != "" && (def.Kind != fileindex.DefFunction || len(fileindex.Segments(def.Key)) != 1): case m.Static && !def.Static: @@ -234,7 +234,7 @@ func (f *finder) matchTestFiles(r rules.Rule) { } f.add(r, ix.Path) for _, def := range ix.Definitions { - if def.Kind != fileindex.DefClass { + if def.Kind != fileindex.DefClass && !def.HoldsData() { f.add(r, def.Key) } } diff --git a/projectgraph/roots.go b/projectgraph/roots.go index c6aa0335..d5c8c4fa 100644 --- a/projectgraph/roots.go +++ b/projectgraph/roots.go @@ -72,7 +72,7 @@ func (b *builder) markRoots(ctx context.Context) error { } b.addRoot(ix.Path, RootLibrarySurface, string(RootLibrarySurface)) for _, def := range ix.Definitions { - if def.Exported && def.Key != ix.Path && def.Kind != fileindex.DefVariable && def.Kind != fileindex.DefInterface { + if def.Exported && def.Key != ix.Path && def.Kind != fileindex.DefVariable && def.Kind != fileindex.DefInterface && !def.HoldsData() { b.addRoot(def.Key, RootLibrarySurface, string(RootLibrarySurface)) } } @@ -152,7 +152,7 @@ func (b *builder) exportKeys(p string, seen map[string]bool) []string { var keys []string if sc.profile.definitionExports { for _, def := range sc.ix.Definitions { - if def.Exported && def.Key != p && def.Kind != fileindex.DefVariable && def.Kind != fileindex.DefInterface { + if def.Exported && def.Key != p && def.Kind != fileindex.DefVariable && def.Kind != fileindex.DefInterface && !def.HoldsData() { keys = append(keys, def.Key) } } diff --git a/projectgraph/scope.go b/projectgraph/scope.go index f803bf5c..20257167 100644 --- a/projectgraph/scope.go +++ b/projectgraph/scope.go @@ -80,7 +80,7 @@ func newScope(ix *fileindex.Index) *scope { func (sc *scope) enclosing(line int) string { key, span := sc.ix.Path, -1 for _, def := range sc.ix.Definitions { - if def.StartLine > line || def.EndLine < line { + if def.StartLine > line || def.EndLine < line || def.HoldsData() { continue } if s := def.EndLine - def.StartLine; span < 0 || s <= span { diff --git a/projectgraph/walk.go b/projectgraph/walk.go index 9855829b..0a8ea29b 100644 --- a/projectgraph/walk.go +++ b/projectgraph/walk.go @@ -64,6 +64,11 @@ func (b *builder) walkFrom(r resolution, names []string) resolution { consumed, done = b.walkVariable(&r, name) case kind == NodeClass || kind == NodeInterface: consumed, done = b.walkClass(&r, name, i+1 == len(names), &instance) + case b.holdsData(r.key): + // The walk does not know the type of a field, a constant or + // a variable of data, so it does not know a name on it. + r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} + return r case kind == NodeExternal || kind == "" && b.g.IsExternal(r.key): // A name before the last one under an external key is a call // of a function or a method whose result has the next name, @@ -232,6 +237,22 @@ func (b *builder) walkClass(r *resolution, name string, last bool, instance *boo return true, false } } + // A read of a field runs no code. A name on a field whose type the + // walk does not know is unknown. + if field, ok := b.goField(r.key, name); ok { + if !last { + r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} + return false, true + } + r.key = field + return true, false + } + // A method that an embedded interface declares runs on the value that + // the field holds, as a call on a value of the interface does. + if iface, ok := b.declaringInterface(r.key, name); ok { + r.class, r.virtual, r.fail = iface, true, noDefinition(name) + return false, true + } base, external := b.externalBase(r.key) switch { case b.forwards(r.key): diff --git a/test/projectgraph/cases/go/dependency-data-reads/expect.yaml b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml new file mode 100644 index 00000000..34065c31 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml @@ -0,0 +1,247 @@ +source: safedep:hand +description: >- + The unit markup has a struct Node with the field Data, a constant KindText, a variable ErrNoRows that + errors.New gives, a variable Handler that holds a function literal, a variable Run that holds the function + run, a struct Writer that embeds the struct Base, a struct Client that embeds the interface Conn. A read of n.Data, KindText or ErrNoRows + runs no code, so the walk into the unit gives no package edge and no record. The reference edges of main end + at the external keys. A call of Run gets a package edge to run. A call of Handler keeps an unknown-receiver + record, in main and in Render, because the index does not know the function that the variable holds. + Writer.Flush is the promoted method Base.Flush. Client.Close is the method Close that the embedded interface + Conn declares. A call of it runs the code of the type that the interface value holds, so it keeps an + interface-receiver record, and os.Remove, which only conn.Close calls, is uncertain, not found. +units: + gomod/example.com/markup@v1.0.0: go:example.com/markup@v1.0.0 + goroot/src: go:std@go1.25.1 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +exact_edges: true +edges: + - from: errors//New + to: go:std@go1.25.1//errors/errors.go//New + kind: package + confidence: high + site: go:std@go1.25.1//errors/errors.go:7 + - from: example.com//markup//Dial + to: go:example.com/markup@v1.0.0//client.go//Dial + kind: package + confidence: high + site: go:example.com/markup@v1.0.0//client.go:24 + - from: example.com//markup//NewWriter + to: go:example.com/markup@v1.0.0//client.go//NewWriter + kind: package + confidence: high + site: go:example.com/markup@v1.0.0//client.go:39 + - from: example.com//markup//Print + to: go:example.com/markup@v1.0.0//node.go//Print + kind: package + confidence: high + site: go:example.com/markup@v1.0.0//node.go:31 + - from: example.com//markup//Render + to: go:example.com/markup@v1.0.0//node.go//Render + kind: package + confidence: high + site: go:example.com/markup@v1.0.0//node.go:35 + - from: example.com//markup//Run + to: go:example.com/markup@v1.0.0//node.go//run + kind: package + confidence: high + site: go:example.com/markup@v1.0.0//node.go:27 + - from: example.com//markup//Writer//Flush + to: go:example.com/markup@v1.0.0//client.go//Base//Flush + kind: package + confidence: high + site: go:example.com/markup@v1.0.0//client.go:30 + - from: go:example.com/markup@v1.0.0//client.go//Base//Flush + to: os//exec//Command + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//client.go:31 + - from: go:example.com/markup@v1.0.0//client.go//conn//Close + to: os//Remove + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//client.go:15 + - from: go:example.com/markup@v1.0.0//node.go + to: errors//New + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//node.go:19 + - from: go:example.com/markup@v1.0.0//node.go + to: go:example.com/markup@v1.0.0//node.go//run + kind: reference + confidence: low + site: go:example.com/markup@v1.0.0//node.go:25 + - from: go:example.com/markup@v1.0.0//node.go + to: os//exec//Command + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//node.go:22 + - from: go:example.com/markup@v1.0.0//node.go//Print + to: os//exec//Command + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//node.go:32 + - from: go:example.com/markup@v1.0.0//node.go//Render + to: go:example.com/markup@v1.0.0//node.go//Print + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//node.go:36 + - from: go:example.com/markup@v1.0.0//node.go//Render + to: go:example.com/markup@v1.0.0//node.go//Print + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//node.go:37 + - from: go:example.com/markup@v1.0.0//node.go//Render + to: go:example.com/markup@v1.0.0//node.go//Print + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//node.go:38 + - from: go:example.com/markup@v1.0.0//node.go//Render + to: go:example.com/markup@v1.0.0//node.go//run + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//node.go:40 + - from: go:example.com/markup@v1.0.0//node.go//run + to: os//exec//Command + kind: direct + confidence: high + site: go:example.com/markup@v1.0.0//node.go:28 + - from: main.go + to: go:example.com/markup@v1.0.0//node.go + kind: import + confidence: high + site: main.go:3 + - from: main.go//call + to: example.com//markup//Handler + kind: direct + confidence: high + site: main.go:19 + - from: main.go//call + to: example.com//markup//Run + kind: direct + confidence: high + site: main.go:20 + - from: main.go//main + to: main.go//call + kind: direct + confidence: high + site: main.go:7 + - from: main.go//main + to: main.go//promoted + kind: direct + confidence: high + site: main.go:8 + - from: main.go//main + to: main.go//read + kind: direct + confidence: high + site: main.go:6 + - from: main.go//promoted + to: example.com//markup//Client//Close + kind: direct + confidence: high + site: main.go:27 + - from: main.go//promoted + to: example.com//markup//Dial + kind: direct + confidence: high + site: main.go:26 + - from: main.go//promoted + to: example.com//markup//NewWriter + kind: direct + confidence: high + site: main.go:24 + - from: main.go//promoted + to: example.com//markup//Writer//Flush + kind: direct + confidence: high + site: main.go:25 + - from: main.go//read + to: example.com//markup//ErrNoRows + kind: reference + confidence: low + site: main.go:13 + - from: main.go//read + to: example.com//markup//KindText + kind: reference + confidence: low + site: main.go:14 + - from: main.go//read + to: example.com//markup//Node//Data + kind: reference + confidence: low + site: main.go:12 + - from: main.go//read + to: example.com//markup//Print + kind: direct + confidence: high + site: main.go:12 + - from: main.go//read + to: example.com//markup//Print + kind: direct + confidence: high + site: main.go:13 + - from: main.go//read + to: example.com//markup//Print + kind: direct + confidence: high + site: main.go:14 + - from: main.go//read + to: example.com//markup//Render + kind: direct + confidence: high + site: main.go:15 + - from: os//Remove + to: go:std@go1.25.1//os/file.go//Remove + kind: package + confidence: high + site: go:std@go1.25.1//os/file.go:3 + - from: os//exec//Command + to: go:std@go1.25.1//os/exec/exec.go//Command + kind: package + confidence: high + site: go:std@go1.25.1//os/exec/exec.go:5 +unresolved: + - caller: example.com//markup//Client//Close + site: go:example.com/markup@v1.0.0 + reason: interface-receiver + - 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: unknown-receiver + method: Handler +results: + - target: example.com//markup//Writer//Flush + status: found + path: + - key: main.go//main + - key: main.go//promoted + site: main.go:8 + kind: direct + confidence: high + - key: example.com//markup//Writer//Flush + site: main.go:25 + kind: direct + confidence: high + call_sites: + - caller: main.go//promoted + site: main.go:25 + reached: true + - target: os//Remove + status: uncertain + call_sites: + - caller: go:example.com/markup@v1.0.0//client.go//conn//Close + site: go:example.com/markup@v1.0.0//client.go:15 + reached: false + uncertainties: + - kind: unresolved-call + site: go:example.com/markup@v1.0.0 + - kind: unresolved-call + site: go:example.com/markup@v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/go.mod b/test/projectgraph/cases/go/dependency-data-reads/project/go.mod new file mode 100644 index 00000000..b4b143f8 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/go.mod @@ -0,0 +1,5 @@ +module example.com/app + +go 1.22 + +require example.com/markup v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/client.go b/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/client.go new file mode 100644 index 00000000..8e8e2a0a --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/client.go @@ -0,0 +1,41 @@ +package markup + +import ( + "os" + "os/exec" +) + +type Conn interface { + Close() error +} + +type conn struct{} + +func (c *conn) Close() error { + os.Remove("close") + return nil +} + +type Client struct { + Conn + name string +} + +func Dial() *Client { + return &Client{Conn: &conn{}} +} + +type Base struct{} + +func (b *Base) Flush() { + exec.Command("flush") +} + +type Writer struct { + *Base + size int +} + +func NewWriter() *Writer { + return &Writer{Base: &Base{}} +} diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/go.mod b/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/go.mod new file mode 100644 index 00000000..2faaaa0c --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module example.com/markup + +go 1.22 diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/node.go b/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/node.go new file mode 100644 index 00000000..c70a8f49 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/node.go @@ -0,0 +1,41 @@ +package markup + +import ( + "errors" + "os/exec" +) + +type Attribute struct { + Key, Val string +} + +type Node struct { + Data string + Attr []Attribute +} + +const KindText = 1 + +var ErrNoRows = errors.New("no rows") + +var Handler = func(s string) { + exec.Command("handler", s) +} + +var Run = run + +func run(s string) { + exec.Command("run", s) +} + +func Print(v any) { + exec.Command("print") +} + +func Render(n *Node) { + Print(n.Data) + Print(ErrNoRows) + Print(KindText) + Handler("render") + Run("render") +} diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/errors/errors.go b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/errors/errors.go new file mode 100644 index 00000000..c8b5828f --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/errors/errors.go @@ -0,0 +1,9 @@ +package errors + +type errorString struct{ s string } + +func (e *errorString) Error() string { return e.s } + +func New(text string) error { + return &errorString{text} +} diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/go.mod b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/go.mod new file mode 100644 index 00000000..1ded0318 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/go.mod @@ -0,0 +1,3 @@ +module std + +go 1.25 diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/exec/exec.go b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/exec/exec.go new file mode 100644 index 00000000..2e151876 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/exec/exec.go @@ -0,0 +1,7 @@ +package exec + +type Cmd struct{} + +func Command(name string, arg ...string) *Cmd { + return &Cmd{} +} diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/file.go b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/file.go new file mode 100644 index 00000000..50fefb80 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/file.go @@ -0,0 +1,5 @@ +package os + +func Remove(name string) error { + return nil +} diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/main.go b/test/projectgraph/cases/go/dependency-data-reads/project/main.go new file mode 100644 index 00000000..7b7ccc21 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/main.go @@ -0,0 +1,28 @@ +package main + +import "example.com/markup" + +func main() { + read(&markup.Node{}) + call() + promoted() +} + +func read(n *markup.Node) { + markup.Print(n.Data) + markup.Print(markup.ErrNoRows) + markup.Print(markup.KindText) + markup.Render(n) +} + +func call() { + markup.Handler("x") + markup.Run("y") +} + +func promoted() { + w := markup.NewWriter() + w.Flush() + c := markup.Dial() + c.Close() +} 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 new file mode 100644 index 00000000..3853092b --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/summaries/go_example.com_markup_v1.0.0.json @@ -0,0 +1,143 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:example.com/markup@v1.0.0", + "symbols": [ + { + "key": "example.com//markup//Attribute", + "definitions": [ + "go:example.com/markup@v1.0.0//node.go//Attribute" + ], + "reaches": [] + }, + { + "key": "example.com//markup//Base", + "definitions": [ + "go:example.com/markup@v1.0.0//client.go//Base" + ], + "reaches": [] + }, + { + "key": "example.com//markup//Base//Flush", + "definitions": [ + "go:example.com/markup@v1.0.0//client.go//Base//Flush" + ], + "reaches": [ + "os//exec//Command" + ] + }, + { + "key": "example.com//markup//Client", + "definitions": [ + "go:example.com/markup@v1.0.0//client.go//Client" + ], + "reaches": [] + }, + { + "key": "example.com//markup//Conn", + "definitions": [ + "go:example.com/markup@v1.0.0//client.go//Conn" + ], + "reaches": [] + }, + { + "key": "example.com//markup//Dial", + "definitions": [ + "go:example.com/markup@v1.0.0//client.go//Dial" + ], + "reaches": [] + }, + { + "key": "example.com//markup//Handler", + "definitions": [], + "reaches": [], + "uncertainties": [ + 0 + ] + }, + { + "key": "example.com//markup//NewWriter", + "definitions": [ + "go:example.com/markup@v1.0.0//client.go//NewWriter" + ], + "reaches": [] + }, + { + "key": "example.com//markup//Node", + "definitions": [ + "go:example.com/markup@v1.0.0//node.go//Node" + ], + "reaches": [] + }, + { + "key": "example.com//markup//Print", + "definitions": [ + "go:example.com/markup@v1.0.0//node.go//Print" + ], + "reaches": [ + "os//exec//Command" + ] + }, + { + "key": "example.com//markup//Render", + "definitions": [ + "go:example.com/markup@v1.0.0//node.go//Render" + ], + "reaches": [ + "os//exec//Command" + ], + "uncertainties": [ + 1 + ] + }, + { + "key": "example.com//markup//Run", + "definitions": [ + "go:example.com/markup@v1.0.0//node.go//run" + ], + "reaches": [ + "os//exec//Command" + ] + }, + { + "key": "example.com//markup//Writer", + "definitions": [ + "go:example.com/markup@v1.0.0//client.go//Writer" + ], + "reaches": [] + } + ], + "targets": [ + "errors//New", + "os//Remove", + "os//exec//Command" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "key": "example.com//markup//Handler", + "site": { + "path": "go:example.com/markup@v1.0.0" + }, + "text": "example.com//markup//Handler (unknown-receiver)", + "unit": "go:example.com/markup@v1.0.0", + "targets": [ + "errors//New", + "os//Remove", + "os//exec//Command" + ] + }, + { + "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 (unknown-receiver)", + "method": "Handler" + } + ] +} 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 new file mode 100644 index 00000000..5aea1ae8 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/summaries/go_std_go1.25.1.json @@ -0,0 +1,37 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:std@go1.25.1", + "symbols": [ + { + "key": "errors//New", + "definitions": [ + "go:std@go1.25.1//errors/errors.go//New" + ], + "reaches": [] + }, + { + "key": "os//Remove", + "definitions": [ + "go:std@go1.25.1//os/file.go//Remove" + ], + "reaches": [] + }, + { + "key": "os//exec//Cmd", + "definitions": [ + "go:std@go1.25.1//os/exec/exec.go//Cmd" + ], + "reaches": [] + }, + { + "key": "os//exec//Command", + "definitions": [ + "go:std@go1.25.1//os/exec/exec.go//Command" + ], + "reaches": [] + } + ], + "targets": [] +} From eebbeddfd3467cb4ba71dba04f37bcaeef9d4914 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:20:00 +0000 Subject: [PATCH 10/58] fix(projectgraph): bind a Go import whose path has one element (FOU-558) A Go type alias, as type Context = context.Context in golang.org/x/net/context, names its target type as a base. The link step did not resolve that base, so a call of netctx.Context.Done gave a no-definition record. The scope bound the name of an import only when the alias differed from the module. A Go import of context, io or sync has the alias context, io or sync, so it bound no name, and no declared type, embedded type or alias of such a package resolved. The Go profile now binds the alias of every import. The alias base gives the external key context//Context, and a unit of the standard library gives it on. The case go/dependency-data-reads gains the alias. This change also resolves each declared type of a package with a one element path, as io.Reader or context.Context. Without a GOROOT unit such a type is an external key with no package edge, as net/http types already were, so a call on it gives no record. Method measure, Go only, against main: no-definition: function 5 -> 0, method 71 -> 16 not-found: function 0 -> 58, method 0 -> 215 ambiguous: function 106 -> 32, method 416 -> 154 The not-found answers need the GOROOT unit of FOU-561 and the check of FOU-563 before vet trusts them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 7 +- projectgraph/golang.go | 1 + projectgraph/profile.go | 4 + projectgraph/scope.go | 2 +- .../go/dependency-data-reads/expect.yaml | 86 ++++++++++++++----- .../example.com/markup@v1.0.0/netctx/ctx.go | 5 ++ .../project/goroot/src/context/context.go | 18 ++++ .../project/goroot/src/os/exec/exec.go | 4 + .../go/dependency-data-reads/project/main.go | 10 ++- .../go_example.com_markup_v1.0.0.json | 7 ++ .../summaries/go_std_go1.25.1.json | 21 +++++ 11 files changed, 139 insertions(+), 26 deletions(-) create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/netctx/ctx.go create mode 100644 test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/context/context.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index d3fbb8fe..f33e7828 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -273,7 +273,12 @@ embedded interface declares, as `Close` of `ssh.Client` that embeds the interface `Conn`, runs on the value that the field holds, as a call on a value of the interface does. The walk of an external key into a unit that ends at such a method, or at a method of an interface of the unit, is a -record with the reason `interface-receiver`. +record with the reason `interface-receiver`. A type alias, as +`type Context = context.Context`, names its target as a base, so a method +of the alias is a method of the target type. An import of a package whose +path has one element, as `context`, binds its name, so the base resolves +to the external key `context//Context`, and a unit of the standard library +gives it on. A `pypi:` unit is a distribution, as a site-packages directory or an unpacked wheel holds it. These rules apply to it. diff --git a/projectgraph/golang.go b/projectgraph/golang.go index 44199e4d..5f3a0c2b 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -29,6 +29,7 @@ var goProfile = profile{ typeConversions: true, scopeWildcards: true, mainLoadsPackage: true, + importsBindAlias: true, } // goProvide returns the package unit of a Go module that an import path diff --git a/projectgraph/profile.go b/projectgraph/profile.go index e5e5c2fb..70093ddb 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -180,6 +180,10 @@ type profile struct { // with no item and no alias binds the first segment, as os for import // os.path in Python. importBindsRoot bool + // importsBindAlias marks a language where each import binds its alias, + // also an alias that is the whole module, as context for import + // "context" in Go. + importsBindAlias bool // moduleNames marks a language where a module gives on each name that // it binds, imports too, unless it lists its exports, and where a // package gives on its submodules, as Python. diff --git a/projectgraph/scope.go b/projectgraph/scope.go index 20257167..b3bf1edc 100644 --- a/projectgraph/scope.go +++ b/projectgraph/scope.go @@ -122,7 +122,7 @@ func bindingName(lang langcode.Code, imp fileindex.Import) string { switch { case imp.Item != "": return lastDotted(cmp.Or(imp.Alias, imp.Item)) - case imp.Alias != "" && imp.Alias != imp.Module: + case imp.Alias != "" && (imp.Alias != imp.Module || profileOf(lang).importsBindAlias): return imp.Alias case profileOf(lang).importBindsRoot: return firstDotted(imp.Module) diff --git a/test/projectgraph/cases/go/dependency-data-reads/expect.yaml b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml index 34065c31..16c9cef5 100644 --- a/test/projectgraph/cases/go/dependency-data-reads/expect.yaml +++ b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml @@ -2,13 +2,15 @@ source: safedep:hand description: >- The unit markup has a struct Node with the field Data, a constant KindText, a variable ErrNoRows that errors.New gives, a variable Handler that holds a function literal, a variable Run that holds the function - run, a struct Writer that embeds the struct Base, a struct Client that embeds the interface Conn. A read of n.Data, KindText or ErrNoRows + run, a struct Writer that embeds the struct Base, a struct Client that embeds the interface Conn, and in + netctx the alias Context of context.Context of the standard library. A read of n.Data, KindText or ErrNoRows runs no code, so the walk into the unit gives no package edge and no record. The reference edges of main end at the external keys. A call of Run gets a package edge to run. A call of Handler keeps an unknown-receiver record, in main and in Render, because the index does not know the function that the variable holds. Writer.Flush is the promoted method Base.Flush. Client.Close is the method Close that the embedded interface - Conn declares. A call of it runs the code of the type that the interface value holds, so it keeps an - interface-receiver record, and os.Remove, which only conn.Close calls, is uncertain, not found. + Conn declares, and netctx.Context.Done is the method Done of the interface context.Context of the unit of + the standard library. A call of such a method runs the code of the type that the interface value holds, so + each keeps an interface-receiver record, and a target that only that code reaches is uncertain, not found. units: gomod/example.com/markup@v1.0.0: go:example.com/markup@v1.0.0 goroot/src: go:std@go1.25.1 @@ -56,6 +58,11 @@ edges: kind: package confidence: high site: go:example.com/markup@v1.0.0//client.go:30 + - from: example.com//markup//netctx//Context//Done + to: context//Context//Done + kind: package + confidence: high + site: go:example.com/markup@v1.0.0 - from: go:example.com/markup@v1.0.0//client.go//Base//Flush to: os//exec//Command kind: direct @@ -111,91 +118,106 @@ edges: kind: direct confidence: high site: go:example.com/markup@v1.0.0//node.go:28 + - from: go:std@go1.25.1//context/context.go//emptyCtx//Done + to: go:std@go1.25.1//os/exec/exec.go//LookPath + kind: direct + confidence: high + site: go:std@go1.25.1//context/context.go:12 - from: main.go to: go:example.com/markup@v1.0.0//node.go kind: import confidence: high - site: main.go:3 + site: main.go:4 - from: main.go//call to: example.com//markup//Handler kind: direct confidence: high - site: main.go:19 + site: main.go:23 - from: main.go//call to: example.com//markup//Run kind: direct confidence: high - site: main.go:20 + site: main.go:24 - from: main.go//main to: main.go//call kind: direct confidence: high - site: main.go:7 + site: main.go:10 - from: main.go//main to: main.go//promoted kind: direct confidence: high - site: main.go:8 + site: main.go:11 - from: main.go//main to: main.go//read kind: direct confidence: high - site: main.go:6 + site: main.go:9 + - from: main.go//main + to: main.go//wait + kind: direct + confidence: high + site: main.go:12 - from: main.go//promoted to: example.com//markup//Client//Close kind: direct confidence: high - site: main.go:27 + site: main.go:31 - from: main.go//promoted to: example.com//markup//Dial kind: direct confidence: high - site: main.go:26 + site: main.go:30 - from: main.go//promoted to: example.com//markup//NewWriter kind: direct confidence: high - site: main.go:24 + site: main.go:28 - from: main.go//promoted to: example.com//markup//Writer//Flush kind: direct confidence: high - site: main.go:25 + site: main.go:29 - from: main.go//read to: example.com//markup//ErrNoRows kind: reference confidence: low - site: main.go:13 + site: main.go:17 - from: main.go//read to: example.com//markup//KindText kind: reference confidence: low - site: main.go:14 + site: main.go:18 - from: main.go//read to: example.com//markup//Node//Data kind: reference confidence: low - site: main.go:12 + site: main.go:16 - from: main.go//read to: example.com//markup//Print kind: direct confidence: high - site: main.go:12 + site: main.go:16 - from: main.go//read to: example.com//markup//Print kind: direct confidence: high - site: main.go:13 + site: main.go:17 - from: main.go//read to: example.com//markup//Print kind: direct confidence: high - site: main.go:14 + site: main.go:18 - from: main.go//read to: example.com//markup//Render kind: direct confidence: high - site: main.go:15 + site: main.go:19 + - from: main.go//wait + to: example.com//markup//netctx//Context//Done + kind: direct + confidence: high + site: main.go:35 - from: os//Remove to: go:std@go1.25.1//os/file.go//Remove kind: package @@ -217,23 +239,39 @@ unresolved: site: go:example.com/markup@v1.0.0//node.go:39 reason: unknown-receiver method: Handler + - caller: context//Context//Done + site: go:std@go1.25.1 + reason: interface-receiver results: - target: example.com//markup//Writer//Flush status: found path: - key: main.go//main - key: main.go//promoted - site: main.go:8 + site: main.go:11 kind: direct confidence: high - key: example.com//markup//Writer//Flush - site: main.go:25 + site: main.go:29 kind: direct confidence: high call_sites: - caller: main.go//promoted - site: main.go:25 + site: main.go:29 reached: true + - target: os//exec//LookPath + status: uncertain + call_sites: + - caller: go:std@go1.25.1//context/context.go//emptyCtx//Done + site: go:std@go1.25.1//context/context.go:12 + reached: false + uncertainties: + - kind: unresolved-call + site: go:example.com/markup@v1.0.0 + - kind: unresolved-call + site: go:example.com/markup@v1.0.0 + - kind: unresolved-call + site: go:std@go1.25.1 - target: os//Remove status: uncertain call_sites: @@ -245,3 +283,5 @@ results: site: go:example.com/markup@v1.0.0 - kind: unresolved-call site: go:example.com/markup@v1.0.0 + - kind: unresolved-call + site: go:std@go1.25.1 diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/netctx/ctx.go b/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/netctx/ctx.go new file mode 100644 index 00000000..6566232f --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/gomod/example.com/markup@v1.0.0/netctx/ctx.go @@ -0,0 +1,5 @@ +package netctx + +import "context" + +type Context = context.Context diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/context/context.go b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/context/context.go new file mode 100644 index 00000000..5816638f --- /dev/null +++ b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/context/context.go @@ -0,0 +1,18 @@ +package context + +import "os/exec" + +type Context interface { + Done() <-chan struct{} +} + +type emptyCtx struct{} + +func (emptyCtx) Done() <-chan struct{} { + exec.LookPath("done") + return nil +} + +func Background() Context { + return emptyCtx{} +} diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/exec/exec.go b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/exec/exec.go index 2e151876..f145c2b6 100644 --- a/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/exec/exec.go +++ b/test/projectgraph/cases/go/dependency-data-reads/project/goroot/src/os/exec/exec.go @@ -5,3 +5,7 @@ type Cmd struct{} func Command(name string, arg ...string) *Cmd { return &Cmd{} } + +func LookPath(file string) (string, error) { + return file, nil +} diff --git a/test/projectgraph/cases/go/dependency-data-reads/project/main.go b/test/projectgraph/cases/go/dependency-data-reads/project/main.go index 7b7ccc21..a28afa78 100644 --- a/test/projectgraph/cases/go/dependency-data-reads/project/main.go +++ b/test/projectgraph/cases/go/dependency-data-reads/project/main.go @@ -1,11 +1,15 @@ package main -import "example.com/markup" +import ( + "example.com/markup" + "example.com/markup/netctx" +) func main() { read(&markup.Node{}) call() promoted() + wait(nil) } func read(n *markup.Node) { @@ -26,3 +30,7 @@ func promoted() { c := markup.Dial() c.Close() } + +func wait(ctx netctx.Context) { + ctx.Done() +} 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 3853092b..5fc4941f 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 @@ -106,6 +106,13 @@ "go:example.com/markup@v1.0.0//client.go//Writer" ], "reaches": [] + }, + { + "key": "example.com//markup//netctx//Context", + "definitions": [ + "go:example.com/markup@v1.0.0//netctx/ctx.go//Context" + ], + "reaches": [] } ], "targets": [ 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 5aea1ae8..6499a8ad 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 @@ -4,6 +4,20 @@ "link_version": "17", "unit": "go:std@go1.25.1", "symbols": [ + { + "key": "context//Background", + "definitions": [ + "go:std@go1.25.1//context/context.go//Background" + ], + "reaches": [] + }, + { + "key": "context//Context", + "definitions": [ + "go:std@go1.25.1//context/context.go//Context" + ], + "reaches": [] + }, { "key": "errors//New", "definitions": [ @@ -31,6 +45,13 @@ "go:std@go1.25.1//os/exec/exec.go//Command" ], "reaches": [] + }, + { + "key": "os//exec//LookPath", + "definitions": [ + "go:std@go1.25.1//os/exec/exec.go//LookPath" + ], + "reaches": [] } ], "targets": [] From b8b35812a8e3ed4d60ddc05ca14579cf5e8c88c8 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:23:26 +0000 Subject: [PATCH 11/58] fix(projectgraph): a Go call of a function value names no target (FOU-560) A call of a function value, as BackoffLinearWithJitter(d, j)(attempt) or route.Handlers[0](c), was a computed-name record. Such a record concerns every target, so one site made most targets of a project uncertain. A Go function value is a function that some code uses as a value, a function literal, or a value that reflection gives. The link step now gives such a call the reason function-value, and the reach query applies it to no target. The soundness argument is in docs/projectgraph.md. The change makes each source of a function value reach the search: - The index records a function that a send statement holds as a reference. - A reference to a method that the link step cannot name, as h.Handle on a value of an interface, is a record with the method name. Before, the link step dropped it. - A Go file that imports a package that runs code at load runs code at load too, also when the package is in a unit of dependency code. Before, the search did not reach the init functions and the package-level values of a unit that the project imports through another unit. - An external key has an import edge to each module top level of its package that runs code at load, so the search of a summary reaches the package-level values. - A call or a reference of Call, CallSlice, Method or MethodByName in a file that imports reflect is a computed-name record, unless each target resolves outside package reflect. Before, reflect.Value.Call concerned no target at all. The Rust index does not record a function that the tail expression of a block or a match arm holds, so a Rust call of a function value stays a computed-name record. The new cases function-value-call, function-value-reflect and dependency-function-table fail before the change. The reflect case gave not-found before. The Go method measure (PROJECTGRAPH_METHOD_LANGUAGES=go), after FOU-557 and after this change: kind=method computed-name 579 -> 2, no-definition 110 -> 509, unknown-receiver 156 -> 183, no-such-target 9 -> 144, found 82 -> 78, found/low 249 -> 267, not-found 0 -> 0 kind=function computed-name 139 -> 6, no-definition 21 -> 115, no-such-target 1 -> 19, not-found 0 -> 0 Four sftp targets go from found to found/low, because a shorter path now goes through a reference of a package-level value. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 53 +++++++++- fileindex/index.go | 5 + plugin/indexer/golang.go | 1 + projectgraph/golang.go | 15 +++ projectgraph/imports.go | 9 +- projectgraph/link.go | 3 +- projectgraph/packages.go | 17 ++++ projectgraph/profile.go | 10 ++ projectgraph/reach/uncertainty.go | 5 + projectgraph/resolve.go | 6 +- projectgraph/values.go | 96 +++++++++++++++++++ .../go/dependency-function-table/expect.yaml | 80 ++++++++++++++++ .../dependency-function-table/project/go.mod | 5 + .../gomod/github.com/acme/a@v1.0.0/a.go | 7 ++ .../gomod/github.com/acme/a@v1.0.0/go.mod | 5 + .../gomod/github.com/acme/b@v1.0.0/b.go | 24 +++++ .../gomod/github.com/acme/b@v1.0.0/go.mod | 3 + .../dependency-function-table/project/main.go | 7 ++ .../go_github.com_acme_a_v1.0.0.json | 20 ++++ .../go_github.com_acme_b_v1.0.0.json | 23 +++++ .../cases/go/function-value-call/expect.yaml | 91 ++++++++++++++++++ .../project/backoff/backoff.go | 10 ++ .../go/function-value-call/project/go.mod | 3 + .../go/function-value-call/project/main.go | 38 ++++++++ .../go/function-value-reflect/expect.yaml | 52 ++++++++++ .../go/function-value-reflect/project/go.mod | 3 + .../go/function-value-reflect/project/main.go | 28 ++++++ 27 files changed, 613 insertions(+), 6 deletions(-) create mode 100644 projectgraph/values.go create mode 100644 test/projectgraph/cases/go/dependency-function-table/expect.yaml create mode 100644 test/projectgraph/cases/go/dependency-function-table/project/go.mod create mode 100644 test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/a@v1.0.0/a.go create mode 100644 test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/a@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/b@v1.0.0/b.go create mode 100644 test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/b@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/dependency-function-table/project/main.go create mode 100644 test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_a_v1.0.0.json create mode 100644 test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_b_v1.0.0.json create mode 100644 test/projectgraph/cases/go/function-value-call/expect.yaml create mode 100644 test/projectgraph/cases/go/function-value-call/project/backoff/backoff.go create mode 100644 test/projectgraph/cases/go/function-value-call/project/go.mod create mode 100644 test/projectgraph/cases/go/function-value-call/project/main.go create mode 100644 test/projectgraph/cases/go/function-value-reflect/expect.yaml create mode 100644 test/projectgraph/cases/go/function-value-reflect/project/go.mod create mode 100644 test/projectgraph/cases/go/function-value-reflect/project/main.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 9ac6d39f..89b74886 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -172,8 +172,10 @@ of the highest confidence. A call that the link step cannot resolve is an unresolved site with a reason: `computed-name`, `dynamic-import`, `unknown-receiver`, `no-definition`, `ambiguous`, `limit`, `interface-receiver`, -`trait-receiver` or `no-dependency-source`. The site keeps the method name of the call, so the -reach query can tell which targets it concerns. +`trait-receiver`, `no-dependency-source` or `function-value`. The site keeps the method name of the call, so the +reach query can tell which targets it concerns. A `function-value` +record concerns no target. [A call of a function value](#a-call-of-a-function-value) +gives the reason. A site with more than 20 candidates of its method name, as `render` in a Django project, is `ambiguous`. The site gets no edge, so the search cannot @@ -558,3 +560,50 @@ record. A summary keeps every uncertainty of a symbol in a table, with the targets that its scope concerns, so `Status` applies the same rule with no graph. + +### A call of a function value + +A Go call of a function value, as `BackoffLinearWithJitter(d, j)(attempt)` +or `route.Handlers[0](c)`, is a record with the reason `function-value`. +The record concerns no target. This is sound because the search reaches +each function that such 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 + 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 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. +- A method value whose method the link step cannot name, as `h.Handle` on a + value of an interface. The link step records the reference with the + method name. That record concerns the targets that a method of the name + reaches, as a call `h.Handle()` does. +- A function literal. The index gives the calls of the literal to the + definition or the module top level that holds it. So a function that + returns a literal reaches the calls of the literal. +- A package-level value, as a map of handlers. Its references start at the + module top level of its file. The program loads each package that it + imports before any code of the package runs. So a file that imports a + package that runs code at load runs code at load too, also when the + package is in a unit of dependency code. An import edge joins the + importer to the module top level, and a search from `main` reaches it. + An external key also has an import edge to each module top level of its + package that runs code at load, so the search of a summary reaches it. +- A value that reflection gives, as `reflect.Value.Call`, + `reflect.Value.MethodByName` or `reflect.Value.Method`. No reference + leads to such a function. A call or a reference of the methods `Call`, + `CallSlice`, `Method` or `MethodByName` in a file that imports `reflect` + gets a `computed-name` record, which concerns every target. The link step + does not always know that the receiver is a `reflect.Value`, so the rule + takes any receiver. + +A function value that code outside the graph makes runs code outside the +graph, which the search does not see for any call. + +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 +block, a match arm, a shorthand field or a tuple holds, and a Rust function +returns its tail expression. So a call of a Rust function value stays a +`computed-name` record. diff --git a/fileindex/index.go b/fileindex/index.go index 0476dfdc..cdc7b9b3 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -98,6 +98,11 @@ const ( // from a package with no code in the graph, as the result of a // function of a module that no unit gives. 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 + // joins to the code that holds them. The link step gives the reason + // in place of computed-name, and the record names no target. + UnresolvedFunctionValue UnresolvedReason = "function-value" ) type FlagKind string diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 01b7d88d..4ac699ac 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -31,6 +31,7 @@ var goRules = languageRules{ "var_spec": "value", "return_statement": "", "literal_value": "", + "send_statement": "value", }, member: "selector_expression", object: "operand", diff --git a/projectgraph/golang.go b/projectgraph/golang.go index c6ccc4eb..24904e3b 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -29,6 +29,21 @@ var goProfile = profile{ typeConversions: true, scopeWildcards: true, mainLoadsPackage: true, + functionValues: true, + reflective: goReflective, +} + +// goReflectMethods are the methods of package reflect that call a function +// value or give a method by its name or its index. Each one can run any +// function of the program. +var goReflectMethods = map[string]bool{"Call": true, "CallSlice": true, "Method": true, "MethodByName": true} + +// goReflective reports whether a call or a reference of the method in a Go +// file that imports reflect can run a function that reflect gives. The +// link step does not always know that the receiver is a reflect.Value, as +// when no unit gives the standard library. So any receiver counts. +func goReflective(ix *fileindex.Index, method string) bool { + return goReflectMethods[method] && slices.ContainsFunc(ix.Imports, func(imp fileindex.Import) bool { return imp.Module == "reflect" }) } // goProvide returns the package unit of a Go module that an import path diff --git a/projectgraph/imports.go b/projectgraph/imports.go index 58ece9cf..2d386ed2 100644 --- a/projectgraph/imports.go +++ b/projectgraph/imports.go @@ -134,7 +134,11 @@ func (b *builder) moduleFiles(mod string) []string { // runsAtLoad reports whether the module top level of a file runs code: an // init function, a call, a reference, an unresolved site, a flag, or an -// import of a package of the project that runs code at load. +// import of a package that runs code at load. The package can be in the +// project or in a unit of dependency code. A Go program loads each package +// that it imports, also through a package that runs no code of its own, and +// the reference edges of a package-level value start at the module top +// level. A call of a function value finds its function only through them. func (b *builder) runsAtLoad(ix *fileindex.Index) bool { if runs, ok := b.loads[ix.Path]; ok { return runs @@ -148,7 +152,8 @@ func (b *builder) runsAtLoad(ix *fileindex.Index) bool { slices.ContainsFunc(ix.Unresolved, func(u fileindex.Unresolved) bool { return atTop(u.Caller) }) || len(b.g.Nodes[ix.Path].Flags) > 0 || slices.ContainsFunc(ix.Imports, func(imp fileindex.Import) bool { - return slices.ContainsFunc(b.resolveModules(ix, imp.Module, formOf(imp)), func(m moduleRef) bool { return len(b.moduleFiles(m.key)) > 0 }) + mods, _ := b.importedModules(ix, imp.Module, formOf(imp)) + return slices.ContainsFunc(mods, func(m moduleRef) bool { return len(b.moduleFiles(m.key)) > 0 }) }) b.loads[ix.Path] = runs return runs diff --git a/projectgraph/link.go b/projectgraph/link.go index 048b02e0..8e8a1674 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -370,6 +370,7 @@ func (b *builder) linkAll(ctx context.Context) error { b.linkReferences(ix) b.linkDecorators(ix) b.copyUnresolved(ix) + b.linkReflection(ix) } if b.opts.Surface { b.addSurfaces() @@ -444,7 +445,7 @@ func (b *builder) copyUnresolved(ix *fileindex.Index) { Text: u.Text, Receiver: u.Receiver, Method: u.Method, - Reason: u.Reason, + Reason: functionValue(profileOf(ix.Language), u.Reason), } if u.Reason == fileindex.UnresolvedDynamicImport { site.ImportPrefix = b.importPrefix(ix, u.Line) diff --git a/projectgraph/packages.go b/projectgraph/packages.go index b20d9570..85c0c905 100644 --- a/projectgraph/packages.go +++ b/projectgraph/packages.go @@ -437,6 +437,23 @@ func (b *builder) linkProvision(key string, o origin, pv provision) { return } 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) +} + +// 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 +// function values the link step joins to the code that holds them. The +// program loads a package before a symbol of it runs. A search that enters +// the package by the key, as the search of a summary does, then reaches the +// init functions and the package-level values. A call of a function value +// that a package-level value holds finds its function only through them. +func (b *builder) linkPackageLoad(lang langcode.Code, key, module string) { + if !profileOf(lang).functionValues { + return + } + for _, p := range b.moduleFiles(module) { + b.link(lang, key, p, Site{Path: p}, EdgeImport, ruleImport.via("package")) + } } // addSurfaces links an external node to each exported symbol of each unit, diff --git a/projectgraph/profile.go b/projectgraph/profile.go index e5e5c2fb..11d1a8d9 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -197,6 +197,16 @@ type profile struct { // mainLoadsPackage marks a language whose program initializes the // package of main before main runs, as Go. mainLoadsPackage bool + // functionValues marks a language whose function values the link step + // joins to the code that holds them, as Go. A call of a function value + // gets the reason function-value in place of computed-name, and it + // concerns no target. A reference to a method that the link step cannot + // name, as h.Handle on an interface, gets a record with the method name. + functionValues bool + // reflective reports whether a call or a reference of the file, by its + // method name, can run a function that reflection gives, as + // reflect.Value.Call. Such a site gets a computed-name record. + reflective func(ix *fileindex.Index, method string) bool // helperProgram reports whether the main of a file is a helper // program of the project and not the program that its users run, as a // build script or an example of a Rust crate. Its root stays, and it diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index e21e2076..5821640f 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -149,6 +149,11 @@ func (s *sources) at(key string, n *projectgraph.Node) []Uncertainty { out = append(out, u) } for _, r := range s.unresolved[key] { + // A call of a function value runs a function that the search + // reaches through the code that holds the value. + if r.Reason == fileindex.UnresolvedFunctionValue { + continue + } u := Uncertainty{Kind: UnresolvedCall, Key: key, Site: r.Site, Text: fmt.Sprintf("%s (%s)", r.Text, r.Reason)} switch { case r.Unit != "": diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 0e76429a..69e7f998 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -340,7 +340,11 @@ func (b *builder) linkReferences(ix *fileindex.Index) { for _, r := range b.resolveTargets(sc, ref.Caller, ref.Targets) { // A Go value of a named type is no function, so passing it // on calls nothing. - if r.fail != nil || r.unbound || sc.profile.typeConversions && b.isType(r.key) { + if r.fail != nil || r.unbound { + b.linkUnknownValue(sc, ref, from, site, r) + continue + } + if sc.profile.typeConversions && b.isType(r.key) { continue } b.link(ix.Language, from, r.key, site, EdgeReference, ruleReference) diff --git a/projectgraph/values.go b/projectgraph/values.go new file mode 100644 index 00000000..d865c66c --- /dev/null +++ b/projectgraph/values.go @@ -0,0 +1,96 @@ +package projectgraph + +import ( + "cmp" + "slices" + "strings" + + "github.com/safedep/code/fileindex" +) + +// functionValue gives a call of a function value the reason function-value +// in a language whose function values the link step joins to the code +// that holds them. The function that such a call runs is a function that +// some code uses as a value, with a reference edge from that code, or a +// function literal, whose calls belong to the code that holds it. The +// search reaches that code before the value exists, so the record names no +// target. +func functionValue(p *profile, reason fileindex.UnresolvedReason) fileindex.UnresolvedReason { + if p.functionValues && reason == fileindex.UnresolvedComputedName { + return fileindex.UnresolvedFunctionValue + } + return reason +} + +// linkUnknownValue records a reference to a member that the link step +// cannot name, as the method value h.Handle on a value of an interface. The +// code that gets the value can call any method of the name, so the record +// keeps the method name. Without it, a call of the value would concern no +// target, because the reference has no edge. +func (b *builder) linkUnknownValue(sc *scope, ref fileindex.Reference, from string, site Site, r resolution) { + if !sc.profile.functionValues || !strings.Contains(ref.Text, ".") { + return + } + reason := fileindex.UnresolvedUnknownReceiver + if r.fail != nil { + reason = r.fail.reason + } + b.addUnresolved(UnresolvedSite{Caller: from, Site: site, Text: ref.Text, Method: lastWritten(ref.Text), Reason: reason}) +} + +// linkReflection adds a computed-name record at each call and each +// reference of a file that can run a function that reflection gives, as +// reflect.ValueOf(f).Call(nil). Such a call can run any function, and a +// function value that reflection gives has no reference edge. A site whose +// every target resolves to a definition outside package reflect, as +// stream.Method() on a type of the project, gets no record. +func (b *builder) linkReflection(ix *fileindex.Index) { + reflective := profileOf(ix.Language).reflective + if reflective == nil || isDeclarationFile(ix.Path) { + return + } + sc := b.scopes[ix.Path] + for _, call := range ix.Calls { + if call.Kind == fileindex.CallImport || !reflective(ix, cmp.Or(call.Method, lastWritten(call.Callee))) { + continue + } + caller := call.Caller + if b.g.Nodes[caller] == nil { + caller = sc.enclosing(call.Line) + } + if b.resolvesOutsideReflect(sc, caller, call.Targets) { + continue + } + site := Site{Path: ix.Path, Line: call.Line, Column: call.Column} + b.addUnresolved(UnresolvedSite{Caller: b.runner(caller), Site: site, Text: call.Callee, Receiver: call.Receiver, Reason: fileindex.UnresolvedComputedName}) + } + for _, ref := range ix.References { + if !reflective(ix, lastWritten(ref.Text)) || b.resolvesOutsideReflect(sc, ref.Caller, ref.Targets) { + continue + } + site := Site{Path: ix.Path, Line: ref.Line, Column: ref.Column} + b.addUnresolved(UnresolvedSite{Caller: b.runner(ref.Caller), Site: site, Text: ref.Text, Reason: fileindex.UnresolvedComputedName}) + } +} + +// resolvesOutsideReflect reports whether each target of a site resolves to +// a key that is not in package reflect, in the graph or under its external +// key. +func (b *builder) resolvesOutsideReflect(sc *scope, caller string, targets []string) bool { + rs := b.resolveTargets(sc, caller, targets) + return len(rs) > 0 && !slices.ContainsFunc(rs, func(r resolution) bool { + key := r.key + switch { + case r.unbound, r.fail != nil && r.class == "": + return true + case r.fail != nil: + // The declared type of the receiver is known, so the call + // is on a value of that type. + key = r.class + } + if external, _, ok := b.unitSymbol(key); ok { + key = external + } + return fileindex.Unit(key) == "reflect" + }) +} diff --git a/test/projectgraph/cases/go/dependency-function-table/expect.yaml b/test/projectgraph/cases/go/dependency-function-table/expect.yaml new file mode 100644 index 00000000..fcee7d81 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-function-table/expect.yaml @@ -0,0 +1,80 @@ +source: safedep:hand +description: >- + main calls a.F of the unit a, and F calls the function value that b.Get of the unit b returns. Get reads + it from the package-level map table, which holds doA. The program loads b, because a imports it, so it + runs the module top level of b.go, with the init of b and the reference to doA. The import of b makes a.go + run code at load, so main.go reaches a.go and b.go through import edges. So the search finds os.Remove + through the reference to doA, and os/exec.Command through init. The call of the value concerns no + target, and no code uses doB as a value, so os.Getwd is not found. +units: + gomod/github.com/acme/a@v1.0.0: go:github.com/acme/a@v1.0.0 + gomod/github.com/acme/b@v1.0.0: go:github.com/acme/b@v1.0.0 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +no_edges: + - from: go:github.com/acme/b@v1.0.0//b.go + to: go:github.com/acme/b@v1.0.0//b.go//doB +unresolved: + - caller: go:github.com/acme/a@v1.0.0//a.go//F + site: go:github.com/acme/a@v1.0.0//a.go:6 + reason: function-value +results: + - target: os//Remove + status: found + path: + - key: main.go + - key: go:github.com/acme/a@v1.0.0//a.go + site: main.go:3 + kind: import + confidence: high + - key: go:github.com/acme/b@v1.0.0//b.go + site: go:github.com/acme/a@v1.0.0//a.go:3 + kind: import + confidence: high + - key: go:github.com/acme/b@v1.0.0//b.go//doA + site: go:github.com/acme/b@v1.0.0//b.go:8 + kind: reference + confidence: low + - key: os//Remove + site: go:github.com/acme/b@v1.0.0//b.go:15 + kind: direct + confidence: high + call_sites: + - caller: go:github.com/acme/b@v1.0.0//b.go//doA + site: go:github.com/acme/b@v1.0.0//b.go:15 + reached: true + - target: os//exec//Command + status: found + path: + - key: main.go + - key: go:github.com/acme/a@v1.0.0//a.go + site: main.go:3 + kind: import + confidence: high + - key: go:github.com/acme/b@v1.0.0//b.go + site: go:github.com/acme/a@v1.0.0//a.go:3 + kind: import + confidence: high + - key: go:github.com/acme/b@v1.0.0//b.go//init + site: go:github.com/acme/b@v1.0.0//b.go:10 + kind: direct + confidence: high + - key: os//exec//Command + site: go:github.com/acme/b@v1.0.0//b.go:11 + kind: direct + confidence: high + call_sites: + - caller: go:github.com/acme/b@v1.0.0//b.go//init + site: go:github.com/acme/b@v1.0.0//b.go:11 + reached: true + - target: os//Getwd + status: not-found + call_sites: + - caller: go:github.com/acme/b@v1.0.0//b.go//doB + site: go:github.com/acme/b@v1.0.0//b.go:19 + reached: false diff --git a/test/projectgraph/cases/go/dependency-function-table/project/go.mod b/test/projectgraph/cases/go/dependency-function-table/project/go.mod new file mode 100644 index 00000000..22b24241 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-function-table/project/go.mod @@ -0,0 +1,5 @@ +module example.com/app + +go 1.22 + +require github.com/acme/a v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/a@v1.0.0/a.go b/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/a@v1.0.0/a.go new file mode 100644 index 00000000..41b2578b --- /dev/null +++ b/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/a@v1.0.0/a.go @@ -0,0 +1,7 @@ +package a + +import "github.com/acme/b" + +func F() { + b.Get(1)() +} diff --git a/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/a@v1.0.0/go.mod b/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/a@v1.0.0/go.mod new file mode 100644 index 00000000..ec4308c6 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/a@v1.0.0/go.mod @@ -0,0 +1,5 @@ +module github.com/acme/a + +go 1.22 + +require github.com/acme/b v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/b@v1.0.0/b.go b/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/b@v1.0.0/b.go new file mode 100644 index 00000000..424d7249 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/b@v1.0.0/b.go @@ -0,0 +1,24 @@ +package b + +import ( + "os" + "os/exec" +) + +var table = map[int]func(){1: doA} + +func init() { + exec.Command("ls") +} + +func doA() { + os.Remove("x") +} + +func doB() { + os.Getwd() +} + +func Get(i int) func() { + return table[i] +} diff --git a/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/b@v1.0.0/go.mod b/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/b@v1.0.0/go.mod new file mode 100644 index 00000000..c8c2abd5 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-function-table/project/gomod/github.com/acme/b@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module github.com/acme/b + +go 1.22 diff --git a/test/projectgraph/cases/go/dependency-function-table/project/main.go b/test/projectgraph/cases/go/dependency-function-table/project/main.go new file mode 100644 index 00000000..61f1c838 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-function-table/project/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/acme/a" + +func main() { + a.F() +} 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 new file mode 100644 index 00000000..340352e6 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_a_v1.0.0.json @@ -0,0 +1,20 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:github.com/acme/a@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//a//F", + "definitions": [ + "go:github.com/acme/a@v1.0.0//a.go//F" + ], + "reaches": [ + "github.com//acme//b//Get" + ] + } + ], + "targets": [ + "github.com//acme//b//Get" + ] +} 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 new file mode 100644 index 00000000..f603fbe8 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-function-table/summaries/go_github.com_acme_b_v1.0.0.json @@ -0,0 +1,23 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:github.com/acme/b@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//b//Get", + "definitions": [ + "go:github.com/acme/b@v1.0.0//b.go//Get" + ], + "reaches": [ + "os//Remove", + "os//exec//Command" + ] + } + ], + "targets": [ + "os//Getwd", + "os//Remove", + "os//exec//Command" + ] +} diff --git a/test/projectgraph/cases/go/function-value-call/expect.yaml b/test/projectgraph/cases/go/function-value-call/expect.yaml new file mode 100644 index 00000000..664ad35b --- /dev/null +++ b/test/projectgraph/cases/go/function-value-call/expect.yaml @@ -0,0 +1,91 @@ +source: safedep:hand +description: >- + main calls the result of backoff.Linear, a function literal that calls os.Getenv. The index gives the + calls of the literal to Linear, so the path to os.Getenv goes through Linear. main calls fns[0], which + holds the method value h.Handle on a value of the interface handler, and more[0], which holds run from a + channel. A call of a function value runs a function that some code uses as a value, so each such call is + a function-value record that concerns no target. The send of run is a reference, so os.Getwd is found. + The link step cannot name the method of h.Handle, so the reference is a record with the method name + Handle. The record gets a name edge to local.Handle, so os/exec.LookPath is found. No code uses unused as + a value, so os.Remove is not found. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +no_edges: + - from: main.go//main + to: main.go//unused + - from: main.go//main + to: os//Remove +unresolved: + - caller: main.go//main + site: main.go:29 + reason: function-value + - caller: main.go//main + site: main.go:31 + reason: no-definition + method: Handle + - caller: main.go//main + site: main.go:32 + reason: function-value + - caller: main.go//main + site: main.go:37 + reason: function-value +results: + - target: os//Getenv + status: found + path: + - key: main.go//main + - key: backoff/backoff.go//Linear + site: main.go:29 + kind: direct + confidence: high + - key: os//Getenv + site: backoff/backoff.go:7 + kind: direct + confidence: high + call_sites: + - caller: backoff/backoff.go//Linear + site: backoff/backoff.go:7 + reached: true + - target: os//Getwd + status: found + path: + - key: main.go//main + - key: main.go//run + site: main.go:34 + kind: reference + confidence: low + - key: os//Getwd + site: main.go:21 + kind: direct + confidence: high + call_sites: + - caller: main.go//run + site: main.go:21 + reached: true + - target: os//exec//LookPath + status: found + path: + - key: main.go//main + - key: main.go//local//Handle + site: main.go:31 + kind: name + confidence: low + - key: os//exec//LookPath + site: main.go:17 + kind: direct + confidence: high + call_sites: + - caller: main.go//local//Handle + site: main.go:17 + reached: true + - target: os//Remove + status: not-found + call_sites: + - caller: main.go//unused + site: main.go:25 + reached: false diff --git a/test/projectgraph/cases/go/function-value-call/project/backoff/backoff.go b/test/projectgraph/cases/go/function-value-call/project/backoff/backoff.go new file mode 100644 index 00000000..79c15347 --- /dev/null +++ b/test/projectgraph/cases/go/function-value-call/project/backoff/backoff.go @@ -0,0 +1,10 @@ +package backoff + +import "os" + +func Linear(d int) func(int) int { + return func(attempt int) int { + os.Getenv("JITTER") + return attempt * d + } +} diff --git a/test/projectgraph/cases/go/function-value-call/project/go.mod b/test/projectgraph/cases/go/function-value-call/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/function-value-call/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/function-value-call/project/main.go b/test/projectgraph/cases/go/function-value-call/project/main.go new file mode 100644 index 00000000..88414942 --- /dev/null +++ b/test/projectgraph/cases/go/function-value-call/project/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "os" + "os/exec" + + "example.com/app/backoff" +) + +type handler interface { + Handle() +} + +type local struct{} + +func (local) Handle() { + exec.LookPath("ls") +} + +func run() { + os.Getwd() +} + +func unused() { + os.Remove("x") +} + +func main() { + backoff.Linear(2)(3) + var h handler = local{} + fns := []func(){h.Handle} + fns[0]() + ch := make(chan func(), 1) + ch <- run + more := make([]func(), 1) + more[0] = <-ch + more[0]() +} diff --git a/test/projectgraph/cases/go/function-value-reflect/expect.yaml b/test/projectgraph/cases/go/function-value-reflect/expect.yaml new file mode 100644 index 00000000..05bd1c74 --- /dev/null +++ b/test/projectgraph/cases/go/function-value-reflect/expect.yaml @@ -0,0 +1,52 @@ +source: safedep:hand +description: >- + main looks up the method Hidden of T by its name with reflect and calls it. No edge and no reference + leads to Hidden, and a call through reflect can run any function. So the calls of MethodByName and of + Call are computed-name records, and os.Remove, which Hidden calls, and os/exec.Command, which the uncalled + other calls, are uncertain. The call r.Method() resolves to the method of R, which is not in reflect, so + it gets no record. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: main.go//main + site: main.go:25 + reason: computed-name + - caller: main.go//main + site: main.go:25 + reason: unknown-receiver + method: MethodByName + - caller: main.go//main + site: main.go:25 + reason: computed-name + - caller: main.go//main + site: main.go:25 + reason: unknown-receiver + method: Call +results: + - target: os//Remove + status: uncertain + call_sites: + - caller: main.go//T//Hidden + site: main.go:12 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:25 + - kind: unresolved-call + site: main.go:25 + - target: os//exec//Command + status: uncertain + call_sites: + - caller: main.go//other + site: main.go:20 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:25 + - kind: unresolved-call + site: main.go:25 diff --git a/test/projectgraph/cases/go/function-value-reflect/project/go.mod b/test/projectgraph/cases/go/function-value-reflect/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/function-value-reflect/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/function-value-reflect/project/main.go b/test/projectgraph/cases/go/function-value-reflect/project/main.go new file mode 100644 index 00000000..9c02a776 --- /dev/null +++ b/test/projectgraph/cases/go/function-value-reflect/project/main.go @@ -0,0 +1,28 @@ +package main + +import ( + "os" + "os/exec" + "reflect" +) + +type T struct{} + +func (T) Hidden() { + os.Remove("x") +} + +type R struct{} + +func (R) Method() {} + +func other() { + exec.Command("ls") +} + +func main() { + v := reflect.ValueOf(T{}) + v.MethodByName("Hidden").Call(nil) + var r R + r.Method() +} From f5d49c79d2443c9ef09c06e3cc5912c2f9fe5e53 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:23:36 +0000 Subject: [PATCH 12/58] docs(projectgraph): keep the unknown code of a unit global, with guard cases (FOU-559) FOU-559 asks to scope a failed or truncated file of a unit, and a record with no method name in the code of a unit, by the code that the unit reaches (unitReaches). This commit does not make that change, because the argument does not hold: - A failed file has no index, so its imports are not known. The closure of the edges of the unit, and the import closure of its packages, read the same indexed files, so neither bounds the packages that the failed file can call. Only the manifest of the unit bounds them, and the link step does not record it. - The project can give the unit a value of an interface. Unknown code of the unit can call a method of that value, and the method can call any code. Known code of the unit records such a call with its method name, which concerns every target of the name. Unknown code can hold such a call for any name. docs/projectgraph.md now holds the argument. Two cases guard the rules: - go/dependency-callback: the project gives the unit a named function, a function literal, a value of an interface and a method value of an interface. Each target that a callback reaches is found or uncertain. The case fails on the code before FOU-560: fs[0]() was a computed-name record then, and the method value c.Close had no record. - go/dependency-failed-callback: a failed file of a unit calls a method of a value that the project gives. The target stays uncertain only through the budget of the failed file, so a scope by the unit would make it not-found. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 28 ++++++ .../cases/go/dependency-callback/expect.yaml | 97 +++++++++++++++++++ .../go/dependency-callback/project/go.mod | 5 + .../gomod/github.com/acme/srv@v1.0.0/go.mod | 3 + .../gomod/github.com/acme/srv@v1.0.0/srv.go | 25 +++++ .../go/dependency-callback/project/main.go | 34 +++++++ .../go_github.com_acme_srv_v1.0.0.json | 95 ++++++++++++++++++ .../go/dependency-failed-callback/expect.yaml | 35 +++++++ .../dependency-failed-callback/project/go.mod | 5 + .../gomod/github.com/acme/srv@v1.0.0/big.go | 18 ++++ .../gomod/github.com/acme/srv@v1.0.0/go.mod | 3 + .../gomod/github.com/acme/srv@v1.0.0/srv.go | 9 ++ .../project/main.go | 17 ++++ .../go_github.com_acme_srv_v1.0.0.json | 54 +++++++++++ 14 files changed, 428 insertions(+) create mode 100644 test/projectgraph/cases/go/dependency-callback/expect.yaml create mode 100644 test/projectgraph/cases/go/dependency-callback/project/go.mod create mode 100644 test/projectgraph/cases/go/dependency-callback/project/gomod/github.com/acme/srv@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/dependency-callback/project/gomod/github.com/acme/srv@v1.0.0/srv.go create mode 100644 test/projectgraph/cases/go/dependency-callback/project/main.go create mode 100644 test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json create mode 100644 test/projectgraph/cases/go/dependency-failed-callback/expect.yaml create mode 100644 test/projectgraph/cases/go/dependency-failed-callback/project/go.mod create mode 100644 test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/big.go create mode 100644 test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/srv.go create mode 100644 test/projectgraph/cases/go/dependency-failed-callback/project/main.go create mode 100644 test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 89b74886..0faa7222 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -607,3 +607,31 @@ array names. It does not record a function that the tail expression of a block, a match arm, a shorthand field or a tuple holds, and a Rust function returns its tail expression. So a call of a Rust function value stays a `computed-name` record. + +### Unknown code of a unit of dependency code + +A failed or truncated file of a unit of dependency code counts for every +target when the search enters the unit. An unresolved site in the code of a +unit with no method name, as an import that no file of the unit gives, +counts for every target too. The closure of the edges of the unit, which +`unitReaches` reads, does not bound such code, for two reasons. + +- The unknown code can call a package that no known code of the unit + calls. A failed file has no index, so its imports are not known. The + imports of the other files of the unit do not bound them, and neither does + the closure of the imports of the packages of the unit, because the + import closure reads the same files. Only the manifest of the unit, as the + `require` list of a `go.mod`, bounds them. The link step does not record + it. +- The project can give the unit a value of an interface, as a handler. The + unknown code can call a method of that value, and the method can call any + code of the program. A function value has a reference edge from the code + of the project that holds it, so the search reaches the function without + the unit. A value of an interface has no such edge. Known code of the unit + records such a call with its method name, and the record concerns every + target of the name. The unknown code can hold such a call for any name. + +So a scope narrower than every target can make a target `not-found` that +the program reaches. The case `go/dependency-failed-callback` shows it: +`os/exec.LookPath` stays uncertain only through the budget of the failed +file. diff --git a/test/projectgraph/cases/go/dependency-callback/expect.yaml b/test/projectgraph/cases/go/dependency-callback/expect.yaml new file mode 100644 index 00000000..de3c65b1 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-callback/expect.yaml @@ -0,0 +1,97 @@ +source: safedep:hand +description: >- + main gives code of the project to the unit srv in four ways. Run calls the named function named, which + main passes as a value. Each calls a function literal that main passes in a slice. Serve calls Handle on + a value of the interface Handler, and Bind calls the method value c.Close on a value of the interface + Closer. A function that main passes is a reference of main, and the calls of a literal belong to main, so + os.Getwd and os.Getenv are found. The call fs[0]() in Each is a function-value record that concerns no + target. The call h.Handle() in Serve has no implementation in the graph, so it is an interface-receiver + record with the method Handle, and os/exec.LookPath, which mine.Handle calls, is uncertain. The link step + cannot name the method of c.Close, so the reference is a record with the method Close, which gets a name + edge to conn.Close. So os.Chdir is found. No target that a callback reaches is not found. +units: + gomod/github.com/acme/srv@v1.0.0: go:github.com/acme/srv@v1.0.0 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +no_edges: + - from: main.go//main + to: main.go//mine//Handle +unresolved: + - 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 + method: f + - caller: go:github.com/acme/srv@v1.0.0//srv.go//Each + site: go:github.com/acme/srv@v1.0.0//srv.go:16 + 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: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: unknown-receiver + method: f + - caller: main.go//main + site: main.go:33 + reason: no-definition + method: Close +results: + - target: os//Getwd + status: found + path: + - key: main.go//main + - key: main.go//named + site: main.go:27 + kind: reference + confidence: low + - key: os//Getwd + site: main.go:23 + kind: direct + confidence: high + call_sites: + - caller: main.go//named + site: main.go:23 + reached: true + - target: os//Getenv + status: found + path: + - key: main.go//main + - key: os//Getenv + site: main.go:29 + kind: direct + confidence: high + call_sites: + - caller: main.go//main + site: main.go:29 + reached: true + - target: os//exec//LookPath + status: uncertain + call_sites: + - caller: main.go//mine//Handle + site: main.go:13 + reached: false + uncertainties: + - kind: unresolved-call + site: go:github.com/acme/srv@v1.0.0//srv.go:20 + - target: os//Chdir + status: found + path: + - key: main.go//main + - key: main.go//conn//Close + site: main.go:33 + kind: name + confidence: low + - key: os//Chdir + site: main.go:19 + kind: direct + confidence: high + call_sites: + - caller: main.go//conn//Close + site: main.go:19 + reached: true diff --git a/test/projectgraph/cases/go/dependency-callback/project/go.mod b/test/projectgraph/cases/go/dependency-callback/project/go.mod new file mode 100644 index 00000000..fc120b93 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-callback/project/go.mod @@ -0,0 +1,5 @@ +module example.com/app + +go 1.22 + +require github.com/acme/srv v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-callback/project/gomod/github.com/acme/srv@v1.0.0/go.mod b/test/projectgraph/cases/go/dependency-callback/project/gomod/github.com/acme/srv@v1.0.0/go.mod new file mode 100644 index 00000000..35c75b45 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-callback/project/gomod/github.com/acme/srv@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module github.com/acme/srv + +go 1.22 diff --git a/test/projectgraph/cases/go/dependency-callback/project/gomod/github.com/acme/srv@v1.0.0/srv.go b/test/projectgraph/cases/go/dependency-callback/project/gomod/github.com/acme/srv@v1.0.0/srv.go new file mode 100644 index 00000000..d03dfd9c --- /dev/null +++ b/test/projectgraph/cases/go/dependency-callback/project/gomod/github.com/acme/srv@v1.0.0/srv.go @@ -0,0 +1,25 @@ +package srv + +type Handler interface { + Handle() +} + +type Closer interface { + Close() +} + +func Run(f func()) { + f() +} + +func Each(fs []func()) { + fs[0]() +} + +func Serve(h Handler) { + h.Handle() +} + +func Bind(f func()) { + f() +} diff --git a/test/projectgraph/cases/go/dependency-callback/project/main.go b/test/projectgraph/cases/go/dependency-callback/project/main.go new file mode 100644 index 00000000..6dc1a926 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-callback/project/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "os" + "os/exec" + + "github.com/acme/srv" +) + +type mine struct{} + +func (mine) Handle() { + exec.LookPath("ls") +} + +type conn struct{} + +func (conn) Close() { + os.Chdir("/") +} + +func named() { + os.Getwd() +} + +func main() { + srv.Run(named) + srv.Each([]func(){func() { + os.Getenv("X") + }}) + srv.Serve(mine{}) + var c srv.Closer = conn{} + srv.Bind(c.Close) +} 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 new file mode 100644 index 00000000..62d26d28 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-callback/summaries/go_github.com_acme_srv_v1.0.0.json @@ -0,0 +1,95 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:github.com/acme/srv@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//srv//Bind", + "definitions": [ + "go:github.com/acme/srv@v1.0.0//srv.go//Bind" + ], + "reaches": [], + "uncertainties": [ + 0 + ] + }, + { + "key": "github.com//acme//srv//Closer", + "definitions": [ + "go:github.com/acme/srv@v1.0.0//srv.go//Closer" + ], + "reaches": [] + }, + { + "key": "github.com//acme//srv//Each", + "definitions": [ + "go:github.com/acme/srv@v1.0.0//srv.go//Each" + ], + "reaches": [] + }, + { + "key": "github.com//acme//srv//Handler", + "definitions": [ + "go:github.com/acme/srv@v1.0.0//srv.go//Handler" + ], + "reaches": [] + }, + { + "key": "github.com//acme//srv//Run", + "definitions": [ + "go:github.com/acme/srv@v1.0.0//srv.go//Run" + ], + "reaches": [], + "uncertainties": [ + 1 + ] + }, + { + "key": "github.com//acme//srv//Serve", + "definitions": [ + "go:github.com/acme/srv@v1.0.0//srv.go//Serve" + ], + "reaches": [], + "uncertainties": [ + 2 + ] + } + ], + "targets": [], + "uncertainties": [ + { + "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 (unknown-receiver)", + "method": "f" + }, + { + "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 (unknown-receiver)", + "method": "f" + }, + { + "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": 20, + "column": 2 + }, + "text": "h.Handle (interface-receiver)", + "method": "Handle" + } + ] +} diff --git a/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml b/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml new file mode 100644 index 00000000..22faff8f --- /dev/null +++ b/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml @@ -0,0 +1,35 @@ +source: safedep:hand +description: >- + main passes a value of mine to Serve of the unit srv. Serve calls serve, which big.go defines. big.go is + over the size limit, so its index fails, and the graph does not see that serve calls Handle on the value. + mine.Handle calls os/exec.LookPath, which no code of srv reaches by an edge. A failed file of a unit can + call a method of a value that the project gives to the unit, so its budget concerns every target where + the search enters the unit, and os/exec.LookPath is uncertain. A scope by the code that the unit reaches + would make it not found. +units: + gomod/github.com/acme/srv@v1.0.0: go:github.com/acme/srv@v1.0.0 +max_file_bytes: 400 +failed: + - go:github.com/acme/srv@v1.0.0//big.go +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - 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 + method: serve +results: + - target: os//exec//LookPath + status: uncertain + call_sites: + - caller: main.go//mine//Handle + site: main.go:12 + reached: false + uncertainties: + - kind: budget + site: go:github.com/acme/srv@v1.0.0//big.go diff --git a/test/projectgraph/cases/go/dependency-failed-callback/project/go.mod b/test/projectgraph/cases/go/dependency-failed-callback/project/go.mod new file mode 100644 index 00000000..fc120b93 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-failed-callback/project/go.mod @@ -0,0 +1,5 @@ +module example.com/app + +go 1.22 + +require github.com/acme/srv v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/big.go b/test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/big.go new file mode 100644 index 00000000..09c66dc8 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/big.go @@ -0,0 +1,18 @@ +package srv + +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. +// This line makes the file larger than the size limit of the index step. + +func serve(h Handler) { + h.Handle() +} diff --git a/test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/go.mod b/test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/go.mod new file mode 100644 index 00000000..35c75b45 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module github.com/acme/srv + +go 1.22 diff --git a/test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/srv.go b/test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/srv.go new file mode 100644 index 00000000..286e05c7 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-failed-callback/project/gomod/github.com/acme/srv@v1.0.0/srv.go @@ -0,0 +1,9 @@ +package srv + +type Handler interface { + Handle() +} + +func Serve(h Handler) { + serve(h) +} diff --git a/test/projectgraph/cases/go/dependency-failed-callback/project/main.go b/test/projectgraph/cases/go/dependency-failed-callback/project/main.go new file mode 100644 index 00000000..3e65cef0 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-failed-callback/project/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "os/exec" + + "github.com/acme/srv" +) + +type mine struct{} + +func (mine) Handle() { + exec.LookPath("ls") +} + +func main() { + srv.Serve(mine{}) +} 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 new file mode 100644 index 00000000..256fb42e --- /dev/null +++ b/test/projectgraph/cases/go/dependency-failed-callback/summaries/go_github.com_acme_srv_v1.0.0.json @@ -0,0 +1,54 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:github.com/acme/srv@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//srv//Handler", + "definitions": [ + "go:github.com/acme/srv@v1.0.0//srv.go//Handler" + ], + "reaches": [], + "uncertainties": [ + 0 + ] + }, + { + "key": "github.com//acme//srv//Serve", + "definitions": [ + "go:github.com/acme/srv@v1.0.0//srv.go//Serve" + ], + "reaches": [], + "uncertainties": [ + 0, + 1 + ] + } + ], + "targets": [], + "uncertainties": [ + { + "kind": "budget", + "key": "go:github.com/acme/srv@v1.0.0//big.go", + "site": { + "path": "go:github.com/acme/srv@v1.0.0//big.go" + }, + "text": "the file is failed" + }, + { + "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 (unknown-receiver)", + "method": "serve" + } + ], + "incomplete": [ + "go:github.com/acme/srv@v1.0.0//big.go" + ] +} From 050c6342b373981d2136e3f6fc76f2980925c349 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:27:26 +0000 Subject: [PATCH 13/58] test(projectgraph): update two Go cases for the merge of FOU-558 and FOU-560 The two changes meet in two cases. FOU-558 indexes package variables, so node.go of dependency-data-reads runs code at load (errors.New). The load rule of FOU-560 then adds an import edge from each external key of the package to node.go, and the summaries of the unit reach errors.New and the calls in the func literal of Handler. The literal does not run at load, so this reach is wider than the truth. It can add a reach and never hide one. FOU-558 binds an import with one path element, as reflect. A chained call on reflect.ValueOf then has a callee package with no unit, so its record reads no-dependency-source in place of unknown-receiver. The record stays, and the computed-name records of the reflect rule stay too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- .../go/dependency-data-reads/expect.yaml | 30 +++++++++++++ .../go_example.com_markup_v1.0.0.json | 44 +++++++++++++++---- .../go/function-value-reflect/expect.yaml | 4 +- 3 files changed, 68 insertions(+), 10 deletions(-) diff --git a/test/projectgraph/cases/go/dependency-data-reads/expect.yaml b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml index 16c9cef5..887cbb70 100644 --- a/test/projectgraph/cases/go/dependency-data-reads/expect.yaml +++ b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml @@ -228,6 +228,36 @@ edges: kind: package confidence: high site: go:std@go1.25.1//os/exec/exec.go:5 + - from: example.com//markup//Dial + to: go:example.com/markup@v1.0.0//node.go + kind: import + confidence: high + site: go:example.com/markup@v1.0.0//node.go + - from: example.com//markup//NewWriter + to: go:example.com/markup@v1.0.0//node.go + kind: import + confidence: high + site: go:example.com/markup@v1.0.0//node.go + - from: example.com//markup//Print + to: go:example.com/markup@v1.0.0//node.go + kind: import + confidence: high + site: go:example.com/markup@v1.0.0//node.go + - from: example.com//markup//Render + to: go:example.com/markup@v1.0.0//node.go + kind: import + confidence: high + site: go:example.com/markup@v1.0.0//node.go + - from: example.com//markup//Run + to: go:example.com/markup@v1.0.0//node.go + kind: import + confidence: high + site: go:example.com/markup@v1.0.0//node.go + - from: example.com//markup//Writer//Flush + to: go:example.com/markup@v1.0.0//node.go + kind: import + confidence: high + site: go:example.com/markup@v1.0.0//node.go unresolved: - caller: example.com//markup//Client//Close site: go:example.com/markup@v1.0.0 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 5fc4941f..86fb8926 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 @@ -9,14 +9,20 @@ "definitions": [ "go:example.com/markup@v1.0.0//node.go//Attribute" ], - "reaches": [] + "reaches": [ + "errors//New", + "os//exec//Command" + ] }, { "key": "example.com//markup//Base", "definitions": [ "go:example.com/markup@v1.0.0//client.go//Base" ], - "reaches": [] + "reaches": [ + "errors//New", + "os//exec//Command" + ] }, { "key": "example.com//markup//Base//Flush", @@ -24,6 +30,7 @@ "go:example.com/markup@v1.0.0//client.go//Base//Flush" ], "reaches": [ + "errors//New", "os//exec//Command" ] }, @@ -32,21 +39,30 @@ "definitions": [ "go:example.com/markup@v1.0.0//client.go//Client" ], - "reaches": [] + "reaches": [ + "errors//New", + "os//exec//Command" + ] }, { "key": "example.com//markup//Conn", "definitions": [ "go:example.com/markup@v1.0.0//client.go//Conn" ], - "reaches": [] + "reaches": [ + "errors//New", + "os//exec//Command" + ] }, { "key": "example.com//markup//Dial", "definitions": [ "go:example.com/markup@v1.0.0//client.go//Dial" ], - "reaches": [] + "reaches": [ + "errors//New", + "os//exec//Command" + ] }, { "key": "example.com//markup//Handler", @@ -61,14 +77,20 @@ "definitions": [ "go:example.com/markup@v1.0.0//client.go//NewWriter" ], - "reaches": [] + "reaches": [ + "errors//New", + "os//exec//Command" + ] }, { "key": "example.com//markup//Node", "definitions": [ "go:example.com/markup@v1.0.0//node.go//Node" ], - "reaches": [] + "reaches": [ + "errors//New", + "os//exec//Command" + ] }, { "key": "example.com//markup//Print", @@ -76,6 +98,7 @@ "go:example.com/markup@v1.0.0//node.go//Print" ], "reaches": [ + "errors//New", "os//exec//Command" ] }, @@ -85,6 +108,7 @@ "go:example.com/markup@v1.0.0//node.go//Render" ], "reaches": [ + "errors//New", "os//exec//Command" ], "uncertainties": [ @@ -97,6 +121,7 @@ "go:example.com/markup@v1.0.0//node.go//run" ], "reaches": [ + "errors//New", "os//exec//Command" ] }, @@ -105,7 +130,10 @@ "definitions": [ "go:example.com/markup@v1.0.0//client.go//Writer" ], - "reaches": [] + "reaches": [ + "errors//New", + "os//exec//Command" + ] }, { "key": "example.com//markup//netctx//Context", diff --git a/test/projectgraph/cases/go/function-value-reflect/expect.yaml b/test/projectgraph/cases/go/function-value-reflect/expect.yaml index 05bd1c74..44007373 100644 --- a/test/projectgraph/cases/go/function-value-reflect/expect.yaml +++ b/test/projectgraph/cases/go/function-value-reflect/expect.yaml @@ -18,14 +18,14 @@ unresolved: reason: computed-name - caller: main.go//main site: main.go:25 - reason: unknown-receiver + reason: no-dependency-source method: MethodByName - caller: main.go//main site: main.go:25 reason: computed-name - caller: main.go//main site: main.go:25 - reason: unknown-receiver + reason: no-dependency-source method: Call results: - target: os//Remove From cbac5523737d2dc6b7cd97f5bbcb4582955dd8da Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:10:16 +0000 Subject: [PATCH 14/58] fix(projectgraph): link Rust extern crate, pub use of a crate and type aliases (FOU-562) Four forms of Rust module paths gave a global no-definition record, so every target of the project was uncertain: extern crate in a submodule, a path through a crate that is not a unit, pub use of a whole crate under a name, and a type alias. extern crate x, extern crate x as y, use x and pub use x as y now bind the name in their module to the crate. A path through that binding is a path of the crate, as rand::thread_rng for crate::secure::rand::thread_rng. A call gets the external key that a plain path of the crate gets. A use of such a path into a crate that no unit gives is a no-dependency-source record with the crate as its import prefix, so only a target of that crate sees it. The route reads each binding of a name in the file, because an inline test module can bind the name again. A first name that names no module that the file sees names a crate, also when a file of the project has the name, as extern crate schannel in src/imp/schannel.rs. The route of an outer lookup stays for the build, because each walk asks for the route of each glob use again. The Rust index records type A = B as a definition of the kind alias, with B in the new field Alias. The link step binds the alias to the type that the file of the alias names when a walk first reads it. A type of another crate is its external key with no node, so a type target does not turn from no-such-target into not-found. Rust method measure, before and after this branch (FOU-556 and FOU-562): function (13): found 1, found/low 3, no-definition 9 -> found 2, found/low 2, ambiguous 2, no-definition 1, unknown-receiver 6 method (38): found/low 16, budget 9, no-definition 13 -> found/low 16, ambiguous 4, budget 9, no-definition 1, unknown-receiver 8 other (5): no-such-target 5 -> no-such-target 5 Cookie finds rand::thread_rng through crate::secure::rand::thread_rng() at src/secure/key.rs:164 with a high hop. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 22 ++ fileindex/index.go | 7 + plugin/indexer/rust.go | 16 +- plugin/indexer/rust_test.go | 18 ++ projectgraph/definitions.go | 2 + projectgraph/graph.go | 5 +- projectgraph/names.go | 41 +++- projectgraph/profile.go | 3 + projectgraph/rules.go | 1 + projectgraph/rust.go | 214 ++++++++++++++++- projectgraph/walk.go | 7 +- .../cases/rust/crate-bindings/expect.yaml | 220 ++++++++++++++++++ .../rust/crate-bindings/project/Cargo.toml | 8 + .../rust/crate-bindings/project/src/defs.rs | 9 + .../rust/crate-bindings/project/src/main.rs | 16 ++ .../crate-bindings/project/src/secure/aes.rs | 8 + .../crate-bindings/project/src/secure/mod.rs | 3 + .../rust/crate-bindings/project/src/store.rs | 6 + .../project/vendor/kit/Cargo.toml | 4 + .../project/vendor/kit/src/lib.rs | 19 ++ .../summaries/cargo_kit_1.0.0.json | 67 ++++++ 21 files changed, 688 insertions(+), 8 deletions(-) create mode 100644 test/projectgraph/cases/rust/crate-bindings/expect.yaml create mode 100644 test/projectgraph/cases/rust/crate-bindings/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/crate-bindings/project/src/defs.rs create mode 100644 test/projectgraph/cases/rust/crate-bindings/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/crate-bindings/project/src/secure/aes.rs create mode 100644 test/projectgraph/cases/rust/crate-bindings/project/src/secure/mod.rs create mode 100644 test/projectgraph/cases/rust/crate-bindings/project/src/store.rs create mode 100644 test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/Cargo.toml create mode 100644 test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/src/lib.rs create mode 100644 test/projectgraph/cases/rust/crate-bindings/summaries/cargo_kit_1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 8f8f6ddb..d5ee0407 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -241,6 +241,28 @@ Rust follows the same rules, with these additions. binds the type that it returns, and `clone`, `to_owned`, `default` and `from` on a type of a unit keep the type also when the crate derives them. +- `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 + `crate::secure::rand::thread_rng()` after `extern crate rand;` in the + module `secure` calls `rand//thread_rng`, and `crate::ffi::ErrorCode` + after `pub use libsqlite3_sys as ffi;` names + `libsqlite3_sys//ErrorCode`. A `use` of such a path into a crate that no + unit gives is a record with the reason `no-dependency-source` and the + crate as its import prefix, so only a target of that crate sees it. A + first name that names no module that the file sees, as a child module or + a module of the root, names a crate, also when a file of the project has + that name, as `extern crate schannel;` in `src/imp/schannel.rs`. +- 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 + reads the alias. A type of the graph gets an `export` edge from the + alias. A type of another crate is its external key, with no node, so a + target of the type stays `no-such-target` and does not turn into + `not-found`. A path, a call or a declared type through the alias then + reads the type. + An alias of a type that the file does not resolve, as a primitive type, + gives the record `unknown-receiver` for a name after it. - A trait object or a generic bound `T: Trait` of a unit gets the `cha` edges to the implementations in the graph, through their external keys. With no implementation, the site is a record with the reason diff --git a/fileindex/index.go b/fileindex/index.go index 93882e54..c9a5833c 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -57,6 +57,9 @@ const ( // DefInterface is a TypeScript or Go interface. It has no code, but a // declared type can name it and a class can implement it. DefInterface DefKind = "interface" + // DefAlias is a type alias, as type A = B of Rust. It has no code. A + // path through it goes on to the type that it names. + DefAlias DefKind = "alias" ) // TryName is the name that a ? of Rust takes in a call target, as @@ -248,6 +251,9 @@ type Definition struct { Wraps string `json:"wraps,omitempty"` Static bool `json:"static,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"` } // Class is one class or interface of the file. Bases are as written, in @@ -629,6 +635,7 @@ func (ix *Index) EachString(fn func(string) string) { kind(&def.Returns) strs(def.Results) kind(&def.Wraps) + kind(&def.Alias) flags(def.Flags) } for i := range ix.Classes { diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 46cf0cca..8f3cb138 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -57,8 +57,9 @@ var rustTypeItems = []string{"struct_item", "enum_item", "union_item"} var rustPointers = []string{"Box", "Rc", "Arc"} // rustTypes adds each struct, enum and union that no impl block of its -// scope names, so that a path names the type. An impl block of the type -// is the class of the type, and its node gives the methods. +// scope names, so that a path names the type, and each type alias. An impl +// block of the type is the class of the type, and its node gives the +// methods. func rustTypes(p *projection) { var walk func(scope string, list *sitter.Node) walk = func(scope string, list *sitter.Node) { @@ -81,6 +82,17 @@ func rustTypes(p *projection) { EndLine: int(item.EndPoint().Row) + 1, } p.addSyntax(key, item) + case item.Type() == "type_item": + key := fileindex.Join(scope, p.text(item.ChildByFieldName("name"))) + p.defs[key] = &fileindex.Definition{ + Key: key, + Kind: fileindex.DefAlias, + Name: fileindex.Last(key), + StartLine: lineOf(item), + EndLine: int(item.EndPoint().Row) + 1, + Alias: p.rustTypeName(item.ChildByFieldName("type"), nil), + } + p.addSyntax(key, item) } } } diff --git a/plugin/indexer/rust_test.go b/plugin/indexer/rust_test.go index 9f27d006..9903d83e 100644 --- a/plugin/indexer/rust_test.go +++ b/plugin/indexer/rust_test.go @@ -96,3 +96,21 @@ func TestFileIndexRustTry(t *testing.T) { assert.Equal(t, "Job", read.Returns) assert.Equal(t, "Result", read.Wraps) } + +func TestFileIndexRustTypeAlias(t *testing.T) { + source := strings.Join([]string{ + "use image::ImageBuffer;", + "pub type Image

= ImageBuffer>;", + "mod inner { type Store = crate::store::Store; }", + }, "\n") + "\n" + ix := indexSource(t, core.LanguageCodeRust, "defs.rs", source) + + image := definitionOf(t, ix, "defs.rs//Image") + assert.Equal(t, fileindex.DefAlias, image.Kind) + assert.Equal(t, "ImageBuffer", image.Alias) + assert.True(t, image.Exported) + store := definitionOf(t, ix, "defs.rs//inner//Store") + assert.Equal(t, fileindex.DefAlias, store.Kind) + assert.Equal(t, "crate::store::Store", store.Alias) + assert.False(t, store.Exported) +} diff --git a/projectgraph/definitions.go b/projectgraph/definitions.go index 807c68c3..6a2e55d0 100644 --- a/projectgraph/definitions.go +++ b/projectgraph/definitions.go @@ -86,6 +86,8 @@ func nodeKind(k fileindex.DefKind) NodeKind { return NodeVariable case fileindex.DefInterface: return NodeInterface + case fileindex.DefAlias: + return NodeExport default: return NodeModule } diff --git a/projectgraph/graph.go b/projectgraph/graph.go index d25631c9..d9cf8b9c 100644 --- a/projectgraph/graph.go +++ b/projectgraph/graph.go @@ -35,8 +35,9 @@ const ( // through it goes on to the value that it holds. NodeVariable NodeKind = "variable" // NodeExport is a name that a file imports and gives on to its own - // importers, as a barrel file or an __init__.py does. The node keeps the - // barrel on a path. + // importers, as a barrel file or an __init__.py does, or a type alias, + // which gives on the type that it names. The node keeps the barrel on a + // path. NodeExport NodeKind = "export" NodeExternal NodeKind = "external" ) diff --git a/projectgraph/names.go b/projectgraph/names.go index 79a8cb9b..b41bcc63 100644 --- a/projectgraph/names.go +++ b/projectgraph/names.go @@ -181,7 +181,46 @@ func (b *builder) bind(sc *scope, name string, line int, target string, r rule) func (b *builder) deref(key string) string { for b.kindOf(key) == NodeExport { - key = b.bindings[key] + target, ok := b.bound(key) + if !ok { + return key + } + key = target } return key } + +// bound returns the key that an export node stands for. A type alias +// binds the type that it names, as the file of the alias resolves it, when +// a walk first reads it, so an alias can name an alias of another file. An +// alias of a type that the file does not resolve, as a primitive type, +// binds nothing. +func (b *builder) bound(key string) (string, bool) { + if target, ok := b.bindings[key]; ok { + return target, true + } + def := b.defs[key] + if def == nil || def.Kind != fileindex.DefAlias || b.reading[key] { + return "", false + } + b.reading[key] = true + defer delete(b.reading, key) + sc := b.scopes[fileindex.Unit(key)] + if def.Alias == "" || sc == nil { + return "", false + } + target, ok := b.resolveNameIn(sc, key, def.Alias) + if !ok && sc.profile.typePath != nil { + target, ok = sc.profile.typePath(b, sc, def.Alias) + } + // A failure can come from the cycle guard of an outer walk, so only a + // target stays. + if !ok || target == key { + return "", false + } + b.bindings[key] = target + if b.g.Nodes[target] != nil { + b.link(sc.ix.Language, key, target, b.siteOf(key), EdgeExport, ruleAlias) + } + return target, true +} diff --git a/projectgraph/profile.go b/projectgraph/profile.go index e5e5c2fb..4364a4d5 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -30,6 +30,9 @@ type profile struct { link func(b *builder, ix *fileindex.Index) // qualified resolves a qualified name, as com.x.Service in Java. qualified func(b *builder, name string) (string, bool) + // typePath resolves a type path that a file writes and that its scope + // does not bind, as kit::Store in type Handle = kit::Store of Rust. + typePath func(b *builder, sc *scope, written string) (string, bool) // scopeFallback resolves a name that no scope of the file binds, as a // Java class of a wildcard import of a dependency. scopeFallback func(b *builder, sc *scope, caller, first string, rest []string, target string) []resolution diff --git a/projectgraph/rules.go b/projectgraph/rules.go index 6bafd0b4..b7c8d7e3 100644 --- a/projectgraph/rules.go +++ b/projectgraph/rules.go @@ -30,6 +30,7 @@ const ( ruleReference rule = "reference" ruleReexport rule = "reexport" ruleReexportStar rule = "reexport-star" + ruleAlias rule = "alias" ruleOverride rule = "override" ruleArgumentFunction rule = "argument-function" ruleDecorator rule = "decorator" diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 0beeafc0..f8444c0c 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -31,6 +31,7 @@ var rustProfile = profile{ sameTypeMethods: map[string]bool{"clone": true, "to_owned": true, "default": true, "from": true}, abstractReceiver: fileindex.UnresolvedTraitReceiver, dependencies: func(b *builder) ([]string, []modules.Problem) { return b.cargoOf(b.project).Dependencies(), nil }, + typePath: rustTypePath, instance: []string{"self"}, superModule: true, unitImports: true, @@ -78,6 +79,40 @@ var rustConversions = map[string][]string{ func (b *builder) linkRust(ix *fileindex.Index) { b.linkClassBodies(ix) b.linkExtensions(ix, b.extensionsNamed) + b.linkCrateRoutes(ix) +} + +// linkCrateRoutes records each use of a path through a module into a +// crate that no unit gives, as use self::aes_gcm::Aes256Gcm after extern +// crate aes_gcm. The code of the crate is not in the graph, so the record +// has the crate as its import prefix, and only a target of the crate sees +// it. A graph with no unit links no crate, so it records nothing. +func (b *builder) linkCrateRoutes(ix *fileindex.Index) { + if len(b.spaces) < 2 { + return + } + sc := b.scopes[ix.Path] + for _, imp := range ix.Imports { + if imp.Kind != fileindex.ImportStatic { + continue + } + module, _ := importedModule(ix.Language, imp) + path, file, ok := b.rustCratePath(ix.Path, module) + if !ok { + continue + } + crate, _ := rustCrateNames(path) + if len(b.providers(origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(b.byPath[file], crate), form: modules.FormImport})) > 0 { + continue + } + b.addUnresolved(UnresolvedSite{ + Caller: b.runner(sc.enclosing(imp.Line)), + Site: Site{Path: ix.Path, Line: imp.Line}, + Text: module, + Reason: fileindex.UnresolvedNoDependencySource, + ImportPrefix: fileindex.Namespace(ix.Language, crate), + }) + } } // rustImplicit name the methods of the traits of std that Rust code calls @@ -106,12 +141,21 @@ var rustUnwraps = map[string]bool{"unwrap": true, "expect": true, "unwrap_unchec 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, and the paths that a resolution reads now, -// by the file that writes them. +// module path of each Rust file, and the paths that a resolution or a +// crate route reads now, by the file that writes them. type rustState struct { cargo map[*space]*modules.Cargo modules map[string]string resolving map[[2]string]bool + routing map[[2]string]bool + routes map[[2]string]crateRoute +} + +// crateRoute is the path in another crate that a path of a file names, and +// the file of the binding. +type crateRoute struct { + path, file string + ok bool } func (b *builder) rustState() *rustState { @@ -139,6 +183,8 @@ func (b *builder) addRustUnits() { s := b.rustState() s.modules = map[string]string{} s.resolving = map[[2]string]bool{} + s.routing = map[[2]string]bool{} + s.routes = map[[2]string]crateRoute{} files := map[string][]string{} for _, p := range b.paths { if ix := b.byPath[p]; ix != nil && ix.Language == langcode.Rust { @@ -371,9 +417,161 @@ func (r rustResolver) Module(importer, spec string) (string, string, bool) { if key, ok := r.b.resolveRust(importer, spec); ok { return key, "path", true } + if path, file, ok := r.b.rustCratePath(importer, spec); ok { + return r.b.rustCrateKey(file, path), "crate", true + } + return "", "", false +} + +// rustCrateKey returns the external key of a path of another crate that a +// file names, and records the path as an origin of the key. It makes no +// node: a walk reads the key with no node, and a call of a name under it +// makes the node of that name. A node of a type that no call reaches would +// turn a target of the type from no-such-target into not-found. +func (b *builder) rustCrateKey(file, path string) string { + crate, names := rustCrateNames(path) + key := fileindex.Namespace(langcode.Rust, path) + b.recordOrigin(b.byPath[file], key, crate, modules.FormImport, names) + return key +} + +// rustTypePath returns the type that a path of a file names, as a type +// alias of the file writes it: a type of the crate, or the key of a type of +// another crate, also through a binding of a module. +func rustTypePath(b *builder, sc *scope, written string) (string, bool) { + if key, ok := b.resolveRust(sc.ix.Path, written); ok { + return b.deref(key), true + } + if path, file, ok := b.rustCratePath(sc.ix.Path, written); ok { + return b.rustCrateKey(file, path), true + } + if !strings.Contains(written, "::") || b.rustLocalRoot(sc.ix.Path, written) { + return "", false + } + return b.rustCrateKey(sc.ix.Path, written), true +} + +// rustCratePath returns the path in another crate that a path of a file +// names through a name that a module binds to that crate, and the file of +// the binding. extern crate rand in the module secure binds rand there, so +// crate::secure::rand::thread_rng is rand::thread_rng. pub use +// libsqlite3_sys as ffi binds ffi, so crate::ffi::ErrorCode is +// libsqlite3_sys::ErrorCode. The deepest module of the path that the crate +// has decides. A path through a use of another module of the crate, as +// self::rand after use crate::secure::rand, follows that use. +// +// A walk asks for the route of each glob use of each module that it reads, +// so the route of an outer call stays for the build. The route reads only +// the tables of the build, so only the cycle guard of an outer route can +// change it. +func (b *builder) rustCratePath(importer, written string) (string, string, bool) { + s := b.rustState() + k := [2]string{importer, written} + if r, ok := s.routes[k]; ok { + return r.path, r.file, r.ok + } + if s.routing[k] { + return "", "", false + } + outer := len(s.routing) == 0 + s.routing[k] = true + path, file, ok := b.routeCrate(importer, written) + delete(s.routing, k) + if outer { + s.routes[k] = crateRoute{path: path, file: file, ok: ok} + } + return path, file, ok +} + +func (b *builder) routeCrate(importer, written string) (string, string, bool) { + parts := strings.Split(strings.TrimPrefix(written, "::"), "::") + for i := len(parts) - 1; i > 0; i-- { + u := b.rustModuleUnit(importer, strings.Join(parts[:i], "::")) + if u == nil { + continue + } + for _, p := range u.files { + for _, imp := range rustBindings(b.scopes[p], parts[i]) { + module, _ := importedModule(langcode.Rust, imp) + if crate, file, ok := b.rustCrate(p, module); ok { + return joinPath(crate, parts[i+1:]), file, true + } + } + } + return "", "", false + } return "", "", false } +// rustModuleUnit returns the unit of the module that a path of a file +// names as written, with no walk through a use. A walk reads the uses and +// the glob uses of each module on the way, and each of them can ask for a +// crate route again, so a route through walks grows with the power of the +// path length. +func (b *builder) rustModuleUnit(importer, written string) *unit { + t := b.namespaces[langcode.Rust] + for _, p := range b.rustPaths(b.rustState().modules[importer], written) { + if u := t.units[p]; u != nil { + return u + } + } + return nil +} + +// rustBindings returns the uses and the extern crates of a file that bind +// a name. extern crate rand and use rand bind the name of the crate, and no +// alias holds it, so the scope keeps no binding for them. The scope keeps +// one binding of a name for the file, and an inline module, as mod tests, +// can bind the name again, so each one counts. +func rustBindings(sc *scope, name string) []fileindex.Import { + var out []fileindex.Import + for _, imp := range sc.imports { + if !imp.Wildcard && (bindingName(langcode.Rust, imp) == name || imp.Item == "" && imp.Module == name) { + out = append(out, imp) + } + } + return out +} + +// rustCrate returns the path in another crate that the module of a use of +// a file names, and the file that names the crate: the route of the module +// through a binding, or else the module itself when its first name names +// no module of the crate, as rand of extern crate rand. +func (b *builder) rustCrate(file, module string) (string, string, bool) { + if path, via, ok := b.rustCratePath(file, module); ok { + return path, via, true + } + if b.rustLocalRoot(file, module) { + return "", "", false + } + return module, file, true +} + +// rustLocalRoot reports whether the first name of a path of a file names +// the crate of the file or a module of it: crate, self, super, a crate of +// the project, a child module, or a module of the root of the crate, as a +// path of the 2015 edition names it. Any other first name names a crate, +// as schannel of extern crate schannel in src/imp/schannel.rs. A file of +// the name does not tell it, because the module of that file is no child. +func (b *builder) rustLocalRoot(file, path string) bool { + first, _, _ := strings.Cut(strings.TrimPrefix(path, "::"), "::") + switch first { + case "crate", "self", "super": + return true + } + return b.cargoOf(b.spaceOf(file)).Has(first) || b.rustModuleUnit(file, first) != nil || b.rustModuleUnit(file, "crate::"+first) != nil +} + +// rustCrateNames splits a path of a crate into the crate and the names +// under it. +func rustCrateNames(path string) (string, []string) { + crate, rest, ok := strings.Cut(path, "::") + if !ok { + return crate, nil + } + return crate, strings.Split(rest, "::") +} + // FirstParty reports whether a path starts with crate, self or super, with // the name of a crate of the project or with the name of a file or a // directory of the project, as a child module. @@ -388,6 +586,14 @@ func (r rustResolver) FirstParty(spec string) bool { return r.b.cargoOf(r.sp).Has(first) || r.sp.files.HasName(first) } +// FirstPartyFrom reports whether a path of a file can name a module of its +// crate, by the modules that the file sees, and not by the names of all +// the files of the project. +func (r rustResolver) FirstPartyFrom(importer, spec string) bool { + first, _, _ := strings.Cut(strings.TrimPrefix(spec, "::"), "::") + return !rustRoots[first] && r.b.rustLocalRoot(importer, spec) +} + // rustScopeFallback resolves a path that no use and no item of the module // binds: a path of the crate, as crate::store::save or store::save for a // child module, or else a path of another crate, as serde_json::to_string, @@ -399,6 +605,10 @@ func (b *builder) rustScopeFallback(sc *scope, _, first string, rest []string, t if key, ok := b.resolveRust(sc.ix.Path, written); ok { return []resolution{b.walkNamespace(key)} } + if path, file, ok := b.rustCratePath(sc.ix.Path, written); ok { + crate, names := rustCrateNames(path) + return []resolution{b.importedWalk(b.byPath[file], fileindex.Namespace(langcode.Rust, path), crate, modules.FormImport, names)} + } if len(rest) > 0 { return []resolution{b.importedWalk(sc.ix, target, first, modules.FormImport, rest)} } diff --git a/projectgraph/walk.go b/projectgraph/walk.go index 83a42752..74545c4c 100644 --- a/projectgraph/walk.go +++ b/projectgraph/walk.go @@ -60,7 +60,12 @@ func (b *builder) walkFrom(r resolution, names []string) resolution { case isNamespacePackage(r.key) || kind == NodeModule: consumed, done = b.walkModule(&r, name) case kind == NodeExport: - r.key = b.bindings[r.key] + target, ok := b.bound(r.key) + if !ok { + r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} + return r + } + r.key = target case kind == NodeVariable: consumed, done = b.walkVariable(&r, name) case kind == NodeClass || kind == NodeInterface: diff --git a/test/projectgraph/cases/rust/crate-bindings/expect.yaml b/test/projectgraph/cases/rust/crate-bindings/expect.yaml new file mode 100644 index 00000000..05fdd1ff --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/expect.yaml @@ -0,0 +1,220 @@ +source: safedep:hand +description: >- + The crate kit is the unit cargo:kit@1.0.0, and no unit gives the crate aes. src/secure/mod.rs binds kit with extern + crate, so crate::secure::kit::flush() in src/secure/aes.rs calls kit//flush. src/main.rs binds kit as ffi with + pub use, and its test module binds ffi again with use crate::ffi. So ffi::reset() after use super::ffi and + crate::ffi::Store::open() in src/store.rs call kit//reset and kit//Store//open. src/defs.rs names kit::Store as the + type alias Handle, so Handle::open() calls kit//Store//open, and the declared type Handle of h gives + kit//Store//save. The alias binds the key kit//Store and makes no node of it, so no type target turns from + no-such-target into not-found. src/secure/aes.rs binds aes with extern crate, and make imports self::aes::Cipher. + The file has the name of the crate, and the crate still names no module of the project. No unit gives aes, so the + use is a no-dependency-source record with the import prefix aes, and the extern crate has no record. A target of + aes is uncertain, and kit//unused, which no code calls, stays not-found. Cipher::new() calls the external key + aes//Cipher//new, as a call of aes::Cipher::new() does. +units: + vendor/kit: cargo:kit@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: cargo:kit@1.0.0//src/lib.rs//Store//open + to: cargo:kit@1.0.0//src/lib.rs//Store + kind: direct + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:7 + - from: cargo:kit@1.0.0//src/lib.rs//Store//save + to: cargo:kit@1.0.0//src/lib.rs//Store + kind: direct + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:10 + - from: cargo:kit@1.0.0//src/lib.rs//Store//save + to: std//fs//write + kind: direct + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:11 + - from: cargo:kit@1.0.0//src/lib.rs//Store//save + to: std//fs//write//unwrap + kind: direct + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:11 + - from: kit//Store//open + to: cargo:kit@1.0.0//src/lib.rs//Store//open + kind: package + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:6 + - from: kit//Store//save + to: cargo:kit@1.0.0//src/lib.rs//Store//save + kind: package + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:10 + - from: kit//flush + to: cargo:kit@1.0.0//src/lib.rs//flush + kind: package + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:15 + - from: kit//reset + to: cargo:kit@1.0.0//src/lib.rs//reset + kind: package + confidence: high + site: cargo:kit@1.0.0//src/lib.rs:17 + - from: src/defs.rs//keep + to: kit//Store//save + kind: direct + confidence: high + site: src/defs.rs:8 + - from: src/defs.rs//run + to: kit//Store//open + kind: direct + confidence: high + site: src/defs.rs:4 + - from: src/defs.rs//run + to: src/defs.rs//keep + kind: direct + confidence: high + site: src/defs.rs:4 + - from: src/main.rs//main + to: src/defs.rs//run + kind: direct + confidence: high + site: src/main.rs:10 + - from: src/main.rs//main + to: src/secure/aes.rs//make + kind: direct + confidence: high + site: src/main.rs:8 + - from: src/main.rs//main + to: src/store.rs//sync + kind: direct + confidence: high + site: src/main.rs:9 + - from: src/secure/aes.rs//make + to: aes//Cipher//new + kind: direct + confidence: high + site: src/secure/aes.rs:7 + - from: src/secure/aes.rs//make + to: kit//flush + kind: direct + confidence: high + site: src/secure/aes.rs:6 + - from: src/store.rs//sync + to: kit//Store//open + kind: direct + confidence: high + site: src/store.rs:5 + - from: src/store.rs//sync + to: kit//reset + kind: direct + confidence: high + site: src/store.rs:4 +unresolved: + - caller: cargo:kit@1.0.0//src/lib.rs//Store//save + site: cargo:kit@1.0.0//src/lib.rs:11 + reason: unknown-receiver + method: unwrap + - caller: src/secure/aes.rs//make + site: src/secure/aes.rs:4 + reason: no-dependency-source +results: + - target: kit//flush + status: found + path: + - key: src/main.rs//main + - key: src/secure/aes.rs//make + site: src/main.rs:8 + kind: direct + confidence: high + - key: kit//flush + site: src/secure/aes.rs:6 + kind: direct + confidence: high + call_sites: + - caller: src/secure/aes.rs//make + site: src/secure/aes.rs:6 + reached: true + - target: kit//reset + status: found + path: + - key: src/main.rs//main + - key: src/store.rs//sync + site: src/main.rs:9 + kind: direct + confidence: high + - key: kit//reset + site: src/store.rs:4 + kind: direct + confidence: high + call_sites: + - caller: src/store.rs//sync + site: src/store.rs:4 + reached: true + - target: kit//Store//open + status: found + path: + - key: src/main.rs//main + - key: src/store.rs//sync + site: src/main.rs:9 + kind: direct + confidence: high + - key: kit//Store//open + site: src/store.rs:5 + kind: direct + confidence: high + call_sites: + - caller: src/defs.rs//run + site: src/defs.rs:4 + reached: true + - caller: src/store.rs//sync + site: src/store.rs:5 + reached: true + - target: kit//Store//save + status: found + path: + - key: src/main.rs//main + - key: src/defs.rs//run + site: src/main.rs:10 + kind: direct + confidence: high + - key: src/defs.rs//keep + site: src/defs.rs:4 + kind: direct + confidence: high + - key: kit//Store//save + site: src/defs.rs:8 + kind: direct + confidence: high + call_sites: + - caller: src/defs.rs//keep + site: src/defs.rs:8 + reached: true + - target: aes//Cipher//new + status: found + path: + - key: src/main.rs//main + - key: src/secure/aes.rs//make + site: src/main.rs:8 + kind: direct + confidence: high + - key: aes//Cipher//new + site: src/secure/aes.rs:7 + kind: direct + confidence: high + call_sites: + - caller: src/secure/aes.rs//make + site: src/secure/aes.rs:7 + reached: true + - target: aes//Cipher//encrypt + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/secure/aes.rs:4 + - target: kit//unused + status: not-found + set_aside: + - kind: unresolved-call + site: src/secure/aes.rs:4 diff --git a/test/projectgraph/cases/rust/crate-bindings/project/Cargo.toml b/test/projectgraph/cases/rust/crate-bindings/project/Cargo.toml new file mode 100644 index 00000000..2745c74d --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +kit = "1.0.0" +aes = "0.8.0" diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/defs.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/defs.rs new file mode 100644 index 00000000..d54f3f31 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/defs.rs @@ -0,0 +1,9 @@ +pub type Handle = kit::Store; + +pub fn run() { + keep(&Handle::open()); +} + +fn keep(h: &Handle) { + h.save(); +} diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/main.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/main.rs new file mode 100644 index 00000000..aff1357b --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/main.rs @@ -0,0 +1,16 @@ +mod defs; +mod secure; +mod store; + +pub use kit as ffi; + +fn main() { + secure::aes::make(); + store::sync(); + defs::run(); +} + +#[cfg(test)] +mod tests { + use crate::ffi; +} diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/secure/aes.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/secure/aes.rs new file mode 100644 index 00000000..83dccdef --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/secure/aes.rs @@ -0,0 +1,8 @@ +extern crate aes; + +pub fn make() { + use self::aes::Cipher; + + crate::secure::kit::flush(); + Cipher::new(); +} diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/secure/mod.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/secure/mod.rs new file mode 100644 index 00000000..ff63e5a8 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/secure/mod.rs @@ -0,0 +1,3 @@ +extern crate kit; + +pub mod aes; diff --git a/test/projectgraph/cases/rust/crate-bindings/project/src/store.rs b/test/projectgraph/cases/rust/crate-bindings/project/src/store.rs new file mode 100644 index 00000000..a6a8b414 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/src/store.rs @@ -0,0 +1,6 @@ +use super::ffi; + +pub fn sync() { + ffi::reset(); + crate::ffi::Store::open(); +} diff --git a/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/Cargo.toml b/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/Cargo.toml new file mode 100644 index 00000000..58da5f79 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "kit" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/src/lib.rs b/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/src/lib.rs new file mode 100644 index 00000000..d6cad052 --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/project/vendor/kit/src/lib.rs @@ -0,0 +1,19 @@ +pub struct Store { + path: String, +} + +impl Store { + pub fn open() -> Store { + Store { path: String::new() } + } + + pub fn save(&self) { + std::fs::write(&self.path, "data").unwrap(); + } +} + +pub fn flush() {} + +pub fn reset() {} + +pub fn unused() {} 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 new file mode 100644 index 00000000..e0d9f46f --- /dev/null +++ b/test/projectgraph/cases/rust/crate-bindings/summaries/cargo_kit_1.0.0.json @@ -0,0 +1,67 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "cargo:kit@1.0.0", + "symbols": [ + { + "key": "kit//Store", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store" + ], + "reaches": [] + }, + { + "key": "kit//Store//open", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store//open" + ], + "reaches": [] + }, + { + "key": "kit//Store//save", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store//save" + ], + "reaches": [], + "uncertainties": [ + 0 + ] + }, + { + "key": "kit//flush", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//flush" + ], + "reaches": [] + }, + { + "key": "kit//reset", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//reset" + ], + "reaches": [] + }, + { + "key": "kit//unused", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//unused" + ], + "reaches": [] + } + ], + "targets": [], + "uncertainties": [ + { + "kind": "unresolved-call", + "key": "cargo:kit@1.0.0//src/lib.rs//Store//save", + "site": { + "path": "cargo:kit@1.0.0//src/lib.rs", + "line": 11, + "column": 9 + }, + "text": "std::fs::write(\u0026self.path, \"data\").unwrap (unknown-receiver)", + "method": "unwrap" + } + ] +} From 02dba3bfca1898feb8e265ccc2e6376b0d25f754 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 09:53:29 +0000 Subject: [PATCH 15/58] fix(projectgraph): give a Go definition of an internal package its external key (FOU-563) The link step gave no external key to a definition of a package under an internal directory of a unit, because no importer outside the module can name it. An advisory names a symbol of such a package by its import path, as google.golang.org/grpc/internal/transport.NewServerTransport. A target of that key matched no definition, so reach could not find it, and the answer was no-such-target. The method measure without a unit of the standard library gave this answer for three grpc targets that govulncheck reaches in grpc-middleware. goImportPath now gives the path of an internal package. A main package still gets none, because no program imports it. The surface of a unit, which a summary reads, still leaves out an internal package. FOU-560 exposed the defect. Before it, the computed-name record of a call of a function value in retry/options.go made every target uncertain. The case go/dependency-internal-package fails without the change: each of its three targets is no-such-target. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 5 ++ projectgraph/golang.go | 16 +++-- .../dependency-internal-package/expect.yaml | 66 +++++++++++++++++++ .../project/go.mod | 5 ++ .../gomod/github.com/acme/kit@v1.0.0/go.mod | 3 + .../acme/kit@v1.0.0/internal/wire/wire.go | 11 ++++ .../gomod/github.com/acme/kit@v1.0.0/kit.go | 9 +++ .../project/main.go | 7 ++ .../go_github.com_acme_kit_v1.0.0.json | 16 +++++ 9 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 test/projectgraph/cases/go/dependency-internal-package/expect.yaml create mode 100644 test/projectgraph/cases/go/dependency-internal-package/project/go.mod create mode 100644 test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/internal/wire/wire.go create mode 100644 test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/kit.go create mode 100644 test/projectgraph/cases/go/dependency-internal-package/project/main.go create mode 100644 test/projectgraph/cases/go/dependency-internal-package/summaries/go_github.com_acme_kit_v1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 8da37c3c..833c8ee2 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -511,6 +511,11 @@ dependency code whose `Node.External` is the key, as the unexported definition and not to the external key, so a target that only the code of its unit calls is found through the definition. The call sites of a target leave out the package edges, which join a key to its definition. +A definition of an internal package of a Go unit has an external key too, +as `google.golang.org//grpc//internal//transport//NewServerTransport`. Only +the module can import such a package, so no importer writes the key, but +an advisory names the symbol by it. An internal package gives no symbol +to a summary. `reach.TargetsByKey` reads a list of keys. `Target.Key` returns the key, and the JSON form of a target is its key. One breadth-first search from diff --git a/projectgraph/golang.go b/projectgraph/golang.go index 25cfefa5..d7bda333 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -86,12 +86,13 @@ func goNarrowProviders(out []provision) []provision { } // goSurface adds the exported names and methods of each Go package of a -// unit that is not internal. +// unit that is not internal. Only the module can import an internal +// package. func goSurface(s *surfacer) { b, sp := s.b, s.sp for _, dir := range slices.Sorted(maps.Keys(sp.goPackages)) { importPath, ok := b.goImportPath(sp, dir) - if !ok { + if !ok || slices.Contains(strings.Split(dir, "/"), "internal") { continue } for _, p := range sp.goPackages[dir].files { @@ -124,12 +125,15 @@ func goSymbol(b *builder, sp *space, def string) (string, []string, bool) { return importPath, fileindex.Segments(def), ok } -// goImportPath returns the import path of a Go package of a unit. A package -// under an internal directory and a main package have no importer outside -// the module. +// goImportPath returns the import path of a Go package of a unit. A main +// package has none, because no program imports it. A package under an +// internal directory keeps its path. The code of its module calls it, and +// an advisory names a symbol of it by that path, as +// google.golang.org/grpc/internal/transport.NewServerTransport. Without the +// path, such a target matches no definition and reads as no-such-target. func (b *builder) goImportPath(sp *space, dir string) (string, bool) { u := sp.goPackages[dir] - if slices.Contains(strings.Split(dir, "/"), "internal") || slices.ContainsFunc(u.files, func(p string) bool { + if slices.ContainsFunc(u.files, func(p string) bool { ix := b.byPath[p] return ix != nil && ix.Package == "main" }) { diff --git a/test/projectgraph/cases/go/dependency-internal-package/expect.yaml b/test/projectgraph/cases/go/dependency-internal-package/expect.yaml new file mode 100644 index 00000000..d2c0b956 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-internal-package/expect.yaml @@ -0,0 +1,66 @@ +source: safedep:hand +description: >- + main calls Serve of the unit github.com/acme/kit. Serve calls Encode and the method Flush of Buffer of the + internal package github.com/acme/kit/internal/wire, and Encode calls the unexported frame. Only the module can + import an internal package, so the project names no key of it, and the summary has no symbol of it. An advisory + names a symbol of an internal package by its import path, as + google.golang.org/grpc/internal/transport.NewServerTransport. So a definition of an internal package carries its + external key, and each target finds its path. Before, the definitions had no external key, and each target was + no-such-target. Unused is called by nothing, so it is not-found. +units: + gomod/github.com/acme/kit@v1.0.0: go:github.com/acme/kit@v1.0.0 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: [] +results: + - target: github.com//acme//kit//internal//wire//frame + status: found + path: + - key: main.go//main + - key: github.com//acme//kit//Serve + site: main.go:6 + kind: direct + confidence: high + - key: go:github.com/acme/kit@v1.0.0//kit.go//Serve + site: go:github.com/acme/kit@v1.0.0//kit.go:5 + kind: package + confidence: high + - key: go:github.com/acme/kit@v1.0.0//internal/wire/wire.go//Encode + site: go:github.com/acme/kit@v1.0.0//kit.go:6 + kind: direct + confidence: high + - key: go:github.com/acme/kit@v1.0.0//internal/wire/wire.go//frame + site: go:github.com/acme/kit@v1.0.0//internal/wire/wire.go:3 + kind: direct + confidence: high + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//internal/wire/wire.go//Encode + site: go:github.com/acme/kit@v1.0.0//internal/wire/wire.go:3 + reached: true + - target: github.com//acme//kit//internal//wire//Buffer//Flush + status: found + path: + - key: main.go//main + - key: github.com//acme//kit//Serve + site: main.go:6 + kind: direct + confidence: high + - key: go:github.com/acme/kit@v1.0.0//kit.go//Serve + site: go:github.com/acme/kit@v1.0.0//kit.go:5 + kind: package + confidence: high + - key: go:github.com/acme/kit@v1.0.0//internal/wire/wire.go//Buffer//Flush + site: go:github.com/acme/kit@v1.0.0//kit.go:8 + kind: member + confidence: high + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//kit.go//Serve + site: go:github.com/acme/kit@v1.0.0//kit.go:8 + reached: true + - target: github.com//acme//kit//internal//wire//Unused + status: not-found diff --git a/test/projectgraph/cases/go/dependency-internal-package/project/go.mod b/test/projectgraph/cases/go/dependency-internal-package/project/go.mod new file mode 100644 index 00000000..c7090c9c --- /dev/null +++ b/test/projectgraph/cases/go/dependency-internal-package/project/go.mod @@ -0,0 +1,5 @@ +module app + +go 1.22 + +require github.com/acme/kit v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/go.mod b/test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/go.mod new file mode 100644 index 00000000..529dd612 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module github.com/acme/kit + +go 1.22 diff --git a/test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/internal/wire/wire.go b/test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/internal/wire/wire.go new file mode 100644 index 00000000..3aa497d1 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/internal/wire/wire.go @@ -0,0 +1,11 @@ +package wire + +func Encode() { frame() } + +func frame() {} + +type Buffer struct{} + +func (b *Buffer) Flush() {} + +func Unused() {} diff --git a/test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/kit.go b/test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/kit.go new file mode 100644 index 00000000..e0074b7b --- /dev/null +++ b/test/projectgraph/cases/go/dependency-internal-package/project/gomod/github.com/acme/kit@v1.0.0/kit.go @@ -0,0 +1,9 @@ +package kit + +import "github.com/acme/kit/internal/wire" + +func Serve() { + wire.Encode() + var b wire.Buffer + b.Flush() +} diff --git a/test/projectgraph/cases/go/dependency-internal-package/project/main.go b/test/projectgraph/cases/go/dependency-internal-package/project/main.go new file mode 100644 index 00000000..5210efdf --- /dev/null +++ b/test/projectgraph/cases/go/dependency-internal-package/project/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/acme/kit" + +func main() { + kit.Serve() +} 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 new file mode 100644 index 00000000..2a476220 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-internal-package/summaries/go_github.com_acme_kit_v1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:github.com/acme/kit@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//kit//Serve", + "definitions": [ + "go:github.com/acme/kit@v1.0.0//kit.go//Serve" + ], + "reaches": [] + } + ], + "targets": [] +} From 7a857fff2f57f8e692b03f847f384e37d5b9dd0f Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:08:49 +0000 Subject: [PATCH 16/58] fix(projectgraph): record Go code outside the graph that can call back into it (FOU-563) A Go call of an external key whose package no unit gives runs code that the graph does not hold. That code can call back into the graph, and no edge showed it. gorilla/websocket passes its reader to io.ReadAll, and io.ReadAll calls Read of the reader. With no unit of the standard library, as vet links a project, the search stopped at io//ReadAll, and messageReader.Read and flateReadWrapper.Read of melody were not-found, which govulncheck reaches. Before FOU-558 (b9adbc6), the no-definition records of the constant reads TextMessage and BinaryMessage made every target of the unit uncertain and hid the gap. The link step now gives each such key a record with the reason no-dependency-source: - A key of the standard library gets a callback record. The standard library imports no package outside it, so it calls into the graph only through a function value, which a reference edge already joins, or through a method of an exported name of a value that it holds. The record concerns the targets that a Go method of an exported name reaches. The reach query counts it once per search, and a summary keeps one entry for it. - A key of another module, and a key of C, gets a record that concerns every target. A summary links one unit alone, so it gets no record for another module. - The builtin panic gets a record for the methods Error and String, which the runtime calls to print the value. docs/projectgraph.md gives the argument and the gaps that stay. The new cases stdlib-callback, stdlib-callback-scope, dependency-no-unit and panic-value fail before the change: each target that the change makes uncertain was not-found. stdlib-callback-scope keeps three targets not-found that no method of an exported name reaches. Method measure, Go only, govulncheck agreement: no std unit: false-negative 5 -> 0 (with the internal key fix) not-found: function 69 -> 8, method 302 -> 1 no-such-target: function 19 -> 1, method 155 -> 10 uncertain/no-dependency-source: function 2 -> 113, method 7 -> 571 govulncheck-not-reached answered not-found 540 -> 20 std unit: false-negative 0 -> 0, not-found 0 -> 0 The callback alone gives not-found 540 -> 233 for the targets that govulncheck does not reach. The records of other modules give the rest, because the corpus gives no unit for most modules of a project, as golang.org/x/sys. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 63 +++++++++++++- fileindex/index.go | 3 +- projectgraph/edges.go | 9 ++ projectgraph/golang.go | 56 ++++++++++++ projectgraph/graph.go | 6 +- projectgraph/link.go | 1 + projectgraph/reach/reach.go | 40 ++++++++- projectgraph/reach/reach_test.go | 87 ++++++++++++++++++- projectgraph/reach/sets.go | 47 ++++++---- projectgraph/reach/uncertainty.go | 44 +++++++--- test/projectgraph/README.md | 2 +- .../go/chained-external-call/expect.yaml | 17 +++- .../cases/go/cross-package/expect.yaml | 12 ++- .../go/dependency-build-files/expect.yaml | 8 +- .../go_github.com_acme_kit_v1.0.0.json | 16 ++++ .../cases/go/dependency-callback/expect.yaml | 13 +++ .../go_example.com_markup_v1.0.0.json | 50 ++++++++++- .../go/dependency-failed-callback/expect.yaml | 3 + .../go/dependency-function-table/expect.yaml | 9 ++ .../go_github.com_acme_b_v1.0.0.json | 13 +++ .../go/dependency-generated-code/expect.yaml | 5 +- .../go_github.com_acme_kit_v1.0.0.json | 13 +++ .../expect.yaml | 3 + .../go_github.com_acme_dep_v1.0.0.json | 16 ++++ .../cases/go/dependency-module/expect.yaml | 4 +- .../cases/go/dependency-no-unit/expect.yaml | 27 ++++++ .../go/dependency-no-unit/project/go.mod | 8 ++ .../gomod/github.com/acme/kit@v1.0.0/go.mod | 5 ++ .../gomod/github.com/acme/kit@v1.0.0/kit.go | 7 ++ .../go/dependency-no-unit/project/main.go | 11 +++ .../go_github.com_acme_kit_v1.0.0.json | 27 ++++++ .../go/dependency-platform-files/expect.yaml | 5 +- .../go_github.com_acme_kit_v1.0.0.json | 13 +++ .../go_example.com_markup_v1.0.0.json | 23 +++++ .../cases/go/dot-import/expect.yaml | 5 +- .../cases/go/function-value-call/expect.yaml | 12 +++ .../go/function-value-reflect/expect.yaml | 10 +++ .../cases/go/interface-method-set/expect.yaml | 10 +++ .../interface-two-implementations/expect.yaml | 10 +++ .../cases/go/main-root/expect.yaml | 11 ++- .../cases/go/panic-value/expect.yaml | 38 ++++++++ .../cases/go/panic-value/project/go.mod | 5 ++ .../gomod/github.com/acme/kit@v1.0.0/go.mod | 3 + .../gomod/github.com/acme/kit@v1.0.0/kit.go | 13 +++ .../cases/go/panic-value/project/main.go | 7 ++ .../go_github.com_acme_kit_v1.0.0.json | 16 ++++ .../go/stdlib-callback-scope/expect.yaml | 65 ++++++++++++++ .../go/stdlib-callback-scope/project/go.mod | 5 ++ .../gomod/github.com/acme/kit@v1.0.0/go.mod | 3 + .../gomod/github.com/acme/kit@v1.0.0/kit.go | 19 ++++ .../go/stdlib-callback-scope/project/main.go | 12 +++ .../go_github.com_acme_kit_v1.0.0.json | 23 +++++ .../cases/go/stdlib-callback/expect.yaml | 53 +++++++++++ .../cases/go/stdlib-callback/project/go.mod | 5 ++ .../gomod/github.com/acme/ws@v1.0.0/conn.go | 23 +++++ .../gomod/github.com/acme/ws@v1.0.0/go.mod | 3 + .../cases/go/stdlib-callback/project/main.go | 8 ++ .../go_github.com_acme_ws_v1.0.0.json | 50 +++++++++++ .../cases/go/struct-method/expect.yaml | 12 ++- .../cases/go/transitive-init/expect.yaml | 5 +- .../cases/go/two-package-clauses/expect.yaml | 8 +- test/projectgraph/cases_test.go | 11 +-- 62 files changed, 1053 insertions(+), 58 deletions(-) create mode 100644 test/projectgraph/cases/go/dependency-no-unit/expect.yaml create mode 100644 test/projectgraph/cases/go/dependency-no-unit/project/go.mod create mode 100644 test/projectgraph/cases/go/dependency-no-unit/project/gomod/github.com/acme/kit@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/dependency-no-unit/project/gomod/github.com/acme/kit@v1.0.0/kit.go create mode 100644 test/projectgraph/cases/go/dependency-no-unit/project/main.go create mode 100644 test/projectgraph/cases/go/dependency-no-unit/summaries/go_github.com_acme_kit_v1.0.0.json create mode 100644 test/projectgraph/cases/go/panic-value/expect.yaml create mode 100644 test/projectgraph/cases/go/panic-value/project/go.mod create mode 100644 test/projectgraph/cases/go/panic-value/project/gomod/github.com/acme/kit@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/panic-value/project/gomod/github.com/acme/kit@v1.0.0/kit.go create mode 100644 test/projectgraph/cases/go/panic-value/project/main.go create mode 100644 test/projectgraph/cases/go/panic-value/summaries/go_github.com_acme_kit_v1.0.0.json create mode 100644 test/projectgraph/cases/go/stdlib-callback-scope/expect.yaml create mode 100644 test/projectgraph/cases/go/stdlib-callback-scope/project/go.mod create mode 100644 test/projectgraph/cases/go/stdlib-callback-scope/project/gomod/github.com/acme/kit@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/stdlib-callback-scope/project/gomod/github.com/acme/kit@v1.0.0/kit.go create mode 100644 test/projectgraph/cases/go/stdlib-callback-scope/project/main.go create mode 100644 test/projectgraph/cases/go/stdlib-callback-scope/summaries/go_github.com_acme_kit_v1.0.0.json create mode 100644 test/projectgraph/cases/go/stdlib-callback/expect.yaml create mode 100644 test/projectgraph/cases/go/stdlib-callback/project/go.mod create mode 100644 test/projectgraph/cases/go/stdlib-callback/project/gomod/github.com/acme/ws@v1.0.0/conn.go create mode 100644 test/projectgraph/cases/go/stdlib-callback/project/gomod/github.com/acme/ws@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/stdlib-callback/project/main.go create mode 100644 test/projectgraph/cases/go/stdlib-callback/summaries/go_github.com_acme_ws_v1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 833c8ee2..4dd3f78f 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -590,8 +590,11 @@ with an unknown receiver and a method name concerns the targets with that last segment, and the targets that any definition of that name reaches. A computed import with a static prefix concerns the targets under the prefix. A call into a package at no known definition concerns the targets -that the code of the package reaches. A result caps the uncertainties that -it lists at three per kind and keeps the full count in `Searched`. +that the code of the package reaches. Code of the Go standard library that +no unit gives concerns the targets that a Go method of an exported name +reaches, as [Code outside the graph](#code-outside-the-graph) argues. A +result caps the uncertainties that it lists at three per kind and keeps +the full count in `Searched`. A file of dependency code that the index step did not complete counts only for a search that enters its package. A file of the project counts @@ -708,3 +711,59 @@ So a scope narrower than every target can make a target `not-found` that the program reaches. The case `go/dependency-failed-callback` shows it: `os/exec.LookPath` stays uncertain only through the budget of the failed file. + +### Code outside the graph + +A Go call or reference of an external key whose package no unit gives +runs code that the graph does not hold. That code can call back into code +that the graph holds, and no edge shows it. As an example, `ReadMessage` of +`gorilla/websocket` passes its reader to `io.ReadAll`, and `io.ReadAll` +calls `Read` of the reader. With no unit of the standard library, the +search stops at the key `io//ReadAll`, and `messageReader.Read` reads as +`not-found`. So the link step gives each such key a record with the reason +`no-dependency-source`, and its caller is the key. The scope of the record +depends on what the code can call. + +- The Go standard library imports no package outside it. So its code can + run code of another module only through a value that it holds: a function + value, or a method of a value. A function value has a reference edge from + the code that makes it, so the search reaches the function without the + standard library. See [A call of a function value](#a-call-of-a-function-value). + A method of a value runs through an interface or through reflection. A + type outside the standard library cannot declare a method of an + unexported name of an interface of the standard library. It can only + embed a type of the standard library that has the method, and that method + is code of the standard library. Reflection gives only the methods of an + exported name. So the record of a key of the standard library is a + callback, as `Callback` marks it. It concerns the targets that a Go + method of an exported name reaches, in the project and in each unit, as + the closure of a method name does. Each key of the standard library gives + the same scope, so a result lists the callback once, with no key. +- A package of another module can import the package of any target and + call any function of it. So its record concerns every target. A summary + links one unit alone, and the reader of a summary joins the summary of + each module that the unit calls. So the link step gives this record only + in a build with a project file. +- C code that cgo calls, as `C//f`, can call each Go function that an + `//export` comment names. So its record concerns every target. +- A builtin runs no code of the program, except `panic`. The runtime prints + 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 target that no Go method of an exported name reaches keeps its answer. +In the case `go/stdlib-callback-scope`, only the function `Dial` calls +`handshake`, so `handshake` stays `not-found`. A search that reaches no +such key keeps every answer. With the unit `go:std@`, the standard +library is code of the graph and gives no callback. A unit of each module +removes the records of the other modules. + +Two gaps stay. + +- A target of a package that no unit gives, which the project names only + in a value, as the composite literal `legacy.Config{}`, has no node. The + standard library can call a method of that value, and the method can call + the target. The graph holds none of that code, so the target reads as + `no-such-target`. +- The rule applies to Go only. Code of another language that no unit gives, + as the standard library of Python with no `python:std` unit, can call a + method of a value too, and no record shows it. diff --git a/fileindex/index.go b/fileindex/index.go index 5a513ddc..f7b7889d 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -101,7 +101,8 @@ const ( UnresolvedTraitReceiver UnresolvedReason = "trait-receiver" // 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. + // function of a module that no unit gives, or an external key of such a + // package, whose code can call back into the graph. 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/projectgraph/edges.go b/projectgraph/edges.go index 1bf14b4d..75f93cc1 100644 --- a/projectgraph/edges.go +++ b/projectgraph/edges.go @@ -138,6 +138,15 @@ func (s *edgeSet) joins(from, to string) bool { return ok } +// pairs yields each caller and callee that an edge joins. +func (s *edgeSet) pairs(yield func(from, to string) bool) { + for pair := range s.linked { + if !yield(s.symbols.strings[pair[0]], s.symbols.strings[pair[1]]) { + return + } + } +} + // sorted returns the edges by caller, site, callee, kind and resolver. func (s *edgeSet) sorted() []Edge { type entry struct { diff --git a/projectgraph/golang.go b/projectgraph/golang.go index d7bda333..e44f5bb6 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -377,3 +377,59 @@ func (b *builder) declaringInterface(class, name string) (string, bool) { } return "", false } + +// goPanicMethods are the methods that the runtime calls on the value of a +// panic that no recover stops, to print it. +var goPanicMethods = []string{"Error", "String"} + +// recordOutsideCode adds a record on each external key that Go code calls +// or uses as a value and whose package no unit gives. The graph holds no +// code of such a key, and that code can call back into code that the +// graph holds. docs/projectgraph.md gives the argument for the scope of +// each record: +// +// - The standard library can call back only a method of an exported name +// of a value that it holds, so its record is a Callback. +// - C code that cgo calls can call any function that an export comment +// names, and another module can call any function of a package that it +// imports. Their records concern every target. A build with no project +// file, as a summary, gives no record for another module, because the +// reader of a summary joins the summary of each module that it calls. +// - A builtin runs no code of the program, except panic. +func (b *builder) recordOutsideCode() { + for from, to := range b.edges.pairs { + if b.kindOf(to) == NodeExternal && b.languageOf(from) == langcode.Go && !b.provided(to) { + for _, r := range b.outsideRecords(to) { + b.addUnresolved(r) + } + } + } +} + +func (b *builder) outsideRecords(key string) []UnresolvedSite { + r := UnresolvedSite{Caller: key, Text: key, Reason: fileindex.UnresolvedNoDependencySource} + first := fileindex.Unit(key) + switch { + case key == "panic": + var out []UnresolvedSite + for _, m := range goPanicMethods { + r.Method = m + out = append(out, r) + } + return out + case langcode.IsBuiltin(langcode.Go, key): + return nil + case first == "C": + case modules.IsStdPath(first): + r.Text, r.Callback = "the Go standard library, which no unit gives", true + case !b.hasProject: + return nil + } + return []UnresolvedSite{r} +} + +// provided reports whether a unit of dependency code gives the package of +// an external key. +func (b *builder) provided(key string) bool { + return slices.ContainsFunc(b.origins[key], func(o origin) bool { return len(b.providers(o)) > 0 }) +} diff --git a/projectgraph/graph.go b/projectgraph/graph.go index d25631c9..98a13691 100644 --- a/projectgraph/graph.go +++ b/projectgraph/graph.go @@ -202,7 +202,10 @@ type Edge struct { // caller and the unit as its site. The code that runs there is code of the // unit, so it can reach only what the code of the unit can reach. A site // with no caller is a unit that no import names, and it holds for every -// search. +// 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. type UnresolvedSite struct { Caller string `json:"caller"` Site Site `json:"site"` @@ -212,6 +215,7 @@ type UnresolvedSite struct { Reason fileindex.UnresolvedReason `json:"reason"` ImportPrefix string `json:"import_prefix,omitempty"` Unit string `json:"unit,omitempty"` + Callback bool `json:"callback,omitempty"` } // Root is a node that runs with no caller in the graph. Rule is the id of the diff --git a/projectgraph/link.go b/projectgraph/link.go index 8e8a1674..e513acca 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -378,6 +378,7 @@ func (b *builder) linkAll(ctx context.Context) error { if err := b.linkPackages(ctx); err != nil { return err } + b.recordOutsideCode() b.checkUnits() b.linkFields() return nil diff --git a/projectgraph/reach/reach.go b/projectgraph/reach/reach.go index 54bc31ca..baf2f221 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" ) @@ -101,8 +102,12 @@ type CallSite struct { // dependency code. ImportPrefix scopes a computed import to the targets // under the prefix, and Unit scopes a call into a package at no known // definition to the targets that the code of the package, and of each -// package that it calls, reaches. With none of them, the uncertainty -// concerns every target. +// package that it calls, reaches. Callback scopes code outside the graph +// that can call into the graph only through a method of a value that it +// holds, as the Go standard library with no unit, to the targets that any +// method of an exported name reaches. Such an uncertainty has no key and +// counts once per search. With none of them, the uncertainty concerns +// every target. type Uncertainty struct { Kind UncertaintyKind `json:"kind"` Key string `json:"key,omitempty"` @@ -111,6 +116,7 @@ type Uncertainty struct { Method string `json:"method,omitempty"` ImportPrefix string `json:"import_prefix,omitempty"` Unit string `json:"unit,omitempty"` + Callback bool `json:"callback,omitempty"` } // Searched is what the search did. Version is the Version of the search @@ -372,6 +378,7 @@ type adjacency struct { closures map[string][]bool byLast map[string][]int methodClosures map[string][]bool + exported []bool } // sources returns the records, the flags and the budgets of the graph by @@ -520,6 +527,35 @@ func (a *adjacency) methodReaches(name string, target Target) bool { return slices.ContainsFunc(a.match(target), func(id int) bool { return c[id] }) } +// exportedClosure returns the nodes that the Go methods of an exported +// name reach, in the project and in each unit of dependency code. Code +// outside the graph can call such a method on a value that it holds. A +// method of a test file is a source only when the query includes the +// tests. +func (a *adjacency) exportedClosure() []bool { + if a.exported == nil { + var methods []int + for id, key := range a.keys { + n := a.g.Nodes[key] + if n == nil || n.Kind != projectgraph.NodeMethod || !n.Exported || n.Test && !a.includeTests { + continue + } + if file := a.g.Nodes[n.Path]; file != nil && file.Language == langcode.Go { + methods = append(methods, id) + } + } + a.exported = a.reachable(methods, func(int) []int { return nil }) + } + return a.exported +} + +// exportedReaches reports whether a Go method of an exported name reaches +// a node of the target. +func (a *adjacency) exportedReaches(target Target) bool { + c := a.exportedClosure() + return slices.ContainsFunc(a.match(target), func(id int) bool { return c[id] }) +} + // match returns the nodes of a target, by id: the external node of its // key, and each definition of a unit of dependency code whose external key // it names. A call inside a unit goes to the definition, so the search diff --git a/projectgraph/reach/reach_test.go b/projectgraph/reach/reach_test.go index 9b961022..d3bd400a 100644 --- a/projectgraph/reach/reach_test.go +++ b/projectgraph/reach/reach_test.go @@ -37,7 +37,7 @@ func graphOf(t *testing.T, spec string) *projectgraph.Graph { return n } n := &projectgraph.Node{Key: key, Kind: projectgraph.NodeExternal} - if unit := fileindex.Unit(key); strings.HasSuffix(unit, ".py") || strings.HasSuffix(unit, ".js") { + if unit := fileindex.Unit(key); strings.HasSuffix(unit, ".py") || strings.HasSuffix(unit, ".js") || strings.HasSuffix(unit, ".go") { n.Kind, n.Path, n.Line, n.Test = projectgraph.NodeFunction, unit, 1, strings.HasPrefix(unit, "tests/") if _, ok := g.Files[unit]; !ok { g.Files[unit] = fileindex.FileIndexed @@ -86,6 +86,17 @@ func graphOf(t *testing.T, spec string) *projectgraph.Graph { g.Unresolved = append(g.Unresolved, projectgraph.UnresolvedSite{ Caller: caller, Site: projectgraph.Site{Path: f[2]}, Text: f[1], Reason: fileindex.UnresolvedNoDefinition, Unit: f[2], }) + case "callback": + node(f[1]) + g.Unresolved = append(g.Unresolved, projectgraph.UnresolvedSite{ + Caller: f[1], Text: "the Go standard library, which no unit gives", Reason: fileindex.UnresolvedNoDependencySource, Callback: true, + }) + case "method": + n := node(f[1]) + n.Kind, n.Exported = projectgraph.NodeMethod, arg(f, 2, "") != "unexported" + if _, ok := g.Nodes[n.Path]; !ok { + g.Nodes[n.Path] = &projectgraph.Node{Key: n.Path, Kind: projectgraph.NodeModule, Path: n.Path, Language: langcode.Go} + } case "flag": n := node(f[1]) l, err := strconv.Atoi(f[3]) @@ -250,6 +261,51 @@ func TestReachUncertainties(t *testing.T) { } } +// TestReachCallback checks the scope of code outside the graph that can +// call a method of a value that it holds. A target that a Go method of an +// exported name reaches is uncertain. A target that only a function or a +// method of an unexported name reaches stays not-found, and so does each +// target of a search that reaches no such code. +func TestReachCallback(t *testing.T) { + const units = ` +method go:kit@1.0.0//k.go//conn//Read +edge go:kit@1.0.0//k.go//conn//Read os//remove direct 5 +method go:kit@1.0.0//k.go//conn//reset unexported +edge go:kit@1.0.0//k.go//conn//reset os//chmod direct 6 +edge go:kit@1.0.0//k.go//dial os//exit direct 7 +callback io//ReadAll +` + tests := []struct { + name string + spec string + want map[string]Status + }{ + { + name: "a search that reaches the code", + spec: "root main.go//main\nedge main.go//main io//ReadAll direct 3" + units, + want: map[string]Status{"os//remove": Uncertain, "os//chmod": NotFound, "os//exit": NotFound}, + }, + { + name: "a search that does not reach the code", + spec: "root main.go//main\nedge main.go//main os//getpid direct 3" + units, + want: map[string]Status{"os//remove": NotFound, "os//chmod": NotFound, "os//exit": NotFound}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := graphOf(t, tt.spec) + for target, status := range tt.want { + r := reachOne(t, g, Query{Targets: targetsOf(t, target)}) + assert.Equal(t, status, r.Status, target) + if status == Uncertain { + require.Len(t, r.Uncertainties, 1) + assert.True(t, r.Uncertainties[0].Callback) + } + } + }) + } +} + // TestReachFileBudgets checks which incomplete files count for a search: a // file of a language that the program does not run holds no caller that a // search can reach, a test file counts only with the tests, and a file of @@ -661,6 +717,17 @@ entry - npm:other@1.0.0 edge npm:other@1.0.0//o.js//f fs//unlink direct 2 `, "project with units": dependencies + "edge main.py//run kit//load//close direct 4\nentry kit//load//close npm:kit@1.0.0\nedge npm:kit@1.0.0//k.js//close fs//unlink\n", + "code outside the graph that calls a method": ` +edge kit//run go:kit@1.0.0//k.go//run package +edge go:kit@1.0.0//k.go//run io//ReadAll direct 2 +edge go:kit@1.0.0//k.go//run os//exit direct 3 +callback io//ReadAll +edge kit//conn//Read go:kit@1.0.0//k.go//conn//Read package +method go:kit@1.0.0//k.go//conn//Read +edge go:kit@1.0.0//k.go//conn//Read os//remove direct 5 +edge go:kit@1.0.0//k.go//conn//Read io//Copy direct 6 +callback io//Copy +`, "two symbols in one cycle": ` edge a//x npm:a@1.0.0//a.js//x package edge npm:a@1.0.0//a.js//x a//y direct 2 @@ -741,6 +808,24 @@ func TestExploreAllScopesTheTargets(t *testing.T) { assert.ErrorContains(t, err, "no node kit//nope") } +// TestExploreAllScopesACallback checks that the code outside the graph that +// can call a method is one entry of the table, whose targets are what the +// Go methods of an exported name reach. +func TestExploreAllScopesACallback(t *testing.T) { + g := graphOf(t, exploreAllGraphs["code outside the graph that calls a method"]) + sets, err := NewExplorer(g).ExploreAll(context.Background(), []string{"kit//run", "kit//conn//Read"}) + require.NoError(t, err) + require.Len(t, sets.Uncertainties, 1) + callback := sets.Uncertainties[0] + assert.True(t, callback.Callback) + assert.Equal(t, []string{"io//Copy", "os//remove"}, callback.Targets) + assert.True(t, callback.Concerns(targetOf(t, "os//remove"))) + assert.False(t, callback.Concerns(targetOf(t, "os//exit"))) + for _, src := range sets.Sources { + assert.Equal(t, []int{0}, src.Uncertainties) + } +} + func TestExploreAllSharesTheSetOfAChain(t *testing.T) { g := graphOf(t, ` edge a//x npm:a@1.0.0//a.js//x package diff --git a/projectgraph/reach/sets.go b/projectgraph/reach/sets.go index 9c531a22..31e09f9d 100644 --- a/projectgraph/reach/sets.go +++ b/projectgraph/reach/sets.go @@ -19,13 +19,13 @@ type Scoped struct { } // Concerns reports whether the uncertainty concerns a target, as the -// result of a reach query lists it: a Unit scope concerns its Targets, an -// ImportPrefix scope every target under the prefix, a Method scope its -// Targets and every target whose last segment is the method, and an -// uncertainty with no scope every target. +// result of a reach query lists it: a Unit or a Callback scope concerns its +// Targets, an ImportPrefix scope every target under the prefix, a Method +// scope its Targets and every target whose last segment is the method, and +// an uncertainty with no scope every target. func (u Scoped) Concerns(t Target) bool { switch { - case u.Unit != "": + case u.Unit != "", u.Callback: return t.MatchesSorted(u.Targets) case u.ImportPrefix != "": return t.mayImport(u.ImportPrefix) @@ -100,18 +100,20 @@ func (x *Explorer) ExploreAll(ctx context.Context, from []string) (*Sets, error) } // table is the item space of the sets: the external nodes first, then the -// units a search can enter, then the nodes with an uncertainty. target -// holds the item of each external node, and unc the item of each node -// with an uncertainty. entries is the table of uncertainties, and at holds -// the index range of each item of a unit or a node in it. +// units a search can enter, then the code outside the graph that can call +// a method, then the nodes with an uncertainty. target holds the item of +// each external node, and unc the item of each node with an uncertainty. +// entries is the table of uncertainties, and at holds the index range of +// each item of a unit, of the callback or of a node in it. type table struct { - items int - targets []int - target map[int]int - unc map[int]int - units map[string]int - entries []Scoped - at map[int][2]int + items int + targets []int + target map[int]int + unc map[int]int + units map[string]int + callback int + entries []Scoped + at map[int][2]int // global holds the indexes of the entries that count for every // search. global []int @@ -138,6 +140,12 @@ func (a *adjacency) table() *table { t.at[t.items] = [2]int{start, len(t.entries)} t.items++ } + if src.callback != nil { + t.callback = t.items + t.at[t.items] = [2]int{len(t.entries), len(t.entries) + 1} + t.entries = append(t.entries, a.scoped(*src.callback)) + t.items++ + } for id, key := range a.keys { n := a.g.Nodes[key] if n == nil { @@ -163,6 +171,8 @@ func (a *adjacency) scoped(u Uncertainty) Scoped { s := Scoped{Uncertainty: u} var closure []bool switch { + case u.Callback: + closure = a.exportedClosure() case u.Unit != "": closure = a.closure(u.Unit) case u.Method != "": @@ -179,7 +189,7 @@ func (a *adjacency) scoped(u Uncertainty) Scoped { } // own returns the items of one node: its external node, the units it -// enters and its uncertainties. +// enters, the callback of code outside the graph and its uncertainties. func (t *table) own(a *adjacency, id int) []int { var items []int if item, ok := t.target[id]; ok { @@ -188,6 +198,9 @@ func (t *table) own(a *adjacency, id int) []int { if item, ok := t.unc[id]; ok { items = append(items, item) } + if a.sources().callbacks[a.keys[id]] { + items = append(items, t.callback) + } n := a.g.Nodes[a.keys[id]] if n == nil { return items diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index 5821640f..10e2de73 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -27,10 +27,13 @@ type found struct { methods []string prefixed []prefixed byUnit []Uncertainty - // reaches reports whether the code of a unit reaches a target, and - // methodReaches whether a node with the last segment reaches one. - reaches func(unit string, target Target) bool - methodReaches func(name string, target Target) bool + callback []Uncertainty + // reaches reports whether the code of a unit reaches a target, + // methodReaches whether a node with the last segment reaches one, and + // exportedReaches whether a method of an exported name reaches one. + reaches func(unit string, target Target) bool + methodReaches func(name string, target Target) bool + exportedReaches func(target Target) bool // units holds the file budgets of each unit of dependency code, and // entered the units that the search entered. units map[string][]Uncertainty @@ -55,11 +58,24 @@ type sources struct { testBudgets map[string][]Uncertainty // units lists the units that a search can enter, in key order. units []string + // callbacks holds the external keys whose code is outside the graph and + // can call a method of a value that it holds, and callback the one + // uncertainty that they share. Each such key gives the same scope, so + // the search counts it once. + callbacks map[string]bool + callback *Uncertainty } func newSources(g *projectgraph.Graph) *sources { - s := &sources{unresolved: map[string][]projectgraph.UnresolvedSite{}, prefixes: map[projectgraph.Site]string{}, truncated: map[string][]projectgraph.Truncation{}, budgets: map[string][]Uncertainty{}, testBudgets: map[string][]Uncertainty{}} + s := &sources{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 + if s.callback == nil { + s.callback = &Uncertainty{Kind: UnresolvedCall, Text: fmt.Sprintf("%s (%s)", u.Text, u.Reason), Callback: true} + } + continue + } s.unresolved[u.Caller] = append(s.unresolved[u.Caller], u) if u.ImportPrefix != "" { s.prefixes[projectgraph.Site{Path: u.Site.Path, Line: u.Site.Line}] = u.ImportPrefix @@ -188,7 +204,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{}, reaches: a.unitReaches, methodReaches: a.methodReaches, units: src.budgets, entered: map[string]bool{}} + f := &found{byMethod: map[string][]Uncertainty{}, reaches: a.unitReaches, methodReaches: a.methodReaches, exportedReaches: a.exportedReaches, units: src.budgets, entered: map[string]bool{}} for _, u := range src.global { f.add(u) } @@ -213,6 +229,9 @@ func (a *adjacency) uncertainties(s *search, maxDepth int) *found { for _, u := range src.at(key, n) { f.add(u) } + if src.callbacks[key] && len(f.callback) == 0 { + f.add(*src.callback) + } } if s.depthCut { f.global = append(f.global, Uncertainty{Kind: Budget, Text: fmt.Sprintf("the search stopped at depth %d", maxDepth)}) @@ -223,10 +242,12 @@ func (a *adjacency) uncertainties(s *search, maxDepth int) *found { return f } -// add routes an uncertainty by what it concerns: a unit, an import -// prefix, a method name, or every target. +// add routes an uncertainty by what it concerns: the methods of an +// exported name, a unit, an import prefix, a method name, or every target. func (f *found) add(u Uncertainty) { switch { + case u.Callback: + f.callback = append(f.callback, u) case u.Unit != "": f.byUnit = append(f.byUnit, u) case u.ImportPrefix != "": @@ -252,7 +273,7 @@ func (f *found) enter(unit string) { // all returns every uncertainty of the search with no cap, in the order of // the search: first the ones that concern every target, then the ones -// that a Method, an ImportPrefix or a Unit scopes. +// that a Method, an ImportPrefix, a Unit or a Callback scopes. func (f *found) all() []Uncertainty { out := slices.Clone(f.global) for _, m := range f.methods { @@ -261,7 +282,7 @@ func (f *found) all() []Uncertainty { for _, p := range f.prefixed { out = append(out, p.u) } - return append(out, f.byUnit...) + return append(append(out, f.byUnit...), f.callback...) } // forTarget returns the uncertainties of a target, at most maxPerKind of @@ -280,6 +301,9 @@ func (f *found) forTarget(target Target) ([]Uncertainty, []Uncertainty, int) { all = append(all, u) } } + if len(f.callback) > 0 && f.exportedReaches(target) { + all = append(all, f.callback...) + } var setAside []Uncertainty for _, p := range f.prefixed { diff --git a/test/projectgraph/README.md b/test/projectgraph/README.md index 43875343..83b38299 100644 --- a/test/projectgraph/README.md +++ b/test/projectgraph/README.md @@ -45,7 +45,7 @@ it at 0.80. | `edges` | A subset of the edges, with `from`, `to`, `kind`, `confidence` and `site` | | `exact_edges` | `true` makes `edges` the whole set, so a spurious edge fails the case. Use it for a small case | | `no_edges` | Pairs of `from` and `to` that no edge of the graph may join. A case that is not exact needs one for each result that is found, so a spurious edge on its path fails the case | - | `unresolved` | The whole set of unresolved sites, with `caller`, `site`, `reason` and `method` | + | `unresolved` | The whole set of unresolved sites, with `caller`, `site`, `reason`, `method` and `callback`. A site of an external key has no `site` | | `truncated` | The whole set of truncation records of the link step, with `kind`, `key` and `site` | | `dropped` | The whole set of `overlay.json` edges that the merge drops, with `site`, `callee` and `reason` | | `results` | The reach result of each `target`, with `status`, `path`, `call_sites` and the uncertainties. An `llm` hop of a path has its `resolver`, as `model m1, prompt v2`. The first hop has its `root` kind, as `query`, when the case checks who chose the start of the path. `sources` names the sources of the query, as keys, and `sources_only` searches from them alone. `error` is the text of the error that the query must give, as for an unknown source, in place of a result | diff --git a/test/projectgraph/cases/go/chained-external-call/expect.yaml b/test/projectgraph/cases/go/chained-external-call/expect.yaml index dac25974..72afb48f 100644 --- a/test/projectgraph/cases/go/chained-external-call/expect.yaml +++ b/test/projectgraph/cases/go/chained-external-call/expect.yaml @@ -5,7 +5,8 @@ description: >- reason is no-dependency-source. The target Tokenizer.Next is then uncertain at the site, not no-such-target. The edge to the key html//NewTokenizer//Next stays, because a Go call of a type name converts a value, and a method of the type - then runs. + then runs. The code of x/net is outside the graph, and it can call any function of the module, so + Server.ServeConn of http2 is uncertain, not no-such-target. roots: - key: main.go kind: main @@ -39,6 +40,13 @@ edges: confidence: high site: tokens.go:10 unresolved: + - caller: golang.org//x//net//html//NewTokenizer + reason: no-dependency-source + - caller: golang.org//x//net//html//NewTokenizer//Next + reason: no-dependency-source + - caller: os//Stdin + reason: no-dependency-source + callback: true - caller: tokens.go//run site: tokens.go:10 reason: no-dependency-source @@ -47,6 +55,8 @@ results: - target: golang.org//x//net//html//Tokenizer//Next status: uncertain uncertainties: + - kind: unresolved-call + - kind: unresolved-call - kind: unresolved-call site: tokens.go:10 - target: golang.org//x//net//html//NewTokenizer @@ -66,4 +76,7 @@ results: site: tokens.go:10 reached: true - target: golang.org//x//net//http2//Server//ServeConn - status: no-such-target + status: uncertain + uncertainties: + - kind: unresolved-call + - kind: unresolved-call diff --git a/test/projectgraph/cases/go/cross-package/expect.yaml b/test/projectgraph/cases/go/cross-package/expect.yaml index 18708431..b6eaa8ca 100644 --- a/test/projectgraph/cases/go/cross-package/expect.yaml +++ b/test/projectgraph/cases/go/cross-package/expect.yaml @@ -1,5 +1,5 @@ source: safedep:hand -description: main calls store.Save through the import path that go.mod gives the directory. Save calls encode, which another file of the package defines, and encode calls yaml.Marshal. No root reaches unused. No file of store runs code at load, so the import makes no edge. +description: main calls store.Save through the import path that go.mod gives the directory. Save calls encode, which another file of the package defines, and encode calls yaml.Marshal. No root reaches unused. No file of store runs code at load, so the import makes no edge. No unit gives yaml.v3, so the code of Marshal is outside the graph and can call Unmarshal. Unmarshal is uncertain, not not-found. roots: - key: cmd/app/main.go kind: main @@ -21,7 +21,11 @@ edges: no_edges: - from: cmd/app/main.go to: internal/store/store.go -unresolved: [] +unresolved: + - caller: gopkg.in//yaml.v3//Marshal + reason: no-dependency-source + - caller: gopkg.in//yaml.v3//Unmarshal + reason: no-dependency-source results: - target: gopkg.in//yaml.v3//Marshal status: found @@ -44,8 +48,10 @@ results: site: internal/store/encode.go:6 reached: true - target: gopkg.in//yaml.v3//Unmarshal - status: not-found + status: uncertain call_sites: - caller: internal/store/encode.go//unused site: internal/store/encode.go:10 reached: false + uncertainties: + - kind: unresolved-call diff --git a/test/projectgraph/cases/go/dependency-build-files/expect.yaml b/test/projectgraph/cases/go/dependency-build-files/expect.yaml index 2f1d3c0f..4419b5d9 100644 --- a/test/projectgraph/cases/go/dependency-build-files/expect.yaml +++ b/test/projectgraph/cases/go/dependency-build-files/expect.yaml @@ -53,7 +53,13 @@ edges: kind: direct confidence: high site: go:github.com/acme/kit@v1.0.0//tables.go:6 -unresolved: [] +unresolved: + - caller: os//exec//Command + reason: no-dependency-source + callback: true + - caller: syscall//CloseHandle + reason: no-dependency-source + callback: true results: - target: os//exec//Command status: found 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 3d125082..1c97e9b2 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 @@ -11,6 +11,9 @@ ], "reaches": [ "syscall//CloseHandle" + ], + "uncertainties": [ + 0 ] }, { @@ -20,11 +23,24 @@ ], "reaches": [ "os//exec//Command" + ], + "uncertainties": [ + 0 ] } ], "targets": [ "os//exec//Command", "syscall//CloseHandle" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true + } ] } diff --git a/test/projectgraph/cases/go/dependency-callback/expect.yaml b/test/projectgraph/cases/go/dependency-callback/expect.yaml index de3c65b1..b620ca18 100644 --- a/test/projectgraph/cases/go/dependency-callback/expect.yaml +++ b/test/projectgraph/cases/go/dependency-callback/expect.yaml @@ -22,6 +22,18 @@ no_edges: - from: main.go//main to: main.go//mine//Handle unresolved: + - caller: os//Chdir + 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//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: unknown-receiver @@ -79,6 +91,7 @@ results: uncertainties: - kind: unresolved-call site: go:github.com/acme/srv@v1.0.0//srv.go:20 + - kind: unresolved-call - target: os//Chdir status: found path: 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 86fb8926..efcb550d 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 @@ -12,6 +12,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -22,6 +25,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -32,6 +38,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -42,6 +51,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -52,6 +64,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -62,6 +77,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -69,7 +87,7 @@ "definitions": [], "reaches": [], "uncertainties": [ - 0 + 1 ] }, { @@ -80,6 +98,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -90,6 +111,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -100,6 +124,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -112,7 +139,8 @@ "os//exec//Command" ], "uncertainties": [ - 1 + 0, + 2 ] }, { @@ -123,6 +151,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -133,6 +164,9 @@ "reaches": [ "errors//New", "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -149,6 +183,18 @@ "os//exec//Command" ], "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true, + "targets": [ + "os//Remove", + "os//exec//Command" + ] + }, { "kind": "unresolved-call", "key": "example.com//markup//Handler", diff --git a/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml b/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml index 22faff8f..105923c4 100644 --- a/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml +++ b/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml @@ -19,6 +19,9 @@ roots: kind: main rule: go.main 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: unknown-receiver diff --git a/test/projectgraph/cases/go/dependency-function-table/expect.yaml b/test/projectgraph/cases/go/dependency-function-table/expect.yaml index fcee7d81..5ee84368 100644 --- a/test/projectgraph/cases/go/dependency-function-table/expect.yaml +++ b/test/projectgraph/cases/go/dependency-function-table/expect.yaml @@ -20,6 +20,15 @@ no_edges: - from: go:github.com/acme/b@v1.0.0//b.go to: go:github.com/acme/b@v1.0.0//b.go//doB unresolved: + - caller: os//Getwd + reason: no-dependency-source + callback: true + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: os//exec//Command + reason: no-dependency-source + callback: true - caller: go:github.com/acme/a@v1.0.0//a.go//F site: go:github.com/acme/a@v1.0.0//a.go:6 reason: function-value 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 f603fbe8..bc5372f3 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 @@ -12,6 +12,9 @@ "reaches": [ "os//Remove", "os//exec//Command" + ], + "uncertainties": [ + 0 ] } ], @@ -19,5 +22,15 @@ "os//Getwd", "os//Remove", "os//exec//Command" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true + } ] } diff --git a/test/projectgraph/cases/go/dependency-generated-code/expect.yaml b/test/projectgraph/cases/go/dependency-generated-code/expect.yaml index f2a52f83..02593923 100644 --- a/test/projectgraph/cases/go/dependency-generated-code/expect.yaml +++ b/test/projectgraph/cases/go/dependency-generated-code/expect.yaml @@ -13,7 +13,10 @@ roots: - key: main.go//main kind: main rule: go.main -unresolved: [] +unresolved: + - caller: os//exec//Command + reason: no-dependency-source + callback: true results: - target: os//exec//Command status: found 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 a4788f1c..6250dcb0 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 @@ -11,10 +11,23 @@ ], "reaches": [ "os//exec//Command" + ], + "uncertainties": [ + 0 ] } ], "targets": [ "os//exec//Command" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true + } ] } diff --git a/test/projectgraph/cases/go/dependency-implements-interface/expect.yaml b/test/projectgraph/cases/go/dependency-implements-interface/expect.yaml index 253cdef4..ce894757 100644 --- a/test/projectgraph/cases/go/dependency-implements-interface/expect.yaml +++ b/test/projectgraph/cases/go/dependency-implements-interface/expect.yaml @@ -43,6 +43,9 @@ edges: confidence: high site: go:github.com/acme/dep@v1.0.0//dep.go:12 unresolved: + - caller: os//exec//Command + reason: no-dependency-source + callback: true - caller: main.go//main site: main.go:10 reason: no-definition 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 8ed13395..83589a10 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 @@ -18,6 +18,9 @@ ], "reaches": [ "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -30,5 +33,18 @@ ], "targets": [ "os//exec//Command" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true, + "targets": [ + "os//exec//Command" + ] + } ] } diff --git a/test/projectgraph/cases/go/dependency-module/expect.yaml b/test/projectgraph/cases/go/dependency-module/expect.yaml index cbb7e6f3..4ec914ac 100644 --- a/test/projectgraph/cases/go/dependency-module/expect.yaml +++ b/test/projectgraph/cases/go/dependency-module/expect.yaml @@ -41,7 +41,9 @@ no_edges: to: go:github.com/acme/yamlish@v1.2.0//load.go - from: main.go//main to: go:github.com/acme/yamlish@v1.2.0//load.go//Dump -unresolved: [] +unresolved: + - caller: gopkg.in//yaml.v3//Unmarshal + reason: no-dependency-source results: - target: gopkg.in//yaml.v3//Unmarshal status: found diff --git a/test/projectgraph/cases/go/dependency-no-unit/expect.yaml b/test/projectgraph/cases/go/dependency-no-unit/expect.yaml new file mode 100644 index 00000000..71b2e1fd --- /dev/null +++ b/test/projectgraph/cases/go/dependency-no-unit/expect.yaml @@ -0,0 +1,27 @@ +source: safedep:hand +description: >- + main calls Run of the unit github.com/acme/kit and Start of example.com/legacy. No unit gives legacy, and + Run calls legacy.Log. The code of legacy is outside the graph. It can import kit and call any function of + it, so each key of legacy has a record that concerns every target, and Purge is uncertain, not not-found. + The summary of kit links the unit alone and has no such record, because the reader of a summary joins the + summary of each module that the unit calls. +units: + gomod/github.com/acme/kit@v1.0.0: go:github.com/acme/kit@v1.0.0 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: example.com//legacy//Log + reason: no-dependency-source + - caller: example.com//legacy//Start + reason: no-dependency-source +results: + - target: github.com//acme//kit//Purge + status: uncertain + uncertainties: + - kind: unresolved-call + - kind: unresolved-call diff --git a/test/projectgraph/cases/go/dependency-no-unit/project/go.mod b/test/projectgraph/cases/go/dependency-no-unit/project/go.mod new file mode 100644 index 00000000..a688949e --- /dev/null +++ b/test/projectgraph/cases/go/dependency-no-unit/project/go.mod @@ -0,0 +1,8 @@ +module app + +go 1.22 + +require ( + example.com/legacy v1.0.0 + github.com/acme/kit v1.0.0 +) diff --git a/test/projectgraph/cases/go/dependency-no-unit/project/gomod/github.com/acme/kit@v1.0.0/go.mod b/test/projectgraph/cases/go/dependency-no-unit/project/gomod/github.com/acme/kit@v1.0.0/go.mod new file mode 100644 index 00000000..4c8f6b06 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-no-unit/project/gomod/github.com/acme/kit@v1.0.0/go.mod @@ -0,0 +1,5 @@ +module github.com/acme/kit + +go 1.22 + +require example.com/legacy v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-no-unit/project/gomod/github.com/acme/kit@v1.0.0/kit.go b/test/projectgraph/cases/go/dependency-no-unit/project/gomod/github.com/acme/kit@v1.0.0/kit.go new file mode 100644 index 00000000..4483be0d --- /dev/null +++ b/test/projectgraph/cases/go/dependency-no-unit/project/gomod/github.com/acme/kit@v1.0.0/kit.go @@ -0,0 +1,7 @@ +package kit + +import "example.com/legacy" + +func Run() { legacy.Log() } + +func Purge() {} diff --git a/test/projectgraph/cases/go/dependency-no-unit/project/main.go b/test/projectgraph/cases/go/dependency-no-unit/project/main.go new file mode 100644 index 00000000..f06bc7ba --- /dev/null +++ b/test/projectgraph/cases/go/dependency-no-unit/project/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "example.com/legacy" + "github.com/acme/kit" +) + +func main() { + kit.Run() + legacy.Start() +} 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 new file mode 100644 index 00000000..62186a87 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-no-unit/summaries/go_github.com_acme_kit_v1.0.0.json @@ -0,0 +1,27 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:github.com/acme/kit@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//kit//Purge", + "definitions": [ + "go:github.com/acme/kit@v1.0.0//kit.go//Purge" + ], + "reaches": [] + }, + { + "key": "github.com//acme//kit//Run", + "definitions": [ + "go:github.com/acme/kit@v1.0.0//kit.go//Run" + ], + "reaches": [ + "example.com//legacy//Log" + ] + } + ], + "targets": [ + "example.com//legacy//Log" + ] +} diff --git a/test/projectgraph/cases/go/dependency-platform-files/expect.yaml b/test/projectgraph/cases/go/dependency-platform-files/expect.yaml index 7b80a2c5..3cc6b619 100644 --- a/test/projectgraph/cases/go/dependency-platform-files/expect.yaml +++ b/test/projectgraph/cases/go/dependency-platform-files/expect.yaml @@ -12,7 +12,10 @@ roots: - key: main.go//main kind: main rule: go.main -unresolved: [] +unresolved: + - caller: os//exec//Command + reason: no-dependency-source + callback: true results: - target: os//exec//Command status: found 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 a4788f1c..6250dcb0 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 @@ -11,10 +11,23 @@ ], "reaches": [ "os//exec//Command" + ], + "uncertainties": [ + 0 ] } ], "targets": [ "os//exec//Command" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true + } ] } 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 0ad93c43..3024cd49 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 @@ -25,6 +25,9 @@ ], "reaches": [ "os//Remove" + ], + "uncertainties": [ + 0 ] }, { @@ -48,6 +51,9 @@ ], "reaches": [ "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -64,6 +70,9 @@ ], "reaches": [ "os//exec//Command" + ], + "uncertainties": [ + 0 ] }, { @@ -77,5 +86,19 @@ "targets": [ "os//Remove", "os//exec//Command" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true, + "targets": [ + "os//Remove", + "os//exec//Command" + ] + } ] } diff --git a/test/projectgraph/cases/go/dot-import/expect.yaml b/test/projectgraph/cases/go/dot-import/expect.yaml index e7143ce3..3445b1da 100644 --- a/test/projectgraph/cases/go/dot-import/expect.yaml +++ b/test/projectgraph/cases/go/dot-import/expect.yaml @@ -13,7 +13,10 @@ edges: kind: wildcard confidence: low site: cmd/app/main.go:6 -unresolved: [] +unresolved: + - caller: html//template//New + reason: no-dependency-source + callback: true results: - target: html//template//New status: found diff --git a/test/projectgraph/cases/go/function-value-call/expect.yaml b/test/projectgraph/cases/go/function-value-call/expect.yaml index 664ad35b..16200629 100644 --- a/test/projectgraph/cases/go/function-value-call/expect.yaml +++ b/test/projectgraph/cases/go/function-value-call/expect.yaml @@ -21,6 +21,18 @@ no_edges: - from: main.go//main to: os//Remove unresolved: + - 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//main site: main.go:29 reason: function-value diff --git a/test/projectgraph/cases/go/function-value-reflect/expect.yaml b/test/projectgraph/cases/go/function-value-reflect/expect.yaml index 44007373..04a25678 100644 --- a/test/projectgraph/cases/go/function-value-reflect/expect.yaml +++ b/test/projectgraph/cases/go/function-value-reflect/expect.yaml @@ -13,6 +13,15 @@ roots: kind: main rule: go.main unresolved: + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: os//exec//Command + reason: no-dependency-source + callback: true + - caller: reflect//ValueOf + reason: no-dependency-source + callback: true - caller: main.go//main site: main.go:25 reason: computed-name @@ -39,6 +48,7 @@ results: site: main.go:25 - kind: unresolved-call site: main.go:25 + - kind: unresolved-call - target: os//exec//Command status: uncertain call_sites: diff --git a/test/projectgraph/cases/go/interface-method-set/expect.yaml b/test/projectgraph/cases/go/interface-method-set/expect.yaml index 0d27bdce..e4407d6b 100644 --- a/test/projectgraph/cases/go/interface-method-set/expect.yaml +++ b/test/projectgraph/cases/go/interface-method-set/expect.yaml @@ -17,6 +17,15 @@ no_edges: - from: cmd/app/main.go//start to: worker/script.go//Script//Run unresolved: + - caller: net//http//Get + reason: no-dependency-source + callback: true + - caller: os//exec//Command + reason: no-dependency-source + callback: true + - caller: os//exec//Command//Run + reason: no-dependency-source + callback: true - caller: cmd/app/main.go//start site: cmd/app/main.go:10 reason: no-definition @@ -55,3 +64,4 @@ results: uncertainties: - kind: unresolved-call site: cmd/app/main.go:10 + - kind: unresolved-call diff --git a/test/projectgraph/cases/go/interface-two-implementations/expect.yaml b/test/projectgraph/cases/go/interface-two-implementations/expect.yaml index c76d4e9e..e8db580f 100644 --- a/test/projectgraph/cases/go/interface-two-implementations/expect.yaml +++ b/test/projectgraph/cases/go/interface-two-implementations/expect.yaml @@ -22,6 +22,15 @@ no_edges: - from: cmd/app/main.go//run to: sink/db.go//Log//Write unresolved: + - caller: database//sql//DB//Exec + reason: no-dependency-source + callback: true + - caller: os//Exit + reason: no-dependency-source + callback: true + - caller: os//WriteFile + reason: no-dependency-source + callback: true - caller: cmd/app/main.go//run site: cmd/app/main.go:10 reason: no-definition @@ -56,3 +65,4 @@ results: uncertainties: - kind: unresolved-call site: cmd/app/main.go:10 + - kind: unresolved-call diff --git a/test/projectgraph/cases/go/main-root/expect.yaml b/test/projectgraph/cases/go/main-root/expect.yaml index 6de4bd6a..c1c2425a 100644 --- a/test/projectgraph/cases/go/main-root/expect.yaml +++ b/test/projectgraph/cases/go/main-root/expect.yaml @@ -18,7 +18,16 @@ edges: kind: direct confidence: high site: lib/register.go:5 -unresolved: [] +unresolved: + - caller: net//http//HandleFunc + reason: no-dependency-source + callback: true + - caller: net//http//ListenAndServe + reason: no-dependency-source + callback: true + - caller: os//exec//Command + reason: no-dependency-source + callback: true results: - target: os//exec//Command status: found diff --git a/test/projectgraph/cases/go/panic-value/expect.yaml b/test/projectgraph/cases/go/panic-value/expect.yaml new file mode 100644 index 00000000..76cea514 --- /dev/null +++ b/test/projectgraph/cases/go/panic-value/expect.yaml @@ -0,0 +1,38 @@ +source: safedep:hand +description: >- + main panics with the error that Fail of the unit github.com/acme/kit gives. No recover stops the panic, so + the runtime prints the value with its method Error, and Error calls report. The builtin panic has a record + for the methods Error and String, so report is uncertain, not not-found. The runtime does not call Unwrap, + so unwrap is not-found. No code of the standard library runs here, so no callback record makes it + uncertain. +units: + gomod/github.com/acme/kit@v1.0.0: go:github.com/acme/kit@v1.0.0 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: panic + reason: no-dependency-source + method: Error + - caller: panic + reason: no-dependency-source + method: String +results: + - target: github.com//acme//kit//report + status: uncertain + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//kit.go//failure//Error + site: go:github.com/acme/kit@v1.0.0//kit.go:7 + reached: false + uncertainties: + - kind: unresolved-call + - target: github.com//acme//kit//unwrap + status: not-found + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//kit.go//failure//Unwrap + site: go:github.com/acme/kit@v1.0.0//kit.go:11 + reached: false diff --git a/test/projectgraph/cases/go/panic-value/project/go.mod b/test/projectgraph/cases/go/panic-value/project/go.mod new file mode 100644 index 00000000..c7090c9c --- /dev/null +++ b/test/projectgraph/cases/go/panic-value/project/go.mod @@ -0,0 +1,5 @@ +module app + +go 1.22 + +require github.com/acme/kit v1.0.0 diff --git a/test/projectgraph/cases/go/panic-value/project/gomod/github.com/acme/kit@v1.0.0/go.mod b/test/projectgraph/cases/go/panic-value/project/gomod/github.com/acme/kit@v1.0.0/go.mod new file mode 100644 index 00000000..529dd612 --- /dev/null +++ b/test/projectgraph/cases/go/panic-value/project/gomod/github.com/acme/kit@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module github.com/acme/kit + +go 1.22 diff --git a/test/projectgraph/cases/go/panic-value/project/gomod/github.com/acme/kit@v1.0.0/kit.go b/test/projectgraph/cases/go/panic-value/project/gomod/github.com/acme/kit@v1.0.0/kit.go new file mode 100644 index 00000000..2deeaaec --- /dev/null +++ b/test/projectgraph/cases/go/panic-value/project/gomod/github.com/acme/kit@v1.0.0/kit.go @@ -0,0 +1,13 @@ +package kit + +type failure struct{} + +func Fail() error { return &failure{} } + +func (f *failure) Error() string { return report() } + +func report() string { return "" } + +func (f *failure) Unwrap() error { return unwrap() } + +func unwrap() error { return nil } diff --git a/test/projectgraph/cases/go/panic-value/project/main.go b/test/projectgraph/cases/go/panic-value/project/main.go new file mode 100644 index 00000000..761c2208 --- /dev/null +++ b/test/projectgraph/cases/go/panic-value/project/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/acme/kit" + +func main() { + panic(kit.Fail()) +} 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 new file mode 100644 index 00000000..4149be74 --- /dev/null +++ b/test/projectgraph/cases/go/panic-value/summaries/go_github.com_acme_kit_v1.0.0.json @@ -0,0 +1,16 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:github.com/acme/kit@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//kit//Fail", + "definitions": [ + "go:github.com/acme/kit@v1.0.0//kit.go//Fail" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/go/stdlib-callback-scope/expect.yaml b/test/projectgraph/cases/go/stdlib-callback-scope/expect.yaml new file mode 100644 index 00000000..08d84a56 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-scope/expect.yaml @@ -0,0 +1,65 @@ +source: safedep:hand +description: >- + main calls os.Getenv, whose code no unit gives, and Run of the unit github.com/acme/kit. The callback record + of os//Getenv concerns only the targets that a method of an exported name reaches, because the standard + library imports no package outside it and reflection gives no method of an unexported name. closeConn is + uncertain, because the method Close of the unexported type client calls it. handshake is not-found, because + only the function Dial calls it, and the standard library calls a function of another module only through + a function value, which a reference edge joins. resetConn and the method reset are not-found, because no + method of an exported name reaches them. +units: + gomod/github.com/acme/kit@v1.0.0: go:github.com/acme/kit@v1.0.0 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: os//Getenv + reason: no-dependency-source + callback: true +results: + - target: github.com//acme//kit//start + status: found + path: + - key: main.go//main + - key: github.com//acme//kit//Run + site: main.go:11 + kind: direct + confidence: high + - key: go:github.com/acme/kit@v1.0.0//kit.go//Run + site: go:github.com/acme/kit@v1.0.0//kit.go:3 + kind: package + confidence: high + - key: go:github.com/acme/kit@v1.0.0//kit.go//start + site: go:github.com/acme/kit@v1.0.0//kit.go:3 + kind: direct + confidence: high + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//kit.go//Run + site: go:github.com/acme/kit@v1.0.0//kit.go:3 + reached: true + - target: github.com//acme//kit//closeConn + status: uncertain + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//kit.go//client//Close + site: go:github.com/acme/kit@v1.0.0//kit.go:13 + reached: false + uncertainties: + - kind: unresolved-call + - target: github.com//acme//kit//handshake + status: not-found + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//kit.go//Dial + site: go:github.com/acme/kit@v1.0.0//kit.go:7 + reached: false + - target: github.com//acme//kit//resetConn + status: not-found + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//kit.go//client//reset + site: go:github.com/acme/kit@v1.0.0//kit.go:17 + reached: false + - target: github.com//acme//kit//client//reset + status: not-found diff --git a/test/projectgraph/cases/go/stdlib-callback-scope/project/go.mod b/test/projectgraph/cases/go/stdlib-callback-scope/project/go.mod new file mode 100644 index 00000000..c7090c9c --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-scope/project/go.mod @@ -0,0 +1,5 @@ +module app + +go 1.22 + +require github.com/acme/kit v1.0.0 diff --git a/test/projectgraph/cases/go/stdlib-callback-scope/project/gomod/github.com/acme/kit@v1.0.0/go.mod b/test/projectgraph/cases/go/stdlib-callback-scope/project/gomod/github.com/acme/kit@v1.0.0/go.mod new file mode 100644 index 00000000..529dd612 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-scope/project/gomod/github.com/acme/kit@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module github.com/acme/kit + +go 1.22 diff --git a/test/projectgraph/cases/go/stdlib-callback-scope/project/gomod/github.com/acme/kit@v1.0.0/kit.go b/test/projectgraph/cases/go/stdlib-callback-scope/project/gomod/github.com/acme/kit@v1.0.0/kit.go new file mode 100644 index 00000000..9cde35e8 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-scope/project/gomod/github.com/acme/kit@v1.0.0/kit.go @@ -0,0 +1,19 @@ +package kit + +func Run() { start() } + +func start() {} + +func Dial() { handshake() } + +func handshake() {} + +type client struct{} + +func (c *client) Close() { closeConn() } + +func closeConn() {} + +func (c *client) reset() { resetConn() } + +func resetConn() {} diff --git a/test/projectgraph/cases/go/stdlib-callback-scope/project/main.go b/test/projectgraph/cases/go/stdlib-callback-scope/project/main.go new file mode 100644 index 00000000..492e1560 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-scope/project/main.go @@ -0,0 +1,12 @@ +package main + +import ( + "os" + + "github.com/acme/kit" +) + +func main() { + os.Getenv("HOME") + kit.Run() +} 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 new file mode 100644 index 00000000..16619903 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-scope/summaries/go_github.com_acme_kit_v1.0.0.json @@ -0,0 +1,23 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:github.com/acme/kit@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//kit//Dial", + "definitions": [ + "go:github.com/acme/kit@v1.0.0//kit.go//Dial" + ], + "reaches": [] + }, + { + "key": "github.com//acme//kit//Run", + "definitions": [ + "go:github.com/acme/kit@v1.0.0//kit.go//Run" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/go/stdlib-callback/expect.yaml b/test/projectgraph/cases/go/stdlib-callback/expect.yaml new file mode 100644 index 00000000..c0c14272 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback/expect.yaml @@ -0,0 +1,53 @@ +source: safedep:hand +description: >- + The shape of melody and gorilla/websocket. main calls ReadMessage of Conn of the unit github.com/acme/ws. + ReadMessage passes the reader that NextReader gives, a messageReader, to io.ReadAll. No unit gives the + standard library, so the graph holds no code of io.ReadAll. That code calls Read of the value, and Read calls + advance. The standard library can call a method of an exported name on a value that it holds, so the key + io//ReadAll has a callback record. Read and advance are uncertain, not not-found. NextReader has a path. +units: + gomod/github.com/acme/ws@v1.0.0: go:github.com/acme/ws@v1.0.0 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: io//ReadAll + reason: no-dependency-source + callback: true +results: + - target: github.com//acme//ws//messageReader//Read + status: uncertain + uncertainties: + - kind: unresolved-call + - target: github.com//acme//ws//Conn//advance + status: uncertain + call_sites: + - caller: go:github.com/acme/ws@v1.0.0//conn.go//messageReader//Read + site: go:github.com/acme/ws@v1.0.0//conn.go:18 + reached: false + uncertainties: + - kind: unresolved-call + - target: github.com//acme//ws//Conn//NextReader + status: found + path: + - key: main.go//main + - key: github.com//acme//ws//Conn//ReadMessage + site: main.go:7 + kind: direct + confidence: high + - key: go:github.com/acme/ws@v1.0.0//conn.go//Conn//ReadMessage + site: go:github.com/acme/ws@v1.0.0//conn.go:7 + kind: package + confidence: high + - key: go:github.com/acme/ws@v1.0.0//conn.go//Conn//NextReader + site: go:github.com/acme/ws@v1.0.0//conn.go:8 + kind: member + confidence: high + call_sites: + - caller: go:github.com/acme/ws@v1.0.0//conn.go//Conn//ReadMessage + site: go:github.com/acme/ws@v1.0.0//conn.go:8 + reached: true diff --git a/test/projectgraph/cases/go/stdlib-callback/project/go.mod b/test/projectgraph/cases/go/stdlib-callback/project/go.mod new file mode 100644 index 00000000..fd6f5e91 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback/project/go.mod @@ -0,0 +1,5 @@ +module app + +go 1.22 + +require github.com/acme/ws v1.0.0 diff --git a/test/projectgraph/cases/go/stdlib-callback/project/gomod/github.com/acme/ws@v1.0.0/conn.go b/test/projectgraph/cases/go/stdlib-callback/project/gomod/github.com/acme/ws@v1.0.0/conn.go new file mode 100644 index 00000000..48036811 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback/project/gomod/github.com/acme/ws@v1.0.0/conn.go @@ -0,0 +1,23 @@ +package ws + +import "io" + +type Conn struct{} + +func (c *Conn) ReadMessage() ([]byte, error) { + return io.ReadAll(c.NextReader()) +} + +func (c *Conn) NextReader() io.Reader { + return &messageReader{c: c} +} + +type messageReader struct{ c *Conn } + +func (r *messageReader) Read(p []byte) (int, error) { + return r.c.advance(p) +} + +func (c *Conn) advance(p []byte) (int, error) { + return len(p), nil +} diff --git a/test/projectgraph/cases/go/stdlib-callback/project/gomod/github.com/acme/ws@v1.0.0/go.mod b/test/projectgraph/cases/go/stdlib-callback/project/gomod/github.com/acme/ws@v1.0.0/go.mod new file mode 100644 index 00000000..d7c2bd13 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback/project/gomod/github.com/acme/ws@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module github.com/acme/ws + +go 1.22 diff --git a/test/projectgraph/cases/go/stdlib-callback/project/main.go b/test/projectgraph/cases/go/stdlib-callback/project/main.go new file mode 100644 index 00000000..0073a3e3 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback/project/main.go @@ -0,0 +1,8 @@ +package main + +import "github.com/acme/ws" + +func main() { + var c ws.Conn + c.ReadMessage() +} 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 new file mode 100644 index 00000000..794a06fe --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback/summaries/go_github.com_acme_ws_v1.0.0.json @@ -0,0 +1,50 @@ +{ + "version": "2", + "index_version": "15", + "link_version": "17", + "unit": "go:github.com/acme/ws@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//ws//Conn", + "definitions": [ + "go:github.com/acme/ws@v1.0.0//conn.go//Conn" + ], + "reaches": [] + }, + { + "key": "github.com//acme//ws//Conn//NextReader", + "definitions": [ + "go:github.com/acme/ws@v1.0.0//conn.go//Conn//NextReader" + ], + "reaches": [] + }, + { + "key": "github.com//acme//ws//Conn//ReadMessage", + "definitions": [ + "go:github.com/acme/ws@v1.0.0//conn.go//Conn//ReadMessage" + ], + "reaches": [ + "io//ReadAll" + ], + "uncertainties": [ + 0 + ] + } + ], + "targets": [ + "io//ReadAll" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true, + "targets": [ + "io//ReadAll" + ] + } + ] +} diff --git a/test/projectgraph/cases/go/struct-method/expect.yaml b/test/projectgraph/cases/go/struct-method/expect.yaml index 40c680b5..e003fa1d 100644 --- a/test/projectgraph/cases/go/struct-method/expect.yaml +++ b/test/projectgraph/cases/go/struct-method/expect.yaml @@ -1,5 +1,5 @@ source: safedep:hand -description: s holds the result of store.New, whose first result is *Store, so s.Put resolves to the method of Store. Put calls Encode on the field enc, whose declared type is *Encoder, and another file of the package defines the method. No root calls Drop. +description: s holds the result of store.New, whose first result is *Store, so s.Put resolves to the method of Store. Put calls Encode on the field enc, whose declared type is *Encoder, and another file of the package defines the method. No root calls Drop. No unit gives yaml.v3, so the code that the project calls in it is outside the graph and can call Unmarshal. Unmarshal is uncertain, not not-found. roots: - key: cmd/app/main.go kind: main @@ -28,7 +28,11 @@ no_edges: to: store/store.go//Store//Drop - from: store/store.go//Store//Put to: store/encoder.go//Encoder//Remove -unresolved: [] +unresolved: + - caller: gopkg.in//yaml.v3//Marshal + reason: no-dependency-source + - caller: gopkg.in//yaml.v3//Unmarshal + reason: no-dependency-source results: - target: gopkg.in//yaml.v3//Marshal status: found @@ -51,8 +55,10 @@ results: site: store/encoder.go:8 reached: true - target: gopkg.in//yaml.v3//Unmarshal - status: not-found + status: uncertain call_sites: - caller: store/encoder.go//Encoder//Remove site: store/encoder.go:12 reached: false + uncertainties: + - kind: unresolved-call diff --git a/test/projectgraph/cases/go/transitive-init/expect.yaml b/test/projectgraph/cases/go/transitive-init/expect.yaml index f6185996..5e461801 100644 --- a/test/projectgraph/cases/go/transitive-init/expect.yaml +++ b/test/projectgraph/cases/go/transitive-init/expect.yaml @@ -21,7 +21,10 @@ edges: no_edges: - from: cmd/app/main.go to: mid/doc.go -unresolved: [] +unresolved: + - caller: net//http//HandleFunc + reason: no-dependency-source + callback: true results: - target: net//http//HandleFunc status: found diff --git a/test/projectgraph/cases/go/two-package-clauses/expect.yaml b/test/projectgraph/cases/go/two-package-clauses/expect.yaml index f33c6131..7255de3d 100644 --- a/test/projectgraph/cases/go/two-package-clauses/expect.yaml +++ b/test/projectgraph/cases/go/two-package-clauses/expect.yaml @@ -29,7 +29,13 @@ no_edges: to: lib/gen.go//parse - from: cmd/app/main.go to: lib/gen.go -unresolved: [] +unresolved: + - caller: encoding//json//Unmarshal + reason: no-dependency-source + callback: true + - caller: os//WriteFile + reason: no-dependency-source + callback: true truncated: - kind: package-clauses key: lib/gen.go diff --git a/test/projectgraph/cases_test.go b/test/projectgraph/cases_test.go index 2ece6fec..2f97d46f 100644 --- a/test/projectgraph/cases_test.go +++ b/test/projectgraph/cases_test.go @@ -103,10 +103,11 @@ type edge struct { } type unresolved struct { - Caller string `yaml:"caller"` - Site string `yaml:"site"` - Reason string `yaml:"reason"` - Method string `yaml:"method,omitempty"` + Caller string `yaml:"caller"` + Site string `yaml:"site"` + Reason string `yaml:"reason"` + Method string `yaml:"method,omitempty"` + Callback bool `yaml:"callback,omitempty"` } // result is one reach query of a case and its result. Sources are the @@ -363,7 +364,7 @@ func edgesOf(g *projectgraph.Graph) []edge { func unresolvedOf(g *projectgraph.Graph) []unresolved { out := []unresolved{} for _, u := range g.Unresolved { - out = append(out, unresolved{Caller: u.Caller, Site: site(u.Site), Reason: string(u.Reason), Method: u.Method}) + out = append(out, unresolved{Caller: u.Caller, Site: site(u.Site), Reason: string(u.Reason), Method: u.Method, Callback: u.Callback}) } return out } From b86cb26c394de37c5fe7c979fb9ae0251490c651 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:14:57 +0000 Subject: [PATCH 17/58] chore: bump fileindex, projectgraph, reach and summary versions The fixes of this branch change the index, the graph, the reach results and the summaries. A store must not serve data of the old rules, and the version tests need one row per version. So this commit bumps each version once for the whole branch: fileindex.Version 16, projectgraph.Version 18, reach.Version 5 and summary.Version 3. It adds the hash rows and the version fields of the test data. 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 +- projectgraph/reach/reach.go | 2 +- projectgraph/summary/summary.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 | 6 +++--- .../summaries/go_github.com_acme_srv_v1.0.0.json | 6 +++--- .../summaries/go_example.com_markup_v1.0.0.json | 6 +++--- .../summaries/go_std_go1.25.1.json | 6 +++--- .../summaries/go_github.com_acme_srv_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_a_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_b_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_kit_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_dep_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_kit_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_kit_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_yamlish_v1.2.0.json | 6 +++--- .../summaries/go_github.com_acme_kit_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_kit_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_kit_v1.0.0.json | 6 +++--- .../summaries/go_example.com_markup_v1.0.0.json | 6 +++--- .../summaries/go_std_go1.25.1.json | 6 +++--- .../summaries/go_github.com_acme_kit_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_kit_v1.0.0.json | 6 +++--- .../summaries/go_github.com_acme_ws_v1.0.0.json | 6 +++--- .../summaries/npm_store-kit_1.0.0.json | 6 +++--- .../summaries/npm_kit_1.0.0.json | 6 +++--- .../summaries/npm_kit_1.0.0.json | 6 +++--- .../summaries/npm_plug_1.0.0.json | 6 +++--- .../summaries/npm_util-kit_1.0.0.json | 6 +++--- .../summaries/npm_lodash._reinterpolate_3.0.0.json | 6 +++--- .../summaries/npm_lodash_4.17.21.json | 6 +++--- .../summaries/pypi_speedy_1.0.0.json | 6 +++--- .../summaries/pypi_PyYAML_6.0.1.json | 6 +++--- .../summaries/pypi_badpkg_1.0.0.json | 6 +++--- .../summaries/pypi_okpkg_1.0.0.json | 6 +++--- .../summaries/pypi_badpkg_1.0.0.json | 6 +++--- .../summaries/pypi_okpkg_1.0.0.json | 6 +++--- .../summaries/pypi_google-auth_2.0.0.json | 6 +++--- .../summaries/pypi_google-cloud-storage_2.1.0.json | 6 +++--- .../summaries/pypi_outer_1.0.0.json | 6 +++--- .../summaries/pypi_PyYAML_6.0.1.json | 6 +++--- .../summaries/pypi_requests_2.31.0.json | 6 +++--- .../summaries/pypi_urllib3_2.0.7.json | 6 +++--- .../summaries/python_std_3.12.3.json | 6 +++--- .../summaries/pypi_dep_1.0.0.json | 6 +++--- .../summaries/pypi_dep_1.0.0.json | 6 +++--- .../crate-bindings/summaries/cargo_kit_1.0.0.json | 6 +++--- .../dependency-crate/summaries/cargo_kit_1.0.0.json | 6 +++--- .../summaries/cargo_kit_1.0.0.json | 6 +++--- .../summaries/cargo_kit_1.0.0.json | 6 +++--- .../rust/glob-use/summaries/cargo_dep_1.2.0.json | 6 +++--- .../summaries/cargo_kit_1.0.0.json | 6 +++--- .../rust/macro-import/summaries/cargo_dep_1.2.0.json | 6 +++--- .../summaries/cargo_dep_1.2.0.json | 6 +++--- .../rust/trait-object/summaries/cargo_dep_1.2.0.json | 6 +++--- test/projectgraph/version_test.go | 3 +++ 144 files changed, 304 insertions(+), 300 deletions(-) diff --git a/fileindex/index.go b/fileindex/index.go index a05a3204..c0e97d0e 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -19,7 +19,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 = "15" +const Version = "16" // 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 53e6bc85..655f2247 100644 --- a/fileindex/testdata/index.json +++ b/fileindex/testdata/index.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "path": "src/app.py", "language": "python", "content_hash": "a172cedcae47474b615c54d510a5d84a8dea3032e958587430b413538be3f333", diff --git a/plugin/indexer/plugin_test.go b/plugin/indexer/plugin_test.go index 1d9fc57b..912ef2b0 100644 --- a/plugin/indexer/plugin_test.go +++ b/plugin/indexer/plugin_test.go @@ -106,6 +106,7 @@ var goldenHashes = map[string]string{ "13": "97614d8b96782e65813ee409e4a3f33c4655157b5dfa58baf17cd3677958c619", "14": "26f22c80fd4120cd04f228a53188accb1695434d0ed1ff5654b110981f8f44dd", "15": "c4b949e72be5b2b4474e06f20ae18b0cc4e080b6c5fe20ccaf712c3714bf2a12", + "16": "235591d02eeb5e1bb771fbacf0ba6db228146a6a70c55670de6cb93392d75d81", } func TestFileIndexGoldenHashFollowsVersion(t *testing.T) { diff --git a/plugin/indexer/testdata/golden/App.cs.json b/plugin/indexer/testdata/golden/App.cs.json index aded0e45..eeed7e2c 100644 --- a/plugin/indexer/testdata/golden/App.cs.json +++ b/plugin/indexer/testdata/golden/App.cs.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 1d3d79b5..5b5fab38 100644 --- a/plugin/indexer/testdata/golden/App.java.json +++ b/plugin/indexer/testdata/golden/App.java.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 7c8f04a9..9ae8908f 100644 --- a/plugin/indexer/testdata/golden/App.kt.json +++ b/plugin/indexer/testdata/golden/App.kt.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 5d3938ae..e56de8c4 100644 --- a/plugin/indexer/testdata/golden/app.go.json +++ b/plugin/indexer/testdata/golden/app.go.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 e96db400..3e4f2cfe 100644 --- a/plugin/indexer/testdata/golden/app.js.json +++ b/plugin/indexer/testdata/golden/app.js.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 5f5c421a..2421e377 100644 --- a/plugin/indexer/testdata/golden/app.php.json +++ b/plugin/indexer/testdata/golden/app.php.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 5f4e785a..d0977a83 100644 --- a/plugin/indexer/testdata/golden/app.py.json +++ b/plugin/indexer/testdata/golden/app.py.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 0a04df74..91f987a7 100644 --- a/plugin/indexer/testdata/golden/app.rb.json +++ b/plugin/indexer/testdata/golden/app.rb.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 0960c899..bc202bbc 100644 --- a/plugin/indexer/testdata/golden/app.rs.json +++ b/plugin/indexer/testdata/golden/app.rs.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 159d8555..de5bc283 100644 --- a/plugin/indexer/testdata/golden/app.ts.json +++ b/plugin/indexer/testdata/golden/app.ts.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 0d17a1b1..b44ef90b 100644 --- a/plugin/indexer/testdata/golden/exports.js.json +++ b/plugin/indexer/testdata/golden/exports.js.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 6321a428..4f230ace 100644 --- a/plugin/indexer/testdata/golden/exports.py.json +++ b/plugin/indexer/testdata/golden/exports.py.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 e4cd7a74..a76cf808 100644 --- a/plugin/indexer/testdata/golden/exports.ts.json +++ b/plugin/indexer/testdata/golden/exports.ts.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 bd504c2f..2e1bcde4 100644 --- a/plugin/indexer/testdata/golden/flags.js.json +++ b/plugin/indexer/testdata/golden/flags.js.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 9031c0c6..c49674dc 100644 --- a/plugin/indexer/testdata/golden/flags.py.json +++ b/plugin/indexer/testdata/golden/flags.py.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "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 1adb53a4..e75b532a 100644 --- a/plugin/indexer/testdata/golden/flags.ts.json +++ b/plugin/indexer/testdata/golden/flags.ts.json @@ -1,5 +1,5 @@ { - "version": "15", + "version": "16", "path": "flags.ts", "language": "typescript", "content_hash": "a84c4ad21735c1c250e33317463237749489ea1f220a93f97e2bea3f07b040c4", diff --git a/projectgraph/graph.go b/projectgraph/graph.go index 2ce006d9..fcb2beb9 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 = "17" +const Version = "18" type NodeKind string diff --git a/projectgraph/reach/reach.go b/projectgraph/reach/reach.go index baf2f221..e0386b67 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 = "4" +const Version = "5" const ( defaultMaxDepth = 64 diff --git a/projectgraph/summary/summary.go b/projectgraph/summary/summary.go index 392d74ea..766b9a4f 100644 --- a/projectgraph/summary/summary.go +++ b/projectgraph/summary/summary.go @@ -27,7 +27,7 @@ import ( // Version is the version of the summary format and of the rules that fill // it. Bump it when a field or a rule changes. A store keys a summary by // Version, IndexVersion and LinkVersion. -const Version = "2" +const Version = "3" // Summary is the summary of one package unit. Symbols are in key order. // Targets holds every external symbol that the code of the unit calls, diff --git a/projectgraph/testdata/javascript/barrel-chain/graph.golden.json b/projectgraph/testdata/javascript/barrel-chain/graph.golden.json index b24dac40..865bbc47 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": "17", + "version": "18", "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 75c2b418..0b002b43 100644 --- a/projectgraph/testdata/javascript/barrel-chain/indexes.json +++ b/projectgraph/testdata/javascript/barrel-chain/indexes.json @@ -1,22 +1,22 @@ [ { - "version": "15", "path": "src/main.js", "language": "javascript", + "version": "16", "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": "15", "path": "src/api/index.js", "language": "javascript", + "version": "16", "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": "15", "path": "src/core/index.js", "language": "javascript", + "version": "16", "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": "15", "path": "src/core/parse.js", "language": "javascript", + "version": "16", "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 a245016f..505db74a 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": "17", + "version": "18", "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 e4ec9795..b529ac24 100644 --- a/projectgraph/testdata/javascript/commonjs-require/indexes.json +++ b/projectgraph/testdata/javascript/commonjs-require/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "main.js", "language": "javascript", + "version": "16", "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": "15", "path": "lib/index.js", "language": "javascript", + "version": "16", "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": "15", "path": "helper.js", "language": "javascript", + "version": "16", "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 a1b1f7ba..8778c03b 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": "17", + "version": "18", "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 4a1637a8..f43e6fb7 100644 --- a/projectgraph/testdata/javascript/default-import/indexes.json +++ b/projectgraph/testdata/javascript/default-import/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "main.js", "language": "javascript", + "version": "16", "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": "15", "path": "log.js", "language": "javascript", + "version": "16", "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": "15", "path": "make.js", "language": "javascript", + "version": "16", "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 69342230..0b8d3473 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": "17", + "version": "18", "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 5341e191..7f761be4 100644 --- a/projectgraph/testdata/javascript/esm-named-import/indexes.json +++ b/projectgraph/testdata/javascript/esm-named-import/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "src/main.js", "language": "javascript", + "version": "16", "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": "15", "path": "src/parser.js", "language": "javascript", + "version": "16", "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": "15", "path": "src/util.js", "language": "javascript", + "version": "16", "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 0987ca09..eeb6e5e1 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": "17", + "version": "18", "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 e5926a2f..4046a108 100644 --- a/projectgraph/testdata/javascript/export-star/indexes.json +++ b/projectgraph/testdata/javascript/export-star/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "main.js", "language": "javascript", + "version": "16", "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": "15", "path": "all.js", "language": "javascript", + "version": "16", "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": "15", "path": "a.js", "language": "javascript", + "version": "16", "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": "15", "path": "b.js", "language": "javascript", + "version": "16", "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": "15", "path": "c.js", "language": "javascript", + "version": "16", "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 383f42a2..7f9522d1 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": "17", + "version": "18", "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 74577682..db20bd33 100644 --- a/projectgraph/testdata/javascript/exports-conditions/indexes.json +++ b/projectgraph/testdata/javascript/exports-conditions/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "app/main.mjs", "language": "javascript", + "version": "16", "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": "15", "path": "lib/esm/index.mjs", "language": "javascript", + "version": "16", "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": "15", "path": "lib/cjs/index.cjs", "language": "javascript", + "version": "16", "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": "15", "path": "lib/src/feature/y.js", "language": "javascript", + "version": "16", "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 33a12cc9..769f397f 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": "17", + "version": "18", "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 6ab9056d..feb78b07 100644 --- a/projectgraph/testdata/javascript/unknown-receiver/indexes.json +++ b/projectgraph/testdata/javascript/unknown-receiver/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", + "version": "16", "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 6ae5010b..38a159e4 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": "17", + "version": "18", "files": 3, "nodes": 6, "edges": 5, diff --git a/projectgraph/testdata/javascript/workspaces/indexes.json b/projectgraph/testdata/javascript/workspaces/indexes.json index 52b12038..14feff26 100644 --- a/projectgraph/testdata/javascript/workspaces/indexes.json +++ b/projectgraph/testdata/javascript/workspaces/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "packages/app/src/main.js", "language": "javascript", + "version": "16", "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": "15", "path": "packages/core/lib/index.js", "language": "javascript", + "version": "16", "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": "15", "path": "packages/core/util.js", "language": "javascript", + "version": "16", "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 78a625e1..54549440 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": "17", + "version": "18", "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 4bde401b..a6a6064d 100644 --- a/projectgraph/testdata/python/all-hides/indexes.json +++ b/projectgraph/testdata/python/all-hides/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "main.py", "language": "python", + "version": "16", "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": "15", "path": "pkg/__init__.py", "language": "python", + "version": "16", "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": "15", "path": "pkg/a.py", "language": "python", + "version": "16", "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 21e85fe1..1036affb 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": "17", + "version": "18", "files": 1, "nodes": 3, "edges": 2, diff --git a/projectgraph/testdata/python/assignment/indexes.json b/projectgraph/testdata/python/assignment/indexes.json index d9ed405e..2bb03823 100644 --- a/projectgraph/testdata/python/assignment/indexes.json +++ b/projectgraph/testdata/python/assignment/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "main.py", "language": "python", + "version": "16", "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 e922a79d..7b4cf804 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": "17", + "version": "18", "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 ed28f565..6c463e77 100644 --- a/projectgraph/testdata/python/class-body/indexes.json +++ b/projectgraph/testdata/python/class-body/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", + "version": "16", "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 361df88b..aeeb6bc2 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": "17", + "version": "18", "files": 1, "nodes": 9, "edges": 5, diff --git a/projectgraph/testdata/python/decorators/indexes.json b/projectgraph/testdata/python/decorators/indexes.json index d706dded..fb3c1026 100644 --- a/projectgraph/testdata/python/decorators/indexes.json +++ b/projectgraph/testdata/python/decorators/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", + "version": "16", "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 71af1f03..9bd2b241 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": "17", + "version": "18", "files": 1, "nodes": 5, "edges": 3, diff --git a/projectgraph/testdata/python/external/indexes.json b/projectgraph/testdata/python/external/indexes.json index 85e263a6..d32422bb 100644 --- a/projectgraph/testdata/python/external/indexes.json +++ b/projectgraph/testdata/python/external/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "main.py", "language": "python", + "version": "16", "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 905b7c85..2e904a4f 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": "17", + "version": "18", "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 fccc8671..f48f01e2 100644 --- a/projectgraph/testdata/python/local-receiver/indexes.json +++ b/projectgraph/testdata/python/local-receiver/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", + "version": "16", "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 377756e7..d33e98aa 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": "17", + "version": "18", "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 5c4112b9..36d770e8 100644 --- a/projectgraph/testdata/python/member-call/indexes.json +++ b/projectgraph/testdata/python/member-call/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "main.py", "language": "python", + "version": "16", "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": "15", "path": "parser.py", "language": "python", + "version": "16", "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": "15", "path": "base.py", "language": "python", + "version": "16", "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 fbfcb273..dc98a83c 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": "17", + "version": "18", "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 b2b9b228..904544ca 100644 --- a/projectgraph/testdata/python/missing-file/indexes.json +++ b/projectgraph/testdata/python/missing-file/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "main.py", "language": "python", + "version": "16", "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 978468df..6739ceea 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": "17", + "version": "18", "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 e9981571..84f0963a 100644 --- a/projectgraph/testdata/python/package-init/indexes.json +++ b/projectgraph/testdata/python/package-init/indexes.json @@ -1,17 +1,17 @@ [ { - "version": "15", "path": "main.py", "language": "python", + "version": "16", "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": "15", "path": "pkg/__init__.py", "language": "python", + "version": "16", "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": "15", "path": "pkg/loader.py", "language": "python", + "version": "16", "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 104e88d7..402dc9f1 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": "17", + "version": "18", "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 a94c2482..4f61c8a3 100644 --- a/projectgraph/testdata/python/reexport-cycle/indexes.json +++ b/projectgraph/testdata/python/reexport-cycle/indexes.json @@ -1,17 +1,17 @@ [ { - "version": "15", "path": "main.py", "language": "python", + "version": "16", "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": "15", "path": "a/__init__.py", "language": "python", + "version": "16", "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": "15", "path": "b/__init__.py", "language": "python", + "version": "16", "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 4f84f186..ff016eb1 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": "17", + "version": "18", "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 f682cd9d..68bb7eb2 100644 --- a/projectgraph/testdata/python/relative-imports/indexes.json +++ b/projectgraph/testdata/python/relative-imports/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "pkg/sub/a.py", "language": "python", + "version": "16", "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": "15", "path": "pkg/sub/b.py", "language": "python", + "version": "16", "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": "15", "path": "pkg/c.py", "language": "python", + "version": "16", "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 9545e3bd..22296c2b 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": "17", + "version": "18", "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 bb9410bb..cd6aa575 100644 --- a/projectgraph/testdata/python/sibling-import/indexes.json +++ b/projectgraph/testdata/python/sibling-import/indexes.json @@ -1,12 +1,12 @@ [ { - "version": "15", "path": "main.py", "language": "python", + "version": "16", "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": "15", "path": "handlers.py", "language": "python", + "version": "16", "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 77e1ff1b..426b2304 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": "17", + "version": "18", "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 57d71afc..0da84fa5 100644 --- a/projectgraph/testdata/python/src-layout/indexes.json +++ b/projectgraph/testdata/python/src-layout/indexes.json @@ -1,16 +1,16 @@ [ { - "version": "15", "path": "services/api/src/api/__init__.py", "language": "python", + "version": "16", "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": "15", "path": "services/api/src/api/cli.py", "language": "python", + "version": "16", "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": "15", "path": "services/api/src/api/core.py", "language": "python", + "version": "16", "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 1d330ec4..15cfe389 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": "17", + "version": "18", "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 4e0ab812..d8120327 100644 --- a/projectgraph/testdata/python/sys-path/indexes.json +++ b/projectgraph/testdata/python/sys-path/indexes.json @@ -1,12 +1,12 @@ [ { - "version": "15", "path": "app/main.py", "language": "python", + "version": "16", "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": "15", "path": "lib/shared.py", "language": "python", + "version": "16", "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 386358f9..a163f008 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": "17", + "version": "18", "files": 2, "nodes": 4, "edges": 3, diff --git a/projectgraph/testdata/python/wildcard/indexes.json b/projectgraph/testdata/python/wildcard/indexes.json index a93d6767..6178747d 100644 --- a/projectgraph/testdata/python/wildcard/indexes.json +++ b/projectgraph/testdata/python/wildcard/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "main.py", "language": "python", + "version": "16", "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": "15", "path": "util.py", "language": "python", + "version": "16", "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 b3287dc2..7f7b79fe 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": "17", + "version": "18", "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 1844aae8..e0415772 100644 --- a/projectgraph/testdata/roots/extra-rules/indexes.json +++ b/projectgraph/testdata/roots/extra-rules/indexes.json @@ -1,10 +1,10 @@ [ { - "version": "15", "path": "app/users/route.ts", "language": "typescript", + "version": "16", "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": "15", "path": "src/handler.js", "language": "javascript", + "version": "16", "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": "15", "path": "views.py", "language": "python", + "version": "16", "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 51665388..da844439 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": "17", + "version": "18", "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 c52849e9..a3d9cf18 100644 --- a/projectgraph/testdata/roots/library-surface/indexes.json +++ b/projectgraph/testdata/roots/library-surface/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "lib/api.py", "language": "python", + "version": "16", "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": "15", "path": "lib/test_api.py", "language": "python", + "version": "16", "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 83944145..0fc5c998 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": "17", + "version": "18", "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 ecd29e9d..35a00d96 100644 --- a/projectgraph/testdata/roots/main-guard/indexes.json +++ b/projectgraph/testdata/roots/main-guard/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "app.py", "language": "python", "main_guard": true, + "version": "16", "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": "15", "path": "lib.py", "language": "python", + "version": "16", "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 9a15b3e7..1859dd4b 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": "17", + "version": "18", "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 bf531a62..36885795 100644 --- a/projectgraph/testdata/roots/package-json-bin/indexes.json +++ b/projectgraph/testdata/roots/package-json-bin/indexes.json @@ -1,12 +1,12 @@ [ { - "version": "15", "path": "bin/cli.js", "language": "javascript", + "version": "16", "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": "15", "path": "lib/run.js", "language": "javascript", + "version": "16", "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 4808ac57..4ab3c13a 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": "17", + "version": "18", "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 abf83c27..074ce92e 100644 --- a/projectgraph/testdata/roots/package-json-main/indexes.json +++ b/projectgraph/testdata/roots/package-json-main/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", + "version": "16", "path": "cli.js", "language": "javascript", "exports": [ @@ -30,7 +30,7 @@ ] }, { - "version": "15", + "version": "16", "path": "fmt.js", "language": "javascript", "exports": [ @@ -60,7 +60,7 @@ ] }, { - "version": "15", + "version": "16", "path": "index.js", "language": "javascript", "exports": [ @@ -104,7 +104,7 @@ ] }, { - "version": "15", + "version": "16", "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 1c376666..210bb426 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": "17", + "version": "18", "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 c1cd3d5f..2ae71e39 100644 --- a/projectgraph/testdata/roots/procfile-dockerfile/indexes.json +++ b/projectgraph/testdata/roots/procfile-dockerfile/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", + "version": "16", "path": "app/wsgi.py", "language": "python", "definitions": [ @@ -23,7 +23,7 @@ ] }, { - "version": "15", + "version": "16", "path": "app/cli.py", "language": "python", "definitions": [ @@ -38,7 +38,7 @@ ] }, { - "version": "15", + "version": "16", "path": "app/__init__.py", "language": "python", "definitions": [ @@ -53,7 +53,7 @@ ] }, { - "version": "15", + "version": "16", "path": "worker.py", "language": "python", "definitions": [ @@ -68,7 +68,7 @@ ] }, { - "version": "15", + "version": "16", "path": "ignored.py", "language": "python", "definitions": [ @@ -83,7 +83,7 @@ ] }, { - "version": "15", + "version": "16", "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 600ba456..a799a608 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": "17", + "version": "18", "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 d020c4e9..eb785ce6 100644 --- a/projectgraph/testdata/roots/pyproject-scripts/indexes.json +++ b/projectgraph/testdata/roots/pyproject-scripts/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", + "version": "16", "path": "src/app/cli.py", "language": "python", "definitions": [ @@ -23,7 +23,7 @@ ] }, { - "version": "15", + "version": "16", "path": "src/app/tool.py", "language": "python", "definitions": [ @@ -46,7 +46,7 @@ ] }, { - "version": "15", + "version": "16", "path": "src/app/plugin.py", "language": "python", "definitions": [ @@ -78,7 +78,7 @@ ] }, { - "version": "15", + "version": "16", "path": "src/app/legacy.py", "language": "python", "definitions": [ @@ -93,7 +93,7 @@ ] }, { - "version": "15", + "version": "16", "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 11784999..0ad976ef 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": "17", + "version": "18", "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 159af41e..1236d66c 100644 --- a/projectgraph/testdata/roots/setup-py/indexes.json +++ b/projectgraph/testdata/roots/setup-py/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", + "version": "16", "path": "tool/main.py", "language": "python", "definitions": [ @@ -23,7 +23,7 @@ ] }, { - "version": "15", + "version": "16", "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 6453dc54..d355f567 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": "17", + "version": "18", "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 443885be..13953dcc 100644 --- a/projectgraph/testdata/roots/test-file/indexes.json +++ b/projectgraph/testdata/roots/test-file/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "tests/test_app.py", "language": "python", + "version": "16", "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 a040c9fe..22cb2426 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": "17", + "version": "18", "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 95c01af5..59cbdd59 100644 --- a/projectgraph/testdata/typescript/js-specifier/indexes.json +++ b/projectgraph/testdata/typescript/js-specifier/indexes.json @@ -1,12 +1,12 @@ [ { - "version": "15", "path": "src/main.ts", "language": "typescript", + "version": "16", "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": "15", "path": "src/util.ts", "language": "typescript", + "version": "16", "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 63378f5d..c1e90122 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": "17", + "version": "18", "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 4dd00b21..7418d5d8 100644 --- a/projectgraph/testdata/typescript/tsconfig-paths/indexes.json +++ b/projectgraph/testdata/typescript/tsconfig-paths/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "src/main.ts", "language": "typescript", + "version": "16", "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": "15", "path": "src/app/server.ts", "language": "typescript", + "version": "16", "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": "15", "path": "src/config.ts", "language": "typescript", + "version": "16", "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 a15569ab..a0be7328 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": "17", + "version": "18", "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 6db64bb1..22df4bbc 100644 --- a/projectgraph/testdata/typescript/type-only/indexes.json +++ b/projectgraph/testdata/typescript/type-only/indexes.json @@ -1,6 +1,6 @@ [ { - "version": "15", "path": "src/main.ts", "language": "typescript", + "version": "16", "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": "15", "path": "src/types.ts", "language": "typescript", + "version": "16", "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": "15", "path": "src/decl.d.ts", "language": "typescript", + "version": "16", "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 1c97e9b2..d887082e 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 62d26d28..603d9c9c 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 efcb550d..e0da8fb1 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 6499a8ad..7c352d86 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 256fb42e..cf32a69b 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 340352e6..59ccf10a 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 bc5372f3..be8b4d52 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 6250dcb0..522906e2 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 83589a10..ddf7042e 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 92ceff86..50b88ae8 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 2a476220..3e6c48d2 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 143f2e1d..e285c37d 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 62186a87..aaa90ac4 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 6250dcb0..522906e2 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 a43b032b..d77e0b19 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 3024cd49..6e5f7d2e 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 b3a973f0..ac5cfad1 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "go:std@go1.25.1", "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 4149be74..3a3c78da 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "go:github.com/acme/kit@v1.0.0", "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 16619903..bcb0ba67 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 794a06fe..014417f5 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "go:github.com/acme/ws@v1.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 cd771458..b8b95a3f 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 2c39dae7..b6fd33e3 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 2c39dae7..b6fd33e3 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 3d525bbd..fb9f4de1 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 0839f61c..ff8dc49b 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 3265721f..6da5f9d8 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 766e9a82..a10bbbcc 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 22a34a9f..78254678 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "pypi:speedy@1.0.0", "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 3f50d6a1..2fd267bc 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 c1762ee2..b82d1f86 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 75940477..571e973e 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 c1762ee2..b82d1f86 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 75940477..571e973e 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 1e7a400f..cb2b0b6b 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 6586eb5d..c7329383 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 4d777061..65c4753a 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 3f50d6a1..2fd267bc 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 9f426c70..a22d03c2 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 620b0a77..f7671ddd 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 0c1cb890..a8ed36ca 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 657312ba..33e4eca4 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 657312ba..33e4eca4 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 e0d9f46f..49d0517f 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 4991c200..0fc6f041 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 e43ee48b..8308d1c4 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 e43ee48b..8308d1c4 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "cargo:kit@1.0.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 12f7c4e6..a88279ec 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "cargo:dep@1.2.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 1f075b50..1109a3ed 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "cargo:kit@1.0.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 e4d8fec0..48a89996 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 85672d3c..7eca2165 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "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 f8846920..a74e9419 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": "2", - "index_version": "15", - "link_version": "17", + "version": "3", + "index_version": "16", + "link_version": "18", "unit": "cargo:dep@1.2.0", "symbols": [ { diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index a07e8df3..535b89ca 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -32,6 +32,7 @@ var graphHashes = map[string]string{ "14/15": "bbc708aba767ccf554c75a7e06816abda3fc1804ad6d0876ad8ce9575f4c3a88", "14/16": "823b1f82c4aed8ba27d19275505c8d1dee755086be48fcda64d323f55b4c62a5", "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", + "16/18": "22947eb8bae556a99ad7131fa93dac167fc7869e4ec4e77fef11ff0cf958f577", } // resultHashes records the hash of the reach results of every case for @@ -48,6 +49,7 @@ var resultHashes = map[string]string{ "14/15/3": "77dfd0ea8ab71869ef612d41b8c2e46852d4fb67a49458cb645321692af77ea0", "14/16/3": "13b6eecb686ddec3c2ba470fc9752ea66cff445d5e18e0772a71b15ae7fe1c9c", "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", + "16/18/5": "d8c8cd0e84f656684b03781de7e98100a19649d38da4263570b9387cbac55df7", } // summaryHashes records the hash of the golden summaries of the cases for @@ -65,6 +67,7 @@ var summaryHashes = map[string]string{ "14/15/2": "5969f3cda15b666cbd902bbc2db953cbdef8c0ce48d5e77446cf243cdd0bb0e7", "14/16/2": "fb678202978212aae932e50a56a0d3dc7f0f794ce908988bf605ce9a6e04bc51", "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", + "16/18/3": "450c8ffebb71aba578e63e27a995927c57691a4c722f389ce752aee384bbdc20", } func TestGraphHashFollowsVersion(t *testing.T) { From 305936f31cafbf83599e8897dac5326dfb536910 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:37:29 +0000 Subject: [PATCH 18/58] fix(projectgraph): keep the record of a call on the result of a builtin (FOU-556) The record of a call on a call result skipped the builtins of the language. The skip assumed that runtime code calls back only through a value that the call passes. Some builtins give back a value that the call passes, or an element of it: new(thing) in Go, max(t, t) and next(iter(ts)) in Python, Object.assign(p, o) in JavaScript. The method then runs on a value of the project or of a dependency. With no record, a target of that method was no-such-target, and a target that the method reaches was not-found. The record now comes for a builtin too. In Go, the name edges of the site come back too, because linkFields makes them from the method-named record. So new(thing).vuln() finds what thing.vuln calls again. The FOU-556 item "Promise.resolve().then gives no no-dependency-source record" goes. The case references-argument has the record again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 9 +-- projectgraph/resolve.go | 12 ++-- .../javascript/builtin-chain/expect.yaml | 47 ++++++++++++++ .../javascript/builtin-chain/project/main.js | 8 +++ .../project/node_modules/plug/index.js | 12 ++++ .../project/node_modules/plug/package.json | 1 + .../builtin-chain/project/package.json | 1 + .../summaries/npm_plug_1.0.0.json | 62 +++++++++++++++++++ .../references-argument/expect.yaml | 8 ++- test/projectgraph/receivers_test.go | 24 +++++++ 10 files changed, 170 insertions(+), 14 deletions(-) create mode 100644 test/projectgraph/cases/javascript/builtin-chain/expect.yaml create mode 100644 test/projectgraph/cases/javascript/builtin-chain/project/main.js create mode 100644 test/projectgraph/cases/javascript/builtin-chain/project/node_modules/plug/index.js create mode 100644 test/projectgraph/cases/javascript/builtin-chain/project/node_modules/plug/package.json create mode 100644 test/projectgraph/cases/javascript/builtin-chain/project/package.json create mode 100644 test/projectgraph/cases/javascript/builtin-chain/summaries/npm_plug_1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 1de64c40..786da76f 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -207,10 +207,11 @@ 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 `uncertain` and not `no-such-target`. A call on the result of a builtin of -the language, as `Promise.resolve(x).then(f)`, makes no record. The code -of the runtime calls the code of the project or of a dependency only -through a value that the call passes, as `f`, and a `reference` edge holds -that value. +the language, as `Promise.resolve(x).then(f)`, is a record too. Some +builtins give back a value that the call passes, or an element of it, as +`new(thing)` in Go, `max(t, t)` in Python or `Object.assign(p, o)` in +JavaScript. The method then runs on a value of the project or of a +dependency. ### Result types diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index b68f1248..bbe3f274 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -160,11 +160,10 @@ func (b *builder) linkCalls(ix *fileindex.Index) { // whose classes start with an upper case letter. A Go name starts with an // upper case letter when the package exports it, so it tells nothing. // -// A builtin of the language, as Promise in Promise.resolve().then() or std -// in std::fs::read(p).map(f), is code of the runtime, which no unit gives. -// That code calls the code of the project or of a dependency only through -// a value that the call passes, and a reference edge holds each such -// value. So a call on its result makes no record. +// A builtin of the language keeps the record too. Some builtins give back +// a value that the call passes, or an element of it, as new(thing) in Go, +// max(t, t) in Python or Object.assign(p, o) in JavaScript. The method then +// runs on a value of the project or of a dependency. func (b *builder) onCallResult(p *profile, call fileindex.Call, r resolution) (fileindex.UnresolvedReason, bool) { called, ok := calledName(call.Receiver) if !ok || r.fail != nil || call.Method == "" || b.kindOf(r.key) != NodeExternal { @@ -177,9 +176,6 @@ func (b *builder) onCallResult(p *profile, call fileindex.Call, r resolution) (f return "", false } if _, provided := b.unitDefinitions(b.origins[r.key]); !provided { - if langcode.IsBuiltin(p.code, fileindex.Unit(r.key)) { - return "", false - } return fileindex.UnresolvedNoDependencySource, true } return fileindex.UnresolvedUnknownReceiver, true diff --git a/test/projectgraph/cases/javascript/builtin-chain/expect.yaml b/test/projectgraph/cases/javascript/builtin-chain/expect.yaml new file mode 100644 index 00000000..8d44b4b9 --- /dev/null +++ b/test/projectgraph/cases/javascript/builtin-chain/expect.yaml @@ -0,0 +1,47 @@ +source: safedep:hand +description: >- + go calls run on the result of Object.assign. Object is a builtin, and Object.assign gives back its + first argument, a Plug of the dependency plug. Plug.run calls fs.rmSync. The link step does not know + the type of the result, so the site is a record with the method name run. The record makes fs.rmSync + uncertain. No definition named run reaches fs.rmdirSync, so it stays not found. +units: + node_modules/plug: npm:plug@1.0.0 +roots: + - key: main.js + kind: manifest + rule: node.package-json +edges: + - from: main.js + to: main.js//go + kind: direct + confidence: high + site: main.js:8 + - from: npm:plug@1.0.0//index.js//Plug//run + to: fs//rmSync + kind: direct + confidence: high + site: npm:plug@1.0.0//index.js:5 +no_edges: + - from: main.js//go + to: npm:plug@1.0.0//index.js//Plug//run +unresolved: + - caller: main.js//go + site: main.js:5 + reason: no-dependency-source + method: run +results: + - target: fs//rmSync + status: uncertain + call_sites: + - caller: npm:plug@1.0.0//index.js//Plug//run + site: npm:plug@1.0.0//index.js:5 + reached: false + uncertainties: + - kind: unresolved-call + site: main.js:5 + - target: fs//rmdirSync + status: not-found + call_sites: + - caller: npm:plug@1.0.0//index.js//Plug//zap + site: npm:plug@1.0.0//index.js:8 + reached: false diff --git a/test/projectgraph/cases/javascript/builtin-chain/project/main.js b/test/projectgraph/cases/javascript/builtin-chain/project/main.js new file mode 100644 index 00000000..7a87d54a --- /dev/null +++ b/test/projectgraph/cases/javascript/builtin-chain/project/main.js @@ -0,0 +1,8 @@ +const { Plug } = require('plug'); + +function go() { + const p = new Plug(); + Object.assign(p, { a: 1 }).run(); +} + +go(); diff --git a/test/projectgraph/cases/javascript/builtin-chain/project/node_modules/plug/index.js b/test/projectgraph/cases/javascript/builtin-chain/project/node_modules/plug/index.js new file mode 100644 index 00000000..9b0dfa72 --- /dev/null +++ b/test/projectgraph/cases/javascript/builtin-chain/project/node_modules/plug/index.js @@ -0,0 +1,12 @@ +const fs = require('fs'); + +class Plug { + run() { + fs.rmSync('/tmp/x'); + } + zap() { + fs.rmdirSync('/tmp/z'); + } +} + +module.exports = { Plug }; diff --git a/test/projectgraph/cases/javascript/builtin-chain/project/node_modules/plug/package.json b/test/projectgraph/cases/javascript/builtin-chain/project/node_modules/plug/package.json new file mode 100644 index 00000000..89483584 --- /dev/null +++ b/test/projectgraph/cases/javascript/builtin-chain/project/node_modules/plug/package.json @@ -0,0 +1 @@ +{ "name": "plug", "version": "1.0.0", "main": "index.js" } diff --git a/test/projectgraph/cases/javascript/builtin-chain/project/package.json b/test/projectgraph/cases/javascript/builtin-chain/project/package.json new file mode 100644 index 00000000..e4de372b --- /dev/null +++ b/test/projectgraph/cases/javascript/builtin-chain/project/package.json @@ -0,0 +1 @@ +{ "name": "app", "main": "main.js", "dependencies": { "plug": "1.0.0" } } 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 new file mode 100644 index 00000000..7b95c886 --- /dev/null +++ b/test/projectgraph/cases/javascript/builtin-chain/summaries/npm_plug_1.0.0.json @@ -0,0 +1,62 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "npm:plug@1.0.0", + "symbols": [ + { + "key": "plug//Plug", + "definitions": [ + "npm:plug@1.0.0//index.js//Plug" + ], + "reaches": [] + }, + { + "key": "plug//Plug//run", + "definitions": [ + "npm:plug@1.0.0//index.js//Plug//run" + ], + "reaches": [ + "fs//rmSync" + ] + }, + { + "key": "plug//Plug//zap", + "definitions": [ + "npm:plug@1.0.0//index.js//Plug//zap" + ], + "reaches": [ + "fs//rmdirSync" + ] + }, + { + "key": "plug//index//Plug", + "definitions": [ + "npm:plug@1.0.0//index.js//Plug" + ], + "reaches": [] + }, + { + "key": "plug//index//Plug//run", + "definitions": [ + "npm:plug@1.0.0//index.js//Plug//run" + ], + "reaches": [ + "fs//rmSync" + ] + }, + { + "key": "plug//index//Plug//zap", + "definitions": [ + "npm:plug@1.0.0//index.js//Plug//zap" + ], + "reaches": [ + "fs//rmdirSync" + ] + } + ], + "targets": [ + "fs//rmSync", + "fs//rmdirSync" + ] +} diff --git a/test/projectgraph/cases/javascript/references-argument/expect.yaml b/test/projectgraph/cases/javascript/references-argument/expect.yaml index 438d688b..edc1f654 100644 --- a/test/projectgraph/cases/javascript/references-argument/expect.yaml +++ b/test/projectgraph/cases/javascript/references-argument/expect.yaml @@ -1,5 +1,5 @@ source: safedep:hand -description: src/main.js passes the imported load to Array.map and the first-party parse to Promise.then. Each reference makes its function possibly called. The call of then is a call on the result of Promise.resolve. Promise is a builtin, whose code calls parse only through the reference, so the site has no record. +description: src/main.js passes the imported load to Array.map and the first-party parse to Promise.then. Each reference makes its function possibly called. The call of then is a call on the result of Promise.resolve, whose type the link step does not know with no code of Promise, so the site keeps a record with the reason no-dependency-source. roots: - key: src/main.js kind: manifest @@ -15,7 +15,11 @@ edges: kind: reference confidence: low site: src/main.js:5 -unresolved: [] +unresolved: + - caller: src/main.js + site: src/main.js:5 + reason: no-dependency-source + method: then results: - target: js-yaml//load status: found diff --git a/test/projectgraph/receivers_test.go b/test/projectgraph/receivers_test.go index 344defd7..c74204d2 100644 --- a/test/projectgraph/receivers_test.go +++ b/test/projectgraph/receivers_test.go @@ -117,6 +117,12 @@ var goReceiverRows = []receiverRow{ want: "uncertain/no-dependency-source", noDeps: true, }, + { + name: "method of a first-party type on new", + source: "type thing struct{}\n\nfunc (t *thing) vuln() {\n\tdep.NewRunner()\n}\n\nfunc run() {\n\tnew(thing).vuln()\n}", + target: "example.com//dep//NewRunner", + want: "found/low", + }, } var rustReceiverRows = []receiverRow{ @@ -252,6 +258,12 @@ var rustReceiverRows = []receiverRow{ target: "dep//Job//start", want: "uncertain/trait-receiver", }, + { + name: "std function that returns its argument", + source: "fn run(t: Thing) {\n std::convert::identity(t).inspect();\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, } // pythonReceiverRows are the forms of a Python receiver. A type hint is @@ -366,6 +378,18 @@ var pythonReceiverRows = []receiverRow{ want: "uncertain/no-dependency-source", noDeps: true, }, + { + name: "builtin that returns an argument", + source: "def run(t):\n max(t, t).do()", + target: "dep//Thing//do", + want: "uncertain/no-dependency-source", + }, + { + name: "builtin that returns an element of an argument", + source: "def run(ts):\n next(iter(ts)).do()", + target: "dep//Thing//do", + want: "uncertain/no-dependency-source", + }, } func TestReceiverForms(t *testing.T) { From 7027f96fc5e07cfb3617411b2ca3ec199e7aaecb Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:49:19 +0000 Subject: [PATCH 19/58] refactor(parser): pick the source of a file once (FOU-576) The indexer picked the language of a component file from its bytes to make the store key. The parse then picked the language and the code again from the same bytes, so each component file got two passes. The indexer also set the language of each file twice. core.SourceSelector now returns a core.Source, which holds the language and the code. The indexer keeps the source in the pending file and gives it to the new parser.ParseSource, so the parse does not pick it again. ParseContent and Parse pick the source and call ParseSource. The indexer sets the language of a file once: from the extension when the read fails, and from the source after the read. A Go file that the indexer excludes after the read gets no language, as the doc of Result.Languages says. The scan of a component file returns its script element as a struct, and it uses strings.ToLower and isSpace where it repeated them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- core/language.go | 18 ++++++++---- lang/component.go | 52 ++++++++++++++++++++-------------- lang/javascript.go | 14 ++++----- parser/parser.go | 38 +++++++++++++------------ plugin/indexer/cache.go | 2 +- plugin/indexer/cache_test.go | 2 +- plugin/indexer/indexer.go | 25 ++++++++-------- plugin/indexer/indexer_pool.go | 8 ++++-- 8 files changed, 89 insertions(+), 70 deletions(-) diff --git a/core/language.go b/core/language.go index c448e457..06c1851a 100644 --- a/core/language.go +++ b/core/language.go @@ -98,11 +98,17 @@ type GrammarSelector interface { // SourceSelector is an optional interface of a Language whose files of some // extensions hold their code in blocks among markup, as the script blocks -// of a Vue component. Source returns the language of the code and the bytes -// that its grammar parses: data with each byte outside the code replaced by -// a space. The bytes keep the offsets and the line ends of data, so a node -// reports its line and column in the file, and its content in data. A file -// of another extension gives the Language and data. +// of a Vue component. Source returns the source of the data of a file of +// the extension. A file of another extension gives the Language and data. type SourceSelector interface { - Source(ext string, data []byte) (Language, []byte) + Source(ext string, data []byte) Source +} + +// Source is what the grammar of a file parses. +type Source struct { + Language Language + // Code is data with each byte outside the code replaced by a space. It + // keeps the offsets and the line ends of data, so a node reports its + // line and column in the file, and its content in data. + Code []byte } diff --git a/lang/component.go b/lang/component.go index db3706fc..b2ceea90 100644 --- a/lang/component.go +++ b/lang/component.go @@ -2,6 +2,7 @@ package lang import ( "bytes" + "strings" ) // componentExtensions are the extensions of the component files of Vue, @@ -47,21 +48,21 @@ func componentSource(ext string, data []byte) ([]byte, scriptDialect) { lower := asciiLower(data) for from < len(data) { - start, end, next, attrs, ok := scriptBlock(data, lower, from) + block, ok := scriptBlock(data, lower, from) if !ok { break } - if _, external := attrs["src"]; !external { - copy(source[start:end], data[start:end]) - dialect = max(dialect, dialectOf(attrs["lang"])) + if _, external := block.attrs["src"]; !external { + copy(source[block.start:block.end], data[block.start:block.end]) + dialect = max(dialect, dialectOf(block.attrs["lang"])) } - from = next + from = block.next } return source, dialect } func dialectOf(lang string) scriptDialect { - switch string(asciiLower([]byte(lang))) { + switch strings.ToLower(lang) { case "ts", "typescript": return dialectTypescript case "tsx": @@ -100,30 +101,39 @@ func lineAt(data []byte, i int) ([]byte, int) { return bytes.TrimRight(data[i:i+end], " \t\r"), i + end + 1 } -// scriptBlock finds the first script element at or after from. It returns -// the bounds of its code, the offset after its end tag, and the attributes -// of its start tag by lower case name. lower is data in ASCII lower case. -func scriptBlock(data, lower []byte, from int) (start, end, next int, attrs map[string]string, ok bool) { +// scriptElement is a script element of a file: the bounds of its code, the +// offset after its end tag, and the attributes of its start tag by lower +// case name. +type scriptElement struct { + start, end, next int + attrs map[string]string +} + +// scriptBlock finds the first script element at or after from. lower is +// data in ASCII lower case. +func scriptBlock(data, lower []byte, from int) (scriptElement, bool) { open := tagAt(lower, "') - if next < 0 { - return start, end, len(data), attrs, true + block.end = end + if next := bytes.IndexByte(data[end:], '>'); next >= 0 { + block.next = end + next + 1 } - return start, end, end + next + 1, attrs, true + return block, true } // tagAt returns the offset of the first tag name at or after from, or -1. @@ -136,7 +146,7 @@ func tagAt(lower []byte, name string, from int) int { } i += from after := i + len(name) - if after == len(lower) || bytes.IndexByte([]byte(" \t\r\n\f/>"), lower[after]) >= 0 { + if after == len(lower) || isSpace(lower[after]) || lower[after] == '/' || lower[after] == '>' { return i } from = after @@ -160,7 +170,7 @@ func startTag(data []byte, i int) (map[string]string, int, bool) { default: var name, value string name, value, i = attribute(data, i) - attrs[string(asciiLower([]byte(name)))] = value + attrs[strings.ToLower(name)] = value } } return attrs, -1, false diff --git a/lang/javascript.go b/lang/javascript.go index 73c67243..a6f732c5 100644 --- a/lang/javascript.go +++ b/lang/javascript.go @@ -57,17 +57,17 @@ func (l *javascriptLanguage) Resolvers() core.LanguageResolvers { // Source gives the script blocks of a component file to the language of // their dialect. It gives every other file to JavaScript. -func (l *javascriptLanguage) Source(ext string, data []byte) (core.Language, []byte) { +func (l *javascriptLanguage) Source(ext string, data []byte) core.Source { if !slices.Contains(componentExtensions, ext) { - return l, data + return core.Source{Language: l, Code: data} } - source, dialect := componentSource(ext, data) + code, dialect := componentSource(ext, data) + source := core.Source{Language: l, Code: code} switch dialect { case dialectTSX: - return l.tsx, source + source.Language = l.tsx case dialectTypescript: - return l.typescript, source - default: - return l, source + source.Language = l.typescript } + return source } diff --git a/parser/parser.go b/parser/parser.go index 18a4525e..c554f357 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -76,14 +76,14 @@ func (p *parserWrapper) Close() { } } -// Source returns the language that parses data as the content of the file -// of the name, and the bytes that its grammar parses. language is the -// language of the extension of the name. A core.SourceSelector picks both. -func Source(language core.Language, name string, data []byte) (core.Language, []byte) { +// Source returns the source of data as the content of the file of the +// name. language is the language of the extension of the name. A +// core.SourceSelector picks the source. +func Source(language core.Language, name string, data []byte) core.Source { if selector, ok := language.(core.SourceSelector); ok { return selector.Source(filepath.Ext(name), data) } - return language, data + return core.Source{Language: language, Code: data} } // Parse parses the file with the language of its extension. It returns an @@ -110,7 +110,7 @@ func (p *parserWrapper) parse(ctx context.Context, file core.File) (core.ParseTr return nil, core.StageRead, err } - tree, err := p.parseContent(ctx, language, file, data) + tree, err := p.ParseSource(ctx, file, data, Source(language, file.Name(), data)) return tree, core.StageParse, err } @@ -119,14 +119,11 @@ func (p *parserWrapper) parse(ctx context.Context, file core.File) (core.ParseTr // file is not read again. The tree keeps data, so the caller must not // change data after the call. func (p *parserWrapper) ParseContent(ctx context.Context, file core.File, data []byte) (core.ParseTree, error) { - if err := ctx.Err(); err != nil { - return nil, err - } language, err := p.languageOf(file) if err != nil { return nil, err } - return p.parseContent(ctx, language, file, data) + return p.ParseSource(ctx, file, data, Source(language, file.Name(), data)) } func (p *parserWrapper) languageOf(file core.File) (core.Language, error) { @@ -149,17 +146,22 @@ func (p *parserWrapper) sitterParser(language core.Language) *sitter.Parser { return sp } -// parseContent parses the source of data. The tree keeps data, not the -// source, so the tree data is the bytes of the file. The source keeps the -// offsets of data, so a node has the same content in both. -func (p *parserWrapper) parseContent(ctx context.Context, language core.Language, file core.File, data []byte) (core.ParseTree, error) { +// ParseSource parses the source of data that Source gave for file. A +// caller that already picked the source uses it, so the parse does not pick +// it again. The tree keeps data, not the code of the source, so the tree +// data is the bytes of the file. The code keeps the offsets of data, so a +// node has the same content in both. The caller must not change data after +// the call. +func (p *parserWrapper) ParseSource(ctx context.Context, file core.File, data []byte, source core.Source) (core.ParseTree, error) { + if err := ctx.Err(); err != nil { + return nil, err + } if p.maxBytes > 0 && len(data) > p.maxBytes { return nil, fmt.Errorf("%w: %s is over %d bytes", ErrInputTooLarge, file.Name(), p.maxBytes) } - language, source := Source(language, file.Name(), data) - sp := p.sitterParser(language) - tree, err := sp.ParseCtx(ctx, nil, source) + sp := p.sitterParser(source.Language) + tree, err := sp.ParseCtx(ctx, nil, source.Code) if err != nil { // A sitter parser resumes a cancelled parse on its next call. // The reset makes the next file parse from its start. @@ -171,7 +173,7 @@ func (p *parserWrapper) parseContent(ctx context.Context, language core.Language tree: tree, data: &data, file: file, - lang: language, + lang: source.Language, }, nil } diff --git a/plugin/indexer/cache.go b/plugin/indexer/cache.go index e9488656..a48786cc 100644 --- a/plugin/indexer/cache.go +++ b/plugin/indexer/cache.go @@ -62,7 +62,7 @@ func artifactKey(p pending, pl CachedPlugin) store.ArtifactKey { // supports reports whether a plugin runs on the language of a file. func supports(pl core.Plugin, p pending) bool { - return slices.Contains(pl.SupportedLanguages(), p.language.Meta().Code) + return slices.Contains(pl.SupportedLanguages(), p.source.Language.Meta().Code) } // needsParse reports whether a file with a stored index still needs its diff --git a/plugin/indexer/cache_test.go b/plugin/indexer/cache_test.go index 1598f915..444f767f 100644 --- a/plugin/indexer/cache_test.go +++ b/plugin/indexer/cache_test.go @@ -206,7 +206,7 @@ func TestAnalyzeKeepsTheCallGraphOnlyForAGraphPlugin(t *testing.T) { require.NoError(t, err) defer p.Close() - file := pending{file: files[0], language: byExtension[".js"], data: data} + file := pending{file: files[0], source: parser.Source(byExtension[".js"], files[0].Name(), data), data: data} for _, keep := range []bool{false, true} { o := analyze(ctx, p, file, keep) require.NoError(t, o.err) diff --git a/plugin/indexer/indexer.go b/plugin/indexer/indexer.go index 0fe90e04..e3317e24 100644 --- a/plugin/indexer/indexer.go +++ b/plugin/indexer/indexer.go @@ -161,10 +161,10 @@ func New(opts Options) (*Indexer, error) { // pending is a file that a run read and has not indexed yet. type pending struct { - file core.File - language core.Language - data []byte - key fileindex.Key + file core.File + source core.Source + data []byte + key fileindex.Key // restored names the cached plugins that took their stored artifact // before another plugin rejected its own. They skip the tree. @@ -287,9 +287,9 @@ func (r *run) index(ctx context.Context, files []core.File, byExtension map[stri r.exclude(file) continue } - r.result.Languages[file.Name()] = language.Meta().Code data, err := read(file, r.opts.MaxFileBytes) if err != nil { + r.result.Languages[file.Name()] = language.Meta().Code r.fail(ctx, file, core.StageRead, err) continue } @@ -298,16 +298,15 @@ func (r *run) index(ctx context.Context, files []core.File, byExtension map[stri continue } // The script blocks of a component file name their language, and - // the key and the index of the file hold it. The parse picks the - // same language again from the same bytes. - language, _ = parser.Source(language, file.Name(), data) - r.result.Languages[file.Name()] = language.Meta().Code + // the key and the index of the file hold it. + source := parser.Source(language, file.Name(), data) + r.result.Languages[file.Name()] = source.Language.Meta().Code batch = append(batch, pending{ - file: file, - language: language, - data: data, - key: fileindex.NewKey(file.Name(), fileindex.HashContent(data), language.Meta().Code), + file: file, + source: source, + data: data, + key: fileindex.NewKey(file.Name(), fileindex.HashContent(data), source.Language.Meta().Code), }) bytes += len(data) if len(batch) == batchSize || bytes >= maxBatchBytes { diff --git a/plugin/indexer/indexer_pool.go b/plugin/indexer/indexer_pool.go index 86756c50..dc252f3a 100644 --- a/plugin/indexer/indexer_pool.go +++ b/plugin/indexer/indexer_pool.go @@ -12,8 +12,10 @@ import ( "github.com/safedep/code/plugin/callgraph" ) -// contentParser parses the bytes of a file that the run already read. +// contentParser parses the bytes of a file that the run already read, from +// the source that the run picked or from the bytes alone. type contentParser interface { + ParseSource(ctx context.Context, file core.File, data []byte, source core.Source) (core.ParseTree, error) ParseContent(ctx context.Context, file core.File, data []byte) (core.ParseTree, error) Close() } @@ -114,7 +116,7 @@ func analyze(ctx context.Context, p contentParser, file pending, keepGraph bool) if ctx.Err() != nil { return outcome{} } - tree, err := p.ParseContent(ctx, file.file, file.data) + tree, err := p.ParseSource(ctx, file.file, file.data, file.source) if err != nil { return outcome{err: err} } @@ -124,7 +126,7 @@ func analyze(ctx context.Context, p contentParser, file pending, keepGraph bool) index = ix return nil }} - if !slices.Contains(indexPlugin.SupportedLanguages(), file.language.Meta().Code) { + if !slices.Contains(indexPlugin.SupportedLanguages(), file.source.Language.Meta().Code) { return outcome{tree: tree} } err = plugin.Analyze(ctx, indexPlugin, file.file, tree) From 9e47c25261cd6d48cefa9abc8a1a3a0511a9a773 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:49:40 +0000 Subject: [PATCH 20/58] fix(indexer): record the markup of a component file (FOU-576) The index read the script blocks of a .vue, .svelte or .astro file and nothing else. Before FOU-576 an import of such a file was a no-definition record, so each target was uncertain. After it, the import resolved, but the markup of the file stayed unread. A function that only the markup calls, as danger in @click="danger(item)", or as a handler that the markup names, had no caller. A method of a Vue options object, which Vue calls, had none too. So such a target read not-found or no-such-target. A \n", + markup: &core.Markup{Offset: 3}, + }, + { + name: "the tags of a script block are markup", + ext: ".vue", + data: "\n", + markup: &core.Markup{Offset: 0}, + }, + { + name: "a script with a src attribute loads its file", + ext: ".vue", + data: "\n\n + + diff --git a/test/projectgraph/cases/javascript/svelte-markup/project/src/main.js b/test/projectgraph/cases/javascript/svelte-markup/project/src/main.js new file mode 100644 index 00000000..a335534c --- /dev/null +++ b/test/projectgraph/cases/javascript/svelte-markup/project/src/main.js @@ -0,0 +1,4 @@ +import { mount } from 'svelte'; +import Other from './Other.svelte'; + +mount(Other, { target: document.body }); diff --git a/test/projectgraph/cases/javascript/vue-options-api/expect.yaml b/test/projectgraph/cases/javascript/vue-options-api/expect.yaml new file mode 100644 index 00000000..3ff5fd4a --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/expect.yaml @@ -0,0 +1,68 @@ +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/main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: src/main.js + to: src/Opt.vue + kind: import + confidence: high + site: src/main.js:2 + - from: src/main.js + to: src/Hooks.vue + 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 + kind: direct + confidence: high + site: src/main.js:6 + - from: src/Opt.vue//methods//go + to: plug//odanger + kind: direct + confidence: high + site: src/Opt.vue:11 + - from: src/Hooks.vue//mounted + to: plug//hdanger + kind: direct + confidence: high + site: src/Hooks.vue:6 +unresolved: + - caller: src/Hooks.vue + site: src/Hooks.vue:1 + reason: markup + - caller: src/Opt.vue + site: src/Opt.vue:1 + reason: markup +results: + - target: plug//odanger + status: uncertain + call_sites: + - caller: src/Opt.vue//methods//go + site: src/Opt.vue:11 + reached: false + uncertainties: + - kind: unresolved-call + site: src/Opt.vue:1 + - kind: unresolved-call + site: src/Hooks.vue:1 + - target: plug//hdanger + status: uncertain + call_sites: + - caller: src/Hooks.vue//mounted + site: src/Hooks.vue:6 + reached: false + uncertainties: + - kind: unresolved-call + site: src/Opt.vue:1 + - kind: unresolved-call + site: src/Hooks.vue:1 diff --git a/test/projectgraph/cases/javascript/vue-options-api/project/package.json b/test/projectgraph/cases/javascript/vue-options-api/project/package.json new file mode 100644 index 00000000..44fcd270 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "vue-options-api", + "bin": { "app": "src/main.js" } +} diff --git a/test/projectgraph/cases/javascript/vue-options-api/project/src/Hooks.vue b/test/projectgraph/cases/javascript/vue-options-api/project/src/Hooks.vue new file mode 100644 index 00000000..d9061384 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/project/src/Hooks.vue @@ -0,0 +1,9 @@ + diff --git a/test/projectgraph/cases/javascript/vue-options-api/project/src/Opt.vue b/test/projectgraph/cases/javascript/vue-options-api/project/src/Opt.vue new file mode 100644 index 00000000..adc22260 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/project/src/Opt.vue @@ -0,0 +1,15 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-options-api/project/src/main.js b/test/projectgraph/cases/javascript/vue-options-api/project/src/main.js new file mode 100644 index 00000000..509191f0 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-options-api/project/src/main.js @@ -0,0 +1,6 @@ +import { createApp } from 'vue'; +import Opt from './Opt.vue'; +import Hooks from './Hooks.vue'; + +createApp(Opt); +createApp(Hooks); diff --git a/test/projectgraph/cases/javascript/vue-script-src/expect.yaml b/test/projectgraph/cases/javascript/vue-script-src/expect.yaml new file mode 100644 index 00000000..29f39ff6 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/expect.yaml @@ -0,0 +1,53 @@ +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/main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: src/main.js + to: src/Ext.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/Ext.vue + to: src/logic.js + kind: import + confidence: high + site: src/Ext.vue:2 + - from: src/logic.js + to: plug//edanger + kind: direct + confidence: high + site: src/logic.js:3 +unresolved: + - caller: src/Ext.vue + site: src/Ext.vue:1 + reason: markup +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 + confidence: high + - key: plug//edanger + site: src/logic.js:3 + kind: direct + confidence: high + call_sites: + - caller: src/logic.js + site: src/logic.js:3 + reached: true diff --git a/test/projectgraph/cases/javascript/vue-script-src/project/package.json b/test/projectgraph/cases/javascript/vue-script-src/project/package.json new file mode 100644 index 00000000..444e462b --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "vue-script-src", + "bin": { "app": "src/main.js" } +} diff --git a/test/projectgraph/cases/javascript/vue-script-src/project/src/Ext.vue b/test/projectgraph/cases/javascript/vue-script-src/project/src/Ext.vue new file mode 100644 index 00000000..0ddee2d8 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/project/src/Ext.vue @@ -0,0 +1,2 @@ + + diff --git a/test/projectgraph/cases/javascript/vue-script-src/project/src/logic.js b/test/projectgraph/cases/javascript/vue-script-src/project/src/logic.js new file mode 100644 index 00000000..cd251f07 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/project/src/logic.js @@ -0,0 +1,4 @@ +import { edanger } from 'plug'; + +edanger(); +export default {}; diff --git a/test/projectgraph/cases/javascript/vue-script-src/project/src/main.js b/test/projectgraph/cases/javascript/vue-script-src/project/src/main.js new file mode 100644 index 00000000..cb71a93c --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-script-src/project/src/main.js @@ -0,0 +1,4 @@ +import { createApp } from 'vue'; +import Ext from './Ext.vue'; + +createApp(Ext); diff --git a/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml b/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml new file mode 100644 index 00000000..e0c011bc --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-setup-handler/expect.yaml @@ -0,0 +1,37 @@ +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/main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - 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/App.vue//onClick + to: plug//danger + kind: direct + confidence: high + site: src/App.vue:9 +unresolved: + - caller: src/App.vue + site: src/App.vue:1 + reason: markup +results: + - target: plug//danger + status: uncertain + call_sites: + - caller: src/App.vue//onClick + site: src/App.vue:9 + reached: false + uncertainties: + - kind: unresolved-call + site: src/App.vue:1 diff --git a/test/projectgraph/cases/javascript/vue-setup-handler/project/package.json b/test/projectgraph/cases/javascript/vue-setup-handler/project/package.json new file mode 100644 index 00000000..df4d053f --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-setup-handler/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "vue-setup-handler", + "bin": { "app": "src/main.js" } +} diff --git a/test/projectgraph/cases/javascript/vue-setup-handler/project/src/App.vue b/test/projectgraph/cases/javascript/vue-setup-handler/project/src/App.vue new file mode 100644 index 00000000..7e8e227e --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-setup-handler/project/src/App.vue @@ -0,0 +1,11 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-setup-handler/project/src/main.js b/test/projectgraph/cases/javascript/vue-setup-handler/project/src/main.js new file mode 100644 index 00000000..c4744d5b --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-setup-handler/project/src/main.js @@ -0,0 +1,4 @@ +import { createApp } from 'vue'; +import App from './App.vue'; + +createApp(App); diff --git a/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml b/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml new file mode 100644 index 00000000..41d80d3a --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-template-markup/expect.yaml @@ -0,0 +1,54 @@ +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/main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - 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/App.vue + to: plug//setup + kind: direct + confidence: high + site: src/App.vue:8 +unresolved: + - caller: src/App.vue + site: src/App.vue:1 + reason: markup +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 + confidence: high + call_sites: + - caller: src/App.vue + site: src/App.vue:8 + reached: true + - target: plug//danger + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/App.vue:1 + - target: plug//render + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/App.vue:1 diff --git a/test/projectgraph/cases/javascript/vue-template-markup/project/package.json b/test/projectgraph/cases/javascript/vue-template-markup/project/package.json new file mode 100644 index 00000000..feaf197e --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-template-markup/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "vue-template-markup", + "bin": { "app": "src/main.js" } +} diff --git a/test/projectgraph/cases/javascript/vue-template-markup/project/src/App.vue b/test/projectgraph/cases/javascript/vue-template-markup/project/src/App.vue new file mode 100644 index 00000000..03c71185 --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-template-markup/project/src/App.vue @@ -0,0 +1,9 @@ + + + diff --git a/test/projectgraph/cases/javascript/vue-template-markup/project/src/main.js b/test/projectgraph/cases/javascript/vue-template-markup/project/src/main.js new file mode 100644 index 00000000..c4744d5b --- /dev/null +++ b/test/projectgraph/cases/javascript/vue-template-markup/project/src/main.js @@ -0,0 +1,4 @@ +import { createApp } from 'vue'; +import App from './App.vue'; + +createApp(App); From b713d2236f715339bf70e77c5862a8a82ff28451 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:49:49 +0000 Subject: [PATCH 21/58] fix(indexer): give a JSX element name a reference (FOU-576) A JSX element renders the component that its name holds, as Child in . The index gave the name no reference, so a component that only an element renders had no caller. A target that only such a component calls read not-found. FOU-576 made the index read .jsx files, and .tsx files had the same gap before. The name of a JSX opening or self-closing element and the value of a JSX expression are now references, as an argument is. So , and onClick={handle} give a reference edge. A lower case name, as div, is a tag of the host, which JSX gives as a string. It gives no reference, also when an import binds the same name. The index also dropped each name in an export statement, to keep the exported name of export default parse from being a reference. That rule dropped each reference in the body of an exported function too, as export default function App() { return }. It now drops only a name at the top level of the file. The cases javascript/jsx-elements and typescript/tsx-elements guard each form and the lower case tag. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 8 ++ plugin/indexer/ecmascript.go | 20 +++ plugin/indexer/references.go | 10 +- .../cases/javascript/jsx-elements/expect.yaml | 119 ++++++++++++++++++ .../jsx-elements/project/package.json | 4 + .../jsx-elements/project/src/App.jsx | 25 ++++ .../jsx-elements/project/src/Other.jsx | 6 + .../jsx-elements/project/src/main.js | 4 + .../cases/typescript/tsx-elements/expect.yaml | 119 ++++++++++++++++++ .../tsx-elements/project/package.json | 4 + .../tsx-elements/project/src/App.tsx | 25 ++++ .../tsx-elements/project/src/Other.tsx | 6 + .../tsx-elements/project/src/main.ts | 4 + 13 files changed, 353 insertions(+), 1 deletion(-) create mode 100644 test/projectgraph/cases/javascript/jsx-elements/expect.yaml create mode 100644 test/projectgraph/cases/javascript/jsx-elements/project/package.json create mode 100644 test/projectgraph/cases/javascript/jsx-elements/project/src/App.jsx create mode 100644 test/projectgraph/cases/javascript/jsx-elements/project/src/Other.jsx create mode 100644 test/projectgraph/cases/javascript/jsx-elements/project/src/main.js create mode 100644 test/projectgraph/cases/typescript/tsx-elements/expect.yaml create mode 100644 test/projectgraph/cases/typescript/tsx-elements/project/package.json create mode 100644 test/projectgraph/cases/typescript/tsx-elements/project/src/App.tsx create mode 100644 test/projectgraph/cases/typescript/tsx-elements/project/src/Other.tsx create mode 100644 test/projectgraph/cases/typescript/tsx-elements/project/src/main.ts diff --git a/docs/projectgraph.md b/docs/projectgraph.md index a3be3c3d..6ef6a0a4 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -108,6 +108,14 @@ 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. +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 +to `Child`, and `` to `table.Row`. A lower case name, as +`div`, is a tag of the host, which JSX gives as a string, so it gives no +edge. A function in a JSX expression, as `handle` in +` + bold + + ); +} diff --git a/test/projectgraph/cases/javascript/jsx-elements/project/src/Other.jsx b/test/projectgraph/cases/javascript/jsx-elements/project/src/Other.jsx new file mode 100644 index 00000000..bd3485a4 --- /dev/null +++ b/test/projectgraph/cases/javascript/jsx-elements/project/src/Other.jsx @@ -0,0 +1,6 @@ +import { odanger } from 'plug'; + +export function Other() { + odanger(); + return null; +} diff --git a/test/projectgraph/cases/javascript/jsx-elements/project/src/main.js b/test/projectgraph/cases/javascript/jsx-elements/project/src/main.js new file mode 100644 index 00000000..d9478ff0 --- /dev/null +++ b/test/projectgraph/cases/javascript/jsx-elements/project/src/main.js @@ -0,0 +1,4 @@ +import { render } from 'react-dom'; +import App from './App'; + +render(App, document.body); diff --git a/test/projectgraph/cases/typescript/tsx-elements/expect.yaml b/test/projectgraph/cases/typescript/tsx-elements/expect.yaml new file mode 100644 index 00000000..19b63a6b --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/expect.yaml @@ -0,0 +1,119 @@ +source: safedep:hand +description: The same JSX forms as javascript/jsx-elements, in .tsx files. App renders Child, the imported Other and the member table.Row as JSX elements, and passes press to a button. Each element name and the prop give a reference, so the search reaches what each component calls. The lower case element b is a tag of the host, a string, so it gives no reference to the imported b. +roots: + - key: src/main.ts + kind: manifest + rule: node.package-json +edges: + - from: src/main.ts + to: src/App.tsx//App + kind: reference + confidence: low + site: src/main.ts:4 + - from: src/App.tsx//App + to: src/App.tsx//Child + kind: reference + confidence: low + site: src/App.tsx:18 + - from: src/App.tsx//App + to: src/Other.tsx//Other + kind: reference + confidence: low + site: src/App.tsx:19 + - from: src/App.tsx//App + to: src/App.tsx//table//Row + kind: reference + confidence: low + site: src/App.tsx:20 + - from: src/App.tsx//App + to: src/App.tsx//press + kind: reference + confidence: low + site: src/App.tsx:21 +no_edges: + - from: src/App.tsx//App + to: plug//b +unresolved: [] +results: + - target: plug//danger + status: found + path: + - key: src/main.ts + - key: src/App.tsx//App + site: src/main.ts:4 + kind: reference + confidence: low + - key: src/App.tsx//Child + site: src/App.tsx:18 + kind: reference + confidence: low + - key: plug//danger + site: src/App.tsx:5 + kind: direct + confidence: high + call_sites: + - caller: src/App.tsx//Child + site: src/App.tsx:5 + reached: true + - target: plug//odanger + status: found + path: + - key: src/main.ts + - key: src/App.tsx//App + site: src/main.ts:4 + kind: reference + confidence: low + - key: src/Other.tsx//Other + site: src/App.tsx:19 + kind: reference + confidence: low + - key: plug//odanger + site: src/Other.tsx:4 + kind: direct + confidence: high + call_sites: + - caller: src/Other.tsx//Other + site: src/Other.tsx:4 + reached: true + - target: plug//viaprop + status: found + path: + - key: src/main.ts + - key: src/App.tsx//App + site: src/main.ts:4 + kind: reference + confidence: low + - key: src/App.tsx//table//Row + site: src/App.tsx:20 + kind: reference + confidence: low + - key: plug//viaprop + site: src/App.tsx:9 + kind: direct + confidence: high + call_sites: + - caller: src/App.tsx//table//Row + site: src/App.tsx:9 + reached: true + - target: plug//onpress + status: found + path: + - key: src/main.ts + - key: src/App.tsx//App + site: src/main.ts:4 + kind: reference + confidence: low + - key: src/App.tsx//press + site: src/App.tsx:21 + kind: reference + confidence: low + - key: plug//onpress + site: src/App.tsx:12 + kind: direct + confidence: high + call_sites: + - caller: src/App.tsx//press + site: src/App.tsx:12 + reached: true + - target: plug//b + status: no-such-target diff --git a/test/projectgraph/cases/typescript/tsx-elements/project/package.json b/test/projectgraph/cases/typescript/tsx-elements/project/package.json new file mode 100644 index 00000000..e6af785c --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/project/package.json @@ -0,0 +1,4 @@ +{ + "name": "tsx-elements", + "bin": { "app": "src/main.ts" } +} diff --git a/test/projectgraph/cases/typescript/tsx-elements/project/src/App.tsx b/test/projectgraph/cases/typescript/tsx-elements/project/src/App.tsx new file mode 100644 index 00000000..2da84acc --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/project/src/App.tsx @@ -0,0 +1,25 @@ +import { danger, viaprop, onpress, b } from 'plug'; +import { Other } from './Other'; + +function Child(): JSX.Element { + danger(); + return

; +} + +const table = { Row: () => {viaprop()} }; + +function press() { + onpress(); +} + +export default function App() { + return ( +
+ + + + + bold +
+ ); +} diff --git a/test/projectgraph/cases/typescript/tsx-elements/project/src/Other.tsx b/test/projectgraph/cases/typescript/tsx-elements/project/src/Other.tsx new file mode 100644 index 00000000..bd3485a4 --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/project/src/Other.tsx @@ -0,0 +1,6 @@ +import { odanger } from 'plug'; + +export function Other() { + odanger(); + return null; +} diff --git a/test/projectgraph/cases/typescript/tsx-elements/project/src/main.ts b/test/projectgraph/cases/typescript/tsx-elements/project/src/main.ts new file mode 100644 index 00000000..d9478ff0 --- /dev/null +++ b/test/projectgraph/cases/typescript/tsx-elements/project/src/main.ts @@ -0,0 +1,4 @@ +import { render } from 'react-dom'; +import App from './App'; + +render(App, document.body); From 1f112f64ccb5c9b27065f145d42e15f10d5335cc Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:53:20 +0000 Subject: [PATCH 22/58] fix(projectgraph): resolve a Rust use path through a use of its module (FOU-562) A use path can start with a name that another use of the module binds, as helpers::run after use crate::deep::helpers. The walk read the binding of helpers, but the walk of a module binding with no names after it ended on a unit, and a walk that ends on a unit fails. So the path read as the crate helpers, the call got the edge helpers//run, and what run calls was not-found. unitMember now gives the unit of such a binding. A path through a use of a module of another crate, as sub::f after use dep::sub, also read as the crate sub. The crate route now reads the uses of the module of the file too. A use that binds its own path, as use rand or use smallvec::smallvec, routes nothing, because Rust never reads the binding of a use in its own path. A use that a route names can bind an item, as fl of use crate::ffi::flush as fl after pub use dep as ffi. A call of fl linked the routed key, which had no node, so the edge reached nothing and the target was not-found. The resolver now makes the node of a routed key, as it does for a key of a use with no route. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 5 + projectgraph/namespaces.go | 6 + projectgraph/rust.go | 27 +++- .../cases/rust/use-binding-path/expect.yaml | 132 ++++++++++++++++++ .../rust/use-binding-path/project/Cargo.toml | 7 + .../rust/use-binding-path/project/src/a.rs | 8 ++ .../rust/use-binding-path/project/src/b.rs | 6 + .../rust/use-binding-path/project/src/c.rs | 5 + .../project/src/deep/helpers.rs | 13 ++ .../use-binding-path/project/src/deep/mod.rs | 1 + .../rust/use-binding-path/project/src/main.rs | 12 ++ .../project/vendor/dep/Cargo.toml | 4 + .../project/vendor/dep/src/lib.rs | 9 ++ .../project/vendor/dep/src/sub.rs | 3 + .../summaries/cargo_dep_1.0.0.json | 44 ++++++ 15 files changed, 278 insertions(+), 4 deletions(-) create mode 100644 test/projectgraph/cases/rust/use-binding-path/expect.yaml create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/src/a.rs create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/src/b.rs create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/src/c.rs create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/src/deep/helpers.rs create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/src/deep/mod.rs create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/sub.rs create mode 100644 test/projectgraph/cases/rust/use-binding-path/summaries/cargo_dep_1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 786da76f..9c61d13a 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -288,6 +288,11 @@ Rust follows the same rules, with these additions. first name that names no module that the file sees, as a child module or a module of the root, names a crate, also when a file of the project has that name, as `extern crate schannel;` in `src/imp/schannel.rs`. +- A path can start with a name that another `use` of the module binds. So + `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`. - 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/projectgraph/namespaces.go b/projectgraph/namespaces.go index 66348e87..abf1d621 100644 --- a/projectgraph/namespaces.go +++ b/projectgraph/namespaces.go @@ -90,6 +90,12 @@ func (b *builder) unitMember(key, name string) (string, bool) { if imp, ok := sc.bindings[name]; ok { module, lead := importedModule(sc.ix.Language, imp) if mod, _, found := b.resolveModule(sc.ix, module); found { + // A use of a module binds the module, as helpers of use + // crate::deep::helpers. A walk that ends on a unit fails, + // so the unit itself is the member. + if len(lead) == 0 && b.units[mod] != nil { + return mod, true + } if r := b.walk(mod, lead); r.fail == nil { return r.key, true } diff --git a/projectgraph/rust.go b/projectgraph/rust.go index f8444c0c..a045c30d 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -1,6 +1,7 @@ package projectgraph import ( + "cmp" "slices" "strings" @@ -417,8 +418,12 @@ func (r rustResolver) Module(importer, spec string) (string, string, bool) { if key, ok := r.b.resolveRust(importer, spec); ok { return 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. Without it the edge reached no node, and + // the target read as not-found. if path, file, ok := r.b.rustCratePath(importer, spec); ok { - return r.b.rustCrateKey(file, path), "crate", true + return r.b.external(r.b.rustCrateKey(file, path)), "crate", true } return "", "", false } @@ -458,7 +463,9 @@ func rustTypePath(b *builder, sc *scope, written string) (string, bool) { // libsqlite3_sys as ffi binds ffi, so crate::ffi::ErrorCode is // libsqlite3_sys::ErrorCode. The deepest module of the path that the crate // has decides. A path through a use of another module of the crate, as -// self::rand after use crate::secure::rand, follows that use. +// self::rand after use crate::secure::rand, follows that use. So does a +// path through a use of the module of the file, as sub::f after use +// dep::sub. // // A walk asks for the route of each glob use of each module that it reads, // so the route of an outer call stays for the build. The route reads only @@ -485,14 +492,17 @@ func (b *builder) rustCratePath(importer, written string) (string, string, bool) func (b *builder) routeCrate(importer, written string) (string, string, bool) { parts := strings.Split(strings.TrimPrefix(written, "::"), "::") - for i := len(parts) - 1; i > 0; i-- { - u := b.rustModuleUnit(importer, strings.Join(parts[:i], "::")) + for i := len(parts) - 1; i >= 0; i-- { + u := b.rustModuleUnit(importer, cmp.Or(strings.Join(parts[:i], "::"), "self")) if u == nil { continue } for _, p := range u.files { for _, imp := range rustBindings(b.scopes[p], parts[i]) { module, _ := importedModule(langcode.Rust, imp) + if i == 0 && rustSelfBinding(module, parts) { + continue + } if crate, file, ok := b.rustCrate(p, module); ok { return joinPath(crate, parts[i+1:]), file, true } @@ -503,6 +513,15 @@ func (b *builder) routeCrate(importer, written string) (string, string, bool) { return "", "", false } +// rustSelfBinding reports whether a use of the module of a path binds the +// first name of the path to the path itself: use rand binds rand, and use +// smallvec::smallvec binds smallvec. Rust never reads the binding of a use +// in its own path, so such a binding routes nothing. +func rustSelfBinding(module string, parts []string) bool { + module = strings.TrimPrefix(module, "::") + return module == parts[0] || module == strings.Join(parts, "::") +} + // rustModuleUnit returns the unit of the module that a path of a file // names as written, with no walk through a use. A walk reads the uses and // the glob uses of each module on the way, and each of them can ask for a diff --git a/test/projectgraph/cases/rust/use-binding-path/expect.yaml b/test/projectgraph/cases/rust/use-binding-path/expect.yaml new file mode 100644 index 00000000..5a9276e0 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/expect.yaml @@ -0,0 +1,132 @@ +source: safedep:hand +description: >- + A use path starts with a name that another use of the module binds. In src/a.rs, use crate::deep::helpers binds + helpers to the module src/deep/helpers.rs, so helpers::run and helpers::Mode name its items. In src/b.rs, use + dep::sub binds sub to the module sub of the crate dep, the unit cargo:dep@1.0.0, so sub::f is dep::sub::f. A + crate named helpers or sub does not exist, and the code of each item calls a target. In src/c.rs, use + crate::ffi::flush as fl names dep::flush through pub use dep as ffi of src/main.rs, and a call of fl reaches the + external node dep//flush. +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/a.rs//go + to: src/deep/helpers.rs//run + kind: direct + confidence: high + site: src/a.rs:6 + - from: src/a.rs//go + to: src/deep/helpers.rs//Mode//start + kind: member + confidence: high + site: src/a.rs:7 + - from: src/b.rs//go + to: dep//sub//f + kind: direct + confidence: high + site: src/b.rs:5 + - from: dep//sub//f + to: cargo:dep@1.0.0//src/sub.rs//f + kind: package + confidence: high + site: cargo:dep@1.0.0//src/sub.rs:1 + - from: src/c.rs//go + to: dep//flush + kind: direct + confidence: high + site: src/c.rs:4 +no_edges: + - from: src/a.rs//go + to: helpers//run + - from: src/a.rs//go + to: helpers//Mode//start + - from: src/b.rs//go + to: sub//f +unresolved: [] +results: + - target: dep//new_thing + status: found + path: + - key: src/main.rs//main + - key: src/a.rs//go + site: src/main.rs:9 + kind: direct + confidence: high + - key: src/deep/helpers.rs//run + site: src/a.rs:6 + kind: direct + confidence: high + - key: dep//new_thing + site: src/deep/helpers.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/deep/helpers.rs//run + site: src/deep/helpers.rs:2 + reached: true + - target: dep//load_thing + status: found + path: + - key: src/main.rs//main + - key: src/a.rs//go + site: src/main.rs:9 + kind: direct + confidence: high + - key: src/deep/helpers.rs//Mode//start + site: src/a.rs:7 + kind: member + confidence: high + - key: dep//load_thing + site: src/deep/helpers.rs:11 + kind: direct + confidence: high + call_sites: + - caller: src/deep/helpers.rs//Mode//start + site: src/deep/helpers.rs:11 + reached: true + - target: dep//sink + status: found + path: + - key: src/main.rs//main + - key: src/b.rs//go + site: src/main.rs:10 + kind: direct + confidence: high + - key: dep//sub//f + site: src/b.rs:5 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/sub.rs//f + site: cargo:dep@1.0.0//src/sub.rs:1 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//sink + site: cargo:dep@1.0.0//src/sub.rs:2 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/sub.rs//f + site: cargo:dep@1.0.0//src/sub.rs:2 + reached: true + - target: dep//flush + status: found + path: + - key: src/main.rs//main + - key: src/c.rs//go + site: src/main.rs:11 + kind: direct + confidence: high + - key: dep//flush + site: src/c.rs:4 + kind: direct + confidence: high + call_sites: + - caller: src/c.rs//go + site: src/c.rs:4 + reached: true diff --git a/test/projectgraph/cases/rust/use-binding-path/project/Cargo.toml b/test/projectgraph/cases/rust/use-binding-path/project/Cargo.toml new file mode 100644 index 00000000..730014fa --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/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/use-binding-path/project/src/a.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/a.rs new file mode 100644 index 00000000..864d8c4d --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/a.rs @@ -0,0 +1,8 @@ +use crate::deep::helpers; +use helpers::run as go_run; +use helpers::Mode; + +pub fn go() { + go_run(); + Mode::start(); +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/b.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/b.rs new file mode 100644 index 00000000..517981dd --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/b.rs @@ -0,0 +1,6 @@ +use dep::sub; +use sub::f as g; + +pub fn go() { + g(); +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/c.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/c.rs new file mode 100644 index 00000000..f3406f34 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/c.rs @@ -0,0 +1,5 @@ +use crate::ffi::flush as fl; + +pub fn go() { + fl(); +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/deep/helpers.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/deep/helpers.rs new file mode 100644 index 00000000..45382687 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/deep/helpers.rs @@ -0,0 +1,13 @@ +pub fn run() { + dep::new_thing(); +} + +pub enum Mode { + Fast, +} + +impl Mode { + pub fn start() { + dep::load_thing(); + } +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/deep/mod.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/deep/mod.rs new file mode 100644 index 00000000..1630fabc --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/deep/mod.rs @@ -0,0 +1 @@ +pub mod helpers; diff --git a/test/projectgraph/cases/rust/use-binding-path/project/src/main.rs b/test/projectgraph/cases/rust/use-binding-path/project/src/main.rs new file mode 100644 index 00000000..ec7f9a04 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/src/main.rs @@ -0,0 +1,12 @@ +mod a; +mod b; +mod c; +mod deep; + +pub use dep as ffi; + +fn main() { + a::go(); + b::go(); + c::go(); +} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..537b4e47 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/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/use-binding-path/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..0ccac26c --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/lib.rs @@ -0,0 +1,9 @@ +pub mod sub; + +pub fn new_thing() {} + +pub fn load_thing() {} + +pub fn sink() {} + +pub fn flush() {} diff --git a/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/sub.rs b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/sub.rs new file mode 100644 index 00000000..7f6fcc5d --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/project/vendor/dep/src/sub.rs @@ -0,0 +1,3 @@ +pub fn f() { + crate::sink(); +} 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 new file mode 100644 index 00000000..beaba435 --- /dev/null +++ b/test/projectgraph/cases/rust/use-binding-path/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,44 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//flush", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//flush" + ], + "reaches": [] + }, + { + "key": "dep//load_thing", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//load_thing" + ], + "reaches": [] + }, + { + "key": "dep//new_thing", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//new_thing" + ], + "reaches": [] + }, + { + "key": "dep//sink", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//sink" + ], + "reaches": [] + }, + { + "key": "dep//sub//f", + "definitions": [ + "cargo:dep@1.0.0//src/sub.rs//f" + ], + "reaches": [] + } + ], + "targets": [] +} From 644b14c67d9aae0ecb203a57c8f12e3c11649a27 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:53:44 +0000 Subject: [PATCH 23/58] fix(indexer): record each form of a Go function value (FOU-560) A call of a Go function value concerns no target. The search must then reach the code that makes the value, through a reference. The index recorded a reference only for a name or a member chain that the file binds. So a method expression, a parenthesized function, a generic instantiation, a method value on a call, a composite literal, a type assertion, new or a dereference, the operand of a conversion or of a generic call with type arguments, and the operand of & gave no reference. A function of another file of the package gave none either. Each such call of a function value gave not-found. The Go index now reads each of these forms. A name that no local of the function declares keeps its text, so the link step resolves a function of another file. A local that shadows such a name keeps its scope. A method value whose receiver the index cannot type, as ts[0].M, gets a record with the method name, which concerns each method of the name. A package variable takes its value in the same forms. TestGoFunctionValues has one row per form, and each row failed before the change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 24 +- plugin/indexer/golang.go | 24 +- plugin/indexer/govalues.go | 281 ++++++++++++++++++ plugin/indexer/project.go | 3 + plugin/indexer/references.go | 48 ++- .../testdata/receivers/go/dep/values.go | 19 ++ test/projectgraph/values_test.go | 175 +++++++++++ 7 files changed, 547 insertions(+), 27 deletions(-) create mode 100644 plugin/indexer/govalues.go create mode 100644 test/projectgraph/testdata/receivers/go/dep/values.go create mode 100644 test/projectgraph/values_test.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 1de64c40..08dcb0e8 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -677,15 +677,21 @@ each function that such 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 - 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 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. -- A method value whose method the link step cannot name, as `h.Handle` on a - value of an interface. The link step records the reference with the - method name. That record concerns the targets that a method of the name - reaches, as a call `h.Handle()` does. + 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 + 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 + another file of the package, a member chain, a method expression, as + `(*T).M`, a method value on a call, a composite literal, a type assertion, + `new` or a dereference, as `f().M`, or a generic instantiation, as `F[int]`. + Each of them can be in parentheses or behind `&`. +- A method value whose method the index or the link step cannot name, as + `ts[0].Close` or `h.Handle` on a value of an interface. The index or the + link step records the reference with the method name. That record + concerns the targets that a method of the name reaches, as a call + `h.Handle()` does. - A function literal. The index gives the calls of the literal to the definition or the module top level that holds it. So a function that returns a literal reaches the calls of the literal. diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 1360e4fc..6e71fabf 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -33,12 +33,17 @@ var goRules = languageRules{ "return_statement": "", "literal_value": "", "send_statement": "value", + // 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. + "type_conversion_expression": "operand", }, member: "selector_expression", object: "operand", property: "field", identifiers: map[string]bool{"identifier": true}, lists: map[string]bool{"expression_list": true, "literal_element": true, "keyed_element": true}, + value: goReference, }, arguments: &argumentSyntax{ calls: map[string]string{"call_expression": "function"}, @@ -211,7 +216,7 @@ func (p *projection) goVariableValues(spec *sitter.Node) { func (p *projection) goDataType(t *sitter.Node) bool { switch t.Type() { case "type_identifier": - return langcode.IsBuiltin(langcode.Go, p.text(t)) && p.defs[p.local(p.text(t))] == nil + return p.goPredeclared(p.text(t)) case "parenthesized_type": return t.NamedChildCount() == 1 && p.goDataType(t.NamedChild(0)) case "pointer_type", "slice_type", "array_type", "map_type", "channel_type", "struct_type", "interface_type": @@ -247,7 +252,7 @@ func (p *projection) goDataValue(value *sitter.Node) bool { } switch function.Type() { case "identifier": - return langcode.IsBuiltin(langcode.Go, p.text(function)) && p.defs[p.local(p.text(function))] == nil + return p.goPredeclared(p.text(function)) case "selector_expression": operand, field := function.ChildByFieldName("operand"), function.ChildByFieldName("field") return operand != nil && field != nil && goErrorConstructors[[2]string{p.importPath(p.text(operand)), p.text(field)}] @@ -268,19 +273,10 @@ func (p *projection) importPath(alias string) string { } // goValueTarget returns the per-file target of a value that names a -// function or another value: a definition of the file, a name of the -// package that the link step resolves, or a member of an import. +// function or another value, in each form that a reference reads. func (p *projection) goValueTarget(value *sitter.Node) (string, bool) { - switch value.Type() { - case "identifier": - if key := p.local(p.text(value)); p.defs[key] != nil { - return key, true - } - return p.text(value), true - case "selector_expression": - return p.reference(*p.rules().references, p.cg.FileName, value) - } - return "", false + target, _ := goReference(p, p.cg.FileName, value) + return target, target != "" } // goBases returns the types that a Go type embeds, and the type that an diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go new file mode 100644 index 00000000..4571525a --- /dev/null +++ b/plugin/indexer/govalues.go @@ -0,0 +1,281 @@ +package indexer + +import ( + "github.com/safedep/code/fileindex" + "github.com/safedep/code/langcode" + "github.com/safedep/code/pkg/ts" + sitter "github.com/smacker/go-tree-sitter" +) + +// goReference returns the per-file target of a Go value that can give a +// function, or the member node of a method value whose receiver the index +// cannot name. A call of a Go function value concerns no target. That is +// sound only when the index records each function that a value can give, +// in each form that docs/projectgraph.md lists. +func goReference(p *projection, caller string, value *sitter.Node) (string, *sitter.Node) { + value = goValueName(value) + if value == nil { + return "", nil + } + switch value.Type() { + case "identifier": + // A Go parameter or local of a declared type holds a value of + // the type, and a value is no function. + if _, typed := p.typedScope(caller, p.text(value)); typed { + return "", nil + } + target, _ := p.goName(caller, value) + return target, nil + case "selector_expression": + if target, ok := p.reference(*p.rules().references, caller, value); ok { + return target, nil + } + if base, ok := p.goOperand(caller, value.ChildByFieldName("operand")); ok { + return fileindex.Join(base, p.text(value.ChildByFieldName("field"))), nil + } + return "", value + case "type_instantiation_expression": + // The grammar parses F[A, B] with more than one type argument + // as a type. + target, _ := p.goTypeTarget(caller, value.NamedChild(0)) + return target, nil + } + return "", nil +} + +// 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 +// index expression can be an element of a slice or a map, and then its +// operand names a variable of the container. +func goValueName(node *sitter.Node) *sitter.Node { + for depth := 0; node != nil && depth < maxReferenceSegments; depth++ { + switch node.Type() { + case "parenthesized_expression": + node = node.NamedChild(0) + case "index_expression": + node = node.ChildByFieldName("operand") + case "unary_expression": + if goOperator(node) != "&" { + return node + } + node = node.ChildByFieldName("operand") + default: + return node + } + } + return node +} + +// goOperand returns the per-file target of the receiver of a method value, +// as the key that a call on the receiver walks from: the type of a +// composite literal, a conversion, a type assertion or new, the function +// whose result a call gives, or a name or a member chain. A receiver that +// the index cannot type, as an element of a slice, gives none. +func (p *projection) goOperand(caller string, node *sitter.Node) (string, bool) { + if node == nil { + return "", false + } + switch node.Type() { + case "parenthesized_expression": + return p.goOperand(caller, node.NamedChild(0)) + case "unary_expression": + if op := goOperator(node); op != "*" && op != "&" { + return "", false + } + return p.goOperand(caller, node.ChildByFieldName("operand")) + case "identifier": + return p.goName(caller, node) + case "selector_expression": + base, ok := p.goOperand(caller, node.ChildByFieldName("operand")) + return fileindex.Join(base, p.text(node.ChildByFieldName("field"))), ok + case "composite_literal", "type_assertion_expression", "type_conversion_expression": + return p.goTypeTarget(caller, node.ChildByFieldName("type")) + case "call_expression": + return p.goResultOperand(caller, node) + } + return "", false +} + +// goResultOperand returns the per-file target of the result of a call: +// the type that new or make gives, or the function that the call names. A +// call of a local function value has a result of no known type. +func (p *projection) goResultOperand(caller string, call *sitter.Node) (string, bool) { + function := call.ChildByFieldName("function") + for function != nil && function.Type() == "parenthesized_expression" { + function = function.NamedChild(0) + } + if function == nil || function.Type() != "identifier" { + return p.goOperand(caller, function) + } + name := p.text(function) + if _, typed := p.typedScope(caller, name); typed { + return "", false + } + if p.goPredeclared(name) { + args := call.ChildByFieldName("arguments") + if (name == "new" || name == "make") && args != nil && args.NamedChildCount() > 0 { + return p.goTypeTarget(caller, args.NamedChild(0)) + } + return "", false + } + return p.goName(caller, function) +} + +// goTypeTarget returns the per-file target of the named type that a type +// node names, as dep//Thing for *dep.Thing or Box for Box[int]. +func (p *projection) goTypeTarget(caller string, node *sitter.Node) (string, bool) { + for depth := 0; node != nil && depth < maxReferenceSegments; depth++ { + switch node.Type() { + case "pointer_type", "parenthesized_type": + node = node.NamedChild(0) + case "generic_type": + node = node.ChildByFieldName("type") + case "type_identifier", "identifier": + return p.goName(caller, node) + case "qualified_type": + pkg, ok := p.goName(caller, node.ChildByFieldName("package")) + return fileindex.Join(pkg, p.text(node.ChildByFieldName("name"))), ok + default: + return "", false + } + } + return "", false +} + +// goName returns the per-file target of a name: the definition that the +// scope chain binds, a typed local, or an import. A name that no local of +// the caller declares keeps its text, as a function of another file of +// the package, and the link step resolves it or drops it. A local that the +// index does not type, or a predeclared name, gives none. +func (p *projection) goName(caller string, node *sitter.Node) (string, bool) { + if node == nil { + return "", false + } + name := p.text(node) + if target, ok := p.valueOf(*p.rules().references, caller, name); ok { + return target, true + } + if name == "_" || p.goPredeclared(name) || p.goLocal(caller, name, node.StartByte()) { + return "", false + } + return name, true +} + +// goPredeclared reports whether a name is a predeclared name of Go, as int +// or len, that no definition of the file shadows. +func (p *projection) goPredeclared(name string) bool { + return langcode.IsBuiltin(langcode.Go, name) && p.defs[p.local(name)] == nil +} + +// goScopedName is a name that a Go function declares, with the bytes of +// the source where the name is in scope. +type goScopedName struct { + name string + from, to uint32 +} + +// goBlocks are the nodes whose extent is the scope of a name that a +// declaration in them makes. +var goBlocks = map[string]bool{ + "block": true, "if_statement": true, "for_statement": true, "expression_switch_statement": true, + "type_switch_statement": true, "select_statement": true, "expression_case": true, "type_case": true, + "default_case": true, "communication_case": true, "func_literal": true, + "function_declaration": true, "method_declaration": true, +} + +// goTypeLiterals are the type literals whose names are no locals. +var goTypeLiterals = map[string]bool{"function_type": true, "interface_type": true, "struct_type": true} + +// goLocal reports whether a local name of the caller is in scope at the +// 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 { + if p.goScoped == nil { + p.goScoped = map[string][]goScopedName{} + } + names, ok := p.goScoped[caller] + if !ok { + names = p.goScopedNames(p.syntax[caller]) + p.goScoped[caller] = names + } + for _, n := range names { + if n.name == name && n.from <= at && at < n.to { + return true + } + } + return false +} + +// goScopedNames returns the names that a function declares, each with its +// scope. The scope of a variable starts after its declaration, so x in +// x := x names the outer x. +func (p *projection) goScopedNames(function *sitter.Node) []goScopedName { + if function == nil || function.Type() != "function_declaration" && function.Type() != "method_declaration" { + return nil + } + var names []goScopedName + add := func(nodes []*sitter.Node, from uint32, block *sitter.Node) { + for _, n := range nodes { + if n.Type() == "identifier" { + names = append(names, goScopedName{name: p.text(n), from: from, to: block.EndByte()}) + } + } + } + var walk func(node, block *sitter.Node, depth int) + walk = func(node, block *sitter.Node, depth int) { + if depth > maxLocalDepth { + return + } + switch { + case goTypeLiterals[node.Type()]: + // The parameters of a function type and the fields of a + // struct type declare no local. + return + case goBlocks[node.Type()]: + block = node + } + switch node.Type() { + case "parameter_declaration", "variadic_parameter_declaration": + add(childrenByField(node, "name"), block.StartByte(), block) + case "short_var_declaration": + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block) + case "var_spec", "const_spec": + add(childrenByField(node, "name"), node.EndByte(), block) + case "type_spec", "type_alias": + add(childrenByField(node, "name"), node.StartByte(), block) + case "range_clause", "receive_statement": + if goDeclares(node) { + add(ts.NamedChildren(node.ChildByFieldName("left")), node.EndByte(), block) + } + case "type_switch_statement": + if alias := node.ChildByFieldName("alias"); alias != nil { + add(ts.NamedChildren(alias), alias.EndByte(), block) + } + } + for _, child := range ts.NamedChildren(node) { + walk(child, block, depth+1) + } + } + walk(function, function, 0) + return names +} + +// goOperator returns the operator of a unary expression, as & or <-. +func goOperator(node *sitter.Node) string { + if op := node.ChildByFieldName("operator"); op != nil { + return op.Type() + } + return "" +} + +// goDeclares reports whether a range clause or a receive declares its left +// side with :=. +func goDeclares(node *sitter.Node) bool { + for i := 0; i < int(node.ChildCount()); i++ { + if node.Child(i).Type() == ":=" { + return true + } + } + return false +} diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index 57f89a03..a51738d7 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -51,6 +51,9 @@ type projection struct { // exportStatements are the export statements and the CommonJS export // assignments of the file. exportStatements []*sitter.Node + // goScoped holds the scoped local names of each Go function that a + // value names, by the key of the function. + goScoped map[string][]goScopedName } // project builds the call graph of tree and projects it into an index. It diff --git a/plugin/indexer/references.go b/plugin/indexer/references.go index a577c566..ef5cb0dc 100644 --- a/plugin/indexer/references.go +++ b/plugin/indexer/references.go @@ -36,6 +36,11 @@ 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 + // 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 + // visitor records the method name of that member. + value func(p *projection, caller string, value *sitter.Node) (target string, member *sitter.Node) } // referenceVisitor adds each function, method, class or import that the @@ -91,15 +96,20 @@ func referenceVisitor(p *projection) visitor { for _, value := range syntax.unlist(values) { // A read of a field, a constant or a variable of data runs // no code and gives the reader no function. - target, ok := p.reference(*syntax, caller, value) - if def := p.defs[target]; !ok || def != nil && def.HoldsData() { + target, member := p.valueTarget(*syntax, caller, value) + if def := p.defs[target]; target == "" && member == nil || def != nil && def.HoldsData() { continue } at := ast.GetNodePosition(value) - if k := (site{int(at.StartLine), int(at.StartColumn), caller, p.text(value)}); !seen[k] { - seen[k] = true + k := site{int(at.StartLine), int(at.StartColumn), caller, p.text(value)} + switch { + case seen[k]: + case target != "": p.addReference(value, caller, p.targets([]string{target})) + default: + p.addValueRecord(member, caller) } + seen[k] = true } } } @@ -120,6 +130,36 @@ func (p *projection) addReference(node *sitter.Node, caller string, targets []st }) } +// valueTarget returns the per-file target of a value, or the member node +// of a method value whose receiver the index cannot name. +func (p *projection) valueTarget(syntax referenceSyntax, caller string, value *sitter.Node) (string, *sitter.Node) { + if syntax.value != nil { + return syntax.value(p, caller, value) + } + target, _ := p.reference(syntax, caller, value) + return target, nil +} + +// addValueRecord records a method value whose receiver the index cannot +// name, as ts[0].Close. The code that gets the value can call any method of +// the name, so the record keeps the method name, as the record of a call +// on such a receiver does. Its site is the method name, so it never shares +// the site of a call that the receiver makes. +func (p *projection) addValueRecord(member *sitter.Node, caller string) { + syntax := p.rules().references + name := member.ChildByFieldName(syntax.property) + site := ast.GetNodePosition(name) + 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))), + Method: p.text(name), + Reason: fileindex.UnresolvedUnknownReceiver, + }) +} + // unlist replaces each list of the values with the value that it holds. func (syntax referenceSyntax) unlist(values []*sitter.Node) []*sitter.Node { var out []*sitter.Node diff --git a/test/projectgraph/testdata/receivers/go/dep/values.go b/test/projectgraph/testdata/receivers/go/dep/values.go new file mode 100644 index 00000000..1470897c --- /dev/null +++ b/test/projectgraph/testdata/receivers/go/dep/values.go @@ -0,0 +1,19 @@ +package dep + +// ValueFunc is the function that a function value of a value row holds. +func ValueFunc() {} + +// ValueGeneric is a generic function that a value row instantiates. +func ValueGeneric[T any]() {} + +// ValuePair is a generic function with two type parameters. +func ValuePair[A, B any]() {} + +// ValueHook holds a function. +var ValueHook = ValueHooked + +// ValueHooked is the function that ValueHook holds. +func ValueHooked() {} + +// ValueMethod is the method that a method value of a value row names. +func (t *Thing) ValueMethod() {} diff --git a/test/projectgraph/values_test.go b/test/projectgraph/values_test.go new file mode 100644 index 00000000..b4ec9f32 --- /dev/null +++ b/test/projectgraph/values_test.go @@ -0,0 +1,175 @@ +package projectgraph + +import ( + "testing" + + "github.com/safedep/code/core" + "github.com/stretchr/testify/assert" +) + +// goValueRows are the forms of a Go expression that gives a function +// value. fill stores the value in fs, and run calls fill and then fs[0]. A +// call of a function value concerns no target. So the search finds the +// function only through the reference that fill records, or through a +// record with the method name of a method value whose receiver the index +// cannot name. +var goValueRows = []receiverRow{ + { + name: "method expression", + source: goValueSource("func(*dep.Thing)", "fs[0] = (*dep.Thing).ValueMethod", "fs[0](nil)"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "parenthesized function", + source: goValueSource("func()", "fs[0] = (dep.ValueFunc)", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "generic instantiation", + source: goValueSource("func()", "fs[0] = dep.ValueGeneric[int]", "fs[0]()"), + target: "example.com//dep//ValueGeneric", + want: "found/low", + }, + { + name: "generic instantiation with two type arguments", + source: goValueSource("func()", "fs[0] = dep.ValuePair[int, string]", "fs[0]()"), + target: "example.com//dep//ValuePair", + want: "found/low", + }, + { + name: "method value on a call result", + source: goValueSource("func()", "fs[0] = dep.NewThing().ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on a type assertion", + source: goValueSource("func()", "var x any = dep.NewThing()\n\tfs[0] = x.(*dep.Thing).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on a composite literal", + source: goValueSource("func()", "fs[0] = (&dep.Thing{}).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on new", + source: goValueSource("func()", "fs[0] = new(dep.Thing).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on a dereference", + source: goValueSource("func()", "t := dep.NewThing()\n\tfs[0] = (*t).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "found/low", + }, + { + name: "method value on an element of a slice", + source: goValueSource("func()", "ts := []*dep.Thing{dep.NewThing()}\n\tfs[0] = ts[0].ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "method value on a local of no known type", + source: goValueSource("func()", "ts := []*dep.Thing{dep.NewThing()}\n\tt := ts[0]\n\tfs[0] = t.ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "method value on a range variable", + source: goValueSource("func()", "for _, t := range []*dep.Thing{dep.NewThing()} {\n\t\tfs[0] = t.ValueMethod\n\t}", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "method value on a receive", + source: goValueSource("func()", "ch := make(chan *dep.Thing, 1)\n\tch <- dep.NewThing()\n\tfs[0] = (<-ch).ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "method value on a local that shadows a function of another file", + source: goValueSource("func()", "shadowed := []*dep.Thing{dep.NewThing()}[0]\n\tfs[0] = shadowed.ValueMethod", "fs[0]()"), + target: "example.com//dep//Thing//ValueMethod", + want: "uncertain/unknown-receiver", + }, + { + name: "argument of a generic call with type arguments", + source: "func id[T any](x T) T { return x }\n\n" + goValueSource("func()", "fs[0] = id[func()](dep.ValueFunc)", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "conversion to a function type", + source: goValueSource("func()", "fs[0] = (func())(dep.ValueFunc)", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "dereference of the address of a variable", + source: goValueSource("func()", "p := &dep.ValueHook\n\tfs[0] = *p", "fs[0]()"), + target: "example.com//dep//ValueHooked", + want: "found/low", + }, + { + name: "function of another file", + source: goValueSource("func()", "fs[0] = helper", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "function of another file after a local of its name in a closed block", + source: goValueSource("func()", "{\n\t\thelper := 1\n\t\t_ = helper\n\t}\n\tfs[0] = helper", "fs[0]()"), + target: "example.com//dep//ValueFunc", + want: "found/low", + }, + { + name: "package variable that holds a function in parentheses", + source: "var h = (dep.ValueFunc)\n\nfunc run() {\n\th()\n}", + target: "example.com//dep//ValueFunc", + want: "found", + }, + { + name: "package variable that holds a generic instantiation", + source: "var h = dep.ValueGeneric[int]\n\nfunc run() {\n\th()\n}", + target: "example.com//dep//ValueGeneric", + want: "found", + }, + { + name: "package variable that holds a method value", + source: "var h = dep.NewThing().ValueMethod\n\nfunc run() {\n\th()\n}", + target: "example.com//dep//Thing//ValueMethod", + want: "found", + }, +} + +// goValueSource is a main.go whose fill stores a value in fs, of the +// element type, with the statements, and whose run calls fill and then the +// element of fs with the call. +func goValueSource(element, statements, call string) string { + return "var fs = make([]" + element + ", 1)\n\nfunc fill() {\n\t" + statements + "\n}\n\nfunc run() {\n\tfill()\n\t" + call + "\n}" +} + +// goValueProject is the receiver project of the source with other.go. Its +// helper calls dep.ValueFunc, and the result of its shadowed has a method of +// the name of a method of dep.Thing. +func goValueProject(source string) map[string]string { + files := goReceiverProject(source) + files["other.go"] = "package main\n\nimport \"example.com/dep\"\n\nfunc helper() {\n\tdep.ValueFunc()\n}\n\n" + + "type local struct{}\n\nfunc (local) ValueMethod() {}\n\nfunc shadowed() local { return local{} }\n" + return files +} + +func TestGoFunctionValues(t *testing.T) { + languages := caseLanguages(t) + for _, row := range goValueRows { + t.Run(row.name, func(t *testing.T) { + assert.Equal(t, row.want, receiverAnswer(t, languages, core.LanguageCodeGo, goValueProject(row.source), row)) + }) + } +} From 24f854b58d1cd44daa2749188aeae7f7a6b5524a Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:53:44 +0000 Subject: [PATCH 24/58] fix(indexer): keep a Go var value that names a function in a skeleton (FOU-560) The skeleton of a generated Go table file blanked each var value with no call and no function literal. A value as map[string]func(){"rm": remove} names functions with neither. A call of an element of the map is a call of a function value, and the search finds remove only through the reference of the value. So os.Remove gave not-found for a call through such a map. The skeleton now keeps a var value that names a value in a position that can give a function. The key of a keyed element and an operand of arithmetic do not count. The case go/dependency-generated-table failed before the change. 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/gounit.go | 26 +++++++- .../go/dependency-generated-table/expect.yaml | 66 +++++++++++++++++++ .../dependency-generated-table/project/go.mod | 5 ++ .../gomod/github.com/acme/kit@v1.0.0/gen.go | 11 ++++ .../gomod/github.com/acme/kit@v1.0.0/go.mod | 3 + .../gomod/github.com/acme/kit@v1.0.0/kit.go | 7 ++ .../project/main.go | 7 ++ .../go_github.com_acme_kit_v1.0.0.json | 35 ++++++++++ 9 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 test/projectgraph/cases/go/dependency-generated-table/expect.yaml create mode 100644 test/projectgraph/cases/go/dependency-generated-table/project/go.mod create mode 100644 test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/gen.go create mode 100644 test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/go.mod create mode 100644 test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/kit.go create mode 100644 test/projectgraph/cases/go/dependency-generated-table/project/main.go create mode 100644 test/projectgraph/cases/go/dependency-generated-table/summaries/go_github.com_acme_kit_v1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 08dcb0e8..f94cae45 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -68,7 +68,9 @@ 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. The index keeps the +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. diff --git a/plugin/indexer/gounit.go b/plugin/indexer/gounit.go index 28fc0dc7..554a1445 100644 --- a/plugin/indexer/gounit.go +++ b/plugin/indexer/gounit.go @@ -114,7 +114,10 @@ func isGeneratedTable(name string, data []byte) bool { // data values of a generated table file are what hit the limits of the // index, as a string of thousands of concatenated parts. Function bodies and // any value with a call or a function literal stay, because a generated -// file can hold code that a program runs, as a .pb.go file. +// file can hold code that a program runs, as a .pb.go file. A var value +// that names a function stays too, as a map of handlers. A call of an +// element of the map is a call of a function value, and the search finds +// the function only through the reference that the value records. func goSkeleton(tree *sitter.Tree, data []byte) []byte { out := bytes.Clone(data) clear := func(from, to uint32) { @@ -132,7 +135,7 @@ func goSkeleton(tree *sitter.Tree, data []byte) []byte { case "var_spec", "const_spec": if values := node.ChildByFieldName("value"); values != nil { for _, v := range ts.NamedChildren(values) { - if !holdsCode(v) { + if !holdsCode(v) && (node.Type() == "const_spec" || !namesValue(v)) { clear(v.StartByte(), v.EndByte()) out[v.StartByte()] = '0' } @@ -157,6 +160,25 @@ func holdsCode(node *sitter.Node) bool { return slices.ContainsFunc(ts.NamedChildren(node), holdsCode) } +// namesValue reports whether an expression holds a name that can give a +// function, as remove in map[string]func(){"rm": remove}. The key of a +// keyed element and an operand of arithmetic give no function. +func namesValue(node *sitter.Node) bool { + switch node.Type() { + case "identifier", "selector_expression": + return true + case "binary_expression": + return false + case "unary_expression": + if op := goOperator(node); op != "&" && op != "*" && op != "<-" { + return false + } + case "keyed_element": + return node.NamedChildCount() > 0 && namesValue(node.NamedChild(int(node.NamedChildCount())-1)) + } + return slices.ContainsFunc(ts.NamedChildren(node), namesValue) +} + // skeletonIndex indexes the skeleton of a generated table file of a // dependency unit whose index hit a limit. The index keeps the hash of the // bytes of the file, so the store serves it on the next run. diff --git a/test/projectgraph/cases/go/dependency-generated-table/expect.yaml b/test/projectgraph/cases/go/dependency-generated-table/expect.yaml new file mode 100644 index 00000000..e5138026 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-generated-table/expect.yaml @@ -0,0 +1,66 @@ +source: safedep:hand +description: >- + main calls Dispatch of the unit, and Dispatch calls an element of handlers, a map in gen.go, a generated + file. The map names remove of kit.go and gen of gen.go. The var data of gen.go holds a string of 60 parts, + past the depth limit of the index, so the index blanks the data values of the file. A call of an element of + handlers is a call of a function value, which concerns no target. So the skeleton keeps the value of + handlers, because it names functions. The import of the unit reaches the module top level of gen.go, and + its references reach remove and gen. A skeleton that blanked the map gave not-found for os.Remove and + os/exec.Command. +units: + gomod/github.com/acme/kit@v1.0.0: go:github.com/acme/kit@v1.0.0 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: os//exec//Command + reason: no-dependency-source + callback: true +results: + - target: os//Remove + status: found + path: + - key: main.go + - key: go:github.com/acme/kit@v1.0.0//gen.go + site: main.go:3 + kind: import + confidence: high + - key: go:github.com/acme/kit@v1.0.0//kit.go//remove + site: go:github.com/acme/kit@v1.0.0//gen.go:9 + kind: reference + confidence: low + - key: os//Remove + site: go:github.com/acme/kit@v1.0.0//kit.go:7 + kind: direct + confidence: high + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//kit.go//remove + site: go:github.com/acme/kit@v1.0.0//kit.go:7 + reached: true + - target: os//exec//Command + status: found + path: + - key: main.go + - key: go:github.com/acme/kit@v1.0.0//gen.go + site: main.go:3 + kind: import + confidence: high + - key: go:github.com/acme/kit@v1.0.0//gen.go//gen + site: go:github.com/acme/kit@v1.0.0//gen.go:9 + kind: reference + confidence: low + - key: os//exec//Command + site: go:github.com/acme/kit@v1.0.0//gen.go:7 + kind: direct + confidence: high + call_sites: + - caller: go:github.com/acme/kit@v1.0.0//gen.go//gen + site: go:github.com/acme/kit@v1.0.0//gen.go:7 + reached: true diff --git a/test/projectgraph/cases/go/dependency-generated-table/project/go.mod b/test/projectgraph/cases/go/dependency-generated-table/project/go.mod new file mode 100644 index 00000000..c7090c9c --- /dev/null +++ b/test/projectgraph/cases/go/dependency-generated-table/project/go.mod @@ -0,0 +1,5 @@ +module app + +go 1.22 + +require github.com/acme/kit v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/gen.go b/test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/gen.go new file mode 100644 index 00000000..6590619b --- /dev/null +++ b/test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/gen.go @@ -0,0 +1,11 @@ +// Code generated by synth. DO NOT EDIT. + +package kit + +import "os/exec" + +func gen() { _ = exec.Command("x") } + +var handlers = map[string]func(){"rm": remove, "gen": gen} + +var data string = "" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" + "x" diff --git a/test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/go.mod b/test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/go.mod new file mode 100644 index 00000000..529dd612 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/go.mod @@ -0,0 +1,3 @@ +module github.com/acme/kit + +go 1.22 diff --git a/test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/kit.go b/test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/kit.go new file mode 100644 index 00000000..d812198a --- /dev/null +++ b/test/projectgraph/cases/go/dependency-generated-table/project/gomod/github.com/acme/kit@v1.0.0/kit.go @@ -0,0 +1,7 @@ +package kit + +import "os" + +func Dispatch(name string) { handlers[name]() } + +func remove() { os.Remove("x") } diff --git a/test/projectgraph/cases/go/dependency-generated-table/project/main.go b/test/projectgraph/cases/go/dependency-generated-table/project/main.go new file mode 100644 index 00000000..b830203a --- /dev/null +++ b/test/projectgraph/cases/go/dependency-generated-table/project/main.go @@ -0,0 +1,7 @@ +package main + +import "github.com/acme/kit" + +func main() { + kit.Dispatch("rm") +} 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 new file mode 100644 index 00000000..085851ee --- /dev/null +++ b/test/projectgraph/cases/go/dependency-generated-table/summaries/go_github.com_acme_kit_v1.0.0.json @@ -0,0 +1,35 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "go:github.com/acme/kit@v1.0.0", + "symbols": [ + { + "key": "github.com//acme//kit//Dispatch", + "definitions": [ + "go:github.com/acme/kit@v1.0.0//kit.go//Dispatch" + ], + "reaches": [ + "os//Remove", + "os//exec//Command" + ], + "uncertainties": [ + 0 + ] + } + ], + "targets": [ + "os//Remove", + "os//exec//Command" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "site": { + "path": "" + }, + "text": "the Go standard library, which no unit gives (no-dependency-source)", + "callback": true + } + ] +} From 32eabd973980c68797ee30f576d7fa7f742d49dd Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:56:14 +0000 Subject: [PATCH 25/58] refactor(projectgraph): read the definitions of code through one accessor (FOU-558) A definition of data has no node, so each loop over the definitions of a file that adds nodes, roots or callers filtered HoldsData by hand. The two root loops also repeated one condition. Index.CodeDefinitions now gives the definitions that hold code. The node loops of addDefinitions, the library surface, the export keys, the root rules, scope.enclosing and goSurface use it. isSurfaceDefinition holds the condition of the two root loops. The walk and the resolve paths keep holdsData, because they read a definition of data by its key. The comment of Definition.Parent is wrapped. A field has its class as Parent, and TestFileIndexGoValues pins that. No result changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- fileindex/index.go | 19 +++++++++++++++++-- projectgraph/definitions.go | 19 +++++++------------ projectgraph/golang.go | 3 +-- projectgraph/rootmatch/match.go | 8 ++++---- projectgraph/roots.go | 15 +++++++++++---- projectgraph/scope.go | 4 ++-- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/fileindex/index.go b/fileindex/index.go index c0e97d0e..6e1dca76 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -9,6 +9,7 @@ import ( "cmp" "crypto/sha256" "encoding/hex" + "iter" "slices" "strings" @@ -226,8 +227,9 @@ type Export struct { // Definition is one module, function, method, class, constructor, module // variable, interface, field or constant. Key is its symbol key, as -// src/a.py//Parser//parse. Parent is the class key of a method or a field. A JavaScript function that an object -// property or a property assignment holds is a method with no parent. +// src/a.py//Parser//parse. Parent is the class key of a method or a +// field. A JavaScript function that an object property or a property +// assignment holds is a method with no parent. // Decorators are as written, in source order, and hold the annotations of // a Java definition. Values are the per-file targets that a variable // holds, as the targets of a Call. Params are the parameters that declare @@ -483,6 +485,19 @@ func (ix *Index) ExternalCalls() []Call { return calls } +// CodeDefinitions returns the definitions of the file that hold code, in +// the order of Definitions. A definition of data runs no code, so the graph +// has no node for it and no root or caller is one. +func (ix *Index) CodeDefinitions() iter.Seq[*Definition] { + return func(yield func(*Definition) bool) { + for i := range ix.Definitions { + if !ix.Definitions[i].HoldsData() && !yield(&ix.Definitions[i]) { + return + } + } + } +} + // DynamicSites returns the flags of the file and of its definitions, by line. func (ix *Index) DynamicSites() []Flag { flags := slices.Clone(ix.Flags) diff --git a/projectgraph/definitions.go b/projectgraph/definitions.go index 7818cf35..e4d86376 100644 --- a/projectgraph/definitions.go +++ b/projectgraph/definitions.go @@ -23,14 +23,12 @@ func (b *builder) addDefinitions(ix *fileindex.Index) { } b.addNode(module) + // No edge goes to a definition of data, so it has no node. A walk + // finds it by its definition. for i := range ix.Definitions { - def := &ix.Definitions[i] - b.defs[def.Key] = def - // No edge goes to a definition of data, so it has no node. A walk - // finds it by its definition. - if def.HoldsData() { - continue - } + b.defs[ix.Definitions[i].Key] = &ix.Definitions[i] + } + for def := range ix.CodeDefinitions() { if def.Key == ix.Path { module.Line = cmp.Or(def.StartLine, 1) module.Flags = append(module.Flags, def.Flags...) @@ -46,16 +44,13 @@ func (b *builder) addDefinitions(ix *fileindex.Index) { Flags: slices.Clone(def.Flags), }) } - for _, def := range ix.Definitions { + for def := range ix.CodeDefinitions() { if r := b.runner(def.Key); r != def.Key { n, runner := b.g.Nodes[def.Key], b.g.Nodes[r] runner.Flags, n.Flags = append(runner.Flags, n.Flags...), nil } } - for _, def := range ix.Definitions { - if def.HoldsData() { - continue - } + for def := range ix.CodeDefinitions() { n := b.g.Nodes[def.Key] slices.SortFunc(n.Flags, fileindex.CompareFlags) n.Flags = slices.Compact(n.Flags) diff --git a/projectgraph/golang.go b/projectgraph/golang.go index e44f5bb6..635c67e4 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -99,10 +99,9 @@ func goSurface(s *surfacer) { if strings.HasSuffix(p, "_test.go") || b.byPath[p] == nil { continue } - for _, def := range b.byPath[p].Definitions { + for def := range b.byPath[p].CodeDefinitions() { segs := fileindex.Segments(def.Key) switch { - case def.HoldsData(): case len(segs) == 1 && isGoExported(segs[0]): s.add(langcode.Go, importPath, modules.FormImport, segs[0]) case len(segs) == 2 && def.Kind == fileindex.DefMethod && isGoExported(segs[0]) && isGoExported(segs[1]): diff --git a/projectgraph/rootmatch/match.go b/projectgraph/rootmatch/match.go index 293bed12..71c628a2 100644 --- a/projectgraph/rootmatch/match.go +++ b/projectgraph/rootmatch/match.go @@ -202,9 +202,9 @@ func (f *finder) matchDefinition(r rules.Rule) { if m.InPackage != "" && ix.Package != m.InPackage { continue } - for _, def := range ix.Definitions { + for def := range ix.CodeDefinitions() { switch { - case def.Name != m.Name || def.HoldsData(): + case def.Name != m.Name: case m.Parent != "" && fileindex.Last(def.Parent) != m.Parent: case m.InPackage != "" && (def.Kind != fileindex.DefFunction || len(fileindex.Segments(def.Key)) != 1): case m.Static && !def.Static: @@ -233,8 +233,8 @@ func (f *finder) matchTestFiles(r rules.Rule) { continue } f.add(r, ix.Path) - for _, def := range ix.Definitions { - if def.Kind != fileindex.DefClass && !def.HoldsData() { + for def := range ix.CodeDefinitions() { + if def.Kind != fileindex.DefClass { f.add(r, def.Key) } } diff --git a/projectgraph/roots.go b/projectgraph/roots.go index d5c8c4fa..19f79a41 100644 --- a/projectgraph/roots.go +++ b/projectgraph/roots.go @@ -71,8 +71,8 @@ func (b *builder) markRoots(ctx context.Context) error { continue } b.addRoot(ix.Path, RootLibrarySurface, string(RootLibrarySurface)) - for _, def := range ix.Definitions { - if def.Exported && def.Key != ix.Path && def.Kind != fileindex.DefVariable && def.Kind != fileindex.DefInterface && !def.HoldsData() { + for def := range ix.CodeDefinitions() { + if isSurfaceDefinition(def, ix.Path) { b.addRoot(def.Key, RootLibrarySurface, string(RootLibrarySurface)) } } @@ -151,8 +151,8 @@ func (b *builder) exportKeys(p string, seen map[string]bool) []string { seen[p] = true var keys []string if sc.profile.definitionExports { - for _, def := range sc.ix.Definitions { - if def.Exported && def.Key != p && def.Kind != fileindex.DefVariable && def.Kind != fileindex.DefInterface && !def.HoldsData() { + for def := range sc.ix.CodeDefinitions() { + if isSurfaceDefinition(def, p) { keys = append(keys, def.Key) } } @@ -171,6 +171,13 @@ func (b *builder) exportKeys(p string, seen map[string]bool) []string { return slices.Compact(keys) } +// isSurfaceDefinition reports whether a definition of code of the file at +// the path is a part of the surface of its library: an exported definition +// that runs code. A variable and an interface run no code of their own. +func isSurfaceDefinition(def *fileindex.Definition, path string) bool { + return def.Exported && def.Key != path && def.Kind != fileindex.DefVariable && def.Kind != fileindex.DefInterface +} + // rootLanguages returns the languages of the files that hold a root other // than a test root, in the order of the codes. A test root runs only a // test, as the JavaScript test of a Python project, so it names no diff --git a/projectgraph/scope.go b/projectgraph/scope.go index b3bf1edc..31d53ecd 100644 --- a/projectgraph/scope.go +++ b/projectgraph/scope.go @@ -79,8 +79,8 @@ func newScope(ix *fileindex.Index) *scope { // or the module key. func (sc *scope) enclosing(line int) string { key, span := sc.ix.Path, -1 - for _, def := range sc.ix.Definitions { - if def.StartLine > line || def.EndLine < line || def.HoldsData() { + for def := range sc.ix.CodeDefinitions() { + if def.StartLine > line || def.EndLine < line { continue } if s := def.EndLine - def.StartLine; span < 0 || s <= span { From 289092c0c98976ba696ef50d68ffe9712de47c9f Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:56:30 +0000 Subject: [PATCH 26/58] refactor(projectgraph): move the Go rules of the link step behind the Go profile The shared link code and the reach query named Go in several places. Now the Go profile holds each Go rule: - outsideCode adds the records of code outside the graph. The Go hook recordGoOutsideCode replaces recordOutsideCode, which filtered Go itself. - callbackMethod says which definitions a callback record concerns. Graph.CallbackMethod reads it, so the reach query no longer filters langcode.Go. - structMember reads a field of a struct and a method of an embedded interface. walkClass called goField and declaringInterface for every language. - reflection names the package and the methods of reflection. The rule no longer hardcodes "reflect" in resolvesOutside. - loadsPackages replaces mainLoadsPackage and gates linkPackageLoad. functionValues gated that rule before. The one-call helper functionValue is gone. Also: b.hasUnits replaces four copies of len(b.spaces) < 2, provided and provides move to packages.go, unknownReceiver replaces eight failure literals in walk.go, lookupName joins the key once, goOutsideRecords builds each panic record, and the reach query keeps one callback pointer. No graph and no result changes. The hand-written cases, the receiver rows and the version hash tests pass unchanged. Findings H4, M1, M7, M11 and the low items of the review. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/golang.go | 102 ++++++++++++++++-------------- projectgraph/imports.go | 2 +- projectgraph/link.go | 30 ++++++++- projectgraph/names.go | 7 +- projectgraph/packages.go | 25 ++++++-- projectgraph/profile.go | 28 ++++++-- projectgraph/reach/reach.go | 20 ++---- projectgraph/reach/uncertainty.go | 19 ++++-- projectgraph/roots.go | 2 +- projectgraph/rust.go | 4 +- projectgraph/values.go | 53 ++++++++-------- projectgraph/walk.go | 37 +++++------ 12 files changed, 196 insertions(+), 133 deletions(-) diff --git a/projectgraph/golang.go b/projectgraph/golang.go index e44f5bb6..ef637256 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -19,6 +19,9 @@ var goProfile = profile{ units: (*builder).addGoUnits, dependencyCode: true, link: (*builder).linkInits, + outsideCode: (*builder).recordGoOutsideCode, + callbackMethod: func(n *Node) bool { return n.Kind == NodeMethod && n.Exported }, + structMember: goStructMember, provide: goProvide, narrowProviders: goNarrowProviders, surface: goSurface, @@ -28,23 +31,12 @@ var goProfile = profile{ implicitInterfaces: true, typeConversions: true, scopeWildcards: true, - mainLoadsPackage: true, + loadsPackages: true, importsBindAlias: true, functionValues: true, - reflective: goReflective, -} - -// goReflectMethods are the methods of package reflect that call a function -// value or give a method by its name or its index. Each one can run any -// function of the program. -var goReflectMethods = map[string]bool{"Call": true, "CallSlice": true, "Method": true, "MethodByName": true} - -// goReflective reports whether a call or a reference of the method in a Go -// file that imports reflect can run a function that reflect gives. The -// link step does not always know that the receiver is a reflect.Value, as -// when no unit gives the standard library. So any receiver counts. -func goReflective(ix *fileindex.Index, method string) bool { - return goReflectMethods[method] && slices.ContainsFunc(ix.Imports, func(imp fileindex.Import) bool { return imp.Module == "reflect" }) + // Call and CallSlice call a function value, and Method and MethodByName + // give a method by its index or its name. + reflection: &reflection{module: "reflect", methods: map[string]bool{"Call": true, "CallSlice": true, "Method": true, "MethodByName": true}}, } // goProvide returns the package unit of a Go module that an import path @@ -354,6 +346,27 @@ func (b *builder) abstract(iface string, depth int) []fileindex.Signature { return sigs } +// goStructMember reads a name of a Go struct that is no method of it. A +// read of a field runs no code, and a name on a field whose type the walk +// does not know is unknown. A method that an embedded interface declares +// runs on the value that the field holds, as a call on a value of the +// interface does. +func goStructMember(b *builder, r *resolution, name string, last bool) (consumed, done, ok bool) { + if field, ok := b.goField(r.key, name); ok { + if !last { + r.fail = unknownReceiver(name) + return false, true, true + } + r.key = field + return true, false, true + } + if iface, ok := b.declaringInterface(r.key, name); ok { + r.class, r.virtual, r.fail = iface, true, noDefinition(name) + return false, true, true + } + return false, false, false +} + // goField returns the field of the name that a struct of the MRO of the // class declares: the struct, or a struct that it embeds. func (b *builder) goField(class, name string) (string, bool) { @@ -382,54 +395,49 @@ func (b *builder) declaringInterface(class, name string) (string, bool) { // panic that no recover stops, to print it. var goPanicMethods = []string{"Error", "String"} -// recordOutsideCode adds a record on each external key that Go code calls -// or uses as a value and whose package no unit gives. The graph holds no -// code of such a key, and that code can call back into code that the -// graph holds. docs/projectgraph.md gives the argument for the scope of -// each record: -// -// - The standard library can call back only a method of an exported name -// of a value that it holds, so its record is a Callback. -// - C code that cgo calls can call any function that an export comment -// names, and another module can call any function of a package that it -// imports. Their records concern every target. A build with no project -// file, as a summary, gives no record for another module, because the -// reader of a summary joins the summary of each module that it calls. -// - A builtin runs no code of the program, except panic. -func (b *builder) recordOutsideCode() { +// goStdText is the text of a record of the Go standard library with no +// unit. +const goStdText = "the Go standard library, which no unit gives" + +// recordGoOutsideCode adds a record for Go code that the graph does not +// hold. docs/projectgraph.md, "Code outside the graph", gives the argument +// for the scope of each record. +func (b *builder) recordGoOutsideCode() { for from, to := range b.edges.pairs { if b.kindOf(to) == NodeExternal && b.languageOf(from) == langcode.Go && !b.provided(to) { - for _, r := range b.outsideRecords(to) { + for _, r := range b.goOutsideRecords(to) { b.addUnresolved(r) } } } } -func (b *builder) outsideRecords(key string) []UnresolvedSite { - r := UnresolvedSite{Caller: key, Text: key, Reason: fileindex.UnresolvedNoDependencySource} - first := fileindex.Unit(key) +// goOutsideRecords returns the records of an external key whose package no +// unit gives. +// +// - The standard library calls back only a method of an exported name, so +// its record is a Callback. +// - C code and the code of another module can call any function, so their +// records concern every target. A summary gets no record for another +// module, because its reader joins the summary of that module. +// - A builtin runs no code of the program, except panic, which calls Error +// or String. +func (b *builder) goOutsideRecords(key string) []UnresolvedSite { + pkg := fileindex.Unit(key) switch { case key == "panic": - var out []UnresolvedSite + out := make([]UnresolvedSite, 0, len(goPanicMethods)) for _, m := range goPanicMethods { - r.Method = m - out = append(out, r) + out = append(out, UnresolvedSite{Caller: key, Text: key, Method: m, Reason: fileindex.UnresolvedNoDependencySource}) } return out case langcode.IsBuiltin(langcode.Go, key): return nil - case first == "C": - case modules.IsStdPath(first): - r.Text, r.Callback = "the Go standard library, which no unit gives", true + case pkg == "C": + case modules.IsStdPath(pkg): + return []UnresolvedSite{{Caller: key, Text: goStdText, Reason: fileindex.UnresolvedNoDependencySource, Callback: true}} case !b.hasProject: return nil } - return []UnresolvedSite{r} -} - -// provided reports whether a unit of dependency code gives the package of -// an external key. -func (b *builder) provided(key string) bool { - return slices.ContainsFunc(b.origins[key], func(o origin) bool { return len(b.providers(o)) > 0 }) + return []UnresolvedSite{{Caller: key, Text: key, Reason: fileindex.UnresolvedNoDependencySource}} } diff --git a/projectgraph/imports.go b/projectgraph/imports.go index 2d386ed2..3dac1de7 100644 --- a/projectgraph/imports.go +++ b/projectgraph/imports.go @@ -101,7 +101,7 @@ func (b *builder) importedModules(ix *fileindex.Index, spec string, form modules if mods := b.resolveModules(ix, spec, form); len(mods) > 0 { return mods, nil } - if len(b.spaces) < 2 { + if !b.hasUnits() { return nil, nil } var mods []moduleRef diff --git a/projectgraph/link.go b/projectgraph/link.go index e513acca..4232e21c 100644 --- a/projectgraph/link.go +++ b/projectgraph/link.go @@ -378,12 +378,32 @@ func (b *builder) linkAll(ctx context.Context) error { if err := b.linkPackages(ctx); err != nil { return err } - b.recordOutsideCode() + for _, p := range profiles { + if p.outsideCode != nil { + p.outsideCode(b) + } + } b.checkUnits() b.linkFields() return nil } +// 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. +func (g *Graph) CallbackMethod(key string) bool { + n := g.Nodes[key] + if n == nil { + return false + } + file := g.Nodes[n.Path] + if file == nil { + return false + } + callback := profileOf(file.Language).callbackMethod + return callback != nil && callback(n) +} + func (b *builder) status(ix *fileindex.Index) fileindex.FileStatus { if len(ix.Truncated) > 0 || slices.ContainsFunc(ix.DynamicSites(), func(f fileindex.Flag) bool { return f.Kind == fileindex.FlagParseError @@ -446,7 +466,13 @@ func (b *builder) copyUnresolved(ix *fileindex.Index) { Text: u.Text, Receiver: u.Receiver, Method: u.Method, - Reason: functionValue(profileOf(ix.Language), u.Reason), + Reason: u.Reason, + } + // The search reaches each function that a call of a function + // value can run, so the record names no target. See "A call of a + // function value" in docs/projectgraph.md. + if u.Reason == fileindex.UnresolvedComputedName && profileOf(ix.Language).functionValues { + site.Reason = fileindex.UnresolvedFunctionValue } if u.Reason == fileindex.UnresolvedDynamicImport { site.ImportPrefix = b.importPrefix(ix, u.Line) diff --git a/projectgraph/names.go b/projectgraph/names.go index d5cf8814..f57543e1 100644 --- a/projectgraph/names.go +++ b/projectgraph/names.go @@ -44,10 +44,11 @@ func (b *builder) lookupName(module, name string, hops int) (string, *failure) { if sc == nil { return module, nil } - if n := b.g.Nodes[fileindex.Join(module, name)]; n != nil && n.Kind != NodeExport { - return n.Key, nil + key := fileindex.Join(module, name) + if n := b.g.Nodes[key]; n != nil && n.Kind != NodeExport { + return key, nil } - if key := fileindex.Join(module, name); b.holdsData(key) { + if b.holdsData(key) { return key, nil } diff --git a/projectgraph/packages.go b/projectgraph/packages.go index e09f9e3c..801400bd 100644 --- a/projectgraph/packages.go +++ b/projectgraph/packages.go @@ -219,6 +219,24 @@ func (b *builder) addProblems(sp *space, problems []modules.Problem) { } } +// hasUnits reports whether the build links a unit of dependency code. A +// build with no unit links no external key. +func (b *builder) hasUnits() bool { + return len(b.spaces) > 1 +} + +// provided reports whether a unit of dependency code gives the package of +// an external key. +func (b *builder) provided(key string) bool { + return slices.ContainsFunc(b.origins[key], b.provides) +} + +// provides reports whether a unit of dependency code gives the package +// that an import names. +func (b *builder) provides(o origin) bool { + return len(b.providers(o)) > 0 +} + // spaceOf returns the space of a key: the space of its unit, or the space // of the project. func (b *builder) spaceOf(key string) *space { @@ -277,7 +295,7 @@ func (b *builder) importedExternal(ix *fileindex.Index, key, module string, form // recordOrigin records an import as an origin of the key. A graph with no // unit of dependency code links no external key, so it records nothing. func (b *builder) recordOrigin(ix *fileindex.Index, key, module string, form modules.Form, names []string) { - if len(b.spaces) < 2 { + if !b.hasUnits() { return } o := origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(ix, module), form: form, names: slices.Clone(names)} @@ -378,7 +396,7 @@ func (b *builder) provide(sp *space, family langcode.Code, module string, form m // package makes at run time, is a record of the unit on the external node. // Any code of the unit can run there. func (b *builder) linkPackages(ctx context.Context) error { - if len(b.spaces) < 2 { + if !b.hasUnits() { return nil } for { @@ -446,13 +464,12 @@ func (b *builder) linkProvision(key string, o origin, pv provision) { // 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 -// function values the link step joins to the code that holds them. The // program loads a package before a symbol of it runs. A search that enters // the package by the key, as the search of a summary does, then reaches the // init functions and the package-level values. A call of a function value // that a package-level value holds finds its function only through them. func (b *builder) linkPackageLoad(lang langcode.Code, key, module string) { - if !profileOf(lang).functionValues { + if !profileOf(lang).loadsPackages { return } for _, p := range b.moduleFiles(module) { diff --git a/projectgraph/profile.go b/projectgraph/profile.go index e13d8015..15d7f696 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -28,6 +28,19 @@ type profile struct { // link adds the edges that the loader of the language runs, as the // init functions of Go. link func(b *builder, ix *fileindex.Index) + // outsideCode adds the records of code outside the graph that code of + // the language can run, as the Go standard library with no unit. It + // runs once, when every other record of the build is known. + outsideCode func(b *builder) + // callbackMethod reports whether code outside the graph can call a + // definition of the language back on a value that it holds, as a Go + // method of an exported name. A record that Callback marks concerns + // what such a definition reaches. + callbackMethod func(n *Node) bool + // structMember reads a name of a class that is no method of it, as a + // field of a Go struct or a method that an embedded interface + // declares. ok reports whether the class has such a member. + structMember func(b *builder, r *resolution, name string, last bool) (consumed, done, ok bool) // qualified resolves a qualified name, as com.x.Service in Java. qualified func(b *builder, name string) (string, bool) // typePath resolves a type path that a file writes and that its scope @@ -201,19 +214,20 @@ type profile struct { // exactWildcards marks a language whose wildcard import of a package of // the project names a class exactly, so its edge is direct. exactWildcards bool - // mainLoadsPackage marks a language whose program initializes the - // package of main before main runs, as Go. - mainLoadsPackage bool + // loadsPackages marks a language whose program initializes a package + // before any code of the package runs, as Go: the package of main + // before main, and the package of an external key before the symbol. + loadsPackages bool // functionValues marks a language whose function values the link step // joins to the code that holds them, as Go. A call of a function value // gets the reason function-value in place of computed-name, and it // concerns no target. A reference to a method that the link step cannot // name, as h.Handle on an interface, gets a record with the method name. functionValues bool - // reflective reports whether a call or a reference of the file, by its - // method name, can run a function that reflection gives, as - // reflect.Value.Call. Such a site gets a computed-name record. - reflective func(ix *fileindex.Index, method string) bool + // reflection names the package of the language that can run any + // function by a value or by a name, and its methods that do it, as + // Call of reflect.Value in Go. + reflection *reflection // helperProgram reports whether the main of a file is a helper // program of the project and not the program that its users run, as a // build script or an example of a Rust crate. Its root stays, and it diff --git a/projectgraph/reach/reach.go b/projectgraph/reach/reach.go index e0386b67..00e5c384 100644 --- a/projectgraph/reach/reach.go +++ b/projectgraph/reach/reach.go @@ -14,7 +14,6 @@ import ( "strings" "github.com/safedep/code/fileindex" - "github.com/safedep/code/langcode" "github.com/safedep/code/projectgraph" ) @@ -527,20 +526,15 @@ func (a *adjacency) methodReaches(name string, target Target) bool { return slices.ContainsFunc(a.match(target), func(id int) bool { return c[id] }) } -// exportedClosure returns the nodes that the Go methods of an exported -// name reach, in the project and in each unit of dependency code. Code -// outside the graph can call such a method on a value that it holds. A -// method of a test file is a source only when the query includes the -// tests. +// exportedClosure returns the nodes that the methods that code outside the +// graph can call back reach, as the graph names them, in the project and in +// each unit of dependency code. A method of a test file is a source only +// when the query includes the tests. func (a *adjacency) exportedClosure() []bool { if a.exported == nil { var methods []int for id, key := range a.keys { - n := a.g.Nodes[key] - if n == nil || n.Kind != projectgraph.NodeMethod || !n.Exported || n.Test && !a.includeTests { - continue - } - if file := a.g.Nodes[n.Path]; file != nil && file.Language == langcode.Go { + if n := a.g.Nodes[key]; n != nil && (a.includeTests || !n.Test) && a.g.CallbackMethod(key) { methods = append(methods, id) } } @@ -549,8 +543,8 @@ func (a *adjacency) exportedClosure() []bool { return a.exported } -// exportedReaches reports whether a Go method of an exported name reaches -// a node of the target. +// exportedReaches reports whether a method that code outside the graph can +// call back reaches a node of the target. func (a *adjacency) exportedReaches(target Target) bool { c := a.exportedClosure() return slices.ContainsFunc(a.match(target), func(id int) bool { return c[id] }) diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index 10e2de73..d009c99d 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -27,7 +27,7 @@ type found struct { methods []string prefixed []prefixed byUnit []Uncertainty - callback []Uncertainty + callback *Uncertainty // reaches reports whether the code of a unit reaches a target, // methodReaches whether a node with the last segment reaches one, and // exportedReaches whether a method of an exported name reaches one. @@ -229,7 +229,7 @@ func (a *adjacency) uncertainties(s *search, maxDepth int) *found { for _, u := range src.at(key, n) { f.add(u) } - if src.callbacks[key] && len(f.callback) == 0 { + if src.callbacks[key] { f.add(*src.callback) } } @@ -247,7 +247,10 @@ func (a *adjacency) uncertainties(s *search, maxDepth int) *found { func (f *found) add(u Uncertainty) { switch { case u.Callback: - f.callback = append(f.callback, u) + // Each callback has the same scope, so a result lists it once. + if f.callback == nil { + f.callback = &u + } case u.Unit != "": f.byUnit = append(f.byUnit, u) case u.ImportPrefix != "": @@ -282,7 +285,11 @@ func (f *found) all() []Uncertainty { for _, p := range f.prefixed { out = append(out, p.u) } - return append(append(out, f.byUnit...), f.callback...) + out = append(out, f.byUnit...) + if f.callback != nil { + out = append(out, *f.callback) + } + return out } // forTarget returns the uncertainties of a target, at most maxPerKind of @@ -301,8 +308,8 @@ func (f *found) forTarget(target Target) ([]Uncertainty, []Uncertainty, int) { all = append(all, u) } } - if len(f.callback) > 0 && f.exportedReaches(target) { - all = append(all, f.callback...) + if f.callback != nil && f.exportedReaches(target) { + all = append(all, *f.callback) } var setAside []Uncertainty diff --git a/projectgraph/roots.go b/projectgraph/roots.go index d5c8c4fa..30fe6b48 100644 --- a/projectgraph/roots.go +++ b/projectgraph/roots.go @@ -55,7 +55,7 @@ func (b *builder) markRoots(ctx context.Context) error { entry = entry || r.Kind != RootTest && (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).mainLoadsPackage { + if u := b.unitOf[fileindex.Unit(r.Key)]; r.Kind == RootMain && u != nil && b.profileAt(r.Key).loadsPackages { for _, p := range u.files { b.addRoot(p, r.Kind, r.Rule) } diff --git a/projectgraph/rust.go b/projectgraph/rust.go index f8444c0c..496297e1 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -88,7 +88,7 @@ func (b *builder) linkRust(ix *fileindex.Index) { // has the crate as its import prefix, and only a target of the crate sees // it. A graph with no unit links no crate, so it records nothing. func (b *builder) linkCrateRoutes(ix *fileindex.Index) { - if len(b.spaces) < 2 { + if !b.hasUnits() { return } sc := b.scopes[ix.Path] @@ -102,7 +102,7 @@ func (b *builder) linkCrateRoutes(ix *fileindex.Index) { continue } crate, _ := rustCrateNames(path) - if len(b.providers(origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(b.byPath[file], crate), form: modules.FormImport})) > 0 { + if b.provides(origin{lang: ix.Language, unit: ix.Unit, module: b.packageModule(b.byPath[file], crate), form: modules.FormImport}) { continue } b.addUnresolved(UnresolvedSite{ diff --git a/projectgraph/values.go b/projectgraph/values.go index d865c66c..f1eaa58a 100644 --- a/projectgraph/values.go +++ b/projectgraph/values.go @@ -8,20 +8,6 @@ import ( "github.com/safedep/code/fileindex" ) -// functionValue gives a call of a function value the reason function-value -// in a language whose function values the link step joins to the code -// that holds them. The function that such a call runs is a function that -// some code uses as a value, with a reference edge from that code, or a -// function literal, whose calls belong to the code that holds it. The -// search reaches that code before the value exists, so the record names no -// target. -func functionValue(p *profile, reason fileindex.UnresolvedReason) fileindex.UnresolvedReason { - if p.functionValues && reason == fileindex.UnresolvedComputedName { - return fileindex.UnresolvedFunctionValue - } - return reason -} - // linkUnknownValue records a reference to a member that the link step // cannot name, as the method value h.Handle on a value of an interface. The // code that gets the value can call any method of the name, so the record @@ -38,34 +24,50 @@ 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}) } +// reflection is the package of a language that can run any function by a +// value or by a name, and the methods of it that do it. +type reflection struct { + module string + methods map[string]bool +} + +// reflective reports whether a call or a reference of the method in a file +// that imports the package of reflection can run a function that it gives. +// The link step does not always know that the receiver is a value of the +// package, as when no unit gives the standard library. So any receiver +// counts. +func (r *reflection) reflective(ix *fileindex.Index, method string) bool { + return r.methods[method] && slices.ContainsFunc(ix.Imports, func(imp fileindex.Import) bool { return imp.Module == r.module }) +} + // linkReflection adds a computed-name record at each call and each // reference of a file that can run a function that reflection gives, as // reflect.ValueOf(f).Call(nil). Such a call can run any function, and a // function value that reflection gives has no reference edge. A site whose -// every target resolves to a definition outside package reflect, as -// stream.Method() on a type of the project, gets no record. +// every target resolves to a definition outside the package of reflection, +// as stream.Method() on a type of the project, gets no record. func (b *builder) linkReflection(ix *fileindex.Index) { - reflective := profileOf(ix.Language).reflective - if reflective == nil || isDeclarationFile(ix.Path) { + refl := profileOf(ix.Language).reflection + if refl == nil || isDeclarationFile(ix.Path) { return } sc := b.scopes[ix.Path] for _, call := range ix.Calls { - if call.Kind == fileindex.CallImport || !reflective(ix, cmp.Or(call.Method, lastWritten(call.Callee))) { + if call.Kind == fileindex.CallImport || !refl.reflective(ix, cmp.Or(call.Method, lastWritten(call.Callee))) { continue } caller := call.Caller if b.g.Nodes[caller] == nil { caller = sc.enclosing(call.Line) } - if b.resolvesOutsideReflect(sc, caller, call.Targets) { + if b.resolvesOutside(refl.module, sc, caller, call.Targets) { continue } site := Site{Path: ix.Path, Line: call.Line, Column: call.Column} b.addUnresolved(UnresolvedSite{Caller: b.runner(caller), Site: site, Text: call.Callee, Receiver: call.Receiver, Reason: fileindex.UnresolvedComputedName}) } for _, ref := range ix.References { - if !reflective(ix, lastWritten(ref.Text)) || b.resolvesOutsideReflect(sc, ref.Caller, ref.Targets) { + if !refl.reflective(ix, lastWritten(ref.Text)) || b.resolvesOutside(refl.module, sc, ref.Caller, ref.Targets) { continue } site := Site{Path: ix.Path, Line: ref.Line, Column: ref.Column} @@ -73,10 +75,9 @@ func (b *builder) linkReflection(ix *fileindex.Index) { } } -// resolvesOutsideReflect reports whether each target of a site resolves to -// a key that is not in package reflect, in the graph or under its external -// key. -func (b *builder) resolvesOutsideReflect(sc *scope, caller string, targets []string) bool { +// resolvesOutside reports whether each target of a site resolves to a key +// that is not in the module, in the graph or under its external key. +func (b *builder) resolvesOutside(module string, sc *scope, caller string, targets []string) bool { rs := b.resolveTargets(sc, caller, targets) return len(rs) > 0 && !slices.ContainsFunc(rs, func(r resolution) bool { key := r.key @@ -91,6 +92,6 @@ func (b *builder) resolvesOutsideReflect(sc *scope, caller string, targets []str if external, _, ok := b.unitSymbol(key); ok { key = external } - return fileindex.Unit(key) == "reflect" + return fileindex.Unit(key) == module }) } diff --git a/projectgraph/walk.go b/projectgraph/walk.go index 0dea2661..0d29fd49 100644 --- a/projectgraph/walk.go +++ b/projectgraph/walk.go @@ -9,6 +9,12 @@ import ( "github.com/safedep/code/langcode" ) +// unknownReceiver is the failure of a name on a value whose type the walk +// does not know. +func unknownReceiver(name string) *failure { + return &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} +} + // walkTyped resolves the names on a receiver of a declared type. func (b *builder) walkTyped(class string, names []string) resolution { r := b.walkFrom(resolution{key: class, kind: EdgeDirect, virtual: true}, names) @@ -62,7 +68,7 @@ func (b *builder) walkFrom(r resolution, names []string) resolution { case kind == NodeExport: target, ok := b.bound(r.key) if !ok { - r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} + r.fail = unknownReceiver(name) return r } r.key = target @@ -73,7 +79,7 @@ func (b *builder) walkFrom(r resolution, names []string) resolution { case b.holdsData(r.key): // The walk does not know the type of a field, a constant or // a variable of data, so it does not know a name on it. - r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} + r.fail = unknownReceiver(name) return r case kind == NodeExternal || kind == "" && b.g.IsExternal(r.key): // A name before the last one under an external key is a call @@ -86,7 +92,7 @@ func (b *builder) walkFrom(r resolution, names []string) resolution { skip, typed := unwrap(res.wraps, names[i+1:]) switch { case !typed: - r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: names[i+1]} + r.fail = unknownReceiver(names[i+1]) return r case i+1+skip < len(names): r.key, r.virtual, r.declared = res.class, true, true @@ -135,7 +141,7 @@ func (b *builder) walkFrom(r resolution, names []string) resolution { if b.kindOf(r.key) == NodeVariable { value, ok := b.variableValue(r.key) if !ok { - r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: fileindex.Last(r.key)} + r.fail = unknownReceiver(fileindex.Last(r.key)) return r } r.key = value @@ -190,7 +196,7 @@ func (b *builder) walkVariable(r *resolution, name string) (consumed, done bool) } value, ok := b.variableValue(r.key) if !ok { - r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} + r.fail = unknownReceiver(name) return false, true } r.key = value @@ -245,31 +251,20 @@ func (b *builder) walkClass(r *resolution, name string, last bool, instance *boo return true, false } } - // A read of a field runs no code. A name on a field whose type the - // walk does not know is unknown. - if field, ok := b.goField(r.key, name); ok { - if !last { - r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} - return false, true + if member := p.structMember; member != nil { + if consumed, done, ok := member(b, r, name, last); ok { + return consumed, done } - r.key = field - return true, false - } - // A method that an embedded interface declares runs on the value that - // the field holds, as a call on a value of the interface does. - if iface, ok := b.declaringInterface(r.key, name); ok { - r.class, r.virtual, r.fail = iface, true, noDefinition(name) - return false, true } base, external := b.externalBase(r.key) switch { case b.forwards(r.key): - r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} + r.fail = unknownReceiver(name) case external && last: r.key, r.kind, r.rule = b.deriveExternal(base, []string{name}), EdgeMember, ruleMember return true, false case external: - r.fail = &failure{reason: fileindex.UnresolvedUnknownReceiver, name: name} + r.fail = unknownReceiver(name) default: r.fail = noDefinition(name) } From 7f5e2841d0b1e8470685048d8e31caacc889649a Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:57:39 +0000 Subject: [PATCH 27/58] refactor(indexer): add definitions through one helper (FOU-558) The definition literal was built in addGoDefinition, which no rule of Go needs, twice in rustTypes and again in addBody. addDefinition in types.go now builds it and returns the definition. goVariableValues found the definition of each name of a spec by its key and a compare of lines. goValue now returns the definition that it adds, and goVariableValues takes these definitions. importPath, which reads Go imports only, is goImportPath now. No result changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/golang.go | 61 ++++++++++++++++------------------------ plugin/indexer/rust.go | 17 ++--------- plugin/indexer/types.go | 13 +++++++-- 3 files changed, 37 insertions(+), 54 deletions(-) diff --git a/plugin/indexer/golang.go b/plugin/indexer/golang.go index 6e71fabf..699dc5b2 100644 --- a/plugin/indexer/golang.go +++ b/plugin/indexer/golang.go @@ -72,7 +72,7 @@ func goPackage(p *projection) string { // of each struct, the constants and the package variables. The call graph // has no node for them, so the tree gives them. func goDefinitions(p *projection) { - var vars []*sitter.Node + var specs []*sitter.Node for _, statement := range ts.NamedChildren(p.tree.Tree().RootNode()) { switch statement.Type() { case "type_declaration": @@ -86,17 +86,18 @@ func goDefinitions(p *projection) { } } case "var_declaration": - vars = append(vars, goVarSpecs(statement)...) + specs = append(specs, goVarSpecs(statement)...) } } - for _, spec := range vars { + vars := make([][]*fileindex.Definition, len(specs)) + for i, spec := range specs { for _, name := range goNames(spec) { - p.goValue(name, spec, fileindex.DefVariable) + vars[i] = append(vars[i], p.goValue(name, spec, fileindex.DefVariable)) } } // A value can name a variable that the file declares after it. - for _, spec := range vars { - p.goVariableValues(spec) + for i, spec := range specs { + p.goVariableValues(spec, vars[i]) } } @@ -117,14 +118,14 @@ func (p *projection) goTypes(statement *sitter.Node) { if t != nil && t.Type() == "interface_type" { kind = fileindex.DefInterface } - p.addGoDefinition(key, kind, spec) + p.addDefinition(key, kind, spec) p.addSyntax(key, spec) if t == nil || t.Type() != "struct_type" || spec.Type() != "type_spec" { continue } for _, field := range p.goStructFields(t) { for _, name := range childrenByField(field, "name") { - p.addGoDefinition(fileindex.Join(key, p.text(name)), fileindex.DefField, field) + p.addDefinition(fileindex.Join(key, p.text(name)), fileindex.DefField, field) } } } @@ -151,26 +152,15 @@ func goNames(spec *sitter.Node) []*sitter.Node { return slices.DeleteFunc(childrenByField(spec, "name"), func(n *sitter.Node) bool { return n.Type() != "identifier" }) } -// goValue adds a constant or a package variable of a spec. The blank name -// _ declares nothing. -func (p *projection) goValue(name, spec *sitter.Node, kind fileindex.DefKind) { - if p.text(name) == "_" { - return - } +// goValue adds a constant or a package variable of a spec, and returns it. +// The blank name _ declares nothing, and a name that the file defines +// already keeps its definition. Both give nil. +func (p *projection) goValue(name, spec *sitter.Node, kind fileindex.DefKind) *fileindex.Definition { key := p.local(p.text(name)) - if _, defined := p.defs[key]; !defined { - p.addGoDefinition(key, kind, spec) - } -} - -func (p *projection) addGoDefinition(key string, kind fileindex.DefKind, node *sitter.Node) { - p.defs[key] = &fileindex.Definition{ - Key: key, - Kind: kind, - Name: fileindex.Last(key), - StartLine: lineOf(node), - EndLine: int(node.EndPoint().Row) + 1, + if _, defined := p.defs[key]; defined || p.text(name) == "_" { + return nil } + return p.addDefinition(key, kind, spec) } // goVariableValues fills each package variable of a spec: the per-file @@ -178,21 +168,20 @@ func (p *projection) addGoDefinition(key string, kind fileindex.DefKind, node *s // var Run = run, or the Data mark when the source shows that the value is // no function. A variable with neither can hold a function that the index // does not know, as the value of a function literal or of a call, so a -// call of it keeps its record. -func (p *projection) goVariableValues(spec *sitter.Node) { - names := goNames(spec) +// call of it keeps its record. defs holds the definition that goValue +// added for each name of the spec, or nil. +func (p *projection) goVariableValues(spec *sitter.Node, defs []*fileindex.Definition) { var values []*sitter.Node if list := spec.ChildByFieldName("value"); list != nil { values = ts.NamedChildren(list) } typed := spec.ChildByFieldName("type") - for i, name := range names { - def := p.defs[p.local(p.text(name))] - if def == nil || def.Kind != fileindex.DefVariable || def.StartLine != lineOf(spec) { + for i, def := range defs { + if def == nil { continue } var value *sitter.Node - if len(values) == len(names) { + if len(values) == len(defs) { value = values[i] } switch { @@ -255,15 +244,15 @@ func (p *projection) goDataValue(value *sitter.Node) bool { return p.goPredeclared(p.text(function)) case "selector_expression": operand, field := function.ChildByFieldName("operand"), function.ChildByFieldName("field") - return operand != nil && field != nil && goErrorConstructors[[2]string{p.importPath(p.text(operand)), p.text(field)}] + return operand != nil && field != nil && goErrorConstructors[[2]string{p.goImportPath(p.text(operand)), p.text(field)}] } return p.goDataType(function) } return false } -// importPath returns the module of the import that binds the alias. -func (p *projection) importPath(alias string) string { +// goImportPath returns the import path of the import that binds the alias. +func (p *projection) goImportPath(alias string) string { for _, imp := range p.ix.Imports { if imp.Alias == alias { return imp.Module diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 08af22f6..d6071d64 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -72,24 +72,11 @@ func rustTypes(p *projection) { if _, ok := p.defs[key]; ok { continue } - p.defs[key] = &fileindex.Definition{ - Key: key, - Kind: fileindex.DefClass, - Name: fileindex.Last(key), - StartLine: lineOf(item), - EndLine: int(item.EndPoint().Row) + 1, - } + p.addDefinition(key, fileindex.DefClass, item) p.addSyntax(key, item) case item.Type() == "type_item": key := fileindex.Join(scope, p.text(item.ChildByFieldName("name"))) - p.defs[key] = &fileindex.Definition{ - Key: key, - Kind: fileindex.DefAlias, - Name: fileindex.Last(key), - StartLine: lineOf(item), - EndLine: int(item.EndPoint().Row) + 1, - Alias: p.rustTypeName(item.ChildByFieldName("type"), nil), - } + p.addDefinition(key, fileindex.DefAlias, item).Alias = p.rustTypeName(item.ChildByFieldName("type"), nil) p.addSyntax(key, item) } } diff --git a/plugin/indexer/types.go b/plugin/indexer/types.go index 754df4f9..c8e3be5f 100644 --- a/plugin/indexer/types.go +++ b/plugin/indexer/types.go @@ -40,9 +40,9 @@ func (p *projection) defined(node *sitter.Node) bool { return ok } -// addBody adds a definition that the call graph does not know and that -// takes the calls of the node. -func (p *projection) addBody(key string, kind fileindex.DefKind, node *sitter.Node) { +// addDefinition adds a definition of the kind with the lines of the node, +// and returns it. +func (p *projection) addDefinition(key string, kind fileindex.DefKind, node *sitter.Node) *fileindex.Definition { def := &fileindex.Definition{ Key: key, Kind: kind, @@ -51,6 +51,13 @@ func (p *projection) addBody(key string, kind fileindex.DefKind, node *sitter.No EndLine: int(node.EndPoint().Row) + 1, } p.defs[key] = def + return def +} + +// addBody adds a definition that the call graph does not know and that +// takes the calls of the node. +func (p *projection) addBody(key string, kind fileindex.DefKind, node *sitter.Node) { + def := p.addDefinition(key, kind, node) p.addSyntax(key, node) p.bodies[spanOf(node)] = key p.fillDefinition(def) From 39eae86f2a5957dd3353c160bc2df243034506b6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:57:54 +0000 Subject: [PATCH 28/58] fix(reach): count the uncertainties of the code that a scope reaches (A3) An ambiguous site keeps its method name (FOU-557), and its record concerns what the definitions of the name reach. The closure followed edges only. A candidate that reached a target through an unresolved site of its own was not counted. In the case python/name-ambiguous-record, H0.handle calls self.codec.load, and main passes the module yaml as the codec. yaml//load read as no-such-target. The closure of a unit and the callback scope of the Go standard library had the same gap. A span now replaces the three closures. It walks the edges from the start of the scope, and at each node it meets the uncertainties as the search of a query counts them. A record with a method name, a unit or a callback enters that scope too. An import prefix adds its prefix. Any other record, a dynamic flag, a truncation, or a unit with an incomplete file concerns every target. reach.Scoped keeps the same scope for a summary, with the new fields Methods, ImportPrefixes and Every. The change only adds targets to a scope, so it cannot turn an uncertain answer into not-found. os//remove in python/name-ambiguous-record and the callback rows of TestReachCallback stay not-found, so the scope does not grow to every target where no record says so. New cases python/name-ambiguous-record, python/dependency-dark-unit-record and go/stdlib-callback-record, and the unit test TestReachSpanCountsTheRecordsOfItsCode. Each one fails on b86cb26 with not-found or no-such-target. The span also removes the three copies of the closure test (M8). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 26 +- projectgraph/reach/reach.go | 154 ++--------- projectgraph/reach/reach_test.go | 47 +++- projectgraph/reach/sets.go | 58 +++-- projectgraph/reach/span.go | 245 ++++++++++++++++++ projectgraph/reach/uncertainty.go | 29 +-- .../go/chained-external-call/expect.yaml | 2 + .../cases/go/dependency-callback/expect.yaml | 6 +- .../go/dependency-data-reads/expect.yaml | 10 +- .../go_example.com_markup_v1.0.0.json | 10 +- .../go/stdlib-callback-record/expect.yaml | 32 +++ .../go/stdlib-callback-record/project/go.mod | 5 + .../go/stdlib-callback-record/project/main.go | 22 ++ .../dependency-dark-unit-record/expect.yaml | 34 +++ .../project/main.py | 9 + .../venv/site-packages/yaml/__init__.py | 9 + .../summaries/pypi_PyYAML_6.0.1.json | 52 ++++ .../python/name-ambiguous-record/expect.yaml | 36 +++ .../name-ambiguous-record/project/Procfile | 1 + .../name-ambiguous-record/project/handlers.py | 133 ++++++++++ .../name-ambiguous-record/project/main.py | 9 + 21 files changed, 737 insertions(+), 192 deletions(-) create mode 100644 projectgraph/reach/span.go create mode 100644 test/projectgraph/cases/go/stdlib-callback-record/expect.yaml create mode 100644 test/projectgraph/cases/go/stdlib-callback-record/project/go.mod create mode 100644 test/projectgraph/cases/go/stdlib-callback-record/project/main.go create mode 100644 test/projectgraph/cases/python/dependency-dark-unit-record/expect.yaml create mode 100644 test/projectgraph/cases/python/dependency-dark-unit-record/project/main.py create mode 100644 test/projectgraph/cases/python/dependency-dark-unit-record/project/venv/site-packages/yaml/__init__.py create mode 100644 test/projectgraph/cases/python/dependency-dark-unit-record/summaries/pypi_PyYAML_6.0.1.json create mode 100644 test/projectgraph/cases/python/name-ambiguous-record/expect.yaml create mode 100644 test/projectgraph/cases/python/name-ambiguous-record/project/Procfile create mode 100644 test/projectgraph/cases/python/name-ambiguous-record/project/handlers.py create mode 100644 test/projectgraph/cases/python/name-ambiguous-record/project/main.py diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 1de64c40..786dee0c 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -201,7 +201,13 @@ follow what the candidates call. The site keeps its method name, because the call can run only a method of that name. A record with a method name concerns the targets with that last segment and the targets that a definition of the name reaches, so it concerns what each candidate -reaches. +reaches. A candidate can also run code that no edge leads to, through an +unresolved site of its own. So the record also concerns what each +uncertainty of the code that a candidate reaches concerns, as +[The honesty rule](#the-honesty-rule) says. In the case +`python/name-ambiguous-record`, `H0.handle` calls `load` on a field whose +value the index does not know, and `yaml.load` is `uncertain` and not +`no-such-target`. 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 @@ -627,6 +633,19 @@ reaches, as [Code outside the graph](#code-outside-the-graph) argues. A result caps the uncertainties that it lists at three per kind and keeps the full count in `Searched`. +The code of such a scope can hold uncertainties of its own: an unresolved +site, a dynamic flag, a truncation, or a file of a unit that the index +step did not complete. That code can run code that no edge leads to. So +the scope also concerns what each of them concerns, as the search of a +query counts them, and the scope of a method name, a unit or a callback +that one of them names goes on into that code too. Without that, a target +that only such code reaches reads as `not-found`. The cases +`python/name-ambiguous-record` for a method name, +`python/dependency-dark-unit-record` for a unit and +`go/stdlib-callback-record` for a callback show it. A summary keeps the +same scope: `reach.Scoped` holds the method names and the import prefixes +of those uncertainties, and `Every` marks one that concerns every target. + A file of dependency code that the index step did not complete counts only for a search that enters its package. A file of the project counts for every search, because it can hold a root. @@ -721,7 +740,7 @@ A failed or truncated file of a unit of dependency code counts for every target when the search enters the unit. An unresolved site in the code of a unit with no method name, as an import that no file of the unit gives, counts for every target too. The closure of the edges of the unit, which -`unitReaches` reads, does not bound such code, for two reasons. +the scope of a unit reads, does not bound such code, for two reasons. - The unknown code can call a package that no known code of the unit calls. A failed file has no index, so its imports are not known. The @@ -768,7 +787,8 @@ depends on what the code can call. exported name. So the record of a key of the standard library is a callback, as `Callback` marks it. It concerns the targets that a Go method of an exported name reaches, in the project and in each unit, as - the closure of a method name does. Each key of the standard library gives + the scope of a method name does, and what the uncertainties of that code + concern. Each key of the standard library gives the same scope, so a result lists the callback once, with no key. - A package of another module can import the package of any target and call any function of it. So its record concerns every target. A summary diff --git a/projectgraph/reach/reach.go b/projectgraph/reach/reach.go index 00e5c384..460f83b2 100644 --- a/projectgraph/reach/reach.go +++ b/projectgraph/reach/reach.go @@ -103,10 +103,12 @@ type CallSite struct { // definition to the targets that the code of the package, and of each // package that it calls, reaches. Callback scopes code outside the graph // that can call into the graph only through a method of a value that it -// holds, as the Go standard library with no unit, to the targets that any -// method of an exported name reaches. Such an uncertainty has no key and -// counts once per search. With none of them, the uncertainty concerns -// every target. +// holds, as the Go standard library with no unit, to the targets that a +// 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. type Uncertainty struct { Kind UncertaintyKind `json:"kind"` Key string `json:"key,omitempty"` @@ -354,10 +356,10 @@ func (x *Explorer) Explore(ctx context.Context, from []string, maxDepth int) (*E // adjacency is the graph with integer node ids, in key order, and the edges out // of each node. unitNodes holds the nodes of each unit of dependency code, -// and closures what the code of each unit reaches. byLast holds the nodes -// by the last segment of their key, and methodClosures what the nodes of -// each last segment reach. Each table fills on first use. includeTests -// lets a node of a test file be a source of a method closure. +// byLast the nodes by the last segment of their key, callbacks the methods +// that code outside the graph can call, and spans what the code of each +// scope can run. Each table fills on first use. includeTests lets a node +// of a test file start the span of a method name or of the callback. type adjacency struct { g *projectgraph.Graph keys []string @@ -372,12 +374,11 @@ type adjacency struct { includeTests bool - src *sources - unitNodes map[string][]int - closures map[string][]bool - byLast map[string][]int - methodClosures map[string][]bool - exported []bool + src *sources + unitNodes map[string][]int + byLast map[string][]int + callbacks []int + spans map[scopeKey]*span } // sources returns the records, the flags and the budgets of the graph by @@ -425,131 +426,6 @@ func newAdjacency(g *projectgraph.Graph) *adjacency { return a } -// unitOf returns the unit of dependency code of a node, or "". -func (a *adjacency) unitOf(id int) string { - if n := a.g.Nodes[a.keys[id]]; n != nil { - return fileindex.PackageOf(n.Path) - } - return "" -} - -// closure returns the nodes that the code of a unit reaches, and the code -// of each unit that a node of it reaches. A value of the project that the -// code of a unit calls comes to it on an edge, as a reference, so the -// search from the project reaches that value already. -func (a *adjacency) closure(unit string) []bool { - if c, ok := a.closures[unit]; ok { - return c - } - if a.unitNodes == nil { - a.unitNodes, a.closures = map[string][]int{}, map[string][]bool{} - for id := range a.keys { - if u := a.unitOf(id); u != "" { - a.unitNodes[u] = append(a.unitNodes[u], id) - } - } - } - entered := map[string]bool{} - seen := a.reachable(a.unitNodes[unit], func(id int) []int { - u := a.unitOf(id) - if u == "" || entered[u] { - return nil - } - entered[u] = true - return a.unitNodes[u] - }) - a.closures[unit] = seen - return seen -} - -// reachable returns the nodes that a search from the sources reaches. The -// search also takes the nodes that more gives for each node that it -// reaches, as the other nodes of the unit of the node. -func (a *adjacency) reachable(sources []int, more func(id int) []int) []bool { - seen := make([]bool, len(a.keys)) - var queue []int - add := func(id int) { - if !seen[id] { - seen[id] = true - queue = append(queue, id) - } - } - for _, id := range sources { - add(id) - } - for len(queue) > 0 { - id := queue[0] - queue = queue[1:] - for _, v := range more(id) { - add(v) - } - for _, e := range a.out[id] { - add(a.id[a.g.Edges[e].To]) - } - } - return seen -} - -// unitReaches reports whether the code of a unit reaches a node of the -// target. -func (a *adjacency) unitReaches(unit string, target Target) bool { - c := a.closure(unit) - return slices.ContainsFunc(a.match(target), func(id int) bool { return c[id] }) -} - -// methodClosure returns the nodes that the nodes with the last segment -// reach: the definitions of the name in the project and in each unit of -// dependency code, and the external keys of the name, which can lead into -// a unit. An unresolved call of the name can run any of them. A node of a -// test file is a source only when the query includes the tests. -func (a *adjacency) methodClosure(name string) []bool { - if c, ok := a.methodClosures[name]; ok { - return c - } - if a.byLast == nil { - a.byLast, a.methodClosures = map[string][]int{}, map[string][]bool{} - for id, key := range a.keys { - if n := a.g.Nodes[key]; n != nil && (a.includeTests || !n.Test) { - a.byLast[fileindex.Last(key)] = append(a.byLast[fileindex.Last(key)], id) - } - } - } - seen := a.reachable(a.byLast[name], func(int) []int { return nil }) - a.methodClosures[name] = seen - return seen -} - -// methodReaches reports whether a node with the last segment reaches a -// node of the target. -func (a *adjacency) methodReaches(name string, target Target) bool { - c := a.methodClosure(name) - return slices.ContainsFunc(a.match(target), func(id int) bool { return c[id] }) -} - -// exportedClosure returns the nodes that the methods that code outside the -// graph can call back reach, as the graph names them, in the project and in -// each unit of dependency code. A method of a test file is a source only -// when the query includes the tests. -func (a *adjacency) exportedClosure() []bool { - if a.exported == nil { - var methods []int - for id, key := range a.keys { - if n := a.g.Nodes[key]; n != nil && (a.includeTests || !n.Test) && a.g.CallbackMethod(key) { - methods = append(methods, id) - } - } - a.exported = a.reachable(methods, func(int) []int { return nil }) - } - return a.exported -} - -// exportedReaches reports whether a method that code outside the graph can -// call back reaches a node of the target. -func (a *adjacency) exportedReaches(target Target) bool { - c := a.exportedClosure() - return slices.ContainsFunc(a.match(target), func(id int) bool { return c[id] }) -} - // match returns the nodes of a target, by id: the external node of its // key, and each definition of a unit of dependency code whose external key // it names. A call inside a unit goes to the definition, so the search diff --git a/projectgraph/reach/reach_test.go b/projectgraph/reach/reach_test.go index d3bd400a..eb158d5a 100644 --- a/projectgraph/reach/reach_test.go +++ b/projectgraph/reach/reach_test.go @@ -381,6 +381,51 @@ unresolved handlers.py//parse 5 unknown-receiver f assert.Equal(t, Uncertain, r.Status) } +// TestReachSpanCountsTheRecordsOfItsCode checks that an unresolved call of a +// method concerns what its candidates run through their own unresolved +// calls, as the ambiguous call handle whose candidate calls load on a value +// of an unknown type. Reach and the table of ExploreAll give one answer. +func TestReachSpanCountsTheRecordsOfItsCode(t *testing.T) { + const spec = ` +root main.py//run +unresolved main.py//run 3 ambiguous handle +edge lib.py//H//handle lib.py//helper direct 4 +edge lib.py//Codec//load pickle//loads direct 8 +edge lib.py//dead os//exit direct 12 +` + tests := []struct { + name string + record string + want map[string]Status + }{ + { + name: "a call of a method in a candidate", + record: "unresolved lib.py//helper 5 unknown-receiver load", + want: map[string]Status{"pickle//loads": Uncertain, "json//load": Uncertain, "os//exit": NotFound}, + }, + { + name: "a computed call in a candidate", + record: "unresolved lib.py//helper 5 computed-name", + want: map[string]Status{"pickle//loads": Uncertain, "json//load": Uncertain, "os//exit": Uncertain}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := graphOf(t, spec+tt.record) + sets, err := NewExplorer(g).ExploreAll(context.Background(), []string{"main.py//run"}) + require.NoError(t, err) + require.Len(t, sets.Sources[0].Uncertainties, 1) + handle := sets.Uncertainties[sets.Sources[0].Uncertainties[0]] + require.Equal(t, "handle", handle.Method) + for target, status := range tt.want { + r := reachOne(t, g, Query{Targets: targetsOf(t, target)}) + assert.Equal(t, status, r.Status, target) + assert.Equal(t, status == Uncertain, handle.Concerns(targetOf(t, target)), target) + } + }) + } +} + func TestReachCapsUncertaintiesPerKind(t *testing.T) { spec := basic for i := range 5 { @@ -628,7 +673,7 @@ func TestReachDependencyCode(t *testing.T) { { name: "an entry into a unit with a failed file", extra: "edge main.py//run kit//load//close direct 4\nentry kit//load//close npm:kit@1.0.0\nfile npm:kit@1.0.0//big.js failed", - target: "fs//rm", status: Uncertain, kinds: []UncertaintyKind{Budget}, + target: "fs//rm", status: Uncertain, kinds: []UncertaintyKind{Budget, UnresolvedCall}, }, } for _, tt := range tests { diff --git a/projectgraph/reach/sets.go b/projectgraph/reach/sets.go index 31e09f9d..c8c44744 100644 --- a/projectgraph/reach/sets.go +++ b/projectgraph/reach/sets.go @@ -12,27 +12,33 @@ import ( // concerns. Targets is nil for an uncertainty that concerns every target. // A Method scope also concerns every target whose last segment is the // method, and an ImportPrefix scope every target under the prefix, whether -// the graph holds the target or not. +// the graph holds the target or not. The code of a Method, a Unit or a +// Callback scope can hold unresolved calls of its own. Methods holds the +// method names of those calls, ImportPrefixes their import prefixes, and +// Every marks one that concerns every target. type Scoped struct { Uncertainty - Targets []string `json:"targets,omitempty"` + Targets []string `json:"targets,omitempty"` + Methods []string `json:"methods,omitempty"` + ImportPrefixes []string `json:"import_prefixes,omitempty"` + Every bool `json:"every,omitempty"` } // Concerns reports whether the uncertainty concerns a target, as the -// result of a reach query lists it: a Unit or a Callback scope concerns its -// Targets, an ImportPrefix scope every target under the prefix, a Method -// scope its Targets and every target whose last segment is the method, and -// an uncertainty with no scope every target. +// result of a reach query lists it: an ImportPrefix scope concerns every +// target under the prefix, and an uncertainty with no scope every target. +// A Method, a Unit or a Callback scope concerns its Targets, every target +// whose last segment is its method or one of its Methods, every target +// under one of its ImportPrefixes, and every target when Every is set. func (u Scoped) Concerns(t Target) bool { - switch { - case u.Unit != "", u.Callback: - return t.MatchesSorted(u.Targets) - case u.ImportPrefix != "": - return t.mayImport(u.ImportPrefix) - case u.Method != "": - return t.hasMethod(u.Method) || t.MatchesSorted(u.Targets) - } - return true + if _, ok := scopeOf(u.Uncertainty); !ok { + return u.ImportPrefix == "" || t.mayImport(u.ImportPrefix) + } + return u.Every || + u.Method != "" && t.hasMethod(u.Method) || + slices.ContainsFunc(u.Methods, t.hasMethod) || + slices.ContainsFunc(u.ImportPrefixes, t.mayImport) || + t.MatchesSorted(u.Targets) } // Reachable is what one source reaches: the external nodes, in key order, @@ -166,25 +172,25 @@ func (a *adjacency) table() *table { return t } -// scoped adds the targets that the scope of an uncertainty concerns. +// scoped adds what the span of the scope of an uncertainty concerns. func (a *adjacency) scoped(u Uncertainty) Scoped { s := Scoped{Uncertainty: u} - var closure []bool - switch { - case u.Callback: - closure = a.exportedClosure() - case u.Unit != "": - closure = a.closure(u.Unit) - case u.Method != "": - closure = a.methodClosure(u.Method) - default: + k, ok := scopeOf(u) + if !ok { + return s + } + sp := a.span(k) + if sp.every { + s.Every = true return s } for _, id := range a.externals { - if closure[id] { + if sp.nodes[id] { s.Targets = append(s.Targets, a.keys[id]) } } + s.Methods = slices.DeleteFunc(slices.Clone(sp.methods), func(m string) bool { return m == u.Method }) + s.ImportPrefixes = sp.prefixes return s } diff --git a/projectgraph/reach/span.go b/projectgraph/reach/span.go new file mode 100644 index 00000000..9a267c2d --- /dev/null +++ b/projectgraph/reach/span.go @@ -0,0 +1,245 @@ +package reach + +import ( + "slices" + + "github.com/safedep/code/fileindex" +) + +// scopeKey names the code that a scoped uncertainty can run: the nodes +// with a last segment, for an unresolved call of a method, the code of a +// unit, for a call into a unit at no known definition, or the methods that +// code outside the graph can call back. +type scopeKey struct { + method string + unit string + callback bool +} + +// scopeOf returns the scope of an uncertainty, or false for an uncertainty +// that an import prefix scopes or that concerns every target. +func scopeOf(u Uncertainty) (scopeKey, bool) { + switch { + case u.Callback: + return scopeKey{callback: true}, true + case u.Unit != "": + return scopeKey{unit: u.Unit}, true + case u.ImportPrefix != "": + return scopeKey{}, false + case u.Method != "": + return scopeKey{method: u.Method}, true + } + return scopeKey{}, false +} + +// span is what the code of a scope can run: the nodes that it reaches, and +// what each uncertainty of those nodes concerns. An unresolved call in that +// code can run code that no edge leads to, so the span goes on into the +// scope of the call. methods holds each method name that the span enters, +// whose targets a call of the name concerns by their last segment, and +// prefixes the import prefix of each computed import. every marks an +// uncertainty that concerns every target. Without it, a target that only a +// candidate of an ambiguous call reaches, through an unresolved call of +// its own, reads as not-found. +type span struct { + nodes []bool + methods []string + prefixes []string + every bool +} + +// concerns reports whether the span concerns a target, by the nodes of the +// target. +func (s *span) concerns(ids []int, t Target) bool { + return s.every || + slices.ContainsFunc(s.methods, t.hasMethod) || + slices.ContainsFunc(s.prefixes, t.mayImport) || + slices.ContainsFunc(ids, func(id int) bool { return s.nodes[id] }) +} + +// concerns reports whether the code of a scope can run a target, or meets +// an uncertainty that concerns it. +func (a *adjacency) concerns(k scopeKey, t Target) bool { + return a.span(k).concerns(a.match(t), t) +} + +// span returns the span of a scope, computed once per adjacency. +func (a *adjacency) span(k scopeKey) *span { + if s, ok := a.spans[k]; ok { + return s + } + if a.spans == nil { + a.spans = map[scopeKey]*span{} + } + w := &spanWalk{a: a, src: a.sources(), s: &span{nodes: make([]bool, len(a.keys))}, inUnit: make([]bool, len(a.keys)), entered: map[scopeKey]bool{}, units: map[string]bool{}} + w.enter(k) + w.run() + a.spans[k] = w.s + return w.s +} + +// spanWalk is the state of the search of one span. The code of a unit is +// any code of the unit, so a node that the code of a unit reaches enters +// every node of its own unit, as inUnit marks. entered holds the scopes +// that the walk entered, and units the units whose code it entered. +type spanWalk struct { + a *adjacency + src *sources + s *span + inUnit []bool + queue []spanNode + entered map[scopeKey]bool + units map[string]bool +} + +type spanNode struct { + id int + unit bool +} + +func (w *spanWalk) add(id int, unit bool) { + if w.inUnit[id] || !unit && w.s.nodes[id] { + return + } + w.s.nodes[id] = true + w.inUnit[id] = unit + w.queue = append(w.queue, spanNode{id: id, unit: unit}) +} + +// enter adds the nodes where the code of a scope starts. +func (w *spanWalk) enter(k scopeKey) { + if w.entered[k] { + return + } + w.entered[k] = true + switch { + case k.callback: + for _, id := range w.a.callbackMethods() { + w.add(id, false) + } + case k.unit != "": + w.enterUnit(k.unit) + default: + w.s.methods = append(w.s.methods, k.method) + for _, id := range w.a.definitionsOf(k.method) { + w.add(id, false) + } + } +} + +// enterUnit adds every node of a unit as code of the unit. A file of the +// unit with no complete index can hold any code. +func (w *spanWalk) enterUnit(unit string) { + if w.units[unit] { + return + } + w.units[unit] = true + w.budget(unit) + for _, id := range w.a.nodesOf(unit) { + w.add(id, true) + } +} + +// budget marks every target when a unit that the span enters has a file +// with no complete index, as the search of a query counts it. +func (w *spanWalk) budget(unit string) { + if len(w.src.budgets[unit]) > 0 || w.a.includeTests && len(w.src.testBudgets[unit]) > 0 { + w.s.every = true + } +} + +func (w *spanWalk) run() { + a := w.a + for i := 0; i < len(w.queue) && !w.s.every; i++ { + n := w.queue[i] + if n.unit { + if u := a.unitOf(n.id); u != "" { + w.enterUnit(u) + } + } + for _, e := range a.out[n.id] { + w.add(a.id[a.g.Edges[e].To], n.unit) + } + w.meet(n.id) + } +} + +// meet enters the scope of each uncertainty of a node, as the search of a +// query adds it. +func (w *spanWalk) meet(id int) { + key := w.a.keys[id] + n := w.a.g.Nodes[key] + if n == nil { + return + } + for _, unit := range w.src.unitsOf(n, key) { + w.budget(unit) + } + if w.src.callbacks[key] { + w.enter(scopeKey{callback: true}) + } + for _, u := range w.src.at(key, n) { + k, ok := scopeOf(u) + switch { + case ok: + w.enter(k) + case u.ImportPrefix != "": + if !slices.Contains(w.s.prefixes, u.ImportPrefix) { + w.s.prefixes = append(w.s.prefixes, u.ImportPrefix) + } + default: + w.s.every = true + } + } +} + +// unitOf returns the unit of dependency code of a node, or "". +func (a *adjacency) unitOf(id int) string { + if n := a.g.Nodes[a.keys[id]]; n != nil { + return fileindex.PackageOf(n.Path) + } + return "" +} + +// nodesOf returns the nodes of a unit of dependency code. +func (a *adjacency) nodesOf(unit string) []int { + if a.unitNodes == nil { + a.unitNodes = map[string][]int{} + for id := range a.keys { + if u := a.unitOf(id); u != "" { + a.unitNodes[u] = append(a.unitNodes[u], id) + } + } + } + return a.unitNodes[unit] +} + +// definitionsOf returns the nodes with the last segment: the definitions of +// the name in the project and in each unit of dependency code, and the +// external keys of the name, which can lead into a unit. An unresolved +// call of the name can run any of them. +func (a *adjacency) definitionsOf(name string) []int { + if a.byLast == nil { + a.byLast = map[string][]int{} + for id, key := range a.keys { + if n := a.g.Nodes[key]; n != nil && (a.includeTests || !n.Test) { + a.byLast[fileindex.Last(key)] = append(a.byLast[fileindex.Last(key)], id) + } + } + } + return a.byLast[name] +} + +// callbackMethods returns the methods that code outside the graph can call +// on a value that it holds, as the graph names them. +func (a *adjacency) callbackMethods() []int { + if a.callbacks == nil { + a.callbacks = []int{} + for id, key := range a.keys { + if n := a.g.Nodes[key]; n != nil && (a.includeTests || !n.Test) && a.g.CallbackMethod(key) { + a.callbacks = append(a.callbacks, id) + } + } + } + return a.callbacks +} diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index d009c99d..4438146f 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -19,8 +19,8 @@ type prefixed struct { // found collects the uncertainties of the nodes that a search reached, in // the order of the search. A record with a method name concerns only the -// targets with that last segment and the targets that a node with that -// last segment reaches, unless its reason can name any target. +// targets with that last segment and the targets that the span of the +// name concerns, unless its reason can name any target. type found struct { global []Uncertainty byMethod map[string][]Uncertainty @@ -28,12 +28,8 @@ type found struct { prefixed []prefixed byUnit []Uncertainty callback *Uncertainty - // reaches reports whether the code of a unit reaches a target, - // methodReaches whether a node with the last segment reaches one, and - // exportedReaches whether a method of an exported name reaches one. - reaches func(unit string, target Target) bool - methodReaches func(name string, target Target) bool - exportedReaches func(target Target) bool + // concerns reports whether the span of a scope concerns a target. + concerns func(k scopeKey, target Target) bool // units holds the file budgets of each unit of dependency code, and // entered the units that the search entered. units map[string][]Uncertainty @@ -58,10 +54,10 @@ type sources struct { testBudgets map[string][]Uncertainty // units lists the units that a search can enter, in key order. units []string - // callbacks holds the external keys whose code is outside the graph and - // can call a method of a value that it holds, and callback the one - // uncertainty that they share. Each such key gives the same scope, so - // the search counts it once. + // callbacks holds the callers of the records of code outside the graph + // that can call a method of a value that it holds, and callback the one + // uncertainty that they share. Each such record gives the same scope, + // so the search counts it once. callbacks map[string]bool callback *Uncertainty } @@ -204,7 +200,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{}, reaches: a.unitReaches, methodReaches: a.methodReaches, exportedReaches: a.exportedReaches, units: src.budgets, entered: map[string]bool{}} + f := &found{byMethod: map[string][]Uncertainty{}, concerns: a.concerns, units: src.budgets, entered: map[string]bool{}} for _, u := range src.global { f.add(u) } @@ -298,17 +294,16 @@ func (f *found) all() []Uncertainty { func (f *found) forTarget(target Target) ([]Uncertainty, []Uncertainty, int) { all := slices.Clone(f.global) for _, m := range f.methods { - if target.hasMethod(m) || f.methodReaches(m, target) { + if f.concerns(scopeKey{method: m}, target) { all = append(all, f.byMethod[m]...) } } - for _, u := range f.byUnit { - if f.reaches(u.Unit, target) { + if f.concerns(scopeKey{unit: u.Unit}, target) { all = append(all, u) } } - if f.callback != nil && f.exportedReaches(target) { + if f.callback != nil && f.concerns(scopeKey{callback: true}, target) { all = append(all, *f.callback) } diff --git a/test/projectgraph/cases/go/chained-external-call/expect.yaml b/test/projectgraph/cases/go/chained-external-call/expect.yaml index 72afb48f..b91ad742 100644 --- a/test/projectgraph/cases/go/chained-external-call/expect.yaml +++ b/test/projectgraph/cases/go/chained-external-call/expect.yaml @@ -80,3 +80,5 @@ results: uncertainties: - kind: unresolved-call - kind: unresolved-call + - kind: unresolved-call + site: tokens.go:10 diff --git a/test/projectgraph/cases/go/dependency-callback/expect.yaml b/test/projectgraph/cases/go/dependency-callback/expect.yaml index b620ca18..c73e2ceb 100644 --- a/test/projectgraph/cases/go/dependency-callback/expect.yaml +++ b/test/projectgraph/cases/go/dependency-callback/expect.yaml @@ -8,7 +8,9 @@ description: >- target. The call h.Handle() in Serve has no implementation in the graph, so it is an interface-receiver record with the method Handle, and os/exec.LookPath, which mine.Handle calls, is uncertain. The link step cannot name the method of c.Close, so the reference is a record with the method Close, which gets a name - edge to conn.Close. So os.Chdir is found. No target that a callback reaches is not found. + edge to conn.Close. So os.Chdir is found. No target that a callback reaches is not found. conn.Close, a + candidate of c.Close, calls os.Chdir, whose callback reaches mine.Handle. So the record c.Close also + concerns os/exec.LookPath. units: gomod/github.com/acme/srv@v1.0.0: go:github.com/acme/srv@v1.0.0 roots: @@ -89,6 +91,8 @@ results: site: main.go:13 reached: false uncertainties: + - kind: unresolved-call + site: main.go:33 - kind: unresolved-call site: go:github.com/acme/srv@v1.0.0//srv.go:20 - kind: unresolved-call diff --git a/test/projectgraph/cases/go/dependency-data-reads/expect.yaml b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml index 887cbb70..8c39d603 100644 --- a/test/projectgraph/cases/go/dependency-data-reads/expect.yaml +++ b/test/projectgraph/cases/go/dependency-data-reads/expect.yaml @@ -11,6 +11,8 @@ description: >- Conn declares, and netctx.Context.Done is the method Done of the interface context.Context of the unit of the standard library. A call of such a method runs the code of the type that the interface value holds, so each keeps an interface-receiver record, and a target that only that code reaches is uncertain, not found. + The call of Handler in Render can run the key example.com//markup//Handler, whose record enters the code of + the unit, so it concerns each target of that code too. units: gomod/example.com/markup@v1.0.0: go:example.com/markup@v1.0.0 goroot/src: go:std@go1.25.1 @@ -297,11 +299,11 @@ results: reached: false uncertainties: - kind: unresolved-call - site: go:example.com/markup@v1.0.0 + site: go:example.com/markup@v1.0.0//node.go:39 - kind: unresolved-call site: go:example.com/markup@v1.0.0 - kind: unresolved-call - site: go:std@go1.25.1 + site: go:example.com/markup@v1.0.0 - target: os//Remove status: uncertain call_sites: @@ -310,8 +312,8 @@ results: reached: false uncertainties: - kind: unresolved-call - site: go:example.com/markup@v1.0.0 + site: go:example.com/markup@v1.0.0//node.go:39 - kind: unresolved-call site: go:example.com/markup@v1.0.0 - kind: unresolved-call - site: go:std@go1.25.1 + site: go:example.com/markup@v1.0.0 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 e0da8fb1..bb22ae07 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 @@ -207,6 +207,9 @@ "errors//New", "os//Remove", "os//exec//Command" + ], + "methods": [ + "Handler" ] }, { @@ -218,7 +221,12 @@ "column": 2 }, "text": "Handler (unknown-receiver)", - "method": "Handler" + "method": "Handler", + "targets": [ + "errors//New", + "os//Remove", + "os//exec//Command" + ] } ] } diff --git a/test/projectgraph/cases/go/stdlib-callback-record/expect.yaml b/test/projectgraph/cases/go/stdlib-callback-record/expect.yaml new file mode 100644 index 00000000..c765d22a --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-record/expect.yaml @@ -0,0 +1,32 @@ +source: safedep:hand +description: >- + main calls os.Getenv, and no unit gives the standard library, so the callback record of os//Getenv concerns + what a method of an exported name can run. store.Close is such a method. It calls Flush of + example.com/legacy, which no unit gives, and the code of legacy can call any function. The callback scope + counts the records of the code that it reaches, so os.RemoveAll, which only the uncalled purge calls, is + uncertain and not not-found. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: example.com//legacy//Flush + reason: no-dependency-source + - caller: os//Getenv + reason: no-dependency-source + callback: true + - caller: os//RemoveAll + reason: no-dependency-source + callback: true +results: + - target: os//RemoveAll + status: uncertain + call_sites: + - caller: main.go//purge + site: main.go:17 + reached: false + uncertainties: + - kind: unresolved-call diff --git a/test/projectgraph/cases/go/stdlib-callback-record/project/go.mod b/test/projectgraph/cases/go/stdlib-callback-record/project/go.mod new file mode 100644 index 00000000..351653a0 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-record/project/go.mod @@ -0,0 +1,5 @@ +module app + +go 1.22 + +require example.com/legacy v1.0.0 diff --git a/test/projectgraph/cases/go/stdlib-callback-record/project/main.go b/test/projectgraph/cases/go/stdlib-callback-record/project/main.go new file mode 100644 index 00000000..9a67bbdb --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-record/project/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "os" + + "example.com/legacy" +) + +type store struct{} + +func (store) Close() error { + legacy.Flush() + return nil +} + +func purge() { + os.RemoveAll("data") +} + +func main() { + os.Getenv("HOME") +} diff --git a/test/projectgraph/cases/python/dependency-dark-unit-record/expect.yaml b/test/projectgraph/cases/python/dependency-dark-unit-record/expect.yaml new file mode 100644 index 00000000..1aeae745 --- /dev/null +++ b/test/projectgraph/cases/python/dependency-dark-unit-record/expect.yaml @@ -0,0 +1,34 @@ +source: safedep:hand +description: >- + The unit PyYAML holds the package yaml, and no import of the project names it, so a record of the unit that + no node holds concerns what the code of the unit can run in every search. construct of the unit calls a + function that getattr gives, which can be any function. The scope of the unit counts the records of its + code, so os.system, which only the uncalled cleanup of the project calls, is uncertain and not not-found. +units: + venv/site-packages/yaml: pypi:PyYAML@6.0.1 +import_dirs: + - venv/site-packages +problems: + - path: pypi:PyYAML@6.0.1 + text: no import names the unit, and it gives no module for the package pyyaml +roots: + - key: main.py + kind: main + rule: python.main-guard +unresolved: + - caller: "" + site: pypi:PyYAML@6.0.1 + reason: no-definition + - caller: pypi:PyYAML@6.0.1//yaml/__init__.py//construct + site: pypi:PyYAML@6.0.1//yaml/__init__.py:9 + reason: computed-name +results: + - target: os//system + status: uncertain + call_sites: + - caller: main.py//cleanup + site: main.py:5 + reached: false + uncertainties: + - kind: unresolved-call + site: pypi:PyYAML@6.0.1 diff --git a/test/projectgraph/cases/python/dependency-dark-unit-record/project/main.py b/test/projectgraph/cases/python/dependency-dark-unit-record/project/main.py new file mode 100644 index 00000000..cfa5de8e --- /dev/null +++ b/test/projectgraph/cases/python/dependency-dark-unit-record/project/main.py @@ -0,0 +1,9 @@ +import os + + +def cleanup(): + os.system("rm -rf build") + + +if __name__ == "__main__": + print("start") diff --git a/test/projectgraph/cases/python/dependency-dark-unit-record/project/venv/site-packages/yaml/__init__.py b/test/projectgraph/cases/python/dependency-dark-unit-record/project/venv/site-packages/yaml/__init__.py new file mode 100644 index 00000000..31dadd3a --- /dev/null +++ b/test/projectgraph/cases/python/dependency-dark-unit-record/project/venv/site-packages/yaml/__init__.py @@ -0,0 +1,9 @@ +import marshal + + +def load(data): + return marshal.loads(data) + + +def construct(loader, name): + return getattr(loader, name)() 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 new file mode 100644 index 00000000..c185c149 --- /dev/null +++ b/test/projectgraph/cases/python/dependency-dark-unit-record/summaries/pypi_PyYAML_6.0.1.json @@ -0,0 +1,52 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "pypi:PyYAML@6.0.1", + "symbols": [ + { + "key": "yaml//construct", + "definitions": [ + "pypi:PyYAML@6.0.1//yaml/__init__.py//construct" + ], + "reaches": [], + "uncertainties": [ + 0, + 1 + ] + }, + { + "key": "yaml//load", + "definitions": [ + "pypi:PyYAML@6.0.1//yaml/__init__.py//load" + ], + "reaches": [ + "marshal//loads" + ] + } + ], + "targets": [ + "marshal//loads" + ], + "uncertainties": [ + { + "kind": "dynamic-flag", + "key": "pypi:PyYAML@6.0.1//yaml/__init__.py//construct", + "site": { + "path": "pypi:PyYAML@6.0.1//yaml/__init__.py", + "line": 9 + }, + "text": "reflection" + }, + { + "kind": "unresolved-call", + "key": "pypi:PyYAML@6.0.1//yaml/__init__.py//construct", + "site": { + "path": "pypi:PyYAML@6.0.1//yaml/__init__.py", + "line": 9, + "column": 12 + }, + "text": "getattr(loader, name) (computed-name)" + } + ] +} diff --git a/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml new file mode 100644 index 00000000..62c5c57e --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml @@ -0,0 +1,36 @@ +source: safedep:hand +description: >- + dispatch calls handle on the parameter handler. 25 classes define handle, so the site is ambiguous and gets + no name edge. H0.handle calls load on its field codec, whose value the index does not know, so that site is + a record with the method load. main passes the module yaml as the codec, so the program runs yaml.load. No + edge leads from a candidate of handle to yaml.load. The ambiguous record concerns what each candidate can + run, through its own records too, so yaml.load is uncertain and not no-such-target. os.remove, which only + the uncalled cleanup calls, is not found. +roots: + - key: main.py + kind: manifest + rule: procfile +no_edges: + - from: main.py//dispatch + to: handlers.py//H0//handle +unresolved: + - caller: handlers.py//H0//handle + site: handlers.py:9 + reason: no-definition + method: load + - caller: main.py//dispatch + site: main.py:6 + reason: ambiguous + method: handle +results: + - target: yaml//load + status: uncertain + uncertainties: + - kind: unresolved-call + site: main.py:6 + - target: os//remove + status: not-found + call_sites: + - caller: handlers.py//cleanup + site: handlers.py:133 + reached: false diff --git a/test/projectgraph/cases/python/name-ambiguous-record/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-record/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-record/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-record/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-record/project/handlers.py new file mode 100644 index 00000000..8c0ab89a --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-record/project/handlers.py @@ -0,0 +1,133 @@ +import os + + +class H0: + def __init__(self, codec): + self.codec = codec + + def handle(self, text): + return self.codec.load(text) + + +class H1: + def handle(self, text): + return text + + +class H2: + def handle(self, text): + return text + + +class H3: + def handle(self, text): + return text + + +class H4: + def handle(self, text): + return text + + +class H5: + def handle(self, text): + return text + + +class H6: + def handle(self, text): + return text + + +class H7: + def handle(self, text): + return text + + +class H8: + def handle(self, text): + return text + + +class H9: + def handle(self, text): + return text + + +class H10: + def handle(self, text): + return text + + +class H11: + def handle(self, text): + return text + + +class H12: + def handle(self, text): + return text + + +class H13: + def handle(self, text): + return text + + +class H14: + def handle(self, text): + return text + + +class H15: + def handle(self, text): + return text + + +class H16: + def handle(self, text): + return text + + +class H17: + def handle(self, text): + return text + + +class H18: + def handle(self, text): + return text + + +class H19: + def handle(self, text): + return text + + +class H20: + def handle(self, text): + return text + + +class H21: + def handle(self, text): + return text + + +class H22: + def handle(self, text): + return text + + +class H23: + def handle(self, text): + return text + + +class H24: + def handle(self, text): + return text + + +def cleanup(path): + os.remove(path) diff --git a/test/projectgraph/cases/python/name-ambiguous-record/project/main.py b/test/projectgraph/cases/python/name-ambiguous-record/project/main.py new file mode 100644 index 00000000..d306846d --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-record/project/main.py @@ -0,0 +1,9 @@ +import yaml +from handlers import H0 + + +def dispatch(handler, text): + return handler.handle(text) + + +dispatch(H0(yaml), "a: 1") From 078534fa548493c324b6c8d746b445b049bec863 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:58:54 +0000 Subject: [PATCH 29/58] refactor(indexer): look up a local of a Go function by its name (FOU-560) goLocal scanned each local of the function for each value that it read. A long generated function has thousands of locals and of values. The scopes are now held by name. No result changes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/govalues.go | 40 +++++++++++++++++--------------------- plugin/indexer/project.go | 7 ++++--- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 4571525a..e585a4c8 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -1,6 +1,8 @@ package indexer import ( + "slices" + "github.com/safedep/code/fileindex" "github.com/safedep/code/langcode" "github.com/safedep/code/pkg/ts" @@ -168,10 +170,9 @@ func (p *projection) goPredeclared(name string) bool { return langcode.IsBuiltin(langcode.Go, name) && p.defs[p.local(name)] == nil } -// goScopedName is a name that a Go function declares, with the bytes of -// the source where the name is in scope. -type goScopedName struct { - name string +// goScope holds the bytes of the source where a local name of a Go +// function is in scope. +type goScope struct { from, to uint32 } @@ -192,33 +193,28 @@ var goTypeLiterals = map[string]bool{"function_type": true, "interface_type": tr // function or a function literal in it declares. func (p *projection) goLocal(caller, name string, at uint32) bool { if p.goScoped == nil { - p.goScoped = map[string][]goScopedName{} + p.goScoped = map[string]map[string][]goScope{} } - names, ok := p.goScoped[caller] + locals, ok := p.goScoped[caller] if !ok { - names = p.goScopedNames(p.syntax[caller]) - p.goScoped[caller] = names + locals = p.goScopes(p.syntax[caller]) + p.goScoped[caller] = locals } - for _, n := range names { - if n.name == name && n.from <= at && at < n.to { - return true - } - } - return false + return slices.ContainsFunc(locals[name], func(s goScope) bool { return s.from <= at && at < s.to }) } -// goScopedNames returns the names that a function declares, each with its -// scope. The scope of a variable starts after its declaration, so x in -// x := x names the outer x. -func (p *projection) goScopedNames(function *sitter.Node) []goScopedName { +// goScopes returns the scopes of each name that a function declares. The +// scope of a variable starts after its declaration, so x in x := x names +// the outer x. +func (p *projection) goScopes(function *sitter.Node) map[string][]goScope { + locals := map[string][]goScope{} if function == nil || function.Type() != "function_declaration" && function.Type() != "method_declaration" { - return nil + return locals } - var names []goScopedName add := func(nodes []*sitter.Node, from uint32, block *sitter.Node) { for _, n := range nodes { if n.Type() == "identifier" { - names = append(names, goScopedName{name: p.text(n), from: from, to: block.EndByte()}) + locals[p.text(n)] = append(locals[p.text(n)], goScope{from: from, to: block.EndByte()}) } } } @@ -258,7 +254,7 @@ func (p *projection) goScopedNames(function *sitter.Node) []goScopedName { } } walk(function, function, 0) - return names + return locals } // goOperator returns the operator of a unary expression, as & or <-. diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index a51738d7..27c97e50 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -51,9 +51,10 @@ type projection struct { // exportStatements are the export statements and the CommonJS export // assignments of the file. exportStatements []*sitter.Node - // goScoped holds the scoped local names of each Go function that a - // value names, by the key of the function. - goScoped map[string][]goScopedName + // goScoped holds the scopes of the local names of each Go function + // whose values the index reads, by the key of the function and the + // name. + goScoped map[string]map[string][]goScope } // project builds the call graph of tree and projects it into an index. It From 757384c7831416c82a371b9b27ba067ae1af5c27 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 10:59:16 +0000 Subject: [PATCH 30/58] fix(projectgraph): give a Go method record the records of code outside the graph (A5) The record of code outside the graph sat only on an external key that a call resolves to. A Go call of a method on a value whose type the link step does not know stays a record with the method name, and such a call can run a method of a package that no unit gives. srv.ListenAndServe() on a package-level variable, servers[0].ListenAndServe(), a range variable, a parameter of an interface type, a method value, a field that holds a function, and ListenAndServe after a dot import of net/http gave example.com//dep//Sink not-found, although ServeHTTP of the handler calls it. recordGoMethodsOutside now gives the caller of each Go method record: - a callback record when no unit gives the standard library, because code of the standard library can give a value of its type to any code; - a record that concerns every target when a Go file imports a package of another module that no unit gives, in a build with a project file, because the code of that module can call any function. A record of the reason no-dependency-source names its package already and gets no other record. Both records only add uncertainty, so no answer turns into not-found. New cases go/stdlib-callback-method-record (seven forms and one source that calls nothing, which stays not-found) and go/dependency-no-unit-method. Each uncertain result is not-found on b86cb26. docs/projectgraph.md states the rule and the gap of a search from the sources alone. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 38 +++- projectgraph/golang.go | 61 +++++++ .../cases/go/dependency-callback/expect.yaml | 19 +- .../go_github.com_acme_srv_v1.0.0.json | 17 +- .../go/dependency-failed-callback/expect.yaml | 7 +- .../go_github.com_acme_srv_v1.0.0.json | 11 +- .../expect.yaml | 3 + .../go/dependency-no-unit-method/expect.yaml | 46 +++++ .../dependency-no-unit-method/project/go.mod | 5 + .../dependency-no-unit-method/project/main.go | 27 +++ .../dependency-platform-results/expect.yaml | 9 +- .../cases/go/function-value-call/expect.yaml | 3 + .../cases/go/interface-method-set/expect.yaml | 3 + .../interface-two-implementations/expect.yaml | 3 + .../stdlib-callback-method-record/expect.yaml | 170 ++++++++++++++++++ .../project/dot.go | 7 + .../project/go.mod | 3 + .../project/main.go | 57 ++++++ 18 files changed, 477 insertions(+), 12 deletions(-) create mode 100644 test/projectgraph/cases/go/dependency-no-unit-method/expect.yaml create mode 100644 test/projectgraph/cases/go/dependency-no-unit-method/project/go.mod create mode 100644 test/projectgraph/cases/go/dependency-no-unit-method/project/main.go create mode 100644 test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml create mode 100644 test/projectgraph/cases/go/stdlib-callback-method-record/project/dot.go create mode 100644 test/projectgraph/cases/go/stdlib-callback-method-record/project/go.mod create mode 100644 test/projectgraph/cases/go/stdlib-callback-method-record/project/main.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 786dee0c..f611ef78 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -724,8 +724,18 @@ these: does not always know that the receiver is a `reflect.Value`, so the rule takes any receiver. -A function value that code outside the graph makes runs code outside the -graph, which the search does not see for any call. +A function value that code outside the graph makes, as the result of +`sync.OnceFunc(g)`, runs code outside the graph. A call of that code makes +the value, and the call has the record that +[Code outside the graph](#code-outside-the-graph) gives. The search +reaches that record where it reaches the code that makes the value. + +The argument holds for a search from the roots, which reaches the module +top level of each package that the program loads. A search from the +sources alone, as `reach.SourcesOnly` asks, starts at the sources and not +at the module top levels of their packages. So a call of a function value +that only a package-level value holds can run a function that such a +search does not reach, and the target 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 @@ -801,6 +811,27 @@ 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 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 +parameter of an interface type, or after a dot import of `net/http`. Such a +call can run a method of a type of a package that no unit gives, and no +edge leads to that code. So the link step gives the caller of each such +record the record of that code too. + +- When no unit gives the standard library, the caller gets a callback + record. Code of the standard library can give a value of its type to any + code, as an `error`, so the rule does not ask for an import. +- When a Go file imports a package of another module that no unit gives, + in a build with a project file, the caller gets a record that concerns + every target. Only code that imports the module, or code of the module, + makes a value of its type, so such an import names the module. +- A record of the reason `no-dependency-source` names its package, and + that package has its own record. It gets no other record. + +The cases `go/stdlib-callback-method-record` and +`go/dependency-no-unit-method` show it. + A target that no Go method of an exported name reaches keeps its answer. In the case `go/stdlib-callback-scope`, only the function `Dial` calls `handshake`, so `handshake` stays `not-found`. A search that reaches no @@ -817,4 +848,5 @@ Two gaps stay. `no-such-target`. - The rule applies to Go only. Code of another language that no unit gives, as the standard library of Python with no `python:std` unit, can call a - method of a value too, and no record shows it. + method of a value too, and a call of a method on a value of an unknown + type can run such code. No record shows it. diff --git a/projectgraph/golang.go b/projectgraph/golang.go index ef637256..3fe358af 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -410,6 +410,7 @@ func (b *builder) recordGoOutsideCode() { } } } + b.recordGoMethodsOutside() } // goOutsideRecords returns the records of an external key whose package no @@ -441,3 +442,63 @@ func (b *builder) goOutsideRecords(key string) []UnresolvedSite { } return []UnresolvedSite{{Caller: key, Text: key, Reason: fileindex.UnresolvedNoDependencySource}} } + +// 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, +// and no edge leads to that code. A method of the standard library can +// call back a method of an exported name, as ListenAndServe of +// http.Server calls ServeHTTP of its handler. A method of another module +// can call any function. A call that names its package already has its +// record on the external key. +func (b *builder) recordGoMethodsOutside() { + std, other := b.goOutsidePackages() + if !std && !other { + return + } + var add []UnresolvedSite + for u := range b.unresolved { + if u.Method == "" || ReachesAny(u.Reason) || u.Reason == fileindex.UnresolvedNoDependencySource || b.kindOf(u.Caller) == NodeExternal || b.languageOf(u.Caller) != langcode.Go { + continue + } + if std { + add = append(add, UnresolvedSite{Caller: u.Caller, Text: goStdText, Reason: fileindex.UnresolvedNoDependencySource, Callback: true}) + } + if other { + add = append(add, UnresolvedSite{Caller: u.Caller, Site: u.Site, Text: u.Text, Reason: fileindex.UnresolvedNoDependencySource}) + } + } + for _, r := range add { + b.addUnresolved(r) + } +} + +// goOutsidePackages reports whether Go code of the build can hold a value +// of a type of the standard library that no unit gives, and of a type of +// another module that no unit gives. Code of the standard library can give +// a value of its type to any code, as an error. Only code that imports a +// package of another module, or code of the module, makes a value of its +// type, and an import of the module names it. A summary does not count +// another module, as goOutsideRecords does not. +func (b *builder) goOutsidePackages() (std, other bool) { + std = !slices.ContainsFunc(b.ordered, func(sp *space) bool { return sp.pkg.Ecosystem == "go" && sp.pkg.Name == modules.StdModule }) + if !b.hasProject { + return std, false + } + for _, p := range b.paths { + ix := b.byPath[p] + if ix == nil || ix.Language != langcode.Go { + continue + } + for _, imp := range ix.Imports { + if imp.Module == "C" || modules.IsStdPath(imp.Module) { + continue + } + if mods, missing := b.importedModules(ix, imp.Module, formOf(imp)); len(mods) > 0 || len(missing) > 0 { + continue + } + return std, true + } + } + return std, false +} diff --git a/test/projectgraph/cases/go/dependency-callback/expect.yaml b/test/projectgraph/cases/go/dependency-callback/expect.yaml index c73e2ceb..aabf6d86 100644 --- a/test/projectgraph/cases/go/dependency-callback/expect.yaml +++ b/test/projectgraph/cases/go/dependency-callback/expect.yaml @@ -8,9 +8,10 @@ description: >- target. The call h.Handle() in Serve has no implementation in the graph, so it is an interface-receiver record with the method Handle, and os/exec.LookPath, which mine.Handle calls, is uncertain. The link step cannot name the method of c.Close, so the reference is a record with the method Close, which gets a name - edge to conn.Close. So os.Chdir is found. No target that a callback reaches is not found. conn.Close, a - candidate of c.Close, calls os.Chdir, whose callback reaches mine.Handle. So the record c.Close also - concerns os/exec.LookPath. + edge to conn.Close. So os.Chdir is found. No target that a callback reaches is not found. No unit gives + the standard library, and a call of a method on a value of an unknown type can run a method of it, so the + caller of each method record also has a callback record. conn.Close, a candidate of c.Close, calls + os.Chdir, whose callback reaches mine.Handle. So the record c.Close also concerns os/exec.LookPath. units: gomod/github.com/acme/srv@v1.0.0: go:github.com/acme/srv@v1.0.0 roots: @@ -24,6 +25,18 @@ no_edges: - from: main.go//main to: main.go//mine//Handle unresolved: + - caller: go:github.com/acme/srv@v1.0.0//srv.go//Bind + reason: no-dependency-source + callback: true + - caller: go:github.com/acme/srv@v1.0.0//srv.go//Run + reason: no-dependency-source + callback: true + - caller: go:github.com/acme/srv@v1.0.0//srv.go//Serve + reason: no-dependency-source + callback: true + - caller: main.go//main + reason: no-dependency-source + callback: true - caller: os//Chdir reason: no-dependency-source callback: true 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 603d9c9c..7491edeb 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 @@ -11,7 +11,8 @@ ], "reaches": [], "uncertainties": [ - 0 + 0, + 1 ] }, { @@ -42,7 +43,8 @@ ], "reaches": [], "uncertainties": [ - 1 + 0, + 2 ] }, { @@ -52,12 +54,21 @@ ], "reaches": [], "uncertainties": [ - 2 + 0, + 3 ] } ], "targets": [], "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/srv@v1.0.0//srv.go//Bind", diff --git a/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml b/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml index 105923c4..03240056 100644 --- a/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml +++ b/test/projectgraph/cases/go/dependency-failed-callback/expect.yaml @@ -5,7 +5,8 @@ description: >- mine.Handle calls os/exec.LookPath, which no code of srv reaches by an edge. A failed file of a unit can call a method of a value that the project gives to the unit, so its budget concerns every target where the search enters the unit, and os/exec.LookPath is uncertain. A scope by the code that the unit reaches - would make it not found. + would make it not found. The call of serve can also run a method of the standard library, which no unit + gives, so Serve has a callback record, which concerns os/exec.LookPath too. units: gomod/github.com/acme/srv@v1.0.0: go:github.com/acme/srv@v1.0.0 max_file_bytes: 400 @@ -19,6 +20,9 @@ roots: kind: main rule: go.main unresolved: + - caller: go:github.com/acme/srv@v1.0.0//srv.go//Serve + reason: no-dependency-source + callback: true - caller: os//exec//LookPath reason: no-dependency-source callback: true @@ -36,3 +40,4 @@ results: uncertainties: - kind: budget site: go:github.com/acme/srv@v1.0.0//big.go + - kind: unresolved-call 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 cf32a69b..45cfa721 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 @@ -22,7 +22,8 @@ "reaches": [], "uncertainties": [ 0, - 1 + 1, + 2 ] } ], @@ -36,6 +37,14 @@ }, "text": "the file is failed" }, + { + "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/srv@v1.0.0//srv.go//Serve", diff --git a/test/projectgraph/cases/go/dependency-implements-interface/expect.yaml b/test/projectgraph/cases/go/dependency-implements-interface/expect.yaml index ce894757..1cf757c3 100644 --- a/test/projectgraph/cases/go/dependency-implements-interface/expect.yaml +++ b/test/projectgraph/cases/go/dependency-implements-interface/expect.yaml @@ -43,6 +43,9 @@ edges: confidence: high site: go:github.com/acme/dep@v1.0.0//dep.go:12 unresolved: + - caller: main.go//main + reason: no-dependency-source + callback: true - caller: os//exec//Command reason: no-dependency-source callback: true diff --git a/test/projectgraph/cases/go/dependency-no-unit-method/expect.yaml b/test/projectgraph/cases/go/dependency-no-unit-method/expect.yaml new file mode 100644 index 00000000..daca758a --- /dev/null +++ b/test/projectgraph/cases/go/dependency-no-unit-method/expect.yaml @@ -0,0 +1,46 @@ +source: safedep:hand +description: >- + No unit gives example.com/legacy, and main makes a value of its type Conn only in a composite literal, so no + edge leads to legacy. run calls Close on an element of a slice of a first-party interface, whose value the + link step does not know. That call can run Close of legacy.Conn, and the code of legacy can call any + function. So the call has a record of code outside the graph that concerns every target, and os.RemoveAll, + which only the uncalled purge calls, is uncertain. idle calls nothing, so os.RemoveAll is not found from it. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: main.go//run + reason: no-dependency-source + callback: true + - caller: os//RemoveAll + reason: no-dependency-source + callback: true + - caller: main.go//run + site: main.go:16 + reason: no-dependency-source + - caller: main.go//run + site: main.go:16 + reason: unknown-receiver + method: Close +results: + - target: os//RemoveAll + status: uncertain + call_sites: + - caller: main.go//purge + site: main.go:22 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:16 + - target: os//RemoveAll + sources: [main.go//idle] + sources_only: true + status: not-found + call_sites: + - caller: main.go//purge + site: main.go:22 + reached: false diff --git a/test/projectgraph/cases/go/dependency-no-unit-method/project/go.mod b/test/projectgraph/cases/go/dependency-no-unit-method/project/go.mod new file mode 100644 index 00000000..351653a0 --- /dev/null +++ b/test/projectgraph/cases/go/dependency-no-unit-method/project/go.mod @@ -0,0 +1,5 @@ +module app + +go 1.22 + +require example.com/legacy v1.0.0 diff --git a/test/projectgraph/cases/go/dependency-no-unit-method/project/main.go b/test/projectgraph/cases/go/dependency-no-unit-method/project/main.go new file mode 100644 index 00000000..824e0d8c --- /dev/null +++ b/test/projectgraph/cases/go/dependency-no-unit-method/project/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "os" + + "example.com/legacy" +) + +type closer interface { + Close() error +} + +var conns = []closer{&legacy.Conn{}} + +func run() { + conns[0].Close() +} + +func idle() {} + +func purge() { + os.RemoveAll("data") +} + +func main() { + run() +} diff --git a/test/projectgraph/cases/go/dependency-platform-results/expect.yaml b/test/projectgraph/cases/go/dependency-platform-results/expect.yaml index 7efe2e58..fe9b4633 100644 --- a/test/projectgraph/cases/go/dependency-platform-results/expect.yaml +++ b/test/projectgraph/cases/go/dependency-platform-results/expect.yaml @@ -2,7 +2,9 @@ source: safedep:hand description: >- new_linux.go and new_windows.go each define New, and they return the types Linux and Windows. Both types have the method Common. The walk follows one result type, so the call New().Common() is a record with the reason - unknown-receiver. Before, the walk took the type of the first file, and Windows//Common was not-found. + unknown-receiver. Before, the walk took the type of the first file, and Windows//Common was not-found. No + unit gives the standard library, so the record also has a callback record, and the callback concerns each + method Common, because its name is exported. units: gomod/github.com/acme/kit@v1.0.0: go:github.com/acme/kit@v1.0.0 roots: @@ -13,6 +15,9 @@ roots: kind: main rule: go.main unresolved: + - caller: main.go//main + reason: no-dependency-source + callback: true - caller: github.com//acme//kit//New//Common site: go:github.com/acme/kit@v1.0.0 reason: unknown-receiver @@ -28,6 +33,7 @@ results: site: main.go:6 - kind: unresolved-call site: go:github.com/acme/kit@v1.0.0 + - kind: unresolved-call - target: github.com//acme//kit//Linux//Common status: uncertain uncertainties: @@ -35,3 +41,4 @@ results: site: main.go:6 - kind: unresolved-call site: go:github.com/acme/kit@v1.0.0 + - kind: unresolved-call diff --git a/test/projectgraph/cases/go/function-value-call/expect.yaml b/test/projectgraph/cases/go/function-value-call/expect.yaml index 16200629..1caa754b 100644 --- a/test/projectgraph/cases/go/function-value-call/expect.yaml +++ b/test/projectgraph/cases/go/function-value-call/expect.yaml @@ -21,6 +21,9 @@ no_edges: - from: main.go//main to: os//Remove unresolved: + - caller: main.go//main + reason: no-dependency-source + callback: true - caller: os//Getenv reason: no-dependency-source callback: true diff --git a/test/projectgraph/cases/go/interface-method-set/expect.yaml b/test/projectgraph/cases/go/interface-method-set/expect.yaml index e4407d6b..234d08ea 100644 --- a/test/projectgraph/cases/go/interface-method-set/expect.yaml +++ b/test/projectgraph/cases/go/interface-method-set/expect.yaml @@ -17,6 +17,9 @@ no_edges: - from: cmd/app/main.go//start to: worker/script.go//Script//Run unresolved: + - caller: cmd/app/main.go//start + reason: no-dependency-source + callback: true - caller: net//http//Get reason: no-dependency-source callback: true diff --git a/test/projectgraph/cases/go/interface-two-implementations/expect.yaml b/test/projectgraph/cases/go/interface-two-implementations/expect.yaml index e8db580f..bfde0c43 100644 --- a/test/projectgraph/cases/go/interface-two-implementations/expect.yaml +++ b/test/projectgraph/cases/go/interface-two-implementations/expect.yaml @@ -22,6 +22,9 @@ no_edges: - from: cmd/app/main.go//run to: sink/db.go//Log//Write unresolved: + - caller: cmd/app/main.go//run + reason: no-dependency-source + callback: true - caller: database//sql//DB//Exec reason: no-dependency-source callback: true diff --git a/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml b/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml new file mode 100644 index 00000000..3da14554 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-method-record/expect.yaml @@ -0,0 +1,170 @@ +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. idle calls nothing, so os.Remove is not found from it. +roots: + - key: dot.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: dot.go//viaDotImport + reason: no-dependency-source + callback: true + - caller: main.go//viaField + reason: no-dependency-source + callback: true + - caller: main.go//viaIndex + reason: no-dependency-source + callback: true + - caller: main.go//viaInterface + reason: no-dependency-source + callback: true + - 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: os//Remove + reason: no-dependency-source + callback: true + - caller: dot.go//viaDotImport + site: dot.go:6 + reason: unknown-receiver + method: ListenAndServe + - caller: main.go//viaVariable + site: main.go:29 + reason: unknown-receiver + method: ListenAndServe + - caller: main.go//viaIndex + 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 + method: ListenAndServe + - caller: main.go//viaField + site: main.go:47 + reason: unknown-receiver + method: serve + - caller: main.go//viaMethodValue + site: main.go:51 + reason: unknown-receiver + method: ListenAndServe + - caller: main.go//viaMethodValue + site: main.go:52 + reason: no-definition + method: serve +results: + - target: os//Remove + sources: [main.go//viaVariable] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//handler//ServeHTTP + site: main.go:11 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:29 + - kind: unresolved-call + - target: os//Remove + sources: [main.go//viaIndex] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//handler//ServeHTTP + site: main.go:11 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:33 + - kind: unresolved-call + - target: os//Remove + sources: [main.go//viaRange] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//handler//ServeHTTP + site: main.go:11 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:38 + - kind: unresolved-call + - target: os//Remove + sources: [main.go//viaInterface] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//handler//ServeHTTP + site: main.go:11 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:43 + - kind: unresolved-call + - target: os//Remove + sources: [main.go//viaField] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//handler//ServeHTTP + site: main.go:11 + reached: false + uncertainties: + - kind: unresolved-call + - target: os//Remove + sources: [main.go//viaMethodValue] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//handler//ServeHTTP + site: main.go:11 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:51 + - kind: unresolved-call + - target: os//Remove + sources: [dot.go//viaDotImport] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//handler//ServeHTTP + site: main.go:11 + reached: false + uncertainties: + - kind: unresolved-call + site: dot.go:6 + - kind: unresolved-call + - target: os//Remove + sources: [main.go//idle] + sources_only: true + status: not-found + call_sites: + - caller: main.go//handler//ServeHTTP + site: main.go:11 + reached: false diff --git a/test/projectgraph/cases/go/stdlib-callback-method-record/project/dot.go b/test/projectgraph/cases/go/stdlib-callback-method-record/project/dot.go new file mode 100644 index 00000000..7b76b721 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-method-record/project/dot.go @@ -0,0 +1,7 @@ +package main + +import . "net/http" + +func viaDotImport() { + ListenAndServe(":80", handler{}) +} diff --git a/test/projectgraph/cases/go/stdlib-callback-method-record/project/go.mod b/test/projectgraph/cases/go/stdlib-callback-method-record/project/go.mod new file mode 100644 index 00000000..3955fcdc --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-method-record/project/go.mod @@ -0,0 +1,3 @@ +module app + +go 1.22 diff --git a/test/projectgraph/cases/go/stdlib-callback-method-record/project/main.go b/test/projectgraph/cases/go/stdlib-callback-method-record/project/main.go new file mode 100644 index 00000000..084793d0 --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-method-record/project/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "net/http" + "os" +) + +type handler struct{} + +func (handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + os.Remove("served") +} + +type lister interface { + ListenAndServe() error +} + +type app struct { + serve func(string, http.Handler) error +} + +var srv = &http.Server{Handler: handler{}} + +var servers = []*http.Server{{Handler: handler{}}} + +var a = app{serve: http.ListenAndServe} + +func viaVariable() { + srv.ListenAndServe() +} + +func viaIndex() { + servers[0].ListenAndServe() +} + +func viaRange(m map[string]*http.Server) { + for _, s := range m { + s.ListenAndServe() + } +} + +func viaInterface(l lister) { + l.ListenAndServe() +} + +func viaField() { + a.serve(":80", handler{}) +} + +func viaMethodValue() { + serve := srv.ListenAndServe + serve() +} + +func idle() {} + +func main() {} From 61fc34e6dcd3391cf4b65c2508159c477d101ca2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 11:00:38 +0000 Subject: [PATCH 31/58] fix(indexer): check every Rust binder of a name before a typed call (FOU-562) The call graph of Rust reads a parameter and a let of one name, and no other binder. So it bound t.inspect() to the parameter t: Local in fn run(t: Local) { if let Some(t) = dep::try_thing() { t.inspect() } }, and the link step read the declared type of the parameter. The call got an edge to Local::inspect and no record, and dep::Thing::inspect read as not-found. The same came from a match arm, a closure parameter, a for, a let of a macro, a local static or const, and an if let after a let. The single-binder check of rustDeclaredAt guarded only a receiver that the call graph does not type, though the doc claimed more. The index now checks each call in a language with declaredAt. A receiver that a binder binds before the call, which the call graph does not read, makes the call a record. A target through a declared type of a name, as run//t//inspect, needs the single-binder check. A static, a const and the tokens of a macro other than a macro of std now count as binders. One cached walk per function gives the binders of each name. It serves the declared type check, the rebound check and the marks of a ?. So rustLets and rustFunction are gone. rustLets walked from the file root, so the depth cap of walkBody counted from there. The walk uses the context of the projection, and a walk that stops records a truncation and makes the binders of the function unknown, so every call on a name there is a record. The hooks typedResult and retarget are now declaredAt and markTries, and typedTarget takes the call. rustTries checks a nil node, because a ? in an error tree can have no operand. rustLastName cuts the last name of a path. 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/calls.go | 131 +++++++++----- plugin/indexer/languages.go | 30 ++-- plugin/indexer/project.go | 8 +- plugin/indexer/rust.go | 262 +++++++++++++++++----------- test/projectgraph/receivers_test.go | 48 +++++ 6 files changed, 341 insertions(+), 158 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 9c61d13a..f1be5078 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -271,11 +271,21 @@ Rust follows the same rules, with these additions. the local, as `let w: Box = Box::new(Html {})`, binds the receiver. A declared type binds the receiver only where the index of the file knows no type of the value. So `let s: String = String::new()` keeps - the target `String//shout` of `s.shout()`. The function must also bind - the name once, as a parameter or a typed `let`, before the call. A `let`, - a `match` arm, an `if let`, a `for` or a closure can bind the name again - with another type. A call on such a name keeps its record, so a method of - the real type does not read as `not-found`. + the target `String//shout` of `s.shout()`. +- A declared type holds at a call only when the function binds the name + once, as a parameter or a typed `let`, before the call. The index checks + this for a receiver that the call graph does not type and for a target + that goes through a declared type, as `run//t//inspect` for `t.inspect()` + in `fn run(t: Local)`. The call graph reads a parameter and a `let` of + one name, and no other binder. A `match` arm, an `if let`, a `for`, a + closure, another pattern, a `static`, a `const` or a macro can bind the + 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 + 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`. - `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/calls.go b/plugin/indexer/calls.go index 7638e3e0..87eeb05c 100644 --- a/plugin/indexer/calls.go +++ b/plugin/indexer/calls.go @@ -1,6 +1,7 @@ package indexer import ( + "context" "regexp" "slices" "strings" @@ -13,29 +14,68 @@ import ( // retargetTyped gives a call on a receiver that a declared type of the // scope names the key of the name in its scope as its target, in a language -// with typedReceivers, or with typedResult at an untyped site. The call +// with typedReceivers, or with declaredAt at an untyped site. The call // graph bound the receiver to the value of an assignment, as dep//new_thing // for t = dep.new_thing(), and a call on that value, as dep//new_thing//do, // names no method. The declared type or the result type of the name does. // A call that the call graph bound to a method of a class of the file, as // Parser//render for p = Parser(), keeps that target. -func (p *projection) retargetTyped(call *fileindex.Call, untyped bool) { +func (p *projection) retargetTyped(ctx context.Context, call *fileindex.Call, untyped bool) { for _, t := range call.Targets { if def, ok := p.defs[fileindex.Parent(t)]; ok && def.Kind == fileindex.DefClass { return } } - if target, ok := p.typedTarget(call.Caller, call.Receiver, call.Method, call.Line, call.Column, untyped); ok { + if target, ok := p.typedTarget(ctx, *call, untyped); ok { call.Targets = []string{target} } } -func (p *projection) addCalls() { - untyped := p.untypedSites() - var retarget func(call *fileindex.Call, callee ast.NodePosition) - if r := p.rules().retarget; r != nil { - retarget = r(p) +// 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 +// 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() } }. +// The target then names a method of the wrong type, and a method of the +// real type reads as not-found. So the receiver must have no binder that +// the call graph does not read, and a target through a declared type of a +// 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.declaredAt == nil { + return true + } + if name, _, _ := strings.Cut(call.Receiver, "."); isIdentifier(name) && rules.rebound(ctx, p, call, name) { + return false + } + for _, target := range call.Targets { + if name, ok := p.typedName(call.Caller, target); ok && !rules.declaredAt(ctx, p, call, name) { + return false + } + } + return true +} + +// typedName returns the name of a parameter or a typed local of the scope +// chain of the caller whose key starts a target, as t for run//t//inspect. +func (p *projection) typedName(caller, target string) (string, bool) { + for scope := caller; fileindex.HasKeyPrefix(scope, p.cg.FileName); scope = fileindex.Parent(scope) { + if rest, ok := strings.CutPrefix(target, scope+fileindex.Separator); ok { + name, _, _ := strings.Cut(rest, fileindex.Separator) + if typed, ok := p.typedScope(caller, name); ok && typed == scope { + return name, true + } + } + if scope == p.cg.FileName { + break + } } + return "", false +} + +func (p *projection) addCalls(ctx context.Context) { + untyped := p.untypedSites() + markTries := p.rules().markTries for key, node := range p.cg.Nodes { for _, ref := range node.CallsTo { kind, ok := callKind(ref.Kind) @@ -68,9 +108,13 @@ func (p *projection) addCalls() { Kind: kind, Args: p.arguments(key, ref.Site), } - p.retargetTyped(&call, untyped[[2]uint32{ref.Site.StartLine, ref.Site.StartColumn}]) - if retarget != nil { - retarget(&call, ref.Site) + p.retargetTyped(ctx, &call, untyped[[2]uint32{ref.Site.StartLine, ref.Site.StartColumn}]) + if !p.bindsAsWritten(ctx, call) { + p.addRecord(call, fileindex.UnresolvedUnknownReceiver) + continue + } + if markTries != nil { + markTries(ctx, p, &call, ref.Site) } p.ix.Calls = append(p.ix.Calls, call) } @@ -79,9 +123,9 @@ func (p *projection) addCalls() { // untypedSites returns the line and the column of each call on a receiver // whose value the call graph does not type, as the value of a call, in a -// language with typedResult. +// language with declaredAt. func (p *projection) untypedSites() map[[2]uint32]bool { - if p.rules().typedResult == nil { + if p.rules().declaredAt == nil { return nil } sites := map[[2]uint32]bool{} @@ -250,63 +294,68 @@ func (p *projection) rebind(caller, name string) []string { } } -func (p *projection) addUnresolved() { +func (p *projection) addUnresolved(ctx context.Context) { for _, u := range p.cg.Unresolved() { if p.members[[2]uint32{u.Site.StartLine, u.Site.StartColumn}] && u.Reason == callgraph.UnresolvedUnknownReceiver { continue } - caller := p.caller(u.Caller, u.Site) - untyped := u.Reason == callgraph.UnresolvedUnknownReceiver - if target, ok := p.typedTarget(caller, u.Receiver, u.Method, int(u.Site.StartLine), int(u.Site.StartColumn), untyped); ok && untyped { - p.ix.Calls = append(p.ix.Calls, fileindex.Call{ - Caller: caller, - Line: int(u.Site.StartLine), - Column: int(u.Site.StartColumn), - Callee: u.Text, - Targets: []string{target}, - Receiver: u.Receiver, - Method: u.Method, - Kind: fileindex.CallCall, - }) - continue - } - p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ - Caller: caller, + call := fileindex.Call{ + Caller: p.caller(u.Caller, u.Site), Line: int(u.Site.StartLine), Column: int(u.Site.StartColumn), - Text: u.Text, + Callee: u.Text, Receiver: u.Receiver, Method: u.Method, - Reason: fileindex.UnresolvedReason(u.Reason), - }) + Kind: fileindex.CallCall, + } + untyped := u.Reason == callgraph.UnresolvedUnknownReceiver + if target, ok := p.typedTarget(ctx, call, untyped); ok && untyped { + call.Targets = []string{target} + p.ix.Calls = append(p.ix.Calls, call) + continue + } + p.addRecord(call, fileindex.UnresolvedReason(u.Reason)) } } +// addRecord records a call site whose target the index does not know. +func (p *projection) addRecord(call fileindex.Call, reason fileindex.UnresolvedReason) { + p.ix.Unresolved = append(p.ix.Unresolved, fileindex.Unresolved{ + Caller: call.Caller, + Line: call.Line, + Column: call.Column, + Text: call.Callee, + Receiver: call.Receiver, + Method: call.Method, + Reason: reason, + }) +} + // typedTarget returns the target of a call on a receiver whose first name a // declared type of the scope chain of the caller types, as main.py//run//t//do // for t.do() in run(t: Thing), in a language with typedReceivers, or with -// typedResult at an untyped site. The link step reads the type of the name +// declaredAt at an untyped site. The link step reads the type of the name // from the scope, as it does for Go. -func (p *projection) typedTarget(caller, receiver, method string, line, column int, untyped bool) (string, bool) { - if receiver == "" || method == "" { +func (p *projection) typedTarget(ctx context.Context, call fileindex.Call, untyped bool) (string, bool) { + if call.Receiver == "" || call.Method == "" { return "", false } - names := strings.Split(receiver, ".") + names := strings.Split(call.Receiver, ".") for _, name := range names { if !isIdentifier(name) { return "", false } } rules := p.rules() - typed := rules.typedReceivers || untyped && rules.typedResult != nil && rules.typedResult(p, caller, names[0], line, column) + typed := rules.typedReceivers || untyped && rules.declaredAt != nil && rules.declaredAt(ctx, p, call, names[0]) if !typed { return "", false } - scope, ok := p.typedScope(caller, names[0]) + scope, ok := p.typedScope(call.Caller, names[0]) if !ok { return "", false } - return fileindex.Join(scope, append(names, method)...), true + return fileindex.Join(scope, append(names, call.Method)...), true } func (p *projection) addTruncations() { diff --git a/plugin/indexer/languages.go b/plugin/indexer/languages.go index 84549497..3325e8e4 100644 --- a/plugin/indexer/languages.go +++ b/plugin/indexer/languages.go @@ -1,6 +1,8 @@ package indexer import ( + "context" + "github.com/safedep/code/core" "github.com/safedep/code/core/ast" "github.com/safedep/code/fileindex" @@ -30,13 +32,19 @@ type languageRules struct { // on such a receiver gets the key of the name in its scope as its // target, as main.py//run//t//do, so the link step reads the type. typedReceivers bool - // typedResult reports whether the declared type of a name types a call - // on it at a line and a column where the call graph knows no type of - // its value, as Rust for w.write() after - // let w: Box = Box::new(Html {}). A value that the call graph - // makes, as String::new(), is an instance of that type alone, so a call - // on it keeps its target. - typedResult func(p *projection, caller, name string, line, column int) bool + // declaredAt reports whether the declared type of a name holds at a + // call: the caller binds the name once, as a parameter or a typed + // local, before the call. Rust reads it for w.write() after + // let w: Box = Box::new(Html {}), where the call graph knows + // no type of the value, and for a call that the call graph binds to a + // parameter. A value that the call graph makes, as String::new(), is an + // instance of that type alone, so a call on it keeps its target. + declaredAt func(ctx context.Context, p *projection, call fileindex.Call, name string) bool + // rebound reports whether the caller binds a name before a call by a + // syntax that the call graph does not read, as Some(t) in + // 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 // packageOf returns the package or the namespace of the file, which the // files of one unit share. @@ -78,10 +86,10 @@ type languageRules struct { // prune drops the definitions that hold no record, after the records. prune func(p *projection) - // retarget returns the function that rewrites 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. - retarget func(p *projection) func(call *fileindex.Call, callee ast.NodePosition) + // 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. + markTries func(ctx context.Context, p *projection, call *fileindex.Call, callee ast.NodePosition) // visitors read the walk of the tree, as for the references and the // flags of the language, in this order. diff --git a/plugin/indexer/project.go b/plugin/indexer/project.go index 57f89a03..e761fc75 100644 --- a/plugin/indexer/project.go +++ b/plugin/indexer/project.go @@ -47,6 +47,10 @@ type projection struct { // members holds the line and the column of each call that names a // member of an object of the file. members map[[2]uint32]bool + // binders holds the nodes that bind each name of a function, by the + // start of the function, for a language with declaredAt. A nil map is + // a function whose walk stopped early. + binders map[uint32]map[string][]*sitter.Node // exportStatements are the export statements and the CommonJS export // assignments of the file. @@ -103,8 +107,8 @@ func project(ctx context.Context, tree core.ParseTree) (*fileindex.Index, *callg p.run(rules.inits) p.walkTree(ctx) p.run(rules.afterWalk) - p.addCalls() - p.addUnresolved() + p.addCalls(ctx) + p.addUnresolved(ctx) p.run(rules.prune) if err := p.addClasses(ctx); err != nil { return nil, nil, err diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 08af22f6..5b1811a3 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -26,9 +26,10 @@ var rustRules = languageRules{ signature: rustSignature, exported: rustPublic, definitions: rustTypes, - retarget: rustRetarget, + markTries: rustMarkTries, visitors: []func(p *projection) visitor{referenceVisitor, rustVisitor}, - typedResult: rustDeclaredAt, + 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. @@ -215,10 +216,7 @@ var rustWrappers = []string{"Option", "Result"} // Result that holds it, as Thing and Option for Option>. func (p *projection) rustResult(node *sitter.Node, bounds map[string]string) (string, string) { if node != nil && node.Type() == "generic_type" { - name := p.rustTypeName(node.ChildByFieldName("type"), bounds) - if i := strings.LastIndex(name, "::"); i >= 0 { - name = name[i+len("::"):] - } + name := rustLastName(p.rustTypeName(node.ChildByFieldName("type"), bounds)) if args := ts.NamedChildren(node.ChildByFieldName("type_arguments")); slices.Contains(rustWrappers, name) && len(args) > 0 { return p.rustTypeName(args[0], bounds), name } @@ -331,44 +329,107 @@ func (p *projection) rustLocals(body *sitter.Node, bounds map[string]string) []f return locals } -// rustDeclaredAt reports whether the declared type of a name of a function -// holds at a line and a column of the function: the function binds the name -// once, as a parameter or a let with a type, before that place. A let, a -// match arm, an if let, a for or a closure can bind the name again with -// another type. Without the check, a call on such a name took the method of -// the declared type and lost its record, so a method of the real type read -// as not-found. -func rustDeclaredAt(p *projection, caller, name string, line, column int) bool { +// rustLastName returns the last name of a path, as Result for +// std::io::Result. +func rustLastName(path string) string { + if i := strings.LastIndex(path, "::"); i >= 0 { + return path[i+len("::"):] + } + return path +} + +// rustDeclaredAt reports whether the declared type of a name holds at a +// call: the function binds the name once, as a parameter or a let with a +// type, before the call. A let, a match arm, an if let, a for, a closure, a +// static, a const or a macro can bind the name again with another type. +func rustDeclaredAt(ctx context.Context, p *projection, call fileindex.Call, name string) bool { + binders, _ := p.rustBinders(ctx, call.Caller) + if len(binders[name]) != 1 { + return false + } + binder := binders[name][0] + if !startsBefore(binder, call) || !rustPlainBinder(binder) { + return false + } + return binder.Type() == "parameter" || binder.ChildByFieldName("type") != nil +} + +// rustRebound reports whether the function binds a name before a call by a +// binder that the call graph does not read. 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 + } + return slices.ContainsFunc(binders[name], func(binder *sitter.Node) bool { + return startsBefore(binder, call) && !rustPlainBinder(binder) + }) +} + +// startsBefore reports whether a node starts before the site of a call. +func startsBefore(node *sitter.Node, call fileindex.Call) bool { + at := node.StartPoint() + line, column := int(at.Row)+1, int(at.Column)+1 + return line < call.Line || line == call.Line && column < call.Column +} + +// rustPlainBinder reports whether the call graph reads a binder: a +// parameter of the function or a let, that binds one name. +func rustPlainBinder(binder *sitter.Node) bool { + switch binder.Type() { + case "parameter": + if parent := binder.Parent(); parent == nil || parent.Type() != "parameters" { + return false + } + case "let_declaration": + default: + return false + } + return rustBoundName(binder.ChildByFieldName("pattern")) != nil +} + +// rustBinders returns the binders of each name of the function of a caller, +// in source order. A nested function binds its own names. It returns false +// when the walk of the function stops early, so its binders are not known. +// A caller that is no function binds no name. +func (p *projection) rustBinders(ctx context.Context, caller string) (map[string][]*sitter.Node, bool) { fn := p.syntax[caller] if fn == nil || fn.Type() != "function_item" { - return false + return nil, true + } + if binders, ok := p.binders[fn.StartByte()]; ok { + return binders, binders != nil } - var binders []*sitter.Node - err := ts.Walk(context.Background(), fn, ts.WalkOptions{}, func(node *sitter.Node) bool { + binders := map[string][]*sitter.Node{} + err := ts.Walk(ctx, fn, ts.WalkOptions{}, func(node *sitter.Node) bool { if node.Type() == "function_item" && !ts.SameNode(node, fn) { return false } - if (node.Type() == "identifier" || node.Type() == "shorthand_field_identifier") && p.text(node) == name { - if binder := rustBinder(node); binder != nil { - binders = append(binders, binder) - } + if binder := p.rustBinder(node); binder != nil { + binders[p.text(node)] = append(binders[p.text(node)], binder) } return true }) - if err != nil || len(binders) != 1 { - return false + if err != nil { + p.truncateWalk(err) + binders = nil } - binder := binders[0] - if at := binder.StartPoint(); int(at.Row)+1 > line || int(at.Row)+1 == line && int(at.Column)+1 >= column { - return false + if p.binders == nil { + p.binders = map[uint32]map[string][]*sitter.Node{} } - return binder.Type() == "parameter" || binder.Type() == "let_declaration" && binder.ChildByFieldName("type") != nil + p.binders[fn.StartByte()] = binders + return binders, binders != nil } -// rustBinder returns the node that binds an identifier of a pattern, as the -// let, the parameter or the match pattern, or nil for an identifier that is -// no binding. -func rustBinder(id *sitter.Node) *sitter.Node { +// rustBinder returns the node that binds an identifier: the let, the +// parameter, the match pattern or another pattern, the static or the +// const, or the call of a macro whose tokens hold it. It returns nil for an +// identifier that binds nothing and for any other node. +func (p *projection) rustBinder(id *sitter.Node) *sitter.Node { + if id.Type() != "identifier" && id.Type() != "shorthand_field_identifier" { + return nil + } child, parent := id, id.Parent() for parent != nil && strings.HasSuffix(parent.Type(), "_pattern") && parent.Type() != "match_pattern" { if typ := parent.ChildByFieldName("type"); typ != nil && ts.SameNode(typ, child) { @@ -386,10 +447,33 @@ func rustBinder(id *sitter.Node) *sitter.Node { if pattern := parent.ChildByFieldName("pattern"); pattern != nil && ts.SameNode(pattern, child) { return parent } + case "static_item", "const_item": + if name := parent.ChildByFieldName("name"); name != nil && ts.SameNode(name, child) { + return parent + } + case "token_tree": + return p.rustBindingMacro(parent) } return nil } +// 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. +func (p *projection) rustBindingMacro(tree *sitter.Node) *sitter.Node { + for tree != nil && tree.Type() == "token_tree" { + tree = tree.Parent() + } + 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" { + return nil + } + return tree +} + // 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 { @@ -402,58 +486,65 @@ func rustBoundName(pattern *sitter.Node) *sitter.Node { return pattern } -// rustRetarget marks each ? in the receiver of a method call in the +// 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 // without the mark the link step cannot tell a method of the value inside // an Option or a Result from a method of the wrapper, as inspect of // Option. -func rustRetarget(p *projection) func(call *fileindex.Call, callee ast.NodePosition) { - var lets map[string][]*sitter.Node - letOf := func(name *sitter.Node) *sitter.Node { - if lets == nil { - lets = p.rustLets() - } - fn := rustFunction(name) - var out *sitter.Node - for _, let := range lets[p.text(name)] { - block := let.Parent() - if let.EndByte() <= name.StartByte() && block != nil && name.EndByte() <= block.EndByte() && rustFunction(let) == fn { - out = let - } - } - return out +func rustMarkTries(ctx context.Context, p *projection, call *fileindex.Call, callee ast.NodePosition) { + node := p.descendant(callee) + if node == nil || node.StartByte() != callee.StartByte || node.EndByte() != callee.EndByte { + return } - var tries func(node *sitter.Node) []int - tries = func(node *sitter.Node) []int { - switch node.Type() { - case "try_expression": - return append(tries(node.NamedChild(0)), 0) - case "parenthesized_expression", "await_expression", "reference_expression": - return tries(node.NamedChild(0)) - case "identifier": - if let := letOf(node); let != nil && let.ChildByFieldName("value") != nil { - return tries(let.ChildByFieldName("value")) - } - case "call_expression": - if method := rustMethod(node.ChildByFieldName("function")); method != nil { - return rustAfter(tries(method.ChildByFieldName("value"))) - } - } + method := rustMethod(node) + if method == nil { + return + } + binders, _ := p.rustBinders(ctx, call.Caller) + marks := rustAfter(p.rustTries(binders, method.ChildByFieldName("value"))) + for i, target := range call.Targets { + call.Targets[i] = withTries(target, marks) + } +} + +// rustTries returns a mark for each ? of a receiver: the count of method +// names after it. A name reads the value of its let. +func (p *projection) rustTries(binders map[string][]*sitter.Node, node *sitter.Node) []int { + if node == nil { return nil } - return func(call *fileindex.Call, callee ast.NodePosition) { - node := p.descendant(callee) - if node == nil || node.StartByte() != callee.StartByte || node.EndByte() != callee.EndByte { - return + switch node.Type() { + case "try_expression": + return append(p.rustTries(binders, node.NamedChild(0)), 0) + case "parenthesized_expression", "await_expression", "reference_expression": + return p.rustTries(binders, node.NamedChild(0)) + case "identifier": + if let := rustLetOf(binders[p.text(node)], node); let != nil { + return p.rustTries(binders, let.ChildByFieldName("value")) } - if method := rustMethod(node); method != nil { - marks := rustAfter(tries(method.ChildByFieldName("value"))) - for i, target := range call.Targets { - call.Targets[i] = withTries(target, marks) - } + case "call_expression": + if method := rustMethod(node.ChildByFieldName("function")); method != nil { + return rustAfter(p.rustTries(binders, method.ChildByFieldName("value"))) } } + return nil +} + +// rustLetOf returns the let of one name, among the binders of the name, +// that binds it at a use: the last one that ends before the use, in a +// block that holds the use. +func rustLetOf(binders []*sitter.Node, use *sitter.Node) *sitter.Node { + var out *sitter.Node + for _, let := range binders { + if let.Type() != "let_declaration" || rustBoundName(let.ChildByFieldName("pattern")) == nil { + continue + } + if block := let.Parent(); let.EndByte() <= use.StartByte() && block != nil && use.EndByte() <= block.EndByte() { + out = let + } + } + return out } // rustMethod returns the field expression of a method call, as @@ -497,33 +588,6 @@ func withTries(target string, marks []int) string { return strings.Join(names, fileindex.Separator) } -// rustLets returns the let declarations of the file that bind one name, by -// the name, in source order. -func (p *projection) rustLets() map[string][]*sitter.Node { - lets := map[string][]*sitter.Node{} - walkBody(p.tree.Tree().RootNode(), nil, func(node *sitter.Node) { - if node.Type() != "let_declaration" { - return - } - if name := rustBoundName(node.ChildByFieldName("pattern")); name != nil { - lets[p.text(name)] = append(lets[p.text(name)], node) - } - }) - return lets -} - -// rustFunction returns the start of the function that holds a node. A let -// binds its name for the rest of its block, and a closure in the block -// sees it, but a nested function does not. -func rustFunction(node *sitter.Node) uint32 { - for n := node.Parent(); n != nil; n = n.Parent() { - if n.Type() == "function_item" { - return n.StartByte() - } - } - return 0 -} - // rustPublic reports whether an item is pub. A method of a trait impl is // as public as the trait. func rustPublic(p *projection, def *fileindex.Definition) bool { diff --git a/test/projectgraph/receivers_test.go b/test/projectgraph/receivers_test.go index c74204d2..cef64f9d 100644 --- a/test/projectgraph/receivers_test.go +++ b/test/projectgraph/receivers_test.go @@ -258,6 +258,54 @@ var rustReceiverRows = []receiverRow{ target: "dep//Job//start", want: "uncertain/trait-receiver", }, + { + name: "parameter that a let shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(t: Local) {\n let t = dep::new_thing();\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "parameter that a macro of std reads", + source: "fn run(t: Thing) {\n println!(\"{}\", t.len());\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "parameter that an if let shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(t: Local) {\n if let Some(t) = dep::try_thing() {\n t.inspect();\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "parameter that a match arm shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(t: Local) {\n match dep::try_thing() {\n Some(t) => t.inspect(),\n None => {}\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "parameter that a closure parameter shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(t: Local) {\n let f = |t: Thing| t.inspect();\n f(dep::new_thing());\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "parameter that a let of a macro shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nmacro_rules! bind {\n ($n:ident) => {\n let $n = dep::new_thing();\n };\n}\n\nfn run(t: Local) {\n bind!(t);\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "typed let that a static shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run(l: Local) {\n let t: Local = l;\n {\n static t: Thing = Thing::new();\n t.inspect();\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "let that an if let shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn mk() -> Local {\n Local\n}\n\nfn run() {\n let t = mk();\n if let Some(t) = dep::try_thing() {\n t.inspect();\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, { name: "std function that returns its argument", source: "fn run(t: Thing) {\n std::convert::identity(t).inspect();\n}", From 0a5eebdc6e573420691117ee324a7e198487760d Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 11:03:14 +0000 Subject: [PATCH 32/58] refactor(projectgraph): give the Rust path ladder one owner rustResolver.Module, rustTypePath and rustScopeFallback each resolved a path in the project, then through a name that a module binds to another crate. rustPath now owns these two steps, and each caller keeps only what it does with the answer. The comment of profile.typePath says how it differs from scopeFallback: it gives a key and makes no node. rustFirstName replaces three copies of the cut of the first name. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- projectgraph/profile.go | 4 ++- projectgraph/rust.go | 65 ++++++++++++++++++++++++++++------------- 2 files changed, 48 insertions(+), 21 deletions(-) diff --git a/projectgraph/profile.go b/projectgraph/profile.go index e13d8015..d52c0348 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -31,7 +31,9 @@ type profile struct { // qualified resolves a qualified name, as com.x.Service in Java. qualified func(b *builder, name string) (string, bool) // typePath resolves a type path that a file writes and that its scope - // does not bind, as kit::Store in type Handle = kit::Store of Rust. + // does not bind, as kit::Store in type Handle = kit::Store of Rust. It + // gives a key and makes no node, where scopeFallback walks the names of + // a call and makes the node of the key that the call reaches. typePath func(b *builder, sc *scope, written string) (string, bool) // scopeFallback resolves a name that no scope of the file binds, as a // Java class of a wildcard import of a dependency. diff --git a/projectgraph/rust.go b/projectgraph/rust.go index a045c30d..c46ee00e 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -415,17 +415,37 @@ type rustResolver struct { } func (r rustResolver) Module(importer, spec string) (string, string, bool) { - if key, ok := r.b.resolveRust(importer, spec); ok { - return key, "path", true + t, ok := r.b.rustPath(importer, spec) + switch { + case !ok: + return "", "", false + case t.key != "": + 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. Without it the edge reached no node, and // the target read as not-found. - if path, file, ok := r.b.rustCratePath(importer, spec); ok { - return r.b.external(r.b.rustCrateKey(file, path)), "crate", true + return r.b.external(r.b.rustCrateKey(t.file, t.path)), "crate", true +} + +// rustTarget is what a path of a file names: a definition or a module of +// the project, or a path in another crate and the file of the binding +// that names the crate. +type rustTarget struct { + key, path, file string +} + +// 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 key, ok := b.resolveRust(importer, written); ok { + return rustTarget{key: key}, true } - return "", "", false + if path, file, ok := b.rustCratePath(importer, written); ok { + return rustTarget{path: path, file: file}, true + } + return rustTarget{}, false } // rustCrateKey returns the external key of a path of another crate that a @@ -444,11 +464,11 @@ func (b *builder) rustCrateKey(file, path string) string { // alias of the file writes it: a type of the crate, or the key of a type of // another crate, also through a binding of a module. func rustTypePath(b *builder, sc *scope, written string) (string, bool) { - if key, ok := b.resolveRust(sc.ix.Path, written); ok { - return b.deref(key), true - } - if path, file, ok := b.rustCratePath(sc.ix.Path, written); ok { - return b.rustCrateKey(file, path), true + if t, ok := b.rustPath(sc.ix.Path, written); ok { + if t.key != "" { + return b.deref(t.key), true + } + return b.rustCrateKey(t.file, t.path), true } if !strings.Contains(written, "::") || b.rustLocalRoot(sc.ix.Path, written) { return "", false @@ -573,7 +593,7 @@ func (b *builder) rustCrate(file, module string) (string, string, bool) { // as schannel of extern crate schannel in src/imp/schannel.rs. A file of // the name does not tell it, because the module of that file is no child. func (b *builder) rustLocalRoot(file, path string) bool { - first, _, _ := strings.Cut(strings.TrimPrefix(path, "::"), "::") + first := rustFirstName(path) switch first { case "crate", "self", "super": return true @@ -581,6 +601,12 @@ func (b *builder) rustLocalRoot(file, path string) bool { return b.cargoOf(b.spaceOf(file)).Has(first) || b.rustModuleUnit(file, first) != nil || b.rustModuleUnit(file, "crate::"+first) != nil } +// 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 +} + // rustCrateNames splits a path of a crate into the crate and the names // under it. func rustCrateNames(path string) (string, []string) { @@ -595,7 +621,7 @@ func rustCrateNames(path string) (string, []string) { // the name of a crate of the project or with the name of a file or a // directory of the project, as a child module. func (r rustResolver) FirstParty(spec string) bool { - first, _, _ := strings.Cut(strings.TrimPrefix(spec, "::"), "::") + first := rustFirstName(spec) switch { case first == "crate" || first == "self" || first == "super": return true @@ -609,8 +635,7 @@ func (r rustResolver) FirstParty(spec string) bool { // crate, by the modules that the file sees, and not by the names of all // the files of the project. func (r rustResolver) FirstPartyFrom(importer, spec string) bool { - first, _, _ := strings.Cut(strings.TrimPrefix(spec, "::"), "::") - return !rustRoots[first] && r.b.rustLocalRoot(importer, spec) + return !rustRoots[rustFirstName(spec)] && r.b.rustLocalRoot(importer, spec) } // rustScopeFallback resolves a path that no use and no item of the module @@ -621,12 +646,12 @@ func (r rustResolver) FirstPartyFrom(importer, spec string) bool { // a local closure can bind it. func (b *builder) rustScopeFallback(sc *scope, _, first string, rest []string, target string) []resolution { written := strings.Join(append([]string{first}, rest...), "::") - if key, ok := b.resolveRust(sc.ix.Path, written); ok { - return []resolution{b.walkNamespace(key)} - } - if path, file, ok := b.rustCratePath(sc.ix.Path, written); ok { - crate, names := rustCrateNames(path) - return []resolution{b.importedWalk(b.byPath[file], fileindex.Namespace(langcode.Rust, path), crate, modules.FormImport, names)} + if t, ok := b.rustPath(sc.ix.Path, written); ok { + if t.key != "" { + return []resolution{b.walkNamespace(t.key)} + } + 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 { return []resolution{b.importedWalk(sc.ix, target, first, modules.FormImport, rest)} From 39763117d68012cdb67da44e63391ab601a214dc Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 11:03:14 +0000 Subject: [PATCH 33/58] refactor(langcode): ask for a smart pointer by language with IsPointer IsRustPointer(name) broke the shape of the package API, IsBuiltin(lang, name). IsPointer(lang, name) replaces it. The call graph reads the pointers of the language of its profile, so the shapes lose their pointer function. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- langcode/builtins.go | 14 ++++++++++++++ langcode/builtins_rust.go | 10 +--------- plugin/callgraph/rust.go | 1 - plugin/callgraph/shapes.go | 16 +++++++++------- plugin/indexer/rust.go | 2 +- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/langcode/builtins.go b/langcode/builtins.go index 393f5437..12107ecc 100644 --- a/langcode/builtins.go +++ b/langcode/builtins.go @@ -1,5 +1,7 @@ package langcode +import "slices" + // builtins are the names that a file of the language uses with no import or // definition: functions, types and runtime globals. Each language holds its // list in its own file. @@ -47,6 +49,18 @@ var javaLangSet = func() map[string]bool { return set }() +// pointers are the smart pointers of each language with them. A method +// call on a pointer runs a method of the value that it holds, as s.save() +// on a Box of Rust. +var pointers = map[Code][]string{ + Rust: rustPointers, +} + +// IsPointer reports whether the name is a smart pointer of the language. +func IsPointer(c Code, name string) bool { + return slices.Contains(pointers[c], name) +} + // IsJavaLang reports whether the name is a class of java.lang, which a // Java or a Kotlin file names with no import. func IsJavaLang(name string) bool { diff --git a/langcode/builtins_rust.go b/langcode/builtins_rust.go index 15988bf6..e5318c6b 100644 --- a/langcode/builtins_rust.go +++ b/langcode/builtins_rust.go @@ -1,7 +1,5 @@ package langcode -import "slices" - // rustBuiltins are the macros of std and the names of the prelude, which are in scope in every module. var rustBuiltins = []string{ "println", "print", "eprintln", "eprint", "format", "format_args", "write", "writeln", @@ -13,11 +11,5 @@ var rustBuiltins = []string{ "std", "core", "alloc", } -// rustPointers are the smart pointers of std. A method call on a pointer -// runs a method of the value that it holds, as s.save() on a Box. +// rustPointers are the smart pointers of std. var rustPointers = []string{"Box", "Rc", "Arc"} - -// IsRustPointer reports whether the name is a smart pointer of std. -func IsRustPointer(name string) bool { - return slices.Contains(rustPointers, name) -} diff --git a/plugin/callgraph/rust.go b/plugin/callgraph/rust.go index f43c2808..38e50242 100644 --- a/plugin/callgraph/rust.go +++ b/plugin/callgraph/rust.go @@ -61,7 +61,6 @@ var rustProfile = shapedProfile(languageProfile{ skipped: []string{"use_declaration", "extern_crate_declaration", "attribute_item", "inner_attribute_item"}, values: map[string]childRef{"generic_function": named("function"), "type_cast_expression": named("value")}, constructorMethods: []string{"new", "default"}, - pointer: langcode.IsRustPointer, classKeywords: []string{"Self"}, computedCallees: map[string]bool{"index_expression": true}, }) diff --git a/plugin/callgraph/shapes.go b/plugin/callgraph/shapes.go index 1a6d4433..2a4fa2e9 100644 --- a/plugin/callgraph/shapes.go +++ b/plugin/callgraph/shapes.go @@ -8,7 +8,9 @@ import ( "unicode" "unicode/utf8" + "github.com/safedep/code/core" "github.com/safedep/code/fileindex" + "github.com/safedep/code/langcode" "github.com/safedep/code/pkg/ts" sitter "github.com/smacker/go-tree-sitter" ) @@ -35,11 +37,6 @@ type syntaxShapes struct { // receiver, as in OpenAI::Client.new. A variable that gets the result is // an instance of the receiver. constructorMethods []string - // pointer reports whether a type is a pointer whose constructor method - // holds its argument, as Box in Box::new(Html {}). A method call on the - // value runs a method of the argument, so the value is no instance of - // the pointer. - pointer func(name string) bool // classKeywords name the class that holds the code, as Self in Rust or // static in PHP. superKeywords name its base class, as parent in PHP. @@ -194,6 +191,7 @@ const maxKeywordBytes = 16 var assignableNamePattern = regexp.MustCompile(`^[$@]*[A-Za-z_][A-Za-z0-9_]*$`) type shapedProcessors struct { + code core.LanguageCode shapes syntaxShapes separators []string } @@ -202,7 +200,7 @@ type shapedProcessors struct { // the reachable scopes. A shape replaces a processor of profile for the same // node type. func shapedProfile(profile languageProfile, shapes syntaxShapes) languageProfile { - p := &shapedProcessors{shapes: shapes, separators: profile.separators} + p := &shapedProcessors{code: profile.code, shapes: shapes, separators: profile.separators} processors := maps.Clone(profile.processors) if processors == nil { @@ -662,8 +660,12 @@ func (p *shapedProcessors) instanceOf(callee string) (string, bool) { return callee, p.shapes.classCalls && unicode.IsUpper(first) } +// isPointer reports whether a type is a smart pointer whose constructor +// method holds its argument, as Box in Box::new(Html {}). A method call on +// the value runs a method of the argument, so the value is no instance of +// the pointer. func (p *shapedProcessors) isPointer(name string) bool { - return p.shapes.pointer != nil && p.shapes.pointer(name) + return langcode.IsPointer(p.code, name) } // lastOf returns the last segment of a namespace. diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 5b1811a3..2694d091 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -189,7 +189,7 @@ func (p *projection) rustTypeName(node *sitter.Node, bounds map[string]string) s return p.text(node) case "generic_type": name := p.rustTypeName(node.ChildByFieldName("type"), bounds) - if args := ts.NamedChildren(node.ChildByFieldName("type_arguments")); langcode.IsRustPointer(name) && len(args) > 0 { + if args := ts.NamedChildren(node.ChildByFieldName("type_arguments")); langcode.IsPointer(langcode.Rust, name) && len(args) > 0 { return p.rustTypeName(args[0], bounds) } return name From ecfc33c8dec942a61d5205151f5a5eba24bec791 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 11:03:14 +0000 Subject: [PATCH 34/58] docs(fileindex): say why a Go type alias stays a class Go and Rust model a type alias in two ways. The comment of DefAlias now says why Go keeps the class model: a Go method can name the alias as its receiver, so the alias holds methods. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- fileindex/index.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fileindex/index.go b/fileindex/index.go index c0e97d0e..b1488881 100644 --- a/fileindex/index.go +++ b/fileindex/index.go @@ -63,7 +63,9 @@ const ( // DefConstant is a constant of a Go package. It holds data and no code. DefConstant DefKind = "constant" // DefAlias is a type alias, as type A = B of Rust. It has no code. A - // path through it goes on to the type that it names. + // path through it goes on to the type that it names. A Go alias stays + // a class with the type that it names as its base, because a Go method + // can name the alias as its receiver, so the alias holds methods. DefAlias DefKind = "alias" ) From 40b327f2984df3352b69be6f2ca22221ae1ae9a6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 11:05:48 +0000 Subject: [PATCH 35/58] fix(indexer): count a Rust static or const in its whole block (FOU-562) A static or a const of a block binds its name in the whole block, also before the item. The rebound check counted only a binder before the call, so t.inspect() before static t in the block kept the edge of the let t, and a method of the static type read as not-found. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- plugin/indexer/rust.go | 6 ++++-- test/projectgraph/receivers_test.go | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 2694d091..691c83ec 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -355,7 +355,8 @@ 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 function whose walk stopped +// 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. func rustRebound(ctx context.Context, p *projection, call fileindex.Call, name string) bool { binders, ok := p.rustBinders(ctx, call.Caller) @@ -363,7 +364,8 @@ func rustRebound(ctx context.Context, p *projection, call fileindex.Call, name s return true } return slices.ContainsFunc(binders[name], func(binder *sitter.Node) bool { - return startsBefore(binder, call) && !rustPlainBinder(binder) + item := binder.Type() == "static_item" || binder.Type() == "const_item" + return (item || startsBefore(binder, call)) && !rustPlainBinder(binder) }) } diff --git a/test/projectgraph/receivers_test.go b/test/projectgraph/receivers_test.go index cef64f9d..65ed3402 100644 --- a/test/projectgraph/receivers_test.go +++ b/test/projectgraph/receivers_test.go @@ -300,6 +300,12 @@ var rustReceiverRows = []receiverRow{ target: "dep//Thing//inspect", want: "uncertain/unknown-receiver", }, + { + name: "let that a static after the call shadows", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn mk() -> Local {\n Local\n}\n\nfn run() {\n let t = mk();\n {\n t.inspect();\n static t: Thing = Thing::new();\n }\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, { name: "let that an if let shadows", source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn mk() -> Local {\n Local\n}\n\nfn run() {\n let t = mk();\n if let Some(t) = dep::try_thing() {\n t.inspect();\n }\n}", From dc741547ccf86bdeb6134dc9882269707dbdb469 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 11:23:51 +0000 Subject: [PATCH 36/58] fix(projectgraph): link the item that a Rust use path names A Rust use path names a module or an item. The link step reads all of the path of use dep::top as the module of the origin, so the key dep//top of a call of top() has an origin with no names. The unit gives the function top for that path. linkProvision read an origin with no names as a whole module and added no package edge. So reach did not enter the unit, and each target that top reaches was not-found. linkProvision now links the key to the provision when the provision is a definition and not a module. The new case rust/use-dependency-item covers a plain use, a rename, a use list, a nested module, a pub use of a private module and a use in a function body. Six of its targets were not-found before the fix. The cases rust/trait-object and rust/trait-object-shadowed get the package edges of the types that their use lists name. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 4 + projectgraph/packages.go | 6 +- .../rust/trait-object-shadowed/expect.yaml | 5 + .../cases/rust/trait-object/expect.yaml | 10 + .../rust/use-dependency-item/expect.yaml | 302 ++++++++++++++++++ .../use-dependency-item/project/Cargo.toml | 7 + .../use-dependency-item/project/src/main.rs | 22 ++ .../project/vendor/dep/Cargo.toml | 4 + .../project/vendor/dep/src/hidden.rs | 5 + .../project/vendor/dep/src/lib.rs | 48 +++ .../project/vendor/dep/src/sub.rs | 5 + .../summaries/cargo_dep_1.0.0.json | 86 +++++ 12 files changed, 503 insertions(+), 1 deletion(-) create mode 100644 test/projectgraph/cases/rust/use-dependency-item/expect.yaml create mode 100644 test/projectgraph/cases/rust/use-dependency-item/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-dependency-item/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/Cargo.toml create mode 100644 test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/hidden.rs create mode 100644 test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/lib.rs create mode 100644 test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/sub.rs create mode 100644 test/projectgraph/cases/rust/use-dependency-item/summaries/cargo_dep_1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 358e234c..17ebb73e 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -287,6 +287,10 @@ Rust follows the same rules, with these additions. crate `kit` of the unit. The external node keeps the written name, as `helper//serve`, and the package edge goes on to the definition in the unit, whose external key is `kit//serve`. +- A `use` path names a module or an item, so the link step reads all of + the path of `use dep::top` as a module. The unit gives the function `top` + for that path, and a call of `top()` gets the package edge to it, as a + call of `dep::top()` does. - The index records the return type of each function and method, with `Self` as the type of its `impl`. A type drops `&`, `&mut`, `Box<..>`, `Rc<..>`, `Arc<..>` and its type arguments, so `&mut SmallVec<[u8; 4]>` diff --git a/projectgraph/packages.go b/projectgraph/packages.go index 801400bd..7b7317a5 100644 --- a/projectgraph/packages.go +++ b/projectgraph/packages.go @@ -435,7 +435,11 @@ func (b *builder) linkProvision(key string, o origin, pv provision) { } b.named[pv.sp.unit] = true names := o.names - if len(names) == 0 { + // A Rust use path names a module or an item, so the provision of use + // dep::top is the function top itself. Without this check, the key of + // the bound name read as a whole module, and got no package edge. + item := b.defs[pv.module] != nil && !b.isModule(pv.module) + if len(names) == 0 && !item { // The key of a whole module is the value of the module. Only the // default export of JavaScript gives that value code, and an // import edge runs the module top level. diff --git a/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml b/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml index d644ce21..ed003dbf 100644 --- a/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml +++ b/test/projectgraph/cases/rust/trait-object-shadowed/expect.yaml @@ -31,6 +31,11 @@ edges: kind: direct confidence: high site: cargo:dep@1.2.0//src/lib.rs:18 + - from: dep//Html + to: cargo:dep@1.2.0//src/lib.rs//Html + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:7 - from: dep//Plain//write to: cargo:dep@1.2.0//src/lib.rs//Plain//write kind: package diff --git a/test/projectgraph/cases/rust/trait-object/expect.yaml b/test/projectgraph/cases/rust/trait-object/expect.yaml index 071baf45..d7fd8ee6 100644 --- a/test/projectgraph/cases/rust/trait-object/expect.yaml +++ b/test/projectgraph/cases/rust/trait-object/expect.yaml @@ -20,6 +20,16 @@ edges: kind: direct confidence: high site: cargo:dep@1.2.0//src/lib.rs:8 + - from: dep//Html + to: cargo:dep@1.2.0//src/lib.rs//Html + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:7 + - from: dep//Write + to: cargo:dep@1.2.0//src/lib.rs//Write + kind: package + confidence: high + site: cargo:dep@1.2.0//src/lib.rs:1 - from: dep//Html//write to: cargo:dep@1.2.0//src/lib.rs//Html//write kind: package diff --git a/test/projectgraph/cases/rust/use-dependency-item/expect.yaml b/test/projectgraph/cases/rust/use-dependency-item/expect.yaml new file mode 100644 index 00000000..d07d598c --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/expect.yaml @@ -0,0 +1,302 @@ +source: safedep:hand +description: >- + main.rs binds items of the crate dep with use and calls each one by its bound name: a function (top), a renamed + function (renamed as r), two names of one use list (other, Type), a function of a nested module (sub::nested), a + function that lib.rs gives on with pub use (reexported), and a use in a function body (inner). The code of the + crate is the unit cargo:dep@1.0.0. A use path names a module or an item, so the link step reads dep::top as the + module of the origin, and the unit gives the function top itself. Before the link step linked an item that way, + the external key had no package edge, and each target but run_sink was not-found. main does not call unused, so + unused_sink stays 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 +exact_edges: true +edges: + - from: cargo:dep@1.0.0//src/hidden.rs//reexported + to: cargo:dep@1.0.0//src/hidden.rs//hidden_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/hidden.rs:2 + - from: cargo:dep@1.0.0//src/lib.rs//Type//run + to: cargo:dep@1.0.0//src/lib.rs//Type + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:43 + - from: cargo:dep@1.0.0//src/lib.rs//Type//run + to: cargo:dep@1.0.0//src/lib.rs//run_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:44 + - from: cargo:dep@1.0.0//src/lib.rs//inner + to: cargo:dep@1.0.0//src/lib.rs//inner_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:25 + - from: cargo:dep@1.0.0//src/lib.rs//other + to: cargo:dep@1.0.0//src/lib.rs//other_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:19 + - from: cargo:dep@1.0.0//src/lib.rs//renamed + to: cargo:dep@1.0.0//src/lib.rs//renamed_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:13 + - from: cargo:dep@1.0.0//src/lib.rs//top + to: cargo:dep@1.0.0//src/lib.rs//sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:7 + - from: cargo:dep@1.0.0//src/lib.rs//unused + to: cargo:dep@1.0.0//src/lib.rs//unused_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:31 + - from: cargo:dep@1.0.0//src/sub.rs//nested + to: cargo:dep@1.0.0//src/sub.rs//nested_sink + kind: direct + confidence: high + site: cargo:dep@1.0.0//src/sub.rs:2 + - from: dep//Type//new + to: cargo:dep@1.0.0//src/lib.rs//Type//new + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:39 + - from: dep//Type//run + to: cargo:dep@1.0.0//src/lib.rs//Type//run + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:43 + - from: dep//inner + to: cargo:dep@1.0.0//src/lib.rs//inner + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:24 + - from: dep//other + to: cargo:dep@1.0.0//src/lib.rs//other + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:18 + - from: dep//reexported + to: cargo:dep@1.0.0//src/hidden.rs//reexported + kind: package + confidence: high + site: cargo:dep@1.0.0//src/hidden.rs:1 + - from: dep//renamed + to: cargo:dep@1.0.0//src/lib.rs//renamed + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:12 + - from: dep//sub//nested + to: cargo:dep@1.0.0//src/sub.rs//nested + kind: package + confidence: high + site: cargo:dep@1.0.0//src/sub.rs:1 + - from: dep//top + to: cargo:dep@1.0.0//src/lib.rs//top + kind: package + confidence: high + site: cargo:dep@1.0.0//src/lib.rs:6 + - from: src/main.rs//local + to: dep//inner + kind: direct + confidence: high + site: src/main.rs:21 + - from: src/main.rs//main + to: dep//Type//new + kind: direct + confidence: high + site: src/main.rs:14 + - from: src/main.rs//main + to: dep//Type//run + kind: direct + confidence: high + site: src/main.rs:14 + - from: src/main.rs//main + to: dep//other + kind: direct + confidence: high + site: src/main.rs:12 + - from: src/main.rs//main + to: dep//reexported + kind: direct + confidence: high + site: src/main.rs:15 + - from: src/main.rs//main + to: dep//renamed + kind: direct + confidence: high + site: src/main.rs:11 + - from: src/main.rs//main + to: dep//sub//nested + kind: direct + confidence: high + site: src/main.rs:13 + - from: src/main.rs//main + to: dep//top + kind: direct + confidence: high + site: src/main.rs:10 + - from: src/main.rs//main + to: src/main.rs//local + kind: direct + confidence: high + site: src/main.rs:16 +unresolved: [] +results: + - target: dep//sink + status: found + path: + - key: src/main.rs//main + - key: dep//top + site: src/main.rs:10 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//top + site: cargo:dep@1.0.0//src/lib.rs:6 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//sink + site: cargo:dep@1.0.0//src/lib.rs:7 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//top + site: cargo:dep@1.0.0//src/lib.rs:7 + reached: true + - target: dep//renamed_sink + status: found + path: + - key: src/main.rs//main + - key: dep//renamed + site: src/main.rs:11 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//renamed + site: cargo:dep@1.0.0//src/lib.rs:12 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//renamed_sink + site: cargo:dep@1.0.0//src/lib.rs:13 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//renamed + site: cargo:dep@1.0.0//src/lib.rs:13 + reached: true + - target: dep//other_sink + status: found + path: + - key: src/main.rs//main + - key: dep//other + site: src/main.rs:12 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//other + site: cargo:dep@1.0.0//src/lib.rs:18 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//other_sink + site: cargo:dep@1.0.0//src/lib.rs:19 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//other + site: cargo:dep@1.0.0//src/lib.rs:19 + reached: true + - target: dep//sub//nested_sink + status: found + path: + - key: src/main.rs//main + - key: dep//sub//nested + site: src/main.rs:13 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/sub.rs//nested + site: cargo:dep@1.0.0//src/sub.rs:1 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/sub.rs//nested_sink + site: cargo:dep@1.0.0//src/sub.rs:2 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/sub.rs//nested + site: cargo:dep@1.0.0//src/sub.rs:2 + reached: true + - target: dep//run_sink + status: found + path: + - key: src/main.rs//main + - key: dep//Type//run + site: src/main.rs:14 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//Type//run + site: cargo:dep@1.0.0//src/lib.rs:43 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//run_sink + site: cargo:dep@1.0.0//src/lib.rs:44 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//Type//run + site: cargo:dep@1.0.0//src/lib.rs:44 + reached: true + - target: dep//hidden//hidden_sink + status: found + path: + - key: src/main.rs//main + - key: dep//reexported + site: src/main.rs:15 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/hidden.rs//reexported + site: cargo:dep@1.0.0//src/hidden.rs:1 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/hidden.rs//hidden_sink + site: cargo:dep@1.0.0//src/hidden.rs:2 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/hidden.rs//reexported + site: cargo:dep@1.0.0//src/hidden.rs:2 + reached: true + - target: dep//inner_sink + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//local + site: src/main.rs:16 + kind: direct + confidence: high + - key: dep//inner + site: src/main.rs:21 + kind: direct + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//inner + site: cargo:dep@1.0.0//src/lib.rs:24 + kind: package + confidence: high + - key: cargo:dep@1.0.0//src/lib.rs//inner_sink + site: cargo:dep@1.0.0//src/lib.rs:25 + kind: direct + confidence: high + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//inner + site: cargo:dep@1.0.0//src/lib.rs:25 + reached: true + - target: dep//unused_sink + status: not-found + call_sites: + - caller: cargo:dep@1.0.0//src/lib.rs//unused + site: cargo:dep@1.0.0//src/lib.rs:31 + reached: false diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/Cargo.toml b/test/projectgraph/cases/rust/use-dependency-item/project/Cargo.toml new file mode 100644 index 00000000..730014fa --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/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/use-dependency-item/project/src/main.rs b/test/projectgraph/cases/rust/use-dependency-item/project/src/main.rs new file mode 100644 index 00000000..9ff577bc --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/src/main.rs @@ -0,0 +1,22 @@ +use dep::top; +use dep::renamed as r; +use dep::{other, Type}; +use dep::sub::nested; +use dep::reexported; +#[allow(unused_imports)] +use dep::unused; + +fn main() { + top(); + r(); + other(); + nested(); + Type::new().run(); + reexported(); + local(); +} + +fn local() { + use dep::inner; + inner(); +} diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/Cargo.toml b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/Cargo.toml new file mode 100644 index 00000000..537b4e47 --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-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/use-dependency-item/project/vendor/dep/src/hidden.rs b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/hidden.rs new file mode 100644 index 00000000..c094c095 --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/hidden.rs @@ -0,0 +1,5 @@ +pub fn reexported() { + hidden_sink(); +} + +fn hidden_sink() {} diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/lib.rs b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/lib.rs new file mode 100644 index 00000000..5bd6dc92 --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/lib.rs @@ -0,0 +1,48 @@ +pub mod sub; +mod hidden; + +pub use hidden::reexported; + +pub fn top() { + sink(); +} + +pub fn sink() {} + +pub fn renamed() { + renamed_sink(); +} + +fn renamed_sink() {} + +pub fn other() { + other_sink(); +} + +fn other_sink() {} + +pub fn inner() { + inner_sink(); +} + +fn inner_sink() {} + +pub fn unused() { + unused_sink(); +} + +fn unused_sink() {} + +pub struct Type; + +impl Type { + pub fn new() -> Type { + Type + } + + pub fn run(&self) { + run_sink(); + } +} + +fn run_sink() {} diff --git a/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/sub.rs b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/sub.rs new file mode 100644 index 00000000..92d365b9 --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/project/vendor/dep/src/sub.rs @@ -0,0 +1,5 @@ +pub fn nested() { + nested_sink(); +} + +fn nested_sink() {} 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 new file mode 100644 index 00000000..f209d93e --- /dev/null +++ b/test/projectgraph/cases/rust/use-dependency-item/summaries/cargo_dep_1.0.0.json @@ -0,0 +1,86 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:dep@1.0.0", + "symbols": [ + { + "key": "dep//Type", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//Type" + ], + "reaches": [] + }, + { + "key": "dep//Type//new", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//Type//new" + ], + "reaches": [] + }, + { + "key": "dep//Type//run", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//Type//run" + ], + "reaches": [] + }, + { + "key": "dep//inner", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//inner" + ], + "reaches": [] + }, + { + "key": "dep//other", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//other" + ], + "reaches": [] + }, + { + "key": "dep//reexported", + "definitions": [ + "cargo:dep@1.0.0//src/hidden.rs//reexported" + ], + "reaches": [] + }, + { + "key": "dep//renamed", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//renamed" + ], + "reaches": [] + }, + { + "key": "dep//sink", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//sink" + ], + "reaches": [] + }, + { + "key": "dep//sub//nested", + "definitions": [ + "cargo:dep@1.0.0//src/sub.rs//nested" + ], + "reaches": [] + }, + { + "key": "dep//top", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//top" + ], + "reaches": [] + }, + { + "key": "dep//unused", + "definitions": [ + "cargo:dep@1.0.0//src/lib.rs//unused" + ], + "reaches": [] + } + ], + "targets": [] +} From 4eb15e569b73161dafe4e45c168c0d340fd1dfd5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 11:37:47 +0000 Subject: [PATCH 37/58] fix(reach): enter the load code of a package from a source (FOU-560) A call of a Go function value concerns no target, because the search reaches the function through the reference edge of the code that holds the value. For a package-level value, that code is the module top level. A search from a source, as reach.SourcesOnly asks, or from a test root did not enter the module top levels of its packages. So var hs = []func(){os.Exit}; func run() { hs[0]() } gave os//Exit not-found from the source run. The same gap gave not-found for a call of a field or an attribute that a package-level value holds, also in Python and JavaScript. A query with a source or with IncludeTests now takes a load step. Each node that the search reaches enters the module top level of each file of its load group: the package of the file in Go, and the file alone in the other languages. The span of a scope takes the same step. A test file loads only with the code of a test. Graph.LoadGroup gives the group, and reach.PathString writes the hop as main.go (load). A search from the roots alone does not take the step, so its results and the summaries do not change. The cases go/source-package-load, python/source-module-load and javascript/source-module-load guard the rule. The case go/stdlib-callback-method-record moves its idle source to a package with no load code, because main.go holds http.ListenAndServe as a value. The example test of a search from a source counts the module top level of handlers.py. The method measure searches from the roots, so it does not change. The reports of 65b7c53 and of this change are the same in both modes, with govulncheck-reached=115 found=71 uncertain=44 false-negative=0. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 41 ++- examples/projectgraph/main_test.go | 2 +- projectgraph/graph.go | 11 + projectgraph/reach/load.go | 79 ++++++ projectgraph/reach/reach.go | 75 +++-- projectgraph/reach/reach_test.go | 3 + projectgraph/reach/sets.go | 2 +- projectgraph/reach/source_test.go | 44 +++ projectgraph/reach/span.go | 5 + .../cases/go/source-package-load/expect.yaml | 263 ++++++++++++++++++ .../project/extra/extra.go | 9 + .../project/extra/extra_test.go | 13 + .../go/source-package-load/project/go.mod | 3 + .../source-package-load/project/impl/impl.go | 11 + .../go/source-package-load/project/lib/lib.go | 9 + .../go/source-package-load/project/main.go | 32 +++ .../stdlib-callback-method-record/expect.yaml | 6 +- .../project/main.go | 2 - .../project/quiet/quiet.go | 3 + .../javascript/source-module-load/expect.yaml | 85 ++++++ .../source-module-load/project/handlers.js | 15 + .../source-module-load/project/main.js | 4 + .../source-module-load/project/package.json | 1 + .../python/source-module-load/expect.yaml | 100 +++++++ .../source-module-load/project/Procfile | 1 + .../source-module-load/project/handlers.py | 18 ++ .../python/source-module-load/project/main.py | 4 + 27 files changed, 804 insertions(+), 37 deletions(-) create mode 100644 projectgraph/reach/load.go create mode 100644 test/projectgraph/cases/go/source-package-load/expect.yaml create mode 100644 test/projectgraph/cases/go/source-package-load/project/extra/extra.go create mode 100644 test/projectgraph/cases/go/source-package-load/project/extra/extra_test.go create mode 100644 test/projectgraph/cases/go/source-package-load/project/go.mod create mode 100644 test/projectgraph/cases/go/source-package-load/project/impl/impl.go create mode 100644 test/projectgraph/cases/go/source-package-load/project/lib/lib.go create mode 100644 test/projectgraph/cases/go/source-package-load/project/main.go create mode 100644 test/projectgraph/cases/go/stdlib-callback-method-record/project/quiet/quiet.go create mode 100644 test/projectgraph/cases/javascript/source-module-load/expect.yaml create mode 100644 test/projectgraph/cases/javascript/source-module-load/project/handlers.js create mode 100644 test/projectgraph/cases/javascript/source-module-load/project/main.js create mode 100644 test/projectgraph/cases/javascript/source-module-load/project/package.json create mode 100644 test/projectgraph/cases/python/source-module-load/expect.yaml create mode 100644 test/projectgraph/cases/python/source-module-load/project/Procfile create mode 100644 test/projectgraph/cases/python/source-module-load/project/handlers.py create mode 100644 test/projectgraph/cases/python/source-module-load/project/main.py diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 118fc270..4297caf0 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -549,6 +549,10 @@ the sources do. | `reach.SourcesAndRoots` | The sources and the roots that the rules marked. It is the default | | `reach.SourcesOnly` | The sources alone. The root fallback does not apply, so no result carries a `root-fallback` uncertainty. It needs at least one source | +A search from a source also enters the load code of each package whose +code it reaches, because the program runs that code before the source. See +[A call of a function value](#a-call-of-a-function-value). + A source must name a first-party node of the graph. A key of no node, an external symbol or a definition of dependency code is an error of `Reach` that wraps `reach.ErrUnknownSource`, one per bad source, so a typo never @@ -788,12 +792,37 @@ the value, and the call has the record that [Code outside the graph](#code-outside-the-graph) gives. The search reaches that record where it reaches the code that makes the value. -The argument holds for a search from the roots, which reaches the module -top level of each package that the program loads. A search from the -sources alone, as `reach.SourcesOnly` asks, starts at the sources and not -at the module top levels of their packages. So a call of a function value -that only a package-level value holds can run a function that such a -search does not reach, and the target can read as `not-found`. +The argument needs a search that reaches the module top level of each +package whose code it reaches. A search from the roots of kind `main` +does. The module top level of each file of the package of `main` is a +root, and an import edge joins a module top level to each module top level +of a package that it imports and that runs code at load. A search from a +source, or from a test root, does not. The program runs the load code of +a package before the source, so no path from the source leads to it. + +So a query with a source, or with `IncludeTests`, adds a load step, and +the search of a test path adds it too. Each node that the search reaches +enters the module top level of each file of its load group, as +`Graph.LoadGroup` gives it. The load group is the +package of the file in a language whose program loads a whole package +before any code of the package runs, as Go. In another language it is the +file alone, as a Python or a JavaScript module, which runs its top level +before any of its functions can run. A test file is in the load group only +of the code of a test. The span of a method name, a unit or a callback +takes the same step, so the code that a candidate of a method name holds +in its package counts too. A hop of the step has the resolver `load`, and +`reach.PathString` writes it as `main.go (load)`. + +The step only adds nodes to a search, so it turns no answer into +`not-found`. It costs precision. A source that calls nothing still reaches +each function value of the load code of its package, as `idle` does in the +case `go/source-package-load`. A search from the roots alone does not take +the step, so its results do not change. The summary of a unit does not +take it either. A summary answers for a search from one symbol of a unit, +and the import edges of an external key give that search the load code of +its Go package. The cases `go/source-package-load`, +`python/source-module-load` and `javascript/source-module-load` hold a +function value in the load code and call it from a source. 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/examples/projectgraph/main_test.go b/examples/projectgraph/main_test.go index af277cb4..7c6160ff 100644 --- a/examples/projectgraph/main_test.go +++ b/examples/projectgraph/main_test.go @@ -148,7 +148,7 @@ func TestExampleSearchesFromASource(t *testing.T) { out, err = exec.Command(bin, "-dir", deadCallCase, "-source", "handlers.py:load_old", "-sources-only", "-target", "json//load").CombinedOutput() require.NoError(t, err, string(out)) - assert.Equal(t, "json//load: not-found\n call at handlers.py:8 in handlers.py//parse, which no root reaches\n searched 1 roots, 3 nodes, 2 edges\n", string(out)) + assert.Equal(t, "json//load: not-found\n call at handlers.py:8 in handlers.py//parse, which no root reaches\n searched 1 roots, 4 nodes, 4 edges\n", string(out)) cmd := exec.Command(bin, "-dir", deadCallCase, "-source", "handlers.py:load_odl", "-target", "yaml//load") var stderr strings.Builder diff --git a/projectgraph/graph.go b/projectgraph/graph.go index fcb2beb9..aac7e573 100644 --- a/projectgraph/graph.go +++ b/projectgraph/graph.go @@ -292,6 +292,17 @@ func (g *Graph) Runs(path string) bool { return len(g.Stats.Languages) == 0 || slices.Contains(g.Stats.Languages, n.Language) } +// LoadGroup returns the key of the files whose module top levels the +// program runs before the code of a file runs: the directory of the file, +// in a language whose program loads a whole package, as Go, and else the +// file alone. +func (g *Graph) LoadGroup(path string) string { + if n := g.Nodes[path]; n != nil && profileOf(n.Language).loadsPackages { + return modules.Dir(path) + } + return path +} + // unitLanguages returns the languages of the code that the ecosystem of a // unit installs, or nil for an ecosystem that the link step does not know. func unitLanguages(unit string) []langcode.Code { diff --git a/projectgraph/reach/load.go b/projectgraph/reach/load.go new file mode 100644 index 00000000..787e8857 --- /dev/null +++ b/projectgraph/reach/load.go @@ -0,0 +1,79 @@ +package reach + +import "github.com/safedep/code/projectgraph" + +// LoadResolver is the Resolver of a hop into a module top level that the +// program runs before the code of the hop before it, as the init code of +// a Go package before a function of the package. +const LoadResolver = "load" + +// loadGroup holds the module top levels of one load group: all of them, +// and the ones of the files that are not tests. +type loadGroup struct { + all, run []int +} + +// loadsOf returns the module top levels that the program runs before the +// code of a node can run: the files of the load group of its file. Code +// at a module top level holds the package-level values, as a table of +// function values, and the reference edges of a value start there. A +// search from a source or a root that the rules do not tie to the load +// code would not reach them. A test file loads only with the code of a +// test. +func (a *adjacency) loadsOf(id int) []int { + n := a.g.Nodes[a.keys[id]] + if n == nil || n.Path == "" { + return nil + } + if a.loadGroups == nil { + a.loadGroups = map[string]*loadGroup{} + for fid, key := range a.keys { + f := a.g.Nodes[key] + if f == nil || f.Kind != projectgraph.NodeModule || f.Path != key { + continue + } + k := a.g.LoadGroup(key) + grp := a.loadGroups[k] + if grp == nil { + grp = &loadGroup{} + a.loadGroups[k] = grp + } + grp.all = append(grp.all, fid) + if !f.Test { + grp.run = append(grp.run, fid) + } + } + } + grp := a.loadGroups[a.g.LoadGroup(n.Path)] + switch { + case grp == nil: + return nil + case n.Test: + return grp.all + } + return grp.run +} + +// loadParent is the parent of a module top level that a search enters +// from the node u by its load group. It is below -1, the parent of a root. +func loadParent(u int) int32 { + return int32(-2 - u) +} + +// edgeInto returns the edge into a node on its shortest path: an edge of +// the graph, or the load step from the node that entered its load group. +func (a *adjacency) edgeInto(s *search, node int) projectgraph.Edge { + p := s.parent[node] + if p >= 0 { + return a.g.Edges[p] + } + key := a.keys[node] + return projectgraph.Edge{ + From: a.keys[-2-p], + To: key, + Site: projectgraph.Site{Path: key}, + Kind: projectgraph.EdgeImport, + Confidence: projectgraph.ConfidenceHigh, + Resolver: LoadResolver, + } +} diff --git a/projectgraph/reach/reach.go b/projectgraph/reach/reach.go index 460f83b2..1a912a49 100644 --- a/projectgraph/reach/reach.go +++ b/projectgraph/reach/reach.go @@ -188,7 +188,9 @@ func (r Result) String() string { // A hop with low confidence names its edge kind, as main.py:3 (name), so // a reader sees the weak hop. An llm hop also names its model and its // prompt. A package hop names its kind at the definition in the package, -// so a reader sees where the path enters dependency code. +// so a reader sees where the path enters dependency code. A load hop, as +// main.go (load), enters a module top level that the program runs before +// the code of the hop before it. func PathString(hops []Hop) string { parts := make([]string, 0, len(hops)+1) if hops[0].Root == RootQuery { @@ -199,6 +201,8 @@ func PathString(hops []Hop) string { switch { case h.EdgeKind == projectgraph.EdgeLLM: part += " (" + string(h.EdgeKind) + ", " + h.Resolver + ")" + case h.Resolver == LoadResolver: + part += " (" + LoadResolver + ")" case h.Confidence == projectgraph.ConfidenceLow || h.EdgeKind == projectgraph.EdgePackage: part += " (" + string(h.EdgeKind) + ")" } @@ -223,6 +227,10 @@ func Reach(ctx context.Context, g *projectgraph.Graph, q Query) ([]Result, error } a := newAdjacency(g) a.includeTests = q.IncludeTests + // A search from the roots of kind main enters the load code through + // the module top levels that the root rules add and through the + // import edges. A search from a source or a test root does not. + a.loads = len(q.Sources) > 0 || q.IncludeTests depth := cmp.Or(q.MaxDepth, defaultMaxDepth) targets := make([][]int, len(q.Targets)) @@ -245,7 +253,7 @@ func Reach(ctx context.Context, g *projectgraph.Graph, q Query) ([]Result, error } } kinds := a.rootKinds(roots) - s := a.search(ctx, a.ids(roots), depth, targets) + s := a.search(ctx, a.ids(roots), depth, targets, a.loads) unc := a.uncertainties(s, depth) // The fallback roots are in the search only with the roots of the // graph, so a search from the sources alone has no fallback to report. @@ -275,7 +283,7 @@ func Reach(ctx context.Context, g *projectgraph.Graph, q Query) ([]Result, error if tests == nil { all := slices.Concat(roots, testRoots) testKinds = a.rootKinds(all) - tests = a.search(ctx, a.ids(all), depth, targets) + tests = a.search(ctx, a.ids(all), depth, targets, true) } if node, ok := tests.nearest(targets[i]); ok { r.TestPath = a.path(tests, node, testKinds) @@ -342,7 +350,7 @@ func (x *Explorer) Explore(ctx context.Context, from []string, maxDepth int) (*E } slices.Sort(sources) depth := cmp.Or(maxDepth, defaultMaxDepth) - s := a.search(ctx, slices.Compact(sources), depth, nil) + s := a.search(ctx, slices.Compact(sources), depth, nil, false) e := &Exploration{Reached: []Reached{}} for _, id := range a.externals { @@ -359,7 +367,10 @@ func (x *Explorer) Explore(ctx context.Context, from []string, maxDepth int) (*E // byLast the nodes by the last segment of their key, callbacks the methods // that code outside the graph can call, and spans what the code of each // scope can run. Each table fills on first use. includeTests lets a node -// of a test file start the span of a method name or of the callback. +// of a test file start the span of a method name or of the callback. loads +// makes the search of a query and each span enter the load group of each +// node that they reach, and loadGroups holds the module top levels of each +// group. type adjacency struct { g *projectgraph.Graph keys []string @@ -373,12 +384,14 @@ type adjacency struct { byExternal map[string][]int includeTests bool - - src *sources - unitNodes map[string][]int - byLast map[string][]int - callbacks []int - spans map[scopeKey]*span + loads bool + + src *sources + loadGroups map[string]*loadGroup + unitNodes map[string][]int + byLast map[string][]int + callbacks []int + spans map[scopeKey]*span } // sources returns the records, the flags and the budgets of the graph by @@ -466,7 +479,7 @@ func (a *adjacency) ids(roots []projectgraph.Root) []int { // search is the state of one breadth-first search. best is the highest // lowest confidence rank of a shortest path to each node, and parent is the -// edge into the node on that path, or -1 at a root. +// edge into the node on that path, -1 at a root, or a load parent. type search struct { dist []int32 best []int8 @@ -477,7 +490,9 @@ type search struct { stopped bool } -func (a *adjacency) search(ctx context.Context, sources []int, maxDepth int, targets [][]int) *search { +// search runs one breadth-first search. With loads, it enters the load +// group of each node that it reaches. +func (a *adjacency) search(ctx context.Context, sources []int, maxDepth int, targets [][]int, loads bool) *search { n := len(a.keys) s := &search{dist: make([]int32, n), best: make([]int8, n), parent: make([]int32, n)} for i := range s.dist { @@ -498,26 +513,36 @@ func (a *adjacency) search(ctx context.Context, sources []int, maxDepth int, tar if len(targets) > 0 && s.allFound(targets) { return s } + unseen := func(v int) bool { return s.dist[v] < 0 } if depth == maxDepth { s.depthCut = slices.ContainsFunc(frontier, func(u int) bool { - return slices.ContainsFunc(a.out[u], func(e int32) bool { return s.dist[a.id[a.g.Edges[e].To]] < 0 }) + return slices.ContainsFunc(a.out[u], func(e int32) bool { return unseen(a.id[a.g.Edges[e].To]) }) || + loads && slices.ContainsFunc(a.loadsOf(u), unseen) }) return s } var next []int + visit := func(v int, rank int8, parent int32) { + switch { + case s.dist[v] < 0: + s.dist[v], s.best[v], s.parent[v] = int32(depth+1), rank, parent + next = append(next, v) + case s.dist[v] == int32(depth+1) && rank > s.best[v]: + s.best[v], s.parent[v] = rank, parent + } + } for _, u := range frontier { for _, e := range a.out[u] { s.edges++ edge := &a.g.Edges[e] - v := a.id[edge.To] - rank := min(s.best[u], int8(edge.Confidence.Rank())) - switch { - case s.dist[v] < 0: - s.dist[v], s.best[v], s.parent[v] = int32(depth+1), rank, e - next = append(next, v) - case s.dist[v] == int32(depth+1) && rank > s.best[v]: - s.best[v], s.parent[v] = rank, e - } + visit(a.id[edge.To], min(s.best[u], int8(edge.Confidence.Rank())), e) + } + if !loads { + continue + } + for _, v := range a.loadsOf(u) { + s.edges++ + visit(v, s.best[u], loadParent(u)) } } s.order = append(s.order, next...) @@ -555,8 +580,8 @@ func (s *search) nearest(ids []int) (int, bool) { // exploration, passes nil. func (a *adjacency) path(s *search, node int, kinds rootKinds) []Hop { var hops []Hop - for s.parent[node] >= 0 { - e := a.g.Edges[s.parent[node]] + for s.parent[node] != -1 { + e := a.edgeInto(s, node) hops = append(hops, Hop{Key: e.To, Site: e.Site, EdgeKind: e.Kind, Confidence: e.Confidence, Resolver: e.Resolver}) node = a.id[e.From] } diff --git a/projectgraph/reach/reach_test.go b/projectgraph/reach/reach_test.go index eb158d5a..09f26c62 100644 --- a/projectgraph/reach/reach_test.go +++ b/projectgraph/reach/reach_test.go @@ -721,6 +721,9 @@ func TestPathStringNamesAWeakHop(t *testing.T) { hops[1].EdgeKind, hops[1].Resolver = projectgraph.EdgeLLM, "model m1, prompt v2" assert.Equal(t, "main.py:3 (llm, model m1, prompt v2) -> app.py:6 -> yaml//load", PathString(hops)) + + hops[1] = Hop{Key: "app.py", Site: projectgraph.Site{Path: "app.py"}, EdgeKind: projectgraph.EdgeImport, Confidence: projectgraph.ConfidenceHigh, Resolver: LoadResolver} + assert.Equal(t, "app.py (load) -> app.py:6 -> yaml//load", PathString(hops)) } func TestResultString(t *testing.T) { diff --git a/projectgraph/reach/sets.go b/projectgraph/reach/sets.go index c8c44744..b6de5cc7 100644 --- a/projectgraph/reach/sets.go +++ b/projectgraph/reach/sets.go @@ -452,7 +452,7 @@ func (x *Explorer) Path(ctx context.Context, from string, target Target, maxDept if maxDepth == NoDepthCap { depth = len(a.keys) } - s := a.search(ctx, []int{id}, depth, targets) + s := a.search(ctx, []int{id}, depth, targets, false) if err := ctx.Err(); err != nil { return nil, false, err } diff --git a/projectgraph/reach/source_test.go b/projectgraph/reach/source_test.go index 8b4c53a6..7c600b9f 100644 --- a/projectgraph/reach/source_test.go +++ b/projectgraph/reach/source_test.go @@ -176,3 +176,47 @@ func TestReachRejectsSourcesOnlyWithNoSource(t *testing.T) { _, err := Reach(context.Background(), g, Query{Targets: targetsOf(t, "yaml//load"), SourceMode: SourcesOnly}) assert.ErrorContains(t, err, "source mode SourcesOnly needs at least one source") } + +// loaded has the Go package app of two files, whose b.go holds os.Remove +// as a value, and the package lib, whose c.go holds os.Chdir. run calls a +// function value, and dispatch calls Handle on a value whose type the +// link step does not know. T.Handle of lib calls a function value. The +// Python module other.py holds os.system as a value. +const loaded = ` +file app/a.go indexed go +file app/b.go indexed go +file lib/c.go indexed go +file other.py indexed python +root app/a.go//main +edge app/a.go//main app/a.go//run direct 3 +edge app/b.go os//Remove reference 3 +edge lib/c.go os//Chdir reference 2 +edge other.py os//system reference 1 +unresolved app/a.go//run 5 function-value +unresolved app/a.go//dispatch 7 unknown-receiver Handle +unresolved lib/c.go//T//Handle 4 function-value +edge other.py//f os//getcwd direct 2 +` + +func TestReachFromASourceEntersItsLoadGroup(t *testing.T) { + g := graphOf(t, loaded) + for _, tt := range []struct { + name, source, target string + want Status + path []string + }{ + {name: "a file of the package of the source", source: "app/a.go//run", target: "os//Remove", want: Found, path: []string{"app/a.go//run", "app/b.go", "os//Remove"}}, + {name: "a package that the span of a method enters", source: "app/a.go//dispatch", target: "os//Chdir", want: Uncertain, path: []string{}}, + {name: "a file of no package of the search", source: "app/a.go//run", target: "os//system", want: NotFound, path: []string{}}, + {name: "the file of the source", source: "other.py//f", target: "os//system", want: Found, path: []string{"other.py//f", "other.py", "os//system"}}, + } { + t.Run(tt.name, func(t *testing.T) { + r := reachOne(t, g, Query{Targets: targetsOf(t, tt.target), Sources: sourcesOf(t, tt.source), SourceMode: SourcesOnly}) + assert.Equal(t, tt.want, r.Status) + assert.Equal(t, tt.path, pathKeys(r.Path)) + }) + } + + r := reachOne(t, g, Query{Targets: targetsOf(t, "os//Remove")}) + assert.Equal(t, NotFound, r.Status, "a search from the roots of kind main enters the load code through the roots and the import edges") +} diff --git a/projectgraph/reach/span.go b/projectgraph/reach/span.go index 9a267c2d..9c39c2b9 100644 --- a/projectgraph/reach/span.go +++ b/projectgraph/reach/span.go @@ -160,6 +160,11 @@ func (w *spanWalk) run() { for _, e := range a.out[n.id] { w.add(a.id[a.g.Edges[e].To], n.unit) } + if a.loads { + for _, id := range a.loadsOf(n.id) { + w.add(id, n.unit) + } + } w.meet(n.id) } } diff --git a/test/projectgraph/cases/go/source-package-load/expect.yaml b/test/projectgraph/cases/go/source-package-load/expect.yaml new file mode 100644 index 00000000..8c5bf709 --- /dev/null +++ b/test/projectgraph/cases/go/source-package-load/expect.yaml @@ -0,0 +1,263 @@ +source: safedep:hand +test_paths: ["*_test.go"] +description: >- + The module top level of main.go holds os.Exit in the slice hooks and os.Getwd in the field Hook of + defaults. run calls hooks[0], and runHook calls the field Hook of its parameter. impl.go holds os.Remove in + the map table, and T.Handle calls table["rm"]. lib.Serve calls Handle on a value of an interface, and lib + does not import impl. extra.go holds os.Chdir, and Run calls steps[0]. The test file of extra holds + os.RemoveAll. A call of a function value concerns no target, because the search reaches the code that + holds the value. The program runs the module top level of each file of a package before any code of the + package. So a search from a source enters the module top levels of each package that it enters, and each + target is found from each source. idle calls nothing, and os.Exit is found from it too, because the + program loads main.go before idle runs. A test file loads only with the code of a test, so os.RemoveAll + is not found from Run, and is found from TestRun. The roots of kind main do not reach extra, so os.Chdir + is found only with the tests, through the test root of extra. +roots: + - key: extra/extra_test.go + kind: test + rule: test.files + - key: extra/extra_test.go//TestRun + kind: test + rule: test.files + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +exact_edges: true +edges: + - from: extra/extra.go + to: os//Chdir + kind: reference + confidence: low + site: extra/extra.go:5 + - from: extra/extra_test.go + to: os//RemoveAll + kind: reference + confidence: low + site: extra/extra_test.go:8 + - from: extra/extra_test.go//TestRun + to: extra/extra.go//Run + kind: direct + confidence: high + site: extra/extra_test.go:11 + - from: impl/impl.go + to: os//Remove + kind: reference + confidence: low + site: impl/impl.go:5 + - from: lib/lib.go//Serve + to: impl/impl.go//T//Handle + kind: cha + confidence: medium + site: lib/lib.go:8 + - from: main.go + to: impl/impl.go + kind: import + confidence: high + site: main.go:6 + - from: main.go + to: os//Exit + kind: reference + confidence: low + site: main.go:10 + - from: main.go + to: os//Getwd + kind: reference + confidence: low + site: main.go:16 + - from: main.go//main + to: main.go//run + kind: direct + confidence: high + site: main.go:29 + - from: main.go//main + to: main.go//runHook + kind: direct + confidence: high + site: main.go:30 + - from: main.go//main + to: lib/lib.go//Serve + kind: direct + confidence: high + site: main.go:31 +unresolved: + - caller: lib/lib.go//Serve + reason: no-dependency-source + callback: true + - caller: main.go//runHook + reason: no-dependency-source + callback: true + - caller: os//Chdir + reason: no-dependency-source + callback: true + - caller: os//Exit + 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//RemoveAll + reason: no-dependency-source + callback: true + - caller: extra/extra.go//Run + site: extra/extra.go:8 + reason: function-value + - caller: extra/extra_test.go//TestRun + site: extra/extra_test.go:12 + reason: function-value + - caller: impl/impl.go//T//Handle + site: impl/impl.go:10 + reason: function-value + - caller: lib/lib.go//Serve + site: lib/lib.go:8 + reason: no-definition + method: Handle + - caller: main.go//run + site: main.go:19 + reason: function-value + - caller: main.go//runHook + site: main.go:23 + reason: unknown-receiver + method: Hook +results: + - target: os//Exit + sources: [main.go//run] + sources_only: true + status: found + path: + - key: main.go//run + root: query + - key: main.go + site: main.go + kind: import + confidence: high + - key: os//Exit + site: main.go:10 + kind: reference + confidence: low + call_sites: + - caller: main.go + site: main.go:10 + reached: true + - target: os//Getwd + sources: [main.go//runHook] + sources_only: true + status: found + path: + - key: main.go//runHook + - key: main.go + site: main.go + kind: import + confidence: high + - key: os//Getwd + site: main.go:16 + kind: reference + confidence: low + call_sites: + - caller: main.go + site: main.go:16 + reached: true + - target: os//Remove + sources: [lib/lib.go//Serve] + sources_only: true + status: found + path: + - key: lib/lib.go//Serve + - key: impl/impl.go//T//Handle + site: lib/lib.go:8 + kind: cha + confidence: medium + - key: impl/impl.go + site: impl/impl.go + kind: import + confidence: high + - key: os//Remove + site: impl/impl.go:5 + kind: reference + confidence: low + call_sites: + - caller: impl/impl.go + site: impl/impl.go:5 + reached: true + - target: os//Exit + sources: [main.go//idle] + sources_only: true + status: found + path: + - key: main.go//idle + - key: main.go + site: main.go + kind: import + confidence: high + - key: os//Exit + site: main.go:10 + kind: reference + confidence: low + call_sites: + - caller: main.go + site: main.go:10 + reached: true + - target: os//RemoveAll + sources: [extra/extra.go//Run] + sources_only: true + status: not-found + call_sites: + - caller: extra/extra_test.go + site: extra/extra_test.go:8 + reached: false + - target: os//RemoveAll + sources: [extra/extra_test.go//TestRun] + sources_only: true + status: found + path: + - key: extra/extra_test.go//TestRun + - key: extra/extra_test.go + site: extra/extra_test.go + kind: import + confidence: high + - key: os//RemoveAll + site: extra/extra_test.go:8 + kind: reference + confidence: low + call_sites: + - caller: extra/extra_test.go + site: extra/extra_test.go:8 + reached: true + - target: os//Chdir + include_tests: true + status: found + path: + - key: extra/extra_test.go + - key: extra/extra.go + site: extra/extra.go + kind: import + confidence: high + - key: os//Chdir + site: extra/extra.go:5 + kind: reference + confidence: low + call_sites: + - caller: extra/extra.go + site: extra/extra.go:5 + reached: true + - target: os//Chdir + status: not-found + test_path: + - key: extra/extra_test.go + - key: extra/extra.go + site: extra/extra.go + kind: import + confidence: high + - key: os//Chdir + site: extra/extra.go:5 + kind: reference + confidence: low + call_sites: + - caller: extra/extra.go + site: extra/extra.go:5 + reached: false diff --git a/test/projectgraph/cases/go/source-package-load/project/extra/extra.go b/test/projectgraph/cases/go/source-package-load/project/extra/extra.go new file mode 100644 index 00000000..2f038377 --- /dev/null +++ b/test/projectgraph/cases/go/source-package-load/project/extra/extra.go @@ -0,0 +1,9 @@ +package extra + +import "os" + +var steps = []func(string) error{os.Chdir} + +func Run() { + steps[0]("/") +} diff --git a/test/projectgraph/cases/go/source-package-load/project/extra/extra_test.go b/test/projectgraph/cases/go/source-package-load/project/extra/extra_test.go new file mode 100644 index 00000000..86b49ac5 --- /dev/null +++ b/test/projectgraph/cases/go/source-package-load/project/extra/extra_test.go @@ -0,0 +1,13 @@ +package extra + +import ( + "os" + "testing" +) + +var cleanups = []func(string) error{os.RemoveAll} + +func TestRun(t *testing.T) { + Run() + cleanups[0]("/tmp/x") +} diff --git a/test/projectgraph/cases/go/source-package-load/project/go.mod b/test/projectgraph/cases/go/source-package-load/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/source-package-load/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/source-package-load/project/impl/impl.go b/test/projectgraph/cases/go/source-package-load/project/impl/impl.go new file mode 100644 index 00000000..64106f6a --- /dev/null +++ b/test/projectgraph/cases/go/source-package-load/project/impl/impl.go @@ -0,0 +1,11 @@ +package impl + +import "os" + +var table = map[string]func(){"rm": os.Remove} + +type T struct{} + +func (T) Handle() { + table["rm"]() +} diff --git a/test/projectgraph/cases/go/source-package-load/project/lib/lib.go b/test/projectgraph/cases/go/source-package-load/project/lib/lib.go new file mode 100644 index 00000000..4d75600c --- /dev/null +++ b/test/projectgraph/cases/go/source-package-load/project/lib/lib.go @@ -0,0 +1,9 @@ +package lib + +type Handler interface { + Handle() +} + +func Serve(h Handler) { + h.Handle() +} diff --git a/test/projectgraph/cases/go/source-package-load/project/main.go b/test/projectgraph/cases/go/source-package-load/project/main.go new file mode 100644 index 00000000..5cdc234b --- /dev/null +++ b/test/projectgraph/cases/go/source-package-load/project/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "os" + + "example.com/app/impl" + "example.com/app/lib" +) + +var hooks = []func(){os.Exit} + +type options struct { + Hook func() +} + +var defaults = options{Hook: os.Getwd} + +func run() { + hooks[0]() +} + +func runHook(o options) { + o.Hook() +} + +func idle() {} + +func main() { + run() + runHook(defaults) + lib.Serve(impl.T{}) +} 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 3da14554..557459b5 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,9 @@ 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. idle calls nothing, so os.Remove is not found from it. + function. 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 @@ -161,7 +163,7 @@ results: site: dot.go:6 - kind: unresolved-call - target: os//Remove - sources: [main.go//idle] + sources: [quiet/quiet.go//Idle] sources_only: true status: not-found call_sites: diff --git a/test/projectgraph/cases/go/stdlib-callback-method-record/project/main.go b/test/projectgraph/cases/go/stdlib-callback-method-record/project/main.go index 084793d0..12243fce 100644 --- a/test/projectgraph/cases/go/stdlib-callback-method-record/project/main.go +++ b/test/projectgraph/cases/go/stdlib-callback-method-record/project/main.go @@ -52,6 +52,4 @@ func viaMethodValue() { serve() } -func idle() {} - func main() {} diff --git a/test/projectgraph/cases/go/stdlib-callback-method-record/project/quiet/quiet.go b/test/projectgraph/cases/go/stdlib-callback-method-record/project/quiet/quiet.go new file mode 100644 index 00000000..cde3ba0c --- /dev/null +++ b/test/projectgraph/cases/go/stdlib-callback-method-record/project/quiet/quiet.go @@ -0,0 +1,3 @@ +package quiet + +func Idle() {} diff --git a/test/projectgraph/cases/javascript/source-module-load/expect.yaml b/test/projectgraph/cases/javascript/source-module-load/expect.yaml new file mode 100644 index 00000000..61d7cbda --- /dev/null +++ b/test/projectgraph/cases/javascript/source-module-load/expect.yaml @@ -0,0 +1,85 @@ +source: safedep:hand +description: >- + The module top level of handlers.js holds child_process.exec in the array hooks, and fs.rmSync in the + property hook of defaults. run calls hooks[0], and runHook calls the property hook of its parameter. The + program runs the module top level of handlers.js before run or runHook can run. So a search from run or + runHook alone enters it, and each target is found through the reference there. Without that step, the + call of o.hook concerns only the targets of the method name hook, and fs.rmSync reads as not-found. +roots: + - key: main.js + kind: manifest + rule: node.package-json +exact_edges: true +edges: + - from: handlers.js + to: child_process//exec + kind: reference + confidence: low + site: handlers.js:4 + - from: handlers.js + to: fs//rmSync + kind: reference + confidence: low + site: handlers.js:5 + - from: main.js + to: handlers.js + kind: import + confidence: high + site: main.js:1 + - from: main.js + to: handlers.js//run + kind: direct + confidence: high + site: main.js:3 + - from: main.js + to: handlers.js//runHook + kind: direct + confidence: high + site: main.js:4 +unresolved: + - caller: handlers.js//run + site: handlers.js:8 + reason: computed-name + - caller: handlers.js//runHook + site: handlers.js:12 + reason: unknown-receiver + method: hook +results: + - target: child_process//exec + sources: [handlers.js//run] + sources_only: true + status: found + path: + - key: handlers.js//run + root: query + - key: handlers.js + site: handlers.js + kind: import + confidence: high + - key: child_process//exec + site: handlers.js:4 + kind: reference + confidence: low + call_sites: + - caller: handlers.js + site: handlers.js:4 + reached: true + - target: fs//rmSync + sources: [handlers.js//runHook] + sources_only: true + status: found + path: + - key: handlers.js//runHook + root: query + - key: handlers.js + site: handlers.js + kind: import + confidence: high + - key: fs//rmSync + site: handlers.js:5 + kind: reference + confidence: low + call_sites: + - caller: handlers.js + site: handlers.js:5 + reached: true diff --git a/test/projectgraph/cases/javascript/source-module-load/project/handlers.js b/test/projectgraph/cases/javascript/source-module-load/project/handlers.js new file mode 100644 index 00000000..be875f66 --- /dev/null +++ b/test/projectgraph/cases/javascript/source-module-load/project/handlers.js @@ -0,0 +1,15 @@ +const child_process = require('child_process'); +const fs = require('fs'); + +const hooks = [child_process.exec]; +const defaults = { hook: fs.rmSync }; + +function run(cmd) { + hooks[0](cmd); +} + +function runHook(o = defaults) { + o.hook('/tmp/x'); +} + +module.exports = { run, runHook }; diff --git a/test/projectgraph/cases/javascript/source-module-load/project/main.js b/test/projectgraph/cases/javascript/source-module-load/project/main.js new file mode 100644 index 00000000..11e31664 --- /dev/null +++ b/test/projectgraph/cases/javascript/source-module-load/project/main.js @@ -0,0 +1,4 @@ +const { run, runHook } = require('./handlers'); + +run('ls'); +runHook(); diff --git a/test/projectgraph/cases/javascript/source-module-load/project/package.json b/test/projectgraph/cases/javascript/source-module-load/project/package.json new file mode 100644 index 00000000..03f3cd96 --- /dev/null +++ b/test/projectgraph/cases/javascript/source-module-load/project/package.json @@ -0,0 +1 @@ +{"name": "app", "version": "1.0.0", "main": "main.js"} diff --git a/test/projectgraph/cases/python/source-module-load/expect.yaml b/test/projectgraph/cases/python/source-module-load/expect.yaml new file mode 100644 index 00000000..0d4171f7 --- /dev/null +++ b/test/projectgraph/cases/python/source-module-load/expect.yaml @@ -0,0 +1,100 @@ +source: safedep:hand +description: >- + The module top level of handlers.py holds os.system in the list HOOKS, and os.getcwd in the attribute hook + of DEFAULTS. run calls HOOKS[0], and run_hook calls the attribute hook of its parameter. The program runs + the module top level of handlers.py before run or run_hook can run. So a search from run or run_hook + alone enters it, and each target is found through the reference there. Without that step, the call of + o.hook concerns only the targets of the method name hook, and os.getcwd reads as not-found. +roots: + - key: main.py + kind: manifest + rule: procfile +exact_edges: true +edges: + - from: handlers.py + to: os//system + kind: reference + confidence: low + site: handlers.py:9 + - from: handlers.py + to: handlers.py//Options + kind: direct + confidence: high + site: handlers.py:10 + - from: handlers.py + to: os//getcwd + kind: reference + confidence: low + site: handlers.py:10 + - from: handlers.py//Options + to: handlers.py//Options//__init__ + kind: direct + confidence: high + site: handlers.py:5 + - from: handlers.py//run_hook + to: handlers.py//Options + kind: reference + confidence: low + site: handlers.py:17 + - from: main.py + to: handlers.py + kind: import + confidence: high + site: main.py:1 + - from: main.py + to: handlers.py//run + kind: direct + confidence: high + site: main.py:3 + - from: main.py + to: handlers.py//run_hook + kind: direct + confidence: high + site: main.py:4 +unresolved: + - caller: handlers.py//run + site: handlers.py:14 + reason: computed-name + - caller: handlers.py//run_hook + site: handlers.py:18 + reason: unknown-receiver + method: hook +results: + - target: os//system + sources: [handlers.py//run] + sources_only: true + status: found + path: + - key: handlers.py//run + root: query + - key: handlers.py + site: handlers.py + kind: import + confidence: high + - key: os//system + site: handlers.py:9 + kind: reference + confidence: low + call_sites: + - caller: handlers.py + site: handlers.py:9 + reached: true + - target: os//getcwd + sources: [handlers.py//run_hook] + sources_only: true + status: found + path: + - key: handlers.py//run_hook + root: query + - key: handlers.py + site: handlers.py + kind: import + confidence: high + - key: os//getcwd + site: handlers.py:10 + kind: reference + confidence: low + call_sites: + - caller: handlers.py + site: handlers.py:10 + reached: true diff --git a/test/projectgraph/cases/python/source-module-load/project/Procfile b/test/projectgraph/cases/python/source-module-load/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/source-module-load/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/source-module-load/project/handlers.py b/test/projectgraph/cases/python/source-module-load/project/handlers.py new file mode 100644 index 00000000..2a089bf1 --- /dev/null +++ b/test/projectgraph/cases/python/source-module-load/project/handlers.py @@ -0,0 +1,18 @@ +import os + + +class Options: + def __init__(self, hook): + self.hook = hook + + +HOOKS = [os.system] +DEFAULTS = Options(os.getcwd) + + +def run(cmd): + HOOKS[0](cmd) + + +def run_hook(o=DEFAULTS): + o.hook() diff --git a/test/projectgraph/cases/python/source-module-load/project/main.py b/test/projectgraph/cases/python/source-module-load/project/main.py new file mode 100644 index 00000000..6a7fe4a8 --- /dev/null +++ b/test/projectgraph/cases/python/source-module-load/project/main.py @@ -0,0 +1,4 @@ +from handlers import run, run_hook + +run("ls") +run_hook() From 4e81ceedd39af5e98630e4ba14c5992d1e047d2e Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 11:58:52 +0000 Subject: [PATCH 38/58] test(projectgraph): update the hash rows for the review fixes The review fixes change graphs, results and summaries of the cases. The versions of this branch already moved to fileindex 16, projectgraph 18, reach 5 and summary 3, and no release holds them yet. So the rows of these versions take the new hashes 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 535b89ca..8ed986da 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -32,7 +32,7 @@ var graphHashes = map[string]string{ "14/15": "bbc708aba767ccf554c75a7e06816abda3fc1804ad6d0876ad8ce9575f4c3a88", "14/16": "823b1f82c4aed8ba27d19275505c8d1dee755086be48fcda64d323f55b4c62a5", "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", - "16/18": "22947eb8bae556a99ad7131fa93dac167fc7869e4ec4e77fef11ff0cf958f577", + "16/18": "917c95d5abe3d1b0b6d2fd07825fd34892e2b5a79311a436ccece8d1bd63ebc7", } // resultHashes records the hash of the reach results of every case for @@ -49,7 +49,7 @@ var resultHashes = map[string]string{ "14/15/3": "77dfd0ea8ab71869ef612d41b8c2e46852d4fb67a49458cb645321692af77ea0", "14/16/3": "13b6eecb686ddec3c2ba470fc9752ea66cff445d5e18e0772a71b15ae7fe1c9c", "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", - "16/18/5": "d8c8cd0e84f656684b03781de7e98100a19649d38da4263570b9387cbac55df7", + "16/18/5": "debdac29066cb2544a07cde77c546924bd1c26528a752871905b2ef5ebd8ea72", } // summaryHashes records the hash of the golden summaries of the cases for @@ -67,7 +67,7 @@ var summaryHashes = map[string]string{ "14/15/2": "5969f3cda15b666cbd902bbc2db953cbdef8c0ce48d5e77446cf243cdd0bb0e7", "14/16/2": "fb678202978212aae932e50a56a0d3dc7f0f794ce908988bf605ce9a6e04bc51", "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", - "16/18/3": "450c8ffebb71aba578e63e27a995927c57691a4c722f389ce752aee384bbdc20", + "16/18/3": "cdac0f560211526c43b8c9141a3bb92663b36b6b555269cf4cfb9d19c7879436", } func TestGraphHashFollowsVersion(t *testing.T) { From ce5470dab0bea6279221fe437f28041b65c59dd6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 12:09:35 +0000 Subject: [PATCH 39/58] docs(projectgraph): report the method measure of the reach MVP branch The report held the numbers of the measure branch before the other fixes merged. This section gives the numbers of the whole branch in both modes, next to main, and the agreement with govulncheck. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- test/projectgraph/report.md | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/projectgraph/report.md b/test/projectgraph/report.md index e78a16de..8151e980 100644 --- a/test/projectgraph/report.md +++ b/test/projectgraph/report.md @@ -1470,3 +1470,44 @@ as the table above. amqp091-go in go-rabbitmq, as `Channel.NotifyClose`. The advisory has no symbol list and gives no target, so the comparison leaves out its findings. + +### The reach MVP branch + +The rows come from two runs of the branch of the reach MVP (FOU-556, +FOU-557, FOU-558, FOU-560, FOU-561, FOU-562, FOU-563, FOU-575, FOU-576 and +FOU-578, with the fixes of two reviews), on 2026-11-14, on the machine +above. The run "without" sets `PROJECTGRAPH_METHOD_NO_STD=1`, as vet runs +today. `found` counts `found` and `found/low`. The main rows come from the +same corpus on origin/main at 39cf1e2, without the unit. + +| Language | Kind | Run | Targets | found | not-found | no-such-target | no-dependency-source | unknown-receiver | no-definition | interface-receiver | ambiguous | budget | computed-name | +| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | +| Go | function | main | 256 | 51 | 0 | 0 | 2 | 3 | 5 | 0 | 106 | 39 | 50 | +| Go | function | without | 256 | 58 | 0 | 0 | 193 | 5 | 0 | 0 | 0 | 0 | 0 | +| Go | function | with | 256 | 59 | 0 | 0 | 187 | 8 | 1 | 0 | 1 | 0 | 0 | +| Go | method | main | 1298 | 331 | 0 | 0 | 2 | 154 | 71 | 2 | 416 | 109 | 213 | +| Go | method | without | 1298 | 343 | 0 | 7 | 776 | 156 | 15 | 1 | 0 | 0 | 0 | +| Go | method | with | 1298 | 384 | 0 | 0 | 432 | 253 | 184 | 14 | 31 | 0 | 0 | +| Rust | function | main | 13 | 4 | 0 | 0 | 0 | 0 | 9 | 0 | 0 | 0 | 0 | +| Rust | function | branch | 13 | 5 | 0 | 0 | 0 | 7 | 1 | 0 | 0 | 0 | 0 | +| Rust | method | main | 38 | 16 | 0 | 0 | 0 | 0 | 13 | 0 | 0 | 9 | 0 | +| Rust | method | branch | 38 | 16 | 0 | 0 | 0 | 12 | 1 | 0 | 0 | 9 | 0 | + +| Run | govulncheck reaches | reach found | reach uncertain | false negative | govulncheck does not reach | reach not-found or no-such-target | reach found (review) | +| --- | --- | --- | --- | --- | --- | --- | --- | +| without | 115 | 71 | 44 | 0 | 1439 | 7 | 330 | +| with | 115 | 71 | 44 | 0 | 1439 | 0 | 372 | + +- No target is a false negative in either run. A measure run fails on one. +- Reach finds 71 of the 115 targets that govulncheck reaches. Main found 60. +- The global causes of main are gone: `ambiguous`, `budget` and + `computed-name`. Most of their targets now read `no-dependency-source`. + That record marks code outside the graph that can call back into it: a + module with no unit, or the standard library with no unit. The corpus + gives a unit only to the modules that an advisory names, so most + modules of a build have no unit. A consumer that gives a unit to each + module with source on the disk, and to GOROOT, removes most of these + records. +- Python does not change. The Rust `no-definition` records of main fall + from 22 to 2. The rest are `unknown-receiver` records of calls that the + link step does not type yet, as a turbofish or a `::m` call. From 45ac9a0e2414ec0956daaa648c44dbbd9c90d05a Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 12:39:36 +0000 Subject: [PATCH 40/58] fix(indexer): keep a Rust declared type only where the root means it (FOU-578) The link step resolves the declared type of a Rust parameter or let at the root of the file. FOU-578 made such a type bind a receiver that the call graph does not type. A type alias, a use or an item of the function, of a block or of an inline module can declare the same name as a type of the root. So in fn run() { type Local = dep::Thing; let t: Local = dep::new_thing(); t.inspect(); } with a struct Local at the root, the call took src/main.rs//Local//inspect with no record. dep::Thing::inspect then read as not-found. The index now 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 use super::* in a module of the root. self and super in an inline module name other modules. A type parameter with a bound still names its bound. A call on a name with no kept type keeps the target of the call graph or its record. The receiver rows add the inputs fn-local-alias, fn-local-use and mod-let of the review, and forms of a block glob, an inline module use, an alias of an outer function and a type parameter of an impl. Each row answered not-found before the fix. Method measure, Rust, before and after, in both modes (std and no std): function targets=13 found=3 found/low=2 uncertain/no-definition=1 uncertain/unknown-receiver=7 method targets=38 found/low=16 uncertain/budget=9 uncertain/no-definition=1 uncertain/unknown-receiver=12 other targets=5 uncertain/unknown-receiver=5 No count of any language changes. govulncheck false-negative=0 in both modes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 15 ++++ plugin/indexer/rust.go | 132 +++++++++++++++++++++++++++- plugin/indexer/rust_test.go | 48 ++++++++++ test/projectgraph/receivers_test.go | 54 ++++++++++++ 4 files changed, 247 insertions(+), 2 deletions(-) diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 0a8ab07c..2349c6d6 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -332,6 +332,21 @@ 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. 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 + `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. - `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 565a8082..8b61dba8 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -269,7 +269,7 @@ func rustSignature(p *projection, def *fileindex.Definition, node *sitter.Node) continue } name := rustBoundName(param.ChildByFieldName("pattern")) - if typ := p.rustTypeName(param.ChildByFieldName("type"), bounds); typ != "" && name != nil { + if typ := p.rustDeclaredType(param, bounds); typ != "" && name != nil { def.Params = append(def.Params, fileindex.Typed{Name: p.text(name), Type: typ}) } } @@ -309,13 +309,141 @@ func (p *projection) rustLocals(body *sitter.Node, bounds map[string]string) []f return } name := rustBoundName(node.ChildByFieldName("pattern")) - if typ := p.rustTypeName(node.ChildByFieldName("type"), bounds); typ != "" && name != nil { + if typ := p.rustDeclaredType(node, bounds); typ != "" && name != nil { locals = append(locals, fileindex.Typed{Name: p.text(name), Type: typ}) } }) return locals } +// 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. +func (p *projection) rustDeclaredType(binder *sitter.Node, bounds map[string]string) string { + typ := p.rustTypeName(binder.ChildByFieldName("type"), bounds) + first, _, _ := strings.Cut(typ, "::") + if typ == "" || p.rustScopeDeclares(binder, first) { + return "" + } + return typ +} + +// 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. +func (p *projection) rustScopeDeclares(node *sitter.Node, name string) bool { + for scope := node.Parent(); scope != nil; scope = scope.Parent() { + switch scope.Type() { + case "block": + if p.rustItemsDeclare(scope, name, false) { + return true + } + case "declaration_list": + mod := scope.Parent() + if mod == nil || mod.Type() != "mod_item" { + continue + } + ofRoot := mod.Parent() != nil && mod.Parent().Type() == "source_file" + if name == "self" || name == "super" || p.rustItemsDeclare(scope, name, ofRoot) { + return true + } + case "function_item", "impl_item", "trait_item": + if slices.ContainsFunc(ts.NamedChildren(scope.ChildByFieldName("type_parameters")), func(param *sitter.Node) bool { + return p.rustTypeParameter(param) == name + }) { + return true + } + } + } + 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 +// names of the root. +func (p *projection) rustItemsDeclare(list *sitter.Node, name string, ofRoot bool) bool { + for _, item := range ts.NamedChildren(list) { + switch item.Type() { + case "struct_item", "enum_item", "union_item", "type_item", "trait_item", "mod_item": + if p.text(item.ChildByFieldName("name")) == name { + return true + } + case "extern_crate_declaration": + bound := item.ChildByFieldName("alias") + if bound == nil { + bound = item.ChildByFieldName("name") + } + if p.text(bound) == name { + return true + } + case "use_declaration": + tree := item.ChildByFieldName("argument") + names, glob := p.rustUseNames(tree, nil) + readsRoot := ofRoot && p.text(tree) == "super::*" + if slices.Contains(names, name) || glob && !readsRoot { + return true + } + } + } + return false +} + +// rustUseNames returns the names that a use tree binds, and whether it +// holds a glob. The prefix is the path of the use list that holds the +// tree, whose last name self binds, as io for std::io::{self}. +func (p *projection) rustUseNames(tree, prefix *sitter.Node) ([]string, bool) { + if tree == nil { + return nil, false + } + switch tree.Type() { + case "use_as_clause": + return []string{p.text(tree.ChildByFieldName("alias"))}, false + case "use_wildcard": + return nil, true + case "scoped_use_list": + return p.rustUseNames(tree.ChildByFieldName("list"), tree.ChildByFieldName("path")) + case "use_list": + var names []string + glob := false + for _, child := range ts.NamedChildren(tree) { + n, g := p.rustUseNames(child, prefix) + names, glob = append(names, n...), glob || g + } + return names, glob + case "self": + if prefix == nil { + return nil, false + } + return []string{rustLastName(p.text(prefix))}, false + case "identifier", "scoped_identifier": + return []string{rustLastName(p.text(tree))}, false + } + return nil, false +} + +// rustTypeParameter returns the name of a type parameter, as T for T, +// T: Store or T = u8, and "" for a lifetime or a const parameter. +func (p *projection) rustTypeParameter(param *sitter.Node) string { + switch param.Type() { + case "optional_type_parameter": + return p.rustTypeParameter(param.ChildByFieldName("name")) + case "constrained_type_parameter": + return p.text(param.ChildByFieldName("left")) + case "type_identifier": + return p.text(param) + } + return "" +} + // rustLastName returns the last name of a path, as Result for // std::io::Result. func rustLastName(path string) string { diff --git a/plugin/indexer/rust_test.go b/plugin/indexer/rust_test.go index 9903d83e..1ca4b428 100644 --- a/plugin/indexer/rust_test.go +++ b/plugin/indexer/rust_test.go @@ -114,3 +114,51 @@ func TestFileIndexRustTypeAlias(t *testing.T) { assert.Equal(t, "crate::store::Store", store.Alias) assert.False(t, store.Exported) } + +func TestFileIndexRustDeclaredTypes(t *testing.T) { + source := strings.Join([]string{ + "fn plain() { let t: Store = make(); }", + "fn sibling() { { use dep::Thing as Store; } let t: Store = make(); }", + "fn alias() { type Store = u8; let t: Store = make(); }", + "fn after() { let t: Store = make(); use dep::Thing as Store; }", + "fn outer() { struct Store; { let t: Store = make(); } }", + "fn listed() { use dep::{store::{self}, Other}; let t: store::Store = make(); }", + "fn globbed() { use dep::*; let t: Store = make(); }", + "fn crated() { extern crate other as dep; let t: dep::Store = make(); }", + "fn generic(s: S) { let t: S = s; }", + "fn bounded(s: S) {}", + "struct Holder(S);", + "impl Holder { fn go(&self, s: S) { let t: Self = make(); } }", + "mod up { use super::*; fn run() { let t: Store = make(); } }", + "mod outer { mod down { use super::*; fn run() { let t: Store = make(); } } }", + "mod own { fn run() { let t: self::Store = make(); } }", + }, "\n") + "\n" + ix := indexSource(t, core.LanguageCodeRust, "main.rs", source) + + store := []fileindex.Typed{{Name: "t", Type: "Store"}} + tests := []struct { + key string + locals []fileindex.Typed + params []fileindex.Typed + }{ + {key: "main.rs//plain", locals: store}, + {key: "main.rs//sibling", locals: store}, + {key: "main.rs//alias"}, + {key: "main.rs//after"}, + {key: "main.rs//outer"}, + {key: "main.rs//listed"}, + {key: "main.rs//globbed"}, + {key: "main.rs//crated"}, + {key: "main.rs//generic"}, + {key: "main.rs//bounded", params: []fileindex.Typed{{Name: "s", Type: "Store"}}}, + {key: "main.rs//Holder//go", locals: []fileindex.Typed{{Name: "t", Type: "Holder"}}}, + {key: "main.rs//up//run", locals: store}, + {key: "main.rs//outer//down//run"}, + {key: "main.rs//own//run"}, + } + for _, tt := range tests { + def := definitionOf(t, ix, tt.key) + assert.Equal(t, tt.locals, def.Locals, tt.key) + assert.Equal(t, tt.params, def.Params, tt.key) + } +} diff --git a/test/projectgraph/receivers_test.go b/test/projectgraph/receivers_test.go index 65ed3402..47be09f3 100644 --- a/test/projectgraph/receivers_test.go +++ b/test/projectgraph/receivers_test.go @@ -318,6 +318,60 @@ var rustReceiverRows = []receiverRow{ target: "dep//Thing//inspect", want: "uncertain/unknown-receiver", }, + { + name: "typed let of a type alias of the function", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run() {\n type Local = dep::Thing;\n let t: Local = dep::new_thing();\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "typed let of a use of the function", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run() {\n use dep::Thing as Local;\n let t: Local = dep::new_thing();\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "typed let of a type alias of an inline module", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nmod inner {\n pub type Local = dep::Thing;\n pub fn go() {\n let t: Local = dep::new_thing();\n t.inspect();\n }\n}\n\nfn run() {\n inner::go();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + name: "typed let of a glob use of a block", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run() {\n mod m {\n pub use dep::Thing as Local;\n }\n use m::*;\n let t: Local = dep::new_thing();\n t.inspect();\n}", + target: "dep//Thing//inspect", + want: "found", + }, + { + 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", + }, + { + name: "parameter of a type alias of the outer function", + source: "struct Local;\nimpl Local {\n fn inspect(&self) {}\n}\n\nfn run() {\n type Local = dep::Thing;\n fn inner(t: Local) {\n t.inspect();\n }\n inner(dep::new_thing());\n}", + target: "dep//Thing//inspect", + want: "uncertain/unknown-receiver", + }, + { + name: "parameter of a type parameter of the impl", + source: "struct Local;\nimpl Local {\n fn run(&self) {}\n}\n\nstruct Holder(T);\nimpl Holder {\n fn go(t: Local) {\n t.run();\n }\n}\n\nfn run() {\n Holder::>::go(dep::new_thing());\n}", + target: "dep//Thing//run", + want: "uncertain/unknown-receiver", + }, + { + name: "typed let of a type parameter of the impl", + source: "struct Local;\nimpl Local {\n fn run(&self) {}\n}\n\nstruct Holder(T);\nimpl Holder {\n fn go(t: Local) {\n let u: Local = t;\n u.run();\n }\n}\n\nfn run() {\n Holder::>::go(dep::new_thing());\n}", + target: "dep//Thing//run", + want: "uncertain/unknown-receiver", + }, + { + name: "boxed trait object in a module that reads the root", + source: "mod inner {\n use super::*;\n pub fn go() {\n let d: Box = Box::new(Thing::::new());\n d.run();\n }\n}\n\nfn run() {\n inner::go();\n}", + target: "dep//Thing//run", + want: "found", + }, } // pythonReceiverRows are the forms of a Python receiver. A type hint is From ab6e59de042eb46b375d8ee209e8152d36cbbdfa Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 12:40:39 +0000 Subject: [PATCH 41/58] fix(reach): count a call of a function value in a search with a source (C1, FOU-560) A Go function-value record concerned no target in every search. The rule holds only when the search reaches each code that can store a function value. A search from the roots that the rules mark does. A search with a source does not, in either source mode. The program can run code before the source that no root reaches, as the init of a package that only main imports and that fills a registry, or a setup that main calls before serve. The load step enters only the load code of the packages that the search reaches, so it does not find that code. So registry.D2 with handlers["rm"]("x") and serve with fs[0]("x") gave os//Remove not-found from a source, where main gave uncertain. A search whose roots hold a root of kind query now counts a function-value record for every target, as a computed-name record does. A summary counts it for every target too. A search of any kind reads a summary, and the value can come from the project, from another unit, or from a package of the unit that the symbol does not import. summarytest now checks a summary against a search from the symbol as a root of kind query, which has the same rule. The load step stays. It finds a call in the load code of the package of a source, and a value that the load code stores in an attribute, as python/source-module-load shows. The case go/source-function-value holds both inputs of the review. It gave not-found for four results before the change. The summaries of go/dependency-callback and go/dependency-function-table now hold their function-value records. The source Run of go/source-package-load gives os.RemoveAll uncertain, because Run calls steps[0]. A search from the roots keeps the rule, so the method measure does not move. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 122 ++++++---- projectgraph/profile.go | 2 +- projectgraph/reach/reach.go | 18 +- projectgraph/reach/source_test.go | 41 +++- projectgraph/reach/uncertainty.go | 10 +- .../summary/summarytest/summarytest.go | 7 +- .../cases/go/dependency-callback/expect.yaml | 3 +- .../go_github.com_acme_srv_v1.0.0.json | 19 +- .../go/dependency-function-table/expect.yaml | 5 +- .../go_github.com_acme_a_v1.0.0.json | 15 ++ .../go/source-function-value/expect.yaml | 215 ++++++++++++++++++ .../go/source-function-value/project/go.mod | 3 + .../go/source-function-value/project/main.go | 25 ++ .../project/plugins/plugins.go | 15 ++ .../project/registry/registry.go | 15 ++ .../cases/go/source-package-load/expect.yaml | 17 +- 16 files changed, 464 insertions(+), 68 deletions(-) create mode 100644 test/projectgraph/cases/go/source-function-value/expect.yaml create mode 100644 test/projectgraph/cases/go/source-function-value/project/go.mod create mode 100644 test/projectgraph/cases/go/source-function-value/project/main.go create mode 100644 test/projectgraph/cases/go/source-function-value/project/plugins/plugins.go create mode 100644 test/projectgraph/cases/go/source-function-value/project/registry/registry.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 0a8ab07c..5d3002f3 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -228,8 +228,10 @@ reason: `computed-name`, `dynamic-import`, `unknown-receiver`, `no-definition`, `ambiguous`, `limit`, `interface-receiver`, `trait-receiver`, `no-dependency-source`, `function-value` or `markup`. The site keeps the method name of the call, so the reach query can tell which targets it concerns. A `function-value` -record concerns no target. [A call of a function value](#a-call-of-a-function-value) -gives the reason. +record concerns no target in a search from the roots that the rules +mark, and every target in a search with a source and in a summary. +[A call of a function value](#a-call-of-a-function-value) gives the +reason. A site with more than 20 candidates of its method name, as `render` in a Django project, is `ambiguous`. The site gets no edge, so the search cannot @@ -554,7 +556,11 @@ the sources do. | `reach.SourcesOnly` | The sources alone. The root fallback does not apply, so no result carries a `root-fallback` uncertainty. It needs at least one source | A search from a source also enters the load code of each package whose -code it reaches, because the program runs that code before the source. See +code it reaches, because the program runs that code before the source. The +caller of a source can also run other code before it that no root +reaches, and that code can store a function value. So in a search with a +source, in either mode, a call of a function value concerns every +target. See [A call of a function value](#a-call-of-a-function-value). A source must name a first-party node of the graph. A key of no node, an @@ -751,9 +757,9 @@ that its scope concerns, so `Status` applies the same rule with no graph. A Go call of a function value, as `BackoffLinearWithJitter(d, j)(attempt)` or `route.Handlers[0](c)`, is a record with the reason `function-value`. -The record concerns no target. This is sound because the search reaches -each function that such a call can run. A Go function value is one of -these: +In a search from the roots that the rules mark, the record concerns no +target. This is sound because that search reaches each function that such +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 @@ -781,7 +787,7 @@ these: package is in a unit of dependency code. An import edge joins the importer to the module top level, and a search from `main` reaches it. An external key also has an import edge to each module top level of its - package that runs code at load, so the search of a summary reaches it. + package that runs code at load. - A value that reflection gives, as `reflect.Value.Call`, `reflect.Value.MethodByName` or `reflect.Value.Method`. No reference leads to such a function. A call or a reference of the methods `Call`, @@ -796,44 +802,80 @@ the value, and the call has the record that [Code outside the graph](#code-outside-the-graph) gives. The search reaches that record where it reaches the code that makes the value. -The argument needs a search that reaches the module top level of each -package whose code it reaches. A search from the roots of kind `main` -does. The module top level of each file of the package of `main` is a -root, and an import edge joins a module top level to each module top level -of a package that it imports and that runs code at load. A search from a -source, or from a test root, does not. The program runs the load code of -a package before the source, so no path from the source leads to it. - -So a query with a source, or with `IncludeTests`, adds a load step, and -the search of a test path adds it too. Each node that the search reaches -enters the module top level of each file of its load group, as -`Graph.LoadGroup` gives it. The load group is the -package of the file in a language whose program loads a whole package -before any code of the package runs, as Go. In another language it is the -file alone, as a Python or a JavaScript module, which runs its top level -before any of its functions can run. A test file is in the load group only -of the code of a test. The span of a method name, a unit or a callback -takes the same step, so the code that a candidate of a method name holds -in its package counts too. A hop of the step has the resolver `load`, and -`reach.PathString` writes it as `main.go (load)`. - -The step only adds nodes to a search, so it turns no answer into -`not-found`. It costs precision. A source that calls nothing still reaches -each function value of the load code of its package, as `idle` does in the -case `go/source-package-load`. A search from the roots alone does not take -the step, so its results do not change. The summary of a unit does not -take it either. A summary answers for a search from one symbol of a unit, -and the import edges of an external key give that search the load code of -its Go package. The cases `go/source-package-load`, -`python/source-module-load` and `javascript/source-module-load` hold a -function value in the load code and call it from a source. +The argument holds only for a search that starts from the roots that the +rules mark. Those roots are the entries of the program: `main` with the +module top level of each file of its package, a test root, or, with the +root fallback, each module top level and each exported symbol. Code that +stores a function value runs only after an entry, so the search reaches +it. An import edge joins a module top level to each module top level of a +package that it imports and that runs code at load, so the search also +reaches the load code of each package that the program loads. + +A search with a source does not have the argument, in either source mode. +The caller of a source can run code before the source that no root +reaches, and that code can store a function value: the init of a package +that only `main` imports and that fills a registry, or a `setup` that +`main` calls before `serve`. With `SourcesAndRoots` the source is an entry +that the rules missed, so its caller can be code that the rules missed +too. So in a search with a source, a `function-value` record concerns +every target, as a `computed-name` record does. The case +`go/source-function-value` holds both forms of the store. + +A summary does not have the argument either. A summary is a search from +one symbol of a unit, and a search of any kind reads it: a search from the +roots or from a source, of a project that the summary does not see. The +value can come from that project, from another unit, or from a package of +the unit that the symbol does not import, as a registry that the init of +another package fills. So a summary counts a `function-value` record for +every target. `summarytest` checks a summary against +a reach query of the unit graph from the symbol as a root of kind `query`, +which has the same rule. + +A query with a source, or with `IncludeTests`, adds a load step, and the +search of a test path adds it too. The program runs the load code of a +package before the source or the test, and no path from the source or the +test root leads to it. Each node that the search reaches enters the module +top level of each file of its load group, as `Graph.LoadGroup` gives it. +The load group is the package of the file in a language whose program +loads a whole package before any code of the package runs, as Go. In +another language it is the file alone, as a Python or a JavaScript module, +which runs its top level before any of its functions can run. A test file +is in the load group only of the code of a test. The span of a method +name, a unit or a callback takes the same step, so the code that a +candidate of a method name holds in its package counts too. A hop of the +step has the resolver `load`, and `reach.PathString` writes it as +`main.go (load)`. + +The step keeps two answers sound. A call in the load code, as `var x = +dep.Init()`, runs before the source, so the search finds its target. A +value that the load code stores in a variable or an attribute is reached +through its reference, also where the call of the value is a record with a +name, as `o.hook()` in the case `python/source-module-load`. The step only +adds nodes to a search, so it turns no answer into `not-found`. It costs +precision. A source that calls nothing still reaches the load code of its +package, as `idle` does in the case `go/source-package-load`. A search from +the roots alone does not take the step, so its results do not change. The +summary of a unit does not take it either. A summary answers for a search +from one symbol of a unit, and the import edges of an external key give +that search the load code of its Go package. The cases +`go/source-package-load`, `python/source-module-load` and +`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`. 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 block, a match arm, a shorthand field or a tuple holds, and a Rust function -returns its tail expression. So a call of a Rust function value stays a -`computed-name` record. +returns its tail expression. So a call of a Rust function value in an +expression, as `v[0](x)`, stays a `computed-name` record. ### Unknown code of a unit of dependency code diff --git a/projectgraph/profile.go b/projectgraph/profile.go index f451712e..976cc30e 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -223,7 +223,7 @@ type profile struct { // functionValues marks a language whose function values the link step // joins to the code that holds them, as Go. A call of a function value // gets the reason function-value in place of computed-name, and it - // concerns no target. A reference to a method that the link step cannot + // concerns no target in a search from the roots. A reference to a method that the link step cannot // name, as h.Handle on an interface, gets a record with the method name. functionValues bool // reflection names the package of the language that can run any diff --git a/projectgraph/reach/reach.go b/projectgraph/reach/reach.go index 1a912a49..d53f0c55 100644 --- a/projectgraph/reach/reach.go +++ b/projectgraph/reach/reach.go @@ -252,6 +252,12 @@ func Reach(ctx context.Context, g *projectgraph.Graph, q Query) ([]Result, error } } } + // The caller of a root of kind query can run code before it that no + // root reaches, as a main that fills a table of handlers and then + // calls the source. That code can store a function value. So only a + // search from the roots that the rules marked reaches each function + // that a call of a value can run. + a.valueEdges = !slices.ContainsFunc(roots, func(r projectgraph.Root) bool { return r.Kind == RootQuery }) kinds := a.rootKinds(roots) s := a.search(ctx, a.ids(roots), depth, targets, a.loads) unc := a.uncertainties(s, depth) @@ -329,7 +335,11 @@ type Explorer struct { a *adjacency } -// NewExplorer builds the adjacency of the graph for the searches of an Explorer. +// NewExplorer builds the adjacency of the graph for the searches of an +// Explorer. A summary is the main user of an Explorer, and a search of any +// kind reads it, from a project that the summary does not see. So the +// searches of an Explorer do not have valueEdges, as a search from a +// source does not. func NewExplorer(g *projectgraph.Graph) *Explorer { return &Explorer{a: newAdjacency(g)} } @@ -370,7 +380,8 @@ func (x *Explorer) Explore(ctx context.Context, from []string, maxDepth int) (*E // of a test file start the span of a method name or of the callback. loads // makes the search of a query and each span enter the load group of each // node that they reach, and loadGroups holds the module top levels of each -// group. +// group. valueEdges marks a search that reaches each code that can store a +// function value, as sources.valueEdges says. type adjacency struct { g *projectgraph.Graph keys []string @@ -385,6 +396,7 @@ type adjacency struct { includeTests bool loads bool + valueEdges bool src *sources loadGroups map[string]*loadGroup @@ -398,7 +410,7 @@ type adjacency struct { // node, read once. func (a *adjacency) sources() *sources { if a.src == nil { - a.src = newSources(a.g) + a.src = newSources(a.g, a.valueEdges) } return a.src } diff --git a/projectgraph/reach/source_test.go b/projectgraph/reach/source_test.go index 7c600b9f..e7192c13 100644 --- a/projectgraph/reach/source_test.go +++ b/projectgraph/reach/source_test.go @@ -178,10 +178,10 @@ func TestReachRejectsSourcesOnlyWithNoSource(t *testing.T) { } // loaded has the Go package app of two files, whose b.go holds os.Remove -// as a value, and the package lib, whose c.go holds os.Chdir. run calls a -// function value, and dispatch calls Handle on a value whose type the -// link step does not know. T.Handle of lib calls a function value. The -// Python module other.py holds os.system as a value. +// as a value, and the package lib, whose c.go holds os.Chdir. run calls +// the field hook of a value, and dispatch calls Handle on a value whose +// type the link step does not know. T.Handle of lib calls the field hook +// too. The Python module other.py holds os.system as a value. const loaded = ` file app/a.go indexed go file app/b.go indexed go @@ -192,9 +192,9 @@ edge app/a.go//main app/a.go//run direct 3 edge app/b.go os//Remove reference 3 edge lib/c.go os//Chdir reference 2 edge other.py os//system reference 1 -unresolved app/a.go//run 5 function-value +unresolved app/a.go//run 5 unknown-receiver hook unresolved app/a.go//dispatch 7 unknown-receiver Handle -unresolved lib/c.go//T//Handle 4 function-value +unresolved lib/c.go//T//Handle 4 unknown-receiver hook edge other.py//f os//getcwd direct 2 ` @@ -220,3 +220,32 @@ func TestReachFromASourceEntersItsLoadGroup(t *testing.T) { r := reachOne(t, g, Query{Targets: targetsOf(t, "os//Remove")}) assert.Equal(t, NotFound, r.Status, "a search from the roots of kind main enters the load code through the roots and the import edges") } + +// TestReachScopesAValueCallOnlyFromTheRoots checks that a call of a +// function value concerns no target in a search from the roots that the +// rules marked. A search with a source and the sets of ExploreAll may not +// reach the code that stores the value, so there it concerns every target. +func TestReachScopesAValueCallOnlyFromTheRoots(t *testing.T) { + const spec = ` +root app/a.go//main +edge app/a.go//main app/a.go//serve direct 3 +edge app/a.go//dead os//Remove direct 16 +` + for _, tt := range []struct{ name, record string }{ + {name: "a call of a function value", record: "unresolved app/a.go//serve 8 function-value"}, + } { + t.Run(tt.name, func(t *testing.T) { + g := graphOf(t, spec+tt.record) + remove := targetsOf(t, "os//Remove") + assert.Equal(t, NotFound, reachOne(t, g, Query{Targets: remove}).Status, "from the roots") + for _, mode := range []SourceMode{SourcesAndRoots, SourcesOnly} { + r := reachOne(t, g, Query{Targets: remove, Sources: sourcesOf(t, "app/a.go//serve"), SourceMode: mode}) + assert.Equal(t, Uncertain, r.Status, "source mode %d", mode) + } + sets, err := NewExplorer(g).ExploreAll(context.Background(), []string{"app/a.go//serve"}) + require.NoError(t, err) + require.Len(t, sets.Sources[0].Uncertainties, 1) + assert.True(t, sets.Uncertainties[sets.Sources[0].Uncertainties[0]].Concerns(remove[0]), "a summary") + }) + } +} diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index 4438146f..d75c0b52 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -60,10 +60,14 @@ type sources struct { // so the search counts it once. callbacks map[string]bool callback *Uncertainty + // valueEdges marks a search that reaches each code that can store a + // function value, so a reference edge leads it to each function that + // a call of a value can run. + valueEdges bool } -func newSources(g *projectgraph.Graph) *sources { - s := &sources{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{}} +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{}} for _, u := range g.Unresolved { if u.Callback { s.callbacks[u.Caller] = true @@ -163,7 +167,7 @@ func (s *sources) at(key string, n *projectgraph.Node) []Uncertainty { for _, r := range s.unresolved[key] { // A call of a function value runs a function that the search // reaches through the code that holds the value. - if r.Reason == fileindex.UnresolvedFunctionValue { + if r.Reason == fileindex.UnresolvedFunctionValue && s.valueEdges { continue } u := Uncertainty{Kind: UnresolvedCall, Key: key, Site: r.Site, Text: fmt.Sprintf("%s (%s)", r.Text, r.Reason)} diff --git a/projectgraph/summary/summarytest/summarytest.go b/projectgraph/summary/summarytest/summarytest.go index a5cc8384..1b622c85 100644 --- a/projectgraph/summary/summarytest/summarytest.go +++ b/projectgraph/summary/summarytest/summarytest.go @@ -18,12 +18,13 @@ import ( // statusOf asks the unit graph from one symbol for the status of every // target, as a reach query of the project would with the symbol as its -// one root. The depth cap is far above the depth of the graph, because a -// summary has none. +// one root. The root has the kind query, because a search of any kind +// reads a summary, as a search from a source does. The depth cap is far +// above the depth of the graph, because a summary has none. func statusOf(t testing.TB, g *projectgraph.Graph, symbol string, targets []reach.Target) map[string]reach.Status { t.Helper() from := *g - from.Roots = []projectgraph.Root{{Key: symbol, Kind: projectgraph.RootMain, Rule: "test"}} + from.Roots = []projectgraph.Root{{Key: symbol, Kind: reach.RootQuery, Rule: "test"}} from.Stats.RootFallback = false results, err := reach.Reach(context.Background(), &from, reach.Query{Targets: targets, MaxDepth: 1 << 20}) require.NoError(t, err) diff --git a/test/projectgraph/cases/go/dependency-callback/expect.yaml b/test/projectgraph/cases/go/dependency-callback/expect.yaml index aabf6d86..89f2d0d4 100644 --- a/test/projectgraph/cases/go/dependency-callback/expect.yaml +++ b/test/projectgraph/cases/go/dependency-callback/expect.yaml @@ -5,7 +5,8 @@ description: >- a value of the interface Handler, and Bind calls the method value c.Close on a value of the interface Closer. A function that main passes is a reference of main, and the calls of a literal belong to main, so os.Getwd and os.Getenv are found. The call fs[0]() in Each is a function-value record that concerns no - target. The call h.Handle() in Serve has no implementation in the graph, so it is an interface-receiver + target in a search from the roots. The summary of srv counts it for every target, because a search of any + kind reads the summary. The call h.Handle() in Serve has no implementation in the graph, so it is an interface-receiver record with the method Handle, and os/exec.LookPath, which mine.Handle calls, is uncertain. The link step cannot name the method of c.Close, so the reference is a record with the method Close, which gets a name edge to conn.Close. So os.Chdir is found. No target that a callback reaches is not found. No unit gives 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 7491edeb..b9294936 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 @@ -27,7 +27,10 @@ "definitions": [ "go:github.com/acme/srv@v1.0.0//srv.go//Each" ], - "reaches": [] + "reaches": [], + "uncertainties": [ + 2 + ] }, { "key": "github.com//acme//srv//Handler", @@ -44,7 +47,7 @@ "reaches": [], "uncertainties": [ 0, - 2 + 3 ] }, { @@ -55,7 +58,7 @@ "reaches": [], "uncertainties": [ 0, - 3 + 4 ] } ], @@ -80,6 +83,16 @@ "text": "f (unknown-receiver)", "method": "f" }, + { + "kind": "unresolved-call", + "key": "go:github.com/acme/srv@v1.0.0//srv.go//Each", + "site": { + "path": "go:github.com/acme/srv@v1.0.0//srv.go", + "line": 16, + "column": 2 + }, + "text": "fs[0] (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-function-table/expect.yaml b/test/projectgraph/cases/go/dependency-function-table/expect.yaml index 5ee84368..1f685f84 100644 --- a/test/projectgraph/cases/go/dependency-function-table/expect.yaml +++ b/test/projectgraph/cases/go/dependency-function-table/expect.yaml @@ -4,8 +4,9 @@ description: >- it from the package-level map table, which holds doA. The program loads b, because a imports it, so it runs the module top level of b.go, with the init of b and the reference to doA. The import of b makes a.go run code at load, so main.go reaches a.go and b.go through import edges. So the search finds os.Remove - through the reference to doA, and os/exec.Command through init. The call of the value concerns no - target, and no code uses doB as a value, so os.Getwd is not found. + through the reference to doA, and os/exec.Command through init. In a search from the roots, the call of + the value concerns no target, and no code uses doB as a value, so os.Getwd is not found. The summary of a + counts the call for every target, because the summary does not see the load code of b. units: gomod/github.com/acme/a@v1.0.0: go:github.com/acme/a@v1.0.0 gomod/github.com/acme/b@v1.0.0: go:github.com/acme/b@v1.0.0 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 59ccf10a..f8674b04 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 @@ -11,10 +11,25 @@ ], "reaches": [ "github.com//acme//b//Get" + ], + "uncertainties": [ + 0 ] } ], "targets": [ "github.com//acme//b//Get" + ], + "uncertainties": [ + { + "kind": "unresolved-call", + "key": "go:github.com/acme/a@v1.0.0//a.go//F", + "site": { + "path": "go:github.com/acme/a@v1.0.0//a.go", + "line": 6, + "column": 2 + }, + "text": "b.Get(1) (function-value)" + } ] } diff --git a/test/projectgraph/cases/go/source-function-value/expect.yaml b/test/projectgraph/cases/go/source-function-value/expect.yaml new file mode 100644 index 00000000..4e33723b --- /dev/null +++ b/test/projectgraph/cases/go/source-function-value/expect.yaml @@ -0,0 +1,215 @@ +source: safedep:hand +description: >- + The init of plugins stores os.Remove in the map and in the slice of registry, and only main imports + plugins. ByName calls handlers["rm"], and First calls list[0]. setup stores os.Chdir in fs[0], and main + calls setup and then serve, which calls fs[0]. A search from the roots reaches each code that stores a + function value, so os.Remove and os.Chdir are found through the references, and a call of a value + concerns no target. No code uses os.Exit as a value, so os.Exit is not found. The program runs code + before a source that a search from the source does not reach, as the init of plugins before ByName or + setup before serve. So in a search with a source, each call of a value concerns every target. os.Remove is + uncertain from ByName and from First, os.Chdir from serve, and os.Exit from serve also with the roots. + serve finds os.Remove, because the program loads main.go, and with it plugins, before serve runs. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +exact_edges: true +edges: + - from: main.go + to: plugins/plugins.go + kind: import + confidence: high + site: main.go:6 + - from: main.go + to: make + kind: direct + confidence: high + site: main.go:10 + - from: main.go//main + to: main.go//setup + kind: direct + confidence: high + site: main.go:21 + - from: main.go//main + to: main.go//serve + kind: direct + confidence: high + site: main.go:22 + - from: main.go//main + to: registry/registry.go//ByName + kind: direct + confidence: high + site: main.go:23 + - from: main.go//main + to: registry/registry.go//First + kind: direct + confidence: high + site: main.go:24 + - from: main.go//setup + to: os//Chdir + kind: reference + confidence: low + site: main.go:13 + - from: plugins/plugins.go + to: plugins/plugins.go//init + kind: direct + confidence: high + site: plugins/plugins.go:9 + - from: plugins/plugins.go//init + to: registry/registry.go//Register + kind: direct + confidence: high + site: plugins/plugins.go:10 + - from: plugins/plugins.go//init + to: os//Remove + kind: reference + confidence: low + site: plugins/plugins.go:10 + - from: plugins/plugins.go//unused + to: os//Exit + kind: direct + confidence: high + site: plugins/plugins.go:14 + - from: registry/registry.go//Register + to: append + kind: direct + confidence: high + site: registry/registry.go:10 +unresolved: + - caller: os//Chdir + reason: no-dependency-source + callback: true + - caller: os//Exit + reason: no-dependency-source + callback: true + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: main.go//serve + site: main.go:17 + reason: function-value + - caller: registry/registry.go//ByName + site: registry/registry.go:13 + reason: function-value + - caller: registry/registry.go//First + site: registry/registry.go:15 + reason: function-value +results: + - target: os//Remove + status: found + path: + - key: main.go + - key: plugins/plugins.go + site: main.go:6 + kind: import + confidence: high + - key: plugins/plugins.go//init + site: plugins/plugins.go:9 + kind: direct + confidence: high + - key: os//Remove + site: plugins/plugins.go:10 + kind: reference + confidence: low + call_sites: + - caller: plugins/plugins.go//init + site: plugins/plugins.go:10 + reached: true + - target: os//Chdir + status: found + path: + - key: main.go//main + - key: main.go//setup + site: main.go:21 + kind: direct + confidence: high + - key: os//Chdir + site: main.go:13 + kind: reference + confidence: low + call_sites: + - caller: main.go//setup + site: main.go:13 + reached: true + - target: os//Exit + status: not-found + call_sites: + - caller: plugins/plugins.go//unused + site: plugins/plugins.go:14 + reached: false + - target: os//Remove + sources: [registry/registry.go//ByName] + sources_only: true + status: uncertain + call_sites: + - caller: plugins/plugins.go//init + site: plugins/plugins.go:10 + reached: false + uncertainties: + - kind: unresolved-call + site: registry/registry.go:13 + - target: os//Remove + sources: [registry/registry.go//First] + sources_only: true + status: uncertain + call_sites: + - caller: plugins/plugins.go//init + site: plugins/plugins.go:10 + reached: false + uncertainties: + - kind: unresolved-call + site: registry/registry.go:15 + - target: os//Chdir + sources: [main.go//serve] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//setup + site: main.go:13 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:17 + - target: os//Exit + sources: [main.go//serve] + status: uncertain + call_sites: + - caller: plugins/plugins.go//unused + site: plugins/plugins.go:14 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:17 + - kind: unresolved-call + site: registry/registry.go:13 + - kind: unresolved-call + site: registry/registry.go:15 + - target: os//Remove + sources: [main.go//serve] + sources_only: true + status: found + path: + - key: main.go//serve + - key: main.go + site: main.go + kind: import + confidence: high + - key: plugins/plugins.go + site: main.go:6 + kind: import + confidence: high + - key: plugins/plugins.go//init + site: plugins/plugins.go:9 + kind: direct + confidence: high + - key: os//Remove + site: plugins/plugins.go:10 + kind: reference + confidence: low + call_sites: + - caller: plugins/plugins.go//init + site: plugins/plugins.go:10 + reached: true diff --git a/test/projectgraph/cases/go/source-function-value/project/go.mod b/test/projectgraph/cases/go/source-function-value/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/source-function-value/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/source-function-value/project/main.go b/test/projectgraph/cases/go/source-function-value/project/main.go new file mode 100644 index 00000000..b0404fcd --- /dev/null +++ b/test/projectgraph/cases/go/source-function-value/project/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "os" + + _ "example.com/app/plugins" + "example.com/app/registry" +) + +var fs = make([]func(string) error, 1) + +func setup() { + fs[0] = os.Chdir +} + +func serve() { + fs[0]("/") +} + +func main() { + setup() + serve() + registry.ByName() + registry.First() +} diff --git a/test/projectgraph/cases/go/source-function-value/project/plugins/plugins.go b/test/projectgraph/cases/go/source-function-value/project/plugins/plugins.go new file mode 100644 index 00000000..44c543f4 --- /dev/null +++ b/test/projectgraph/cases/go/source-function-value/project/plugins/plugins.go @@ -0,0 +1,15 @@ +package plugins + +import ( + "os" + + "example.com/app/registry" +) + +func init() { + registry.Register("rm", os.Remove) +} + +func unused() { + os.Exit(1) +} diff --git a/test/projectgraph/cases/go/source-function-value/project/registry/registry.go b/test/projectgraph/cases/go/source-function-value/project/registry/registry.go new file mode 100644 index 00000000..819c8694 --- /dev/null +++ b/test/projectgraph/cases/go/source-function-value/project/registry/registry.go @@ -0,0 +1,15 @@ +package registry + +type F func(string) error + +var handlers = map[string]F{} +var list []F + +func Register(name string, f F) { + handlers[name] = f + list = append(list, f) +} + +func ByName() { handlers["rm"]("x") } + +func First() { list[0]("x") } diff --git a/test/projectgraph/cases/go/source-package-load/expect.yaml b/test/projectgraph/cases/go/source-package-load/expect.yaml index 8c5bf709..c7623f16 100644 --- a/test/projectgraph/cases/go/source-package-load/expect.yaml +++ b/test/projectgraph/cases/go/source-package-load/expect.yaml @@ -5,13 +5,14 @@ description: >- defaults. run calls hooks[0], and runHook calls the field Hook of its parameter. impl.go holds os.Remove in the map table, and T.Handle calls table["rm"]. lib.Serve calls Handle on a value of an interface, and lib does not import impl. extra.go holds os.Chdir, and Run calls steps[0]. The test file of extra holds - os.RemoveAll. A call of a function value concerns no target, because the search reaches the code that - holds the value. The program runs the module top level of each file of a package before any code of the + os.RemoveAll. The program runs the module top level of each file of a package before any code of the package. So a search from a source enters the module top levels of each package that it enters, and each target is found from each source. idle calls nothing, and os.Exit is found from it too, because the - program loads main.go before idle runs. A test file loads only with the code of a test, so os.RemoveAll - is not found from Run, and is found from TestRun. The roots of kind main do not reach extra, so os.Chdir - is found only with the tests, through the test root of extra. + program loads main.go before idle runs. A test file loads only with the code of a test, so the search + from Run does not enter it, and os.RemoveAll is found only from TestRun. From Run, os.RemoveAll is + uncertain. Code that a search from a source does not reach can store a function value, so there the call + steps[0] concerns every target. The roots of kind main do not reach extra, so os.Chdir is found only with + the tests, through the test root of extra. roots: - key: extra/extra_test.go kind: test @@ -205,11 +206,15 @@ results: - target: os//RemoveAll sources: [extra/extra.go//Run] sources_only: true - status: not-found + status: uncertain call_sites: - caller: extra/extra_test.go site: extra/extra_test.go:8 reached: false + uncertainties: + - kind: unresolved-call + site: extra/extra.go:8 + - kind: unresolved-call - target: os//RemoveAll sources: [extra/extra_test.go//TestRun] sources_only: true From e16286a6f9f384993ef5e248e2a323f38a57001a Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 12:41:04 +0000 Subject: [PATCH 42/58] fix(projectgraph): keep the method name of an ambiguous site only where a call runs a method (C3, FOU-557) An ambiguous site kept its method name in every language, and the reach query counted only what the candidates of the name reach. A Python call of a name can run code that no definition of the name holds: an object with __call__ in a class attribute, a value that __getattr__ gives, a function that a module binds by an import, or code of a module that no unit gives. So os.remove read as not-found or no-such-target, where main gave uncertain. A Rust call of a parameter, as handle(s), can run a function value that the Rust index does not record, as the tail expression of a function. It read as not-found too. The profile now decides. A Go site keeps the name, because a Go call of a name runs a method of the name or a function value, and a reference edge leads to each function value. A Rust call on a receiver keeps the name, because a Rust field that holds a function needs the form (x.f)(). Every other site, and every site of Python, JavaScript, TypeScript, Ruby and PHP, concerns every target, as on main. A Go call of a field of a function type can run a value that code before a source stores. So a search with a source, and a summary, count an ambiguous site for every target, as they count a function-value record. The cases python/name-ambiguous-callable, -getattr, -module and -nounit hold the review inputs, and rust/name-ambiguous-param holds the Rust form. Each gave not-found or no-such-target before the change. go/name-ambiguous gave os.Chdir and os.Remove not-found from the source serve before the change. It and rust/name-ambiguous-method guard the precision of a search from the roots: a target that only uncalled code reaches stays not-found. python/name-ambiguous, python/name-ambiguous-record and javascript/field-ambiguous move back to the answers of main. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 65 ++++--- projectgraph/cha_test.go | 2 +- projectgraph/field.go | 5 +- projectgraph/link_test.go | 2 +- projectgraph/profile.go | 16 ++ projectgraph/profile_test.go | 20 +++ projectgraph/reach/reach_test.go | 6 +- projectgraph/reach/source_test.go | 9 +- projectgraph/reach/uncertainty.go | 5 +- projectgraph/rust.go | 1 + .../cases/go/name-ambiguous/expect.yaml | 107 ++++++++++++ .../cases/go/name-ambiguous/project/go.mod | 3 + .../project/handlers/handlers.go | 108 ++++++++++++ .../cases/go/name-ambiguous/project/main.go | 20 +++ .../javascript/field-ambiguous/expect.yaml | 7 +- .../name-ambiguous-callable/expect.yaml | 24 +++ .../name-ambiguous-callable/project/Procfile | 1 + .../project/handlers.py | 105 ++++++++++++ .../name-ambiguous-callable/project/main.py | 6 + .../python/name-ambiguous-getattr/expect.yaml | 24 +++ .../name-ambiguous-getattr/project/Procfile | 1 + .../project/handlers.py | 105 ++++++++++++ .../name-ambiguous-getattr/project/main.py | 6 + .../python/name-ambiguous-module/expect.yaml | 20 +++ .../name-ambiguous-module/project/Procfile | 1 + .../name-ambiguous-module/project/handlers.py | 96 +++++++++++ .../name-ambiguous-module/project/main.py | 6 + .../name-ambiguous-module/project/plug.py | 1 + .../python/name-ambiguous-nounit/expect.yaml | 29 ++++ .../name-ambiguous-nounit/project/Procfile | 1 + .../name-ambiguous-nounit/project/handlers.py | 101 +++++++++++ .../name-ambiguous-nounit/project/main.py | 7 + .../python/name-ambiguous-record/expect.yaml | 12 +- .../cases/python/name-ambiguous/expect.yaml | 14 +- .../rust/name-ambiguous-method/expect.yaml | 47 ++++++ .../name-ambiguous-method/project/Cargo.toml | 4 + .../project/src/handlers.rs | 159 ++++++++++++++++++ .../name-ambiguous-method/project/src/main.rs | 15 ++ .../rust/name-ambiguous-param/expect.yaml | 38 +++++ .../name-ambiguous-param/project/Cargo.toml | 4 + .../project/src/handlers.rs | 151 +++++++++++++++++ .../name-ambiguous-param/project/src/main.rs | 17 ++ 42 files changed, 1324 insertions(+), 47 deletions(-) create mode 100644 test/projectgraph/cases/go/name-ambiguous/expect.yaml create mode 100644 test/projectgraph/cases/go/name-ambiguous/project/go.mod create mode 100644 test/projectgraph/cases/go/name-ambiguous/project/handlers/handlers.go create mode 100644 test/projectgraph/cases/go/name-ambiguous/project/main.go create mode 100644 test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml create mode 100644 test/projectgraph/cases/python/name-ambiguous-callable/project/Procfile create mode 100644 test/projectgraph/cases/python/name-ambiguous-callable/project/handlers.py create mode 100644 test/projectgraph/cases/python/name-ambiguous-callable/project/main.py create mode 100644 test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml create mode 100644 test/projectgraph/cases/python/name-ambiguous-getattr/project/Procfile create mode 100644 test/projectgraph/cases/python/name-ambiguous-getattr/project/handlers.py create mode 100644 test/projectgraph/cases/python/name-ambiguous-getattr/project/main.py create mode 100644 test/projectgraph/cases/python/name-ambiguous-module/expect.yaml create mode 100644 test/projectgraph/cases/python/name-ambiguous-module/project/Procfile create mode 100644 test/projectgraph/cases/python/name-ambiguous-module/project/handlers.py create mode 100644 test/projectgraph/cases/python/name-ambiguous-module/project/main.py create mode 100644 test/projectgraph/cases/python/name-ambiguous-module/project/plug.py create mode 100644 test/projectgraph/cases/python/name-ambiguous-nounit/expect.yaml create mode 100644 test/projectgraph/cases/python/name-ambiguous-nounit/project/Procfile create mode 100644 test/projectgraph/cases/python/name-ambiguous-nounit/project/handlers.py create mode 100644 test/projectgraph/cases/python/name-ambiguous-nounit/project/main.py create mode 100644 test/projectgraph/cases/rust/name-ambiguous-method/expect.yaml create mode 100644 test/projectgraph/cases/rust/name-ambiguous-method/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/name-ambiguous-method/project/src/handlers.rs create mode 100644 test/projectgraph/cases/rust/name-ambiguous-method/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/name-ambiguous-param/expect.yaml create mode 100644 test/projectgraph/cases/rust/name-ambiguous-param/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/name-ambiguous-param/project/src/handlers.rs create mode 100644 test/projectgraph/cases/rust/name-ambiguous-param/project/src/main.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 5d3002f3..aa9b65e3 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -235,17 +235,41 @@ reason. A site with more than 20 candidates of its method name, as `render` in a Django project, is `ambiguous`. The site gets no edge, so the search cannot -follow what the candidates call. The site keeps its method name, because -the call can run only a method of that name. A record with a method name -concerns the targets with that last segment and the targets that a -definition of the name reaches, so it concerns what each candidate -reaches. A candidate can also run code that no edge leads to, through an -unresolved site of its own. So the record also concerns what each -uncertainty of the code that a candidate reaches concerns, as -[The honesty rule](#the-honesty-rule) says. In the case -`python/name-ambiguous-record`, `H0.handle` calls `load` on a field whose -value the index does not know, and `yaml.load` is `uncertain` and not -`no-such-target`. +follow what the candidates call. A record with a method name concerns the +targets with that last segment and the targets that a definition of the +name reaches, so it concerns what each candidate reaches. A candidate can +also run code that no edge leads to, through an unresolved site of its +own. So the record also concerns what each uncertainty of the code that a +candidate reaches concerns, as [The honesty rule](#the-honesty-rule) says. + +An ambiguous site keeps its method name only where a call of the name can +run nothing but a method of the name or a function value that a +`reference` edge leads to. The profile of the language says where. + +- Go. A call runs a method of the name or a function value, as a field of + a function type. A function value has a `reference` edge from the code + that stores it, as [A call of a function value](#a-call-of-a-function-value) + argues. So the site keeps its name, and the search from the roots + reaches the stored function through that edge. A search with a source + and a summary may not reach the code that stores the value, so there the + site concerns every target. The case `go/name-ambiguous` shows each + answer. +- Rust. A call on a receiver, as `h.handle(s)`, runs only a method of the + name, because a field that holds a function needs the form `(h.f)(s)`. + So the site keeps its name, as in the case `rust/name-ambiguous-method`. + A call with no receiver, as `handle(s)` of a parameter, can run any + function value, and the Rust index does not record each one. So that + site concerns every target, as in the case `rust/name-ambiguous-param`. + The reach query does not read the language of a site, so a search with a + source and a summary count a Rust site for every target too. +- Python, JavaScript, TypeScript, Ruby and PHP. A call of a name can run + code that no definition of the name holds: an object with `__call__` in a + class attribute, a value that `__getattr__` gives, a function that a + module binds to the name by an import, or code of a module that no unit + gives. So an ambiguous site keeps no method name and concerns every + target. The cases `python/name-ambiguous-callable`, + `python/name-ambiguous-getattr`, `python/name-ambiguous-module` and + `python/name-ambiguous-nounit` show each form. 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 @@ -559,8 +583,8 @@ A search from a source also enters the load code of each package whose code it reaches, because the program runs that code before the source. The caller of a source can also run other code before it that no root reaches, and that code can store a function value. So in a search with a -source, in either mode, a call of a function value concerns every -target. See +source, in either mode, a call of a function value and an ambiguous call +concern every target. See [A call of a function value](#a-call-of-a-function-value). A source must name a first-party node of the graph. A key of no node, an @@ -706,7 +730,7 @@ the scope also concerns what each of them concerns, as the search of a query counts them, and the scope of a method name, a unit or a callback that one of them names goes on into that code too. Without that, a target that only such code reaches reads as `not-found`. The cases -`python/name-ambiguous-record` for a method name, +`rust/name-ambiguous-method` for a method name, `python/dependency-dark-unit-record` for a unit and `go/stdlib-callback-record` for a callback show it. A summary keeps the same scope: `reach.Scoped` holds the method names and the import prefixes @@ -818,16 +842,18 @@ that only `main` imports and that fills a registry, or a `setup` that `main` calls before `serve`. With `SourcesAndRoots` the source is an entry that the rules missed, so its caller can be code that the rules missed too. So in a search with a source, a `function-value` record concerns -every target, as a `computed-name` record does. The case -`go/source-function-value` holds both forms of the store. +every target, as a `computed-name` record does. So does an ambiguous site, +because a Go call of a name can run a function value in a field. The case +`go/source-function-value` holds both forms of the store, and +`go/name-ambiguous` holds a field. A summary does not have the argument either. A summary is a search from one symbol of a unit, and a search of any kind reads it: a search from the roots or from a source, of a project that the summary does not see. The value can come from that project, from another unit, or from a package of the unit that the symbol does not import, as a registry that the init of -another package fills. So a summary counts a `function-value` record for -every target. `summarytest` checks a summary against +another package fills. So a summary counts a `function-value` record and +an ambiguous site for every target. `summarytest` checks a summary against a reach query of the unit graph from the symbol as a root of kind `query`, which has the same rule. @@ -868,7 +894,8 @@ 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`. +call can read as `not-found`. Only an ambiguous site of the form concerns +every target. 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/cha_test.go b/projectgraph/cha_test.go index 9c909a7d..cf0136d8 100644 --- a/projectgraph/cha_test.go +++ b/projectgraph/cha_test.go @@ -210,7 +210,7 @@ func TestBuildFieldEdges(t *testing.T) { assert.Equal(t, []string{ "main.js:1 main.js unknown-receiver parse", "main.js:2 main.js unknown-receiver parse", - "main.js:3 main.js ambiguous get", + "main.js:3 main.js ambiguous ", }, unresolvedStrings(g), "25 candidates make the site ambiguous") } diff --git a/projectgraph/field.go b/projectgraph/field.go index e9996c2a..0840baa6 100644 --- a/projectgraph/field.go +++ b/projectgraph/field.go @@ -51,11 +51,12 @@ func (b *builder) linkFields() { } } } - // An ambiguous site keeps its method name. It can run only a - // method of that name, so the reach query scopes it by the name. if len(candidates) > maxNameCandidates { delete(b.unresolved, u) u.Reason = fileindex.UnresolvedAmbiguous + if !profileOf(ix.Language).keepsAmbiguousMethod(u.Receiver) { + u.Method = "" + } b.addUnresolved(u) continue } diff --git a/projectgraph/link_test.go b/projectgraph/link_test.go index 9df7832a..bd2ca572 100644 --- a/projectgraph/link_test.go +++ b/projectgraph/link_test.go @@ -862,7 +862,7 @@ func TestBuildNameEdges(t *testing.T) { assert.Equal(t, []string{ "main.py:2 main.py unknown-receiver cb", "main.py:4 main.py unknown-receiver run", - "main.py:5 main.py ambiguous get", + "main.py:5 main.py ambiguous ", "main.py:6 main.py computed-name ", }, unresolvedStrings(g), "a bare name with no binding is a record, and too many candidates make the site ambiguous") assert.Contains(t, g.Nodes, "print", "a builtin stays external") diff --git a/projectgraph/profile.go b/projectgraph/profile.go index 976cc30e..1c899bb3 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -226,6 +226,10 @@ type profile struct { // concerns no target in a search from the roots. A reference to a method that the link step cannot // name, as h.Handle on an interface, gets a record with the method name. functionValues bool + // receiverMethods marks a language where a call on a receiver, as + // x.m(), runs only a method of the name m, as Rust. A Rust field that + // holds a function needs another form, as (x.f)(). + receiverMethods bool // reflection names the package of the language that can run any // function by a value or by a name, and its methods that do it, as // Call of reflect.Value in Go. @@ -261,6 +265,18 @@ func profileOf(lang langcode.Code) *profile { return noProfile } +// keepsAmbiguousMethod reports whether an ambiguous site with the receiver +// keeps its method name, so the reach query scopes it by the name. A call +// on a receiver of a language with receiverMethods runs only a method of +// the name. A call of a language with functionValues runs a method of the +// name or a function value, and a reference edge leads to each function +// value. In another language a call of a name can run code that no +// definition of the name holds, as a Python class attribute that holds an +// object with __call__, or a Rust parameter that holds a function. +func (p *profile) keepsAmbiguousMethod(receiver string) bool { + return p.functionValues || p.receiverMethods && receiver != "" +} + func isInstanceKeyword(lang langcode.Code, name string) bool { for _, k := range profileOf(lang).instance { if k == name { diff --git a/projectgraph/profile_test.go b/projectgraph/profile_test.go index 368ac4eb..743b3534 100644 --- a/projectgraph/profile_test.go +++ b/projectgraph/profile_test.go @@ -35,3 +35,23 @@ func TestLangStateIsOnePerLanguage(t *testing.T) { assert.Same(t, b.rustState(), langState[rustState](b, langcode.Rust)) assert.Nil(t, b.goState().methods, "a table that no lookup filled is empty") } + +func TestAmbiguousSitesKeepTheMethodNameWhereOnlyAMethodRuns(t *testing.T) { + for _, tt := range []struct { + lang langcode.Code + receiver string + want bool + }{ + {lang: langcode.Go, receiver: "s", want: true}, + {lang: langcode.Go, receiver: "", want: true}, + {lang: langcode.Rust, receiver: "s", want: true}, + {lang: langcode.Rust, receiver: "", want: false}, + {lang: langcode.Python, receiver: "s", want: false}, + {lang: langcode.Javascript, receiver: "s", want: false}, + {lang: langcode.Typescript, receiver: "s", want: false}, + {lang: langcode.Ruby, receiver: "s", want: false}, + {lang: langcode.PHP, receiver: "s", want: false}, + } { + assert.Equal(t, tt.want, profileOf(tt.lang).keepsAmbiguousMethod(tt.receiver), "%s with the receiver %q", tt.lang, tt.receiver) + } +} diff --git a/projectgraph/reach/reach_test.go b/projectgraph/reach/reach_test.go index 09f26c62..6834ccab 100644 --- a/projectgraph/reach/reach_test.go +++ b/projectgraph/reach/reach_test.go @@ -383,12 +383,12 @@ unresolved handlers.py//parse 5 unknown-receiver f // TestReachSpanCountsTheRecordsOfItsCode checks that an unresolved call of a // method concerns what its candidates run through their own unresolved -// calls, as the ambiguous call handle whose candidate calls load on a value -// of an unknown type. Reach and the table of ExploreAll give one answer. +// calls, as the call handle whose candidate calls load on a value of an +// unknown type. Reach and the table of ExploreAll give one answer. func TestReachSpanCountsTheRecordsOfItsCode(t *testing.T) { const spec = ` root main.py//run -unresolved main.py//run 3 ambiguous handle +unresolved main.py//run 3 unknown-receiver handle edge lib.py//H//handle lib.py//helper direct 4 edge lib.py//Codec//load pickle//loads direct 8 edge lib.py//dead os//exit direct 12 diff --git a/projectgraph/reach/source_test.go b/projectgraph/reach/source_test.go index e7192c13..939a7e7c 100644 --- a/projectgraph/reach/source_test.go +++ b/projectgraph/reach/source_test.go @@ -222,17 +222,20 @@ func TestReachFromASourceEntersItsLoadGroup(t *testing.T) { } // TestReachScopesAValueCallOnlyFromTheRoots checks that a call of a -// function value concerns no target in a search from the roots that the -// rules marked. A search with a source and the sets of ExploreAll may not -// reach the code that stores the value, so there it concerns every target. +// function value concerns no target, and an ambiguous call only what its +// method name concerns, in a search from the roots that the rules marked. +// A search with a source and the sets of ExploreAll may not reach the code +// that stores the value, so there each record concerns every target. func TestReachScopesAValueCallOnlyFromTheRoots(t *testing.T) { const spec = ` root app/a.go//main edge app/a.go//main app/a.go//serve direct 3 +edge app/a.go//H//Handle os//Getwd direct 12 edge app/a.go//dead os//Remove direct 16 ` for _, tt := range []struct{ name, record string }{ {name: "a call of a function value", record: "unresolved app/a.go//serve 8 function-value"}, + {name: "an ambiguous call of a method name", record: "unresolved app/a.go//serve 8 ambiguous Handle"}, } { t.Run(tt.name, func(t *testing.T) { g := graphOf(t, spec+tt.record) diff --git a/projectgraph/reach/uncertainty.go b/projectgraph/reach/uncertainty.go index d75c0b52..41f1281b 100644 --- a/projectgraph/reach/uncertainty.go +++ b/projectgraph/reach/uncertainty.go @@ -176,7 +176,10 @@ func (s *sources) at(key string, n *projectgraph.Node) []Uncertainty { u.Unit = r.Unit case r.ImportPrefix != "": u.ImportPrefix = r.ImportPrefix - case r.Method != "" && !projectgraph.ReachesAny(r.Reason): + case r.Method != "" && !projectgraph.ReachesAny(r.Reason) && (s.valueEdges || r.Reason != fileindex.UnresolvedAmbiguous): + // An ambiguous site that keeps its method name can call a + // function value, as a Go field of a function type, so only + // a search with valueEdges scopes it by the name. u.Method = r.Method } out = append(out, u) diff --git a/projectgraph/rust.go b/projectgraph/rust.go index 5582c75f..bd3516f1 100644 --- a/projectgraph/rust.go +++ b/projectgraph/rust.go @@ -29,6 +29,7 @@ var rustProfile = profile{ symbol: rustSymbol, surface: rustSurface, unitTypes: true, + receiverMethods: true, sameTypeMethods: map[string]bool{"clone": true, "to_owned": true, "default": true, "from": true}, abstractReceiver: fileindex.UnresolvedTraitReceiver, dependencies: func(b *builder) ([]string, []modules.Problem) { return b.cargoOf(b.project).Dependencies(), nil }, diff --git a/test/projectgraph/cases/go/name-ambiguous/expect.yaml b/test/projectgraph/cases/go/name-ambiguous/expect.yaml new file mode 100644 index 00000000..e3d7c094 --- /dev/null +++ b/test/projectgraph/cases/go/name-ambiguous/expect.yaml @@ -0,0 +1,107 @@ +source: safedep:hand +description: >- + serve calls the field Handle of a server, which holds a function value. The link step does not know the + value, and 25 types of handlers define a method Handle. That is over the cap of 20, so the site gets no + name edge and is ambiguous. A Go call of a name runs a method of the name or a function value, and a + reference edge leads to each function value from the code that stores it. So the site keeps the method + name Handle. A search from the roots reaches main, which stores os.Chdir in the field, so os.Chdir is + found. H0.Handle calls os/exec.Command, so os/exec.Command is uncertain. os.Remove, which only the + uncalled Cleanup calls, is not found. A search from serve alone does not reach the code that stores the + value, so there the site concerns every target, and os.Chdir and os.Remove are uncertain. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +exact_edges: true +edges: + - from: handlers/handlers.go//Cleanup + to: os//Remove + kind: direct + confidence: high + site: handlers/handlers.go:108 + - from: handlers/handlers.go//H0//Handle + to: os//exec//Command + kind: direct + confidence: high + site: handlers/handlers.go:10 + - from: main.go//main + to: os//Chdir + kind: reference + confidence: low + site: main.go:18 + - from: main.go//main + to: main.go//server//serve + kind: member + confidence: high + site: main.go:19 +unresolved: + - caller: main.go//server//serve + reason: no-dependency-source + callback: true + - caller: os//Chdir + reason: no-dependency-source + callback: true + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: os//exec//Command + reason: no-dependency-source + callback: true + - caller: main.go//server//serve + site: main.go:14 + reason: ambiguous + method: Handle +results: + - target: os//Chdir + status: found + path: + - key: main.go//main + - key: os//Chdir + site: main.go:18 + kind: reference + confidence: low + call_sites: + - caller: main.go//main + site: main.go:18 + reached: true + - target: os//exec//Command + status: uncertain + call_sites: + - caller: handlers/handlers.go//H0//Handle + site: handlers/handlers.go:10 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:14 + - kind: unresolved-call + - target: os//Remove + status: not-found + call_sites: + - caller: handlers/handlers.go//Cleanup + site: handlers/handlers.go:108 + reached: false + - target: os//Chdir + sources: [main.go//server//serve] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//main + site: main.go:18 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:14 + - target: os//Remove + sources: [main.go//server//serve] + sources_only: true + status: uncertain + call_sites: + - caller: handlers/handlers.go//Cleanup + site: handlers/handlers.go:108 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:14 diff --git a/test/projectgraph/cases/go/name-ambiguous/project/go.mod b/test/projectgraph/cases/go/name-ambiguous/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/name-ambiguous/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/name-ambiguous/project/handlers/handlers.go b/test/projectgraph/cases/go/name-ambiguous/project/handlers/handlers.go new file mode 100644 index 00000000..13a846cc --- /dev/null +++ b/test/projectgraph/cases/go/name-ambiguous/project/handlers/handlers.go @@ -0,0 +1,108 @@ +package handlers + +import ( + "os" + "os/exec" +) + +type H0 struct{} + +func (H0) Handle(s string) { exec.Command(s) } + +type H1 struct{} + +func (H1) Handle(s string) {} + +type H2 struct{} + +func (H2) Handle(s string) {} + +type H3 struct{} + +func (H3) Handle(s string) {} + +type H4 struct{} + +func (H4) Handle(s string) {} + +type H5 struct{} + +func (H5) Handle(s string) {} + +type H6 struct{} + +func (H6) Handle(s string) {} + +type H7 struct{} + +func (H7) Handle(s string) {} + +type H8 struct{} + +func (H8) Handle(s string) {} + +type H9 struct{} + +func (H9) Handle(s string) {} + +type H10 struct{} + +func (H10) Handle(s string) {} + +type H11 struct{} + +func (H11) Handle(s string) {} + +type H12 struct{} + +func (H12) Handle(s string) {} + +type H13 struct{} + +func (H13) Handle(s string) {} + +type H14 struct{} + +func (H14) Handle(s string) {} + +type H15 struct{} + +func (H15) Handle(s string) {} + +type H16 struct{} + +func (H16) Handle(s string) {} + +type H17 struct{} + +func (H17) Handle(s string) {} + +type H18 struct{} + +func (H18) Handle(s string) {} + +type H19 struct{} + +func (H19) Handle(s string) {} + +type H20 struct{} + +func (H20) Handle(s string) {} + +type H21 struct{} + +func (H21) Handle(s string) {} + +type H22 struct{} + +func (H22) Handle(s string) {} + +type H23 struct{} + +func (H23) Handle(s string) {} + +type H24 struct{} + +func (H24) Handle(s string) {} + +func Cleanup() { os.Remove("x") } diff --git a/test/projectgraph/cases/go/name-ambiguous/project/main.go b/test/projectgraph/cases/go/name-ambiguous/project/main.go new file mode 100644 index 00000000..7006ceb2 --- /dev/null +++ b/test/projectgraph/cases/go/name-ambiguous/project/main.go @@ -0,0 +1,20 @@ +package main + +import ( + "os" + + _ "example.com/app/handlers" +) + +type server struct { + Handle func(string) error +} + +func (s *server) serve() { + s.Handle("/") +} + +func main() { + s := &server{Handle: os.Chdir} + s.serve() +} diff --git a/test/projectgraph/cases/javascript/field-ambiguous/expect.yaml b/test/projectgraph/cases/javascript/field-ambiguous/expect.yaml index 34f74fc9..d2229db5 100644 --- a/test/projectgraph/cases/javascript/field-ambiguous/expect.yaml +++ b/test/projectgraph/cases/javascript/field-ambiguous/expect.yaml @@ -1,9 +1,5 @@ source: safedep:hand -description: >- - dispatch calls handle on the parameter handler, whose value the index does not know. 25 object properties - hold a function named handle. That is over the cap of 20, so the site gets no field edge and is ambiguous. The - site keeps the method name handle, and h0.handle calls yaml.load, so the reach result for yaml.load is - uncertain. +description: dispatch calls handle on the parameter handler, whose value the index does not know. 25 object properties hold a function named handle. That is over the cap of 20, so the site gets no field edge and is ambiguous. An ambiguous site can call any target, so the reach result for yaml.load is uncertain. roots: - key: src/main.js kind: manifest @@ -21,7 +17,6 @@ unresolved: - caller: src/main.js//dispatch site: src/main.js:4 reason: ambiguous - method: handle results: - target: js-yaml//load status: uncertain diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml new file mode 100644 index 00000000..c3de824a --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-callable/expect.yaml @@ -0,0 +1,24 @@ +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. +roots: + - key: main.py + kind: manifest + rule: procfile +unresolved: + - caller: main.py//dispatch + site: main.py:4 + reason: ambiguous +results: + - target: os//remove + status: uncertain + call_sites: + - caller: handlers.py//Cb//__call__ + site: handlers.py:5 + reached: false + uncertainties: + - kind: unresolved-call + site: main.py:4 diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-callable/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-callable/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-callable/project/handlers.py new file mode 100644 index 00000000..c465d824 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-callable/project/handlers.py @@ -0,0 +1,105 @@ +import os + +class Cb: + def __call__(self, path): + os.remove(path) + +class H0: + handle = Cb() + +class H1: + def handle(self, text): + return text + +class H2: + def handle(self, text): + return text + +class H3: + def handle(self, text): + return text + +class H4: + def handle(self, text): + return text + +class H5: + def handle(self, text): + return text + +class H6: + def handle(self, text): + return text + +class H7: + def handle(self, text): + return text + +class H8: + def handle(self, text): + return text + +class H9: + def handle(self, text): + return text + +class H10: + def handle(self, text): + return text + +class H11: + def handle(self, text): + return text + +class H12: + def handle(self, text): + return text + +class H13: + def handle(self, text): + return text + +class H14: + def handle(self, text): + return text + +class H15: + def handle(self, text): + return text + +class H16: + def handle(self, text): + return text + +class H17: + def handle(self, text): + return text + +class H18: + def handle(self, text): + return text + +class H19: + def handle(self, text): + return text + +class H20: + def handle(self, text): + return text + +class H21: + def handle(self, text): + return text + +class H22: + def handle(self, text): + return text + +class H23: + def handle(self, text): + return text + +class H24: + def handle(self, text): + return text + diff --git a/test/projectgraph/cases/python/name-ambiguous-callable/project/main.py b/test/projectgraph/cases/python/name-ambiguous-callable/project/main.py new file mode 100644 index 00000000..4455897b --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-callable/project/main.py @@ -0,0 +1,6 @@ +from handlers import H0 + +def dispatch(handler, text): + return handler.handle(text) + +dispatch(H0(), 'a') diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml new file mode 100644 index 00000000..e5f43d55 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/expect.yaml @@ -0,0 +1,24 @@ +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. +roots: + - key: main.py + kind: manifest + rule: procfile +unresolved: + - caller: main.py//dispatch + site: main.py:4 + reason: ambiguous +results: + - target: os//remove + status: uncertain + call_sites: + - caller: handlers.py//cleanup + site: handlers.py:4 + reached: false + uncertainties: + - kind: unresolved-call + site: main.py:4 diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-getattr/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-getattr/project/handlers.py new file mode 100644 index 00000000..496760f4 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/project/handlers.py @@ -0,0 +1,105 @@ +import os + +def cleanup(path): + os.remove(path) + +class H0: + def __getattr__(self, name): + return cleanup + +class H1: + def handle(self, text): + return text + +class H2: + def handle(self, text): + return text + +class H3: + def handle(self, text): + return text + +class H4: + def handle(self, text): + return text + +class H5: + def handle(self, text): + return text + +class H6: + def handle(self, text): + return text + +class H7: + def handle(self, text): + return text + +class H8: + def handle(self, text): + return text + +class H9: + def handle(self, text): + return text + +class H10: + def handle(self, text): + return text + +class H11: + def handle(self, text): + return text + +class H12: + def handle(self, text): + return text + +class H13: + def handle(self, text): + return text + +class H14: + def handle(self, text): + return text + +class H15: + def handle(self, text): + return text + +class H16: + def handle(self, text): + return text + +class H17: + def handle(self, text): + return text + +class H18: + def handle(self, text): + return text + +class H19: + def handle(self, text): + return text + +class H20: + def handle(self, text): + return text + +class H21: + def handle(self, text): + return text + +class H22: + def handle(self, text): + return text + +class H23: + def handle(self, text): + return text + +class H24: + def handle(self, text): + return text + diff --git a/test/projectgraph/cases/python/name-ambiguous-getattr/project/main.py b/test/projectgraph/cases/python/name-ambiguous-getattr/project/main.py new file mode 100644 index 00000000..4455897b --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-getattr/project/main.py @@ -0,0 +1,6 @@ +from handlers import H0 + +def dispatch(handler, text): + return handler.handle(text) + +dispatch(H0(), 'a') diff --git a/test/projectgraph/cases/python/name-ambiguous-module/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-module/expect.yaml new file mode 100644 index 00000000..b7d69dee --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/expect.yaml @@ -0,0 +1,20 @@ +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 the module plug, which binds os.remove to the name handle by + an import. No definition of the name handle runs. So the site keeps no method name and concerns every + target, and os.remove is uncertain and not no-such-target. +roots: + - key: main.py + kind: manifest + rule: procfile +unresolved: + - caller: main.py//dispatch + site: main.py:4 + reason: ambiguous +results: + - target: os//remove + status: uncertain + uncertainties: + - kind: unresolved-call + site: main.py:4 diff --git a/test/projectgraph/cases/python/name-ambiguous-module/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-module/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-module/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-module/project/handlers.py new file mode 100644 index 00000000..4ecf9269 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/project/handlers.py @@ -0,0 +1,96 @@ +class H1: + def handle(self, text): + return text + +class H2: + def handle(self, text): + return text + +class H3: + def handle(self, text): + return text + +class H4: + def handle(self, text): + return text + +class H5: + def handle(self, text): + return text + +class H6: + def handle(self, text): + return text + +class H7: + def handle(self, text): + return text + +class H8: + def handle(self, text): + return text + +class H9: + def handle(self, text): + return text + +class H10: + def handle(self, text): + return text + +class H11: + def handle(self, text): + return text + +class H12: + def handle(self, text): + return text + +class H13: + def handle(self, text): + return text + +class H14: + def handle(self, text): + return text + +class H15: + def handle(self, text): + return text + +class H16: + def handle(self, text): + return text + +class H17: + def handle(self, text): + return text + +class H18: + def handle(self, text): + return text + +class H19: + def handle(self, text): + return text + +class H20: + def handle(self, text): + return text + +class H21: + def handle(self, text): + return text + +class H22: + def handle(self, text): + return text + +class H23: + def handle(self, text): + return text + +class H24: + def handle(self, text): + return text + diff --git a/test/projectgraph/cases/python/name-ambiguous-module/project/main.py b/test/projectgraph/cases/python/name-ambiguous-module/project/main.py new file mode 100644 index 00000000..d13a1635 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/project/main.py @@ -0,0 +1,6 @@ +import plug + +def dispatch(handler, text): + return handler.handle(text) + +dispatch(plug, 'a') diff --git a/test/projectgraph/cases/python/name-ambiguous-module/project/plug.py b/test/projectgraph/cases/python/name-ambiguous-module/project/plug.py new file mode 100644 index 00000000..0955f9e4 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-module/project/plug.py @@ -0,0 +1 @@ +from os import remove as handle diff --git a/test/projectgraph/cases/python/name-ambiguous-nounit/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-nounit/expect.yaml new file mode 100644 index 00000000..76d73b13 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-nounit/expect.yaml @@ -0,0 +1,29 @@ +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 extlib.Handler, and no unit gives extlib. Its handle can + call any code of extlib, as extlib.vuln, which only the uncalled unused calls in the project. So the site + keeps no method name and concerns every target, and extlib.vuln is uncertain and not not-found. +roots: + - key: main.py + kind: manifest + rule: procfile +unresolved: + - caller: main.py//dispatch + site: main.py:5 + reason: ambiguous +results: + - target: extlib//vuln + status: uncertain + call_sites: + - caller: handlers.py//unused + site: handlers.py:4 + reached: false + uncertainties: + - kind: unresolved-call + site: main.py:5 + - target: extlib//Handler//handle + status: uncertain + uncertainties: + - kind: unresolved-call + site: main.py:5 diff --git a/test/projectgraph/cases/python/name-ambiguous-nounit/project/Procfile b/test/projectgraph/cases/python/name-ambiguous-nounit/project/Procfile new file mode 100644 index 00000000..629b83ad --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-nounit/project/Procfile @@ -0,0 +1 @@ +web: python main.py diff --git a/test/projectgraph/cases/python/name-ambiguous-nounit/project/handlers.py b/test/projectgraph/cases/python/name-ambiguous-nounit/project/handlers.py new file mode 100644 index 00000000..65b8ec72 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-nounit/project/handlers.py @@ -0,0 +1,101 @@ +import extlib + +def unused(): + extlib.vuln() + +class H1: + def handle(self, text): + return text + +class H2: + def handle(self, text): + return text + +class H3: + def handle(self, text): + return text + +class H4: + def handle(self, text): + return text + +class H5: + def handle(self, text): + return text + +class H6: + def handle(self, text): + return text + +class H7: + def handle(self, text): + return text + +class H8: + def handle(self, text): + return text + +class H9: + def handle(self, text): + return text + +class H10: + def handle(self, text): + return text + +class H11: + def handle(self, text): + return text + +class H12: + def handle(self, text): + return text + +class H13: + def handle(self, text): + return text + +class H14: + def handle(self, text): + return text + +class H15: + def handle(self, text): + return text + +class H16: + def handle(self, text): + return text + +class H17: + def handle(self, text): + return text + +class H18: + def handle(self, text): + return text + +class H19: + def handle(self, text): + return text + +class H20: + def handle(self, text): + return text + +class H21: + def handle(self, text): + return text + +class H22: + def handle(self, text): + return text + +class H23: + def handle(self, text): + return text + +class H24: + def handle(self, text): + return text + diff --git a/test/projectgraph/cases/python/name-ambiguous-nounit/project/main.py b/test/projectgraph/cases/python/name-ambiguous-nounit/project/main.py new file mode 100644 index 00000000..50203479 --- /dev/null +++ b/test/projectgraph/cases/python/name-ambiguous-nounit/project/main.py @@ -0,0 +1,7 @@ +import extlib +import handlers + +def dispatch(handler, text): + return handler.handle(text) + +dispatch(extlib.Handler(), 'a') diff --git a/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml b/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml index 62c5c57e..664d3254 100644 --- a/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml +++ b/test/projectgraph/cases/python/name-ambiguous-record/expect.yaml @@ -3,9 +3,9 @@ description: >- dispatch calls handle on the parameter handler. 25 classes define handle, so the site is ambiguous and gets no name edge. H0.handle calls load on its field codec, whose value the index does not know, so that site is a record with the method load. main passes the module yaml as the codec, so the program runs yaml.load. No - edge leads from a candidate of handle to yaml.load. The ambiguous record concerns what each candidate can - run, through its own records too, so yaml.load is uncertain and not no-such-target. os.remove, which only - the uncalled cleanup calls, is not found. + edge leads from a candidate of handle to yaml.load. A Python call of a name can run code that no definition + of the name holds, so the ambiguous site keeps no method name and concerns every target. yaml.load is + uncertain and not no-such-target, and so is os.remove, which only the uncalled cleanup calls. roots: - key: main.py kind: manifest @@ -21,7 +21,6 @@ unresolved: - caller: main.py//dispatch site: main.py:6 reason: ambiguous - method: handle results: - target: yaml//load status: uncertain @@ -29,8 +28,11 @@ results: - kind: unresolved-call site: main.py:6 - target: os//remove - status: not-found + status: uncertain call_sites: - caller: handlers.py//cleanup site: handlers.py:133 reached: false + uncertainties: + - kind: unresolved-call + site: main.py:6 diff --git a/test/projectgraph/cases/python/name-ambiguous/expect.yaml b/test/projectgraph/cases/python/name-ambiguous/expect.yaml index ac4b970c..dd9a16fa 100644 --- a/test/projectgraph/cases/python/name-ambiguous/expect.yaml +++ b/test/projectgraph/cases/python/name-ambiguous/expect.yaml @@ -1,10 +1,10 @@ source: safedep:hand description: >- dispatch calls handle on the parameter handler, whose value the index does not know. 25 classes define - handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. The site keeps the - method name handle. H0.handle calls yaml.load, so the reach result for yaml.load is uncertain. An ambiguous - call of handle runs no function of another name, so os.remove, which only the uncalled cleanup calls, is - not found. + handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. A Python call of a + name can run code that no definition of the name holds, as a class attribute that holds an object with + __call__. So the site keeps no method name and concerns every target. yaml.load, which H0.handle calls, + and os.remove, which only the uncalled cleanup calls, are uncertain. roots: - key: main.py kind: manifest @@ -22,7 +22,6 @@ unresolved: - caller: main.py//dispatch site: main.py:5 reason: ambiguous - method: handle results: - target: yaml//load status: uncertain @@ -34,8 +33,11 @@ results: - kind: unresolved-call site: main.py:5 - target: os//remove - status: not-found + status: uncertain call_sites: - caller: handlers.py//cleanup site: handlers.py:132 reached: false + uncertainties: + - kind: unresolved-call + site: main.py:5 diff --git a/test/projectgraph/cases/rust/name-ambiguous-method/expect.yaml b/test/projectgraph/cases/rust/name-ambiguous-method/expect.yaml new file mode 100644 index 00000000..ef76045c --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-method/expect.yaml @@ -0,0 +1,47 @@ +source: safedep:hand +description: >- + dispatch calls handle on an element of a vector of a generic type, which the link step does not know. 25 + types define handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. A Rust + call on a receiver runs only a method of the name, because a field that holds a function needs the form + (h.f)(). So the site keeps the method name handle. H0.handle calls std::env::var, so std::env::var is + uncertain. H1.handle calls decode on a trait object of Codec, which no type of the graph implements, so + that site is a record with the method decode. The ambiguous site concerns what the code of each candidate + can run, through its records too, so the Engine::decode of base64 is uncertain and not no-such-target. + std::fs::remove_file, which only the uncalled cleanup calls, is not found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +unresolved: + - caller: src/handlers.rs//H1//handle + site: src/handlers.rs:19 + reason: trait-receiver + method: decode + - caller: src/main.rs//dispatch + site: src/main.rs:9 + reason: ambiguous + method: handle +results: + - target: std//env//var + status: uncertain + call_sites: + - caller: src/handlers.rs//H0//handle + site: src/handlers.rs:5 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:9 + - target: base64//engine//Engine//decode + status: uncertain + uncertainties: + - kind: unresolved-call + site: src/main.rs:9 + - target: std//fs//remove_file + status: not-found + call_sites: + - caller: src/main.rs//cleanup + site: src/main.rs:4 + reached: false diff --git a/test/projectgraph/cases/rust/name-ambiguous-method/project/Cargo.toml b/test/projectgraph/cases/rust/name-ambiguous-method/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-method/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/name-ambiguous-method/project/src/handlers.rs b/test/projectgraph/cases/rust/name-ambiguous-method/project/src/handlers.rs new file mode 100644 index 00000000..d32c474f --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-method/project/src/handlers.rs @@ -0,0 +1,159 @@ +pub struct H0; + +impl H0 { + pub fn handle(&self, s: &str) { + let _ = std::env::var(s); + } +} + +pub trait Codec { + fn decode(&self, s: &str) -> Vec; +} + +pub struct H1 { + pub codec: Box, +} + +impl H1 { + pub fn handle(&self, s: &str) { + self.codec.decode(s); + } +} + +pub struct H2; + +impl H2 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H3; + +impl H3 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H4; + +impl H4 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H5; + +impl H5 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H6; + +impl H6 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H7; + +impl H7 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H8; + +impl H8 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H9; + +impl H9 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H10; + +impl H10 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H11; + +impl H11 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H12; + +impl H12 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H13; + +impl H13 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H14; + +impl H14 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H15; + +impl H15 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H16; + +impl H16 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H17; + +impl H17 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H18; + +impl H18 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H19; + +impl H19 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H20; + +impl H20 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H21; + +impl H21 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H22; + +impl H22 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H23; + +impl H23 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H24; + +impl H24 { + pub fn handle(&self, _s: &str) {} +} diff --git a/test/projectgraph/cases/rust/name-ambiguous-method/project/src/main.rs b/test/projectgraph/cases/rust/name-ambiguous-method/project/src/main.rs new file mode 100644 index 00000000..19a82879 --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-method/project/src/main.rs @@ -0,0 +1,15 @@ +mod handlers; + +fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} + +fn dispatch(items: Vec, s: &str) { + for h in items { + h.handle(s); + } +} + +fn main() { + dispatch(vec![handlers::H0], "a"); +} diff --git a/test/projectgraph/cases/rust/name-ambiguous-param/expect.yaml b/test/projectgraph/cases/rust/name-ambiguous-param/expect.yaml new file mode 100644 index 00000000..0b5dc406 --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-param/expect.yaml @@ -0,0 +1,38 @@ +source: safedep:hand +description: >- + dispatch calls its parameter handle, which holds the function that pick returns. 25 types define a + method handle. That is over the cap of 20, so the site gets no name edge and is ambiguous. A Rust call + of a name with no receiver can run any function value, and the index records no reference for the tail + expression cleanup of pick. So the site keeps no method name and concerns every target, and + std::fs::remove_file, which cleanup calls, is uncertain and not not-found. So is std::env::var, which + H0.handle calls. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +unresolved: + - caller: src/main.rs//dispatch + site: src/main.rs:12 + reason: ambiguous +results: + - target: std//fs//remove_file + status: uncertain + call_sites: + - caller: src/main.rs//cleanup + site: src/main.rs:4 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:12 + - target: std//env//var + status: uncertain + call_sites: + - caller: src/handlers.rs//H0//handle + site: src/handlers.rs:5 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:12 diff --git a/test/projectgraph/cases/rust/name-ambiguous-param/project/Cargo.toml b/test/projectgraph/cases/rust/name-ambiguous-param/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-param/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/name-ambiguous-param/project/src/handlers.rs b/test/projectgraph/cases/rust/name-ambiguous-param/project/src/handlers.rs new file mode 100644 index 00000000..34d67801 --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-param/project/src/handlers.rs @@ -0,0 +1,151 @@ +pub struct H0; + +impl H0 { + pub fn handle(&self, s: &str) { + let _ = std::env::var(s); + } +} + +pub struct H1; + +impl H1 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H2; + +impl H2 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H3; + +impl H3 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H4; + +impl H4 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H5; + +impl H5 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H6; + +impl H6 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H7; + +impl H7 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H8; + +impl H8 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H9; + +impl H9 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H10; + +impl H10 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H11; + +impl H11 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H12; + +impl H12 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H13; + +impl H13 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H14; + +impl H14 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H15; + +impl H15 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H16; + +impl H16 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H17; + +impl H17 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H18; + +impl H18 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H19; + +impl H19 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H20; + +impl H20 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H21; + +impl H21 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H22; + +impl H22 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H23; + +impl H23 { + pub fn handle(&self, _s: &str) {} +} + +pub struct H24; + +impl H24 { + pub fn handle(&self, _s: &str) {} +} diff --git a/test/projectgraph/cases/rust/name-ambiguous-param/project/src/main.rs b/test/projectgraph/cases/rust/name-ambiguous-param/project/src/main.rs new file mode 100644 index 00000000..51768849 --- /dev/null +++ b/test/projectgraph/cases/rust/name-ambiguous-param/project/src/main.rs @@ -0,0 +1,17 @@ +mod handlers; + +fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} + +fn pick() -> fn(&str) { + cleanup +} + +fn dispatch(handle: fn(&str), s: &str) { + handle(s) +} + +fn main() { + dispatch(pick(), "a"); +} From ac0bd9a4eb20c49f8b922f934bb6784ebbb2c771 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 12:51:54 +0000 Subject: [PATCH 43/58] test(projectgraph): update the hash rows for the second review fixes The fixes of the second review change graphs, results and summaries of the cases. The rows of the versions of this branch take the new hashes in place, because no release holds these versions yet. 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 8ed986da..2dc68161 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -32,7 +32,7 @@ var graphHashes = map[string]string{ "14/15": "bbc708aba767ccf554c75a7e06816abda3fc1804ad6d0876ad8ce9575f4c3a88", "14/16": "823b1f82c4aed8ba27d19275505c8d1dee755086be48fcda64d323f55b4c62a5", "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", - "16/18": "917c95d5abe3d1b0b6d2fd07825fd34892e2b5a79311a436ccece8d1bd63ebc7", + "16/18": "874641322c73f0fb4e36e6eef1d82dcddb49124155ecfe2c0d8e684daad20b9d", } // resultHashes records the hash of the reach results of every case for @@ -49,7 +49,7 @@ var resultHashes = map[string]string{ "14/15/3": "77dfd0ea8ab71869ef612d41b8c2e46852d4fb67a49458cb645321692af77ea0", "14/16/3": "13b6eecb686ddec3c2ba470fc9752ea66cff445d5e18e0772a71b15ae7fe1c9c", "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", - "16/18/5": "debdac29066cb2544a07cde77c546924bd1c26528a752871905b2ef5ebd8ea72", + "16/18/5": "7e2da26a54509db6379456f6869d0fd60d33b77403c36532da9d91c7d45cb1df", } // summaryHashes records the hash of the golden summaries of the cases for @@ -67,7 +67,7 @@ var summaryHashes = map[string]string{ "14/15/2": "5969f3cda15b666cbd902bbc2db953cbdef8c0ce48d5e77446cf243cdd0bb0e7", "14/16/2": "fb678202978212aae932e50a56a0d3dc7f0f794ce908988bf605ce9a6e04bc51", "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", - "16/18/3": "cdac0f560211526c43b8c9141a3bb92663b36b6b555269cf4cfb9d19c7879436", + "16/18/3": "8d06ed658dcbfd6007a74a7c6104e75beb5e7d33a94f2748f9e5f263db2539da", } func TestGraphHashFollowsVersion(t *testing.T) { From 7f6516eb8a4aebaf00ec7e0b7f2354dd53af4432 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 13:00:33 +0000 Subject: [PATCH 44/58] docs(projectgraph): update the method measure of the reach MVP branch The fixes of the second review move the cause labels of some uncertain answers. An ambiguous site of Python concerns every target again, as on main. The found counts and the agreement with govulncheck do not change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- test/projectgraph/report.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/projectgraph/report.md b/test/projectgraph/report.md index 8151e980..e9e4941f 100644 --- a/test/projectgraph/report.md +++ b/test/projectgraph/report.md @@ -1484,14 +1484,14 @@ same corpus on origin/main at 39cf1e2, without the unit. | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | Go | function | main | 256 | 51 | 0 | 0 | 2 | 3 | 5 | 0 | 106 | 39 | 50 | | Go | function | without | 256 | 58 | 0 | 0 | 193 | 5 | 0 | 0 | 0 | 0 | 0 | -| Go | function | with | 256 | 59 | 0 | 0 | 187 | 8 | 1 | 0 | 1 | 0 | 0 | +| Go | function | with | 256 | 59 | 0 | 0 | 187 | 8 | 2 | 0 | 0 | 0 | 0 | | Go | method | main | 1298 | 331 | 0 | 0 | 2 | 154 | 71 | 2 | 416 | 109 | 213 | | Go | method | without | 1298 | 343 | 0 | 7 | 776 | 156 | 15 | 1 | 0 | 0 | 0 | -| Go | method | with | 1298 | 384 | 0 | 0 | 432 | 253 | 184 | 14 | 31 | 0 | 0 | +| Go | method | with | 1298 | 384 | 0 | 0 | 442 | 253 | 205 | 14 | 0 | 0 | 0 | | Rust | function | main | 13 | 4 | 0 | 0 | 0 | 0 | 9 | 0 | 0 | 0 | 0 | -| Rust | function | branch | 13 | 5 | 0 | 0 | 0 | 7 | 1 | 0 | 0 | 0 | 0 | +| Rust | function | branch | 13 | 5 | 0 | 0 | 0 | 5 | 1 | 0 | 2 | 0 | 0 | | Rust | method | main | 38 | 16 | 0 | 0 | 0 | 0 | 13 | 0 | 0 | 9 | 0 | -| Rust | method | branch | 38 | 16 | 0 | 0 | 0 | 12 | 1 | 0 | 0 | 9 | 0 | +| Rust | method | branch | 38 | 16 | 0 | 0 | 0 | 8 | 1 | 0 | 4 | 9 | 0 | | Run | govulncheck reaches | reach found | reach uncertain | false negative | govulncheck does not reach | reach not-found or no-such-target | reach found (review) | | --- | --- | --- | --- | --- | --- | --- | --- | @@ -1500,7 +1500,7 @@ same corpus on origin/main at 39cf1e2, without the unit. - No target is a false negative in either run. A measure run fails on one. - Reach finds 71 of the 115 targets that govulncheck reaches. Main found 60. -- The global causes of main are gone: `ambiguous`, `budget` and +- The global causes of main are gone for Go: `ambiguous`, `budget` and `computed-name`. Most of their targets now read `no-dependency-source`. That record marks code outside the graph that can call back into it: a module with no unit, or the standard library with no unit. The corpus @@ -1508,6 +1508,12 @@ same corpus on origin/main at 39cf1e2, without the unit. modules of a build have no unit. A consumer that gives a unit to each module with source on the disk, and to GOROOT, removes most of these records. -- Python does not change. The Rust `no-definition` records of main fall - from 22 to 2. The rest are `unknown-receiver` records of calls that the - link step does not type yet, as a turbofish or a `::m` call. +- The answers of Python do not change. An ambiguous site of Python, + JavaScript, TypeScript, Ruby and PHP concerns every target, as on main, + because a call by name there can run a callable object, a + `__getattr__` value or a module attribute. Go keeps the method name of an + ambiguous site in a search from the roots, and Rust keeps it for a call + on a receiver. +- The Rust `no-definition` records of main fall from 22 to 2. The rest are + `unknown-receiver` and `ambiguous` records of calls that the link step + does not type yet, as a turbofish or a `::m` call. From c799adbccaa6d0e974d667983cd6f8ed0f09e059 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 13:25:36 +0000 Subject: [PATCH 45/58] fix(reach): let a method record concern the aliases of its name (FOU-557) A Rust call of into runs the from of an impl of From. The link step adds name edges to the from candidates of the project. But the record of the site keeps only the method name into. When the site has more than 20 candidates, it is ambiguous and gets no edge. Then the reach query starts the scope only from the definitions of into, and a target that one from reaches reads as not-found. The same holds for a from of a dependency, which is never a candidate. The span of a method scope now enters the scope of each alias of the name, as the profiles give them. MethodAliases takes the aliases of every language, because the reach query does not read the language of a site. The cases rust/into-ambiguous and rust/into-dependency fail without the change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 6 + projectgraph/profile.go | 14 ++ projectgraph/reach/span.go | 4 + .../cases/rust/into-ambiguous/expect.yaml | 36 ++++ .../rust/into-ambiguous/project/Cargo.toml | 4 + .../into-ambiguous/project/src/convert.rs | 178 ++++++++++++++++++ .../rust/into-ambiguous/project/src/main.rs | 17 ++ .../cases/rust/into-dependency/expect.yaml | 40 ++++ .../rust/into-dependency/project/Cargo.toml | 7 + .../rust/into-dependency/project/src/main.rs | 11 ++ .../project/vendor/kit/Cargo.toml | 4 + .../project/vendor/kit/src/lib.rs | 14 ++ .../summaries/cargo_kit_1.0.0.json | 37 ++++ 13 files changed, 372 insertions(+) create mode 100644 test/projectgraph/cases/rust/into-ambiguous/expect.yaml create mode 100644 test/projectgraph/cases/rust/into-ambiguous/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/into-ambiguous/project/src/convert.rs create mode 100644 test/projectgraph/cases/rust/into-ambiguous/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/into-dependency/expect.yaml create mode 100644 test/projectgraph/cases/rust/into-dependency/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/into-dependency/project/src/main.rs create mode 100644 test/projectgraph/cases/rust/into-dependency/project/vendor/kit/Cargo.toml create mode 100644 test/projectgraph/cases/rust/into-dependency/project/vendor/kit/src/lib.rs create mode 100644 test/projectgraph/cases/rust/into-dependency/summaries/cargo_kit_1.0.0.json diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 4d20a8aa..ac7e488d 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -730,6 +730,12 @@ causes it. An uncertainty concerns every target, unless its site says less. A call with an unknown receiver and a method name concerns the targets with that last segment, and the targets that any definition of that name reaches. A +call of a Rust conversion runs a method of another name, as `into` runs the +`from` of an impl of `From`. So the record concerns the method names that +the profile of Rust gives for the name too. The reach query does not read +the language of a site, so a call of the name in another language concerns +them too. The cases `rust/into-ambiguous` and `rust/into-dependency` show +it. A computed import with a static prefix concerns the targets under the prefix. A call into a package at no known definition concerns the targets that the code of the package reaches. Code of the Go standard library that diff --git a/projectgraph/profile.go b/projectgraph/profile.go index 1c899bb3..04983797 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -265,6 +265,20 @@ func profileOf(lang langcode.Code) *profile { return noProfile } +// MethodAliases returns the names of the methods that a call of the method +// name can run by another name, as from for into of Rust. The reach query +// does not read the language of a site, so it takes the aliases of every +// language. Without them, a call of into whose From impl calls a target +// concerns only the targets that a method into reaches, and the target +// reads as not-found. +func MethodAliases(method string) []string { + var out []string + for _, p := range profiles { + out = append(out, p.methodAliases[method]...) + } + return out +} + // keepsAmbiguousMethod reports whether an ambiguous site with the receiver // keeps its method name, so the reach query scopes it by the name. A call // on a receiver of a language with receiverMethods runs only a method of diff --git a/projectgraph/reach/span.go b/projectgraph/reach/span.go index 9c39c2b9..2abcfc00 100644 --- a/projectgraph/reach/span.go +++ b/projectgraph/reach/span.go @@ -4,6 +4,7 @@ import ( "slices" "github.com/safedep/code/fileindex" + "github.com/safedep/code/projectgraph" ) // scopeKey names the code that a scoped uncertainty can run: the nodes @@ -124,6 +125,9 @@ func (w *spanWalk) enter(k scopeKey) { for _, id := range w.a.definitionsOf(k.method) { w.add(id, false) } + for _, alias := range projectgraph.MethodAliases(k.method) { + w.enter(scopeKey{method: alias}) + } } } diff --git a/test/projectgraph/cases/rust/into-ambiguous/expect.yaml b/test/projectgraph/cases/rust/into-ambiguous/expect.yaml new file mode 100644 index 00000000..be7c4d21 --- /dev/null +++ b/test/projectgraph/cases/rust/into-ambiguous/expect.yaml @@ -0,0 +1,36 @@ +source: safedep:hand +description: >- + dispatch calls into on an element of a vector of a generic type, which the link step does not know. A call + of into runs the from of an impl of From, so the method from gives the candidates too. 22 types implement + From. That is over the cap of 20, so the site gets no name edge and is ambiguous, and it keeps the + method name into. The from of D0 calls std::env::var. The record concerns what a method of the name or of + an alias of the name reaches, so std::env::var is uncertain and not not-found. std::fs::remove_file, which + only the uncalled cleanup calls, is not found. +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +unresolved: + - caller: src/main.rs//dispatch + site: src/main.rs:10 + reason: ambiguous + method: into +results: + - target: std//env//var + status: uncertain + call_sites: + - caller: src/convert.rs//D0//from + site: src/convert.rs:7 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:10 + - target: std//fs//remove_file + status: not-found + call_sites: + - caller: src/main.rs//cleanup + site: src/main.rs:4 + reached: false diff --git a/test/projectgraph/cases/rust/into-ambiguous/project/Cargo.toml b/test/projectgraph/cases/rust/into-ambiguous/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/into-ambiguous/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/into-ambiguous/project/src/convert.rs b/test/projectgraph/cases/rust/into-ambiguous/project/src/convert.rs new file mode 100644 index 00000000..55d28122 --- /dev/null +++ b/test/projectgraph/cases/rust/into-ambiguous/project/src/convert.rs @@ -0,0 +1,178 @@ +pub struct In; + +pub struct D0; + +impl From for D0 { + fn from(_s: In) -> D0 { + let _ = std::env::var("HOME"); + D0 + } +} + +pub struct D1; + +impl From for D1 { + fn from(_s: In) -> D1 { + D1 + } +} + +pub struct D2; + +impl From for D2 { + fn from(_s: In) -> D2 { + D2 + } +} + +pub struct D3; + +impl From for D3 { + fn from(_s: In) -> D3 { + D3 + } +} + +pub struct D4; + +impl From for D4 { + fn from(_s: In) -> D4 { + D4 + } +} + +pub struct D5; + +impl From for D5 { + fn from(_s: In) -> D5 { + D5 + } +} + +pub struct D6; + +impl From for D6 { + fn from(_s: In) -> D6 { + D6 + } +} + +pub struct D7; + +impl From for D7 { + fn from(_s: In) -> D7 { + D7 + } +} + +pub struct D8; + +impl From for D8 { + fn from(_s: In) -> D8 { + D8 + } +} + +pub struct D9; + +impl From for D9 { + fn from(_s: In) -> D9 { + D9 + } +} + +pub struct D10; + +impl From for D10 { + fn from(_s: In) -> D10 { + D10 + } +} + +pub struct D11; + +impl From for D11 { + fn from(_s: In) -> D11 { + D11 + } +} + +pub struct D12; + +impl From for D12 { + fn from(_s: In) -> D12 { + D12 + } +} + +pub struct D13; + +impl From for D13 { + fn from(_s: In) -> D13 { + D13 + } +} + +pub struct D14; + +impl From for D14 { + fn from(_s: In) -> D14 { + D14 + } +} + +pub struct D15; + +impl From for D15 { + fn from(_s: In) -> D15 { + D15 + } +} + +pub struct D16; + +impl From for D16 { + fn from(_s: In) -> D16 { + D16 + } +} + +pub struct D17; + +impl From for D17 { + fn from(_s: In) -> D17 { + D17 + } +} + +pub struct D18; + +impl From for D18 { + fn from(_s: In) -> D18 { + D18 + } +} + +pub struct D19; + +impl From for D19 { + fn from(_s: In) -> D19 { + D19 + } +} + +pub struct D20; + +impl From for D20 { + fn from(_s: In) -> D20 { + D20 + } +} + +pub struct D21; + +impl From for D21 { + fn from(_s: In) -> D21 { + D21 + } +} diff --git a/test/projectgraph/cases/rust/into-ambiguous/project/src/main.rs b/test/projectgraph/cases/rust/into-ambiguous/project/src/main.rs new file mode 100644 index 00000000..4e7bcddc --- /dev/null +++ b/test/projectgraph/cases/rust/into-ambiguous/project/src/main.rs @@ -0,0 +1,17 @@ +mod convert; + +fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} + +fn dispatch>(items: Vec) -> Vec { + let mut out = Vec::new(); + for h in items { + out.push(h.into()); + } + out +} + +fn main() { + let _d: Vec = dispatch(vec![convert::In]); +} diff --git a/test/projectgraph/cases/rust/into-dependency/expect.yaml b/test/projectgraph/cases/rust/into-dependency/expect.yaml new file mode 100644 index 00000000..6060e883 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/expect.yaml @@ -0,0 +1,40 @@ +source: safedep:hand +description: >- + dispatch calls into on an element of a vector of a generic type, which the link step does not know. The + crate kit implements From for Store, and that from calls std::fs::remove_file. The site gets name + edges only to the candidates of the project, and the project has none, so the site is a record with the + method into. A call of into runs the from of an impl of From. So the record concerns what a method from + reaches, and kit//Store//from and std::fs::remove_file are uncertain and not not-found. +units: + vendor/kit: cargo:kit@1.0.0 +roots: + - key: src/main.rs + kind: main + rule: rust.main + - key: src/main.rs//main + kind: main + rule: rust.main +unresolved: + - caller: src/main.rs//dispatch + site: src/main.rs:4 + reason: unknown-receiver + method: into +results: + - target: kit//Store//from + status: uncertain + call_sites: + - caller: cargo:kit@1.0.0//src/lib.rs//Store + site: cargo:kit@1.0.0//src/lib.rs:6 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:4 + - target: std//fs//remove_file + status: uncertain + call_sites: + - caller: cargo:kit@1.0.0//src/lib.rs//Store//from + site: cargo:kit@1.0.0//src/lib.rs:7 + reached: false + uncertainties: + - kind: unresolved-call + site: src/main.rs:4 diff --git a/test/projectgraph/cases/rust/into-dependency/project/Cargo.toml b/test/projectgraph/cases/rust/into-dependency/project/Cargo.toml new file mode 100644 index 00000000..492d638f --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/project/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[dependencies] +kit = "1.0.0" diff --git a/test/projectgraph/cases/rust/into-dependency/project/src/main.rs b/test/projectgraph/cases/rust/into-dependency/project/src/main.rs new file mode 100644 index 00000000..289f9bd6 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/project/src/main.rs @@ -0,0 +1,11 @@ +fn dispatch>(items: Vec) -> Vec { + let mut out = Vec::new(); + for h in items { + out.push(h.into()); + } + out +} + +fn main() { + let _d: Vec = dispatch(vec![kit::raw()]); +} diff --git a/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/Cargo.toml b/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/Cargo.toml new file mode 100644 index 00000000..58da5f79 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "kit" +version = "1.0.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/src/lib.rs b/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/src/lib.rs new file mode 100644 index 00000000..4b2f4304 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/project/vendor/kit/src/lib.rs @@ -0,0 +1,14 @@ +pub struct Raw; + +pub struct Store; + +impl From for Store { + fn from(_r: Raw) -> Store { + let _ = std::fs::remove_file("x"); + Store + } +} + +pub fn raw() -> Raw { + Raw +} 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 new file mode 100644 index 00000000..6fac9200 --- /dev/null +++ b/test/projectgraph/cases/rust/into-dependency/summaries/cargo_kit_1.0.0.json @@ -0,0 +1,37 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "cargo:kit@1.0.0", + "symbols": [ + { + "key": "kit//Raw", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Raw" + ], + "reaches": [] + }, + { + "key": "kit//Store", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store" + ], + "reaches": [] + }, + { + "key": "kit//Store//from", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//Store//from" + ], + "reaches": [] + }, + { + "key": "kit//raw", + "definitions": [ + "cargo:kit@1.0.0//src/lib.rs//raw" + ], + "reaches": [] + } + ], + "targets": [] +} From 4e7940d12621a4f17fd80f7865e358eb7cd052ec Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 13:25:36 +0000 Subject: [PATCH 46/58] fix(indexer): keep a predeclared Go name as a reference (FOU-560) The index dropped a reference to a predeclared name, as clear, unless the file itself declared the name. A declaration of another file of the package shadows the name in every file of the package. So []func(){clear} in main.go gave no reference to the clear of other.go. The call of the element is a function-value record, which concerns no target in a search from the roots, and the target of clear read as not-found. goName now keeps a predeclared name as a name. The link step resolves it in the package, or drops it when no declaration binds it, as it does for other names that the file does not bind. The case go/predeclared-shadow fails without the change. 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 | 15 +++- .../cases/go/predeclared-shadow/expect.yaml | 75 +++++++++++++++++++ .../go/predeclared-shadow/project/go.mod | 3 + .../go/predeclared-shadow/project/main.go | 15 ++++ .../go/predeclared-shadow/project/other.go | 13 ++++ 6 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 test/projectgraph/cases/go/predeclared-shadow/expect.yaml create mode 100644 test/projectgraph/cases/go/predeclared-shadow/project/go.mod create mode 100644 test/projectgraph/cases/go/predeclared-shadow/project/main.go create mode 100644 test/projectgraph/cases/go/predeclared-shadow/project/other.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index ac7e488d..717295c5 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -816,7 +816,12 @@ a call can run. A Go function value is one of these: another file of the package, a member chain, a method expression, as `(*T).M`, a method value on a call, a composite literal, a type assertion, `new` or a dereference, as `f().M`, or a generic instantiation, as `F[int]`. - Each of them can be in parentheses or behind `&`. + Each of them can be in parentheses or behind `&`. The index keeps a + predeclared name, as `clear`, as a name too. A declaration of another + file of the package can shadow it, and the index of one file does not + see that file. The link step resolves the name in the package, or drops + it when no declaration binds it. The case `go/predeclared-shadow` shows + it. - A method value whose method the index or the link step cannot name, as `ts[0].Close` or `h.Handle` on a value of an interface. The index or the link step records the reference with the method name. That record diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index e585a4c8..0e0dd0a8 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -148,8 +148,10 @@ func (p *projection) goTypeTarget(caller string, node *sitter.Node) (string, boo // goName returns the per-file target of a name: the definition that the // scope chain binds, a typed local, or an import. A name that no local of // the caller declares keeps its text, as a function of another file of -// the package, and the link step resolves it or drops it. A local that the -// index does not type, or a predeclared name, gives none. +// the package, and the link step resolves it or drops it. A predeclared +// name keeps its text too, because a function of another file of the +// package can shadow it, as func clear(). A local that the index does not +// type gives none. func (p *projection) goName(caller string, node *sitter.Node) (string, bool) { if node == nil { return "", false @@ -158,14 +160,19 @@ func (p *projection) goName(caller string, node *sitter.Node) (string, bool) { if target, ok := p.valueOf(*p.rules().references, caller, name); ok { return target, true } - if name == "_" || p.goPredeclared(name) || p.goLocal(caller, name, node.StartByte()) { + if name == "_" || p.goLocal(caller, name, node.StartByte()) { return "", false } return name, true } // goPredeclared reports whether a name is a predeclared name of Go, as int -// or len, that no definition of the file shadows. +// or len, that no definition of the file shadows. A declaration of another +// file of the package can still shadow it. So a reference keeps the name, +// and only two rules read this. The Data mark of a package variable hides +// no function, because its value is a reference of its file, and a search +// reaches the file with the variable. new and make take a type argument +// only as the builtins. func (p *projection) goPredeclared(name string) bool { return langcode.IsBuiltin(langcode.Go, name) && p.defs[p.local(name)] == nil } diff --git a/test/projectgraph/cases/go/predeclared-shadow/expect.yaml b/test/projectgraph/cases/go/predeclared-shadow/expect.yaml new file mode 100644 index 00000000..47668b1b --- /dev/null +++ b/test/projectgraph/cases/go/predeclared-shadow/expect.yaml @@ -0,0 +1,75 @@ +source: safedep:hand +description: >- + other.go declares the function clear and the type any. Both names are predeclared names of Go, and a + declaration of the package shadows them in every file of the package. main stores clear in a slice and + calls an element of it. The index of main.go does not see the declaration in other.go, so it keeps clear + as a name, and the link step resolves it in the package to other.go//clear. The call fs[0]() is a + function-value record that concerns no target, so the reference edge is what makes os.Remove found. The + variable hook has the type any, which the index of main.go takes for the predeclared any and marks as + data. Its value rm is a reference of the file main.go, so os.Getenv is found through the file. No code + calls unused or uses it as a value, so os.Getwd is not found. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main + - key: other.go + kind: main + rule: go.main +no_edges: + - from: main.go//main + to: main.go//unused +unresolved: + - 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: main.go//main + site: main.go:13 + reason: function-value +results: + - target: os//Remove + status: found + path: + - key: main.go//main + - key: other.go//clear + site: main.go:12 + kind: reference + confidence: low + - key: os//Remove + site: other.go:8 + kind: direct + confidence: high + call_sites: + - caller: other.go//clear + site: other.go:8 + reached: true + - target: os//Getenv + status: found + path: + - key: main.go + - key: other.go//rm + site: main.go:5 + kind: reference + confidence: low + - key: os//Getenv + site: other.go:12 + kind: direct + confidence: high + call_sites: + - caller: other.go//rm + site: other.go:12 + reached: true + - target: os//Getwd + status: not-found + call_sites: + - caller: main.go//unused + site: main.go:8 + reached: false diff --git a/test/projectgraph/cases/go/predeclared-shadow/project/go.mod b/test/projectgraph/cases/go/predeclared-shadow/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/predeclared-shadow/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/predeclared-shadow/project/main.go b/test/projectgraph/cases/go/predeclared-shadow/project/main.go new file mode 100644 index 00000000..23bbeefa --- /dev/null +++ b/test/projectgraph/cases/go/predeclared-shadow/project/main.go @@ -0,0 +1,15 @@ +package main + +import "os" + +var hook any = rm + +func unused() { + os.Getwd() +} + +func main() { + fs := []func(){clear} + fs[0]() + hook() +} diff --git a/test/projectgraph/cases/go/predeclared-shadow/project/other.go b/test/projectgraph/cases/go/predeclared-shadow/project/other.go new file mode 100644 index 00000000..5ff2ac80 --- /dev/null +++ b/test/projectgraph/cases/go/predeclared-shadow/project/other.go @@ -0,0 +1,13 @@ +package main + +import "os" + +type any func() + +func clear() { + os.Remove("x") +} + +func rm() { + os.Getenv("x") +} From 93b9a3c61e5d74043cc6f71d04ec27ca5ba1a296 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 13:25:36 +0000 Subject: [PATCH 47/58] fix(projectgraph): treat a Go plugin as reflection (FOU-560) A Go program can load a plugin with plugin.Open, get a function of it with Lookup, and call it as sym.(func())(). The plugin is code of another build, so no reference edge names that function. The call is a function-value record, which concerns no target in a search from the roots. So a target that only the plugin calls read as not-found, and a target that no code of the graph names read as no-such-target. A unit of the standard library does not help, because its plugin code fills the symbols from code of another build. The reflection rule of the Go profile now holds a list of packages. A call or a reference of Open or Lookup in a file that imports plugin gets a computed-name record, as Call or MethodByName in a file that imports reflect does. docs/projectgraph.md argues why no other package of the standard library gives a function that no reference names. The cases go/plugin-lookup and go/plugin-lookup-std fail without the change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 24 ++++++- projectgraph/golang.go | 9 ++- projectgraph/profile.go | 6 +- projectgraph/values.go | 15 ++-- .../cases/go/plugin-lookup-std/expect.yaml | 45 ++++++++++++ .../cases/go/plugin-lookup-std/project/go.mod | 3 + .../project/goroot/src/go.mod | 3 + .../project/goroot/src/os/file.go | 5 ++ .../project/goroot/src/plugin/plugin.go | 15 ++++ .../go/plugin-lookup-std/project/main.go | 15 ++++ .../project/plugins/hook/hook.go | 7 ++ .../summaries/go_std_go1.25.1.json | 44 ++++++++++++ .../cases/go/plugin-lookup/expect.yaml | 70 +++++++++++++++++++ .../cases/go/plugin-lookup/project/go.mod | 3 + .../cases/go/plugin-lookup/project/main.go | 15 ++++ .../project/plugins/hook/hook.go | 7 ++ 16 files changed, 274 insertions(+), 12 deletions(-) create mode 100644 test/projectgraph/cases/go/plugin-lookup-std/expect.yaml create mode 100644 test/projectgraph/cases/go/plugin-lookup-std/project/go.mod create mode 100644 test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/go.mod create mode 100644 test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/os/file.go create mode 100644 test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/plugin/plugin.go create mode 100644 test/projectgraph/cases/go/plugin-lookup-std/project/main.go create mode 100644 test/projectgraph/cases/go/plugin-lookup-std/project/plugins/hook/hook.go create mode 100644 test/projectgraph/cases/go/plugin-lookup-std/summaries/go_std_go1.25.1.json create mode 100644 test/projectgraph/cases/go/plugin-lookup/expect.yaml create mode 100644 test/projectgraph/cases/go/plugin-lookup/project/go.mod create mode 100644 test/projectgraph/cases/go/plugin-lookup/project/main.go create mode 100644 test/projectgraph/cases/go/plugin-lookup/project/plugins/hook/hook.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 717295c5..952727c4 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -844,7 +844,25 @@ a call can run. A Go function value is one of these: `CallSlice`, `Method` or `MethodByName` in a file that imports `reflect` gets a `computed-name` record, which concerns every target. The link step does not always know that the receiver is a `reflect.Value`, so the rule - takes any receiver. + takes any receiver. Each other form of `reflect` gives back a value that + code stored with a reference, or a method that these methods give. So + `Value.Interface().(func())` needs no record of its own. `MakeFunc` makes + a function that calls the function of its argument, and the argument is + a reference. +- A value that a plugin gives, as `sym.(func())()` after `plugin.Open` + and `Plugin.Lookup`. The plugin is code of another build. `Open` runs + its init, and `Lookup` gives its functions and variables. No reference + of the graph names them, also when a unit gives the standard library. So + a call or a reference of `Open` or `Lookup` in a file that imports + `plugin` gets a `computed-name` record too, in both modes. The cases + `go/plugin-lookup` and `go/plugin-lookup-std` show it. + +No other package of the standard library gives a function that no +reference names. A callback that the standard library stores, as the +function of `syscall.NewCallback`, `sync.OnceFunc` or `runtime.SetFinalizer`, +is an argument, so a reference names it. `runtime.FuncForPC` gives no +function that code can call. A conversion with `unsafe` of a pointer to a +function value needs that value first, and a reference names it. A function value that code outside the graph makes, as the result of `sync.OnceFunc(g)`, runs code outside the graph. A call of that code makes @@ -974,7 +992,9 @@ depends on what the code can call. run code of another module only through a value that it holds: a function value, or a method of a value. A function value has a reference edge from the code that makes it, so the search reaches the function without the - standard library. See [A call of a function value](#a-call-of-a-function-value). + standard library. A function of a plugin has no such edge, so a call of + `plugin.Open` or `Lookup` concerns every target. See + [A call of a function value](#a-call-of-a-function-value). A method of a value runs through an interface or through reflection. A type outside the standard library cannot declare a method of an unexported name of an interface of the standard library. It can only diff --git a/projectgraph/golang.go b/projectgraph/golang.go index 2d68d9d7..134d6f4c 100644 --- a/projectgraph/golang.go +++ b/projectgraph/golang.go @@ -35,8 +35,13 @@ var goProfile = profile{ importsBindAlias: true, functionValues: true, // Call and CallSlice call a function value, and Method and MethodByName - // give a method by its index or its name. - reflection: &reflection{module: "reflect", methods: map[string]bool{"Call": true, "CallSlice": true, "Method": true, "MethodByName": true}}, + // give a method by its index or its name. Open of plugin runs the init + // of a plugin, and Lookup gives a function of it. The plugin is code of + // another build. + reflection: []reflection{ + {module: "reflect", methods: map[string]bool{"Call": true, "CallSlice": true, "Method": true, "MethodByName": true}}, + {module: "plugin", methods: map[string]bool{"Open": true, "Lookup": true}}, + }, } // goProvide returns the package unit of a Go module that an import path diff --git a/projectgraph/profile.go b/projectgraph/profile.go index 04983797..af2a7e82 100644 --- a/projectgraph/profile.go +++ b/projectgraph/profile.go @@ -230,10 +230,10 @@ type profile struct { // x.m(), runs only a method of the name m, as Rust. A Rust field that // holds a function needs another form, as (x.f)(). receiverMethods bool - // reflection names the package of the language that can run any - // function by a value or by a name, and its methods that do it, as + // reflection names the packages of the language that give or run a + // function that no reference names, and their methods that do it, as // Call of reflect.Value in Go. - reflection *reflection + reflection []reflection // helperProgram reports whether the main of a file is a helper // program of the project and not the program that its users run, as a // build script or an example of a Rust crate. Its root stays, and it diff --git a/projectgraph/values.go b/projectgraph/values.go index f1eaa58a..d2d9ac25 100644 --- a/projectgraph/values.go +++ b/projectgraph/values.go @@ -24,8 +24,8 @@ 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}) } -// reflection is the package of a language that can run any function by a -// value or by a name, and the methods of it that do it. +// 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 { module string methods map[string]bool @@ -36,7 +36,7 @@ type reflection struct { // The link step does not always know that the receiver is a value of the // package, as when no unit gives the standard library. So any receiver // counts. -func (r *reflection) reflective(ix *fileindex.Index, method string) bool { +func (r reflection) reflective(ix *fileindex.Index, method string) bool { return r.methods[method] && slices.ContainsFunc(ix.Imports, func(imp fileindex.Import) bool { return imp.Module == r.module }) } @@ -47,10 +47,15 @@ func (r *reflection) reflective(ix *fileindex.Index, method string) bool { // every target resolves to a definition outside the package of reflection, // as stream.Method() on a type of the project, gets no record. func (b *builder) linkReflection(ix *fileindex.Index) { - refl := profileOf(ix.Language).reflection - if refl == nil || isDeclarationFile(ix.Path) { + if isDeclarationFile(ix.Path) { return } + for _, refl := range profileOf(ix.Language).reflection { + b.linkReflectionOf(ix, refl) + } +} + +func (b *builder) linkReflectionOf(ix *fileindex.Index, refl reflection) { sc := b.scopes[ix.Path] for _, call := range ix.Calls { if call.Kind == fileindex.CallImport || !refl.reflective(ix, cmp.Or(call.Method, lastWritten(call.Callee))) { diff --git a/test/projectgraph/cases/go/plugin-lookup-std/expect.yaml b/test/projectgraph/cases/go/plugin-lookup-std/expect.yaml new file mode 100644 index 00000000..81a8b6e7 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup-std/expect.yaml @@ -0,0 +1,45 @@ +source: safedep:hand +description: >- + The case go/plugin-lookup with a unit of the standard library. The unit gives the code of plugin, so + Open and Lookup resolve into it. That code fills the symbols of a plugin from code of another build, + and no reference of the graph names them. So each call of Open or Lookup in a file that imports plugin + is a computed-name record with the unit too. os.Remove, which only the plugin calls, and os.Getenv, + which no code of the graph names, are uncertain. +units: + goroot/src: go:std@go1.25.1 +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: main.go//main + site: main.go:6 + reason: computed-name + - caller: main.go//main + site: main.go:10 + reason: computed-name + - caller: main.go//main + site: main.go:14 + reason: function-value +results: + - target: os//Remove + status: uncertain + call_sites: + - caller: plugins/hook/hook.go//Run + site: plugins/hook/hook.go:6 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:6 + - kind: unresolved-call + site: main.go:10 + - target: os//Getenv + status: uncertain + uncertainties: + - kind: unresolved-call + site: main.go:6 + - kind: unresolved-call + site: main.go:10 diff --git a/test/projectgraph/cases/go/plugin-lookup-std/project/go.mod b/test/projectgraph/cases/go/plugin-lookup-std/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup-std/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/go.mod b/test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/go.mod new file mode 100644 index 00000000..1ded0318 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/go.mod @@ -0,0 +1,3 @@ +module std + +go 1.25 diff --git a/test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/os/file.go b/test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/os/file.go new file mode 100644 index 00000000..50fefb80 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/os/file.go @@ -0,0 +1,5 @@ +package os + +func Remove(name string) error { + return nil +} diff --git a/test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/plugin/plugin.go b/test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/plugin/plugin.go new file mode 100644 index 00000000..44ee75d9 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup-std/project/goroot/src/plugin/plugin.go @@ -0,0 +1,15 @@ +package plugin + +type Symbol any + +type Plugin struct { + syms map[string]any +} + +func Open(path string) (*Plugin, error) { + return &Plugin{syms: map[string]any{}}, nil +} + +func (p *Plugin) Lookup(symName string) (Symbol, error) { + return p.syms[symName], nil +} diff --git a/test/projectgraph/cases/go/plugin-lookup-std/project/main.go b/test/projectgraph/cases/go/plugin-lookup-std/project/main.go new file mode 100644 index 00000000..7e01f268 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup-std/project/main.go @@ -0,0 +1,15 @@ +package main + +import "plugin" + +func main() { + p, err := plugin.Open("hook.so") + if err != nil { + return + } + sym, err := p.Lookup("Run") + if err != nil { + return + } + sym.(func())() +} diff --git a/test/projectgraph/cases/go/plugin-lookup-std/project/plugins/hook/hook.go b/test/projectgraph/cases/go/plugin-lookup-std/project/plugins/hook/hook.go new file mode 100644 index 00000000..461953aa --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup-std/project/plugins/hook/hook.go @@ -0,0 +1,7 @@ +package main + +import "os" + +func Run() { + os.Remove("x") +} 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 new file mode 100644 index 00000000..f978ead0 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup-std/summaries/go_std_go1.25.1.json @@ -0,0 +1,44 @@ +{ + "version": "3", + "index_version": "16", + "link_version": "18", + "unit": "go:std@go1.25.1", + "symbols": [ + { + "key": "os//Remove", + "definitions": [ + "go:std@go1.25.1//os/file.go//Remove" + ], + "reaches": [] + }, + { + "key": "plugin//Open", + "definitions": [ + "go:std@go1.25.1//plugin/plugin.go//Open" + ], + "reaches": [] + }, + { + "key": "plugin//Plugin", + "definitions": [ + "go:std@go1.25.1//plugin/plugin.go//Plugin" + ], + "reaches": [] + }, + { + "key": "plugin//Plugin//Lookup", + "definitions": [ + "go:std@go1.25.1//plugin/plugin.go//Plugin//Lookup" + ], + "reaches": [] + }, + { + "key": "plugin//Symbol", + "definitions": [ + "go:std@go1.25.1//plugin/plugin.go//Symbol" + ], + "reaches": [] + } + ], + "targets": [] +} diff --git a/test/projectgraph/cases/go/plugin-lookup/expect.yaml b/test/projectgraph/cases/go/plugin-lookup/expect.yaml new file mode 100644 index 00000000..2a316587 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup/expect.yaml @@ -0,0 +1,70 @@ +source: safedep:hand +description: >- + main loads a plugin with plugin.Open, gets the function Run of it with Lookup, and calls it. The plugin + is code of another build, as plugins/hook, and no reference of the graph names its function. Open runs + the init of the plugin, and the function that Lookup gives can call any function. So in a file that + imports plugin, each call of Open or Lookup is a computed-name record, as a call of Call or MethodByName + in a file that imports reflect. os.Remove, which only the plugin calls, and os.Getenv, which no code of + the graph names, are uncertain in a search from the roots and from a source. No unit gives the standard + library here. The case go/plugin-lookup-std gives it. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main +unresolved: + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: plugin//Open + reason: no-dependency-source + callback: true + - caller: main.go//main + site: main.go:6 + reason: computed-name + - caller: main.go//main + site: main.go:10 + reason: computed-name + - caller: main.go//main + site: main.go:10 + reason: no-dependency-source + method: Lookup + - caller: main.go//main + site: main.go:14 + reason: function-value +results: + - target: os//Remove + status: uncertain + call_sites: + - caller: plugins/hook/hook.go//Run + site: plugins/hook/hook.go:6 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:6 + - kind: unresolved-call + site: main.go:10 + - target: os//Getenv + status: uncertain + uncertainties: + - kind: unresolved-call + site: main.go:6 + - kind: unresolved-call + site: main.go:10 + - target: os//Remove + sources: [main.go//main] + sources_only: true + status: uncertain + call_sites: + - caller: plugins/hook/hook.go//Run + site: plugins/hook/hook.go:6 + reached: false + uncertainties: + - kind: unresolved-call + site: main.go:6 + - kind: unresolved-call + site: main.go:10 + - kind: unresolved-call + site: main.go:14 diff --git a/test/projectgraph/cases/go/plugin-lookup/project/go.mod b/test/projectgraph/cases/go/plugin-lookup/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/plugin-lookup/project/main.go b/test/projectgraph/cases/go/plugin-lookup/project/main.go new file mode 100644 index 00000000..7e01f268 --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup/project/main.go @@ -0,0 +1,15 @@ +package main + +import "plugin" + +func main() { + p, err := plugin.Open("hook.so") + if err != nil { + return + } + sym, err := p.Lookup("Run") + if err != nil { + return + } + sym.(func())() +} diff --git a/test/projectgraph/cases/go/plugin-lookup/project/plugins/hook/hook.go b/test/projectgraph/cases/go/plugin-lookup/project/plugins/hook/hook.go new file mode 100644 index 00000000..461953aa --- /dev/null +++ b/test/projectgraph/cases/go/plugin-lookup/project/plugins/hook/hook.go @@ -0,0 +1,7 @@ +package main + +import "os" + +func Run() { + os.Remove("x") +} From 06577dadbc83c7800bbe9eab1f384f9aa63ffac5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 13:38:03 +0000 Subject: [PATCH 48/58] test(projectgraph): update the hash rows for the fixes of the PR review The fixes of the Codex review add cases, references of predeclared Go names and plugin records. The rows of the versions of this branch take the new hashes in place, because no release holds these versions yet. 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 2dc68161..a4b0bc1a 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -32,7 +32,7 @@ var graphHashes = map[string]string{ "14/15": "bbc708aba767ccf554c75a7e06816abda3fc1804ad6d0876ad8ce9575f4c3a88", "14/16": "823b1f82c4aed8ba27d19275505c8d1dee755086be48fcda64d323f55b4c62a5", "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", - "16/18": "874641322c73f0fb4e36e6eef1d82dcddb49124155ecfe2c0d8e684daad20b9d", + "16/18": "1be7368a64b35bbcea38ac1807a0175cb26f5832b49b1d4c8add12caccbfabda", } // resultHashes records the hash of the reach results of every case for @@ -49,7 +49,7 @@ var resultHashes = map[string]string{ "14/15/3": "77dfd0ea8ab71869ef612d41b8c2e46852d4fb67a49458cb645321692af77ea0", "14/16/3": "13b6eecb686ddec3c2ba470fc9752ea66cff445d5e18e0772a71b15ae7fe1c9c", "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", - "16/18/5": "7e2da26a54509db6379456f6869d0fd60d33b77403c36532da9d91c7d45cb1df", + "16/18/5": "5f52d287e988f7f478720ab1ee5b129c7f8af7648a1d3851c9d4b9671710dcf3", } // summaryHashes records the hash of the golden summaries of the cases for @@ -67,7 +67,7 @@ var summaryHashes = map[string]string{ "14/15/2": "5969f3cda15b666cbd902bbc2db953cbdef8c0ce48d5e77446cf243cdd0bb0e7", "14/16/2": "fb678202978212aae932e50a56a0d3dc7f0f794ce908988bf605ce9a6e04bc51", "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", - "16/18/3": "8d06ed658dcbfd6007a74a7c6104e75beb5e7d33a94f2748f9e5f263db2539da", + "16/18/3": "00874f23561eff140ecdb5472fc3745b2314c84a619ac2ceef4494762cad893d", } func TestGraphHashFollowsVersion(t *testing.T) { From c642d32edb98297ef1722881135195df9a6835b8 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 13:47:37 +0000 Subject: [PATCH 49/58] fix(indexer): check the scope of a typed Go local before it hides a name The index records the typed locals of a Go function with no extent. So a typed local of a closed block hid a package function with the same name. In `{ var helper T; _ = helper }; fs := []func(){helper}`, the index gave no reference to the function helper of another file. The call fs[0]() is a function-value record that concerns no target in a search from the roots. So a dependency that only helper calls read not-found. goReference now skips a typed name only when goLocal finds the declaration in scope at the node. goName keeps the raw name of a typed local that is out of scope, and the link step resolves it in the package or drops it. The case go/typed-local-block fails without the fix. Its typed local cleanup is in scope, so it guards that a typed value still gives no reference. 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/govalues.go | 34 ++++++++--- .../cases/go/typed-local-block/expect.yaml | 56 +++++++++++++++++++ .../cases/go/typed-local-block/project/go.mod | 3 + .../go/typed-local-block/project/main.go | 17 ++++++ .../go/typed-local-block/project/other.go | 11 ++++ test/projectgraph/version_test.go | 4 +- 7 files changed, 119 insertions(+), 12 deletions(-) create mode 100644 test/projectgraph/cases/go/typed-local-block/expect.yaml create mode 100644 test/projectgraph/cases/go/typed-local-block/project/go.mod create mode 100644 test/projectgraph/cases/go/typed-local-block/project/main.go create mode 100644 test/projectgraph/cases/go/typed-local-block/project/other.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 952727c4..895e238d 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -821,7 +821,11 @@ a call can run. A Go function value is one of these: file of the package can shadow it, and the index of one file does not see that file. The link step resolves the name in the package, or drops it when no declaration binds it. The case `go/predeclared-shadow` shows - it. + it. A parameter or a local of a declared type holds a value of the + type, so its name gives no reference. The index takes the name as that + local only where the declaration is in scope. So a typed local of a + closed block does not hide a function of the package with the same name. + The case `go/typed-local-block` shows it. - A method value whose method the index or the link step cannot name, as `ts[0].Close` or `h.Handle` on a value of an interface. The index or the link step records the reference with the method name. That record diff --git a/plugin/indexer/govalues.go b/plugin/indexer/govalues.go index 0e0dd0a8..1febb324 100644 --- a/plugin/indexer/govalues.go +++ b/plugin/indexer/govalues.go @@ -21,9 +21,7 @@ func goReference(p *projection, caller string, value *sitter.Node) (string, *sit } switch value.Type() { case "identifier": - // A Go parameter or local of a declared type holds a value of - // the type, and a value is no function. - if _, typed := p.typedScope(caller, p.text(value)); typed { + if p.goTypedLocal(caller, value) { return "", nil } target, _ := p.goName(caller, value) @@ -147,25 +145,43 @@ func (p *projection) goTypeTarget(caller string, node *sitter.Node) (string, boo // goName returns the per-file target of a name: the definition that the // scope chain binds, a typed local, or an import. A name that no local of -// the caller declares keeps its text, as a function of another file of -// the package, and the link step resolves it or drops it. A predeclared -// name keeps its text too, because a function of another file of the -// package can shadow it, as func clear(). A local that the index does not -// type gives none. +// the caller declares in scope keeps its text, as a function of another +// file of the package, and the link step resolves it or drops it. A typed +// local of a closed block does not bind the name. A predeclared name keeps +// its text too, because a function of another file of the package can +// shadow it, as func clear(). A local that the index does not type gives +// none. func (p *projection) goName(caller string, node *sitter.Node) (string, bool) { if node == nil { return "", false } name := p.text(node) + inScope := p.goLocal(caller, name, node.StartByte()) + if _, typed := p.typedScope(caller, name); typed && !inScope { + return name, true + } if target, ok := p.valueOf(*p.rules().references, caller, name); ok { return target, true } - if name == "_" || p.goLocal(caller, name, node.StartByte()) { + if name == "_" || inScope { return "", false } return name, true } +// goTypedLocal reports whether a name is a parameter or a local of a +// declared type that is in scope at the node. Such a name holds a value of +// the type, and a value is no function. The index records the typed locals +// of a function with no extent. So a local of a closed block can hide a +// function of the package with the same name. A call of the value is a +// function-value record that concerns no target, and the reference to the +// function is then the only edge to it. +func (p *projection) goTypedLocal(caller string, node *sitter.Node) bool { + name := p.text(node) + _, typed := p.typedScope(caller, name) + return typed && p.goLocal(caller, name, node.StartByte()) +} + // goPredeclared reports whether a name is a predeclared name of Go, as int // or len, that no definition of the file shadows. A declaration of another // file of the package can still shadow it. So a reference keeps the name, diff --git a/test/projectgraph/cases/go/typed-local-block/expect.yaml b/test/projectgraph/cases/go/typed-local-block/expect.yaml new file mode 100644 index 00000000..4aa37e7a --- /dev/null +++ b/test/projectgraph/cases/go/typed-local-block/expect.yaml @@ -0,0 +1,56 @@ +source: safedep:hand +description: >- + main declares the typed local helper in a block that closes before line 10. So helper on line 10 names + the function helper of other.go, and the link step resolves it there. The call fs[0]() is a + function-value record that concerns no target, so the reference edge is what makes os.Remove found. + The typed local cleanup is in scope where gs stores it, so it names no function, and os.Getwd is not + found. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main + - key: other.go + kind: main + rule: go.main +no_edges: + - from: main.go//main + to: other.go//cleanup +unresolved: + - caller: os//Getwd + reason: no-dependency-source + callback: true + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: main.go//main + site: main.go:11 + reason: function-value + - caller: main.go//main + site: main.go:15 + reason: function-value +results: + - target: os//Remove + status: found + path: + - key: main.go//main + - key: other.go//helper + site: main.go:10 + kind: reference + confidence: low + - key: os//Remove + site: other.go:6 + kind: direct + confidence: high + call_sites: + - caller: other.go//helper + site: other.go:6 + reached: true + - target: os//Getwd + status: not-found + call_sites: + - caller: other.go//cleanup + site: other.go:10 + reached: false diff --git a/test/projectgraph/cases/go/typed-local-block/project/go.mod b/test/projectgraph/cases/go/typed-local-block/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/typed-local-block/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/typed-local-block/project/main.go b/test/projectgraph/cases/go/typed-local-block/project/main.go new file mode 100644 index 00000000..5996b4f5 --- /dev/null +++ b/test/projectgraph/cases/go/typed-local-block/project/main.go @@ -0,0 +1,17 @@ +package main + +type T func() + +func main() { + { + var helper T + _ = helper + } + fs := []func(){helper} + fs[0]() + { + var cleanup T + gs := []func(){cleanup} + gs[0]() + } +} diff --git a/test/projectgraph/cases/go/typed-local-block/project/other.go b/test/projectgraph/cases/go/typed-local-block/project/other.go new file mode 100644 index 00000000..59016c35 --- /dev/null +++ b/test/projectgraph/cases/go/typed-local-block/project/other.go @@ -0,0 +1,11 @@ +package main + +import "os" + +func helper() { + os.Remove("x") +} + +func cleanup() { + os.Getwd() +} diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index a4b0bc1a..959829ab 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -32,7 +32,7 @@ var graphHashes = map[string]string{ "14/15": "bbc708aba767ccf554c75a7e06816abda3fc1804ad6d0876ad8ce9575f4c3a88", "14/16": "823b1f82c4aed8ba27d19275505c8d1dee755086be48fcda64d323f55b4c62a5", "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", - "16/18": "1be7368a64b35bbcea38ac1807a0175cb26f5832b49b1d4c8add12caccbfabda", + "16/18": "b5ae8d1a79d13984ffdaf14130a24973d41c9fb8ab5174d935cd0a1a3168739a", } // resultHashes records the hash of the reach results of every case for @@ -49,7 +49,7 @@ var resultHashes = map[string]string{ "14/15/3": "77dfd0ea8ab71869ef612d41b8c2e46852d4fb67a49458cb645321692af77ea0", "14/16/3": "13b6eecb686ddec3c2ba470fc9752ea66cff445d5e18e0772a71b15ae7fe1c9c", "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", - "16/18/5": "5f52d287e988f7f478720ab1ee5b129c7f8af7648a1d3851c9d4b9671710dcf3", + "16/18/5": "2509da23e707b872faaa21eb3dfcecbd97de26aa42f710d5edc47e2d33a925d9", } // summaryHashes records the hash of the golden summaries of the cases for From a2e15941fbab97a4fb132d329a20ec770146479e Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 14:01:31 +0000 Subject: [PATCH 50/58] fix(projectgraph): record a Go call of a field or a variable as a function value The link step skipped a call that resolved to a definition of data, as a field or a variable. In Go such a call runs the function value that the field or the variable holds, as s.cfg.Remove("x") for a field of a function type. No edge names that function. A search from the roots reaches the code that stores the value, but a search from a source does not. From run alone, os.Remove read not-found, where main gave uncertain. A call of data in a language with function values now gets a record with the reason function-value. A search from the roots lets it concern no target, so its answers do not change. A search from a source lets it concern every target. The case go/source-field-func fails without the fix. Four cases and the summary of dependency-generated-table gain the record. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 7 ++- projectgraph/resolve.go | 11 +++- .../go/dependency-generated-table/expect.yaml | 3 + .../go_github.com_acme_kit_v1.0.0.json | 13 +++- .../cases/go/name-ambiguous/expect.yaml | 7 +++ .../cases/go/predeclared-shadow/expect.yaml | 3 + .../cases/go/source-field-func/expect.yaml | 59 +++++++++++++++++++ .../cases/go/source-field-func/project/go.mod | 3 + .../go/source-field-func/project/main.go | 8 +++ .../cases/go/source-field-func/project/run.go | 13 ++++ .../cases/go/source-package-load/expect.yaml | 3 + test/projectgraph/version_test.go | 6 +- 12 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 test/projectgraph/cases/go/source-field-func/expect.yaml create mode 100644 test/projectgraph/cases/go/source-field-func/project/go.mod create mode 100644 test/projectgraph/cases/go/source-field-func/project/main.go create mode 100644 test/projectgraph/cases/go/source-field-func/project/run.go diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 895e238d..34cefa4b 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -427,7 +427,12 @@ Such a definition of data has no node. A read of it, as `n.Data` or `pgx.ErrNoRows`, gives no edge and no record, in the project and in the walk of an external key into a unit. A name on it, as `bufPool.Get()`, is a record with the reason `unknown-receiver`, because the walk does not -know its type. +know its type. A call of it, as `s.cfg.Remove("x")` for a field of a +function type, runs the function value that it holds. No edge names that +function, so the call is a record with the reason `function-value`, as +[A call of a function value](#a-call-of-a-function-value) says. A search +from a source that does not reach the code that stores the value keeps the +target `uncertain`. The case `go/source-field-func` shows it. A call of any other variable keeps its edge or its record. A variable that holds a name gives the edge to that name, as a call of `Run` gives the diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index bbe3f274..73d89df1 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -84,7 +84,16 @@ func (b *builder) linkCalls(ix *fileindex.Index) { continue } for _, r := range resolutions { - if r.unbound && sc.profile.literals[r.key] || r.fail == nil && b.holdsData(r.key) { + if r.unbound && sc.profile.literals[r.key] { + continue + } + // A call of a field or a variable runs the function value that + // it holds, and no edge names that function. A search from a + // source does not reach the code that stores the value. + if r.fail == nil && b.holdsData(r.key) { + if sc.profile.functionValues { + b.addUnresolved(UnresolvedSite{Caller: from, Site: site, Text: call.Callee, Receiver: call.Receiver, Reason: fileindex.UnresolvedFunctionValue}) + } continue } if r.unbound && sc.profile.thisCalls { diff --git a/test/projectgraph/cases/go/dependency-generated-table/expect.yaml b/test/projectgraph/cases/go/dependency-generated-table/expect.yaml index e5138026..de8c40d6 100644 --- a/test/projectgraph/cases/go/dependency-generated-table/expect.yaml +++ b/test/projectgraph/cases/go/dependency-generated-table/expect.yaml @@ -23,6 +23,9 @@ unresolved: - caller: os//exec//Command reason: no-dependency-source callback: true + - caller: go:github.com/acme/kit@v1.0.0//kit.go//Dispatch + site: go:github.com/acme/kit@v1.0.0//kit.go:5 + reason: function-value results: - target: os//Remove status: found 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 085851ee..acd42af3 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 @@ -14,7 +14,8 @@ "os//exec//Command" ], "uncertainties": [ - 0 + 0, + 1 ] } ], @@ -30,6 +31,16 @@ }, "text": "the Go standard library, which no unit gives (no-dependency-source)", "callback": true + }, + { + "kind": "unresolved-call", + "key": "go:github.com/acme/kit@v1.0.0//kit.go//Dispatch", + "site": { + "path": "go:github.com/acme/kit@v1.0.0//kit.go", + "line": 5, + "column": 30 + }, + "text": "handlers (function-value)" } ] } diff --git a/test/projectgraph/cases/go/name-ambiguous/expect.yaml b/test/projectgraph/cases/go/name-ambiguous/expect.yaml index e3d7c094..730ec9e1 100644 --- a/test/projectgraph/cases/go/name-ambiguous/expect.yaml +++ b/test/projectgraph/cases/go/name-ambiguous/expect.yaml @@ -50,6 +50,9 @@ unresolved: - caller: os//exec//Command reason: no-dependency-source callback: true + - caller: main.go//server//serve + site: main.go:14 + reason: function-value - caller: main.go//server//serve site: main.go:14 reason: ambiguous @@ -94,6 +97,8 @@ results: uncertainties: - kind: unresolved-call site: main.go:14 + - kind: unresolved-call + site: main.go:14 - target: os//Remove sources: [main.go//server//serve] sources_only: true @@ -105,3 +110,5 @@ results: uncertainties: - kind: unresolved-call site: main.go:14 + - kind: unresolved-call + site: main.go:14 diff --git a/test/projectgraph/cases/go/predeclared-shadow/expect.yaml b/test/projectgraph/cases/go/predeclared-shadow/expect.yaml index 47668b1b..8c724964 100644 --- a/test/projectgraph/cases/go/predeclared-shadow/expect.yaml +++ b/test/projectgraph/cases/go/predeclared-shadow/expect.yaml @@ -34,6 +34,9 @@ unresolved: - caller: main.go//main site: main.go:13 reason: function-value + - caller: main.go//main + site: main.go:14 + reason: function-value results: - target: os//Remove status: found diff --git a/test/projectgraph/cases/go/source-field-func/expect.yaml b/test/projectgraph/cases/go/source-field-func/expect.yaml new file mode 100644 index 00000000..c0c2ce55 --- /dev/null +++ b/test/projectgraph/cases/go/source-field-func/expect.yaml @@ -0,0 +1,59 @@ +source: safedep:hand +description: >- + main stores os.Remove in the field Remove of a Config, and run calls s.cfg.Remove. The link step types + cfg as Config and resolves Remove to the field, which holds a function value. So the call is a + function-value record. A search from the roots reaches main, which stores os.Remove, so os.Remove is + found through the reference. A search from run alone does not reach that code, so the record concerns + every target, and os.Remove is uncertain. A link step that dropped the call of the field as data gave + not-found there. +roots: + - key: main.go + kind: main + rule: go.main + - key: main.go//main + kind: main + rule: go.main + - key: run.go + kind: main + rule: go.main +no_edges: + - from: run.go//Svc//run + to: os//Remove +unresolved: + - caller: os//Remove + reason: no-dependency-source + callback: true + - caller: run.go//Svc//run + reason: no-dependency-source + callback: true + - caller: run.go//Svc//run + site: run.go:12 + reason: unknown-receiver + method: cfg + - caller: run.go//Svc//run + site: run.go:12 + reason: function-value +results: + - target: os//Remove + status: found + path: + - key: main.go//main + - key: os//Remove + site: main.go:6 + kind: reference + confidence: low + call_sites: + - caller: main.go//main + site: main.go:6 + reached: true + - target: os//Remove + sources: [run.go//Svc//run] + sources_only: true + status: uncertain + call_sites: + - caller: main.go//main + site: main.go:6 + reached: false + uncertainties: + - kind: unresolved-call + site: run.go:12 diff --git a/test/projectgraph/cases/go/source-field-func/project/go.mod b/test/projectgraph/cases/go/source-field-func/project/go.mod new file mode 100644 index 00000000..925d4b78 --- /dev/null +++ b/test/projectgraph/cases/go/source-field-func/project/go.mod @@ -0,0 +1,3 @@ +module example.com/app + +go 1.22 diff --git a/test/projectgraph/cases/go/source-field-func/project/main.go b/test/projectgraph/cases/go/source-field-func/project/main.go new file mode 100644 index 00000000..bf01a57f --- /dev/null +++ b/test/projectgraph/cases/go/source-field-func/project/main.go @@ -0,0 +1,8 @@ +package main + +import "os" + +func main() { + s := &Svc{cfg: Config{Remove: os.Remove}} + s.run() +} diff --git a/test/projectgraph/cases/go/source-field-func/project/run.go b/test/projectgraph/cases/go/source-field-func/project/run.go new file mode 100644 index 00000000..c49b7373 --- /dev/null +++ b/test/projectgraph/cases/go/source-field-func/project/run.go @@ -0,0 +1,13 @@ +package main + +type Config struct { + Remove func(string) error +} + +type Svc struct { + cfg Config +} + +func (s *Svc) run() { + s.cfg.Remove("x") +} diff --git a/test/projectgraph/cases/go/source-package-load/expect.yaml b/test/projectgraph/cases/go/source-package-load/expect.yaml index c7623f16..864dc4da 100644 --- a/test/projectgraph/cases/go/source-package-load/expect.yaml +++ b/test/projectgraph/cases/go/source-package-load/expect.yaml @@ -121,6 +121,9 @@ unresolved: - caller: main.go//run site: main.go:19 reason: function-value + - caller: main.go//runHook + site: main.go:23 + reason: function-value - caller: main.go//runHook site: main.go:23 reason: unknown-receiver diff --git a/test/projectgraph/version_test.go b/test/projectgraph/version_test.go index 959829ab..1608bbd4 100644 --- a/test/projectgraph/version_test.go +++ b/test/projectgraph/version_test.go @@ -32,7 +32,7 @@ var graphHashes = map[string]string{ "14/15": "bbc708aba767ccf554c75a7e06816abda3fc1804ad6d0876ad8ce9575f4c3a88", "14/16": "823b1f82c4aed8ba27d19275505c8d1dee755086be48fcda64d323f55b4c62a5", "15/17": "d823ce8ba221c534ab062f7e48abfe91d1c90d50b2a2fdb00d41183dc0e9da09", - "16/18": "b5ae8d1a79d13984ffdaf14130a24973d41c9fb8ab5174d935cd0a1a3168739a", + "16/18": "d5496159c3687e76c95031baaebfe57436bb160c60face106ab2ed82a17f5959", } // resultHashes records the hash of the reach results of every case for @@ -49,7 +49,7 @@ var resultHashes = map[string]string{ "14/15/3": "77dfd0ea8ab71869ef612d41b8c2e46852d4fb67a49458cb645321692af77ea0", "14/16/3": "13b6eecb686ddec3c2ba470fc9752ea66cff445d5e18e0772a71b15ae7fe1c9c", "15/17/4": "1b32d7c1dbb0a4637853eb7a00b0db91a0a4e1a31f221b37ed0d0654a66ed13e", - "16/18/5": "2509da23e707b872faaa21eb3dfcecbd97de26aa42f710d5edc47e2d33a925d9", + "16/18/5": "02809a9c04d75995d58596708bc942ff29866ad78748dbad441d7f6b2aaf4b9a", } // summaryHashes records the hash of the golden summaries of the cases for @@ -67,7 +67,7 @@ var summaryHashes = map[string]string{ "14/15/2": "5969f3cda15b666cbd902bbc2db953cbdef8c0ce48d5e77446cf243cdd0bb0e7", "14/16/2": "fb678202978212aae932e50a56a0d3dc7f0f794ce908988bf605ce9a6e04bc51", "15/17/2": "00a6bd5871f70459a617079f431939ef7c92e4b3f360013c1dedd017fe1146be", - "16/18/3": "00874f23561eff140ecdb5472fc3745b2314c84a619ac2ceef4494762cad893d", + "16/18/3": "694efb5f72075356359031d449d7b4ecbdca2ddd5099dacbcd2adad745ac6e19", } func TestGraphHashFollowsVersion(t *testing.T) { From f5a7c1dfc4b68375c01b754b16d7c877f2bb3aa5 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 05:09:36 +0000 Subject: [PATCH 51/58] test(projectgraph): case for a Rust alias that replaces a struct of the same name A file declares Client as a struct under one cfg and as a type alias under the other cfg. The index lets the alias replace the definition of the struct. A call of send on a parameter of the type Client then gets only the edge to the method of the type that the alias names. So std::fs::remove_file, which the send of the struct calls, reads not-found. origin/main gives found. The case expects found, so it fails on this branch. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- .../cases/rust/alias-cfg-struct/expect.yaml | 48 +++++++++++++++++++ .../rust/alias-cfg-struct/project/Cargo.toml | 8 ++++ .../rust/alias-cfg-struct/project/src/main.rs | 36 ++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml create mode 100644 test/projectgraph/cases/rust/alias-cfg-struct/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs diff --git a/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml b/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml new file mode 100644 index 00000000..a3890069 --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml @@ -0,0 +1,48 @@ +source: safedep:hand +description: >- + The file declares Client two times. With the feature native, Client is a struct, and its method send calls + std::fs::remove_file. Without the feature, Client is an alias of fallback::Client, whose send calls + nothing. work calls send on a parameter of the type Client. The call can run the send of each + declaration, so std::fs::remove_file is found through the send of the struct. An index that lets the alias + replace the struct gives only the edge to fallback::Client.send, 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 +edges: + - from: src/main.rs//main + to: src/main.rs//work + kind: direct + confidence: high + site: src/main.rs:35 + - from: src/main.rs//work + to: src/main.rs//Client//send + kind: member + confidence: high + site: src/main.rs:31 +unresolved: [] +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//work + site: src/main.rs:35 + kind: direct + confidence: high + - key: src/main.rs//Client//send + site: src/main.rs:31 + kind: member + confidence: high + - key: std//fs//remove_file + site: src/main.rs:23 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//Client//send + site: src/main.rs:23 + reached: true diff --git a/test/projectgraph/cases/rust/alias-cfg-struct/project/Cargo.toml b/test/projectgraph/cases/rust/alias-cfg-struct/project/Cargo.toml new file mode 100644 index 00000000..7f3bfbc8 --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-struct/project/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" + +[features] +default = ["native"] +native = [] diff --git a/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs b/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs new file mode 100644 index 00000000..9dee8c8c --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs @@ -0,0 +1,36 @@ +mod fallback { + pub struct Client; + + impl Client { + pub fn new() -> Client { + Client + } + + pub fn send(&self) {} + } +} + +#[cfg(feature = "native")] +pub struct Client; + +#[cfg(feature = "native")] +impl Client { + pub fn new() -> Client { + Client + } + + pub fn send(&self) { + let _ = std::fs::remove_file("x"); + } +} + +#[cfg(not(feature = "native"))] +pub type Client = fallback::Client; + +fn work(c: &Client) { + c.send(); +} + +fn main() { + work(&Client::new()); +} From 3ca4e6b1cc90ba92b65f4133a2b80c0b3c9654dd Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 05:09:36 +0000 Subject: [PATCH 52/58] test(projectgraph): case for a JSX spread attribute in a .jsx file App spreads an object of handlers into a button, as ; +} diff --git a/test/projectgraph/cases/javascript/jsx-spread-attributes/project/src/main.js b/test/projectgraph/cases/javascript/jsx-spread-attributes/project/src/main.js new file mode 100644 index 00000000..eb515c7e --- /dev/null +++ b/test/projectgraph/cases/javascript/jsx-spread-attributes/project/src/main.js @@ -0,0 +1,5 @@ +import { createElement } from 'react'; +import { createRoot } from 'react-dom/client'; +import { App } from './App.jsx'; + +createRoot(document.body).render(createElement(App)); From b0315ff462faeb24bbe68c706a80ba53a9d70aa7 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 05:12:35 +0000 Subject: [PATCH 53/58] test(projectgraph): case for a Rust module that a glob use brings into scope worker.rs has use super::*, and go calls helpers::cleanup of the sibling module helpers. The link step does not resolve a module through a glob use. origin/main reads helpers as a possible module of the project, because a file has that name, and records the call with the reason no-definition, so std::fs::remove_file is uncertain. This branch reads helpers as another crate, because it is no child module and no module of the root. The call gets the key helpers//cleanup and no record, so std::fs::remove_file reads not-found. The case expects found, so it fails on this branch. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- .../cases/rust/glob-use-module/expect.yaml | 47 +++++++++++++++++++ .../rust/glob-use-module/project/Cargo.toml | 4 ++ .../project/src/deep/helpers.rs | 3 ++ .../glob-use-module/project/src/deep/mod.rs | 2 + .../project/src/deep/worker.rs | 5 ++ .../rust/glob-use-module/project/src/main.rs | 5 ++ 6 files changed, 66 insertions(+) create mode 100644 test/projectgraph/cases/rust/glob-use-module/expect.yaml create mode 100644 test/projectgraph/cases/rust/glob-use-module/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/glob-use-module/project/src/deep/helpers.rs create mode 100644 test/projectgraph/cases/rust/glob-use-module/project/src/deep/mod.rs create mode 100644 test/projectgraph/cases/rust/glob-use-module/project/src/deep/worker.rs create mode 100644 test/projectgraph/cases/rust/glob-use-module/project/src/main.rs diff --git a/test/projectgraph/cases/rust/glob-use-module/expect.yaml b/test/projectgraph/cases/rust/glob-use-module/expect.yaml new file mode 100644 index 00000000..f03b9797 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/expect.yaml @@ -0,0 +1,47 @@ +source: safedep:hand +description: >- + The module deep declares the modules helpers and worker. worker.rs has use super::*, which brings the + module helpers into scope, and go calls helpers::cleanup. cleanup calls std::fs::remove_file, so + std::fs::remove_file is found. The name helpers is no child module of worker and no module of the root. + A link step that reads such a name as another crate gives the edge to the key 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 +edges: + - from: src/main.rs//main + to: src/deep/worker.rs//go + kind: direct + confidence: high + site: src/main.rs:4 + - from: src/deep/worker.rs//go + to: src/deep/helpers.rs//cleanup + kind: wildcard + confidence: low + site: src/deep/worker.rs:4 +unresolved: [] +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/deep/worker.rs//go + site: src/main.rs:4 + kind: direct + confidence: high + - key: src/deep/helpers.rs//cleanup + site: src/deep/worker.rs:4 + kind: wildcard + confidence: low + - key: std//fs//remove_file + site: src/deep/helpers.rs:2 + kind: direct + confidence: high + call_sites: + - caller: src/deep/helpers.rs//cleanup + site: src/deep/helpers.rs:2 + reached: true diff --git a/test/projectgraph/cases/rust/glob-use-module/project/Cargo.toml b/test/projectgraph/cases/rust/glob-use-module/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/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-module/project/src/deep/helpers.rs b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/helpers.rs new file mode 100644 index 00000000..af9dfae2 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/helpers.rs @@ -0,0 +1,3 @@ +pub fn cleanup(p: &str) { + let _ = std::fs::remove_file(p); +} diff --git a/test/projectgraph/cases/rust/glob-use-module/project/src/deep/mod.rs b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/mod.rs new file mode 100644 index 00000000..ee3caa76 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/mod.rs @@ -0,0 +1,2 @@ +pub mod helpers; +pub mod worker; diff --git a/test/projectgraph/cases/rust/glob-use-module/project/src/deep/worker.rs b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/worker.rs new file mode 100644 index 00000000..bba92cff --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/project/src/deep/worker.rs @@ -0,0 +1,5 @@ +use super::*; + +pub fn go() { + helpers::cleanup("x"); +} diff --git a/test/projectgraph/cases/rust/glob-use-module/project/src/main.rs b/test/projectgraph/cases/rust/glob-use-module/project/src/main.rs new file mode 100644 index 00000000..2ab48314 --- /dev/null +++ b/test/projectgraph/cases/rust/glob-use-module/project/src/main.rs @@ -0,0 +1,5 @@ +mod deep; + +fn main() { + deep::worker::go(); +} From 88d6dee36298880c753571114fc2a87a489a530f Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 05:24:19 +0000 Subject: [PATCH 54/58] fix(projectgraph): bind no type for a Rust alias with a second declaration (FOU-562) A scope can declare one name more than once under cfg attributes, as a struct and an alias, or two aliases of other types. The index holds one definition per key. So the last alias replaced the struct or the other alias, and the link step sent each walk through that alias. A call then got only the method of one declaration. A target that only the other declaration calls read not-found. The case alias-cfg-struct showed this for std::fs::remove_file. The index does not know which cfg the build sets. Now an alias with another meaning in its scope names no type. A typed call through the name gets the record unknown-receiver, and the name edges reach the method of each declaration. The methods of the struct stay under the key of the alias. So the link step also gives the record for a path to one of them, as Client::new, and not a single edge to the method of the struct. An alias that the scope declares once, or two aliases of one type, still bind the type, as FOU-562 asks. The case alias-cfg-struct now also checks a target that only the type of the alias calls through Client::new. The new case alias-cfg-two checks two aliases with a path call and a typed call. Both cases read not-found before the fix. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01PFQbqPqd9xn1fT6hBsyoUm Signed-off-by: Claude --- docs/projectgraph.md | 8 ++ plugin/indexer/rust.go | 36 +++++++- plugin/indexer/rust_test.go | 12 +++ projectgraph/names.go | 13 +++ projectgraph/resolve.go | 3 + .../cases/rust/alias-cfg-struct/expect.yaml | 71 +++++++++++---- .../rust/alias-cfg-struct/project/src/main.rs | 1 + .../cases/rust/alias-cfg-two/expect.yaml | 87 +++++++++++++++++++ .../rust/alias-cfg-two/project/Cargo.toml | 4 + .../rust/alias-cfg-two/project/src/main.rs | 41 +++++++++ 10 files changed, 259 insertions(+), 17 deletions(-) create mode 100644 test/projectgraph/cases/rust/alias-cfg-two/expect.yaml create mode 100644 test/projectgraph/cases/rust/alias-cfg-two/project/Cargo.toml create mode 100644 test/projectgraph/cases/rust/alias-cfg-two/project/src/main.rs diff --git a/docs/projectgraph.md b/docs/projectgraph.md index 34cefa4b..0e404b2c 100644 --- a/docs/projectgraph.md +++ b/docs/projectgraph.md @@ -400,6 +400,14 @@ Rust follows the same rules, with these additions. reads the type. An alias of a type that the file does not resolve, as a primitive type, gives the record `unknown-receiver` for a name after it. + Only a `cfg` attribute lets a scope declare a name more than once, as a + struct under one `cfg` and an alias under the other, or two aliases of + other types. The index does not know which `cfg` the build sets, so such + an alias names no type. A call through the name, as `Client::new` or + `c.send()` on a parameter of the type `Client`, then gets the record + `unknown-receiver` and a `name` edge to the method of each declaration. + It does not take the method of one declaration only, which would turn a + target of the other declaration into `not-found`. - A trait object or a generic bound `T: Trait` of a unit gets the `cha` edges to the implementations in the graph, through their external keys. With no implementation, the site is a record with the reason diff --git a/plugin/indexer/rust.go b/plugin/indexer/rust.go index 8b61dba8..1d8acfc1 100644 --- a/plugin/indexer/rust.go +++ b/plugin/indexer/rust.go @@ -62,6 +62,7 @@ var rustTypeItems = []string{"struct_item", "enum_item", "union_item"} func rustTypes(p *projection) { var walk func(scope string, list *sitter.Node) walk = func(scope string, list *sitter.Node) { + targets := p.rustAliasTargets(list) for _, item := range ts.NamedChildren(list) { switch { case item.Type() == "mod_item": @@ -76,8 +77,9 @@ func rustTypes(p *projection) { p.addDefinition(key, fileindex.DefClass, item) p.addSyntax(key, item) case item.Type() == "type_item": - key := fileindex.Join(scope, p.text(item.ChildByFieldName("name"))) - p.addDefinition(key, fileindex.DefAlias, item).Alias = p.rustTypeName(item.ChildByFieldName("type"), nil) + name := p.text(item.ChildByFieldName("name")) + key := fileindex.Join(scope, name) + p.addDefinition(key, fileindex.DefAlias, item).Alias = targets[name] p.addSyntax(key, item) } } @@ -85,6 +87,36 @@ func rustTypes(p *projection) { walk(p.cg.FileName, p.tree.Tree().RootNode()) } +// rustAliasTargets returns the type that each alias of a list of items +// names. Only a cfg attribute lets a scope declare a name more than once, +// as a struct under one cfg and an alias under the other, or two aliases. +// The index does not know which cfg the build sets, and one definition +// holds the key. So an alias with another meaning in its scope names no +// type, and a call through it gets the record unknown-receiver. A target +// of each meaning then stays uncertain and does not read not-found. +func (p *projection) rustAliasTargets(list *sitter.Node) map[string]string { + aliases := map[string][]string{} + types := map[string]bool{} + for _, item := range ts.NamedChildren(list) { + name := p.text(item.ChildByFieldName("name")) + switch { + case item.Type() == "type_item": + if target := p.rustTypeName(item.ChildByFieldName("type"), nil); !slices.Contains(aliases[name], target) { + aliases[name] = append(aliases[name], target) + } + case slices.Contains(rustTypeItems, item.Type()): + types[name] = true + } + } + targets := map[string]string{} + for name, named := range aliases { + if len(named) == 1 && !types[name] { + targets[name] = named[0] + } + } + return targets +} + // rustSiblings returns the items of the scope of an item that define or // implement the type of the name, as the struct and each impl block of // Store. diff --git a/plugin/indexer/rust_test.go b/plugin/indexer/rust_test.go index 1ca4b428..bb5ea023 100644 --- a/plugin/indexer/rust_test.go +++ b/plugin/indexer/rust_test.go @@ -102,9 +102,21 @@ func TestFileIndexRustTypeAlias(t *testing.T) { "use image::ImageBuffer;", "pub type Image

= ImageBuffer>;", "mod inner { type Store = crate::store::Store; }", + "#[cfg(unix)] type Imp = unix::Thing;", + "#[cfg(windows)] type Imp = windows::Thing;", + "#[cfg(feature = \"native\")] pub struct Client;", + "#[cfg(not(feature = \"native\"))] pub type Client = fallback::Client;", + "#[cfg(unix)] type Same = io::Thing;", + "#[cfg(windows)] type Same = io::Thing;", }, "\n") + "\n" ix := indexSource(t, core.LanguageCodeRust, "defs.rs", source) + for key, alias := range map[string]string{"defs.rs//Imp": "", "defs.rs//Client": "", "defs.rs//Same": "io::Thing"} { + def := definitionOf(t, ix, key) + assert.Equal(t, fileindex.DefAlias, def.Kind, key) + assert.Equal(t, alias, def.Alias, key) + } + image := definitionOf(t, ix, "defs.rs//Image") assert.Equal(t, fileindex.DefAlias, image.Kind) assert.Equal(t, "ImageBuffer", image.Alias) diff --git a/projectgraph/names.go b/projectgraph/names.go index f57543e1..d80b77c0 100644 --- a/projectgraph/names.go +++ b/projectgraph/names.go @@ -228,3 +228,16 @@ func (b *builder) bound(key string) (string, bool) { } return target, true } + +// unboundAlias reports whether the key is a type alias that binds no type. +// A struct and an alias of one name under two cfg attributes give such an +// alias, and the methods of the struct stay under its key. A path to one of +// them, as Client::new, can also run the method of the type of the alias, +// so it gets the record of an unknown receiver and not a single edge. +func (b *builder) unboundAlias(key string) bool { + if def := b.defs[key]; def == nil || def.Kind != fileindex.DefAlias { + return false + } + _, ok := b.bound(key) + return !ok +} diff --git a/projectgraph/resolve.go b/projectgraph/resolve.go index 73d89df1..7c2c60c0 100644 --- a/projectgraph/resolve.go +++ b/projectgraph/resolve.go @@ -558,6 +558,9 @@ func (b *builder) enclosingClass(key string) (string, bool) { // caller is a parameter or a local variable of the function, and its // declared type gives its class. func (b *builder) resolveLocal(sc *scope, caller, target string) resolution { + if b.unboundAlias(fileindex.Parent(target)) { + return resolution{fail: unknownReceiver(fileindex.Last(target)), rule: ruleLocal} + } if b.g.Nodes[target] != nil || b.holdsData(target) { return b.walkAs(target, nil, ruleLocal) } diff --git a/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml b/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml index a3890069..0d637b1a 100644 --- a/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml +++ b/test/projectgraph/cases/rust/alias-cfg-struct/expect.yaml @@ -1,10 +1,12 @@ source: safedep:hand description: >- The file declares Client two times. With the feature native, Client is a struct, and its method send calls - std::fs::remove_file. Without the feature, Client is an alias of fallback::Client, whose send calls - nothing. work calls send on a parameter of the type Client. The call can run the send of each - declaration, so std::fs::remove_file is found through the send of the struct. An index that lets the alias - replace the struct gives only the edge to fallback::Client.send, and std::fs::remove_file reads + std::fs::remove_file. Without the feature, Client is an alias of fallback::Client, whose new calls + std::env::set_var. main calls Client::new, and work calls send on a parameter of the type Client. The index + does not know which cfg the build sets, so the alias binds no type. Each call gets the record + unknown-receiver and a name edge to the method of each declaration. So both targets are found. An index + that lets the alias replace the struct gives only fallback::Client.send, and std::fs::remove_file reads + not-found. An index that lets the struct win gives only the struct's new, and std::env::set_var reads not-found. roots: - key: src/main.rs @@ -18,31 +20,70 @@ edges: to: src/main.rs//work kind: direct confidence: high - site: src/main.rs:35 + site: src/main.rs:36 + - from: src/main.rs//main + to: src/main.rs//Client//new + kind: name + confidence: low + site: src/main.rs:36 + - from: src/main.rs//main + to: src/main.rs//fallback//Client//new + kind: name + confidence: low + site: src/main.rs:36 - from: src/main.rs//work to: src/main.rs//Client//send - kind: member - confidence: high - site: src/main.rs:31 -unresolved: [] + kind: name + confidence: low + site: src/main.rs:32 + - from: src/main.rs//work + to: src/main.rs//fallback//Client//send + kind: name + confidence: low + site: src/main.rs:32 +unresolved: + - caller: src/main.rs//work + site: src/main.rs:32 + reason: unknown-receiver + method: send + - caller: src/main.rs//main + site: src/main.rs:36 + reason: unknown-receiver + method: new results: - target: std//fs//remove_file status: found path: - key: src/main.rs//main - key: src/main.rs//work - site: src/main.rs:35 + site: src/main.rs:36 kind: direct confidence: high - key: src/main.rs//Client//send - site: src/main.rs:31 - kind: member - confidence: high + site: src/main.rs:32 + kind: name + confidence: low - key: std//fs//remove_file - site: src/main.rs:23 + site: src/main.rs:24 kind: direct confidence: high call_sites: - caller: src/main.rs//Client//send - site: src/main.rs:23 + site: src/main.rs:24 + reached: true + - target: std//env//set_var + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//fallback//Client//new + site: src/main.rs:36 + kind: name + confidence: low + - key: std//env//set_var + site: src/main.rs:6 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//fallback//Client//new + site: src/main.rs:6 reached: true diff --git a/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs b/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs index 9dee8c8c..d4dfabfd 100644 --- a/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs +++ b/test/projectgraph/cases/rust/alias-cfg-struct/project/src/main.rs @@ -3,6 +3,7 @@ mod fallback { impl Client { pub fn new() -> Client { + std::env::set_var("MODE", "fallback"); Client } diff --git a/test/projectgraph/cases/rust/alias-cfg-two/expect.yaml b/test/projectgraph/cases/rust/alias-cfg-two/expect.yaml new file mode 100644 index 00000000..60952a0b --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-two/expect.yaml @@ -0,0 +1,87 @@ +source: safedep:hand +description: >- + The file declares the alias Imp two times, as unix::Thing under cfg(unix) and as windows::Thing under + cfg(windows). Only the open and the close of unix::Thing call the standard library. main calls Imp::open, + and work calls close on a parameter of the type Imp. The index does not know which cfg the build sets, so + the alias binds no type. Each call gets the record unknown-receiver and a name edge to the method of each + type. So std::fs::remove_file and std::process::Command::new are found. An index that lets the last alias + win gives only the methods of windows::Thing, and both targets 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/main.rs//work + kind: direct + confidence: high + site: src/main.rs:40 + - from: src/main.rs//main + to: src/main.rs//unix//Thing//open + kind: name + confidence: low + site: src/main.rs:39 + - from: src/main.rs//main + to: src/main.rs//windows//Thing//open + kind: name + confidence: low + site: src/main.rs:39 + - from: src/main.rs//work + to: src/main.rs//unix//Thing//close + kind: name + confidence: low + site: src/main.rs:35 + - from: src/main.rs//work + to: src/main.rs//windows//Thing//close + kind: name + confidence: low + site: src/main.rs:35 +unresolved: + - caller: src/main.rs//work + site: src/main.rs:35 + reason: unknown-receiver + method: close + - caller: src/main.rs//main + site: src/main.rs:39 + reason: unknown-receiver + method: open +results: + - target: std//fs//remove_file + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//unix//Thing//open + site: src/main.rs:39 + kind: name + confidence: low + - key: std//fs//remove_file + site: src/main.rs:6 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//unix//Thing//open + site: src/main.rs:6 + reached: true + - target: std//process//Command//new + status: found + path: + - key: src/main.rs//main + - key: src/main.rs//work + site: src/main.rs:40 + kind: direct + confidence: high + - key: src/main.rs//unix//Thing//close + site: src/main.rs:35 + kind: name + confidence: low + - key: std//process//Command//new + site: src/main.rs:11 + kind: direct + confidence: high + call_sites: + - caller: src/main.rs//unix//Thing//close + site: src/main.rs:11 + reached: true diff --git a/test/projectgraph/cases/rust/alias-cfg-two/project/Cargo.toml b/test/projectgraph/cases/rust/alias-cfg-two/project/Cargo.toml new file mode 100644 index 00000000..0d44349a --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-two/project/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "app" +version = "0.1.0" +edition = "2021" diff --git a/test/projectgraph/cases/rust/alias-cfg-two/project/src/main.rs b/test/projectgraph/cases/rust/alias-cfg-two/project/src/main.rs new file mode 100644 index 00000000..42c22799 --- /dev/null +++ b/test/projectgraph/cases/rust/alias-cfg-two/project/src/main.rs @@ -0,0 +1,41 @@ +mod unix { + pub struct Thing; + + impl Thing { + pub fn open() -> Thing { + let _ = std::fs::remove_file("x"); + Thing + } + + pub fn close(&self) { + let _ = std::process::Command::new("sync"); + } + } +} + +mod windows { + pub struct Thing; + + impl Thing { + pub fn open() -> Thing { + Thing + } + + pub fn close(&self) {} + } +} + +#[cfg(unix)] +type Imp = unix::Thing; + +#[cfg(windows)] +type Imp = windows::Thing; + +fn work(t: &Imp) { + t.close(); +} + +fn main() { + let t = Imp::open(); + work(&t); +} From 6ae298356103769505afbb51a79457785eebb989 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 05:21:21 +0000 Subject: [PATCH 55/58] fix(indexer): read the object of a JSX spread attribute The index dropped the value of a spread attribute, as {...handlers} in ; +} + +export function App() { + return