Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/ENGINE-TEMPLATE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"types": "dist/index.d.ts",
"dependencies": {
"@types/node": "^20.0.0",
"@salesforce/code-analyzer-engine-api": "0.27.0"
"@salesforce/code-analyzer-engine-api": "0.28.0-SNAPSHOT"
},
"devDependencies": {
"@eslint/js": "^9.32.0",
Expand Down Expand Up @@ -60,4 +60,4 @@
"!src/index.ts"
]
}
}
}
4 changes: 2 additions & 2 deletions packages/T-E-M-P-L-A-T-E/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"types": "dist/index.d.ts",
"dependencies": {
"@types/node": "^20.0.0",
"@salesforce/code-analyzer-engine-api": "0.27.0"
"@salesforce/code-analyzer-engine-api": "0.28.0-SNAPSHOT"
},
"devDependencies": {
"@eslint/js": "^9.32.0",
Expand Down Expand Up @@ -60,4 +60,4 @@
"!src/index.ts"
]
}
}
}
6 changes: 3 additions & 3 deletions packages/code-analyzer-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@salesforce/code-analyzer-core",
"description": "Core Package for the Salesforce Code Analyzer",
"version": "0.33.0",
"version": "0.34.0-SNAPSHOT",
"author": "The Salesforce Code Analyzer Team",
"license": "BSD-3-Clause",
"homepage": "https://developer.salesforce.com/docs/platform/salesforce-code-analyzer/overview",
Expand All @@ -13,7 +13,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"dependencies": {
"@salesforce/code-analyzer-engine-api": "0.27.0",
"@salesforce/code-analyzer-engine-api": "0.28.0-SNAPSHOT",
"@types/node": "^20.0.0",
"csv-stringify": "^6.6.0",
"js-yaml": "^4.1.0",
Expand Down Expand Up @@ -69,4 +69,4 @@
"!src/index.ts"
]
}
}
}
64 changes: 50 additions & 14 deletions packages/code-analyzer-core/src/code-analyzer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {pathToFileURL} from "node:url";
import os from "node:os";
import {RuleImpl, RuleSelection, RuleSelectionImpl} from "./rules"
import {
EngineRunResults,
Expand All @@ -24,7 +25,13 @@ import * as engApi from "@salesforce/code-analyzer-engine-api"
import {Clock, RealClock} from '@salesforce/code-analyzer-engine-api/utils';
import {EventEmitter} from "node:events";
import {CodeAnalyzerConfig, ConfigDescription, EngineOverrides, FIELDS, RuleOverride} from "./config";
import {EngineProgressAggregator, RuntimeUniqueIdGenerator, toAbsolutePath, UniqueIdGenerator} from "./utils";
import {
EngineProgressAggregator,
FileSystemHandler,
RuntimeFileSystemHandler,
RuntimeUniqueIdGenerator,
toAbsolutePath,
UniqueIdGenerator} from "./utils";
import fs from "node:fs";
import path from 'node:path';

Expand Down Expand Up @@ -95,13 +102,15 @@ export class CodeAnalyzer {
private readonly config: CodeAnalyzerConfig;
private clock: Clock = new RealClock();
private uniqueIdGenerator: UniqueIdGenerator = new RuntimeUniqueIdGenerator();
private fileSystemHandler: FileSystemHandler = new RuntimeFileSystemHandler();
private readonly eventEmitter: EventEmitter = new EventEmitter();
private readonly engines: Map<string, engApi.Engine> = new Map();
private readonly uninstantiableEnginesMap: Map<string, Error> = new Map();
private readonly engineConfigs: Map<string, EngineConfig> = new Map();
private readonly engineConfigDescriptions: Map<string, ConfigDescription> = new Map();
private readonly rulesCache: Map<string, RuleImpl[]> = new Map();
private readonly engineRuleDiscoveryProgressAggregator: EngineProgressAggregator = new EngineProgressAggregator();
private workingFolderRoot?: string;

constructor(config: CodeAnalyzerConfig, version: string = process.version) {
this.validateEnvironment(version);
Expand All @@ -124,6 +133,9 @@ export class CodeAnalyzer {
_setUniqueIdGenerator(uniqueIdGenerator: UniqueIdGenerator) {
this.uniqueIdGenerator = uniqueIdGenerator;
}
_setFileSystemHandler(fileSystemHandler: FileSystemHandler): void {
this.fileSystemHandler = fileSystemHandler;
}

/**
* Convenience method to return the same CodeAnalyzerConfig instance that was provided to the constructor
Expand Down Expand Up @@ -296,15 +308,16 @@ export class CodeAnalyzer {
// called a second time before the first call to run hasn't finished. This can occur if someone builds
// up a bunch of RunResults promises and then does a Promise.all on them. Otherwise, the progress events may
// override each other.

const engineRunOptions: engApi.RunOptions = extractEngineRunOptions(runOptions, this.config.getLogFolder());
const tmpDirRoot: string = path.join(await this.getWorkingFolderRoot(), `run-${this.clock.formatToDateTimeString()}`);
await this.fileSystemHandler.createDirectory(tmpDirRoot);
const workspace: engApi.Workspace = toEngApiWorkspace(runOptions.workspace);
this.emitLogEvent(LogLevel.Debug, getMessage('RunningWithWorkspace', JSON.stringify({
filesAndFolders: runOptions.workspace.getRawFilesAndFolders(),
targets: runOptions.workspace.getRawTargets()
})));

const runPromises: Promise<EngineRunResults>[] = ruleSelection.getEngineNames().map(
engineName => this.runEngineAndValidateResults(engineName, ruleSelection, engineRunOptions));
engineName => this.runEngineAndValidateResults(engineName, ruleSelection, this.config.getLogFolder(), workspace, tmpDirRoot));
const engineRunResultsList: EngineRunResults[] = await Promise.all(runPromises);

const runResults: RunResultsImpl = new RunResultsImpl(this.clock);
Expand All @@ -331,16 +344,27 @@ export class CodeAnalyzer {
private async getAllRules(workspace?: Workspace): Promise<RuleImpl[]> {
const cacheKey: string = workspace ? workspace.getWorkspaceId() : process.cwd();
if (!this.rulesCache.has(cacheKey)) {
// TODO: THIS WILL BE CONFIGURABLE SOON.
const tmpDirRoot: string = path.join(await this.getWorkingFolderRoot(), `describe-${this.clock.formatToDateTimeString()}`);
await this.fileSystemHandler.createDirectory(tmpDirRoot);
this.engineRuleDiscoveryProgressAggregator.reset(this.getEngineNames());
const engApiWorkspace: engApi.Workspace | undefined = workspace ? toEngApiWorkspace(workspace) : undefined;

const rulePromises: Promise<RuleImpl[]>[] = this.getEngineNames().map(engineName =>
this.getAllRulesFor(engineName, {workspace: engApiWorkspace, logFolder: this.config.getLogFolder()}));
this.getAllRulesFor(engineName, engApiWorkspace, tmpDirRoot, this.config.getLogFolder()));
this.rulesCache.set(cacheKey, (await Promise.all(rulePromises)).flat());
}
return this.rulesCache.get(cacheKey)!;
}

private async getAllRulesFor(engineName: string, describeOptions: engApi.DescribeOptions): Promise<RuleImpl[]> {
private async getAllRulesFor(engineName: string, workspace: engApi.Workspace | undefined, tmpDirRoot: string, logFolder: string): Promise<RuleImpl[]> {
const workingFolder: string = path.join(tmpDirRoot, engineName);
await this.fileSystemHandler.createDirectory(workingFolder);
const describeOptions: engApi.DescribeOptions = {
workspace,
workingFolder,
logFolder
};
this.emitLogEvent(LogLevel.Debug, getMessage('GatheringRulesFromEngine', engineName));
let ruleDescriptions: engApi.RuleDescription[] = [];
try {
Expand All @@ -366,7 +390,14 @@ export class CodeAnalyzer {
this.emitEvent({type: EventType.RuleSelectionProgressEvent, timestamp: this.clock.now(), percentComplete: aggregatedPerc});
}

private async runEngineAndValidateResults(engineName: string, ruleSelection: RuleSelection, engineRunOptions: engApi.RunOptions): Promise<EngineRunResults> {
private async runEngineAndValidateResults(engineName: string, ruleSelection: RuleSelection, logFolder: string, workspace: engApi.Workspace, tmpDirRoot: string): Promise<EngineRunResults> {

@stephen-carter-at-sf stephen-carter-at-sf Aug 25, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please call it "workingFolder" instead of workingDirectory. 2 reasons: 1) not to be confused with the current working directory (CWD) and 2) because it matches what the field should be in the run options.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change to workingFolder and tempFolder instead of workingDirectory and tempDirectory.

const workingFolder: string = path.join(tmpDirRoot, engineName);
await this.fileSystemHandler.createDirectory(workingFolder);
const engineRunOptions: engApi.RunOptions = {
logFolder,
workspace,
workingFolder
};
this.emitEvent<EngineRunProgressEvent>({
type: EventType.EngineRunProgressEvent, timestamp: this.clock.now(), engineName: engineName, percentComplete: 0
});
Expand Down Expand Up @@ -539,6 +570,17 @@ export class CodeAnalyzer {
private getEngine(engineName: string): engApi.Engine {
return this.engines.get(engineName)!;
}

private async getWorkingFolderRoot(): Promise<string> {
if (!this.workingFolderRoot) {
const workingFolderRoot: string = path.join(os.tmpdir(), 'code-analyzer');
if (!this.fileSystemHandler.directoryExists(workingFolderRoot)) {
await this.fileSystemHandler.createDirectory(workingFolderRoot);
}
this.workingFolderRoot = workingFolderRoot;
}
return this.workingFolderRoot;
}
}

/**
Expand Down Expand Up @@ -602,13 +644,6 @@ function validateRuleDescriptions(ruleDescriptions: engApi.RuleDescription[], en
}
}

function extractEngineRunOptions(runOptions: RunOptions, logFolder: string): engApi.RunOptions {
return {
logFolder: logFolder,
workspace: toEngApiWorkspace(runOptions.workspace),
};
}

async function validateFileOrFolder(fileOrFolder: string): Promise<string> {
const absFileOrFolder: string = toAbsolutePath(fileOrFolder);
try {
Expand Down Expand Up @@ -692,6 +727,7 @@ function validateViolationCodeLocations(violation: engApi.Violation, engineName:
engineName, violation.ruleName, codeLocation.endLine, codeLocation.startLine));
}

// istanbul ignore else
if (codeLocation.endColumn !== undefined) {
if (!isValidLineOrColumn(codeLocation.endColumn)) {
throw new Error(getMessage('EngineReturnedViolationWithCodeLocationWithInvalidLineOrColumn',
Expand Down
Loading
Loading