rust-lang / rust-lang/rustfmt

cargo fmt fails to format code when using symlinks

Open
#6,555 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the minimal workspace shown in the issue, including the symlink from linked/src/main.rs to plain/src/main.rs. Run cargo fmt --check with and without the explicit linked file path, then trace how rustfmt discovers modules through symlinked files. Done means the default command formats both referenced modules without requiring an explicit path.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.