Skip to content

Commit 83bc1e4

Browse files
authored
Merge pull request #194 from data-douser/data-douser/cds-ts-rewrite-1-fixes
Fix CDS extractor `findPackageJsonDirs` function
2 parents 44b5a5a + 75426e1 commit 83bc1e4

File tree

3 files changed

+306
-70
lines changed

3 files changed

+306
-70
lines changed

extractors/cds/tools/index-files.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if (!envSetupSuccess) {
4242
process.exit(1);
4343
}
4444

45-
// Validate response file and get the fully paths of CDS files to process.
45+
// Validate response file and get the full paths of CDS files to process.
4646
const filePathsResult = getCdsFilePathsToProcess(responseFile, platformInfo);
4747
if (!filePathsResult.success) {
4848
console.warn(filePathsResult.errorMessage);
@@ -54,7 +54,8 @@ if (!filePathsResult.success) {
5454
const cdsFilePathsToProcess = filePathsResult.cdsFilePaths;
5555

5656
// Find all package.json directories that have a `@sap/cds` node dependency.
57-
const packageJsonDirs = findPackageJsonDirs(cdsFilePathsToProcess, codeqlExePath);
57+
// Pass the source root to prevent searching above it
58+
const packageJsonDirs = findPackageJsonDirs(cdsFilePathsToProcess, codeqlExePath, sourceRoot);
5859

5960
// Install node dependencies in each directory.
6061
console.log('Pre-installing required CDS compiler versions ...');

extractors/cds/tools/src/packageManager.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,32 @@ export interface PackageJson {
1414
}
1515

1616
/**
17-
* Find directories containing package.json with @sap/cds dependency
18-
* @param filePaths List of CDS file paths to check
19-
* @param codeqlExePath Path to the CodeQL executable (optional)
20-
* @returns Set of directories containing relevant package.json files
17+
* Find directories containing package.json with a `@sap/cds` dependency.
18+
* @param filePaths List of CDS file paths to check.
19+
* @param codeqlExePath Path to the CodeQL executable (optional).
20+
* @param sourceRoot The source root directory (optional) - Limits the search to
21+
* never go above this directory.
22+
* @returns Set of directories containing relevant package.json files.
2123
*/
22-
export function findPackageJsonDirs(filePaths: string[], codeqlExePath?: string): Set<string> {
24+
export function findPackageJsonDirs(
25+
filePaths: string[],
26+
codeqlExePath?: string,
27+
sourceRoot?: string,
28+
): Set<string> {
2329
const packageJsonDirs = new Set<string>();
30+
const absoluteSourceRoot = sourceRoot ? resolve(sourceRoot) : undefined;
2431

2532
filePaths.forEach(file => {
2633
let dir = dirname(resolve(file));
27-
const rootDir = dirname(dir); // Keep track of the root to avoid infinite loop
2834

29-
while (dir !== rootDir && rootDir !== dir) {
30-
// Check until we reach the root directory
35+
// Check current directory and parent directories for package.json with a
36+
// dependency on `@sap/cds`. Never look above the source root directory.
37+
while (true) {
38+
// Stop if we've reached or gone above the source root directory.
39+
if (absoluteSourceRoot && !dir.startsWith(absoluteSourceRoot)) {
40+
break;
41+
}
42+
3143
const packageJsonPath = join(dir, 'package.json');
3244
if (existsSync(packageJsonPath)) {
3345
try {

0 commit comments

Comments
 (0)