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 🙏 |
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 |
3f3d54b to
4270e15
Compare
|
@matthewhughes934 No problem, I have rewritten the PR as suggested. Please review! |
| config_path_not_found(path.to_str().unwrap()) | ||
| } | ||
| } | ||
| Some(path) if path.is_dir() => resolve_project_file(path, false), |
There was a problem hiding this comment.
I think it's clearer if we add a separate function for the user dir logic, rather than overloading with a bool argument.
| Some(path) if path.is_dir() => resolve_project_file(path, false), | |
| Some(path) if path.is_dir() => resolve_project_file(path).or(config_from_user_dirs())?, |
where
fn config_from_user_dirs() -> Result<Option<PathBuf>, Error> {
for dir in [dirs::home_dir(), dirs::config_dir()].iter().flatten() {
if let Some(path) = get_toml_path(&dir)? {
return Ok(Some(path));
}
}
Ok(None)
}| ); | ||
| assert_eq!(stdout, ""); | ||
|
|
||
| let args = ["--config-path", toml_name, src_name]; |
There was a problem hiding this comment.
what does this part of the verify that the previous one didn't?
| let args = ["--config-path", src_dir, &src_file]; | ||
| let (stdout, stderr) = rustfmt(&args); | ||
|
|
||
| assert_eq!(stderr, ""); | ||
| // Due to `disable_all_formatting = true` in `tests/config/issue_4660/inner_lib/rustfmt.toml`, | ||
| // the source file should not be modified. | ||
| assert_eq!(stdout, ""); |
There was a problem hiding this comment.
I don't think this test catches anything: the file is already formatted correctly, and I don't think rustfmt some_file.rs will print anything to stdout if it did format (but it will with --check) I think
| let args = ["--config-path", src_dir, &src_file]; | |
| let (stdout, stderr) = rustfmt(&args); | |
| assert_eq!(stderr, ""); | |
| // Due to `disable_all_formatting = true` in `tests/config/issue_4660/inner_lib/rustfmt.toml`, | |
| // the source file should not be modified. | |
| assert_eq!(stdout, ""); | |
| let args = ["--config-path", src_dir, "--check", &src_file]; | |
| let (stdout, stderr) = rustfmt(&args); | |
| assert_eq!(stderr, ""); | |
| // Due to `disable_all_formatting = true` in `tests/config/issue_4660/inner_lib/rustfmt.toml`, | |
| // the source file should not be modified. | |
| assert_eq!(stdout, ""); |
The main.rs should be rewritten to something that would format, e.g.
fn main() {
println!(
"Hello, world!");
}
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 withexists and metadata says it's a dir, then search for predefined names (/, or if it[".rustfmt.toml", "rustfmt.toml"]) underpathand all its parents. Bails out if none is found.Otherwise, interpret it as a TOML file by searching for its filename under its parent dir and all parents of the latter. Bails out if none is found.Remains unchanged as per fix(config): search for config file recursively on--config-path#7115 (comment).