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

fix: cli include args should not override negated includes in config #797

Merged
merged 2 commits into from
Dec 1, 2023
Merged
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
34 changes: 34 additions & 0 deletions crates/dprint/src/commands/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,40 @@ mod test {
assert_eq!(logged_messages, vec!["/sub/file3.txt"]);
}

#[test]
fn providing_includes_to_cli_should_not_override_negated_includes() {
let environment = TestEnvironmentBuilder::with_initialized_remote_wasm_plugin()
.with_default_config(|c| {
c.add_includes("**/*.txt").add_includes("!sub/file4.txt");
})
.write_file("/file.txt", "const t=4;")
.write_file("/file2.txt", "const t=4;")
.write_file("/sub/file3.txt", "const t=4;")
.write_file("/sub/file4.txt", "const t=4;")
.write_file("/sub2/file5.txt", "const t=4;")
.build();
// make sure it works as expected with no args
{
run_test_cli(vec!["output-file-paths"], &environment).unwrap();
let mut logged_messages = environment.take_stdout_messages();
logged_messages.sort();
assert_eq!(logged_messages, vec!["/file.txt", "/file2.txt", "/sub/file3.txt", "/sub2/file5.txt",]);
}
// now provide an includes
{
run_test_cli(vec!["output-file-paths", "./sub/*.*"], &environment).unwrap();
let mut logged_messages = environment.take_stdout_messages();
logged_messages.sort();
assert_eq!(
logged_messages,
vec![
// should not have sub/file4.txt here
"/sub/file3.txt",
]
);
}
}

#[test]
fn should_clear_cache_directory() {
let environment = TestEnvironment::new();
Expand Down
28 changes: 26 additions & 2 deletions crates/dprint/src/configuration/resolve_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::environment::CanonicalizedPathBuf;
use crate::environment::Environment;
use crate::plugins::parse_plugin_source_reference;
use crate::plugins::PluginSourceReference;
use crate::utils::is_negated_glob;
use crate::utils::resolve_url_or_file_path;
use crate::utils::PathSource;
use crate::utils::PluginKind;
Expand Down Expand Up @@ -124,8 +125,13 @@ pub async fn resolve_config_from_path<TEnvironment: Environment>(
}
// =========

let includes = take_array_from_config_map(&mut config_map, "includes")?;
let excludes = take_array_from_config_map(&mut config_map, "excludes")?;
let mut includes = take_array_from_config_map(&mut config_map, "includes")?;
let mut excludes = take_array_from_config_map(&mut config_map, "excludes")?;

// Move the negated includes to the excludes so that when someone provides
// includes on the command line, then it will still ignore these
move_negated_includes_to_excludes(&mut includes, &mut excludes);

let incremental = take_bool_from_config_map(&mut config_map, "incremental")?;
config_map.remove("projectType"); // this was an old config property that's no longer used
let extends = take_extends(&mut config_map)?;
Expand All @@ -143,6 +149,24 @@ pub async fn resolve_config_from_path<TEnvironment: Environment>(
Ok(resolve_extends(resolved_config, extends, base_source, environment.clone()).await?)
}

fn move_negated_includes_to_excludes(includes: &mut Option<Vec<String>>, excludes: &mut Option<Vec<String>>) {
let Some(includes) = includes else {
return;
};
for i in (0..includes.len()).rev() {
if is_negated_glob(&includes[i]) {
let value = includes.remove(i);
if excludes.is_none() {
*excludes = Some(Vec::new());
}
// Make negated includes to have a higher priority than excludes.
// This means when checking if something is not matched, it will
// look at these first.
excludes.as_mut().unwrap().insert(0, value);
}
}
}

fn resolve_extends<TEnvironment: Environment>(
mut resolved_config: ResolvedConfig,
extends: Vec<String>,
Expand Down
Loading