Skip to content

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

Open

Description

@unbekannt3

Version: graphifyy 0.9.111 (latest on PyPI), Python 3.14, Linux

Summary

In a Cargo workspace with two crates that each define a type of the same name, a use crate::… import in one crate resolves to the other crate's type. It happens once the other crate has a file that itself imports its own type with use crate::… inside a nested mod tests. The correct edge is then missing entirely, so the import lands only on the wrong type.

Reproduction

Cargo.toml                           [workspace] members = ["crates/*"]
crates/core/Cargo.toml               [package] name = "app-core"
crates/core/src/api/mod.rs
crates/core/src/client/calls.rs
crates/server/Cargo.toml             [package] name = "app-server"
crates/server/src/error.rs
crates/server/src/auth/dpop.rs

crates/core/src/api/mod.rs

pub enum ApiError { Network }
pub trait ServerApi {}

crates/core/src/client/calls.rs

use crate::api::ApiError;

crates/server/src/error.rs

pub enum ApiError { NotFound }

crates/server/src/auth/dpop.rs

pub fn check() {}

#[cfg(test)]
mod tests {
  use crate::error::ApiError;

  fn t() -> Option<ApiError> { None }
}

Script:

import sys
from pathlib import Path
from graphify.extract import extract
root = Path(sys.argv[1]).resolve()
r = extract(sorted(root.rglob("*.rs")), cache_root=root, root=root)
for e in r["edges"]:
    if e["target"].endswith("apierror") and e["relation"] in ("imports_from", "references"):
        print(e["relation"], e["source"], "->", e["target"], e.get("source_location"))

Actual

imports_from crates_core_src_client_calls -> crates_server_src_error_apierror L1
imports_from crates_server_src_auth_dpop -> crates_server_src_error_apierror L5
references crates_server_src_auth_dpop_t -> crates_server_src_error_apierror L7

crates/core/src/client/calls.rs imports app-server's ApiError, although crate:: there can only mean app-core.

Expected

imports_from crates_core_src_client_calls -> crates_core_src_api_mod_apierror L1

Notes

Activity

  1. github-actions commented on Nov 16, 2026

    @github-actions

    Thanks for opening this issue, @unbekannt3. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions