Skip to content

fix(diagnostics): no reports inside a COMPILE/OMIT block that does not compile - #866

Merged
msarson merged 1 commit into
msarson:version-1.0.11from
geircodes:fix/inactive-block-diagnostics
Nov 25, 2026
Merged

msarson merged 1 commit into
msarson:version-1.0.11from
geircodes:fix/inactive-block-diagnostics

Conversation

@geircodes

Copy link
Copy Markdown
Contributor

fix(diagnostics): no reports inside a COMPILE/OMIT block that does not compile

Part of #270 (phase 1, diagnostics only).

What happened

A program includes a class only when a project switch is on, and a module uses it under the same switch:

! Program file
  COMPILE('***Pdf***', _PDF_)
  INCLUDE('PdfClass.inc'),ONCE
  !***Pdf***

! Member module
  COMPILE('***Pdf***', _PDF_)
Test_Pdf  PROCEDURE
Pdf  PdfClass                    ! <- squiggle warning: pdfclass.inc is not included
  ...
!***Pdf***

With _PDF_=>off in the .cwproj's DefineConstants, #807/#812 correctly stop following the INCLUDE, but the member module still reports 'PdfClass' is defined in 'PdfClass.inc' which is not included. on Pdf PdfClass. The compiler never sees that line. The same holds for any validator on any line in a conditional block that does not compile: a switch set by an EQUATE in the file, by an EQUATE in an earlier INCLUDE, or by a project define.

Root cause

The facade filter every validator passes through only dropped unconditional OMIT bodies, DiagnosticProvider.filterOmitted:

const blocks = OmitCompileDetector.findDirectiveBlocks(tokens, document);
if (blocks.length === 0) return diagnostics;
const directiveStartLines = new Set(blocks.map(b => b.startLine));
return diagnostics.filter(d =>
    directiveStartLines.has(d.range.start.line) ||
    !OmitCompileDetector.isLineOmittedWithBlocks(d.range.start.line, blocks));

A conditional directive never becomes one of those blocks ("we cannot evaluate macros at analysis time"), so a file whose only directives are conditional even returned before looking. Its comment said "Conditional OMIT/COMPILE stays live (defines aren't evaluated yet)". Since #807/#812 they are: scanConditionalCompile(...).isInactive(line) already decides every line, but only inactiveIncludeLines used it, for INCLUDE lines.

The fix

ConditionalIncludes.ts keeps the runs of lines that do not compile next to the INCLUDE lines, from the same scan and in the same cache entry, and exposes them:

export function inactiveCodeTest(filePath: string | null, text: string): (line: number) => boolean {
    const code = decide(filePath, text).code;
    if (code.length === 0) return () => false;
    ...binary search over [first, last] pairs...
}

filterOmitted drops a diagnostic when either the old detector or this test says its line does not compile. The directive lines that always keep their reports now come from the OMIT/COMPILE directive tokens, so a conditional directive's own report ("unterminated") survives too:

const directiveLines = new Set<number>();
for (const t of tokens) {
    if (t.type === TokenType.Directive && /^(?:OMIT|COMPILE)$/i.test(t.value)) directiveLines.add(t.line);
}
if (directiveLines.size === 0) return diagnostics;
const blocks = OmitCompileDetector.findDirectiveBlocks(tokens, document);
const inactive = this.inactiveLineTest(tokens, document);
return diagnostics.filter(d => {
    const line = d.range.start.line;
    return directiveLines.has(line) ||
        !(OmitCompileDetector.isLineOmittedWithBlocks(line, blocks) || inactive(line));
});

The reading is done once per token list (a WeakMap keyed like blocksMemo, checked against uri and version), so the ten or so validators of one pass share it. A block whose switch is not known stays live, as #807 reads it: the change only ever removes reports.

Testing

New ConditionalCompileDiagnostics270.test.ts, 8 tests through DiagnosticProvider.validateDocument, with a live control line in each fixture:

  • COMPILE with SW EQUATE(0), and OMIT with SW EQUATE(1): no report in the block.
  • The same blocks the other way round: the report stays.
  • A switch nobody defines: the report stays.
  • SW EQUATE(0) from an earlier INCLUDE: no report.
  • SW=>off in a .cwproj's DefineConstants (the COMPILE/OMIT: decide conditions from the project's defines (.cwproj DefineConstants, .prj #pragma define) #812 harness): no report.
  • An unterminated conditional COMPILE decided off: the report on the directive line survives.
  • inactiveCodeTest per line, over adjacent runs and a plain OMIT.

Without the fix, the 4 "decided off" tests fail with the reported symptom. With the directive-line rule removed, its test fails. Full server suite: 4627 passing.

Scope

  • Every validator is affected, since the filter sits at the facade. Only lines in a block decided not to compile lose reports.
  • One change for an unconditional OMIT too: the line holding its terminator now counts as part of the block, as Honour COMPILE/OMIT conditions when following INCLUDEs (version-specific declarations) #807's reading has it (the help: "The block runs to the line that contains the terminator, and that line is part of it"). OmitCompileDetector.isLineOmittedWithBlocks treats only the lines between directive and terminator as omitted, so a report on code before the terminator ( c = 3 !***) used to show and is now dropped. The compiler skips that line as well. The directive line itself is not affected, since a report there always survives.
  • References, CodeLens counts and rename are unchanged: conditional blocks stay live there (OmitCompileDetector is not touched beyond a comment). Phase 1's other half, and the gray-out decoration (phase 2), are left for OMIT/COMPILE phase 2: define-driven conditional evaluation + editor gray-out decoration #270.

🤖 Generated with Claude Code

…t compile

A diagnostic on a line inside COMPILE('***',SW) with SW off (by an
EQUATE, one from an earlier INCLUDE, or a project define) still showed:
the facade filter (msarson#255) only dropped unconditional OMIT bodies, and
conditional directives never become OmitCompileDetector blocks.
filterOmitted now also asks msarson#807's reading which lines do not compile
(inactiveCodeTest, the same scan and cache as inactiveIncludeLines). A
block whose switch is not known stays live, and a report on a directive
line always survives. Affects every validator.

New ConditionalCompileDiagnostics270.test.ts: 8 tests, 4 fail without
the fix; full server suite 4627 passing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@msarson
msarson merged commit 686f9ac into msarson:version-1.0.11 Nov 25, 2026
1 check passed
msarson added a commit that referenced this pull request Nov 25, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@geircodes
geircodes deleted the fix/inactive-block-diagnostics branch November 25, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants