Skip to content

invoking rustfmt uses the project-wide rustfmt.toml, not the one for the current subcrate #15540

Closed
@RalfJung

Description

@RalfJung

The rustc repository contains multiple rustfmt.toml files, for instance:

./rustfmt.toml
./src/tools/clippy/rustfmt.toml
./src/tools/miri/rustfmt.toml

So, when invoking formatting via RA in a file in src/tools/miri, I'd hope ./src/tools/miri/rustfmt.toml would be used. But unfortunately it always uses ./rustfmt.toml, which regularly leads to people accidentally formatting Miri files in their rustc PRs with the wrong settings.

This can be reproduced, for example, by opening src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs and invoking Ctrl-Shift-I in vscode.

#7227 looks similar and is supposedly fixed, but I can still reproduce the problem on a recent RA. I'm not sure who is even doing the config file finding here -- is RA telling rustfmt "use this config file", or is rustfmt supposed to somehow figure this out itself? If the latter, how -- the working directory? AFAIK rustfmt doesn't get the actual filename, it just receives the text on stdin and produces the formatted text on stdout.

rust-analyzer version: rust-analyzer version: 0.3.1641-standalone

rustc version: rustc 1.74.0-nightly (59a829484 2023-08-30)

relevant settings:

{
    "rust-analyzer.rustc.source": "./Cargo.toml",
    "rust-analyzer.linkedProjects": [
        "Cargo.toml",
        "src/bootstrap/Cargo.toml",
        //"compiler/rustc_codegen_cranelift/Cargo.toml", // broken due to toolchain file
    ],
    "rust-analyzer.check.invocationLocation": "root",
    "rust-analyzer.check.invocationStrategy": "once",
    "rust-analyzer.check.overrideCommand": [
        "./x.sh",
        "check",
        "--json-output",
        "library/std",
        "compiler/rustc",
        "src/tools/miri",
        // "compiler/rustc_codegen_cranelift", // doesn't actually show any warnings
    ],
    "rust-analyzer.rustfmt.overrideCommand": [
        "./build/host/rustfmt/bin/rustfmt",
        "--edition=2021",
    ],
    // These just make everything way too slow.
    "rust-analyzer.cargo.buildScripts.enable": false,
    "rust-analyzer.procMacro.enable": false,
    // Some diagnostics don't work properly here
    "rust-analyzer.showUnlinkedFileNotification": false,
    "rust-analyzer.diagnostics.enable": false, // without proc macros, there's just too many false positives
    //
    "files.watcherExclude": {
        "*rustc*/src/llvm-project/**": true,
        "*rustc*/build/**": true,
    },
    "files.exclude": {
        "src/llvm-project/**": true,
        "build/**": true
    },
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions