Skip to content

[Feature Request] Read cargo fmt configurations from .cargo/config.toml #5988

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
aDogCalledSpot opened this issue Dec 22, 2023 · 7 comments
Closed

Comments

@aDogCalledSpot
Copy link

aDogCalledSpot commented Dec 22, 2023

I would like a single project to use a custom formatter. I have created a .cargo/config.toml with the following contents:

[env]
RUSTFMT = "echo"

however, the usual formatter appears to be called.

Using RUSTFMT="echo" cargo fmt returns /path/to/main.rs --edition 2021 as expected.

@ytmimi
Copy link
Contributor

ytmimi commented Dec 22, 2023

Thanks for reaching out!

cargo fmt is really just a thin wrapper around rustfmt that reads metadata from your Cargo.toml file, and rustfmt only reads configurations values from the .rustfmt.toml or rustfmt.toml files.

@calebcartwright correct me if I'm wrong, but I don't think there's been any discussion about reading configurations / env variables from the .cargo/config.toml. Is it common for other tools to read configuration values from there?

@aDogCalledSpot to the best of my knowledge, and the Contributing guide the RUSTFMT environment variable is meant as a testing utility when developing and working on rustfmt locally.

As you've demonstrated above, you can certainly use the RUSTFMT environment variable to point to an arbitrary binary but that's not the intended use case.

@ytmimi ytmimi changed the title rustfmt doesn't use RUSTFMT environment variable in config.toml [Feature Request] Read cargo fmt configurations from .cargo/config.toml Dec 22, 2023
@aDogCalledSpot
Copy link
Author

Interesting, I wasnt aware of the intended use case. Sorry.

Since this is a feature request now, Ill give some more context of what I'm trying to achieve.

I have a project using Yew with some other collaborators. I recently found yew-fmt which fomats the code in Yew's html macro which is otherwise left untouched by rustfmt, and otherwise hands the rest of the code off to rustfmt.

It makes sense to use this for the project but I dont want to set RUSTFMT globally in my shell.

@ytmimi
Copy link
Contributor

ytmimi commented Dec 22, 2023

Looks like this is where the RUSTFMT environment variable comes from: #4419, so maybe it's not just for testing as I had assumed.

Thanks for sharing your usecase. In order to support it we'd need to read configurations from the .cargo/config.toml. I just don't know if that's something other tools do or if that's something we want to support in rustfmt.

@ytmimi
Copy link
Contributor

ytmimi commented Dec 22, 2023

I created a shell script called my_custom_formatter.sh with the following content:

echo "Hey There you're using my custom formatter!!!"
echo "passing the following to rustfmt!"
for var in "$@"
do
    echo "$var"
done

Running the cargo-fmt binary built from source with

cargo --config="env.RUSTFMT= \"path/to/my_custom_formatter.sh\"" run --bin cargo-fmt -- --version

prints

Hey There you're using my custom formatter!!!
passing the following to rustfmt!
--version

Running RUSTFMT="/path/to/my_custom_formatter.sh" cargo fmt -- --version also prints

Hey There you're using my custom formatter!!!
passing the following to rustfmt!
--version

However running the cargo-fmt that I have installed on my machine with

cargo --config="env.RUSTFMT= \"path/to/my_custom_formatter.sh\"" fmt -- --version

prints out rustfmt 1.6.0-nightly (1c05d50c 2023-10-21).

From what I can tell this doesn't seem to be an issue with the rustfmt or cargo-fmt binaries. It seems that cargo has special knowledge of the RUSTFMT environment variable: https://github.com/rust-lang/cargo/blob/363a2d11320faf531f6aacd1ea067c6bc08343b9/src/doc/src/reference/environment-variables.md?plain=1#L78-L80

@aDogCalledSpot
Copy link
Author

aDogCalledSpot commented Dec 22, 2023

I just tried this in my test project where have the env var set in the config.toml, and to my surprise I get:

error: failed to merge --config key `env` into `/path/to/frontend/.cargo/config.toml`

Caused by:
  failed to merge key `RUSTFMT` between /path/to/.cargo/config.toml and --config cli option

Caused by:
  failed to merge config value from `--config cli option` into `/path/to/.cargo/config.toml`: expected table, but found string

@aDogCalledSpot
Copy link
Author

The values from env are only passed through to the spawned processes by cargo - not handled directly by cargo.

@ytmimi
Copy link
Contributor

ytmimi commented Dec 22, 2023

Yeah. I was just looking at these docs: https://doc.rust-lang.org/cargo/reference/config.html#env, but like I said before cargo reads the RUSTFMT environment variable.

It seems that everything is working as expected, and you'll need to make sure that RUSTFMT is set if you want to leverage it to call a different formatting binary.

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants