Skip to content

Commit cc99914

Browse files
committed
index-files.js must compiles cds to file (not dir)
Forces the `cds` compilier to output JSON to a file (via stdout) instead of creating an output directory. Accounts for what appears to be a change in the behavior of the `cds` (CLI) compiler, where the `-o` (or `--dest`) option now `Writes output to the given folder instead of stdout`. ``` $ npx cds --version @cap-js/asyncapi: 1.0.2 @cap-js/db-service: 1.16.0 @cap-js/openapi: 1.1.2 @cap-js/sqlite: 1.7.7 @capire/bookshop: 1.0.0 @sap/cds: 8.5.0 @sap/cds-compiler: 5.2.0 @sap/cds-dk: 8.7.1 @sap/cds-fiori: 1.2.7 @sap/cds-foss: 5.0.1 @sap/cds-mtxs: 2.5.1 @sap/eslint-plugin-cds: 3.1.2 Node.js: v20.15.0 ```
1 parent 212820a commit cc99914

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

extractors/cds/tools/index-files.js

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { execFileSync, spawnSync } = require('child_process');
2-
const { existsSync, readFileSync, statSync } = require('fs');
2+
const { existsSync, readFileSync, statSync, writeFileSync } = require('fs');
33
const { arch, platform } = require('os');
4-
const { dirname, join, resolve } = require('path');
4+
const { basename, dirname, join, resolve } = require('path');
55
const { quote } = require('shell-quote');
66

77
// Terminate early if this script is not invoked with the required arguments.
@@ -23,7 +23,7 @@ const osPlatform = platform();
2323
const osPlatformArch = arch();
2424
console.log(`Detected OS platform=${osPlatform} : arch=${osPlatformArch}`);
2525
const codeqlExe = osPlatform === 'win32' ? 'codeql.exe' : 'codeql';
26-
const codeqlExePath = join(quote([process.env.CODEQL_DIST]), codeqlExe);
26+
const codeqlExePath = resolve(join(quote([process.env.CODEQL_DIST]), codeqlExe));
2727

2828
if (!existsSync(sourceRoot)) {
2929
console.warn(`'${codeqlExe} database index-files --language cds' terminated early due to internal error: could not find project root directory '${sourceRoot}'.`);
@@ -61,9 +61,9 @@ if (!CODEQL_EXTRACTOR_JAVASCRIPT_ROOT) {
6161
}
6262

6363
const autobuildScriptName = osPlatform === 'win32' ? 'autobuild.cmd' : 'autobuild.sh';
64-
const autobuildScriptPath = join(
64+
const autobuildScriptPath = resolve(join(
6565
CODEQL_EXTRACTOR_JAVASCRIPT_ROOT, 'tools', autobuildScriptName
66-
);
66+
));
6767

6868
/**
6969
* Terminate early if:
@@ -104,7 +104,7 @@ let cdsCommand = 'cds';
104104
try {
105105
execFileSync('cds', ['--version'], { stdio: 'ignore' });
106106
} catch {
107-
console.log('Pre-installing cds compiler');
107+
console.log('Pre-installing cds compiler ...');
108108

109109
// Use a JS `Set` to avoid duplicate processing of the same directory.
110110
const packageJsonDirs = new Set();
@@ -156,18 +156,18 @@ try {
156156
// Sanity check that we found at least one package.json directory from which the CDS
157157
// compiler dependencies may be installed.
158158
if (packageJsonDirs.size === 0) {
159-
console.warn('WARN: failed to detect any package.json directories for cds compiler installation.');
159+
console.warn('WARN: failed to detect any package.json directories for cds compiler installation.');
160160
exit(0);
161161
}
162162

163163
packageJsonDirs.forEach((dir) => {
164-
console.log(`Installing '@sap/cds-dk' into ${dir} to enable CDS compilation.`);
164+
console.log(`Installing '@sap/cds-dk' into ${dir} to enable CDS compilation ...`);
165165
execFileSync(
166166
'npm',
167-
['install', '--quiet', '--no-audit', '--no-fund', '@sap/cds-dk'],
167+
['install', '--quiet', '--no-audit', '--no-fund', '--no-save', '@sap/cds-dk'],
168168
{ cwd: dir, stdio: 'inherit' }
169169
);
170-
console.log(`Installing node packages into ${dir} to enable CDS compilation.`);
170+
console.log(`Installing node packages into ${dir} to enable CDS compilation ...`);
171171
execFileSync(
172172
'npm',
173173
['install', '--quiet', '--no-audit', '--no-fund'],
@@ -183,7 +183,7 @@ try {
183183
cdsCommand = 'npx -y --package @sap/cds-dk cds';
184184
}
185185

186-
console.log('Processing CDS files to JSON');
186+
console.log('Processing CDS files to JSON ...');
187187

188188
/**
189189
* Run the cds compile command on each file in the response files list, outputting the
@@ -192,33 +192,44 @@ console.log('Processing CDS files to JSON');
192192
responseFiles.forEach(rawCdsFilePath => {
193193
const cdsFilePath = quote([rawCdsFilePath]);
194194
const cdsJsonFilePath = `${cdsFilePath}.json`;
195-
console.log(`Processing CDS file ${cdsFilePath} to: ${cdsJsonFilePath}`);
195+
console.log(`Processing CDS file ${cdsFilePath} to ${cdsJsonFilePath} ...`);
196196
const result = spawnSync(
197197
cdsCommand,
198-
['compile', cdsFilePath, '-2', 'json', '-o', cdsJsonFilePath, '--locations'],
199-
{ shell: true }
198+
[
199+
'compile', cdsFilePath,
200+
'-2', 'json',
201+
'--locations',
202+
'--log-level', 'warn'
203+
],
204+
{ cwd: dirname(cdsFilePath), shell: true, stdio: 'pipe' }
200205
);
201-
if (result.error || result.status !== 0) {
202-
const stderrTruncated = result.stderr.toString().split('\n').filter(line => line.startsWith('[ERROR]')).slice(-4).join('\n');
203-
const errorMessage = `Could not compile the file ${cdsFilePath}.\nReported error(s):\n\`\`\`\n${stderrTruncated}\n\`\`\``;
206+
if (result.error || result.status !== 0 || !result.stdout) {
207+
const errorMessage = `Could not compile the file ${cdsFilePath}.\nReported error(s):\n\`\`\`\n${result.stderr.toString()}\n\`\`\``;
204208
console.log(errorMessage);
205-
execFileSync(
206-
codeqlExePath,
207-
[
208-
'database',
209-
'add-diagnostic',
210-
'--extractor-name=cds',
211-
'--ready-for-status-page',
212-
'--source-id=cds/compilation-failure',
213-
'--source-name="Failure to compile one or more SAP CAP CDS files"',
214-
'--severity=error',
215-
`--markdown-message="${errorMessage}"`,
216-
`--file-path="${cdsFilePath}"`,
217-
'--',
218-
`${process.env.CODEQL_EXTRACTOR_CDS_WIP_DATABASE}`
219-
],
220-
);
209+
try {
210+
execFileSync(
211+
codeqlExePath,
212+
[
213+
'database',
214+
'add-diagnostic',
215+
'--extractor-name=cds',
216+
'--ready-for-status-page',
217+
'--source-id=cds/compilation-failure',
218+
'--source-name="Failure to compile one or more SAP CAP CDS files"',
219+
'--severity=error',
220+
`--markdown-message="${errorMessage}"`,
221+
`--file-path="${cdsFilePath}"`,
222+
'--',
223+
`${process.env.CODEQL_EXTRACTOR_CDS_WIP_DATABASE}`
224+
],
225+
);
226+
console.log(`Added error diagnostic for source file: ${cdsFilePath}`);
227+
} catch (err) {
228+
console.error(`Failed to add error diagnostic for source file=${cdsFilePath} : ${err}`);
229+
}
221230
}
231+
// Write the compiled JSON result to cdsJsonFilePath.
232+
writeFileSync(cdsJsonFilePath, result.stdout);
222233
});
223234

224235
let excludeFilters = '';

0 commit comments

Comments
 (0)