Skip to main content

logcompact_builtins/
problem_matcher.rs

1use std::collections::BTreeMap;
2use std::error::Error;
3use std::fmt::{Display, Formatter};
4
5use logcompact_core::{
6    Diagnostic, DiagnosticClass, Emitter, EvidenceQuality, Location, LogLine, Parser, Provenance,
7    ScopeBoundary, Severity, Stream,
8};
9use regex::{Captures, Regex, RegexBuilder};
10use serde::Deserialize;
11use serde_json::Value;
12
13/// Hard limits for untrusted problem-matcher definitions and partial matches.
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15pub struct ProblemMatcherLimits {
16    pub max_document_bytes: usize,
17    pub max_matchers: usize,
18    pub max_patterns_per_matcher: usize,
19    pub max_total_regex_bytes: usize,
20    pub max_compiled_regex_bytes: usize,
21    pub max_active_states: usize,
22    pub max_state_bytes: usize,
23}
24
25impl Default for ProblemMatcherLimits {
26    fn default() -> Self {
27        Self {
28            max_document_bytes: 1024 * 1024,
29            max_matchers: 64,
30            max_patterns_per_matcher: 8,
31            max_total_regex_bytes: 64 * 1024,
32            max_compiled_regex_bytes: 2 * 1024 * 1024,
33            max_active_states: 512,
34            max_state_bytes: 256 * 1024,
35        }
36    }
37}
38
39/// Invalid or unsupported problem-matcher configuration.
40#[derive(Debug)]
41pub enum ProblemMatcherError {
42    Json(serde_json::Error),
43    Invalid(String),
44}
45
46impl Display for ProblemMatcherError {
47    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
48        match self {
49            Self::Json(error) => write!(formatter, "invalid problem matcher JSON: {error}"),
50            Self::Invalid(message) => formatter.write_str(message),
51        }
52    }
53}
54
55impl Error for ProblemMatcherError {
56    fn source(&self) -> Option<&(dyn Error + 'static)> {
57        match self {
58            Self::Json(error) => Some(error),
59            Self::Invalid(_) => None,
60        }
61    }
62}
63
64impl From<serde_json::Error> for ProblemMatcherError {
65    fn from(value: serde_json::Error) -> Self {
66        Self::Json(value)
67    }
68}
69
70/// Ordered registry compiled before parsing begins. A later owner replaces an
71/// earlier owner, matching GitHub Actions registration behavior.
72#[derive(Clone)]
73pub struct ProblemMatcherRegistry {
74    limits: ProblemMatcherLimits,
75    matchers: Vec<CompiledMatcher>,
76}
77
78impl ProblemMatcherRegistry {
79    #[must_use]
80    pub fn new(limits: ProblemMatcherLimits) -> Self {
81        Self {
82            limits,
83            matchers: Vec::new(),
84        }
85    }
86
87    #[must_use]
88    pub fn is_empty(&self) -> bool {
89        self.matchers.is_empty()
90    }
91
92    #[must_use]
93    pub fn len(&self) -> usize {
94        self.matchers.len()
95    }
96
97    /// Returns the built-in matcher owners replaced by this registry.
98    #[must_use]
99    pub fn builtin_overrides(&self) -> crate::BuiltinMatcherOverrides {
100        crate::BuiltinMatcherOverrides::from_owners(
101            self.matchers.iter().map(|matcher| matcher.owner.as_str()),
102        )
103    }
104
105    /// Compiles one GitHub problem-matcher document, a VS Code inline matcher,
106    /// or an array of inline matchers. The registry is unchanged on failure.
107    pub fn add_json(&mut self, input: &[u8]) -> Result<(), ProblemMatcherError> {
108        if input.len() > self.limits.max_document_bytes {
109            return Err(invalid(format!(
110                "problem matcher document is {} bytes; maximum is {}",
111                input.len(),
112                self.limits.max_document_bytes
113            )));
114        }
115        let value: Value = serde_json::from_slice(input)?;
116        let raw = raw_matchers(value)?;
117        if raw.len() > self.limits.max_matchers {
118            return Err(invalid(format!(
119                "problem matcher document contains {} entries; maximum is {}",
120                raw.len(),
121                self.limits.max_matchers
122            )));
123        }
124        let compiled = raw
125            .into_iter()
126            .enumerate()
127            .map(|(index, matcher)| compile_matcher(matcher, index, self.limits))
128            .collect::<Result<Vec<_>, _>>()?;
129
130        let mut next = self.matchers.clone();
131        for matcher in compiled {
132            next.retain(|existing| existing.owner != matcher.owner);
133            next.push(matcher);
134        }
135        if next.len() > self.limits.max_matchers {
136            return Err(invalid(format!(
137                "problem matcher registry contains {} owners; maximum is {}",
138                next.len(),
139                self.limits.max_matchers
140            )));
141        }
142        let regex_bytes = next
143            .iter()
144            .flat_map(|matcher| &matcher.patterns)
145            .map(|pattern| pattern.source_bytes)
146            .fold(0_usize, usize::saturating_add);
147        if regex_bytes > self.limits.max_total_regex_bytes {
148            return Err(invalid(format!(
149                "problem matcher regex sources total {regex_bytes} bytes; maximum is {}",
150                self.limits.max_total_regex_bytes
151            )));
152        }
153        self.matchers = next;
154        Ok(())
155    }
156
157    #[must_use]
158    pub fn into_parser(self) -> ProblemMatcherParser {
159        ProblemMatcherParser {
160            matchers: self.matchers,
161            limits: self.limits,
162            states: BTreeMap::new(),
163        }
164    }
165}
166
167impl Default for ProblemMatcherRegistry {
168    fn default() -> Self {
169        Self::new(ProblemMatcherLimits::default())
170    }
171}
172
173/// Bounded, deterministic single-line and consecutive multiline matcher.
174pub struct ProblemMatcherParser {
175    matchers: Vec<CompiledMatcher>,
176    limits: ProblemMatcherLimits,
177    states: BTreeMap<(String, Stream, usize), MatchState>,
178}
179
180impl Parser for ProblemMatcherParser {
181    fn id(&self) -> &'static str {
182        "problem-matcher.v1"
183    }
184
185    fn observe(&mut self, line: &LogLine<'_>, emitter: &mut Emitter<'_>) {
186        let limits = self.limits;
187        for matcher_index in 0..self.matchers.len() {
188            observe_matcher(
189                &self.matchers[matcher_index],
190                matcher_index,
191                line,
192                emitter,
193                &mut self.states,
194                limits,
195                "problem-matcher.v1",
196            );
197        }
198    }
199
200    fn boundary(&mut self, boundary: ScopeBoundary<'_>, _emitter: &mut Emitter<'_>) {
201        if let ScopeBoundary::End { scope, .. } = boundary {
202            self.states
203                .retain(|(scope_id, _, _), _| scope_id != &scope.id);
204        }
205    }
206
207    fn finish(&mut self, _emitter: &mut Emitter<'_>) {
208        self.states.clear();
209    }
210}
211
212#[derive(Clone)]
213struct CompiledMatcher {
214    owner: String,
215    source: Option<String>,
216    severity: Severity,
217    file_location: FileLocation,
218    patterns: Vec<CompiledPattern>,
219}
220
221#[derive(Clone)]
222struct CompiledPattern {
223    regex: Regex,
224    source_bytes: usize,
225    kind: Option<LocationKind>,
226    file: Option<usize>,
227    from_path: Option<usize>,
228    location: Option<usize>,
229    line: Option<usize>,
230    column: Option<usize>,
231    end_line: Option<usize>,
232    end_column: Option<usize>,
233    severity: Option<usize>,
234    code: Option<usize>,
235    message: Option<usize>,
236    r#loop: bool,
237}
238
239#[derive(Clone, Copy, Debug, Eq, PartialEq)]
240enum LocationKind {
241    File,
242    Location,
243}
244
245#[derive(Clone)]
246enum FileLocation {
247    Captured,
248    Relative(String),
249}
250
251#[derive(Clone, Default)]
252struct CapturedProblem {
253    file: Option<String>,
254    from_path: Option<String>,
255    location: Option<String>,
256    line: Option<String>,
257    column: Option<String>,
258    end_line: Option<String>,
259    end_column: Option<String>,
260    severity: Option<String>,
261    code: Option<String>,
262    message: Option<String>,
263}
264
265impl CapturedProblem {
266    fn bytes(&self) -> usize {
267        [
268            &self.file,
269            &self.from_path,
270            &self.location,
271            &self.line,
272            &self.column,
273            &self.end_line,
274            &self.end_column,
275            &self.severity,
276            &self.code,
277            &self.message,
278        ]
279        .into_iter()
280        .filter_map(|value| value.as_ref())
281        .map(String::len)
282        .fold(0_usize, usize::saturating_add)
283    }
284}
285
286struct MatchState {
287    next_pattern: usize,
288    data: CapturedProblem,
289    start_line: u64,
290}
291
292fn observe_matcher(
293    matcher: &CompiledMatcher,
294    matcher_index: usize,
295    line: &LogLine<'_>,
296    emitter: &mut Emitter<'_>,
297    states: &mut BTreeMap<(String, Stream, usize), MatchState>,
298    limits: ProblemMatcherLimits,
299    parser_id: &'static str,
300) {
301    let key = (line.scope.id.clone(), line.stream, matcher_index);
302    if let Some(mut state) = states.remove(&key) {
303        let pattern = &matcher.patterns[state.next_pattern];
304        if let Some(captures) = pattern.regex.captures(line.text) {
305            if state.next_pattern == matcher.patterns.len() - 1 {
306                if pattern.r#loop {
307                    let mut finding = state.data.clone();
308                    apply_captures(&mut finding, pattern, &captures);
309                    emit_problem(
310                        matcher,
311                        finding,
312                        line,
313                        state.start_line,
314                        emitter,
315                        limits,
316                        parser_id,
317                    );
318                    states.insert(key, state);
319                } else {
320                    apply_captures(&mut state.data, pattern, &captures);
321                    emit_problem(
322                        matcher,
323                        state.data,
324                        line,
325                        state.start_line,
326                        emitter,
327                        limits,
328                        parser_id,
329                    );
330                }
331            } else {
332                apply_captures(&mut state.data, pattern, &captures);
333                state.next_pattern += 1;
334                if retained_state_bytes(states).saturating_add(state.data.bytes())
335                    <= limits.max_state_bytes
336                {
337                    states.insert(key, state);
338                } else {
339                    emitter.candidate_dropped();
340                }
341            }
342            return;
343        }
344    }
345    start_match(
346        matcher,
347        matcher_index,
348        line,
349        emitter,
350        states,
351        limits,
352        parser_id,
353    );
354}
355
356fn start_match(
357    matcher: &CompiledMatcher,
358    matcher_index: usize,
359    line: &LogLine<'_>,
360    emitter: &mut Emitter<'_>,
361    states: &mut BTreeMap<(String, Stream, usize), MatchState>,
362    limits: ProblemMatcherLimits,
363    parser_id: &'static str,
364) {
365    let pattern = &matcher.patterns[0];
366    let Some(captures) = pattern.regex.captures(line.text) else {
367        return;
368    };
369    let mut data = CapturedProblem::default();
370    apply_captures(&mut data, pattern, &captures);
371    if matcher.patterns.len() == 1 {
372        emit_problem(
373            matcher,
374            data,
375            line,
376            line.stream_line,
377            emitter,
378            limits,
379            parser_id,
380        );
381    } else if retained_state_bytes(states).saturating_add(data.bytes()) > limits.max_state_bytes
382        || states.len() >= limits.max_active_states
383    {
384        emitter.candidate_dropped();
385    } else {
386        states.insert(
387            (line.scope.id.clone(), line.stream, matcher_index),
388            MatchState {
389                next_pattern: 1,
390                data,
391                start_line: line.stream_line,
392            },
393        );
394    }
395}
396
397fn retained_state_bytes(states: &BTreeMap<(String, Stream, usize), MatchState>) -> usize {
398    states
399        .values()
400        .map(|state| state.data.bytes())
401        .fold(0_usize, usize::saturating_add)
402}
403
404fn apply_captures(data: &mut CapturedProblem, pattern: &CompiledPattern, captures: &Captures<'_>) {
405    fill(&mut data.file, pattern.file, captures, true);
406    fill(&mut data.from_path, pattern.from_path, captures, true);
407    fill(&mut data.location, pattern.location, captures, true);
408    fill(&mut data.line, pattern.line, captures, true);
409    fill(&mut data.column, pattern.column, captures, true);
410    fill(&mut data.end_line, pattern.end_line, captures, true);
411    fill(&mut data.end_column, pattern.end_column, captures, true);
412    fill(&mut data.severity, pattern.severity, captures, true);
413    fill(&mut data.code, pattern.code, captures, true);
414    append_message(&mut data.message, pattern.message, captures);
415}
416
417fn fill(target: &mut Option<String>, group: Option<usize>, captures: &Captures<'_>, trim: bool) {
418    if target.is_some() {
419        return;
420    }
421    let Some(value) = group.and_then(|index| captures.get(index)) else {
422        return;
423    };
424    let value = if trim {
425        value.as_str().trim()
426    } else {
427        value.as_str()
428    };
429    *target = Some(value.to_owned());
430}
431
432fn append_message(target: &mut Option<String>, group: Option<usize>, captures: &Captures<'_>) {
433    let Some(value) = group.and_then(|index| captures.get(index)) else {
434        return;
435    };
436    let value = value.as_str().trim();
437    if let Some(existing) = target {
438        existing.push('\n');
439        existing.push_str(value);
440    } else {
441        *target = Some(value.to_owned());
442    }
443}
444
445fn emit_problem(
446    matcher: &CompiledMatcher,
447    data: CapturedProblem,
448    line: &LogLine<'_>,
449    start_line: u64,
450    emitter: &mut Emitter<'_>,
451    limits: ProblemMatcherLimits,
452    parser_id: &'static str,
453) {
454    if data.bytes() > limits.max_state_bytes {
455        emitter.candidate_dropped();
456        return;
457    }
458    let Some(file) = data.file.as_deref().filter(|value| !value.is_empty()) else {
459        return;
460    };
461    let path = matcher
462        .file_location
463        .resolve(data.from_path.as_deref(), file);
464    let location = location(
465        &data,
466        path,
467        matcher.patterns[0].kind.unwrap_or(LocationKind::Location),
468    );
469    let Some(location) = location else {
470        return;
471    };
472    let severity = data
473        .severity
474        .as_deref()
475        .and_then(parse_severity)
476        .unwrap_or(matcher.severity);
477    let Some(message) = data.message.filter(|value| !value.is_empty()) else {
478        return;
479    };
480    let label = matcher.source.as_deref().unwrap_or(&matcher.owner);
481    emitter.diagnostic(Diagnostic {
482        severity,
483        class: DiagnosticClass::Tool,
484        code: data.code.filter(|value| !value.is_empty()),
485        message,
486        location: Some(location),
487        provenance: Some(
488            Provenance::new(stream_name(line.stream))
489                .with_label(label)
490                .with_scope(line.scope)
491                .with_span(line.stream, start_line, line.stream_line)
492                .with_parser(parser_id),
493        ),
494        quality: EvidenceQuality::Located,
495        repetition_count: 1,
496    });
497}
498
499fn location(data: &CapturedProblem, path: String, kind: LocationKind) -> Option<Location> {
500    if kind == LocationKind::File {
501        return Some(Location {
502            path,
503            line: None,
504            column: None,
505            end_line: None,
506            end_column: None,
507        });
508    }
509    let (line, column, end_line, end_column) = if let Some(value) = &data.location {
510        parse_location(value)?
511    } else {
512        let line = parse_coordinate(data.line.as_deref()?)?;
513        let column = data.column.as_deref().and_then(parse_coordinate);
514        let end_line = data.end_line.as_deref().and_then(parse_coordinate);
515        let end_column = data.end_column.as_deref().and_then(parse_coordinate);
516        (line, column, end_line, end_column)
517    };
518    let end_line = end_line.or_else(|| end_column.map(|_| line));
519    Some(Location {
520        path,
521        line: Some(line),
522        column,
523        end_line: end_line.map(|value| value.max(line)),
524        end_column,
525    })
526}
527
528type ParsedLocation = (u32, Option<u32>, Option<u32>, Option<u32>);
529
530fn parse_location(value: &str) -> Option<ParsedLocation> {
531    let parts = value.split(',').map(str::trim).collect::<Vec<_>>();
532    match parts.as_slice() {
533        [line] => Some((parse_coordinate(line)?, None, None, None)),
534        [line, column] => Some((
535            parse_coordinate(line)?,
536            Some(parse_coordinate(column)?),
537            None,
538            None,
539        )),
540        [line, column, end_line, end_column] => Some((
541            parse_coordinate(line)?,
542            Some(parse_coordinate(column)?),
543            Some(parse_coordinate(end_line)?),
544            Some(parse_coordinate(end_column)?),
545        )),
546        _ => None,
547    }
548}
549
550fn parse_coordinate(value: &str) -> Option<u32> {
551    value.parse::<u32>().ok().filter(|value| *value > 0)
552}
553
554impl FileLocation {
555    fn resolve(&self, from_path: Option<&str>, file: &str) -> String {
556        let path = from_path.map_or_else(|| file.to_owned(), |root| join_path(root, file));
557        match self {
558            Self::Captured => path,
559            Self::Relative(prefix) => join_path(prefix, &path),
560        }
561    }
562}
563
564fn join_path(prefix: &str, path: &str) -> String {
565    if prefix.is_empty() || is_absolute(path) {
566        return path.to_owned();
567    }
568    let prefix = prefix.trim_end_matches(['/', '\\']);
569    let path = path.trim_start_matches(['/', '\\']);
570    if prefix.is_empty() {
571        path.to_owned()
572    } else if path.is_empty() {
573        prefix.to_owned()
574    } else {
575        format!("{prefix}/{path}")
576    }
577}
578
579fn is_absolute(path: &str) -> bool {
580    path.starts_with(['/', '\\'])
581        || path
582            .as_bytes()
583            .get(1)
584            .is_some_and(|delimiter| *delimiter == b':')
585}
586
587fn stream_name(stream: Stream) -> &'static str {
588    match stream {
589        Stream::Stdout => "stdout",
590        Stream::Stderr => "stderr",
591        Stream::Combined => "combined",
592        Stream::Annotation => "annotation",
593        Stream::Structured => "structured",
594    }
595}
596
597fn parse_severity(value: &str) -> Option<Severity> {
598    match value.trim().to_ascii_lowercase().as_str() {
599        "error" | "err" | "e" => Some(Severity::Error),
600        "warning" | "warn" | "w" => Some(Severity::Warning),
601        "info" | "information" | "note" | "notice" | "hint" | "i" => Some(Severity::Note),
602        _ => None,
603    }
604}
605
606#[derive(Deserialize)]
607#[serde(rename_all = "camelCase", deny_unknown_fields)]
608struct RawDocument {
609    problem_matcher: OneOrMany<RawMatcherEntry>,
610}
611
612#[derive(Deserialize)]
613#[serde(untagged)]
614enum OneOrMany<T> {
615    One(T),
616    Many(Vec<T>),
617}
618
619impl<T> OneOrMany<T> {
620    fn into_vec(self) -> Vec<T> {
621        match self {
622            Self::One(value) => vec![value],
623            Self::Many(values) => values,
624        }
625    }
626}
627
628#[derive(Deserialize)]
629#[serde(untagged)]
630enum RawMatcherEntry {
631    Definition(Box<RawMatcher>),
632    Reference(String),
633}
634
635#[derive(Deserialize)]
636#[serde(rename_all = "camelCase", deny_unknown_fields)]
637struct RawMatcher {
638    owner: Option<String>,
639    source: Option<String>,
640    severity: Option<String>,
641    file_location: Option<Value>,
642    pattern: RawPatternSet,
643    base: Option<String>,
644    name: Option<String>,
645    label: Option<String>,
646    apply_to: Option<String>,
647    background: Option<Value>,
648    watching: Option<Value>,
649    watched_task_begins_reg_exp: Option<String>,
650    watched_task_ends_reg_exp: Option<String>,
651}
652
653#[derive(Deserialize)]
654#[serde(untagged)]
655enum RawPatternSet {
656    Single(RawPattern),
657    Multiple(Vec<RawPattern>),
658    Reference(String),
659}
660
661#[derive(Clone, Deserialize)]
662#[serde(rename_all = "camelCase", deny_unknown_fields)]
663struct RawPattern {
664    regexp: String,
665    kind: Option<String>,
666    file: Option<usize>,
667    from_path: Option<usize>,
668    location: Option<usize>,
669    line: Option<usize>,
670    column: Option<usize>,
671    end_line: Option<usize>,
672    end_column: Option<usize>,
673    severity: Option<usize>,
674    code: Option<usize>,
675    message: Option<usize>,
676    #[serde(default)]
677    r#loop: bool,
678}
679
680fn raw_matchers(value: Value) -> Result<Vec<RawMatcherEntry>, ProblemMatcherError> {
681    if value
682        .as_object()
683        .is_some_and(|object| object.contains_key("problemMatcher"))
684    {
685        return Ok(serde_json::from_value::<RawDocument>(value)?
686            .problem_matcher
687            .into_vec());
688    }
689    if value.is_array() {
690        return Ok(serde_json::from_value::<Vec<RawMatcherEntry>>(value)?);
691    }
692    Ok(vec![serde_json::from_value::<RawMatcherEntry>(value)?])
693}
694
695fn compile_matcher(
696    entry: RawMatcherEntry,
697    index: usize,
698    limits: ProblemMatcherLimits,
699) -> Result<CompiledMatcher, ProblemMatcherError> {
700    let raw = match entry {
701        RawMatcherEntry::Definition(raw) => *raw,
702        RawMatcherEntry::Reference(reference) => {
703            return Err(invalid(format!(
704                "problem matcher {index}: named matcher reference {reference:?} is not self-contained"
705            )));
706        }
707    };
708    if let Some(base) = raw.base {
709        return Err(invalid(format!(
710            "problem matcher {index}: base reference {base:?} cannot be resolved from a standalone definition"
711        )));
712    }
713    if raw
714        .apply_to
715        .as_deref()
716        .is_some_and(|value| !value.eq_ignore_ascii_case("allDocuments"))
717    {
718        return Err(invalid(format!(
719            "problem matcher {index}: applyTo requires editor document state and only allDocuments is portable"
720        )));
721    }
722    let _lifecycle_metadata = (
723        raw.name,
724        raw.label,
725        raw.background,
726        raw.watching,
727        raw.watched_task_begins_reg_exp,
728        raw.watched_task_ends_reg_exp,
729    );
730    let owner = required_bounded(raw.owner, "owner", index, 1024)?;
731    let source = bounded_optional(raw.source, "source", index, 1024)?;
732    let severity = raw
733        .severity
734        .as_deref()
735        .map(|value| {
736            parse_severity(value).ok_or_else(|| {
737                invalid(format!(
738                    "problem matcher {index} ({owner:?}): invalid default severity {value:?}"
739                ))
740            })
741        })
742        .transpose()?
743        .unwrap_or(Severity::Error);
744    let file_location = compile_file_location(raw.file_location, index, &owner)?;
745    let mut patterns = match raw.pattern {
746        RawPatternSet::Single(pattern) => vec![pattern],
747        RawPatternSet::Multiple(patterns) => patterns,
748        RawPatternSet::Reference(reference) => {
749            return Err(invalid(format!(
750                "problem matcher {index} ({owner:?}): named pattern reference {reference:?} cannot be resolved"
751            )));
752        }
753    };
754    if patterns.is_empty() {
755        return Err(invalid(format!(
756            "problem matcher {index} ({owner:?}): pattern must not be empty"
757        )));
758    }
759    if patterns.len() > limits.max_patterns_per_matcher {
760        return Err(invalid(format!(
761            "problem matcher {index} ({owner:?}) has {} patterns; maximum is {}",
762            patterns.len(),
763            limits.max_patterns_per_matcher
764        )));
765    }
766    apply_single_line_defaults(&mut patterns);
767    validate_pattern_contract(&patterns, index, &owner)?;
768    let patterns = patterns
769        .into_iter()
770        .enumerate()
771        .map(|(pattern_index, pattern)| {
772            compile_pattern(pattern, index, &owner, pattern_index, limits)
773        })
774        .collect::<Result<Vec<_>, _>>()?;
775    Ok(CompiledMatcher {
776        owner,
777        source,
778        severity,
779        file_location,
780        patterns,
781    })
782}
783
784fn apply_single_line_defaults(patterns: &mut [RawPattern]) {
785    if patterns.len() != 1 {
786        return;
787    }
788    let pattern = &mut patterns[0];
789    pattern.file.get_or_insert(1);
790    if pattern.location.is_none()
791        && !pattern
792            .kind
793            .as_deref()
794            .is_some_and(|value| value.eq_ignore_ascii_case("file"))
795    {
796        pattern.line.get_or_insert(2);
797        pattern.column.get_or_insert(3);
798    }
799    pattern.message.get_or_insert(0);
800}
801
802fn validate_pattern_contract(
803    patterns: &[RawPattern],
804    matcher_index: usize,
805    owner: &str,
806) -> Result<(), ProblemMatcherError> {
807    let mut has_file = false;
808    let mut has_message = false;
809    let mut has_location = false;
810    for (index, pattern) in patterns.iter().enumerate() {
811        if index > 0 && pattern.kind.is_some() {
812            return Err(invalid(format!(
813                "problem matcher {matcher_index} ({owner:?}) pattern {index}: kind is only valid on the first pattern"
814            )));
815        }
816        if pattern.r#loop && (index != patterns.len() - 1 || patterns.len() == 1) {
817            return Err(invalid(format!(
818                "problem matcher {matcher_index} ({owner:?}) pattern {index}: loop is only valid on the last pattern of a multiline matcher"
819            )));
820        }
821        has_file |= pattern.file.is_some();
822        has_message |= pattern.message.is_some();
823        has_location |= pattern.location.is_some() || pattern.line.is_some();
824    }
825    if !has_file || !has_message {
826        return Err(invalid(format!(
827            "problem matcher {matcher_index} ({owner:?}) must capture a file and message"
828        )));
829    }
830    let kind = patterns[0]
831        .kind
832        .as_deref()
833        .map(parse_location_kind)
834        .transpose()?
835        .unwrap_or(LocationKind::Location);
836    if kind == LocationKind::Location && !has_location {
837        return Err(invalid(format!(
838            "problem matcher {matcher_index} ({owner:?}) must capture line or location unless kind is file"
839        )));
840    }
841    Ok(())
842}
843
844fn compile_pattern(
845    raw: RawPattern,
846    matcher_index: usize,
847    owner: &str,
848    pattern_index: usize,
849    limits: ProblemMatcherLimits,
850) -> Result<CompiledPattern, ProblemMatcherError> {
851    let regex = RegexBuilder::new(&raw.regexp)
852        .size_limit(limits.max_compiled_regex_bytes)
853        .dfa_size_limit(limits.max_compiled_regex_bytes)
854        .build()
855        .map_err(|error| {
856            invalid(format!(
857                "problem matcher {matcher_index} ({owner:?}) pattern {pattern_index}: invalid or unsupported regular expression: {error}"
858            ))
859        })?;
860    let pattern = CompiledPattern {
861        regex,
862        source_bytes: raw.regexp.len(),
863        kind: raw.kind.as_deref().map(parse_location_kind).transpose()?,
864        file: raw.file,
865        from_path: raw.from_path,
866        location: raw.location,
867        line: raw.line,
868        column: raw.column,
869        end_line: raw.end_line,
870        end_column: raw.end_column,
871        severity: raw.severity,
872        code: raw.code,
873        message: raw.message,
874        r#loop: raw.r#loop,
875    };
876    let capture_count = pattern.regex.captures_len();
877    for (field, group) in pattern.capture_groups() {
878        if group >= capture_count {
879            return Err(invalid(format!(
880                "problem matcher {matcher_index} ({owner:?}) pattern {pattern_index}: {field} references capture group {group}, but the regexp has {} groups",
881                capture_count.saturating_sub(1)
882            )));
883        }
884    }
885    Ok(pattern)
886}
887
888impl CompiledPattern {
889    fn capture_groups(&self) -> Vec<(&'static str, usize)> {
890        [
891            ("file", self.file),
892            ("fromPath", self.from_path),
893            ("location", self.location),
894            ("line", self.line),
895            ("column", self.column),
896            ("endLine", self.end_line),
897            ("endColumn", self.end_column),
898            ("severity", self.severity),
899            ("code", self.code),
900            ("message", self.message),
901        ]
902        .into_iter()
903        .filter_map(|(field, value)| value.map(|group| (field, group)))
904        .collect()
905    }
906}
907
908fn compile_file_location(
909    value: Option<Value>,
910    matcher_index: usize,
911    owner: &str,
912) -> Result<FileLocation, ProblemMatcherError> {
913    let Some(value) = value else {
914        return Ok(FileLocation::Captured);
915    };
916    if let Some(kind) = value.as_str() {
917        return match kind.to_ascii_lowercase().as_str() {
918            "relative" | "absolute" | "autodetect" => Ok(FileLocation::Captured),
919            "search" => Err(invalid(format!(
920                "problem matcher {matcher_index} ({owner:?}): fileLocation search requires filesystem access"
921            ))),
922            _ => Err(invalid(format!(
923                "problem matcher {matcher_index} ({owner:?}): invalid fileLocation {kind:?}"
924            ))),
925        };
926    }
927    let Some(values) = value.as_array() else {
928        return Err(invalid(format!(
929            "problem matcher {matcher_index} ({owner:?}): fileLocation must be a string or pair"
930        )));
931    };
932    if values.len() != 2 {
933        return Err(invalid(format!(
934            "problem matcher {matcher_index} ({owner:?}): fileLocation pair must have two elements"
935        )));
936    }
937    let Some(kind) = values[0].as_str() else {
938        return Err(invalid(format!(
939            "problem matcher {matcher_index} ({owner:?}): fileLocation kind must be a string"
940        )));
941    };
942    if kind.eq_ignore_ascii_case("search") {
943        return Err(invalid(format!(
944            "problem matcher {matcher_index} ({owner:?}): fileLocation search requires filesystem access"
945        )));
946    }
947    if !kind.eq_ignore_ascii_case("relative") && !kind.eq_ignore_ascii_case("autodetect") {
948        return Err(invalid(format!(
949            "problem matcher {matcher_index} ({owner:?}): fileLocation pair must use relative or autodetect"
950        )));
951    }
952    let Some(prefix) = values[1].as_str() else {
953        return Err(invalid(format!(
954            "problem matcher {matcher_index} ({owner:?}): fileLocation prefix must be a string"
955        )));
956    };
957    if prefix.len() > 4096 {
958        return Err(invalid(format!(
959            "problem matcher {matcher_index} ({owner:?}): fileLocation prefix exceeds 4096 bytes"
960        )));
961    }
962    Ok(FileLocation::Relative(prefix.to_owned()))
963}
964
965fn parse_location_kind(value: &str) -> Result<LocationKind, ProblemMatcherError> {
966    match value.to_ascii_lowercase().as_str() {
967        "file" => Ok(LocationKind::File),
968        "location" => Ok(LocationKind::Location),
969        _ => Err(invalid(format!("invalid problem matcher kind {value:?}"))),
970    }
971}
972
973fn required_bounded(
974    value: Option<String>,
975    field: &str,
976    matcher_index: usize,
977    maximum: usize,
978) -> Result<String, ProblemMatcherError> {
979    let Some(value) = value.filter(|value| !value.is_empty()) else {
980        return Err(invalid(format!(
981            "problem matcher {matcher_index}: {field} is required and must not be empty"
982        )));
983    };
984    if value.len() > maximum {
985        return Err(invalid(format!(
986            "problem matcher {matcher_index}: {field} exceeds {maximum} bytes"
987        )));
988    }
989    Ok(value)
990}
991
992fn bounded_optional(
993    value: Option<String>,
994    field: &str,
995    matcher_index: usize,
996    maximum: usize,
997) -> Result<Option<String>, ProblemMatcherError> {
998    if value.as_ref().is_some_and(|value| value.len() > maximum) {
999        return Err(invalid(format!(
1000            "problem matcher {matcher_index}: {field} exceeds {maximum} bytes"
1001        )));
1002    }
1003    Ok(value)
1004}
1005
1006fn invalid(message: String) -> ProblemMatcherError {
1007    ProblemMatcherError::Invalid(message)
1008}
1009
1010#[cfg(test)]
1011mod tests {
1012    use logcompact_core::{
1013        Budget, EndReason, GenericRanker, NoPathMapping, NoRedaction, OutputPolicy, ParserPlan,
1014        Reduction, ReductionSession, Scope, SessionOptions,
1015    };
1016
1017    use super::*;
1018
1019    fn reduce(definition: &[u8], chunks: &[&[u8]]) -> Reduction {
1020        let mut registry = ProblemMatcherRegistry::default();
1021        registry.add_json(definition).unwrap();
1022        let mut plan = ParserPlan::new();
1023        plan.push(registry.into_parser()).unwrap();
1024        let mut session = ReductionSession::new(
1025            plan,
1026            SessionOptions {
1027                budget: Budget::unbounded(),
1028                ..SessionOptions::default()
1029            },
1030            OutputPolicy::new(&NoRedaction, &NoPathMapping, &GenericRanker),
1031        );
1032        session.begin_scope(Scope::step("matcher"));
1033        for chunk in chunks {
1034            session.push_chunk("matcher", Stream::Stderr, chunk);
1035        }
1036        session.end_scope("matcher", EndReason::Complete);
1037        session.finish()
1038    }
1039
1040    fn reduce_with_builtins(definition: &[u8], input: &[u8]) -> Reduction {
1041        let mut registry = ProblemMatcherRegistry::default();
1042        registry.add_json(definition).unwrap();
1043        let plan = crate::builtin_parser_plan_with_problem_matchers(
1044            crate::BuiltinParserOptions::default(),
1045            registry,
1046        );
1047        let mut session = ReductionSession::new(
1048            plan,
1049            SessionOptions {
1050                budget: Budget::unbounded(),
1051                ..SessionOptions::default()
1052            },
1053            OutputPolicy::new(&NoRedaction, &NoPathMapping, &GenericRanker),
1054        );
1055        session.begin_scope(Scope::step("matcher"));
1056        session.push_chunk("matcher", Stream::Stderr, input);
1057        session.end_scope("matcher", EndReason::Complete);
1058        session.finish()
1059    }
1060
1061    #[test]
1062    fn parses_single_line_github_definition_with_ranges() {
1063        let definition = br#"{
1064            "problemMatcher": [{
1065                "owner": "example",
1066                "source": "example compiler",
1067                "fileLocation": ["relative", "workspace"],
1068                "severity": "warning",
1069                "pattern": [{
1070                    "regexp": "^([^|]+)\\|(.+):(\\d+):(\\d+)-(\\d+):(\\d+): (error|warning): (.+) \\[(.+)\\]$",
1071                    "fromPath": 1,
1072                    "file": 2,
1073                    "line": 3,
1074                    "column": 4,
1075                    "endLine": 5,
1076                    "endColumn": 6,
1077                    "severity": 7,
1078                    "message": 8,
1079                    "code": 9
1080                }]
1081            }]
1082        }"#;
1083        let reduction = reduce(
1084            definition,
1085            &[b"package|src/main.rs:12:4-13:8: error: unknown variable [E100]\n"],
1086        );
1087        assert_eq!(reduction.diagnostics.len(), 1);
1088        let diagnostic = &reduction.diagnostics[0];
1089        assert_eq!(diagnostic.severity, Severity::Error);
1090        assert_eq!(diagnostic.code.as_deref(), Some("E100"));
1091        assert_eq!(diagnostic.message, "unknown variable");
1092        let location = diagnostic.location.as_ref().unwrap();
1093        assert_eq!(location.path, "workspace/package/src/main.rs");
1094        assert_eq!(location.line, Some(12));
1095        assert_eq!(location.column, Some(4));
1096        assert_eq!(location.end_line, Some(13));
1097        assert_eq!(location.end_column, Some(8));
1098        assert_eq!(
1099            diagnostic.provenance.as_ref().unwrap().label.as_deref(),
1100            Some("example compiler")
1101        );
1102    }
1103
1104    #[test]
1105    fn multiline_loop_inherits_file_and_resets_on_noise() {
1106        let definition = br#"{
1107            "problemMatcher": [{
1108                "owner": "eslint-stylish",
1109                "pattern": [
1110                    {"regexp": "^([^\\s].*)$", "file": 1},
1111                    {
1112                        "regexp": "^\\s+(\\d+):(\\d+)\\s+(error|warning)\\s+(.+?)\\s{2,}(\\S+)$",
1113                        "line": 1,
1114                        "column": 2,
1115                        "severity": 3,
1116                        "message": 4,
1117                        "code": 5,
1118                        "loop": true
1119                    }
1120                ]
1121            }]
1122        }"#;
1123        let input = b"src/example.js\n  1:2 error Missing strict  strict\n  5:10 warning Unused value  unused\nnoise\nsrc/other.js\n  ignored\n  9:1 error Not consecutive  rule\n";
1124        let whole = reduce(definition, &[input]);
1125        let chunked = reduce(definition, &[&input[..17], &input[17..51], &input[51..]]);
1126        assert_eq!(whole, chunked);
1127        assert_eq!(whole.diagnostics.len(), 2);
1128        assert_eq!(
1129            whole.diagnostics[0].location.as_ref().unwrap().line,
1130            Some(1)
1131        );
1132        assert_eq!(
1133            whole.diagnostics[1].location.as_ref().unwrap().line,
1134            Some(5)
1135        );
1136        assert_eq!(whole.diagnostics[1].severity, Severity::Warning);
1137    }
1138
1139    #[test]
1140    fn duplicate_owner_uses_last_registered_definition() {
1141        let first = br#"{"problemMatcher":[{"owner":"same","pattern":{"regexp":"^(.+):(\\d+):(\\d+): (.+)$","file":1,"line":2,"column":3,"message":4}}]}"#;
1142        let second = br#"{"problemMatcher":[{"owner":"same","severity":"warning","pattern":{"regexp":"^(.+):(\\d+):(\\d+): (.+)$","file":1,"line":2,"column":3,"message":4}}]}"#;
1143        let mut registry = ProblemMatcherRegistry::default();
1144        registry.add_json(first).unwrap();
1145        registry.add_json(second).unwrap();
1146        assert_eq!(registry.len(), 1);
1147
1148        let mut plan = ParserPlan::new();
1149        plan.push(registry.into_parser()).unwrap();
1150        let mut session = ReductionSession::new(
1151            plan,
1152            SessionOptions {
1153                budget: Budget::unbounded(),
1154                ..SessionOptions::default()
1155            },
1156            OutputPolicy::new(&NoRedaction, &NoPathMapping, &GenericRanker),
1157        );
1158        session.begin_scope(Scope::step("matcher"));
1159        session.push_chunk("matcher", Stream::Stderr, b"file.rs:1:2: message\n");
1160        session.end_scope("matcher", EndReason::Complete);
1161        assert_eq!(session.finish().diagnostics[0].severity, Severity::Warning);
1162    }
1163
1164    #[test]
1165    fn custom_matchers_suppress_fallbacks_but_not_recognized_builtins() {
1166        let fallback_definition = br#"{
1167            "problemMatcher": [{
1168                "owner": "custom",
1169                "pattern": {
1170                    "regexp": "^CUSTOM (.+):(\\d+):(\\d+): error: (.+)$",
1171                    "file": 1, "line": 2, "column": 3, "message": 4
1172                }
1173            }]
1174        }"#;
1175        let fallback = reduce_with_builtins(
1176            fallback_definition,
1177            b"CUSTOM src/main.dsl:4:2: error: broken widget\n",
1178        );
1179        assert_eq!(fallback.diagnostics.len(), 1);
1180        assert_eq!(
1181            fallback.diagnostics[0]
1182                .provenance
1183                .as_ref()
1184                .unwrap()
1185                .parser
1186                .as_deref(),
1187            Some("problem-matcher.v1")
1188        );
1189
1190        let go_definition = br#"{
1191            "problemMatcher": [{
1192                "owner": "custom-go",
1193                "pattern": {
1194                    "regexp": "^(.+):(\\d+):(\\d+): (.+)$",
1195                    "file": 1, "line": 2, "column": 3, "message": 4
1196                }
1197            }]
1198        }"#;
1199        let recognized =
1200            reduce_with_builtins(go_definition, b"src/main.go:12:4: undefined: total\n");
1201        assert_eq!(recognized.diagnostics.len(), 2);
1202        assert_eq!(recognized.diagnostics[0].class, DiagnosticClass::Compiler);
1203        assert_eq!(recognized.diagnostics[1].class, DiagnosticClass::Tool);
1204    }
1205
1206    #[test]
1207    fn a_custom_owner_replaces_the_matching_builtin() {
1208        let definition = br#"{
1209            "problemMatcher": [{
1210                "owner": "rust-compiler",
1211                "source": "custom rust",
1212                "pattern": [
1213                    {
1214                        "regexp": "^error\\[(E\\d+)\\]: (.+)$",
1215                        "code": 1, "message": 2
1216                    },
1217                    {
1218                        "regexp": "^ --> (.+):(\\d+):(\\d+)$",
1219                        "file": 1, "line": 2, "column": 3
1220                    }
1221                ]
1222            }]
1223        }"#;
1224        let reduction = reduce_with_builtins(
1225            definition,
1226            b"error[E0308]: mismatched types\n --> src/lib.rs:7:5\n",
1227        );
1228        assert_eq!(reduction.diagnostics.len(), 1);
1229        assert_eq!(reduction.diagnostics[0].class, DiagnosticClass::Tool);
1230        assert_eq!(reduction.diagnostics[0].code.as_deref(), Some("E0308"));
1231        assert_eq!(reduction.diagnostics[0].message, "mismatched types");
1232        assert_eq!(
1233            reduction.diagnostics[0]
1234                .provenance
1235                .as_ref()
1236                .unwrap()
1237                .label
1238                .as_deref(),
1239            Some("custom rust")
1240        );
1241    }
1242
1243    #[test]
1244    fn parses_combined_locations_and_file_only_findings() {
1245        let definition = br#"[
1246            {
1247                "owner": "range",
1248                "pattern": {
1249                    "regexp": "^RANGE (.+)\\((\\d+,\\d+,\\d+,\\d+)\\): (.+)$",
1250                    "file": 1,
1251                    "location": 2,
1252                    "message": 3
1253                }
1254            },
1255            {
1256                "owner": "whole-file",
1257                "pattern": {
1258                    "regexp": "^FILE (.+): (.+)$",
1259                    "kind": "file",
1260                    "file": 1,
1261                    "message": 2
1262                }
1263            }
1264        ]"#;
1265        let reduction = reduce(
1266            definition,
1267            &[b"RANGE src/lib.rs(2,3,4,5): ranged\nFILE src/all.rs: whole file\n"],
1268        );
1269        assert_eq!(reduction.diagnostics.len(), 2);
1270        let range = reduction.diagnostics[0].location.as_ref().unwrap();
1271        assert_eq!(range.line, Some(2));
1272        assert_eq!(range.column, Some(3));
1273        assert_eq!(range.end_line, Some(4));
1274        assert_eq!(range.end_column, Some(5));
1275        let file = reduction.diagnostics[1].location.as_ref().unwrap();
1276        assert_eq!(file.path, "src/all.rs");
1277        assert_eq!(file.line, None);
1278    }
1279
1280    #[test]
1281    fn state_bounds_are_reported_as_truncation() {
1282        let definition = br#"{
1283            "problemMatcher": [{
1284                "owner": "bounded",
1285                "pattern": [
1286                    {"regexp": "^FILE (.+)$", "file": 1},
1287                    {"regexp": "^LINE (\\d+): (.+)$", "line": 1, "message": 2}
1288                ]
1289            }]
1290        }"#;
1291        let mut registry = ProblemMatcherRegistry::new(ProblemMatcherLimits {
1292            max_state_bytes: 4,
1293            ..ProblemMatcherLimits::default()
1294        });
1295        registry.add_json(definition).unwrap();
1296        let mut plan = ParserPlan::new();
1297        plan.push(registry.into_parser()).unwrap();
1298        let mut session = ReductionSession::new(
1299            plan,
1300            SessionOptions {
1301                budget: Budget::unbounded(),
1302                ..SessionOptions::default()
1303            },
1304            OutputPolicy::new(&NoRedaction, &NoPathMapping, &GenericRanker),
1305        );
1306        session.begin_scope(Scope::step("matcher"));
1307        session.push_chunk(
1308            "matcher",
1309            Stream::Stderr,
1310            b"FILE long-name.rs\nLINE 1: message\n",
1311        );
1312        session.end_scope("matcher", EndReason::Complete);
1313        let reduction = session.finish();
1314        assert!(reduction.diagnostics.is_empty());
1315        assert!(reduction.truncated);
1316        assert_eq!(reduction.stats.candidates_dropped, 1);
1317    }
1318
1319    #[test]
1320    fn retained_state_bytes_are_bounded_across_scopes() {
1321        let definition = br#"{
1322            "problemMatcher": [{
1323                "owner": "bounded",
1324                "pattern": [
1325                    {"regexp": "^FILE (.+)$", "file": 1},
1326                    {"regexp": "^LINE (\\d+): (.+)$", "line": 1, "message": 2}
1327                ]
1328            }]
1329        }"#;
1330        let mut registry = ProblemMatcherRegistry::new(ProblemMatcherLimits {
1331            max_state_bytes: 7,
1332            ..ProblemMatcherLimits::default()
1333        });
1334        registry.add_json(definition).unwrap();
1335        let mut plan = ParserPlan::new();
1336        plan.push(registry.into_parser()).unwrap();
1337        let mut session = ReductionSession::new(
1338            plan,
1339            SessionOptions {
1340                budget: Budget::unbounded(),
1341                ..SessionOptions::default()
1342            },
1343            OutputPolicy::new(&NoRedaction, &NoPathMapping, &GenericRanker),
1344        );
1345        session.begin_scope(Scope::step("one"));
1346        session.begin_scope(Scope::step("two"));
1347        session.push_chunk("one", Stream::Stderr, b"FILE a.rs\n");
1348        session.push_chunk("two", Stream::Stderr, b"FILE b.rs\n");
1349        session.end_scope("one", EndReason::Complete);
1350        session.end_scope("two", EndReason::Complete);
1351        let reduction = session.finish();
1352        assert!(reduction.diagnostics.is_empty());
1353        assert!(reduction.truncated);
1354        assert_eq!(reduction.stats.candidates_dropped, 1);
1355    }
1356
1357    #[test]
1358    fn rejects_unbounded_or_nonportable_definitions() {
1359        let mut registry = ProblemMatcherRegistry::default();
1360        for (definition, expected) in [
1361            (
1362                br#"{"problemMatcher":[{"owner":"bad","pattern":"$gcc"}]}"#.as_slice(),
1363                "named pattern reference",
1364            ),
1365            (
1366                br#"{"problemMatcher":[{"owner":"bad","fileLocation":"search","pattern":{"regexp":"^(.+):(\\d+):(.*)$","file":1,"line":2,"message":3}}]}"#.as_slice(),
1367                "filesystem access",
1368            ),
1369            (
1370                br#"{"problemMatcher":[{"owner":"bad","pattern":{"regexp":"^(a)$","file":1,"line":2,"message":3}}]}"#.as_slice(),
1371                "capture group 2",
1372            ),
1373            (
1374                br#"{"problemMatcher":[{"owner":"bad","pattern":{"regexp":"^(a)\\1$","file":1,"kind":"file","message":0}}]}"#.as_slice(),
1375                "unsupported regular expression",
1376            ),
1377        ] {
1378            let error = registry.add_json(definition).unwrap_err().to_string();
1379            assert!(error.contains(expected), "{error:?} did not contain {expected:?}");
1380            assert!(registry.is_empty());
1381        }
1382    }
1383}