Skip to content

Commit eb82ee5

Browse files
authored
FIX @W-21101982@ Adding changes to avoid sfge scan when no target files are there (#409)
1 parent 20abe29 commit eb82ee5

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

packages/code-analyzer-sfge-engine/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@salesforce/code-analyzer-sfge-engine",
33
"description": "Plugin package that adds 'Salesforce Graph Engine' as an engine into Salesforce Code Analyzer",
4-
"version": "0.17.0",
4+
"version": "0.18.0-SNAPSHOT",
55
"author": "The Salesforce Code Analyzer Team",
66
"license": "BSD-3-Clause",
77
"homepage": "https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/overview",

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)