Skip to content

fix(rust): keep a use crate:: import inside its own crate (#5240) - #5263

Open
Max17190 wants to merge 1 commit into
Graphify-Labs:v8from
Max17190:fix/rust-use-crate-per-file
Open

Max17190 wants to merge 1 commit into
Graphify-Labs:v8from
Max17190:fix/rust-use-crate-per-file

Conversation

@Max17190

Copy link
Copy Markdown

Fixes #5240

Why

In a Cargo workspace, use crate::api::ApiError in one crate can land on another crate's ApiError as an EXTRACTED edge. The graph then states a dependency that cannot exist. On ripgrep, 20 use crate:: imports point at the wrong crate. For example, the Error in globset's use crate::{Candidate, Error, ErrorKind, new_regex} reaches the regex crate's Error.

Summary

Every crate's use crate::…::X targets the same bare stub id. _rewire_unique_stub_nodes scopes each stub to its own file's crate (#4699), but it keeps one answer per id and applies it to every edge. Whichever file left the stub decides for all crates. The issue points at the nested mod tests, but a top-level use triggers it too.

  • _use_stubs gives each importing file its own stub instead of one per name, unless the file already left a stub for that name.
  • The rewire keeps one answer per Rust file, and each Rust edge uses its own file's answer.
  • A file whose crate has no unique match leaves the import unbound instead of borrowing another crate's answer.
  • Edges with no per-file answer, which includes every non-Rust edge, keep the existing per-id answer.

This keeps one invariant. A Rust use crate:: import resolves to a definition in the importing file's own crate, or stays unbound.

The change runs after per-file extraction, so cached AST results stay valid and the cache schema doesn't change.

Limitations:

  • When the importing crate defines two items with the same name, the import stays unbound, as it did before.
  • A use that names another crate, like use grep_regex::RegexMatcher, is out of scope. The extractor records the imported name only for crate:: paths, so these edges get no stub of their own and keep the per-id answer, unchanged from v8. Some still land in the wrong crate. In ripgrep, grep-printer's use grep_regex::RegexMatcher reaches grep-searcher's test RegexMatcher. Fixing that means recording the name in the extractor, which changes cached extraction output, so I left it for a follow-up.

Test plan

uv run pytest tests/ -q                     # 11428 passed, 134 skipped, 1 xfailed (Python 3.11 and 3.13)
uv run pytest tests/test_rust_crate_use_scope.py -q   # 23 passed; the 4 new tests fail on v8
uv run ruff check .                         # passes
uv run pyright                              # same 750 errors as v8, none in changed lines
uv run python -m tools.skillgen --check     # 134 artifacts match

End to end with graphify update .:

  • On the issue's fixture, crates_core_src_client_calls now imports crates_core_src_api_mod_apierror. On v8 it imports crates_server_src_error_apierror.

  • The import stays correct on a warm-cache rebuild, in a checkout copied to a new folder, and through a symlinked path.

  • After touching each of the fixture's four files, an incremental update matches a fresh full build. On v8 all four differ.

  • ripgrep @ 178cc02: 20 wrong-crate imports now land in their own crate and 13 unresolved imports now resolve.

  • axum @ 2449f16: 10 wrong-crate imports and 7 unresolved imports are fixed. axum-extra's Multipart import is now unbound because that crate defines two Multipart types. On v8 it pointed at axum's.

  • One other change shows up in both repos. Where a test module imports its own file's item, like use super::file_name, that file's contains edge to the item becomes imports_from. v8 already does this in a single-crate project, and now it also happens when the import resolves in a workspace.

  • I checked a sample of the changed edges against the source of both repos.

  • Regression tests fail on v8 and pass with the fix

  • No generated skill files changed

  • Extraction stays deterministic, and incremental updates match a full build

  • No shell or Python interpolation, secrets, or local graph data

  • AI help disclosed in the commit trailer

…-Labs#5240)

In a Cargo workspace, every crate's `use crate::…::X` targets the same
bare stub id. The rewire scopes each stub to its own file's crate
(Graphify-Labs#4699), but it kept one answer per id and applied it to every edge. One
file's stub decided where every crate's import landed. On ripgrep, the
`Error` in globset's `use crate::{Candidate, Error, ErrorKind, new_regex}`
reached the regex crate's `Error`.

Resolve per file instead. Each importing file gets its own `use` stub,
and each Rust edge uses its own file's answer. A file whose crate has no
unique match leaves its import unbound rather than borrowing another
crate's answer.

Fixes Graphify-Labs#5240

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Max17190
Max17190 requested a review from safishamsi as a code owner November 16, 2026 19:59
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Max17190. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.11 and 3.13, and that the change keeps extraction deterministic.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 2 changed function(s) (1 sampled, not proven, 1 not verified).

Formal verification. PR-changed functions: 1/2 verified (0 proven, 1 may-equivalent, 0 distinguished) · 1 not verified (1 vacuous).

Not verified on this run: extract (vacuous: never exercised).


Graphify review — findings

Fixes Rust use crate::…::X imports landing in the wrong crate when two crates each import their own X. Previously every such import shared one bare stub id, so one crate's resolution could decide where another crate's edge pointed. _rewire_unique_stub_nodes now records each stub's answer per importing file and has Rust edges take their own file's result. If two stubs in one file disagree, or the file's crate has no match, that file's edges stay unbound instead of borrowing another crate's target. extract also lends one use stub per importing file rather than one per name, and skips files that already have a stub. New tests cover two crates with the same name, use-only crates, a nested mod tests import, and a crate with no local match.

No blocking issues surfaced. 1 lower-confidence candidate did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 4791 functions depend on the 433 functions this change touches.

Health — grade A; 10 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):

  • extract() — 1257 callers, 78 callees (high)
  • _rebuild_code() — 281 callers, 76 callees (high)
  • build_from_json() — 319 callers, 25 callees (high)
  • build_merge() — 123 callers, 18 callees (high)
  • dispatch_command() — 8 callers, 154 callees (high)
  • extract_js() — 101 callers, 8 callees (high)
  • build() — 92 callers, 6 callees (high)
  • main() — 126 callers, 3 callees (high)
  • …and 2 more

Verification — 4791 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 4077 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

334 of 581 test file(s) selected (57%) via static blast radius.

  • tests/test_4874_jvm_resolution.py — impact
  • tests/test_4978_reconcile_cost.py — impact
  • tests/test_4992_ts_import_types.py — impact
  • tests/test_affected_overrides.py — impact
  • tests/test_alias_folding.py — impact
  • tests/test_analyze.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_blade_extractor.py — impact
  • tests/test_build.py — impact
  • tests/test_build_located_semantic_identity.py — impact
  • tests/test_build_merge_dedup_scope.py — impact
  • tests/test_build_merge_ghost_fold_4955.py — impact
  • tests/test_build_merge_hyperedges_and_prune.py — impact
  • tests/test_build_merge_shrink_guard.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_cache.py — impact
  • tests/test_carried_hyperedge_remap.py — impact
  • tests/test_case_only_decl_ids_4904.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cli_export.py — impact
  • tests/test_cluster.py — impact
  • tests/test_cobol_extractor.py — impact
  • tests/test_confidence.py — impact
  • tests/test_confidence_score_output.py — impact
  • tests/test_corrupt_graph_json.py — impact
  • tests/test_cpp_alias_member_resolution.py — impact
  • tests/test_cpp_anonymous_namespace_receivers.py — impact
  • tests/test_cpp_conditional_initializers.py — impact
  • tests/test_cpp_forward_declarations.py — impact
  • tests/test_cpp_forward_receiver_completion.py — impact
  • tests/test_cpp_inherited_member_resolution.py — impact
  • tests/test_cpp_local_callable_shadowing.py — impact
  • tests/test_cpp_member_call_by_receiver_type.py — impact
  • tests/test_cpp_member_declaration_identity.py — impact
  • tests/test_cpp_method_declarations.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cpp_receiver_chains.py — impact
  • tests/test_cpp_receiver_parameters.py — impact
  • tests/test_cpp_split_header_owner.py — impact
  • tests/test_cpp_template_members.py — impact
  • tests/test_cpp_template_type_params.py — impact
  • tests/test_cpp_type_alias.py — impact
  • tests/test_cpp_underscore_ids.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_language_call_resolution.py — impact
  • … and 284 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

No difference found (not proven): No behavior difference found in \_rewire\_unique\_stub\_nodes (not a proof).

The verifier ran both versions of \_rewire\_unique\_stub\_nodes on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.

Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.

Note: An input the sampler did not try could still differ.

Could not verify: Could not verify extract.

The verifier did not have enough to check extract, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 90 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — names the real obstacle, not a sampling gap)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust: use crate::X resolves to another workspace crate's same-named X when that crate imports X in a nested mod tests

1 participant