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): respect exclude option for deno check command #21779

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
27 changes: 27 additions & 0 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,23 @@ fn resolve_fmt_options(
options
}

#[derive(Clone, Debug, Default)]
pub struct CheckOptions {
pub exclude: Vec<PathBuf>,
}

impl CheckOptions {
pub fn resolve(
maybe_files_config: Option<FilesConfig>,
) -> Result<Self, AnyError> {
Ok(Self {
exclude: expand_globs(
maybe_files_config.map(|c| c.exclude).unwrap_or_default(),
)?,
})
}
}

#[derive(Clone)]
pub struct TestOptions {
pub files: FilesConfig,
Expand Down Expand Up @@ -1182,6 +1199,16 @@ impl CliOptions {
LintOptions::resolve(maybe_lint_config, Some(lint_flags))
}

pub fn resolve_check_options(&self) -> Result<CheckOptions, AnyError> {
let maybe_files_config = if let Some(config_file) = &self.maybe_config_file
{
config_file.to_files_config()?
} else {
None
};
CheckOptions::resolve(maybe_files_config)
}

pub fn resolve_test_options(
&self,
test_flags: TestFlags,
Expand Down
29 changes: 25 additions & 4 deletions cli/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,7 @@ impl ModuleLoadPreparer {
) -> Result<(), AnyError> {
let lib = self.options.ts_type_lib_window();

let specifiers = files
.iter()
.map(|file| resolve_url_or_path(file, self.options.initial_cwd()))
.collect::<Result<Vec<_>, _>>()?;
let specifiers = self.collect_specifiers(files);
self
.prepare_module_load(
specifiers,
Expand All @@ -237,6 +234,30 @@ impl ModuleLoadPreparer {
)
.await
}

fn collect_specifiers(&self, files: &[String]) -> Vec<ModuleSpecifier> {
let excludes = match self.options.resolve_check_options() {
Ok(o) => o.exclude,
Err(_) => vec![],
};
files
.iter()
.filter_map(|file| {
let file_url =
resolve_url_or_path(file, self.options.initial_cwd()).ok()?;
if file_url.scheme() != "file" {
return Some(file_url);
}
// ignore local files that match any of files listed in `exclude` option
let file_path = file_url.to_file_path().ok()?;
if excludes.iter().any(|e| file_path.starts_with(e)) {
None
} else {
Some(file_url)
}
})
.collect::<Vec<_>>()
}
}

pub struct ModuleCodeSource {
Expand Down
28 changes: 28 additions & 0 deletions cli/tests/integration/check_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ itest!(check_deno_unstable_from_config {
output_str: Some(""),
});

itest!(check_with_exclude_option_by_dir {
args:
"check --quiet --config check/exclude_option/deno.exclude_dir.json check/exclude_option/ignored/index.ts",
output_str: Some(""),
exit_code: 0,
});

itest!(check_with_exclude_option_by_glob {
args:
"check --quiet --config check/exclude_option/deno.exclude_glob.json check/exclude_option/ignored/index.ts",
output_str: Some(""),
exit_code: 0,
});

itest!(check_without_exclude_option {
args:
"check --quiet --config check/exclude_option/deno.json check/exclude_option/ignored/index.ts",
output: "check/exclude_option/exclude_option.ts.error.out",
exit_code: 1,
});

itest!(check_imported_files_listed_in_exclude_option {
args:
"check --quiet --config check/exclude_option/deno.exclude_dir.json check/exclude_option/index.ts",
output: "check/exclude_option/exclude_option.ts.error.out",
exit_code: 1,
});

#[test]
fn cache_switching_config_then_no_config() {
let context = TestContext::default();
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/testdata/check/exclude_option/deno.exclude_dir.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"exclude": [
"ignored"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"exclude": [
"ignored/**/*"
]
}
3 changes: 3 additions & 0 deletions cli/tests/testdata/check/exclude_option/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"exclude": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: TS2304 [ERROR]: Cannot find name 'nothing'.
export { nothing }
~~~~~~~
at [WILDCARD]
1 change: 1 addition & 0 deletions cli/tests/testdata/check/exclude_option/ignored/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { nothing }
5 changes: 5 additions & 0 deletions cli/tests/testdata/check/exclude_option/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { nothing } from "./ignored/index.ts"

const foo = 1

export { foo, nothing }
Loading