Skip to content

cargo fmt fails to format code when using symlinks #6555

Open
@ckoehne

Description

@ckoehne

When symlinking a file, it looks like cargo fmt isn't able to properly resolve modules. It causes cargo fmt to ignore certain modules referenced by that symlinked file.

Here's a minimal reproducable example:

.
├── Cargo.toml
├── linked
│   ├── Cargo.toml
│   └── src
│       ├── main.rs -> ../../plain/src/main.rs
│       └── unformatted.rs
└── plain
    ├── Cargo.toml
    └── src
        ├── main.rs
        └── unformatted.rs
==> ./Cargo.toml <==
[workspace]
resolver = "2"
members = [ "plain", "linked" ]

==> ./linked/Cargo.toml <==
[package]
name = "linked"
version = "0.1.0"
edition = "2024"

[dependencies]

==> ./linked/src/unformatted.rs <==
pub fn unformatted() { println!("Hello from linked!"); }

==> ./plain/Cargo.toml <==
[package]
name = "plain"
version = "0.1.0"
edition = "2024"

[dependencies]

==> ./plain/src/main.rs <==
mod unformatted;

fn main() {
    unformatted::unformatted();
}

==> ./plain/src/unformatted.rs <==
pub fn unformatted() { println!("Hello from plain!"); }

cargo fmt only formats the plain file by default. You have to explicitly pass linked/src/unformatted.rs to cargo fmt to format it.

> cargo fmt --check
Diff in plain/src/unformatted.rs:1:
-pub fn unformatted() { println!("Hello from plain!"); }
+pub fn unformatted() {
+    println!("Hello from plain!");
+}
 
> cargo fmt --check -- linked/src/unformatted.rs
Diff in plain/src/unformatted.rs:1:
-pub fn unformatted() { println!("Hello from plain!"); }
+pub fn unformatted() {
+    println!("Hello from plain!");
+}
 
Diff in linked/src/unformatted.rs:1:
-pub fn unformatted() { println!("Hello from linked!"); }
+pub fn unformatted() {
+    println!("Hello from linked!");
+}

The rust code itself is working as expected:

> cargo run --manifest-path linked/Cargo.toml
Hello from linked!
> cargo run --manifest-path plain/Cargo.toml
Hello from plain!

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