Skip to content

Commit

Permalink
Ensure that fn config_path returns canonicalized paths.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anforowicz authored and ytmimi committed Aug 14, 2024
1 parent 68dc912 commit caaa612
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ fn get_toml_path(dir: &Path) -> Result<Option<PathBuf>, Error> {
match fs::metadata(&config_file) {
// Only return if it's a file to handle the unlikely situation of a directory named
// `rustfmt.toml`.
Ok(ref md) if md.is_file() => return Ok(Some(config_file)),
Ok(ref md) if md.is_file() => return Ok(Some(config_file.canonicalize()?)),
// Return the error if it's something other than `NotFound`; otherwise we didn't
// find the project file yet, and continue searching.
Err(e) => {
Expand Down Expand Up @@ -417,7 +417,11 @@ fn config_path(options: &dyn CliOptions) -> Result<Option<PathBuf>, Error> {
config_path_not_found(path.to_str().unwrap())
}
}
path => Ok(path.map(ToOwned::to_owned)),
Some(path) => Ok(Some(
// Canonicalize only after checking above that the `path.exists()`.
path.canonicalize()?,
)),
None => Ok(None),
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ impl FromStr for IgnoreList {
/// values in a config with values from the command line.
pub trait CliOptions {
fn apply_to(self, config: &mut Config);

/// It is ok if the returned path doesn't exist or is not canonicalized
/// (i.e. the callers are expected to handle such cases).
fn config_path(&self) -> Option<&Path>;
}

Expand Down

0 comments on commit caaa612

Please sign in to comment.