Fix files added in beforeProgramValidate hook not being validated#1
Fix files added in beforeProgramValidate hook not being validated#1
Conversation
… are now validated correctly Co-authored-by: iBicha <17722782+iBicha@users.noreply.github.com>
| file.isValidated = true; | ||
|
|
||
| this.plugins.emit('afterFileValidate', file); | ||
| .once(() => { |
There was a problem hiding this comment.
This isn't just a normal .forEach. It's part of a class called Sequencer that runs the validations in small batches, yielding to the event loop every so often to allow the LanguageServer to cancel validations if necessary. A tight loop iterating over every file would block the event loop for the entire list.
In v1, we've modified the sequencer to accept a factory function for the items array instead of predetermining them. This would possibly solve this issue, because the factory could return the list of files right when the .foreach is processed instead of doing it ahead of time.
https://github.com/rokucommunity/brighterscript/blob/v1/src/common/Sequencer.ts
And if that's still not quite flexible enough, that factory could return a generator, so you could constantly be looking up the next file to return even if devs have added a new file during the process. Something like this:
public *allFiles(): Generator<[string, BrsFile | XmlFile], void, unknown> {
const yielded = new Set<string>();
while (true) {
let foundNew = false;
for (const [key, file] of this.files) {
if (!yielded.has(key)) {
yielded.add(key);
foundNew = true;
yield [key, file];
}
}
if (!foundNew) {
break;
}
}
}
Fixes issue rokucommunity#1450 where files added by plugins during the
beforeProgramValidatehook were not being validated.Problem
When plugins added files using
program.setFile()during thebeforeProgramValidateevent, those files were not included in the validation process. This broke plugins that dynamically generate files needed by other source files during validation.For example, this plugin scenario would fail:
Root Cause
In
Program.ts, the validation method usedsequencer.forEach(Object.values(this.files), ...)which captured the files array before thebeforeProgramValidateevent was emitted. Any files added during the plugin hook were added after this snapshot was taken.Solution
Moved the file collection to happen after the
beforeProgramValidateevent by replacing theforEachwith a manual iteration in a separateonce()block:This ensures files added by plugins during
beforeProgramValidateare properly included in validation.Testing
This is a minimal, surgical fix that preserves all existing behavior while resolving the execution order issue.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.