Skip to content

fix(references): a PROGRAM MAP INCLUDEd from an .inc reaches shim-headed members - #854

Merged
msarson merged 1 commit into
msarson:version-1.0.11from
geircodes:fix/references-included-program-map
Nov 17, 2026
Merged

msarson merged 1 commit into
msarson:version-1.0.11from
geircodes:fix/references-included-program-map

Conversation

@geircodes

Copy link
Copy Markdown
Contributor

What happens

A procedure prototyped in a MAP that lives in an .inc, which the PROGRAM INCLUDEs in its global data, and called from modules that get their MEMBER through an INCLUDE('member.clw') shim:

  PROGRAM                          ! prog.clw
  INCLUDE('helpers.inc'),ONCE
  MAP                              ! helpers.inc
    MODULE('helpers.clw')
      Helper(LONG pValue),LONG,PROC
    END
  END
  INCLUDE('member.clw')            ! caller.clw  (member.clw holds MEMBER('prog'))
Caller PROCEDURE()
r LONG
  CODE
  r = Helper(1)

The MAP is the PROGRAM's, so Helper is visible to every module, but Find All References never reaches caller.clw:

  • From the implementation label (Helper PROCEDURE in helpers.clw) or the prototype in helpers.inc: only the prototype and the implementation are listed. The call in caller.clw is missing.
  • From the call site in caller.clw, with an older copy of helpers.inc in a later .red folder: one result, the prototype in the older copy. The real prototype, the implementation and the call itself are all missing.

Root cause

Three separate gaps, each enough to lose the caller.

1. The #602 widening asks the project for the .inc. A MAP in a PROGRAM is widened to the whole program, but getFilesToSearch finds the program by looking up the declaring file among the project's sources:

const program = solutionManager?.findProjectForFile?.(path.basename(declPath));

An .inc is rarely a project source file, so this is undefined, and the search stays at the .inc, its MODULE target and its direct includers.

2. isDeclarationReachableFrom (#483) cannot find a shim-headed module's PROGRAM. It takes the caller's PROGRAM from the caller's own MEMBER edge:

const program = this.getProgramFile(from);
const belongsHere = (f: string): boolean =>
    f === from || (program !== undefined && (f === program || this.getProgramFile(f) === program));

caller.clw has no MEMBER edge (the shim has it), so program is undefined, and the walk up from helpers.inc never meets the caller. The real helpers.inc is reported unreachable (false).

3. findProcedureViaIndex then picks the copy the graph has never seen. The filter drops only false:

const hits = allHits.filter(h => frg.isDeclarationReachableFrom(h.filePath, curPathForReach) !== false);

The real helpers.inc is dropped by gap 2. The older copy is outside every project, so the graph answers undefined for it, and it is kept as the only hit. Since that copy has no includers, the search is that one file.

Fix

FileRelationshipGraph.isDeclarationReachableFrom finds a module's PROGRAM through its shim too:

const programOf = (f: string): string | undefined => this.getProgramFile(f) ?? this.programViaIncludedShim(f);
const program = programOf(from);
const belongsHere = (f: string): boolean =>
    f === from || (program !== undefined && (f === program || programOf(f) === program));

findProcedureViaIndex ranks a hit the graph proves reachable above one it has never seen, and falls back to the old filter when there is none:

const reach = allHits.map(h => ({ h, r: frg.isDeclarationReachableFrom(h.filePath, curPathForReach) }));
const proven = reach.filter(x => x.r === true).map(x => x.h);
const hits = proven.length > 0 ? proven : reach.filter(x => x.r !== false).map(x => x.h);

A new FileRelationshipGraph.getIncludingProgramFiles(filePath) walks the reverse INCLUDE edges up to the PROGRAM files that include a file at program level. A walk stops at a MEMBER module (its own MEMBER edge or a shim's), because a MAP that arrives there is that module's, as with #322's callout INCs. getFilesToSearch uses it when the project lookup finds nothing:

let program = solutionManager?.findProjectForFile?.(path.basename(declPath));
if (!program && graph.isBuilt) {
    for (const programFile of graph.getIncludingProgramFiles(declPath)) {
        program = solutionManager?.findProjectForFile?.(programFile.replace(/\//g, '\\'));
        if (program) break;
    }
}

From there the existing #602 branch widens to that program's project as before.

Testing

New server/src/test/ReferencesIncludedProgramMap.test.ts, 5 tests, on the fixture above. Its findProjectForFile mirrors the real lookup, where a file belongs to the project only when it is a listed source, so neither .inc is one. The older copy is in a second .red folder that the declaration index scans first.

Test Expected
FAR from the implementation label caller.clw:6, helpers.clw:2, helpers.inc:2
FAR from the .inc prototype the same three
FAR from the call site the same three
isDeclarationReachableFrom(helpers.inc, caller.clw) true
getIncludingProgramFiles(helpers.inc) prog.clw only, not helpers.clw, which includes it too

With the three source files reverted to the base, all four tests that compile against the base fail. From the implementation label the result is helpers.clw:2, helpers.inc:2, without the caller. From the call site it is stale/helpers.inc:3 alone.

The existing #602 and FAR-from-implementation suites pass unchanged.

Server suite: 4541 passing, 3 pending, 0 failing.

Scope

Left out on purpose:

  • Where in the PROGRAM the INCLUDE sits. getIncludingProgramFiles counts every INCLUDE from a PROGRAM file. An .inc INCLUDEd inside a procedure's own data would really be local to that procedure, and FAR would now search the whole program for it. Over-inclusive only, never missing a reference.
  • An .inc that is a project source (a None item in the .cwproj) already took the existing basename lookup and is unchanged.

…ded members

Find All References on a procedure prototyped in a MAP inside an .inc
that the PROGRAM INCLUDEs missed callers in modules whose MEMBER comes
from an INCLUDE('member.clw') shim. Three gaps: the msarson#602 widening
looked the .inc up as a project source and found none;
isDeclarationReachableFrom took the caller's PROGRAM only from its own
MEMBER edge, so the real .inc was unreachable; and findProcedureViaIndex
then kept a stale copy in a later .red folder. Reachability now follows the shim, a provably
reachable hit ranks above an unknown one, and the new
getIncludingProgramFiles() finds the PROGRAM that includes the .inc.

Tests: new ReferencesIncludedProgramMap.test.ts, 5 tests; the 4 that
compile against the base fail there. Server suite: 4541 passing,
3 pending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@geircodes
geircodes force-pushed the fix/references-included-program-map branch from 0e9b463 to 326f905 Compare November 6, 2026 07:26
@msarson
msarson merged commit 0a2f40a into msarson:version-1.0.11 Nov 17, 2026
1 check passed
msarson added a commit that referenced this pull request Nov 17, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@geircodes
geircodes deleted the fix/references-included-program-map 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