croft-software 0.2.82

VSCode-style TUI written in Rust
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Build problem matchers (#119): turn a finished command's output into
//! PROBLEMS entries — VS Code's problem-matcher role, run croft-natively at
//! the `FinishedCommand` boundary (the whole 133;C→133;D output block at
//! once, so multi-line formats need no streaming state machine).
//!
//! Built-in matchers only, first match per line wins:
//! - rustc/cargo: `error[E0308]: msg` + `  --> src/x.rs:12:5`
//! - tsc: `src/x.ts(12,5): error TS2322: msg` and `src/x.ts:12:5 - error TS2322: msg`
//! - gcc/clang/generic: `file:line:col: error|warning: msg`
//! - python: a traceback's deepest `File "x", line N` frame, message from
//!   the final `SomethingError: …` line
//! - eslint (stylish): the indented `line:col  severity  msg  rule` rows
//!   under their file-path header line
//! - mypy, javac: `file:line: error|warning|note: msg`, with no column (#1594)
//! - go build / go vet: `file.go:line:col: msg`, with no severity word
//! - flake8, pylint, ruff (concise): `file:line:col: CODE msg`
//! - ruff (default): `CODE msg` + ` --> file:line:col`
//! - eslint (compact): `file: line N, col N, Error - msg (rule)`
//! - MSBuild / msCompile: `file(line,col): error CODE: msg`
//!
//! The go and `CODE msg` rows are loose enough to match other text, so the
//! caller keeps them only for a file that exists ([`BuildDiag::needs_file`]).

use std::sync::LazyLock;

use regex::Regex;

use crate::lsp::manager::DiagnosticSeverity;

/// One matched problem, file still as the tool printed it (resolution
/// against the command's cwd is the caller's job).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BuildDiag {
    pub file: String,
    /// 0-based, like the LSP diagnostics PROBLEMS already carries.
    pub line: u32,
    pub col: u32,
    pub severity: DiagnosticSeverity,
    pub message: String,
    /// Which matcher produced it: a built-in tag (`rustc`, `tsc`, …) or a
    /// user matcher's name (#252).
    pub source: String,
    /// Kept only when its file exists (#1594): set by the loose built-in go
    /// and lint rows (`file.go:1:2: text`, `file:1:2: X1 text`), shapes
    /// other output can take where every other built-in row needs a
    /// severity word. A flag rather than a check on `source`, which a user
    /// matcher may also name `go` or `lint`.
    pub needs_file: bool,
}

fn severity(word: &str) -> DiagnosticSeverity {
    match word.to_ascii_lowercase().as_str() {
        "error" | "fatal error" => DiagnosticSeverity::Error,
        "warning" => DiagnosticSeverity::Warning,
        _ => DiagnosticSeverity::Information,
    }
}

static RUSTC_HEAD: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^(error|warning)(\[[A-Z0-9]+\])?: (.+)$").unwrap());
static RUSTC_ARROW: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^\s*-->\s+(.+?):(\d+):(\d+)\s*$").unwrap());
static TSC_PAREN: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^(.+?)\((\d+),(\d+)\): (error|warning) (TS\d+): (.+)$").unwrap());
static TSC_PRETTY: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^(.+?):(\d+):(\d+) - (error|warning) (TS\d+): (.+)$").unwrap());
static GENERIC: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^(.+?):(\d+):(\d+):\s*(error|warning|fatal error):\s*(.+)$").unwrap()
});
static NO_COLUMN: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^(.+?\.[A-Za-z0-9]+):(\d+): (error|warning|note): (.+)$").unwrap()
});
static GO: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^(\S+\.go):(\d+):(\d+): (.+)$").unwrap());
static LINT: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^(\S+?):(\d+):(\d+): ([A-Z]+\d+):? (?:\[\*\] )?(.+)$").unwrap());
static RUFF_HEAD: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^([A-Z]+\d+) (?:\[\*\] )?(.+)$").unwrap());
static ESLINT_COMPACT: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^(.+?): line (\d+), col (\d+), (Error|Warning) - (.+)$").unwrap()
});
static MS_COMPILE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^\s*(.+?)\((\d+)(?:,(\d+))?\)\s*: (error|warning) ([A-Za-z]+\d+)\s*: (.+)$")
        .unwrap()
});
static PY_FRAME: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r#"^\s*File "(.+?)", line (\d+)"#).unwrap());
static PY_RAISE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^[A-Za-z_][\w.]*(Error|Exception|Warning)(: .*)?$").unwrap());
static ESLINT_FILE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^\S+\.(m?[jt]sx?|vue|svelte)$").unwrap());
static ESLINT_ROW: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^\s+(\d+):(\d+)\s+(error|warning)\s+(.+?)\s\s+[\w@/-]+$").unwrap()
});

/// Scan a finished command's output. `line`/`col` convert from the tools'
/// 1-based coordinates to the 0-based ones PROBLEMS uses (clamped at 0).
pub fn scan(output: &str) -> Vec<BuildDiag> {
    let mut out = Vec::new();
    // rustc: severity+message on one line, the location arrow later.
    let mut rustc_pending: Option<(DiagnosticSeverity, String)> = None;
    // python: the deepest frame of the traceback in progress.
    let mut py_frame: Option<(String, u32)> = None;
    // eslint: the file-path header the indented rows belong to.
    let mut eslint_file: Option<String> = None;
    // ruff: a `CODE msg` line, which counts only when the very next line
    // is its ` --> file:line:col` arrow.
    let mut ruff_pending: Option<(usize, String)> = None;

    let zero = |n: &str| n.parse::<u32>().unwrap_or(1).saturating_sub(1);
    for (i, line) in output.lines().enumerate() {
        if let Some(c) = RUSTC_HEAD.captures(line) {
            rustc_pending = Some((severity(&c[1]), c[3].to_string()));
            continue;
        }
        if let Some(c) = RUSTC_ARROW.captures(line) {
            if let Some((at, msg)) = ruff_pending.take()
                && at + 1 == i
            {
                out.push(BuildDiag {
                    file: c[1].to_string(),
                    line: zero(&c[2]),
                    col: zero(&c[3]),
                    severity: DiagnosticSeverity::Warning,
                    message: msg,
                    source: String::from("ruff"),
                    needs_file: false,
                });
                continue;
            }
            if let Some((sev, msg)) = rustc_pending.take() {
                out.push(BuildDiag {
                    file: c[1].to_string(),
                    line: zero(&c[2]),
                    col: zero(&c[3]),
                    severity: sev,
                    message: msg,
                    source: String::from("rustc"),
                    needs_file: false,
                });
            }
            continue;
        }
        if let Some(c) = TSC_PAREN
            .captures(line)
            .or_else(|| TSC_PRETTY.captures(line))
        {
            out.push(BuildDiag {
                file: c[1].to_string(),
                line: zero(&c[2]),
                col: zero(&c[3]),
                severity: severity(&c[4]),
                message: format!("{}: {}", &c[5], &c[6]),
                source: String::from("tsc"),
                needs_file: false,
            });
            continue;
        }
        if let Some(c) = PY_FRAME.captures(line) {
            py_frame = Some((c[1].to_string(), zero(&c[2])));
            continue;
        }
        if PY_RAISE.is_match(line) {
            if let Some((file, lno)) = py_frame.take() {
                out.push(BuildDiag {
                    file,
                    line: lno,
                    col: 0,
                    severity: DiagnosticSeverity::Error,
                    message: line.trim().to_string(),
                    source: String::from("python"),
                    needs_file: false,
                });
            }
            continue;
        }
        if ESLINT_FILE.is_match(line.trim_end()) {
            eslint_file = Some(line.trim().to_string());
            continue;
        }
        if let Some(c) = ESLINT_ROW.captures(line) {
            if let Some(file) = eslint_file.clone() {
                out.push(BuildDiag {
                    file,
                    line: zero(&c[1]),
                    col: zero(&c[2]),
                    severity: severity(&c[3]),
                    message: c[4].trim().to_string(),
                    source: String::from("eslint"),
                    needs_file: false,
                });
            }
            continue;
        }
        if let Some(c) = GENERIC.captures(line) {
            out.push(BuildDiag {
                file: c[1].to_string(),
                line: zero(&c[2]),
                col: zero(&c[3]),
                severity: severity(&c[4]),
                message: c[5].to_string(),
                source: String::from("build"),
                needs_file: false,
            });
            continue;
        }
        if let Some(c) = MS_COMPILE.captures(line) {
            out.push(BuildDiag {
                file: c[1].trim().to_string(),
                line: zero(&c[2]),
                col: c.get(3).map_or(0, |m| zero(m.as_str())),
                severity: severity(&c[4]),
                message: format!("{}: {}", &c[5], &c[6]),
                source: String::from("msCompile"),
                needs_file: false,
            });
            continue;
        }
        if let Some(c) = ESLINT_COMPACT.captures(line) {
            out.push(BuildDiag {
                file: c[1].to_string(),
                line: zero(&c[2]),
                col: zero(&c[3]),
                severity: severity(&c[4]),
                message: c[5].to_string(),
                source: String::from("eslint-compact"),
                needs_file: false,
            });
            continue;
        }
        if let Some(c) = NO_COLUMN.captures(line) {
            out.push(BuildDiag {
                file: c[1].to_string(),
                line: zero(&c[2]),
                col: 0,
                severity: severity(&c[3]),
                message: c[4].to_string(),
                source: String::from("no-column"),
                needs_file: false,
            });
            continue;
        }
        if let Some(c) = LINT.captures(line) {
            out.push(BuildDiag {
                file: c[1].to_string(),
                line: zero(&c[2]),
                col: zero(&c[3]),
                severity: DiagnosticSeverity::Warning,
                message: format!("{} {}", &c[4], &c[5]),
                source: String::from("lint"),
                needs_file: true,
            });
            continue;
        }
        if let Some(c) = GO.captures(line) {
            out.push(BuildDiag {
                file: c[1].to_string(),
                line: zero(&c[2]),
                col: zero(&c[3]),
                severity: DiagnosticSeverity::Error,
                message: c[4].to_string(),
                source: String::from("go"),
                needs_file: true,
            });
            continue;
        }
        if let Some(c) = RUFF_HEAD.captures(line) {
            ruff_pending = Some((i, format!("{} {}", &c[1], &c[2])));
            continue;
        }
        if line.trim().is_empty() {
            // A blank line ends an eslint file section; rustc/python state
            // survives blanks (both formats interleave them).
            eslint_file = None;
        }
    }
    out
}

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

    #[test]
    fn rustc_pairs_the_message_with_its_arrow_line() {
        let out = scan(
            "   Compiling demo v0.1.0\n\
             error[E0308]: mismatched types\n\
             \x20 --> src/main.rs:12:5\n\
             \x20  |\n\
             warning: unused variable: `x`\n\
             \x20 --> src/lib.rs:3:9\n\
             error: could not compile `demo`\n",
        );
        assert_eq!(out.len(), 2);
        assert_eq!(
            (out[0].file.as_str(), out[0].line, out[0].col),
            ("src/main.rs", 11, 4),
            "1-based tool coordinates become the 0-based PROBLEMS ones"
        );
        assert_eq!(out[0].severity, DiagnosticSeverity::Error);
        assert_eq!(out[0].message, "mismatched types");
        assert_eq!(out[1].severity, DiagnosticSeverity::Warning);
        assert_eq!(out[1].source, "rustc");
    }

    #[test]
    fn tsc_matches_both_output_shapes() {
        let out = scan(
            "src/app.ts(4,10): error TS2322: Type 'string' is not assignable.\n\
             src/util.ts:9:1 - error TS1005: ';' expected.\n",
        );
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].file, "src/app.ts");
        assert_eq!((out[0].line, out[0].col), (3, 9));
        assert!(out[1].message.starts_with("TS1005"));
    }

    #[test]
    fn generic_gcc_shape_and_python_traceback_deepest_frame() {
        let out = scan(
            "main.c:7:3: error: expected ';' before 'return'\n\
             Traceback (most recent call last):\n\
             \x20 File \"app.py\", line 30, in <module>\n\
             \x20   run()\n\
             \x20 File \"app.py\", line 12, in run\n\
             \x20   x = 1 / 0\n\
             ZeroDivisionError: division by zero\n",
        );
        assert_eq!(out.len(), 2);
        assert_eq!(out[0].source, "build");
        assert_eq!(out[0].file, "main.c");
        assert_eq!(
            (out[1].file.as_str(), out[1].line),
            ("app.py", 11),
            "the DEEPEST frame is where it raised"
        );
        assert_eq!(out[1].message, "ZeroDivisionError: division by zero");
    }

    #[test]
    fn eslint_stylish_rows_attach_to_their_file_header() {
        let out = scan(
            "src/index.js\n\
             \x20 12:5  error  Unexpected console statement  no-console\n\
             \x20 20:1  warning  Missing semicolon  semi\n\
             \n\
             \x20 3:3  error  Orphan row without a file header  no-undef\n",
        );
        assert_eq!(out.len(), 2, "the blank line ends the file section");
        assert_eq!(out[0].file, "src/index.js");
        assert_eq!(out[0].message, "Unexpected console statement");
        assert_eq!(out[1].severity, DiagnosticSeverity::Warning);
    }

    #[test]
    fn fatal_error_counts_as_an_error() {
        let out = scan("bad.c:1:1: fatal error: 'nope.h' file not found\n");
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].severity, DiagnosticSeverity::Error);
    }

    #[test]
    fn clean_output_yields_nothing() {
        assert!(scan("   Compiling demo\n    Finished dev profile\n").is_empty());
    }

    /// The issue's sample lines (#1594), one per tool.
    const TOOLS: &str = "app.py:2: error: Incompatible types in assignment (expression has type \"str\", variable has type \"int\")  [assignment]\n\
Main.java:1: error: incompatible types: String cannot be converted to int\n\
./main.go:4:6: declared and not used: unused\n\
app.py:1:8: F401 [*] `os` imported but unused\n\
app.py:1:1: F401 'os' imported but unused\n\
csrc/c.c:1:20: error: 'y' undeclared (first use in this function)\n";

    /// #1594: mypy, javac, go, ruff (concise), flake8 and gcc each yield
    /// their one row, at the tool's file, line and column.
    #[test]
    fn mypy_javac_go_ruff_flake8_and_gcc_rows_all_match() {
        let out = scan(TOOLS);
        let rows: Vec<(&str, u32, u32, &str)> = out
            .iter()
            .map(|d| (d.file.as_str(), d.line, d.col, d.source.as_str()))
            .collect();
        assert_eq!(
            rows,
            [
                ("app.py", 1, 0, "no-column"),
                ("Main.java", 0, 0, "no-column"),
                ("./main.go", 3, 5, "go"),
                ("app.py", 0, 7, "lint"),
                ("app.py", 0, 0, "lint"),
                ("csrc/c.c", 0, 19, "build"),
            ]
        );
        assert_eq!(out[0].severity, DiagnosticSeverity::Error);
        assert!(out[0].message.starts_with("Incompatible types"));
        assert_eq!(out[2].severity, DiagnosticSeverity::Error);
        assert_eq!(out[2].message, "declared and not used: unused");
        assert_eq!(out[3].message, "F401 `os` imported but unused");
        assert_eq!(out[3].severity, DiagnosticSeverity::Warning);
    }

    /// #1594: a mypy note is information, a warning a warning, and pylint's
    /// `CODE:` form reads like flake8's.
    #[test]
    fn notes_warnings_and_pylint_codes_keep_their_meaning() {
        let out = scan(
            "app.py:3: note: Revealed type is \"builtins.int\"\n\
             Main.java:5: warning: [removal] Integer(int) has been deprecated\n\
             app.py:1:0: C0114: Missing module docstring (missing-module-docstring)\n",
        );
        assert_eq!(out.len(), 3, "{out:?}");
        assert_eq!(out[0].severity, DiagnosticSeverity::Information);
        assert_eq!(out[1].severity, DiagnosticSeverity::Warning);
        assert_eq!(
            out[2].message,
            "C0114 Missing module docstring (missing-module-docstring)"
        );
    }

    /// #1594: a mypy or javac path under a directory with a space still
    /// matches, whole.
    #[test]
    fn a_no_column_path_may_contain_spaces() {
        let out = scan("/tmp/my project/app.py:2: error: Incompatible types\n");
        assert_eq!(out.len(), 1, "{out:?}");
        assert_eq!(out[0].file, "/tmp/my project/app.py");
        assert_eq!(out[0].line, 1);
    }

    /// #1594: ruff's default output puts the place on the arrow line under
    /// the code and message.
    #[test]
    fn ruff_default_pairs_the_code_with_its_arrow() {
        let out = scan(
            "F401 [*] `os` imported but unused\n\
             \x20--> app.py:1:8\n\
             \x20 |\n\
             1 | import os\n\
             \x20 |        ^^\n\
             \n\
             Found 1 error.\n",
        );
        assert_eq!(out.len(), 1, "{out:?}");
        assert_eq!(
            (out[0].file.as_str(), out[0].line, out[0].col),
            ("app.py", 0, 7)
        );
        assert_eq!(out[0].message, "F401 `os` imported but unused");
        assert_eq!(out[0].source, "ruff");
    }

    /// #1594: eslint's compact format and MSBuild's.
    #[test]
    fn eslint_compact_and_ms_compile_rows_match() {
        let out = scan(
            "/w/web/app.js: line 1, col 5, Error - 'a' is assigned a value but never used. (no-unused-vars)\n\
             Program.cs(12,9): error CS1002: ; expected\n",
        );
        assert_eq!(out.len(), 2, "{out:?}");
        assert_eq!((out[0].line, out[0].col), (0, 4));
        assert_eq!(out[0].severity, DiagnosticSeverity::Error);
        assert_eq!(out[0].source, "eslint-compact");
        assert_eq!(
            (out[1].file.as_str(), out[1].line, out[1].col),
            ("Program.cs", 11, 8)
        );
        assert_eq!(out[1].message, "CS1002: ; expected");
    }

    /// Negative (#1594): `grep -rn` output, a version banner that looks like
    /// a code and a code line with no arrow after it yield nothing; only the
    /// loose go and lint rows need an existing file.
    #[test]
    fn grep_output_and_loose_text_are_not_problems() {
        let out = scan(
            "./app.py:2:x: int = \"a\"\n\
             ./main.go:4:\tvar unused int\n\
             HTTP2 support enabled\n\
             see above\n\
             E501 line too long\n",
        );
        assert!(out.is_empty(), "{out:?}");
    }
}