Skip to content

Fix CDS extractor findPackageJsonDirs function #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions extractors/cds/tools/index-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if (!envSetupSuccess) {
process.exit(1);
}

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

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

// Install node dependencies in each directory.
console.log('Pre-installing required CDS compiler versions ...');
Expand Down
28 changes: 20 additions & 8 deletions extractors/cds/tools/src/packageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,32 @@ export interface PackageJson {
}

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

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

while (dir !== rootDir && rootDir !== dir) {
// Check until we reach the root directory
// Check current directory and parent directories for package.json with a
// dependency on `@sap/cds`. Never look above the source root directory.
while (true) {
// Stop if we've reached or gone above the source root directory.
if (absoluteSourceRoot && !dir.startsWith(absoluteSourceRoot)) {
break;
}

const packageJsonPath = join(dir, 'package.json');
if (existsSync(packageJsonPath)) {
try {
Expand Down
Loading