Skip to content

Commit f9e41aa

Browse files
committed
Fix CDS extractor environment setup
Fixes the setup of the CDS extractor environment to ensure that the codeql CLI can be reliably found and to avoid duplicate runs of the CDS parser's graph building process for "debug-parser" versus other run modes.
1 parent ea19649 commit f9e41aa

File tree

6 files changed

+503
-239
lines changed

6 files changed

+503
-239
lines changed

extractors/cds/tools/cds-extractor.ts

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
compileCdsToJson,
44
determineCdsCommand,
55
findProjectForCdsFile,
6-
writeParserDebugInfo,
76
} from './src/cds';
7+
import { CdsProjectMapWithDebugSignals } from './src/cds/parser/types';
88
import { runJavaScriptExtractor } from './src/codeql';
99
import { addCompilationDiagnostic } from './src/diagnostics';
1010
import { configureLgtmIndexFilters, setupAndValidateEnvironment } from './src/environment';
@@ -25,42 +25,14 @@ if (!validationResult.isValid) {
2525
// Get the validated and sanitized arguments
2626
const { runMode, sourceRoot, responseFile } = validationResult.args!;
2727

28-
// Handle debug parser mode
29-
if (runMode === (RunMode.DEBUG_PARSER as string)) {
30-
console.log('Running CDS Parser in debug mode...');
31-
console.log(`Source Root Directory: ${sourceRoot}`);
32-
33-
// Use the project-aware parser as the main entry point
34-
const scriptDir = __dirname;
35-
const projectMap = buildCdsProjectDependencyGraph(sourceRoot, runMode, scriptDir);
36-
37-
if (projectMap.size === 0) {
38-
console.warn('No CDS projects found. Cannot generate debug information.');
39-
process.exit(1);
40-
}
41-
42-
// Check if debug information was successfully written
43-
if (runMode === 'debug-parser' && !writeParserDebugInfo(projectMap, sourceRoot, scriptDir)) {
44-
console.warn('Failed to write debug information.');
45-
process.exit(1);
46-
}
47-
48-
console.log('Debug parser process completed successfully.');
49-
process.exit(0);
50-
} else if (runMode === (RunMode.AUTOBUILD as string)) {
28+
// Check for autobuild mode
29+
if (runMode === (RunMode.AUTOBUILD as string)) {
5130
console.log('Autobuild mode is not implemented yet.');
5231
process.exit(1);
5332
}
5433

55-
// Force this script, and any process it spawns, to use the project (source) root
56-
// directory as the current working directory.
57-
process.chdir(sourceRoot);
58-
59-
console.log(
60-
`INFO: CodeQL CDS extractor using run mode '${runMode}' for scan of project source root directory '${sourceRoot}'.`,
61-
);
62-
63-
// Setup the environment and validate all requirements.
34+
// Setup the environment and validate all requirements first, before changing directory
35+
// This ensures we can properly locate the CodeQL tools
6436
const {
6537
success: envSetupSuccess,
6638
errorMessages,
@@ -80,6 +52,16 @@ if (!envSetupSuccess) {
8052
process.exit(1);
8153
}
8254

55+
// Force this script, and any process it spawns, to use the project (source) root
56+
// directory as the current working directory.
57+
process.chdir(sourceRoot);
58+
59+
console.log(
60+
`INFO: CodeQL CDS extractor using run mode '${runMode}' for scan of project source root directory '${sourceRoot}'.`,
61+
);
62+
console.log(`DEBUG: CodeQL executable path: ${codeqlExePath}`);
63+
console.log(`DEBUG: Current working directory: ${process.cwd()}`);
64+
8365
// Only process response file for INDEX_FILES mode
8466
let cdsFilePathsToProcess: string[] = [];
8567
if (runMode === (RunMode.INDEX_FILES as string)) {
@@ -99,10 +81,25 @@ if (runMode === (RunMode.INDEX_FILES as string)) {
9981
console.log('Detecting CDS projects and analyzing their structure...');
10082

10183
// Build the project dependency graph using the project-aware parser
102-
const projectMap = buildCdsProjectDependencyGraph(sourceRoot, runMode);
84+
// Pass the script directory (__dirname) to support debug-parser mode internally
85+
const projectMap = buildCdsProjectDependencyGraph(sourceRoot, runMode, __dirname);
86+
87+
// Cast to the interface with debug signals to properly handle debug mode
88+
const typedProjectMap = projectMap as CdsProjectMapWithDebugSignals;
89+
90+
// Check if we're in debug-parser mode and should exit (based on signals from buildCdsProjectDependencyGraph)
91+
if (typedProjectMap.__debugParserSuccess) {
92+
console.log('Debug parser mode completed successfully.');
93+
process.exit(0);
94+
} else if (typedProjectMap.__debugParserFailure) {
95+
console.warn('No CDS projects found. Cannot generate debug information.');
96+
process.exit(1);
97+
}
10398

10499
// Install dependencies using the new caching approach
105100
console.log('Installing required CDS compiler versions using cached approach...');
101+
console.log(`DEBUG: Before installDependencies - CodeQL path: ${codeqlExePath}`);
102+
console.log(`DEBUG: Current working directory: ${process.cwd()}`);
106103
const projectCacheDirMap = installDependencies(projectMap, sourceRoot, codeqlExePath);
107104

108105
// Determine the CDS command to use.
@@ -140,6 +137,7 @@ for (const rawCdsFilePath of cdsFilePathsToProcess) {
140137
// Configure the "LGTM" index filters for proper extraction.
141138
configureLgtmIndexFilters();
142139

140+
console.log(`DEBUG: CODEQL_DIST before runJavaScriptExtractor: ${process.env.CODEQL_DIST}`);
143141
// Run CodeQL's JavaScript extractor to process the compiled JSON files.
144142
const extractorResult = runJavaScriptExtractor(sourceRoot, autobuildScriptPath, codeqlExePath);
145143
if (!extractorResult.success && extractorResult.error) {

extractors/cds/tools/src/cds/parser/debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export function writeParserDebugInfo(
113113
// cat/read when debugging.
114114
writeFileSync(debugFilePath, `${debugContent}\n`, 'utf-8');
115115

116-
console.log(`Parser debug information written to: ${debugFilePath}`);
116+
console.log(`INFO: CDS extractor parser debug information written to: ${debugFilePath}`);
117117
return true;
118118
} catch (error: unknown) {
119119
console.error(`Error writing parser debug information: ${String(error)}`);

extractors/cds/tools/src/cds/parser/functions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export function buildCdsProjectDependencyGraph(
3939
runMode?: string,
4040
scriptDir?: string,
4141
): Map<string, CdsProject> {
42+
// If debug-parser mode, log additional information
43+
if (runMode === 'debug-parser') {
44+
console.log('Running CDS Parser in debug mode...');
45+
console.log(`Source Root Directory: ${sourceRootDir}`);
46+
}
47+
4248
// Find all CDS projects under the source directory
4349
console.log('Detecting CDS projects...');
4450
const projectDirs = determineCdsProjectsUnderSourceDir(sourceRootDir);
@@ -145,6 +151,13 @@ export function buildCdsProjectDependencyGraph(
145151
'Failed to write parser debug information. This indicates an empty project map, possibly due to a misconfiguration when calling the parent script.',
146152
);
147153
}
154+
155+
// Instead of exiting directly (which interrupts tests), return with a signal property
156+
if (projectMap.size === 0) {
157+
return Object.assign(projectMap, { __debugParserFailure: true });
158+
} else {
159+
return Object.assign(projectMap, { __debugParserSuccess: true });
160+
}
148161
}
149162

150163
return projectMap;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ export interface CdsProject {
176176
projectDir: string;
177177
}
178178

179+
/** Extended CdsProject map with debug signals */
180+
export interface CdsProjectMapWithDebugSignals extends Map<string, CdsProject> {
181+
/** Signal for successful debug parser operation */
182+
__debugParserSuccess?: boolean;
183+
184+
/** Signal for failed debug parser operation */
185+
__debugParserFailure?: boolean;
186+
}
187+
179188
/** Represents a CDS service definition. */
180189
export interface CdsService {
181190
/** Annotations attached to the service. */

0 commit comments

Comments
 (0)