Skip to content

Commit 815b08d

Browse files
committed
add target to detect error target
1 parent 36f3fbb commit 815b08d

File tree

4 files changed

+205
-1
lines changed

4 files changed

+205
-1
lines changed

.github/actions/build/action.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ runs:
8181
COMMERCIAL_REPO_PASSWORD: ${{ inputs.commercial-repository-password }}
8282
COMMERCIAL_REPO_USERNAME: ${{ inputs.commercial-repository-username }}
8383
COMMERCIAL_SNAPSHOT_REPO_URL: ${{ inputs.commercial-snapshot-repository-url }}
84-
run: NX_BATCH_MODE=true NX_CLOUD_DEREFERENCE_SYMLINKS=true NX_VERBOSE_LOGGING=true NX_PERF_LOGGING=true NX_CLOUD_NO_TIMEOUTS=true NX_CLOUD_VERBOSE_LOGGING=true npx nx run-many -t build-ci --parallel=32 --batch --outputStyle=stream
84+
run: |
85+
echo "🔍 Running with debug logging to identify problematic targets..."
86+
node debug-nx-targets.js "NX_BATCH_MODE=true NX_CLOUD_DEREFERENCE_SYMLINKS=true NX_VERBOSE_LOGGING=true NX_PERF_LOGGING=true NX_CLOUD_NO_TIMEOUTS=true NX_CLOUD_VERBOSE_LOGGING=true npx nx run-many -t build-ci --parallel=32 --batch --outputStyle=stream"
8587
# - name: Publish
8688
# id: publish
8789
# if: ${{ inputs.publish == 'true' }}

debug-nx-targets.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
}

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
"dependencies": {
44
"@nx/gradle": "21.3.0",
55
"nx": "21.3.0"
6+
},
7+
"scripts": {
8+
"debug-targets": "./test-targets.sh",
9+
"debug-build": "node debug-nx-targets.js 'npx nx run-many -t build-ci --parallel=32 --batch --outputStyle=stream'",
10+
"postinstall": "echo '⚠️ If you get target configuration errors, run: npm run debug-targets'",
11+
"postci": "echo '✅ CI completed. If there were target errors, check the debug output above.'"
612
}
713
}

test-targets.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
3+
echo "🔍 Testing target access to identify problematic targets..."
4+
echo ""
5+
6+
# First, let's see what projects and targets are available
7+
echo "📋 Available projects:"
8+
npx nx show projects --json | jq -r '.[]' | head -10
9+
10+
echo ""
11+
echo "🎯 Testing target access for a few projects..."
12+
13+
# Test a few known projects to see which ones have issues
14+
projects=("spring-boot" "spring-boot-actuator" "spring-boot-test" "spring-boot-autoconfigure")
15+
16+
for project in "${projects[@]}"; do
17+
echo "Testing project: $project"
18+
19+
# Try to get project configuration
20+
if npx nx show project "$project" --json > /tmp/project_config.json 2>/dev/null; then
21+
echo " ✅ Project $project exists"
22+
23+
# Check if build-ci target exists
24+
if jq -e ".targets[\"build-ci\"]" /tmp/project_config.json > /dev/null 2>&1; then
25+
echo " ✅ Target build-ci exists in $project"
26+
else
27+
echo " ❌ Target build-ci MISSING in $project"
28+
echo " Available targets:"
29+
jq -r ".targets | keys[]" /tmp/project_config.json | head -5
30+
fi
31+
else
32+
echo " ❌ Project $project NOT FOUND"
33+
fi
34+
echo ""
35+
done
36+
37+
echo "🏃 Now testing the actual nx run-many command with a small subset..."
38+
39+
# Try running with just a few projects to isolate the issue
40+
echo "Testing with 1 project at a time..."
41+
for project in "${projects[@]}"; do
42+
echo "Testing: npx nx run $project:build-ci"
43+
if npx nx run "$project:build-ci" --dry-run 2>&1 | grep -q "Cannot read properties of undefined"; then
44+
echo "❌ ERROR found with project: $project"
45+
break
46+
else
47+
echo "✅ Project $project looks OK"
48+
fi
49+
done
50+
51+
rm -f /tmp/project_config.json
52+
echo "🔍 Debug completed. Check output above for problematic targets."

0 commit comments

Comments
 (0)