Skip to content

Commit a6c89e6

Browse files
committed
More CDS extractor code cleanup
This commit: - removes unused functions from CDS extractor "src/cds/**/*.ts"; - removes logging of CDS extractor memeory usage; - renames some CDS extractor logging functions for consistency; - addresses PR comments for the `src/cds/parser/functions.ts` file.
1 parent 759e118 commit a6c89e6

33 files changed

+385
-2613
lines changed

extractors/cds/tools/cds-extractor.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import { sync as globSync } from 'glob';
44

55
import { determineCdsCommand } from './src/cds';
66
import { orchestrateCompilation } from './src/cds/compiler';
7-
import { buildEnhancedCdsProjectDependencyGraph } from './src/cds/parser';
7+
import { buildCdsProjectDependencyGraph } from './src/cds/parser';
88
import { runJavaScriptExtractor } from './src/codeql';
99
import { addCompilationDiagnostic } from './src/diagnostics';
1010
import { configureLgtmIndexFilters, setupAndValidateEnvironment } from './src/environment';
1111
import {
1212
cdsExtractorLog,
13-
endPerformanceTracking,
14-
logExtractorStop,
1513
logExtractorStart,
16-
logMemoryUsage,
14+
logExtractorStop,
1715
logPerformanceMilestone,
16+
logPerformanceTrackingStart,
17+
logPerformanceTrackingStop,
1818
setSourceRootDirectory,
19-
startPerformanceTracking,
2019
} from './src/logging';
2120
import { installDependencies } from './src/packageManager';
2221
import { validateArguments } from './src/utils';
@@ -37,21 +36,19 @@ setSourceRootDirectory(sourceRoot);
3736

3837
// Log the start of the CDS extractor session as a whole.
3938
logExtractorStart(sourceRoot);
40-
// We log the memory usage at the start of the extractor to track memory growth.
41-
logMemoryUsage('Extractor Start');
4239

4340
// Setup the environment and validate all requirements first, before changing
4441
// directory back to the "sourceRoot" directory. This ensures we can properly locate
4542
// the CodeQL tools.
46-
startPerformanceTracking('Environment Setup');
43+
logPerformanceTrackingStart('Environment Setup');
4744
const {
4845
success: envSetupSuccess,
4946
errorMessages,
5047
codeqlExePath,
5148
autobuildScriptPath,
5249
platformInfo,
5350
} = setupAndValidateEnvironment(sourceRoot);
54-
endPerformanceTracking('Environment Setup');
51+
logPerformanceTrackingStop('Environment Setup');
5552

5653
if (!envSetupSuccess) {
5754
const codeqlExe = platformInfo.isWindows ? 'codeql.exe' : 'codeql';
@@ -75,7 +72,7 @@ cdsExtractorLog(
7572
`CodeQL CDS extractor using autobuild mode for scan of project source root directory '${sourceRoot}'.`,
7673
);
7774

78-
cdsExtractorLog('info', 'Building enhanced CDS project dependency graph...');
75+
cdsExtractorLog('info', 'Building CDS project dependency graph...');
7976

8077
// Build the CDS project `dependencyGraph` as the foundation for the extraction process.
8178
// This graph will contain all discovered CDS projects, their dependencies, the `.cds`
@@ -88,22 +85,21 @@ cdsExtractorLog('info', 'Building enhanced CDS project dependency graph...');
8885
let dependencyGraph;
8986

9087
try {
91-
startPerformanceTracking('Dependency Graph Build');
92-
dependencyGraph = buildEnhancedCdsProjectDependencyGraph(sourceRoot, __dirname);
93-
endPerformanceTracking('Dependency Graph Build');
88+
logPerformanceTrackingStart('Dependency Graph Build');
89+
dependencyGraph = buildCdsProjectDependencyGraph(sourceRoot, __dirname);
90+
logPerformanceTrackingStop('Dependency Graph Build');
9491

9592
logPerformanceMilestone(
9693
'Dependency graph created',
9794
`${dependencyGraph.projects.size} projects, ${dependencyGraph.statusSummary.totalCdsFiles} CDS files`,
9895
);
99-
logMemoryUsage('After Dependency Graph');
10096

10197
// Log details about discovered projects for debugging
10298
if (dependencyGraph.projects.size > 0) {
10399
for (const [projectDir, project] of dependencyGraph.projects.entries()) {
104100
cdsExtractorLog(
105101
'info',
106-
`Enhanced Project: ${projectDir}, Status: ${project.status}, CDS files: ${project.cdsFiles.length}, Files to compile: ${project.cdsFilesToCompile.length}`,
102+
`Project: ${projectDir}, Status: ${project.status}, CDS files: ${project.cdsFiles.length}, Files to compile: ${project.cdsFilesToCompile.length}`,
107103
);
108104
}
109105
} else {
@@ -148,15 +144,15 @@ try {
148144
process.exit(1);
149145
}
150146
} catch (error) {
151-
cdsExtractorLog('error', `Failed to build enhanced dependency graph: ${String(error)}`);
147+
cdsExtractorLog('error', `Failed to build CDS dependency graph: ${String(error)}`);
152148
// Exit with error since we can't continue without a proper dependency graph
153149
logExtractorStop(false, 'Terminated: Dependency graph build failed');
154150
process.exit(1);
155151
}
156152

157-
startPerformanceTracking('Dependency Installation');
153+
logPerformanceTrackingStart('Dependency Installation');
158154
const projectCacheDirMap = installDependencies(dependencyGraph, sourceRoot, codeqlExePath);
159-
endPerformanceTracking('Dependency Installation');
155+
logPerformanceTrackingStop('Dependency Installation');
160156

161157
// Check if dependency installation resulted in any usable project mappings
162158
if (projectCacheDirMap.size === 0) {
@@ -184,7 +180,7 @@ if (projectCacheDirMap.size === 0) {
184180

185181
const cdsFilePathsToProcess: string[] = [];
186182

187-
// Use the enhanced dependency graph to collect all `.cds` files from each project.
183+
// Use the dependency graph to collect all `.cds` files from each project.
188184
// We want to "extract" all `.cds` files from all projects so that we have a copy
189185
// of each `.cds` source file in the CodeQL database.
190186
for (const project of dependencyGraph.projects.values()) {
@@ -193,13 +189,13 @@ for (const project of dependencyGraph.projects.values()) {
193189

194190
// Initialize CDS command cache early to avoid repeated testing during compilation.
195191
// This is a critical optimization that avoids testing commands for every single file.
196-
startPerformanceTracking('CDS Command Cache Initialization');
192+
logPerformanceTrackingStart('CDS Command Cache Initialization');
197193
try {
198194
determineCdsCommand(undefined, sourceRoot);
199-
endPerformanceTracking('CDS Command Cache Initialization');
195+
logPerformanceTrackingStop('CDS Command Cache Initialization');
200196
cdsExtractorLog('info', 'CDS command cache initialized successfully');
201197
} catch (error) {
202-
endPerformanceTracking('CDS Command Cache Initialization');
198+
logPerformanceTrackingStop('CDS Command Cache Initialization');
203199
cdsExtractorLog('warn', `CDS command cache initialization failed: ${String(error)}`);
204200
}
205201

@@ -209,7 +205,7 @@ cdsExtractorLog(
209205
`Found ${cdsFilePathsToProcess.length} total CDS files, ${dependencyGraph.statusSummary.totalCdsFiles} CDS files in dependency graph`,
210206
);
211207

212-
startPerformanceTracking('CDS Compilation');
208+
logPerformanceTrackingStart('CDS Compilation');
213209
try {
214210
// Use the new orchestrated compilation approach (autobuild mode, no debug)
215211
orchestrateCompilation(dependencyGraph, projectCacheDirMap, codeqlExePath);
@@ -229,11 +225,10 @@ try {
229225
// Don't exit with error - let the JavaScript extractor run on whatever was compiled
230226
}
231227

232-
endPerformanceTracking('CDS Compilation');
228+
logPerformanceTrackingStop('CDS Compilation');
233229
logPerformanceMilestone('CDS compilation completed');
234-
logMemoryUsage('After CDS Compilation');
235230
} catch (error) {
236-
endPerformanceTracking('CDS Compilation');
231+
logPerformanceTrackingStop('CDS Compilation');
237232
cdsExtractorLog('error', `Compilation orchestration failed: ${String(error)}`);
238233

239234
// Add diagnostic for the overall failure
@@ -251,9 +246,9 @@ configureLgtmIndexFilters();
251246

252247
// Run CodeQL's JavaScript extractor to process the .cds source files and
253248
// the compiled .cds.json files.
254-
startPerformanceTracking('JavaScript Extraction');
249+
logPerformanceTrackingStart('JavaScript Extraction');
255250
const extractorResult = runJavaScriptExtractor(sourceRoot, autobuildScriptPath, codeqlExePath);
256-
endPerformanceTracking('JavaScript Extraction');
251+
logPerformanceTrackingStop('JavaScript Extraction');
257252

258253
if (!extractorResult.success && extractorResult.error) {
259254
cdsExtractorLog('error', `Error running JavaScript extractor: ${extractorResult.error}`);

extractors/cds/tools/src/cds/compiler/compile.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { CdsCompilationResult } from './types';
77
import { getCdsVersion } from './version';
88
import { fileExists, dirExists, recursivelyRenameJsonFiles } from '../../filesystem';
99
import { cdsExtractorLog } from '../../logging';
10-
import { CdsProject } from '../parser/types';
10+
import { BasicCdsProject } from '../parser/types';
1111

1212
/**
1313
* Compiles a CDS file to JSON using robust, project-aware compilation only.
@@ -24,7 +24,7 @@ import { CdsProject } from '../parser/types';
2424
*
2525
* @param cdsCommand The actual shell command to use for `cds compile`.
2626
* @param cacheDir Full path to the cache directory where dependencies are stored.
27-
* @param projectMap Map of project directories to {@link CdsProject} instances.
27+
* @param projectMap Map of project directories to {@link BasicCdsProject} instances.
2828
* @param projectDir The project directory to which `cdsFilePath` belongs.
2929
*
3030
* @returns The {@link CdsCompilationResult} of the compilation attempt.
@@ -34,7 +34,7 @@ export function compileCdsToJson(
3434
sourceRoot: string,
3535
cdsCommand: string,
3636
cacheDir: string | undefined,
37-
projectMap: Map<string, CdsProject>,
37+
projectMap: Map<string, BasicCdsProject>,
3838
projectDir: string,
3939
): CdsCompilationResult {
4040
try {
@@ -422,7 +422,10 @@ function createSpawnOptions(
422422
* @param relativePath The relative path of the file being checked
423423
* @returns true if the file should be compiled individually
424424
*/
425-
function shouldCompileIndividually(project: CdsProject | undefined, relativePath: string): boolean {
425+
function shouldCompileIndividually(
426+
project: BasicCdsProject | undefined,
427+
relativePath: string,
428+
): boolean {
426429
return project?.cdsFilesToCompile?.includes(relativePath) ?? true;
427430
}
428431

@@ -432,6 +435,6 @@ function shouldCompileIndividually(project: CdsProject | undefined, relativePath
432435
* @param project The CDS project to check
433436
* @returns true if project-level compilation should be used
434437
*/
435-
function shouldUseProjectLevelCompilation(project: CdsProject | undefined): boolean {
438+
function shouldUseProjectLevelCompilation(project: BasicCdsProject | undefined): boolean {
436439
return project?.cdsFilesToCompile?.includes('__PROJECT_LEVEL_COMPILATION__') ?? false;
437440
}

extractors/cds/tools/src/cds/compiler/graph.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import {
44
AlternativeCdsCommand,
55
CompilationAttempt,
66
CompilationTask,
7-
EnhancedCompilationConfig,
7+
CompilationConfig,
88
} from './types';
99
import { addCompilationDiagnostic } from '../../diagnostics';
1010
import { cdsExtractorLog } from '../../logging';
11-
import { CdsDependencyGraph, EnhancedCdsProject } from '../parser/types';
11+
import { CdsDependencyGraph, CdsProject } from '../parser/types';
1212

1313
/**
1414
* Attempt compilation with a specific command and configuration
@@ -43,11 +43,20 @@ function attemptCompilation(
4343
dependencyGraph.sourceRootDir,
4444
cdsCommand,
4545
cacheDir,
46-
// Convert enhanced projects back to the format expected by compileCdsToJson
46+
// Convert CDS projects to BasicCdsProject format expected by compileCdsToJson
4747
new Map(
4848
Array.from(dependencyGraph.projects.entries()).map(([key, value]) => [
4949
key,
50-
value as import('../parser/types').CdsProject,
50+
{
51+
cdsFiles: value.cdsFiles,
52+
cdsFilesToCompile: value.cdsFilesToCompile,
53+
expectedOutputFiles: value.expectedOutputFiles,
54+
projectDir: value.projectDir,
55+
dependencies: value.dependencies,
56+
imports: value.imports,
57+
packageJson: value.packageJson,
58+
compilationConfig: value.compilationConfig,
59+
},
5160
]),
5261
),
5362
task.projectDir,
@@ -105,14 +114,14 @@ function createCompilationTask(
105114
}
106115

107116
/**
108-
* Create enhanced compilation configuration with retry alternatives
117+
* Create compilation configuration with retry alternatives
109118
*/
110-
function createEnhancedCompilationConfig(
119+
function createCompilationConfig(
111120
primaryCommand: string,
112121
primaryCacheDir: string | undefined,
113122
useProjectLevel: boolean,
114123
alternatives: AlternativeCdsCommand[] = [],
115-
): EnhancedCompilationConfig {
124+
): CompilationConfig {
116125
return {
117126
primaryCdsCommand: primaryCommand,
118127
primaryCacheDir,
@@ -130,7 +139,7 @@ function createEnhancedCompilationConfig(
130139
*/
131140
function executeCompilationTask(
132141
task: CompilationTask,
133-
project: EnhancedCdsProject,
142+
project: CdsProject,
134143
dependencyGraph: CdsDependencyGraph,
135144
codeqlExePath: string,
136145
): void {
@@ -216,7 +225,7 @@ export function executeCompilationTasks(
216225
const compilationStartTime = new Date();
217226

218227
// Collect all tasks and sort by priority
219-
const allTasks: Array<{ task: CompilationTask; project: EnhancedCdsProject }> = [];
228+
const allTasks: Array<{ task: CompilationTask; project: CdsProject }> = [];
220229

221230
for (const project of dependencyGraph.projects.values()) {
222231
for (const task of project.compilationTasks) {
@@ -395,14 +404,14 @@ export function planCompilationTasks(
395404
// Determine primary CDS command
396405
const cdsCommand = determineCdsCommand(cacheDir, dependencyGraph.sourceRootDir);
397406

398-
// Create enhanced compilation configuration
399-
const enhancedConfig = createEnhancedCompilationConfig(
407+
// Create compilation configuration
408+
const compilationConfig = createCompilationConfig(
400409
cdsCommand,
401410
cacheDir,
402411
project.cdsFilesToCompile.includes('__PROJECT_LEVEL_COMPILATION__'),
403412
);
404413

405-
project.enhancedCompilationConfig = enhancedConfig;
414+
project.enhancedCompilationConfig = compilationConfig;
406415

407416
// Create compilation tasks
408417
if (project.cdsFilesToCompile.includes('__PROJECT_LEVEL_COMPILATION__')) {

extractors/cds/tools/src/cds/compiler/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ export type {
1414
CompilationAttempt,
1515
CompilationStatus,
1616
CompilationTask,
17-
EnhancedCompilationConfig,
17+
CompilationConfig,
1818
} from './types';
1919
export { getCdsVersion } from './version';

extractors/cds/tools/src/cds/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export interface CompilationTask {
101101
/**
102102
* Compilation configuration with retry alternatives
103103
*/
104-
export interface EnhancedCompilationConfig {
104+
export interface CompilationConfig {
105105
/** Primary CDS command to use */
106106
primaryCdsCommand: string;
107107
/** Primary cache directory */

0 commit comments

Comments
 (0)