Skip to content
This repository was archived by the owner on Jan 28, 2026. It is now read-only.

Commit a7930a8

Browse files
authored
Update lint.js
1 parent 75f7299 commit a7930a8

1 file changed

Lines changed: 98 additions & 87 deletions

File tree

scripts/lint.js

Lines changed: 98 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ const HAS_REVIEWDOG_TOKEN = process.env.GITHUB_TOKEN || process.env.REVIEWDOG_GI
1515

1616
function runReviewdog(input, format, name) {
1717
if (!IS_PR) {
18-
console.log(`📝 Skipping reviewdog - not a PR (IS_PR: ${IS_PR})`);
18+
console.log(`📝 Skipping reviewdog - not a PR (event: ${process.env.GITHUB_EVENT_NAME})`);
1919
return;
2020
}
2121

2222
if (!HAS_REVIEWDOG_TOKEN) {
23-
console.log(`📝 Skipping reviewdog - no token (HAS_TOKEN: ${!!HAS_REVIEWDOG_TOKEN})`);
23+
console.log(`📝 Skipping reviewdog - no token found`);
24+
console.log(` GITHUB_TOKEN: ${!!process.env.GITHUB_TOKEN}`);
25+
console.log(` REVIEWDOG_GITHUB_API_TOKEN: ${!!process.env.REVIEWDOG_GITHUB_API_TOKEN}`);
2426
return;
2527
}
2628

2729
console.log(`🔍 Running reviewdog for ${name}...`);
30+
console.log(` Token available: ✓`);
31+
console.log(` PR number: ${process.env.GITHUB_EVENT_NAME === 'pull_request' ? 'from event' : 'n/a'}`);
2832

2933
// Ensure artifacts directory exists
3034
try {
@@ -42,82 +46,52 @@ function runReviewdog(input, format, name) {
4246
}
4347

4448
try {
45-
// Try multiple approaches for better debugging
46-
const configs = [
47-
{
48-
name: 'github-pr-review with diff_context',
49-
args: [
50-
`-f=${format}`,
51-
`-name=${name}`,
52-
'-reporter=github-pr-review',
53-
'-filter-mode=diff_context',
54-
'-level=info',
55-
'-fail-on-error=false',
56-
'-tee' // Add tee for debug output
57-
]
58-
},
59-
{
60-
name: 'github-check for fallback',
61-
args: [
62-
`-f=${format}`,
63-
`-name=${name}`,
64-
'-reporter=github-check',
65-
'-filter-mode=added',
66-
'-level=info',
67-
'-fail-on-error=false',
68-
'-tee'
69-
]
70-
}
49+
// Use the appropriate reporter and filter for PR comments
50+
const args = [
51+
`-f=${format}`,
52+
`-name=${name}`,
53+
'-reporter=github-pr-review',
54+
'-filter-mode=added', // Only comment on added/modified lines in the PR
55+
'-level=info',
56+
'-fail-on-error=false'
7157
];
7258

73-
// Try each configuration
74-
for (const config of configs) {
75-
console.log(`\n🧪 Trying reviewdog with: ${config.name}`);
76-
console.log(`📋 Full command: reviewdog ${config.args.join(' ')}`);
77-
78-
// Show first 500 chars of input being sent
79-
console.log(`📥 Input preview (first 500 chars):\n${input.slice(0, 500)}${input.length > 500 ? '\n...[truncated]' : ''}`);
80-
81-
const rd = spawnSync('reviewdog', config.args, {
82-
input: input,
83-
stdio: ['pipe', 'pipe', 'pipe'],
84-
encoding: 'utf8',
85-
env: {
86-
...process.env,
87-
REVIEWDOG_GITHUB_API_TOKEN: process.env.GITHUB_TOKEN || process.env.REVIEWDOG_GITHUB_API_TOKEN
88-
}
89-
});
90-
91-
console.log(`📊 Exit code: ${rd.status}`);
92-
93-
// Show ALL output for debugging
94-
if (rd.stdout && rd.stdout.trim()) {
95-
console.log(`📤 STDOUT (${rd.stdout.length} chars):`);
96-
console.log(rd.stdout);
97-
} else {
98-
console.log(`📤 STDOUT: (empty)`);
99-
}
100-
101-
if (rd.stderr && rd.stderr.trim()) {
102-
console.log(`📤 STDERR (${rd.stderr.length} chars):`);
103-
console.log(rd.stderr);
104-
} else {
105-
console.log(`📤 STDERR: (empty)`);
106-
}
107-
108-
// Save outputs for debugging
109-
try {
110-
fs.writeFileSync(`artifacts/${name}-${config.name.replace(/[^a-zA-Z0-9]/g, '_')}-stdout.txt`, rd.stdout || '');
111-
fs.writeFileSync(`artifacts/${name}-${config.name.replace(/[^a-zA-Z0-9]/g, '_')}-stderr.txt`, rd.stderr || '');
112-
} catch (writeError) {
113-
console.log(`⚠️ Could not save reviewdog output (non-critical):`, writeError.message);
114-
}
115-
116-
if (rd.status === 0) {
117-
console.log(`✅ Reviewdog completed successfully with ${config.name}`);
118-
} else {
119-
console.log(`❌ Reviewdog failed with ${config.name}, exit code: ${rd.status}`);
59+
console.log(`📋 Running: reviewdog ${args.join(' ')}`);
60+
61+
const rd = spawnSync('reviewdog', args, {
62+
input: input,
63+
stdio: ['pipe', 'pipe', 'pipe'],
64+
encoding: 'utf8',
65+
env: {
66+
...process.env,
67+
REVIEWDOG_GITHUB_API_TOKEN: process.env.GITHUB_TOKEN || process.env.REVIEWDOG_GITHUB_API_TOKEN
12068
}
69+
});
70+
71+
console.log(`📊 Exit code: ${rd.status}`);
72+
73+
if (rd.stdout && rd.stdout.trim()) {
74+
console.log(`📤 Output: ${rd.stdout.trim()}`);
75+
}
76+
77+
if (rd.stderr && rd.stderr.trim()) {
78+
console.log(`⚠️ Warnings/Errors: ${rd.stderr.trim()}`);
79+
}
80+
81+
// Save outputs for debugging
82+
try {
83+
if (rd.stdout) fs.writeFileSync(`artifacts/${name}-reviewdog-stdout.txt`, rd.stdout);
84+
if (rd.stderr) fs.writeFileSync(`artifacts/${name}-reviewdog-stderr.txt`, rd.stderr);
85+
} catch (writeError) {
86+
console.log(`⚠️ Could not save reviewdog output (non-critical):`, writeError.message);
87+
}
88+
89+
if (rd.status === 0) {
90+
console.log(`✅ Reviewdog completed successfully for ${name}`);
91+
} else if (rd.status === 1 && rd.stderr?.includes('no findings')) {
92+
console.log(`✅ Reviewdog found no issues in modified files for ${name}`);
93+
} else {
94+
console.log(`❌ Reviewdog exited with code ${rd.status} for ${name}`);
12195
}
12296

12397
} catch (error) {
@@ -132,7 +106,8 @@ function runPrettier() {
132106
// First, let's see what files prettier would change
133107
let filesToCheck = '';
134108
try {
135-
filesToCheck = execSync('npx prettier --list-different "tests/**/*.{js,ts,tsx,json}"', { encoding: 'utf8' });
109+
// Focus on test files as requested
110+
filesToCheck = execSync('npx prettier --list-different "tests/**/*.{js,ts,tsx,json}" 2>/dev/null || true', { encoding: 'utf8' });
136111
} catch (e) {
137112
// If no files need formatting, prettier exits with code 1
138113
filesToCheck = e.stdout?.toString() || '';
@@ -279,37 +254,66 @@ function runESLint() {
279254

280255
try {
281256
let raw = '';
257+
let hasConfigError = false;
258+
282259
try {
283260
raw = execSync('npx eslint "tests/**/*.{js,ts,tsx}" --format json', { encoding: 'utf8' });
284261
} catch (e) {
285-
// ESLint exits with code 1 when there are lint errors, but still outputs JSON
286-
raw = e.stdout?.toString() || '';
287-
if (!raw && e.stderr) {
288-
console.error('❌ ESLint error:', e.stderr.toString());
289-
console.error('📝 This might be a configuration issue. Check your .eslintrc file.');
262+
// Check if it's a configuration error by looking at stderr
263+
const stderr = e.stderr?.toString() || '';
264+
if (stderr.includes('Oops!') || stderr.includes('Cannot find module') || stderr.includes('Configuration')) {
265+
console.error('❌ ESLint configuration error:', stderr);
266+
console.error('📝 Check your ESLint configuration files (.eslintrc.json, eslint.config.mjs, etc.)');
267+
hasConfigError = true;
268+
} else {
269+
// ESLint exits with code 1 when there are lint errors, but still outputs JSON
270+
raw = e.stdout?.toString() || '';
271+
}
272+
273+
if (!raw && !hasConfigError) {
274+
console.error('❌ ESLint error:', stderr);
290275
return {
291276
files: 0,
292277
errors: 0,
293278
warnings: 0,
294279
fixableErrors: 0,
295280
fixableWarnings: 0,
296281
first: '',
297-
error: e.stderr.toString()
282+
error: stderr
298283
};
299284
}
300285
}
301286

302-
if (!raw) {
303-
console.log('✅ No ESLint output');
287+
// If we have a config error, return early
288+
if (hasConfigError) {
289+
return {
290+
files: 0,
291+
errors: 0,
292+
warnings: 0,
293+
fixableErrors: 0,
294+
fixableWarnings: 0,
295+
first: '',
296+
error: 'Configuration error'
297+
};
298+
}
299+
300+
if (!raw || raw.trim() === '') {
301+
console.log('✅ No ESLint output (no files to lint or all files clean)');
304302
return { files: 0, errors: 0, warnings: 0 };
305303
}
306304

307305
let results = [];
308306
try {
307+
// Clean the output in case there's any non-JSON content
308+
const jsonStart = raw.indexOf('[');
309+
if (jsonStart > 0) {
310+
console.log('⚠️ Found non-JSON content before results, cleaning...');
311+
raw = raw.substring(jsonStart);
312+
}
309313
results = JSON.parse(raw);
310314
} catch (parseError) {
311315
console.error('❌ Failed to parse ESLint JSON:', parseError.message);
312-
console.error('📝 ESLint output might be corrupted. Try running ESLint manually to debug.');
316+
console.error('📝 Raw output (first 500 chars):', raw.substring(0, 500));
313317
return { files: 0, errors: 0, warnings: 0, error: 'Parse error' };
314318
}
315319

@@ -391,6 +395,12 @@ npx eslint . --fix
391395
// Main execution with enhanced debugging
392396
console.log('🚀 Starting lint checks with enhanced debugging...');
393397

398+
// Check if test files exist
399+
const testDirExists = fs.existsSync('tests');
400+
if (!testDirExists) {
401+
console.log('⚠️ Warning: No tests directory found. Lint checks may not find any files.');
402+
}
403+
394404
// Environment info
395405
console.log('🔍 Environment:');
396406
console.log(` ├─ Event: ${process.env.GITHUB_EVENT_NAME}`);
@@ -399,7 +409,8 @@ console.log(` ├─ SHA: ${process.env.GITHUB_SHA?.slice(0, 8)}`);
399409
console.log(` ├─ Ref: ${process.env.GITHUB_REF}`);
400410
console.log(` ├─ Is PR: ${IS_PR}`);
401411
console.log(` ├─ Has Token: ${!!HAS_REVIEWDOG_TOKEN}`);
402-
console.log(` └─ Working Dir: ${process.cwd()}`);
412+
console.log(` ├─ Working Dir: ${process.cwd()}`);
413+
console.log(` └─ Test Dir Exists: ${testDirExists}`);
403414

404415
// Check if reviewdog is available and get verbose version info
405416
if (IS_PR && HAS_REVIEWDOG_TOKEN) {
@@ -458,4 +469,4 @@ console.log('└─ 📁 Check artifacts/ folder for debugging files');
458469

459470
// IMPORTANT: Always exit with success code
460471
console.log('\n✅ Lint check completed - check GitHub Actions logs and artifacts for reviewdog debugging info');
461-
process.exit(0);
472+
process.exit(0);

0 commit comments

Comments
 (0)