Skip to content

Commit f8d3aa5

Browse files
committed
Add a path to the parent dir of rustfmt.toml as a prefix
Paths users specify in `ignore` configuraiton option is relative to the directory which contains the rustfmt.toml file. When processing the ignore paths internally, rustfmt should add a path to the directory as a prefix since RealPath passed from libsyntax is a full path.
1 parent 1f61286 commit f8d3aa5

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

Diff for: src/config/config_type.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ macro_rules! create_config {
141141
ConfigWasSet(self)
142142
}
143143

144-
fn fill_from_parsed_config(mut self, parsed: PartialConfig) -> Config {
144+
fn fill_from_parsed_config(mut self, parsed: PartialConfig, dir: &Path) -> Config {
145145
$(
146146
if let Some(val) = parsed.$i {
147147
if self.$i.3 {
@@ -160,6 +160,7 @@ macro_rules! create_config {
160160
)+
161161
self.set_heuristics();
162162
self.set_license_template();
163+
self.set_ignore(dir);
163164
self
164165
}
165166

@@ -286,6 +287,9 @@ macro_rules! create_config {
286287
}
287288
}
288289

290+
fn set_ignore(&mut self, dir: &Path) {
291+
self.ignore.2.add_prefix(dir);
292+
}
289293

290294
/// Returns `true` if the config key was explicitly set and is the default value.
291295
pub fn is_default(&self, key: &str) -> bool {

Diff for: src/config/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ impl Config {
190190
let mut file = File::open(&file_path)?;
191191
let mut toml = String::new();
192192
file.read_to_string(&mut toml)?;
193-
Config::from_toml(&toml).map_err(|err| Error::new(ErrorKind::InvalidData, err))
193+
Config::from_toml(&toml, file_path.parent().unwrap())
194+
.map_err(|err| Error::new(ErrorKind::InvalidData, err))
194195
}
195196

196197
/// Resolves the config for input in `dir`.
@@ -252,7 +253,7 @@ impl Config {
252253
}
253254
}
254255

255-
pub(crate) fn from_toml(toml: &str) -> Result<Config, String> {
256+
pub(crate) fn from_toml(toml: &str, dir: &Path) -> Result<Config, String> {
256257
let parsed: ::toml::Value = toml
257258
.parse()
258259
.map_err(|e| format!("Could not parse TOML: {}", e))?;
@@ -271,7 +272,7 @@ impl Config {
271272
if !err.is_empty() {
272273
eprint!("{}", err);
273274
}
274-
Ok(Config::default().fill_from_parsed_config(parsed_config))
275+
Ok(Config::default().fill_from_parsed_config(parsed_config, dir))
275276
}
276277
Err(e) => {
277278
err.push_str("Error: Decoding config file failed:\n");
@@ -425,7 +426,7 @@ mod test {
425426

426427
#[test]
427428
fn test_was_set() {
428-
let config = Config::from_toml("hard_tabs = true").unwrap();
429+
let config = Config::from_toml("hard_tabs = true", Path::new("")).unwrap();
429430

430431
assert_eq!(config.was_set().hard_tabs(), true);
431432
assert_eq!(config.was_set().verbose(), false);

Diff for: src/ignore_path.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use ignore::{self, gitignore};
21
use std::path::PathBuf;
32

3+
use ignore::{self, gitignore};
4+
45
use crate::config::{FileName, IgnoreList};
56

67
pub struct IgnorePathSet {
@@ -33,16 +34,19 @@ impl IgnorePathSet {
3334

3435
#[cfg(test)]
3536
mod test {
37+
use std::path::{Path, PathBuf};
38+
3639
use crate::config::{Config, FileName};
3740
use crate::ignore_path::IgnorePathSet;
38-
use std::path::PathBuf;
3941

4042
#[test]
4143
fn test_ignore_path_set() {
4244
match option_env!("CFG_RELEASE_CHANNEL") {
4345
// this test requires nightly
4446
None | Some("nightly") => {
45-
let config = Config::from_toml(r#"ignore = ["foo.rs", "bar_dir/*"]"#).unwrap();
47+
let config =
48+
Config::from_toml(r#"ignore = ["foo.rs", "bar_dir/*"]"#, Path::new(""))
49+
.unwrap();
4650
let ignore_path_set = IgnorePathSet::from_ignore_list(&config.ignore()).unwrap();
4751

4852
assert!(ignore_path_set.is_match(&FileName::Real(PathBuf::from("src/foo.rs"))));

Diff for: src/test/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ fn get_config(config_file: Option<&Path>) -> Config {
564564
.read_to_string(&mut def_config)
565565
.expect("Couldn't read config");
566566

567-
Config::from_toml(&def_config).expect("invalid TOML")
567+
Config::from_toml(&def_config, Path::new("tests/config/")).expect("invalid TOML")
568568
}
569569

570570
// Reads significant comments of the form: `// rustfmt-key: value` into a hash map.

0 commit comments

Comments
 (0)