Skip to content

Use --config-path instead of changing working directory when calling rustfmt #23412

Open

Description

@rami3l

Rationale

In the past, rustfmt didn't implement proper directory walking semantics with the --config-path=<dir> parameter. So the following hack of changing the working directory of the rustfmt subprocess is used instead:

// try to chdir to the file so we can respect `rustfmt.toml`
// FIXME: use `rustfmt --config-path` once
// https://github.com/rust-lang/rustfmt/issues/4660 gets fixed
let current_dir = match text_document.uri.to_file_path() {
Ok(mut path) => {
// pop off file name
if path.pop() && path.is_dir() { path } else { std::env::current_dir()? }
}

However, the above hack is preventing me from doing the following IDE-specific --config-path override in the rustup repo, because the resolution of --config-path is always relative to the working directory:

rustfmt = { extraArgs = { "+nightly", "--config-path=.rustfmt.unstable.toml" } },

Proposed solution

After fixing rustfmt so that it has the correct directory walking semantics in rust-lang/rustfmt#7115, I'd love to offer a patch here regarding the above rust-analyzer logic, by replacing the hack with a properly-constructed --config-path= parameter if one doesn't exist already.

Concerns

The replacement should be in theory quite simple but it potentially involves rust-analyzer working with a sufficiently old rustfmt where that parameter was flawed.

Please feel free to let me know if there are any compatibility concerns to be considered and/or any prior art I can look into.

Many thanks in advance :)

References

Related to:

Activity

  1. ChayimFriedman2 commented on Sep 24, 2026

    @ChayimFriedman2
    Contributor

    Makes sense but I do wonder if it's not too early given this was only fixed a few days ago. I'd say, after this reaches stable we can use this after guarding on the Rust version.

    Also note that there are several places we call rustfmt.

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