1
+ #!/usr/bin/env node
2
+
3
+ const { execSync } = require ( 'child_process' ) ;
4
+ const fs = require ( 'fs' ) ;
5
+
6
+ // Override the getTargetConfigurationForTask function to add logging
7
+ const originalUtilsPath = 'node_modules/nx/src/tasks-runner/utils.js' ;
8
+ const backupUtilsPath = 'node_modules/nx/src/tasks-runner/utils.js.backup' ;
9
+
10
+ try {
11
+ console . log ( '🔍 Debugging Nx target configuration issue...' ) ;
12
+
13
+ // Backup original file if not already backed up
14
+ if ( ! fs . existsSync ( backupUtilsPath ) ) {
15
+ fs . copyFileSync ( originalUtilsPath , backupUtilsPath ) ;
16
+ console . log ( '✅ Backed up original utils.js' ) ;
17
+ }
18
+
19
+ // Read the original file
20
+ let utilsContent = fs . readFileSync ( originalUtilsPath , 'utf8' ) ;
21
+
22
+ // Find and replace the getTargetConfigurationForTask function to add logging
23
+ const originalFunction = `function getTargetConfigurationForTask(task, projectGraph) {
24
+ return projectGraph.nodes[task.target.project].data.targets[task.target.target];
25
+ }` ;
26
+
27
+ const debugFunction = `function getTargetConfigurationForTask(task, projectGraph) {
28
+ const projectNode = projectGraph.nodes[task.target.project];
29
+ if (!projectNode) {
30
+ console.error('❌ ERROR: Project not found in graph:', task.target.project);
31
+ console.error('Available projects:', Object.keys(projectGraph.nodes));
32
+ throw new Error(\`Project \${task.target.project} not found in project graph\`);
33
+ }
34
+
35
+ if (!projectNode.data) {
36
+ console.error('❌ ERROR: Project data is undefined for:', task.target.project);
37
+ console.error('Project node:', JSON.stringify(projectNode, null, 2));
38
+ throw new Error(\`Project data is undefined for \${task.target.project}\`);
39
+ }
40
+
41
+ if (!projectNode.data.targets) {
42
+ console.error('❌ ERROR: Project targets are undefined for:', task.target.project);
43
+ console.error('Project data:', JSON.stringify(projectNode.data, null, 2));
44
+ throw new Error(\`Project targets are undefined for \${task.target.project}\`);
45
+ }
46
+
47
+ const target = projectNode.data.targets[task.target.target];
48
+ if (!target) {
49
+ console.error('❌ ERROR: Target not found:', {
50
+ project: task.target.project,
51
+ target: task.target.target,
52
+ availableTargets: Object.keys(projectNode.data.targets)
53
+ });
54
+ throw new Error(\`Target \${task.target.target} not found in project \${task.target.project}\`);
55
+ }
56
+
57
+ return target;
58
+ }` ;
59
+
60
+ // Replace the function
61
+ if ( utilsContent . includes ( 'function getTargetConfigurationForTask(task, projectGraph)' ) ) {
62
+ utilsContent = utilsContent . replace ( originalFunction , debugFunction ) ;
63
+ fs . writeFileSync ( originalUtilsPath , utilsContent ) ;
64
+ console . log ( '✅ Added debug logging to getTargetConfigurationForTask' ) ;
65
+ } else {
66
+ console . log ( '⚠️ Could not find exact function signature to replace, trying alternative approach' ) ;
67
+
68
+ // Alternative approach - replace the problematic line directly
69
+ const problemLine = 'return projectGraph.nodes[task.target.project].data.targets[task.target.target];' ;
70
+ const debugLine = `
71
+ const projectNode = projectGraph.nodes[task.target.project];
72
+ if (!projectNode) {
73
+ console.error('❌ ERROR: Project not found in graph:', task.target.project);
74
+ console.error('Available projects:', Object.keys(projectGraph.nodes));
75
+ throw new Error(\`Project \${task.target.project} not found in project graph\`);
76
+ }
77
+
78
+ if (!projectNode.data) {
79
+ console.error('❌ ERROR: Project data is undefined for:', task.target.project);
80
+ throw new Error(\`Project data is undefined for \${task.target.project}\`);
81
+ }
82
+
83
+ if (!projectNode.data.targets) {
84
+ console.error('❌ ERROR: Project targets are undefined for:', task.target.project);
85
+ throw new Error(\`Project targets are undefined for \${task.target.project}\`);
86
+ }
87
+
88
+ const target = projectNode.data.targets[task.target.target];
89
+ if (!target) {
90
+ console.error('❌ ERROR: Target not found:', {
91
+ project: task.target.project,
92
+ target: task.target.target,
93
+ availableTargets: Object.keys(projectNode.data.targets)
94
+ });
95
+ throw new Error(\`Target \${task.target.target} not found in project \${task.target.project}\`);
96
+ }
97
+
98
+ return target;` ;
99
+
100
+ if ( utilsContent . includes ( problemLine ) ) {
101
+ utilsContent = utilsContent . replace ( problemLine , debugLine ) ;
102
+ fs . writeFileSync ( originalUtilsPath , utilsContent ) ;
103
+ console . log ( '✅ Added debug logging to problematic line' ) ;
104
+ } else {
105
+ console . log ( '⚠️ Could not find problematic line to replace' ) ;
106
+ console . log ( 'File content snippet around line 289:' ) ;
107
+ const lines = utilsContent . split ( '\n' ) ;
108
+ for ( let i = 285 ; i < 295 ; i ++ ) {
109
+ if ( lines [ i ] ) {
110
+ console . log ( `${ i + 1 } : ${ lines [ i ] } ` ) ;
111
+ }
112
+ }
113
+ }
114
+ }
115
+
116
+ console . log ( '🚀 Debug logging enabled. Running the original command...' ) ;
117
+ console . log ( '' ) ;
118
+
119
+ // Run the original command that was failing
120
+ const command = process . argv . slice ( 2 ) . join ( ' ' ) ;
121
+ if ( command ) {
122
+ console . log ( `Running: ${ command } ` ) ;
123
+ execSync ( command , { stdio : 'inherit' } ) ;
124
+ } else {
125
+ console . log ( 'No command provided. You can now run your nx commands and see detailed error messages.' ) ;
126
+ }
127
+
128
+ } catch ( error ) {
129
+ console . error ( 'Error in debug script:' , error . message ) ;
130
+
131
+ // Restore original file on error
132
+ if ( fs . existsSync ( backupUtilsPath ) ) {
133
+ fs . copyFileSync ( backupUtilsPath , originalUtilsPath ) ;
134
+ console . log ( '🔄 Restored original utils.js' ) ;
135
+ }
136
+
137
+ process . exit ( 1 ) ;
138
+ } finally {
139
+ // Restore original file
140
+ if ( fs . existsSync ( backupUtilsPath ) ) {
141
+ fs . copyFileSync ( backupUtilsPath , originalUtilsPath ) ;
142
+ console . log ( '🔄 Restored original utils.js' ) ;
143
+ }
144
+ }
0 commit comments