harn-cli 0.10.29

CLI for the Harn programming language — run, test, REPL, format, and lint
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Structured `--json` report for `harn lint`.
//!
//! Lint mirrors the per-file diagnostic shape already used by
//! `harn check --json` so agent consumers can dispatch on a single
//! `CheckDiagnostic` layout regardless of whether they invoked
//! `check` or `lint`.
//!
//! See `docs/src/cli-json-contract.md` for the envelope contract and
//! `crates/harn-cli/src/json_envelope.rs` for the `JsonEnvelope`
//! wrapper.

use std::collections::HashSet;
use std::path::{Path, PathBuf};

use harn_lint::LintSeverity;
use harn_parser::analysis::AnalysisDatabase;
use serde::Serialize;

use crate::json_envelope::{JsonEnvelope, JsonError};
use crate::package::CheckConfig;

use super::analysis::{analyze_file, FileAnalysisError};
use super::check_cmd::{
    check_diagnostic_from_analysis_error, check_span, CheckDiagnostic, CheckFileStatus,
};
use super::outcome::CommandOutcome;

pub(crate) const LINT_SCHEMA_VERSION: u32 = 1;

#[derive(Debug)]
pub(crate) struct LintJsonCommandOutcome {
    pub envelope: JsonEnvelope<LintReport>,
    pub exit_code: i32,
}

#[derive(Debug, Clone, Copy, Default)]
pub(crate) struct LintJsonOptions {
    pub strict: bool,
    pub require_file_header: bool,
    pub require_public_api_types: bool,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct LintReport {
    pub files: Vec<LintFileReport>,
    pub summary: LintSummary,
}

#[derive(Debug, Clone, Serialize)]
pub(crate) struct LintFileReport {
    pub path: String,
    pub status: CheckFileStatus,
    pub diagnostics: Vec<CheckDiagnostic>,
    /// Number of autofix edits that *would* apply if `--fix` were set
    /// (or that did apply, when `--fix` was set). Mirrors the same
    /// field on the human-readable output.
    pub fixable: usize,
    /// Number of edits actually applied by `--fix`. Always zero when
    /// `--fix` is not set.
    pub fixed: usize,
}

impl LintFileReport {
    pub(crate) fn outcome(&self) -> CommandOutcome {
        CommandOutcome {
            has_error: matches!(self.status, CheckFileStatus::Error),
            has_warning: matches!(self.status, CheckFileStatus::Warning),
            findings: self.diagnostics.len(),
            fixable: self.fixable,
        }
    }
}

#[derive(Debug, Clone, Default, Serialize)]
pub(crate) struct LintSummary {
    pub ok: usize,
    pub warnings: usize,
    pub errors: usize,
    pub diagnostics: usize,
    pub fixable: usize,
    pub fixed: usize,
}

impl LintReport {
    pub(crate) fn from_files(files: Vec<LintFileReport>) -> Self {
        let mut summary = LintSummary::default();
        for file in &files {
            match file.status {
                CheckFileStatus::Ok => summary.ok += 1,
                CheckFileStatus::Warning => summary.warnings += 1,
                CheckFileStatus::Error => summary.errors += 1,
            }
            summary.diagnostics += file.diagnostics.len();
            summary.fixable += file.fixable;
            summary.fixed += file.fixed;
        }
        Self { files, summary }
    }
}

/// Execute the structured lint command without rendering or terminating the
/// process. JSON mode is report-only: it never applies `--fix` edits.
pub(crate) async fn run_lint_json(
    files: &[PathBuf],
    options: LintJsonOptions,
) -> LintJsonCommandOutcome {
    let mut analysis = AnalysisDatabase::new();
    let module_graph = super::build_module_graph_and_seed_analysis(files, &mut analysis);
    let cross_file_imports = super::collect_cross_file_imports(&module_graph);
    let script_rule_diags = super::run_project_script_rules(files).await;
    let mut should_fail = false;
    let mut reports = Vec::with_capacity(files.len());

    for file in files {
        let mut config = crate::package::load_check_config(Some(file));
        let mut lint_config = super::load_harn_lint_config(file);
        lint_config.require_file_header |= options.require_file_header;
        lint_config.require_public_api_types |= options.require_public_api_types;
        super::apply_loaded_harn_lint_config(&lint_config, &mut config);
        let script_diagnostics = script_rule_diags
            .get(file)
            .map(Vec::as_slice)
            .unwrap_or(&[]);
        let report = lint_file_report(
            &mut analysis,
            file,
            &config,
            &cross_file_imports,
            &module_graph,
            &lint_config,
            script_diagnostics,
        );
        should_fail |= report
            .outcome()
            .should_fail(config.strict || options.strict);
        reports.push(report);
    }

    let report = LintReport::from_files(reports);
    let envelope = if should_fail {
        JsonEnvelope {
            schema_version: LINT_SCHEMA_VERSION,
            ok: false,
            data: Some(report),
            error: Some(JsonError {
                code: "lint_failed".to_string(),
                message: "one or more files failed `harn lint`".to_string(),
                details: serde_json::Value::Null,
            }),
            warnings: Vec::new(),
        }
    } else {
        JsonEnvelope::ok(LINT_SCHEMA_VERSION, report)
    };

    LintJsonCommandOutcome {
        envelope,
        exit_code: i32::from(should_fail),
    }
}

/// Lint one `.harn` file and return a structured report. Mirrors
/// [`super::lint::lint_file_inner`] but suppresses human-readable
/// stderr rendering and captures every diagnostic into a serializable
/// shape.
pub(crate) fn lint_file_report(
    analysis: &mut AnalysisDatabase,
    path: &Path,
    config: &CheckConfig,
    externally_imported_names: &HashSet<String>,
    module_graph: &harn_modules::ModuleGraph,
    lint_config: &super::config::HarnLintConfig,
    script_rule_diagnostics: &[harn_lint::LintDiagnostic],
) -> LintFileReport {
    let path_str = path.to_string_lossy().into_owned();
    let output = match analyze_file(analysis, path, config, module_graph) {
        Ok(output) => output,
        Err(error) => return file_analysis_error_report(path_str, error),
    };
    let source = output.source;
    let program = output.program;

    let engine_rules = super::lint::project_engine_rule_sources(path);
    let native_rule_paths = super::lint::project_native_rule_paths(path);
    let options = harn_lint::LintOptions {
        file_path: Some(path),
        require_file_header: lint_config.require_file_header,
        require_docstrings: lint_config.require_docstrings,
        require_public_api_types: lint_config.require_public_api_types,
        complexity_threshold: lint_config.complexity_threshold,
        persona_step_allowlist: &lint_config.persona_step_allowlist,
        require_stdlib_metadata: harn_lint::path_is_stdlib_source(path),
        engine_rules: &engine_rules,
        native_rule_paths: &native_rule_paths,
        severity_overrides: lint_config.severity_overrides.clone(),
    };
    let lint_diagnostics = harn_lint::lint_with_module_graph(
        &program,
        &config.disable_rules,
        Some(&source),
        externally_imported_names,
        module_graph,
        path,
        &options,
    );
    let type_lint_diagnostics = harn_lint::lint_diagnostics_from_type_diagnostics(
        &output.diagnostics,
        &config.disable_rules,
    );

    let mut has_error = false;
    let mut has_warning = false;
    let mut fixable = 0usize;
    let mut diagnostics: Vec<CheckDiagnostic> = Vec::new();

    for diag in &lint_diagnostics {
        match diag.severity {
            LintSeverity::Error => has_error = true,
            LintSeverity::Warning => has_warning = true,
            LintSeverity::Info => {}
        }
        if diag.fix.is_some() {
            fixable += 1;
        }
        diagnostics.push(CheckDiagnostic {
            source: "lint",
            severity: lint_severity_label(diag.severity),
            code: Some(diag.code.to_string()),
            message: diag.message.clone(),
            span: Some(check_span(diag.span)),
            help: diag.suggestion.clone(),
        });
    }

    for diag in &type_lint_diagnostics {
        match diag.severity {
            LintSeverity::Error => has_error = true,
            LintSeverity::Warning => has_warning = true,
            LintSeverity::Info => {}
        }
        if diag.fix.is_some() {
            fixable += 1;
        }
        diagnostics.push(CheckDiagnostic {
            source: "lint",
            severity: lint_severity_label(diag.severity),
            code: Some(diag.code.to_string()),
            message: diag.message.clone(),
            span: Some(check_span(diag.span)),
            help: diag.suggestion.clone(),
        });
    }

    // `.harn`-authored custom lint rules (#2850), pre-computed in the async
    // command handler (they need the VM) and merged into the report so they
    // appear and affect status exactly like built-in rules.
    for diag in script_rule_diagnostics.iter().filter(|d| {
        !config
            .disable_rules
            .iter()
            .any(|r| r.as_str() == d.rule.as_ref())
    }) {
        match diag.severity {
            LintSeverity::Error => has_error = true,
            LintSeverity::Warning => has_warning = true,
            LintSeverity::Info => {}
        }
        diagnostics.push(CheckDiagnostic {
            source: "lint",
            severity: lint_severity_label(diag.severity),
            code: Some(diag.code.to_string()),
            message: diag.message.clone(),
            span: Some(check_span(diag.span)),
            help: diag.suggestion.clone(),
        });
    }

    let status = if has_error {
        CheckFileStatus::Error
    } else if has_warning {
        CheckFileStatus::Warning
    } else {
        CheckFileStatus::Ok
    };

    LintFileReport {
        path: path_str,
        status,
        diagnostics,
        fixable,
        fixed: 0,
    }
}

fn lint_severity_label(severity: LintSeverity) -> &'static str {
    match severity {
        LintSeverity::Info => "info",
        LintSeverity::Error => "error",
        LintSeverity::Warning => "warning",
    }
}

fn file_analysis_error_report(path: String, error: FileAnalysisError) -> LintFileReport {
    let diagnostic = match error {
        FileAnalysisError::Read(error) => CheckDiagnostic {
            source: "io",
            severity: "error",
            code: None,
            message: format!("Error reading {path}: {error}"),
            span: None,
            help: None,
        },
        FileAnalysisError::Analysis(error) => check_diagnostic_from_analysis_error(error),
    };
    LintFileReport {
        path,
        status: CheckFileStatus::Error,
        diagnostics: vec![diagnostic],
        fixable: 0,
        fixed: 0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn write_untyped_api(root: &Path) -> PathBuf {
        let path = root.join("api.harn");
        std::fs::write(
            &path,
            "pub fn run(value) { return value }\npub pipeline deploy(task) { return task }\n",
        )
        .expect("write API fixture");
        path
    }

    fn public_api_diagnostics(outcome: &LintJsonCommandOutcome) -> Vec<&CheckDiagnostic> {
        outcome.envelope.data.as_ref().expect("lint report").files[0]
            .diagnostics
            .iter()
            .filter(|diagnostic| diagnostic.code.as_deref() == Some("HARN-LNT-067"))
            .collect()
    }

    #[tokio::test(flavor = "current_thread")]
    async fn public_api_type_command_override_emits_structured_diagnostics() {
        let temp = tempfile::tempdir().expect("tempdir");
        let file = write_untyped_api(temp.path());

        let outcome = run_lint_json(
            &[file],
            LintJsonOptions {
                require_public_api_types: true,
                ..LintJsonOptions::default()
            },
        )
        .await;

        assert_eq!(outcome.exit_code, 0, "warnings remain advisory");
        assert!(outcome.envelope.ok);
        assert!(outcome.envelope.error.is_none());
        let diagnostics = public_api_diagnostics(&outcome);
        assert_eq!(diagnostics.len(), 4, "envelope: {:#?}", outcome.envelope);
        assert!(diagnostics
            .iter()
            .all(|diagnostic| diagnostic.source == "lint"));
        assert!(diagnostics
            .iter()
            .all(|diagnostic| diagnostic.span.is_some()));
        serde_json::to_value(&outcome.envelope).expect("lint envelope serializes");
    }

    #[tokio::test(flavor = "current_thread")]
    async fn public_api_type_project_policy_and_severity_fail_without_override() {
        let temp = tempfile::tempdir().expect("tempdir");
        let file = write_untyped_api(temp.path());
        std::fs::write(
            temp.path().join("harn.toml"),
            r#"
[lint]
require-public-api-types = true

[lint.severity]
missing-public-api-type = "error"
"#,
        )
        .expect("write project policy");

        let outcome = run_lint_json(&[file], LintJsonOptions::default()).await;

        assert_eq!(outcome.exit_code, 1);
        assert!(!outcome.envelope.ok);
        assert_eq!(
            outcome
                .envelope
                .error
                .as_ref()
                .map(|error| error.code.as_str()),
            Some("lint_failed")
        );
        let diagnostics = public_api_diagnostics(&outcome);
        assert_eq!(diagnostics.len(), 4, "envelope: {:#?}", outcome.envelope);
        assert!(diagnostics
            .iter()
            .all(|diagnostic| diagnostic.severity == "error"));
        serde_json::to_value(&outcome.envelope).expect("lint envelope serializes");
    }
}