Skip to content

fix: resolve local self-crate aliases to the target root - #99

Draft
grzegorznowak wants to merge 5 commits into
dragonsec-ai:mainfrom
grzegorznowak:fix/issue-98-self-crate-alias-resolution
Draft

grzegorznowak wants to merge 5 commits into
dragonsec-ai:mainfrom
grzegorznowak:fix/issue-98-self-crate-alias-resolution

Conversation

@grzegorznowak

Copy link
Copy Markdown

Fixes #98.

Architectural approach

This change stays within Extract in the existing SOURCE → EXTRACT → PROJECT → ASSERT → REPORT pipeline. It corrects the dependency information supplied to downstream stages rather than adding alias-specific behavior to projections or architecture rules.

No public API, dependency, lockfile, assertion, or reporting-policy changes are introduced.

Implementation

Two changes in src/common/extraction/extract_dependencies.rs work together:

  1. Collect self-crate bindings as internal aliases.
    collect_internal_aliases now accepts ImportKind::ExternCrate only when its path is exactly ["self"]. Other external-crate declarations remain excluded from internal alias collection.

  2. Resolve the declaration in the target’s root context.
    resolve_direct distinguishes an ExternCrate declaration naming self from an ordinary self:: path. The declaration uses the existing longest_prefix lookup with an empty module path; ordinary self:: paths remain module-relative.

The lookup retains the current package, dependency scope, and Cargo target identity. A binary target therefore resolves to its own root rather than a library root in the same package.

Once collected, the binding uses the existing alias-classification and suffix-resolution paths. No second resolver, filesystem-path special case, or diagnostic suppression is added. Alias lookup remains module-local; this does not introduce inherited or chained-alias resolution.

Regression coverage

  • Inline resolver tests exercise root selection, competing library/binary targets, binding precedence, non-self external-crate exclusion, and alias isolation.
  • A complete fixture project exercises parsing, dependency extraction, and graph construction through the public extraction APIs.
  • The integration test checks individual qualified references separately from the declaration, including exact occurrence counts and a deeper module destination. A correctly resolved declaration alone cannot hide missing qualified references.
  • Graph assertions check internal destinations, merged import kinds, and clean diagnostics.

Admit only exact extern crate self bindings as internal aliases and distinguish their crate-root meaning from ordinary self:: paths. Add eight bounded regressions without extending inherited or chained alias resolution.

Keep regressions in a focused test-only module for a small resolver diff. This fork-only inspection PR has no tracking issue; no upstream publication or merge is requested.
Add a compilable nested-alias fixture and public extraction assertions for the declaration, each qualified root reference, module suffix, diagnostics, and graph edges. Include competing lib/bin root and alias-isolation coverage.

Validate the integration test against the unpatched resolver and an alias-disabled variant that retains declaration-root resolution, ensuring a declaration edge cannot hide missing qualified references.
Move the self-crate alias unit regressions into the inline extraction test
module and the source-backed regression into the existing graph extraction
suite, so the fix reuses established repository test mechanisms.

Fold the alias-resolution and binding-precedence cases into the existing
tables, drop the nested-only and ordinary-`self::` duplicates now covered
through real source, and drop a vacuous line assertion plus a diagnostics
check that `extract_graph` copies by construction.

Coverage is unchanged: declaration and alias root resolution, competing
lib/bin roots with alias isolation, module-suffix resolution, import via a
renamed alias, root-module shadowing, non-self extern exclusion, and
sibling isolation. The graph regression fails when alias collection is
disabled while declaration-root resolution stays.

Gates: cargo fmt --all -- --check, cargo clippy --workspace --all-targets
--all-features --locked -- -D warnings, cargo test --workspace
--all-features (326 unit, 148 integration, 55 doctests).
Introduce `use_alias` in the extraction test module so `use`-style
references with a bound name are constructed in one expression instead of
starting from a plain reference and mutating kind/binding afterward.

Three call sites in the alias-resolution, binding-precedence, and renamed
alias tests switch to the helper. No production code or other test helpers
change.

Gates: cargo fmt --all -- --check, cargo clippy --workspace --all-targets
--all-features --locked -- -D warnings, cargo test --workspace
--all-features (326 unit, 148 integration, 55 doctests).
Reorder the extraction test helpers so the two specialised reference
builders sit together. No behavior change.

Gates: cargo fmt --all -- --check, cargo clippy --workspace --all-targets
--all-features --locked -- -D warnings, cargo test --workspace
--all-features (326 unit, 148 integration, 55 doctests).
@grzegorznowak
grzegorznowak marked this pull request as draft November 15, 2026 16:05

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.

Dependency extraction misses references through local extern crate self aliases

1 participant