diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 52049828c588b..e9524802a8f08 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -1,4 +1,4 @@ -use std::{env, io::BufWriter, time::Instant}; +use std::{env, io::BufWriter, path::PathBuf, time::Instant}; use ignore::gitignore::Gitignore; use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler}; @@ -18,13 +18,14 @@ use crate::{ pub struct LintRunner { options: LintCommand, + cwd: PathBuf, } impl Runner for LintRunner { type Options = LintCommand; fn new(options: Self::Options) -> Self { - Self { options } + Self { options, cwd: env::current_dir().expect("Failed to get current working directory") } } fn run(self) -> CliRunResult { @@ -51,6 +52,16 @@ impl Runner for LintRunner { let provided_path_count = paths.len(); let now = Instant::now(); + // append cwd to all paths + paths = paths + .into_iter() + .map(|x| { + let mut path_with_cwd = self.cwd.clone(); + path_with_cwd.push(x); + path_with_cwd + }) + .collect(); + // The ignore crate whitelists explicit paths, but priority // should be given to the ignore file. Many users lint // automatically and pass a list of changed files explicitly. @@ -72,13 +83,7 @@ impl Runner for LintRunner { }); } - if let Ok(cwd) = env::current_dir() { - paths.push(cwd); - } else { - return CliRunResult::InvalidOptions { - message: "Failed to get current working directory.".to_string(), - }; - } + paths.push(self.cwd.clone()); } let filter = match Self::get_filters(filter) { @@ -97,8 +102,6 @@ impl Runner for LintRunner { let number_of_files = paths.len(); - let cwd = std::env::current_dir().unwrap(); - let mut oxlintrc = if let Some(config_path) = basic_options.config.as_ref() { match Oxlintrc::from_file(config_path) { Ok(config) => config, @@ -129,8 +132,9 @@ impl Runner for LintRunner { }; } - let mut options = - LintServiceOptions::new(cwd, paths).with_cross_module(builder.plugins().has_import()); + let mut options = LintServiceOptions::new(self.cwd, paths) + .with_cross_module(builder.plugins().has_import()); + let linter = builder.build(); let tsconfig = basic_options.tsconfig; @@ -175,6 +179,12 @@ impl Runner for LintRunner { } impl LintRunner { + #[must_use] + pub fn with_cwd(mut self, cwd: PathBuf) -> Self { + self.cwd = cwd; + self + } + fn get_diagnostic_service( warning_options: &WarningOptions, output_options: &OutputOptions, @@ -235,6 +245,8 @@ impl LintRunner { #[cfg(all(test, not(target_os = "windows")))] mod test { + use std::env; + use super::LintRunner; use crate::cli::{lint_command, CliRunResult, LintResult, Runner}; @@ -248,6 +260,20 @@ mod test { } } + fn test_with_cwd(cwd: &str, args: &[&str]) -> LintResult { + let mut new_args = vec!["--silent"]; + new_args.extend(args); + let options = lint_command().run_inner(new_args.as_slice()).unwrap(); + + let mut current_cwd = env::current_dir().unwrap(); + current_cwd.push(cwd); + + match LintRunner::new(options).with_cwd(current_cwd).run() { + CliRunResult::LintResult(lint_result) => lint_result, + other => panic!("{other:?}"), + } + } + fn test_invalid_options(args: &[&str]) -> String { let mut new_args = vec!["--quiet"]; new_args.extend(args); @@ -279,6 +305,16 @@ mod test { assert_eq!(result.number_of_errors, 0); } + #[test] + fn cwd() { + let args = &["debugger.js"]; + let result = test_with_cwd("fixtures/linter", args); + assert!(result.number_of_rules > 0); + assert_eq!(result.number_of_files, 1); + assert_eq!(result.number_of_warnings, 1); + assert_eq!(result.number_of_errors, 0); + } + #[test] fn file() { let args = &["fixtures/linter/debugger.js"]; diff --git a/tasks/website/src/linter/snapshots/cli.snap b/tasks/website/src/linter/snapshots/cli.snap index 3bddf2116085d..d4184ce620767 100644 --- a/tasks/website/src/linter/snapshots/cli.snap +++ b/tasks/website/src/linter/snapshots/cli.snap @@ -1,6 +1,7 @@ --- source: tasks/website/src/linter/cli.rs expression: snapshot +snapshot_kind: text --- ## Usage **`oxlint`** \[**`-c`**=_`<./oxlintrc.json>`_\] \[_`PATH`_\]... diff --git a/tasks/website/src/linter/snapshots/cli_terminal.snap b/tasks/website/src/linter/snapshots/cli_terminal.snap index b490bd1f009aa..3d55cd84e0835 100644 --- a/tasks/website/src/linter/snapshots/cli_terminal.snap +++ b/tasks/website/src/linter/snapshots/cli_terminal.snap @@ -1,6 +1,7 @@ --- source: tasks/website/src/linter/cli.rs expression: snapshot +snapshot_kind: text --- Usage: [-c=<./oxlintrc.json>] [PATH]...