@@ -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