Skip to content

Commit caaa612

Browse files
anforowiczytmimi
authored andcommitted
Ensure that fn config_path returns canonicalized paths.
This commit ensures that `fn config_path` in `rustfmt/src/config/mod.rs` always returns canonicalized paths. This is achieved by canonicalizing both: 1) paths received via `CliOptions` (after checking if the path exists) as well as 2) paths returned via `get_toml_path` (canonicalizing within `fn get_toml_path`). Fixes #6178
1 parent 68dc912 commit caaa612

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Diff for: src/config/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
378378
match fs::metadata(&config_file) {
379379
// Only return if it's a file to handle the unlikely situation of a directory named
380380
// `rustfmt.toml`.
381-
Ok(ref md) if md.is_file() => return Ok(Some(config_file)),
381+
Ok(ref md) if md.is_file() => return Ok(Some(config_file.canonicalize()?)),
382382
// Return the error if it's something other than `NotFound`; otherwise we didn't
383383
// find the project file yet, and continue searching.
384384
Err(e) => {
@@ -417,7 +417,11 @@ fn config_path(options: &dyn CliOptions) -> Result<Option<PathBuf>, Error> {
417417
config_path_not_found(path.to_str().unwrap())
418418
}
419419
}
420-
path => Ok(path.map(ToOwned::to_owned)),
420+
Some(path) => Ok(Some(
421+
// Canonicalize only after checking above that the `path.exists()`.
422+
path.canonicalize()?,
423+
)),
424+
None => Ok(None),
421425
}
422426
}
423427

Diff for: src/config/options.rs

+3
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,9 @@ impl FromStr for IgnoreList {
415415
/// values in a config with values from the command line.
416416
pub trait CliOptions {
417417
fn apply_to(self, config: &mut Config);
418+
419+
/// It is ok if the returned path doesn't exist or is not canonicalized
420+
/// (i.e. the callers are expected to handle such cases).
418421
fn config_path(&self) -> Option<&Path>;
419422
}
420423

0 commit comments

Comments
 (0)