Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/ignore_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ impl IgnorePathSet {
pub(crate) fn is_match(&self, file_name: &FileName) -> bool {
match file_name {
FileName::Stdin => false,
FileName::Real(p) => self
.ignore_set
.matched_path_or_any_parents(p, false)
.is_ignore(),
FileName::Real(p) => {
if p.is_absolute() && !p.starts_with(self.ignore_set.path()) {
false
Comment on lines +26 to +27
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the underlying bug is: someone tried to invoke rustfmt with a --config-path that is outside the tree, then

  • Before: we panic
  • Now: we silently ignore it, since is_match always returns false here

I'm wondering if instead we could do one of:

  • Print an error to the user, telling them that the config path isn't valid
  • Try to make sense of it: is there anything we could reasonably do here? If we're running in cargo fmt could we instead use the path to Cargo.toml as the root of ignores?

It doesn't help that --config-path is a bit confusing #5206

Copy link
Copy Markdown
Member

@jieyouxu jieyouxu Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that silently ignoring is very fishy. I think there's a larger question about what an out-of-tree config path w/ ignores is supposed to mean.

I agree that to keep this PR reasonably contained and preserve "not working", I would also say repport an error instead.

} else {
self.ignore_set
.matched_path_or_any_parents(p, false)
.is_ignore()
}
}
}
}
}
Expand Down Expand Up @@ -70,4 +75,21 @@ mod test {
assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz/a.rs"))));
assert!(!ignore_path_set.is_match(&FileName::Real(PathBuf::from("bar_dir/baz/what.rs"))));
}

#[nightly_only_test]
#[test]
fn test_ignore_path_outside_root() {
//See: https://github.com/rust-lang/rustfmt/issues/6843
use crate::config::{Config, FileName};
use crate::ignore_path::IgnorePathSet;

let config_path = std::env::temp_dir().join("a").join("rustfmt.toml");
let file_path = std::env::temp_dir().join("b").join("foo.rs");

let config = Config::from_toml(r#"ignore = ["bar.rs"]"#, &config_path).unwrap();
let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore()).unwrap();

//should not panic
assert!(!ignore_path_set.is_match(&FileName::Real(file_path)));
}
}
Loading