Skip to content

Commit 2c5761a

Browse files
Merge pull request #223 from data-douser/cds-compile-retry
Use CDS extractor diagnostics instead of exit error codes
2 parents eaf18e2 + 9837fb4 commit 2c5761a

File tree

7 files changed

+496
-99
lines changed

7 files changed

+496
-99
lines changed

extractors/cds/tools/cds-extractor.ts

Lines changed: 127 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import { sync as globSync } from 'glob';
55
import { orchestrateCompilation } from './src/cds/compiler';
66
import { buildCdsProjectDependencyGraph } from './src/cds/parser';
77
import { runJavaScriptExtractor } from './src/codeql';
8-
import { addCompilationDiagnostic } from './src/diagnostics';
8+
import {
9+
addCompilationDiagnostic,
10+
addDependencyGraphDiagnostic,
11+
addDependencyInstallationDiagnostic,
12+
addEnvironmentSetupDiagnostic,
13+
addJavaScriptExtractorDiagnostic,
14+
addNoCdsProjectsDiagnostic,
15+
} from './src/diagnostics';
916
import { configureLgtmIndexFilters, setupAndValidateEnvironment } from './src/environment';
1017
import {
1118
cdsExtractorLog,
@@ -24,8 +31,13 @@ import { validateArguments } from './src/utils';
2431
const validationResult = validateArguments(process.argv);
2532
if (!validationResult.isValid) {
2633
console.warn(validationResult.usageMessage);
27-
// Exit with an error code on invalid use of this script.
28-
process.exit(1);
34+
// For invalid arguments, we can't proceed but we also can't add diagnostics since we don't have
35+
// the necessary context (sourceRoot, codeqlExePath). Log the issue and exit gracefully.
36+
console.log(
37+
`CDS extractor terminated due to invalid arguments: ${validationResult.usageMessage}`,
38+
);
39+
console.log(`Completed run of the cds-extractor.js script for the CDS extractor.`);
40+
process.exit(0); // Use exit code 0 to not fail the overall JavaScript extractor
2941
}
3042

3143
// Get the validated and sanitized arguments.
@@ -52,21 +64,28 @@ logPerformanceTrackingStop('Environment Setup');
5264

5365
if (!envSetupSuccess) {
5466
const codeqlExe = platformInfo.isWindows ? 'codeql.exe' : 'codeql';
55-
cdsExtractorLog(
56-
'warn',
57-
`'${codeqlExe} database index-files --language cds' terminated early due to: ${errorMessages.join(
58-
', ',
59-
)}.`,
67+
const errorMessage = `'${codeqlExe} database index-files --language cds' terminated early due to: ${errorMessages.join(
68+
', ',
69+
)}.`;
70+
71+
cdsExtractorLog('warn', errorMessage);
72+
73+
// Add diagnostic for environment setup failure if we have a codeqlExePath
74+
if (codeqlExePath) {
75+
addEnvironmentSetupDiagnostic(sourceRoot, errorMessage, codeqlExePath);
76+
}
77+
78+
// Continue with a warning instead of exiting - let JavaScript extractor proceed
79+
logExtractorStop(
80+
false,
81+
'Warning: Environment setup failed, continuing with limited functionality',
6082
);
61-
// Exit with an error code when environment setup fails.
62-
logExtractorStop(false, 'Terminated: Environment setup failed');
63-
process.exit(1);
83+
} else {
84+
// Force this script, and any process it spawns, to use the project (source) root
85+
// directory as the current working directory.
86+
process.chdir(sourceRoot);
6487
}
6588

66-
// Force this script, and any process it spawns, to use the project (source) root
67-
// directory as the current working directory.
68-
process.chdir(sourceRoot);
69-
7089
cdsExtractorLog(
7190
'info',
7291
`CodeQL CDS extractor using autobuild mode for scan of project source root directory '${sourceRoot}'.`,
@@ -139,15 +158,80 @@ try {
139158
cdsExtractorLog('warn', `Could not perform direct CDS file search: ${String(globError)}`);
140159
}
141160

142-
// Exit early since we have no CDS projects to process
143-
logExtractorStop(false, 'Terminated: No CDS projects detected');
144-
process.exit(1);
161+
// Add diagnostic warning for no CDS projects detected
162+
const warningMessage =
163+
'No CDS projects were detected. This may be expected if the source does not contain CAP/CDS projects.';
164+
if (codeqlExePath) {
165+
addNoCdsProjectsDiagnostic(sourceRoot, warningMessage, codeqlExePath);
166+
}
167+
168+
// Continue instead of exiting - let JavaScript extractor proceed with non-CDS files
169+
logExtractorStop(false, 'Warning: No CDS projects detected, skipping CDS-specific processing');
170+
171+
// Skip the rest of CDS processing and go directly to JavaScript extraction
172+
configureLgtmIndexFilters();
173+
174+
// Run CodeQL's JavaScript extractor to process any remaining files
175+
logPerformanceTrackingStart('JavaScript Extraction');
176+
const extractorResult = runJavaScriptExtractor(
177+
sourceRoot,
178+
autobuildScriptPath || '', // Use empty string if autobuildScriptPath is undefined
179+
codeqlExePath,
180+
);
181+
logPerformanceTrackingStop('JavaScript Extraction');
182+
183+
if (!extractorResult.success && extractorResult.error) {
184+
cdsExtractorLog('error', `Error running JavaScript extractor: ${extractorResult.error}`);
185+
if (codeqlExePath) {
186+
addJavaScriptExtractorDiagnostic(sourceRoot, extractorResult.error, codeqlExePath);
187+
}
188+
logExtractorStop(false, 'JavaScript extractor failed');
189+
} else {
190+
logExtractorStop(true, 'JavaScript extraction completed (CDS processing was skipped)');
191+
}
192+
193+
console.log(`Completed run of the cds-extractor.js script for the CDS extractor.`);
194+
process.exit(0); // Graceful exit to skip the rest of the processing
145195
}
146196
} catch (error) {
147-
cdsExtractorLog('error', `Failed to build CDS dependency graph: ${String(error)}`);
148-
// Exit with error since we can't continue without a proper dependency graph
149-
logExtractorStop(false, 'Terminated: Dependency graph build failed');
150-
process.exit(1);
197+
const errorMessage = `Failed to build CDS dependency graph: ${String(error)}`;
198+
cdsExtractorLog('error', errorMessage);
199+
200+
// Add diagnostic for dependency graph build failure
201+
if (codeqlExePath) {
202+
addDependencyGraphDiagnostic(sourceRoot, errorMessage, codeqlExePath);
203+
}
204+
205+
// Continue with a warning instead of exiting - let JavaScript extractor proceed with non-CDS files
206+
logExtractorStop(
207+
false,
208+
'Warning: Dependency graph build failed, skipping CDS-specific processing',
209+
);
210+
211+
// Skip the rest of CDS processing and go directly to JavaScript extraction
212+
configureLgtmIndexFilters();
213+
214+
// Run CodeQL's JavaScript extractor to process any remaining files
215+
logPerformanceTrackingStart('JavaScript Extraction');
216+
const extractorResult = runJavaScriptExtractor(
217+
sourceRoot,
218+
autobuildScriptPath || '', // Use empty string if autobuildScriptPath is undefined
219+
codeqlExePath,
220+
);
221+
logPerformanceTrackingStop('JavaScript Extraction');
222+
223+
if (!extractorResult.success && extractorResult.error) {
224+
cdsExtractorLog('error', `Error running JavaScript extractor: ${extractorResult.error}`);
225+
if (codeqlExePath) {
226+
addJavaScriptExtractorDiagnostic(sourceRoot, extractorResult.error, codeqlExePath);
227+
}
228+
logExtractorStop(false, 'JavaScript extractor failed');
229+
} else {
230+
logExtractorStop(true, 'JavaScript extraction completed (CDS processing was skipped)');
231+
}
232+
233+
console.log(`Completed run of the cds-extractor.js script for the CDS extractor.`);
234+
process.exit(0); // Graceful exit to skip the rest of the processing
151235
}
152236

153237
logPerformanceTrackingStart('Dependency Installation');
@@ -163,12 +247,19 @@ if (projectCacheDirMap.size === 0) {
163247

164248
// This is a critical error if we have projects but no cache mappings
165249
if (dependencyGraph.projects.size > 0) {
166-
cdsExtractorLog(
167-
'error',
168-
`Found ${dependencyGraph.projects.size} CDS projects but failed to install dependencies for any of them. Cannot proceed with compilation.`,
250+
const errorMessage = `Found ${dependencyGraph.projects.size} CDS projects but failed to install dependencies for any of them. Cannot proceed with compilation.`;
251+
cdsExtractorLog('error', errorMessage);
252+
253+
// Add diagnostic for dependency installation failure
254+
if (codeqlExePath) {
255+
addDependencyInstallationDiagnostic(sourceRoot, errorMessage, codeqlExePath);
256+
}
257+
258+
// Continue with a warning instead of exiting - let JavaScript extractor proceed
259+
logExtractorStop(
260+
false,
261+
'Warning: Dependency installation failed for all projects, continuing with limited functionality',
169262
);
170-
logExtractorStop(false, 'Terminated: Dependency installation failed for all projects');
171-
process.exit(1);
172263
}
173264

174265
// If we have no projects and no cache mappings, this should have been caught earlier
@@ -253,6 +344,15 @@ dependencyGraph.statusSummary.performance.totalDurationMs = totalDuration;
253344

254345
if (!extractorResult.success && extractorResult.error) {
255346
cdsExtractorLog('error', `Error running JavaScript extractor: ${extractorResult.error}`);
347+
348+
// Add diagnostic for JavaScript extractor failure
349+
if (codeqlExePath && dependencyGraph.projects.size > 0) {
350+
// Use the first CDS file as a representative file for the diagnostic
351+
const firstProject = Array.from(dependencyGraph.projects.values())[0];
352+
const representativeFile = firstProject.cdsFiles[0] || sourceRoot;
353+
addJavaScriptExtractorDiagnostic(representativeFile, extractorResult.error, codeqlExePath);
354+
}
355+
256356
logExtractorStop(false, 'JavaScript extractor failed');
257357
} else {
258358
logExtractorStop(true, 'CDS extraction completed successfully');

0 commit comments

Comments
 (0)