Skip to content

Format on save in VS Code ignores rustfmt.toml #6973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Benlitz opened this issue Dec 21, 2020 · 17 comments · Fixed by #7371
Closed

Format on save in VS Code ignores rustfmt.toml #6973

Benlitz opened this issue Dec 21, 2020 · 17 comments · Fixed by #7371
Labels
S-actionable Someone could pick this issue up and work on it right now

Comments

@Benlitz
Copy link

Benlitz commented Dec 21, 2020

Hi,

It seems that when I save a file in VS Code, it ignores the rustfmt.toml file and applies the default configuration for formatting. I just added a rustfmt.toml file with only max_width = 160 in it. When I save a file, it clearly applies the default 100 character width. If I run rustfmt [file] in a console it works properly.

Note: I'm working in a workspace with multiple crates in it, and some other crates have their own rustfmt.toml. I doubt this is related to the problem but still mentioning.

@lnicola
Copy link
Member

lnicola commented Dec 21, 2020

Note: I'm working in a workspace with multiple crates in it, and some other crates have their own rustfmt.toml. I doubt this is related to the problem but still mentioning.

It might. Can you still reproduce this in a non-workspace project?

@lnicola lnicola added the S-actionable Someone could pick this issue up and work on it right now label Dec 21, 2020
@Benlitz
Copy link
Author

Benlitz commented Dec 21, 2020

Note: I'm working in a workspace with multiple crates in it, and some other crates have their own rustfmt.toml. I doubt this is related to the problem but still mentioning.

It might. Can you still reproduce this in a non-workspace project?

So indeed, it works in that case.
I also tried to create a very simple workspace with two crates, and only one with a rustfmt.toml, and I still have the problem, so it's unlikely to be a conflict between multiple rustfmt.toml files.

(I also removed the "workaround" I initially mentioned in the description, it doesn't work - formatting isn't run with it)

@d4h0
Copy link

d4h0 commented Jan 17, 2021

Unfortunately, I have the same problem.

My workaround for now, is to use the VSCode extension Run on Save with the following configuration:

"emeraldwalk.runonsave": {
	"commands": [
		{
			"match": "\\.rs$",
			"cmd": "rustfmt '${file}'"
		}
	]
}

(This needs to be added to a workspace.json or the user settings.json file)

I also have disabled the native VSCode "Format on Save" (otherwise, the rustfmt that rust-analyzer executes could lead to problems).

@lnicola
Copy link
Member

lnicola commented Jan 18, 2021

@Benlitz just to make sure, is that a cargo or Code workspace? If you were able to reproduce it in a new project, do you mind sharing it?

@Benlitz
Copy link
Author

Benlitz commented Jan 19, 2021

it was on a cargo workspace, but actually it happens as soon as the folder you open in vscode is a folder above the crate folder.

I'm joining a simple project. Repro:

  • In VS Code, Open Folder and select the (unzipped) test_issue folder
  • Open my_crate/src/main.rs via the explorer panel
  • Press Ctrl+S (with format on save enabled)
    → the file should be reformatted so that lines are <100
  • Open a terminal in VS Code and run rustfmt my_crate/src/main.rs
    → Formatting is back to <160 as specified in the rustfmt.toml

And just in case it's a folder separator issue or something similar: I'm having this issue on Windows. I haven't tested it on Linux yet.

test_issue.zip

@d4h0
Copy link

d4h0 commented Jan 19, 2021

is that a cargo or Code workspace?

I'm using a Cargo Workspace with a VSCode Workspace that is on the same directory.

I haven't tested it on Linux yet.

I use Linux, so the problem is probably not specific to Windows

@lnicola
Copy link
Member

lnicola commented Jan 21, 2021

@Benlitz reproduced on Linux. I think the issue is that we're running rustfmt in the project directory. We can't chdir to the file because it might not exist on disk, but maybe to the Cargo.toml directory? What if there's more than one root? Which rustfmt.toml should we pick?

Any opinion, @matklad?

@matklad
Copy link
Member

matklad commented Jan 21, 2021

Can we set

        --config-path [Path for the configuration file]
                        Recursively searches the given path for the
                        rustfmt.toml config file. If not found reverts to the
                        input file path

@lnicola
Copy link
Member

lnicola commented Jan 21, 2021

But what do we set it to?

@matklad
Copy link
Member

matklad commented Jan 21, 2021

To the path of the .rs file. I hope that "Recursively searches" part will just work then

@flodiebold
Copy link
Member

flodiebold commented Jan 21, 2021

Might be interesting to check what happens if the last part of the config path doesn't exist...

Otherwise we could also just chdir as far down as possible to the file path 🤔

@lnicola
Copy link
Member

lnicola commented Jan 21, 2021

What happens for files that haven't been saved?

EDIT: this happens

image

@lnicola
Copy link
Member

lnicola commented Jan 21, 2021

Looks like it's not as recursive as one would expect:

$ rustfmt --config-path ~/Downloads/test_issue/my_crate/src/main.rs --edition 2018 < ~/Downloads/test_issue/my_crate/src/main.rs
Could not parse TOML: expected an equals, found an identifier at line 1 column 4

$ rustfmt --config-path ~/Downloads/test_issue/my_crate/src/ --edition 2018 < ~/Downloads/test_issue/my_crate/src/main.rs
Error: unable to find a config file for the given path: `~/Downloads/test_issue/my_crate/src/`

$ rustfmt --config-path ~/Downloads/test_issue/my_crate/src --edition 2018 < ~/Downloads/test_issue/my_crate/src/main.rs
Error: unable to find a config file for the given path: `~/Downloads/test_issue/my_crate/src`

$ rustfmt --config-path ~/Downloads/test_issue/my_crate --edition 2018 < ~/Downloads/test_issue/my_crate/src/main.rs
fn fjsdlkfjklsdajflksdjlfdasdsadsadsaadkjdskljflkasj_jflksjdklfjsdlkjflkdjslkfjas(a: i32, b: i32, c: i32) {
    println!("TEST");
}

fn main() {
    fjsdlkfjklsdajflksdjlfdasdsadsadsaadkjdskljflkasj_jflksjdklfjsdlkjflkdjslkfjas(1, 2, 3);
    println!("Hello, world!");
}

$ rustfmt --config-path ~/Downloads/test_issue/ --edition 2018 < ~/Downloads/test_issue/my_crate/src/main.rs
Error: unable to find a config file for the given path: `~/Downloads/test_issue/`

@kangalio
Copy link

Hallelujah, this will eliminate the need to keep changing my global rustfmt config when changing projects. Thank you for the implementation, this is really useful.

@gyzerok
Copy link

gyzerok commented Oct 8, 2022

It still does not seem to work for me. Can someone share their config?

@DanielSWolf
Copy link

Same problem here.

If I go to the Rust-analyzer option "Rustfmt: Extra Args" and enter --config=version=Two, this setting is applied correctly.

But if I create a file rustfmt.toml in the same directory as Cargo.toml and enter version = "Two" there, this file is ignored.

@ConsoleTVs
Copy link

Happened the same to me, having manually specified it in the vscode extension extra arguments works but the file itself seems to be ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants