|
| 1 | +/** Full dependency installation utilities for retry scenarios. */ |
| 2 | + |
| 3 | +import { execFileSync } from 'child_process'; |
| 4 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'; |
| 5 | +import { join } from 'path'; |
| 6 | + |
| 7 | +import { cdsExtractorLog } from '../../logging'; |
| 8 | +import type { CdsProject } from '../parser/types'; |
| 9 | + |
| 10 | +/** Result of full dependency installation for a project */ |
| 11 | +export interface FullDependencyInstallationResult { |
| 12 | + /** Whether installation was successful */ |
| 13 | + success: boolean; |
| 14 | + /** Path to the retry cache directory */ |
| 15 | + retryCacheDir: string; |
| 16 | + /** Installation error message if failed */ |
| 17 | + error?: string; |
| 18 | + /** Warnings during installation */ |
| 19 | + warnings: string[]; |
| 20 | + /** Duration of installation in milliseconds */ |
| 21 | + durationMs: number; |
| 22 | + /** Whether a timeout occurred */ |
| 23 | + timedOut: boolean; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Installs full dependencies for a project using its package.json |
| 28 | + * @param project The CDS project to install dependencies for |
| 29 | + * @param sourceRoot Source root directory |
| 30 | + * @param codeqlExePath Path to the CodeQL executable for diagnostics |
| 31 | + * @returns Installation result with details |
| 32 | + */ |
| 33 | +export function installFullDependencies( |
| 34 | + project: CdsProject, |
| 35 | + sourceRoot: string, |
| 36 | + _codeqlExePath: string, |
| 37 | +): FullDependencyInstallationResult { |
| 38 | + const startTime = Date.now(); |
| 39 | + |
| 40 | + const result: FullDependencyInstallationResult = { |
| 41 | + success: false, |
| 42 | + retryCacheDir: '', |
| 43 | + warnings: [], |
| 44 | + durationMs: 0, |
| 45 | + timedOut: false, |
| 46 | + }; |
| 47 | + |
| 48 | + try { |
| 49 | + // Create retry-specific cache directory |
| 50 | + const retryCacheDir = createRetryCacheDirectory(project, sourceRoot); |
| 51 | + result.retryCacheDir = retryCacheDir; |
| 52 | + |
| 53 | + // Create package.json in retry cache directory |
| 54 | + if (!createPackageJsonForRetry(project, sourceRoot, retryCacheDir)) { |
| 55 | + result.error = 'Failed to create package.json for retry'; |
| 56 | + return result; |
| 57 | + } |
| 58 | + |
| 59 | + // Install dependencies using npm |
| 60 | + cdsExtractorLog( |
| 61 | + 'info', |
| 62 | + `Installing full dependencies for project ${project.projectDir} in retry cache directory`, |
| 63 | + ); |
| 64 | + |
| 65 | + try { |
| 66 | + execFileSync('npm', ['install', '--quiet', '--no-audit', '--no-fund'], { |
| 67 | + cwd: retryCacheDir, |
| 68 | + stdio: 'inherit', |
| 69 | + timeout: 120000, // 2-minute timeout |
| 70 | + }); |
| 71 | + |
| 72 | + result.success = true; |
| 73 | + cdsExtractorLog( |
| 74 | + 'info', |
| 75 | + `Successfully installed full dependencies for project ${project.projectDir}`, |
| 76 | + ); |
| 77 | + } catch (execError) { |
| 78 | + if (execError instanceof Error && 'signal' in execError && execError.signal === 'SIGTERM') { |
| 79 | + result.timedOut = true; |
| 80 | + result.error = 'Dependency installation timed out'; |
| 81 | + } else { |
| 82 | + result.error = `npm install failed: ${String(execError)}`; |
| 83 | + } |
| 84 | + |
| 85 | + // Still attempt retry compilation even if dependency installation fails (optimistic approach) |
| 86 | + result.warnings.push( |
| 87 | + `Dependency installation failed but will still attempt retry compilation: ${result.error}`, |
| 88 | + ); |
| 89 | + cdsExtractorLog('warn', result.warnings[0]); |
| 90 | + } |
| 91 | + } catch (error) { |
| 92 | + result.error = `Failed to install full dependencies: ${String(error)}`; |
| 93 | + cdsExtractorLog('error', result.error); |
| 94 | + } finally { |
| 95 | + result.durationMs = Date.now() - startTime; |
| 96 | + } |
| 97 | + |
| 98 | + return result; |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Determines if a project needs full dependency installation |
| 103 | + * @param project The CDS project to check |
| 104 | + * @returns Whether full dependency installation is needed |
| 105 | + */ |
| 106 | +export function needsFullDependencyInstallation(project: CdsProject): boolean { |
| 107 | + // Check if already installed |
| 108 | + if (project.retryStatus?.fullDependenciesInstalled) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + // Check if project has failed tasks that could benefit from full dependencies |
| 113 | + const hasFailedTasks = project.compilationTasks.some( |
| 114 | + task => task.status === 'failed' && !task.retryInfo?.hasBeenRetried, |
| 115 | + ); |
| 116 | + |
| 117 | + return hasFailedTasks && project.packageJson !== undefined; |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Creates retry-specific cache directory for a project |
| 122 | + * @param project The CDS project |
| 123 | + * @param sourceRoot Source root directory |
| 124 | + * @returns Path to the created retry cache directory |
| 125 | + */ |
| 126 | +export function createRetryCacheDirectory(project: CdsProject, sourceRoot: string): string { |
| 127 | + const cacheSubDirName = '.cds-extractor-cache'; |
| 128 | + const cacheRootDir = join(sourceRoot, cacheSubDirName); |
| 129 | + |
| 130 | + // Generate unique retry cache directory name |
| 131 | + const projectHash = Buffer.from(project.projectDir).toString('base64').replace(/[/+=]/g, '_'); |
| 132 | + const timestamp = Date.now(); |
| 133 | + const retryCacheDirName = `retry-${projectHash}-${timestamp}`; |
| 134 | + const retryCacheDir = join(cacheRootDir, retryCacheDirName); |
| 135 | + |
| 136 | + // Create cache root directory if it doesn't exist |
| 137 | + if (!existsSync(cacheRootDir)) { |
| 138 | + try { |
| 139 | + mkdirSync(cacheRootDir, { recursive: true }); |
| 140 | + cdsExtractorLog('info', `Created cache root directory: ${cacheRootDir}`); |
| 141 | + } catch (error) { |
| 142 | + throw new Error(`Failed to create cache root directory: ${String(error)}`); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Create retry-specific cache directory |
| 147 | + try { |
| 148 | + mkdirSync(retryCacheDir, { recursive: true }); |
| 149 | + cdsExtractorLog('info', `Created retry cache directory: ${retryCacheDirName}`); |
| 150 | + } catch (error) { |
| 151 | + throw new Error(`Failed to create retry cache directory: ${String(error)}`); |
| 152 | + } |
| 153 | + |
| 154 | + return retryCacheDir; |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Creates a package.json file in the retry cache directory based on the project's original package.json |
| 159 | + * @param project The CDS project |
| 160 | + * @param sourceRoot Source root directory |
| 161 | + * @param retryCacheDir Path to the retry cache directory |
| 162 | + * @returns Whether package.json creation was successful |
| 163 | + */ |
| 164 | +function createPackageJsonForRetry( |
| 165 | + project: CdsProject, |
| 166 | + sourceRoot: string, |
| 167 | + retryCacheDir: string, |
| 168 | +): boolean { |
| 169 | + if (!project.packageJson) { |
| 170 | + cdsExtractorLog('warn', `No package.json found for project ${project.projectDir}`); |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + try { |
| 175 | + // Check if original package-lock.json exists |
| 176 | + const originalPackageLockPath = join(sourceRoot, project.projectDir, 'package-lock.json'); |
| 177 | + let packageLockContent: unknown = undefined; |
| 178 | + |
| 179 | + if (existsSync(originalPackageLockPath)) { |
| 180 | + try { |
| 181 | + const lockContent = readFileSync(originalPackageLockPath, 'utf8'); |
| 182 | + packageLockContent = JSON.parse(lockContent); |
| 183 | + cdsExtractorLog('info', `Found package-lock.json for project ${project.projectDir}`); |
| 184 | + } catch (error) { |
| 185 | + cdsExtractorLog( |
| 186 | + 'warn', |
| 187 | + `Failed to read package-lock.json for project ${project.projectDir}: ${String(error)}`, |
| 188 | + ); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // Create package.json with all dependencies |
| 193 | + const retryPackageJson: Record<string, unknown> = { |
| 194 | + name: `${project.packageJson.name ?? 'unknown'}-retry`, |
| 195 | + version: project.packageJson.version ?? '1.0.0', |
| 196 | + private: true, |
| 197 | + dependencies: { |
| 198 | + ...(project.packageJson.dependencies ?? {}), |
| 199 | + ...(project.packageJson.devDependencies ?? {}), // Include dev dependencies as dependencies |
| 200 | + }, |
| 201 | + }; |
| 202 | + |
| 203 | + // Copy other relevant fields that might affect dependency resolution |
| 204 | + if (project.packageJson.engines) { |
| 205 | + retryPackageJson.engines = project.packageJson.engines; |
| 206 | + } |
| 207 | + if (project.packageJson.peerDependencies) { |
| 208 | + retryPackageJson.peerDependencies = project.packageJson.peerDependencies; |
| 209 | + } |
| 210 | + |
| 211 | + // Write package.json |
| 212 | + const packageJsonPath = join(retryCacheDir, 'package.json'); |
| 213 | + writeFileSync(packageJsonPath, JSON.stringify(retryPackageJson, null, 2)); |
| 214 | + cdsExtractorLog('info', `Created retry package.json for project ${project.projectDir}`); |
| 215 | + |
| 216 | + // Copy package-lock.json if it exists |
| 217 | + if (packageLockContent) { |
| 218 | + const packageLockPath = join(retryCacheDir, 'package-lock.json'); |
| 219 | + writeFileSync(packageLockPath, JSON.stringify(packageLockContent, null, 2)); |
| 220 | + cdsExtractorLog('info', `Copied package-lock.json for project ${project.projectDir}`); |
| 221 | + } |
| 222 | + |
| 223 | + return true; |
| 224 | + } catch (error) { |
| 225 | + cdsExtractorLog('error', `Failed to create package.json for retry: ${String(error)}`); |
| 226 | + return false; |
| 227 | + } |
| 228 | +} |
0 commit comments