11import type { CodeQLConfig } from '../types' ;
2- import * as fs from 'node:fs' ;
32import * as process from 'node:process' ;
43import { exec } from '@actions/exec' ;
54import { FileUtils } from '../utils/file-utils' ;
65import { Logger } from '../utils/logger' ;
6+ import {
7+ restorePackageJson ,
8+ temporarilyRenamePackageJsonIfTypeModule ,
9+ } from './utils/package-json-utils' ;
710
811export class CodeQLDatabase {
912 static async createDatabase (
@@ -25,7 +28,6 @@ export class CodeQLDatabase {
2528 Logger . info ( `Languages to analyze: ${ languages . join ( ', ' ) } ` ) ;
2629 Logger . info ( `Multi-language analysis: ${ isMultiLanguage } ` ) ;
2730
28- // --- START: ADDED CODE FOR THE FIX ---
2931 const packageJsonPath = FileUtils . joinPath ( filteredPath , 'package.json' ) ;
3032 const packageJsonBackupPath = FileUtils . joinPath ( filteredPath , 'package.json.bak' ) ;
3133 const rootPackageJsonPath = FileUtils . joinPath ( process . cwd ( ) , 'package.json' ) ;
@@ -35,79 +37,20 @@ export class CodeQLDatabase {
3537 ) ;
3638 let needsCleanup = false ;
3739 let needsRootCleanup = false ;
38- // --- END: ADDED CODE FOR THE FIX ---
3940
4041 try {
41- // --- START: ADDED CODE FOR THE FIX ---
42- // Check if package.json exists and contains "type": "module"
43- Logger . info ( `Looking for package.json in filtered path: ${ packageJsonPath } ` ) ;
42+ // Use helpers to reduce duplication
43+ needsCleanup = temporarilyRenamePackageJsonIfTypeModule (
44+ packageJsonPath ,
45+ packageJsonBackupPath ,
46+ 'filtered path' ,
47+ ) ;
4448
45- if ( FileUtils . exists ( packageJsonPath ) ) {
46- Logger . info ( 'package.json found. Reading file content.' ) ;
47- const packageJsonContent = FileUtils . readFile ( packageJsonPath ) ;
48- try {
49- const parsed = JSON . parse ( packageJsonContent ) as unknown ;
50- if ( typeof parsed === 'object' && parsed !== null && 'type' in parsed ) {
51- const packageJson = parsed as { type ?: unknown } ;
52- Logger . info ( `Detected package.json.type = ${ String ( packageJson . type ) } ` ) ;
53- if ( packageJson . type === 'module' ) {
54- Logger . info (
55- 'Temporarily renaming package.json to avoid ESM/CJS conflict with CodeQL extractor.' ,
56- ) ;
57- fs . renameSync ( packageJsonPath , packageJsonBackupPath ) ;
58- needsCleanup = true ;
59- Logger . info ( 'package.json was renamed to package.json.bak' ) ;
60- } else {
61- Logger . info ( 'package.json.type is not "module". No rename necessary.' ) ;
62- }
63- } else {
64- Logger . info (
65- 'package.json does not contain a "type" field. No rename necessary.' ,
66- ) ;
67- }
68- } catch ( e ) {
69- Logger . warning (
70- `Could not parse package.json. Skipping temporary rename. Error: ${ e instanceof Error ? e . message : String ( e ) } ` ,
71- ) ;
72- }
73- } else {
74- Logger . info ( 'No package.json found in filtered path.' ) ;
75- }
76-
77- // Also check for package.json in the root directory (where CodeQL might also look)
78- Logger . info ( `Looking for package.json in root path: ${ rootPackageJsonPath } ` ) ;
79- if ( FileUtils . exists ( rootPackageJsonPath ) ) {
80- Logger . info ( 'Root package.json found. Reading file content.' ) ;
81- const rootPackageJsonContent = FileUtils . readFile ( rootPackageJsonPath ) ;
82- try {
83- const parsed = JSON . parse ( rootPackageJsonContent ) as unknown ;
84- if ( typeof parsed === 'object' && parsed !== null && 'type' in parsed ) {
85- const packageJson = parsed as { type ?: unknown } ;
86- Logger . info ( `Detected root package.json.type = ${ String ( packageJson . type ) } ` ) ;
87- if ( packageJson . type === 'module' ) {
88- Logger . info (
89- 'Temporarily renaming root package.json to avoid ESM/CJS conflict with CodeQL extractor.' ,
90- ) ;
91- fs . renameSync ( rootPackageJsonPath , rootPackageJsonBackupPath ) ;
92- needsRootCleanup = true ;
93- Logger . info ( 'Root package.json was renamed to package.json.bak' ) ;
94- } else {
95- Logger . info ( 'Root package.json.type is not "module". No rename necessary.' ) ;
96- }
97- } else {
98- Logger . info (
99- 'Root package.json does not contain a "type" field. No rename necessary.' ,
100- ) ;
101- }
102- } catch ( e ) {
103- Logger . warning (
104- `Could not parse root package.json. Skipping temporary rename. Error: ${ e instanceof Error ? e . message : String ( e ) } ` ,
105- ) ;
106- }
107- } else {
108- Logger . info ( 'No package.json found in root path.' ) ;
109- }
110- // --- END: ADDED CODE FOR THE FIX ---
49+ needsRootCleanup = temporarilyRenamePackageJsonIfTypeModule (
50+ rootPackageJsonPath ,
51+ rootPackageJsonBackupPath ,
52+ 'root path' ,
53+ ) ;
11154
11255 const args = [ 'database' , 'create' , dbPath ] ;
11356
@@ -121,19 +64,23 @@ export class CodeQLDatabase {
12164 }
12265
12366 args . push ( `--source-root=${ filteredPath } ` ) ;
124- await exec ( codeqlPath , args ) ;
67+ try {
68+ await exec ( codeqlPath , args ) ;
69+ } catch ( e ) {
70+ // Log the error clearly, then rethrow so the finally block can restore package.json files
71+ Logger . error (
72+ `CodeQL execution failed: ${ e instanceof Error ? e . message : String ( e ) } ` ,
73+ ) ;
74+ throw e ;
75+ }
12576 } finally {
126- // --- START: ADDED CODE FOR THE FIX ---
12777 // Ensure package.json is restored even if exec fails
12878 if ( needsCleanup ) {
129- Logger . info ( 'Restoring original package.json.' ) ;
130- fs . renameSync ( packageJsonBackupPath , packageJsonPath ) ;
79+ restorePackageJson ( packageJsonBackupPath , packageJsonPath , 'filtered path' ) ;
13180 }
13281 if ( needsRootCleanup ) {
133- Logger . info ( 'Restoring original root package.json.' ) ;
134- fs . renameSync ( rootPackageJsonBackupPath , rootPackageJsonPath ) ;
82+ restorePackageJson ( rootPackageJsonBackupPath , rootPackageJsonPath , 'root path' ) ;
13583 }
136- // --- END: ADDED CODE FOR THE FIX ---
13784 }
13885 }
13986
0 commit comments