Conversation
ba0a09c to
6bc2a74
Compare
|
I still some concerns regarding the distinction between the dir mode and the TOML mode, so reconverting to draft. |
6bc2a74 to
3f3d54b
Compare
|
Addressed the concern regarding mode switching; updated both the PR description accordingly to reflect the new design. This PR is ready for review 🙏 |
| let config_file_path = get_toml_path(path)?; | ||
| Some(path) => { | ||
| let config_file_path = | ||
| if path.as_os_str().as_encoded_bytes().last() == Some(&b'/') || path.is_dir() { |
There was a problem hiding this comment.
is .is_dir not a sufficient check here?
There was a problem hiding this comment.
@matthewhughes934 Originally that had been largely sufficient, but with the new semantics, the path to be passed in through the base dir mode might not exist immediately, and has to be found via recursive search. In this case, a trailing / makes it clear that we are looking for a directory rather than a TOML file.
This should be able to match how rust-analyzer is calling rustfmt: it is passing the parent directory of the file, which itself is passed in via stdin.
There was a problem hiding this comment.
but with the new semantics, the path to be passed in through the base dir mode might not exist immediately
Can you explain that case more? If someone passes as path that doesn't existing to --config-path I would expect it to error out.
My primary concern here is that / is not a path separator on Windows mostly because of Windows weirdness I expect simply checking for \ might also not be sufficient here
There was a problem hiding this comment.
Can you explain that case more? If someone passes as path that doesn't existing to --config-path I would expect it to error out.
@matthewhughes934 I totally get your point no, and my apologies for potential confusion. Here is the full background:
Firstly, when rust-analyzer tries to format dir/file.rs, it wants to pass --config-path=dir to it, and that directory may or may not have the expected .rustfmt.toml or rustfmt.toml, so a recursive search should be initiated starting from there. However, due to #4660 being unresolved, rust-analyzer has instead used the hack of cding to dir when running rustfmt.
From there, I'd agree that --config-path=dir should bail out when dir doesn't exist.
Secondly, however, if the user wants to use a different name for the config file, taking the example mentioned in #4660 (comment):
rustfmt = { extraArgs = { "+nightly", "--config-path=.rustfmt.unstable.toml" } },The file (.rustfmt.unstable.toml in the above example) is better placed at CWD, but it may totally be at a different place. This time I OTOH don't want --config-path=file to bail out immediately because I may want to use ../../file etc.
The crux of the problem here, it seems to me, is that --config-path is overloaded with two meanings and this has made it hard for us to disambiguate, for which some manual intervention must be involved.
The trailing / is, as you said, probably not the best idea for disambiguation.
Could you expand more on this use case, where is |
|
@matthewhughes934 TLDR, I'll interpret this case as "I want to override the config file name, but at the same time I also want to override the base directory, but unfortunately they are under the same flag". In the case I posted above, I have Unfortunately, Looking back, of course you can argue that it's not
If you agree with this, I can change the feature commit quite quickly to reflect it. |
👍 this sounds good to me, I think the directory behaviour sounds closer to what's documented in the help output. It might be worth further discussing the file behaviour (there's also some discussion on it with #5206), but that's best done separately to this change. CC @ytmimi since you were discussing on the original issue |
Note
LLM has been used to analyze the existing usage of directory walking behavior.
The implementation of this patch is otherwise fully manual.
Closes #4660 based on the design discussed in #4660 (comment).
Background
The
--config-pathhelp description says:However this description is not quite appropriate because:
rustfmt.To be more precise, the current semantics of
--config-pathis that when it receives apath, it chooses to enter one of the following modes in the below fallback order:pathis exists and metadata says it's a dir, then search for predefined names ([".rustfmt.toml", "rustfmt.toml"]) underpathonly. Bails out if none is found.pathexists and the metadata says it's not a dir, then interpret it as a TOML file. Bails out if none is found.Proposed solution
This PR changes it to:
pathterminates with/, or if it exists and metadata says it's a dir, then search for predefined names ([".rustfmt.toml", "rustfmt.toml"]) underpathand all its parents. Bails out if none is found.