Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

rustfmt.toml not respected? #132

Closed
Sushisource opened this issue Aug 23, 2017 · 20 comments
Closed

rustfmt.toml not respected? #132

Sushisource opened this issue Aug 23, 2017 · 20 comments
Labels

Comments

@Sushisource
Copy link

Hello

Loving the plugin. Did notice something I'm fairly sure is a bug:

I've got a rustfmt.toml with

fn_args_density = "Compressed"

In it, and when I run rustfmt on the command line that appears to work, but when running from VS Code it'll use the default "Tall" behavior. This is version 0.2.2 of the plugin.

@nrc
Copy link
Member

nrc commented Aug 23, 2017

Does your rustfmt.toml work with the latest rustfmt-nightly? We do explicitly look for a rustfmt.toml, so this should work, you may have hit a bug though.

@nrc nrc added the bug label Aug 23, 2017
@Sushisource
Copy link
Author

Certainly seems so, running rustup show says I'm on nightly and then cargo fmt produces the correct result while format in VS Code undoes it

@nrc
Copy link
Member

nrc commented Aug 24, 2017

the rust version is not linked to the rustfmt version. Try running rustfmt --version, you should see something like nightly-0.2.2

@Sushisource
Copy link
Author

Ah, yup, my bad. That was exactly it. I must've somehow had an old version. It also seemed if I had a directory override set to apply it didn't apply to running rustfmt bare, it complained about missing dlls after I updated it. Running it with "rustup run nightly" worked fine though. My original problem doesn't exist though, both ways are consistent. Thanks!

@Sushisource
Copy link
Author

Sushisource commented Aug 25, 2017

Actually, I'm gonna flip-flop. They do seem divergent. This code is preserved by the vscode plugin as-is:

    let mut get_siz = || {
      sizer.ind_sample(&mut rng).abs().max(scaler / 30.0).min(scaler / 2.0) as f32
    };

The long line is > 80 cols.

But this:

>rustup run nightly rustfmt src\dungeongen\rooms.rs
>rustup run nightly rustfmt -V
0.2.2-nightly ( )

Results in:

    let mut get_siz = || {
      sizer
        .ind_sample(&mut rng)
        .abs()
        .max(scaler / 30.0)
        .min(scaler / 2.0) as f32
    };

With this config:

max_width = 80
tab_spaces = 2
fn_args_density = "Compressed"
fn_single_line = true
single_line_if_else_max_width = 70

Actually after typing all that I just put in some random whitespace, and seems it's jut not formatting at all, which I don't think was happening earlier, so I've got no clue now why it's gone wrong. There aren't any errors in the problems tab. Sorry for the awful bug report.

@Sushisource Sushisource reopened this Aug 25, 2017
@colemickens
Copy link

I'm having this problem.

$ rustfmt --version
0.9.0-nightly ( )

When I run cargo fmt -- --write-mode=overwrite, it respects Cargo.toml and doesn't put annotations next to fields in structs.

When I save in VS Code, it seems to be ignoring the rustfmt.toml. Which version does VS Code use? I would've assumed it's the same one as the nightly toolchain is my default.

@scottjmaddox
Copy link

I'm also having this problem. cargo fmt respects rustfmt.toml, but VSCode Format Document does not.

I'm on macOS 10.12.6
VSCode version is 1.17.0
Rust (rls) version is 0.3.1

$ cargo fmt -- --version
0.2.8-nightly ( )

@stiv-yakovenko
Copy link

I also have the same problem, rustfmt ignores rustfmt.toml

@dvdplm
Copy link

dvdplm commented Nov 30, 2017

I am seeing the same thing here, vscode does not seem to respect the rustfmt.toml while cargo fmt -- --write-mode=overwrite does.

Version:

$ cargo fmt -- --version
0.6.2 ()

@jonasbb
Copy link
Contributor

jonasbb commented Nov 30, 2017

@dvdplm You are using the old version of rustfmt. If you want to compare the formatting of rustfmt and rls you need to use the rustfmt-nightly crate.

@dvdplm
Copy link

dvdplm commented Nov 30, 2017

@jonasbb thank you, I missed that. However, even with rustfmt-nightly at the right version the problem with VSCode remains. :/

@norru
Copy link

norru commented Jul 28, 2018

I am seeing a similar problem in Eclipse Rust corrosion plugin. If it helps, in my case It does seem to be caused by rls launching rustfmt from the user's home directory rather than the project root. This seems to be causing rustfmt to attempt loading $HOME/.rustfmt.toml rather than /path/to/cargo_project/.rustfmt.toml

@Xanewok
Copy link
Member

Xanewok commented Apr 7, 2019

This should be fixed on VSCode side of things, since we always spawn RLS in the workspace side of things, however RLS itself might need to take the rootUri into account instead of the cwd.

Opened rust-lang/rls#1418 to track.

@Xanewok Xanewok closed this as completed Apr 7, 2019
@timvermeulen
Copy link

I still seem to be having this issue (I'm using rustfmt-nightly).

@supernomad
Copy link

supernomad commented Nov 1, 2019

for what its worth I am seeing this still as well I have tried a rustfmt.toml and a .rustfmt.toml neither seem to get picked up by rls when formating on save is enabled in vscode. When I run cargo +nightly fmt -- --emit=files everything works as expected 😞.

$ rustfmt --version
rustfmt 1.4.9-nightly (33e3667 2019-10-07)
$ rls --version
rls 1.38.0 (7b0a20b 2019-08-11)

rls-vscode: 0.7.0

I am not entirely sure what exactly is happening but would love some guidance on how to fix this particular issue.

@tobx
Copy link

tobx commented Feb 1, 2020

My current workaround is to edit settings.json:

"rust.rustfmt_path": "~/.cargo/bin/rustfmt"

It might help to replace ~ with ${env.HOME} if there are any issues. On macOS this works.

@edwardloveall
Copy link

FWIW, I spend a day working with this problem and have learned some things:

  • A vscode extension thinks the path of child_process.execSync is /
  • If you run rustfmt from / it does not pick up the rustfmt.toml

For example:

cd /full/path/to/rust
cat /full/path/to/rust/file.rs | rustfmt

# formatted rust that uses rustfmt.toml

cd /
cat /full/path/to/rust/file.rs | rustfmt

# default formatted rust

I've been playing around with this extension and adding the project directory to child_process.execSync fixes this for me:

function getFormattedString(doc: vscode.TextDocument): string {
+	const documentUri = vscode.Uri.parse(doc.fileName);
+	const workspaceDir = vscode.workspace.getWorkspaceFolder(documentUri);
	return child_process.execSync("rustfmt", {
		encoding: 'utf-8',
		input: doc.getText(),
+		cwd: workspaceDir.uri.fsPath,
	}).toString();
}

I realize this is for an entirely different project but this is what comes up in search result so perhaps it will help someone.

@chamons
Copy link

chamons commented Mar 8, 2021

I am still seeing this in 3/21.

@chevdor
Copy link

chevdor commented Jul 28, 2021

FYI: I implemented the fix from @edwardloveall and it seems to be working fine.
I submitted a PR to the original author but the repo did not see updates since 2019...

I also published a .vsix package. You can alctually build it yourself with the npm run package new package script from my repo.

NOTE: I am not planning take over the maintenance of the upstream repo :) an I hope the original author will update. And if not, you now have a plan B.

@lnicola
Copy link
Member

lnicola commented Jul 28, 2021

CC rust-lang/rustfmt#4660

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests