Skip to content

Commit daef88a

Browse files
committed
license should be from relative dir
1 parent 8cbee56 commit daef88a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/config/config_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ macro_rules! create_config {
390390
if self.was_set().license_template_path() {
391391
let lt_path = self.license_template_path();
392392
if lt_path.len() > 0 {
393-
match license::load_and_compile_template(&lt_path) {
393+
match license::load_and_compile_template(self.get_ignore(), &lt_path) {
394394
Ok(re) => self.license_template = Some(re),
395395
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
396396
lt_path, msg),

src/config/license.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,16 @@ impl TemplateParser {
211211
}
212212
}
213213

214-
pub(crate) fn load_and_compile_template(path: &str) -> Result<Regex, LicenseError> {
214+
pub(crate) fn load_and_compile_template(base_dir: &str, path: &str) -> Result<Regex, LicenseError> {
215+
let mut path = Path(path);
216+
if path.is_relative() {
217+
// The tempate path needs to be resolved relative to the .rustfmt config file
218+
// rather than whatever dir cargo fmt was run from.
219+
let current = std::env::current_dir().expect("pwd");
220+
std::env::set_current_dir(base_dir);
221+
path = path.canonicalize();
222+
std::env::set_current_dir(current).expect("change pwd");
223+
}
215224
let mut lt_file = File::open(&path)?;
216225
let mut lt_str = String::new();
217226
lt_file.read_to_string(&mut lt_str)?;

0 commit comments

Comments
 (0)