Skip to content

Commit a17b8c2

Browse files
committed
Set cwd for CDS JS autobuild process
Fixes the invocation of the javascript extractor autobuild within the index-files.js script of the CDS extractor. Ensures that the `cwd` property/option is set when spawning the process that runs the javascript extractor autobuild. Potential working version of initial rewrite for the CDS extractor.
1 parent d02234e commit a17b8c2

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

extractors/cds/tools/index-files.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { execFileSync, execSync, spawnSync } = require('child_process');
1+
const { execFileSync, spawnSync } = require('child_process');
22
const { existsSync, readFileSync, statSync } = require('fs');
33
const { arch, platform } = require('os');
44
const { dirname, join, resolve } = require('path');
@@ -13,6 +13,12 @@ const osPlatformArch = arch();
1313
console.log(`Detected OS platform=${osPlatform} : arch=${osPlatformArch}`);
1414
const codeqlExe = osPlatform === 'win32' ? 'codeql.exe' : 'codeql';
1515
const codeqlExePath = join(quote([process.env.CODEQL_DIST]), codeqlExe);
16+
const projectRootDir = resolve(`${dirname(__filename)}/../../..`);
17+
18+
if (!existsSync(projectRootDir)) {
19+
console.warn(`'${codeqlExe} database index-files --language cds' terminated early due to internal error: could not find project root directory '${projectRootDir}'.`);
20+
process.exit(0);
21+
}
1622

1723
let CODEQL_EXTRACTOR_JAVASCRIPT_ROOT = process.env.CODEQL_EXTRACTOR_JAVASCRIPT_ROOT
1824
? quote([process.env.CODEQL_EXTRACTOR_JAVASCRIPT_ROOT])
@@ -122,11 +128,7 @@ try {
122128
typeof packageJsonData.dependencies === 'object'
123129
) {
124130
const dependencyNames = Object.keys(packageJsonData.dependencies);
125-
if (
126-
dependencyNames.includes('@sap/cds')
127-
&&
128-
dependencyNames.includes('@sap/cds-dk')
129-
) {
131+
if (dependencyNames.includes('@sap/cds')) {
130132
packageJsonDirs.add(dir);
131133
break;
132134
}
@@ -138,14 +140,20 @@ try {
138140
}
139141
});
140142

141-
// TODO : revise this check as the equality is probably not guaranteed.
142-
if (responseFiles.length !== packageJsonDirs.size) {
143-
console.warn(
144-
`WARN: mismatch between number of response files (${responseFiles.length}) and package.json directories (${packageJsonDirs.length})`
145-
);
143+
// Sanity check that we found at least one package.json directory from which the CDS
144+
// compiler dependencies may be installed.
145+
if (packageJsonDirs.size === 0) {
146+
console.warn('WARN: failed to detect any package.json directories for cds compiler installation.');
147+
exit(0);
146148
}
147149

148150
packageJsonDirs.forEach((dir) => {
151+
console.log(`Installing '@sap/cds-dk' into ${dir} to enable CDS compilation.`);
152+
execFileSync(
153+
'npm',
154+
['install', '--quiet', '--no-audit', '--no-fund', '@sap/cds-dk'],
155+
{ cwd: dir, stdio: 'inherit' }
156+
);
149157
console.log(`Installing node packages into ${dir} to enable CDS compilation.`);
150158
execFileSync(
151159
'npm',
@@ -244,19 +252,26 @@ process.env.LGTM_INDEX_FILETYPES = '.cds:JSON';
244252
// refer to .js or .ts files.
245253
delete process.env.LGTM_INDEX_INCLUDE;
246254

247-
console.log('Extracting the .cds.json files');
248-
249-
console.log(`Running 'javascript' extractor autobuild script: ${autobuildScriptPath}`);
255+
console.log(
256+
`Extracting the .cds.json files by running the 'javascript' extractor autobuild script:
257+
${autobuildScriptPath}`
258+
);
250259
/**
251260
* Invoke the javascript autobuilder to index the .cds.json files only.
252261
*
253262
* Environment variables must be passed from this script's process to the
254263
* process that invokes the autobuild script, otherwise the CDS autobuild.sh
255264
* script will not be invoked by the autobuild script built into the
256265
* 'javascript' extractor.
266+
*
267+
* IMPORTANT: The JavaScript extractor autobuild script must be invoked with
268+
* the current working directory set to the project root directory because it
269+
* assumes it is running from there. Without the `cwd` property set to the
270+
* project root directory, the autobuild script will not detect the .cds.json
271+
* files as being in the project and will not index them.
257272
*/
258273
spawnSync(
259274
autobuildScriptPath,
260275
[],
261-
{ env: process.env, shell: true, stdio: 'inherit' }
276+
{ cwd: projectRootDir, env: process.env, shell: true, stdio: 'inherit' }
262277
);

0 commit comments

Comments
 (0)