Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Fixes

- In Python, `import time` no longer links the importing file to a project function that happens to be named `time`, so such a function stops listing every file that imports the standard library module among its callers and in its impact. The same goes for `json`, `copy`, `random`, `logging` and any other module, and for a fallback variable like `brotli = None` beside a guarded `import brotli`. Thanks @inth3shadows for the report. (#3239)
- In Rust, a call through a module that a glob `use` brings in, like `gzip::write()` in a test module with `use super::*`, or that a `use` inside the function names, now links to that module's function again. Thanks @Egrn for the report and @mohitdixit02. (#3253)

## [1.6.3] - 2026-11-17

Expand Down
186 changes: 186 additions & 0 deletions __tests__/rust-glob-module-paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/**
* A Rust path whose first segment is a module that a glob `use` brings in —
* `gzip::write()` in a `mod tests { use super::*; }` whose parent declares
* `pub mod gzip;` (issue #3253) — walks to that module, as Rust looks the
* name up: the module's own items, then its `use`s, then its globs. A glob
* sees everything of a module it is written inside (`use super::*`), and only
* the `pub` part of any other. A `use` written in a function binds the module
* there alone, ahead of the file's own.
*/
import { afterEach, describe, expect, it } from 'vitest';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { CodeGraph } from '../src';

let root = '';
let cg: CodeGraph | undefined;

afterEach(() => {
cg?.close();
cg = undefined;
if (root) fs.rmSync(root, { recursive: true, force: true });
root = '';
});

async function index(files: Record<string, string>, manifest = ''): Promise<CodeGraph> {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-rust-glob-mod-'));
const all: Record<string, string> = {
'Cargo.toml': `[package]\nname = "repro"\nversion = "0.1.0"\nedition = "2021"\n${manifest}`,
...files,
};
for (const [rel, text] of Object.entries(all)) {
const dest = path.join(root, rel);
fs.mkdirSync(path.dirname(dest), { recursive: true });
fs.writeFileSync(dest, text);
}
cg = await CodeGraph.init(root, { index: true });
return cg;
}

/** The `calls` targets of function `fn` in `file`, as `<file>:<qualified name>`, sorted. */
function callsOf(graph: CodeGraph, file: string, fn: string): string[] {
const node = graph.getNodesInFile(file).find((n) => n.name === fn && (n.kind === 'function' || n.kind === 'method'));
expect(node, `${file}:${fn}`).toBeDefined();
return graph.getOutgoingEdges(node!.id)
.filter((e) => e.kind === 'calls')
.map((e) => {
const t = graph.getNode(e.target)!;
return `${t.filePath}:${t.qualifiedName}`;
})
.sort();
}

const GZIP = 'pub fn read() -> u8 {\n 1\n}\n\npub fn write() {}\n';

describe('Rust: a module path through a glob use (#3253)', () => {
it('links gzip::write() in an inline mod tests whose use super::* brings the module in (the issue repro)', async () => {
const graph = await index({
'src/lib.rs': 'pub mod store;\n',
'src/store/mod.rs': `pub mod gzip;

pub fn load() -> u8 {
gzip::read()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn writes() {
gzip::write();
let _ = load();
}
}
`,
'src/store/gzip.rs': GZIP,
});
expect(callsOf(graph, 'src/store/mod.rs', 'load')).toEqual(['src/store/gzip.rs:read']);
expect(callsOf(graph, 'src/store/mod.rs', 'writes')).toEqual(['src/store/gzip.rs:write', 'src/store/mod.rs:load']);
});

it('links it from a test module in a file of its own, from a private module, and through an inline module', async () => {
const graph = await index({
'src/lib.rs': 'pub mod store;\n',
'src/store/mod.rs': `mod gzip;

pub mod helper {
pub fn run() {}
}

#[cfg(test)]
mod tests;
`,
'src/store/gzip.rs': GZIP,
'src/store/tests.rs': `use super::*;

#[test]
fn writes_from_file() {
gzip::write();
helper::run();
}
`,
});
expect(callsOf(graph, 'src/store/tests.rs', 'writes_from_file')).toEqual([
'src/store/gzip.rs:write',
'src/store/mod.rs:run',
]);
});

it('links it through a glob of another module, but only to what that module makes public', async () => {
const graph = await index({
'src/lib.rs': 'pub mod store;\npub mod other;\n',
'src/store/mod.rs': 'pub mod gzip;\nmod hidden;\n',
'src/store/gzip.rs': GZIP,
'src/store/hidden.rs': 'pub fn reveal() {}\n',
// `hidden` here is the dependency crate: store's private module is not
// what `use crate::store::*` passes on.
'src/other.rs': `use crate::store::*;

pub fn outside() {
gzip::write();
hidden::reveal();
}
`,
}, '\n[dependencies]\nhidden = "1"\n');
expect(callsOf(graph, 'src/other.rs', 'outside')).toEqual(['src/store/gzip.rs:write']);
});

it('links a module a use inside the function binds, ahead of the file\'s own use of that name', async () => {
const graph = await index({
'src/lib.rs': 'pub mod a;\npub mod b;\npub mod store;\npub mod c;\n',
'src/a.rs': 'pub fn write() {}\n',
'src/b.rs': 'pub fn write() {}\n',
'src/store/mod.rs': 'pub mod gzip;\n',
'src/store/gzip.rs': GZIP,
'src/c.rs': `use crate::a as m;

pub fn explicit() {
use crate::store::gzip;
gzip::write();
}

pub fn shadowed() {
use crate::b as m;
m::write();
}

pub fn file_level() {
m::write();
}
`,
});
expect(callsOf(graph, 'src/c.rs', 'explicit')).toEqual(['src/store/gzip.rs:write']);
expect(callsOf(graph, 'src/c.rs', 'shadowed')).toEqual(['src/b.rs:write']);
expect(callsOf(graph, 'src/c.rs', 'file_level')).toEqual(['src/a.rs:write']);
});

it('ends a walk through modules that glob each other, and links nothing a glob does not bring in', async () => {
const graph = await index({
'src/lib.rs': 'pub mod store;\n',
'src/store/mod.rs': `pub use self::inner::*;

pub mod inner {
pub use super::*;

pub fn run() {
missing::call();
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn nothing() {
absent::call();
}
}
`,
});
expect(callsOf(graph, 'src/store/mod.rs', 'run')).toEqual([]);
expect(callsOf(graph, 'src/store/mod.rs', 'nothing')).toEqual([]);
});
});
98 changes: 87 additions & 11 deletions src/resolution/name-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10161,6 +10161,12 @@ interface RustUses {
pathMods: Set<string>;
/** Modules it declares out of line with no `#[path]` (`mod name;`): the files the regular rules name. */
mods: Set<string>;
/**
* Every module it declares, inline or not, by the inline module it is
* declared in (`${block's open | -1}\0${name}`) → whether it is `pub` (of
* any reach): what a glob of that module passes on.
*/
modDecls: Map<string, boolean>;
/** Whether it starts a binary of its own: a top-level `fn main` or `#![no_main]` (a trybuild case, a fuzz target). */
main: boolean;
/** Whether every brace closes, so `blocks` can be trusted to say what is inside them. */
Expand Down Expand Up @@ -10442,7 +10448,7 @@ function rustUsesOf(filePath: string, context: ResolutionContext): RustUses {
if (hit) return hit;
const uses: RustUses = {
names: new Set(), globs: new Set(), external: new Set(), bound: new Set(), boundFrom: new Map(),
paths: new Map(), stdPaths: new Map(), globPaths: [], blocks: [], pathMods: new Set(), mods: new Set(), main: false, balanced: true, lineStarts: [],
paths: new Map(), stdPaths: new Map(), globPaths: [], blocks: [], pathMods: new Set(), mods: new Set(), modDecls: new Map(), main: false, balanced: true, lineStarts: [],
};
const leaves = (tree: string): string[] => [
...[...tree.matchAll(/([A-Za-z_]\w*)\s*(?=[,}]|$|\s+as\b)|\bas\s+([A-Za-z_]\w*)/g)]
Expand Down Expand Up @@ -10518,6 +10524,12 @@ function rustUsesOf(filePath: string, context: ResolutionContext): RustUses {
for (const m of text.matchAll(/\bmod\s+(?:r#)?([A-Za-z_]\w*)\s*;/g)) {
if (!pathModAt.has(m.index!)) uses.mods.add(m[1]!);
}
// Every module declaration, `mod name;` or `mod name {`, by the inline module it is in, and whether it is `pub`.
for (const m of text.matchAll(/\bmod\s+(?:r#)?([A-Za-z_]\w*)\s*[;{]/g)) {
const key = `${rustBlockAt(layout.blocks, m.index!)?.open ?? -1}\0${m[1]!}`;
const pub = /\bpub(?:\s*\([^()]*\))?\s*$/.test(text.slice(Math.max(0, m.index! - 48), m.index!));
uses.modDecls.set(key, pub || uses.modDecls.get(key) === true);
}
uses.main = /^(?:pub\s+)?(?:async\s+)?fn\s+main\s*\(|^[ \t]*#!\[\s*no_main\s*\]/m.test(text);
for (let at = 0; at !== -1; at = text.indexOf('\n', at) + 1 || -1) uses.lineStarts.push(at);
memo.set(filePath, uses);
Expand Down Expand Up @@ -11057,12 +11069,17 @@ function rustLibModule(crateDir: string, context: ResolutionContext): RustModule
* else loads that file too (a helper several tests load is each test's);
* cfg alternatives that name a file each (tokio's `process::imp` is
* `unix/mod.rs` or `windows.rs`) are all of them (rustCfgChoiceItems); null
* when a `cfg_attr` sets the path, as for one with no file.
* when a `cfg_attr` sets the path, as for one with no file. `publicOnly` is a
* glob's view of `m` from outside it: only a module `m` declares `pub`, or
* passes on with a `pub use`.
*/
function rustSubmodule(m: RustModule, name: string, context: ResolutionContext, depth = 0): RustModule | null {
if (m.choices) return rustCfgChoice(m.choices.map((c) => rustSubmodule(c, name, context, depth)).filter((c) => c !== null));
function rustSubmodule(m: RustModule, name: string, context: ResolutionContext, depth = 0, publicOnly = false): RustModule | null {
if (m.choices) return rustCfgChoice(m.choices.map((c) => rustSubmodule(c, name, context, depth, publicOnly)).filter((c) => c !== null));
const dir = rustJoin(m.dir, name);
const layout = rustUsesOf(m.file, context);
// A `mod name;` without `pub` keeps the name to `m` and the modules in it.
const declared = layout.modDecls.get(`${m.block?.open ?? -1}\0${name}`);
if (publicOnly && declared === false) return null;
if (layout.pathMods.has(name)) {
const decls = rustPathDeclsIn(m.file, context).filter((d) => d.name === name && rustBlockAt(layout.blocks, d.at) === m.block);
const files = new Set<string>();
Expand All @@ -11074,18 +11091,58 @@ function rustSubmodule(m: RustModule, name: string, context: ResolutionContext,
}
const block = layout.blocks.find((b) => b.name === name && b.parent === m.block);
if (block) return { file: m.file, dir, block, root: false };
for (const file of [`${dir}.rs`, `${dir}/mod.rs`]) {
// (From outside, a file is the module only when `m` declares it.)
for (const file of !publicOnly || declared ? [`${dir}.rs`, `${dir}/mod.rs`] : []) {
if (context.fileExists(file)) return { file, dir, block: null, root: false };
}
if (depth >= 8) return null;
for (const p of layout.paths.get(name) ?? []) {
if (p.scope || rustBlockAt(layout.blocks, p.at) !== m.block) continue;
if (p.scope || (publicOnly && !p.pub) || rustBlockAt(layout.blocks, p.at) !== m.block) continue;
const passed = rustWalkModules(p.segs, m, context, depth + 1);
if (passed !== null && passed !== 'outside') return passed;
}
return null;
}

/**
* The module `name` that a glob `m` writes at its own level brings in, as Rust
* looks a name up past a module's own items and `use`s (#3253): a test's
* `use super::*` sees its parent's `mod gzip;`, `pub` or not, and a glob of
* any other module only the modules that one makes public — through its own
* globs too, when they are `pub use …::*`. A glob's path is walked without
* globs, so modules that glob each other end the walk, as `seen` (the lookups
* under way) does for a module reached twice.
*/
function rustGlobbedSubmodule(
m: RustModule,
name: string,
context: ResolutionContext,
depth: number,
publicOnly: boolean,
seen: Set<string>,
): RustModule | null {
if (m.choices) {
return rustCfgChoice(m.choices.map((c) => rustGlobbedSubmodule(c, name, context, depth, publicOnly, seen)).filter((c) => c !== null));
}
if (depth >= 8) return null;
const layout = rustUsesOf(m.file, context);
if (layout.globPaths.length === 0) return null;
const key = `${m.file}\0${m.block?.open ?? -1}\0${name}\0${publicOnly ? 1 : 0}`;
if (seen.has(key)) return null;
seen.add(key);
for (const g of layout.globPaths) {
if (g.scope || (publicOnly && !g.pub) || rustBlockAt(layout.blocks, g.at) !== m.block) continue;
const globbed = rustWalkModules(g.segs, m, context, depth + 1, false);
if (globbed === null || globbed === 'outside') continue;
// What a glob brings in is what its module lets `m` see: all of it from inside.
const outside = !rustModuleWithin(m, globbed, context);
const found = rustSubmodule(globbed, name, context, depth + 1, outside) ??
rustGlobbedSubmodule(globbed, name, context, depth + 1, outside, seen);
if (found) return found;
}
return null;
}

/** One module for the cfg alternatives `mods`; null for none. */
function rustCfgChoice(mods: RustModule[]): RustModule | null {
return mods.length > 1 ? { ...mods[0]!, choices: mods } : mods[0] ?? null;
Expand Down Expand Up @@ -11117,9 +11174,10 @@ function rustSuperModule(m: RustModule, context: ResolutionContext): RustModule
* module `from` declares (2018 paths start from the current module); a project
* crate by the name its manifest gives the file's crate, or by its own.
* 'outside' for the standard library and every other crate; null when a step
* finds no module the project holds.
* finds no module the project holds. Without `globs`, a first segment that
* only a glob of `from` brings in is none.
*/
function rustWalkModules(segs: readonly string[], from: RustModule, context: ResolutionContext, depth = 0): RustModule | 'outside' | null {
function rustWalkModules(segs: readonly string[], from: RustModule, context: ResolutionContext, depth = 0, globs = true): RustModule | 'outside' | null {
const [first, ...rest] = segs;
if (first === undefined) return from;
let at: RustModule | null;
Expand All @@ -11129,6 +11187,8 @@ function rustWalkModules(segs: readonly string[], from: RustModule, context: Res
else if (RUST_STD_ROOTS.has(first)) return 'outside';
else {
at = rustSubmodule(from, first, context, depth);
// One a glob of `from` brings in: `gzip::write()` under `use super::*` (#3253).
if (at === null && globs) at = rustGlobbedSubmodule(from, first, context, depth, false, new Set());
if (at === null) {
const graph = rustCrateGraph(context);
const own = rustCrateDirOf(from.file, graph);
Expand Down Expand Up @@ -11808,7 +11868,8 @@ const RUST_PATH_TARGETS = new WeakMap<ResolutionContext, Map<string, string | nu
/**
* The item a Rust path written in `ref`'s file names — `crate::util::take`,
* `super::Item` (from an inline `mod tests { }` too), `util::take` through a
* module a `use` binds, a project crate's by its name — walking the modules as
* module a `use` binds (one in the function first) or a glob brings in
* (`use super::*`), a project crate's by its name — walking the modules as
* Rust does from the one the path is written in (rustWalkModules), else, as a
* 2015 path, from its crate's root, to what the last module declares, passes
* on (`pub use`) or globs in (rustModuleItems): a function, a type, a trait —
Expand All @@ -11835,15 +11896,20 @@ export function rustPathTarget(ref: UnresolvedRef, context: ResolutionContext):
const from = rustModuleAt(ref.filePath, offset, context);
const head = segs[0]!;
const bare = head !== 'crate' && head !== 'self' && head !== 'super';
// A `use` in the function, or in a block of it, binds the head there alone,
// ahead of the module's own: `fn f() { use crate::store::gzip; gzip::write() }`.
let headUse: RustVisibleUse | undefined;
if (bare) {
const bound = rustVisibleUse(head, ref.filePath, offset, context);
if (bound && bound.std.length > 0 && bound.paths.length === 0) return null;
if (bound && bound.block >= 0 && bound.paths.length > 0) headUse = bound;
}
let memo = RUST_PATH_TARGETS.get(context);
if (!memo) RUST_PATH_TARGETS.set(context, (memo = new Map()));
// (A type a `use` binds is that `use`'s, wherever it is written.)
// (A type a `use` binds is that `use`'s, wherever it is written; so is a head.)
const scope = ownerUse ? `${ownerUse.block}\0${ownerUse.paths[0]!.at}` : '';
const key = `${ref.filePath}\0${from.block?.open ?? -1}\0${variants ? 1 : 0}\0${scope}\0${segs.join('::')}`;
const headScope = headUse ? `${headUse.block}\0${headUse.paths[0]!.at}` : '';
const key = `${ref.filePath}\0${from.block?.open ?? -1}\0${variants ? 1 : 0}\0${scope}\0${headScope}\0${segs.join('::')}`;
const hit = memo.get(key);
if (hit !== undefined) return hit === null ? null : context.getNodeById?.(hit) ?? null;
// The items the path's segments name — up to its type's, for a typed one.
Expand All @@ -11853,6 +11919,16 @@ export function rustPathTarget(ref: UnresolvedRef, context: ResolutionContext):
const ids = rustUseItemsAt(path[0]!, ref, context);
return ids === null ? null : [...ids].map((id) => context.getNodeById?.(id)).filter((n): n is Node => !!n);
}
// A head a `use` in the function binds: the path through that `use`'s, from where it is written.
if (headUse) {
const found: Node[] = [];
for (const p of headUse.paths) {
const items = rustUsePathItems([...p.segs, ...path.slice(1)], rustModuleAt(ref.filePath, p.at, context), context, new Set());
if (items === null) return null;
found.push(...items);
}
return found;
}
let items = rustUsePathItems(path, from, context, new Set());
// As a 2015 path, from the crate's root — unless the module declares the
// head itself, where a `#[path]` puts it (a cfg's choice): another module
Expand Down