Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

license should be from relative dir #4967

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ macro_rules! create_config {
// if a license_template_path has been specified, successfully read, parsed and compiled
// into a regex, it will be stored here
pub license_template: Option<Regex>,
// The location from which to resolve relative dirs.
root_dir: Option<PathBuf>,
// For each config item, we store a bool indicating whether it has
// been accessed and the value, and a bool whether the option was
// manually initialised, or taken from the default,
Expand Down Expand Up @@ -166,6 +168,7 @@ macro_rules! create_config {
self.set_license_template();
self.set_ignore(dir);
self.set_merge_imports();
self.set_root_dir(dir);
self
}

Expand Down Expand Up @@ -390,7 +393,7 @@ macro_rules! create_config {
if self.was_set().license_template_path() {
let lt_path = self.license_template_path();
if lt_path.len() > 0 {
match license::load_and_compile_template(&lt_path) {
match license::load_and_compile_template(&self.root_dir, &lt_path) {
Ok(re) => self.license_template = Some(re),
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
lt_path, msg),
Expand All @@ -405,6 +408,10 @@ macro_rules! create_config {
self.ignore.2.add_prefix(dir);
}

fn set_root_dir(&mut self, dir: &Path) {
self.root_dir = Some(dir.to_path_buf());
}

fn set_merge_imports(&mut self) {
if self.was_set().merge_imports() {
eprintln!(
Expand Down Expand Up @@ -438,6 +445,7 @@ macro_rules! create_config {
fn default() -> Config {
Config {
license_template: None,
root_dir: None,
$(
$i: (Cell::new(false), false, $def, $stb),
)+
Expand Down
18 changes: 17 additions & 1 deletion src/config/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,23 @@ impl TemplateParser {
}
}

pub(crate) fn load_and_compile_template(path: &str) -> Result<Regex, LicenseError> {
pub(crate) fn load_and_compile_template(
root_dir: &Option<std::path::PathBuf>,
path: &str,
) -> Result<Regex, LicenseError> {
let mut path = std::path::PathBuf::from(path);
if path.is_relative() {
if let Some(root_dir) = root_dir {
// The tempate path needs to be resolved relative to the .rustfmt config file
// rather than whatever dir cargo fmt was run from.
let current = std::env::current_dir().expect("pwd");
std::env::set_current_dir(root_dir).expect("alter pwd");
if let Ok(abs_path) = path.canonicalize() {
gilescope marked this conversation as resolved.
Show resolved Hide resolved
path = abs_path;
}
std::env::set_current_dir(current).expect("change pwd");
}
}
let mut lt_file = File::open(&path)?;
let mut lt_str = String::new();
lt_file.read_to_string(&mut lt_str)?;
Expand Down