Skip to content

Commit 1c483e3

Browse files
committed
FIX @W-21101982@ Adding changes to avoid sfge scan when no target files are there (#409)
1 parent 8475a53 commit 1c483e3

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

packages/code-analyzer-sfge-engine/src/engine.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ export class SfgeEngine extends Engine {
6868
return { violations: [] };
6969
}
7070

71+
// Get targeted files and return early if empty - prevents SFGE from analyzing all workspace files
72+
const targetedFiles: string[] = await runOptions.workspace.getTargetedFiles();
73+
if (targetedFiles.length === 0) {
74+
this.emitRunRulesProgressEvent(100);
75+
return { violations: [] };
76+
}
77+
7178
await this.validateWorkspaceCompleteness(runOptions.workspace);
7279

7380
const allRulesInfoList: SfgeRuleInfo[] = await this.getSfgeRuleInfoList(
@@ -95,7 +102,7 @@ export class SfgeEngine extends Engine {
95102

96103
const sfgeResults: SfgeRunResult[] = await this.sfgeWrapper.invokeRunCommand(
97104
selectedRuleInfoList,
98-
await runOptions.workspace.getTargetedFiles(),
105+
targetedFiles,
99106
relevantWorkspaceFiles,
100107
sfgeRunOptions,
101108
runOptions.workingFolder,

packages/code-analyzer-sfge-engine/test/engine.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,37 @@ describe('SfgeEngine', () => {
133133
expect(progressEvents.map(e => e.percentComplete)).toEqual([2, 100]);
134134
});
135135

136+
it('When targets are empty, no violations are returned and SFGE is not invoked', async () => {
137+
// This test verifies that when the workspace contains .cls files but targets are empty
138+
// (e.g., after being filtered out by ignores), SFGE returns early without invoking
139+
// the Java process. This prevents SFGE from scanning all workspace files when
140+
// no targets are provided.
141+
const engine: SfgeEngine = new SfgeEngine(DEFAULT_SFGE_ENGINE_CONFIG, fixedClock);
142+
// Workspace folder has .cls files, but we pass empty targets
143+
const workspace: Workspace = new Workspace(
144+
'id',
145+
[path.join(TEST_DATA_FOLDER, 'sampleRelevantWorkspace')],
146+
[] // Empty targets - simulates all targets being filtered out by ignores
147+
);
148+
const logEvents: LogEvent[] = [];
149+
engine.onEvent(EventType.LogEvent, (e: LogEvent) => logEvents.push(e));
150+
const progressEvents: RunRulesProgressEvent[] = [];
151+
engine.onEvent(EventType.RunRulesProgressEvent, (e: RunRulesProgressEvent) => progressEvents.push(e));
152+
const ruleNames: string[] = ['ApexFlsViolation'];
153+
154+
// ====== TESTED BEHAVIOR ======
155+
const results: EngineRunResults = await engine.runRules(ruleNames, createRunOptions(workspace));
156+
157+
// ====== ASSERTIONS ======
158+
// No violations should be returned
159+
expect(results.violations).toHaveLength(0);
160+
// SFGE should not be invoked, so no log events about calling Java commands
161+
expect(logEvents).toHaveLength(0);
162+
// The progress events should skip from the very first one (2%) to the very last one (100%)
163+
// This confirms we returned early without running SFGE
164+
expect(progressEvents.map(e => e.percentComplete)).toEqual([2, 100]);
165+
});
166+
136167
it.each([
137168
{case: 'a folder with relevant files that do not violate the selected rules', workspacePaths: [path.join(TEST_DATA_FOLDER, 'sampleRelevantWorkspace')]},
138169
{

0 commit comments

Comments
 (0)