Skip to content

Commit 0b6994c

Browse files
committed
Another attempted fix for index-files.js cwd
Changes the way the index-files.js script is invoked such that the original `--source-root` directory is used, where possible, as the current working directory for any work performed with within the extractor. Passes the original working directory as a parameter of the index-files.js script to allow that (child) script to run from the project/source root while ensuring node package dependencies are still installed in `extractors/cds/tools/node_modules`.
1 parent a17b8c2 commit 0b6994c

File tree

5 files changed

+60
-33
lines changed

5 files changed

+60
-33
lines changed

extractors/cds/tools/autobuild.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
set -eu
44

5-
# NOTE: the code below is copied in three places:
6-
# - scripts/compile-cds.sh
7-
# - extractors/cds/tools/autobuild.sh (here)
8-
# - extractors/javascript/tools/pre-finalize.sh
9-
# Any changes should be synchronized between these three places.
10-
115
exec "${CODEQL_DIST}/codeql" database index-files \
126
--include-extension=.cds \
137
--language cds \

extractors/cds/tools/index-files.cmd

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,36 @@ if %ERRORLEVEL% neq 0 (
1919

2020
set "_response_file_path=%~1"
2121
set "_script_dir=%~dp0"
22+
REM Set _cwd before changing the working directory to the script directory.
23+
set "_cwd=%CD%"
2224

2325
echo Checking response file for CDS files to index
2426

27+
REM Terminate early if the _response_file_path doesn't exist or is empty,
28+
REM which indicates that no CDS files were selected or found.
2529
if not exist "%_response_file_path%" (
2630
echo 'codeql database index-files --language cds' command terminated early as response file '%_response_file_path%' does not exist or is empty. This is because no CDS files were selected or found.
2731
exit /b 0
2832
)
2933

30-
REM Change to the directory of this script to ensure that npm looks up
31-
REM the package.json file in the correct directory and installs the
32-
REM dependencies (i.e. node_modules) relative to this directory.
34+
REM Change to the directory of this script to ensure that npm looks up the
35+
REM package.json file in the correct directory and installs the dependencies
36+
REM (i.e. node_modules) relative to this directory. This is technically a
37+
REM violation of the assumption that extractor scripts will be run with the
38+
REM current working directory set to the root of the project source, but we
39+
REM also need node_modules to be installed here and not in the project source
40+
REM root, so we make a compromise of:
41+
REM 1. changing to this script's directory;
42+
REM 2. installing node dependencies here;
43+
REM 3. passing the original working directory as a parameter to the
44+
REM index-files.js script;
45+
REM 4. expecting the index-files.js script to immediately change back to
46+
REM the original working (aka the project source root) directory.
47+
3348
cd /d "%_script_dir%" && ^
34-
echo Installing node package dependencies and running the 'index-files.js' script && ^
35-
npm install --quiet --no-audit --no-fund --no-package-json && ^
36-
node "%_script_dir%index-files.js" "%_response_file_path%"
49+
echo Installing node package dependencies && ^
50+
npm install --quiet --no-audit --no-fund && ^
51+
echo Running the 'index-files.js' script && ^
52+
node "%_script_dir%index-files.js" "%_response_file_path%" "%_cwd%"
3753

3854
exit /b %ERRORLEVEL%

extractors/cds/tools/index-files.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,29 @@ const { arch, platform } = require('os');
44
const { dirname, join, resolve } = require('path');
55
const { quote } = require('shell-quote');
66

7-
console.log('Indexing CDS files');
7+
// Terminate early if this script is not invoked with the required arguments.
8+
if (process.argv.length !== 4) {
9+
console.warn(`Usage: node index-files.js <response-file> <source-root>`);
10+
process.exit(0);
11+
}
812

913
const responseFile = process.argv[2];
14+
const sourceRoot = process.argv[3];
15+
16+
// Force this script, and any process it spawns, to use the project (source)
17+
// root directory as the current working directory.
18+
process.chdir(sourceRoot);
19+
20+
console.log(`Indexing CDS files in project source directory: ${sourceRoot}`);
1021

1122
const osPlatform = platform();
1223
const osPlatformArch = arch();
1324
console.log(`Detected OS platform=${osPlatform} : arch=${osPlatformArch}`);
1425
const codeqlExe = osPlatform === 'win32' ? 'codeql.exe' : 'codeql';
1526
const codeqlExePath = join(quote([process.env.CODEQL_DIST]), codeqlExe);
16-
const projectRootDir = resolve(`${dirname(__filename)}/../../..`);
1727

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}'.`);
28+
if (!existsSync(sourceRoot)) {
29+
console.warn(`'${codeqlExe} database index-files --language cds' terminated early due to internal error: could not find project root directory '${sourceRoot}'.`);
2030
process.exit(0);
2131
}
2232

@@ -265,13 +275,15 @@ console.log(
265275
* 'javascript' extractor.
266276
*
267277
* 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.
278+
* the current working directory set to the project (source) root directory
279+
* because it assumes it is running from there. The JavaScript extractor will
280+
* only find the .cds files to index (to the database) if those file are
281+
* relative to where the autobuild script is invoked from, which should be the
282+
* same as the `--source-root` argument passed to the `codeql database create`
283+
* command.
272284
*/
273285
spawnSync(
274286
autobuildScriptPath,
275287
[],
276-
{ cwd: projectRootDir, env: process.env, shell: true, stdio: 'inherit' }
288+
{ cwd: sourceRoot, env: process.env, shell: true, stdio: 'inherit' }
277289
);

extractors/cds/tools/index-files.sh

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ then
2020
exit 3
2121
fi
2222

23+
_cwd="$PWD"
2324
_response_file_path="$1"
2425
_script_dir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
2526

@@ -36,11 +37,22 @@ then
3637
exit 0
3738
fi
3839

39-
# Change to the directory of this script to ensure that npm looks up
40-
# the package.json file in the correct directory and installs the
41-
# dependencies (i.e. node_modules) relative to this directory.
40+
# Change to the directory of this script to ensure that npm looks up the
41+
# package.json file in the correct directory and installs the dependencies
42+
# (i.e. node_modules) relative to this directory. This is technically a
43+
# violation of the assumption that extractor scripts will be run with the
44+
# current working directory set to the root of the project source, but we
45+
# also need node_modules to be installed here and not in the project source
46+
# root, so we make a compromise of:
47+
# 1. changing to this script's directory;
48+
# 2. installing node dependencies here;
49+
# 3. passing the original working directory as a parameter to the
50+
# index-files.js script;
51+
# 4. expecting the index-files.js script to immediately change back to
52+
# the original working (aka the project source root) directory.
53+
4254
cd "$_script_dir" && \
4355
echo "Installing node package dependencies" && \
4456
npm install --quiet --no-audit --no-fund && \
4557
echo "Running the 'index-files.js' script" && \
46-
node "$(dirname "$0")/index-files.js" "$_response_file_path"
58+
node "$(dirname "$0")/index-files.js" "$_response_file_path" "${_cwd}"

extractors/javascript/tools/pre-finalize.sh

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22

33
set -eu
44

5-
# NOTE: the code below is copied in three places:
6-
# - scripts/compile-cds.sh
7-
# - extractors/cds/tools/autobuild.sh
8-
# - extractors/javascript/tools/pre-finalize.sh (here)
9-
# Any changes should be synchronized between these three places.
10-
11-
# Do not extract CDS files if the
12-
# CODEQL_EXTRACTOR_CDS_SKIP_EXTRACTION
5+
# Do not extract CDS files if the CODEQL_EXTRACTOR_CDS_SKIP_EXTRACTION
136
# environment variable is set.
147
if [ -z "${CODEQL_EXTRACTOR_CDS_SKIP_EXTRACTION:-}" ]; then
158
# Call the index-files command with the CDS extractor

0 commit comments

Comments
 (0)