desloppify icon indicating copy to clipboard operation
desloppify copied to clipboard

Rust crate-imports autofix breaks binary imports of its package library

Open cfilipescu opened this issue 3 weeks ago • 0 comments

Problem

In a Rust package with both a library and a binary, rust_import_hygiene treats a binary module's import of the package's library as a same-crate import. Running the suggested crate-imports autofix changes a valid program into one that fails to compile.

Reproduced with Desloppify 1.0; the same ownership assumption is present in the current detector.

Minimal example

Cargo.toml:

[package]
name = "sample-app"
version = "0.1.0"
edition = "2021"

[lib]
name = "sample_lib"

src/lib.rs:

pub mod library {
    pub fn work() {}
}

src/main.rs:

mod binary;
fn main() { binary::run(); }

src/binary.rs:

use sample_lib::library;
pub fn run() { library::work(); }

cargo check --bin sample-app succeeds. desloppify --lang rust detect rust_import_hygiene --path . reports src/binary.rs:1 as a same-crate import. desloppify --lang rust autofix crate-imports --path . rewrites it to use crate::library;. The next cargo check --bin sample-app fails with E0432 because the binary crate root has no library module.

Expected behavior

Imports from the package's library in a binary target should be left intact. Module ownership may be ambiguous when targets share files or use custom paths; in those cases the autofix should avoid rewriting unless it can establish that the import is truly within the same crate.

cfilipescu avatar Nov 13 '26 00:11 cfilipescu