nu_lint/
engine.rs

1use std::{
2    fs,
3    io::{self, BufRead},
4    path::{Path, PathBuf},
5    sync::{
6        Mutex, OnceLock,
7        atomic::{AtomicBool, Ordering},
8    },
9};
10
11use ignore::WalkBuilder;
12use nu_parser::parse;
13use nu_protocol::{
14    ast::Block,
15    engine::{EngineState, StateWorkingSet},
16};
17use rayon::prelude::*;
18
19use crate::{
20    LintError,
21    config::Config,
22    context::LintContext,
23    rules::ALL_RULES,
24    violation::{SourceFile, Violation},
25};
26
27/// Parse Nushell source code into an AST and return both the Block and
28/// `StateWorkingSet`, along with the file's starting offset in the span space.
29fn parse_source<'a>(
30    engine_state: &'a EngineState,
31    source: &[u8],
32) -> (Block, StateWorkingSet<'a>, usize) {
33    let mut working_set = StateWorkingSet::new(engine_state);
34    // Get the offset where this file will start in the virtual span space
35    let file_offset = working_set.next_span_start();
36    // Add the source to the working set's file stack so spans work correctly
37    let _file_id = working_set.add_file("source".to_string(), source);
38    let block = parse(&mut working_set, Some("source"), source, false);
39
40    ((*block).clone(), working_set, file_offset)
41}
42
43/// Check if a file is a Nushell script (by extension or shebang)
44fn is_nushell_file(path: &Path) -> bool {
45    path.extension()
46        .and_then(|s| s.to_str())
47        .is_some_and(|ext| ext == "nu")
48        || fs::File::open(path)
49            .ok()
50            .and_then(|file| {
51                let mut reader = io::BufReader::new(file);
52                let mut first_line = String::new();
53                reader.read_line(&mut first_line).ok()?;
54                first_line.starts_with("#!").then(|| {
55                    first_line
56                        .split_whitespace()
57                        .any(|word| word.ends_with("/nu") || word == "nu")
58                })
59            })
60            .unwrap_or(false)
61}
62
63/// Collect .nu files from a directory, respecting .gitignore files
64#[must_use]
65pub fn collect_nu_files_from_dir(dir: &Path) -> Vec<PathBuf> {
66    WalkBuilder::new(dir)
67        .standard_filters(true)
68        .build()
69        .filter_map(|result| match result {
70            Ok(entry) => {
71                let path = entry.path().to_path_buf();
72                (path.is_file() && is_nushell_file(&path)).then_some(path)
73            }
74            Err(err) => {
75                log::warn!("Error walking directory: {err}");
76                None
77            }
78        })
79        .collect()
80}
81
82/// Collect all Nushell files to lint from given paths
83///
84/// For files: includes them if they are `.nu` files or have a nushell shebang
85/// For directories: recursively collects `.nu` files, respecting `.gitignore`
86#[must_use]
87pub fn collect_nu_files(paths: &[PathBuf]) -> Vec<PathBuf> {
88    paths
89        .iter()
90        .flat_map(|path| {
91            if !path.exists() {
92                log::warn!("Path not found: {}", path.display());
93                return vec![];
94            }
95
96            if path.is_file() {
97                if is_nushell_file(path) {
98                    vec![path.clone()]
99                } else {
100                    vec![]
101                }
102            } else if path.is_dir() {
103                collect_nu_files_from_dir(path)
104            } else {
105                vec![]
106            }
107        })
108        .collect()
109}
110
111pub struct LintEngine {
112    pub(crate) config: Config,
113    engine_state: &'static EngineState,
114}
115
116impl LintEngine {
117    /// Get or initialize the default engine state
118    #[must_use]
119    pub fn default_engine_state() -> &'static EngineState {
120        static ENGINE: OnceLock<EngineState> = OnceLock::new();
121        ENGINE.get_or_init(|| {
122            let mut engine_state = nu_cmd_lang::create_default_context();
123            engine_state = nu_command::add_shell_command_context(engine_state);
124            engine_state = nu_cli::add_cli_context(engine_state);
125
126            // Add print command (exported by nu-cli but not added by add_cli_context)
127            let delta = {
128                let mut working_set = StateWorkingSet::new(&engine_state);
129                working_set.add_decl(Box::new(nu_cli::Print));
130                working_set.render()
131            };
132            engine_state
133                .merge_delta(delta)
134                .expect("Failed to add Print command");
135
136            // nu_std::load_standard_library(&mut engine_state).unwrap();
137            engine_state
138        })
139    }
140
141    #[must_use]
142    pub fn new(config: Config) -> Self {
143        Self {
144            config,
145            engine_state: Self::default_engine_state(),
146        }
147    }
148
149    /// Lint a file at the given path.
150    ///
151    /// # Errors
152    ///
153    /// Returns an error if the file cannot be read.
154    pub fn lint_file(&self, path: &Path) -> Result<Vec<Violation>, LintError> {
155        log::debug!("Linting file: {}", path.display());
156        let source = fs::read_to_string(path).map_err(|source| LintError::Io {
157            path: path.to_path_buf(),
158            source,
159        })?;
160        let mut violations = self.lint_str(&source);
161
162        for violation in &mut violations {
163            violation.file = Some(path.into());
164        }
165
166        violations.sort_by(|a, b| {
167            a.file_span()
168                .start
169                .cmp(&b.file_span().start)
170                .then(a.lint_level.cmp(&b.lint_level))
171        });
172        Ok(violations)
173    }
174
175    /// Lint multiple files, optionally in parallel
176    ///
177    /// Returns a tuple of (violations, `has_errors`) where `has_errors`
178    /// indicates if any files failed to be read/parsed.
179    #[must_use]
180    pub fn lint_files(&self, files: &[PathBuf]) -> (Vec<Violation>, bool) {
181        let violations_mutex = Mutex::new(Vec::new());
182        let has_errors = AtomicBool::new(false);
183
184        let process_file = |path: &PathBuf| match self.lint_file(path) {
185            Ok(violations) => {
186                violations_mutex
187                    .lock()
188                    .expect("Failed to lock violations mutex")
189                    .extend(violations);
190            }
191            Err(e) => {
192                log::error!("Error linting {}: {}", path.display(), e);
193                has_errors.store(true, Ordering::Relaxed);
194            }
195        };
196
197        if self.config.sequential {
198            for path in files {
199                log::debug!("Processing file: {}", path.display());
200                process_file(path);
201            }
202        } else {
203            files.par_iter().for_each(process_file);
204        }
205
206        let violations = violations_mutex
207            .into_inner()
208            .expect("Failed to unwrap violations mutex");
209        (violations, has_errors.load(Ordering::Relaxed))
210    }
211
212    /// Lint content from stdin
213    #[must_use]
214    pub fn lint_stdin(&self, source: &str) -> Vec<Violation> {
215        let mut violations = self.lint_str(source);
216        let source_owned = source.to_string();
217
218        for violation in &mut violations {
219            violation.file = Some(SourceFile::Stdin);
220            violation.source = Some(source_owned.clone().into());
221        }
222
223        violations
224    }
225
226    #[must_use]
227    pub fn lint_str(&self, source: &str) -> Vec<Violation> {
228        let (block, working_set, file_offset) = parse_source(self.engine_state, source.as_bytes());
229
230        let context =
231            LintContext::new(source, &block, self.engine_state, &working_set, file_offset);
232
233        let mut violations = self.collect_violations(&context);
234
235        // Normalize all spans in violations to be file-relative
236        for violation in &mut violations {
237            violation.normalize_spans(file_offset);
238        }
239
240        violations
241    }
242
243    /// Collect violations from all enabled rules
244    fn collect_violations(&self, context: &LintContext) -> Vec<Violation> {
245        ALL_RULES
246            .iter()
247            .filter_map(|rule| {
248                let lint_level = self.config.get_lint_level(rule)?;
249
250                let mut violations = (rule.check)(context);
251                for violation in &mut violations {
252                    violation.set_rule_id(rule.id);
253                    violation.set_lint_level(lint_level);
254                    violation.set_doc_url(rule.doc_url);
255                }
256
257                (!violations.is_empty()).then_some(violations)
258            })
259            .flatten()
260            .collect()
261    }
262}