Skip to content

Commit 04d8980

Browse files
Juan Castañojuancastano
authored andcommitted
Fix error detection again
1 parent 4989894 commit 04d8980

File tree

2 files changed

+211
-24
lines changed

2 files changed

+211
-24
lines changed

lib/e2b.ts

Lines changed: 209 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { benchifyFileSchema } from './schemas';
33
import { z } from 'zod';
44
import { fetchAllSandboxFiles } from './file-filter';
55
import { applyTransformations } from './sandbox-helpers';
6-
import { detectCodeErrors, parseTypeScriptErrors } from './error-detection';
76

87
const E2B_API_KEY = process.env.E2B_API_KEY;
98

@@ -89,36 +88,225 @@ export async function createSandbox({ files }: { files: z.infer<typeof benchifyF
8988
// Start dev server in background
9089
const devServerResult = await sandbox.commands.run('cd /app && npm run dev', { background: true });
9190

92-
console.log('Dev server command executed');
93-
console.log('Dev server exit code:', devServerResult.exitCode);
94-
console.log('Dev server stderr:', devServerResult.stderr || 'No stderr');
95-
console.log('Dev server stdout:', devServerResult.stdout || 'No stdout');
91+
console.log('=== DEV SERVER INITIAL RESULT ===');
92+
console.log('Exit code:', devServerResult.exitCode);
93+
console.log('Stderr length:', devServerResult.stderr?.length || 0);
94+
console.log('Stdout length:', devServerResult.stdout?.length || 0);
95+
console.log('Stderr content:', devServerResult.stderr || 'No stderr');
96+
console.log('Stdout content:', devServerResult.stdout || 'No stdout');
9697

9798
// Give it a moment to start and potentially fail
98-
console.log('Waiting 3 seconds for dev server to start...');
99-
await new Promise(resolve => setTimeout(resolve, 3000));
99+
console.log('Waiting 5 seconds for dev server to start...');
100+
await new Promise(resolve => setTimeout(resolve, 5000));
100101

101-
// Check the initial output for immediate errors
102-
if (devServerResult.stderr || devServerResult.stdout) {
103-
const allOutput = (devServerResult.stderr || '') + '\n' + (devServerResult.stdout || '');
104-
105-
// Use the error detection module
106-
const errorResult = detectCodeErrors(allOutput);
102+
// Check if the dev server is actually running by trying to access it
103+
console.log('=== CHECKING IF DEV SERVER IS ACTUALLY RUNNING ===');
104+
try {
105+
const healthCheck = await sandbox.commands.run('curl -s -o /dev/null -w "%{http_code}" http://localhost:5173', { timeoutMs: 5000 });
106+
console.log('Health check result:', healthCheck);
107+
console.log('HTTP status code:', healthCheck.stdout);
107108

108-
if (errorResult.hasErrors) {
109-
console.log('🔴 CODE ERRORS DETECTED!');
110-
buildErrors.push(...errorResult.errors);
111-
} else if (errorResult.isInfrastructureOnly) {
112-
console.log('⚠️ Only infrastructure errors detected (ignoring)');
109+
if (healthCheck.stdout === '200') {
110+
console.log('✅ Dev server is running successfully despite permission errors!');
113111
} else {
114-
console.log('✅ No errors detected');
112+
console.log('❌ Dev server is not responding properly');
115113
}
116-
} else {
117-
console.log('⚠️ No stderr or stdout from dev server command');
114+
} catch (healthError) {
115+
console.log('Health check failed:', healthError);
116+
}
117+
118+
// Check what processes are running
119+
console.log('=== CHECKING RUNNING PROCESSES ===');
120+
try {
121+
const processCheck = await sandbox.commands.run('ps aux | grep -E "(vite|node)" | grep -v grep');
122+
console.log('Running processes:', processCheck.stdout);
123+
} catch (processError) {
124+
console.log('Process check failed:', processError);
125+
}
126+
127+
// Check if there are any recent logs
128+
console.log('=== CHECKING FOR RECENT COMMAND OUTPUT ===');
129+
try {
130+
const recentLogs = await sandbox.commands.run('cd /app && timeout 2s npm run dev 2>&1 || true');
131+
console.log('Recent dev server attempt:', recentLogs.stdout);
132+
console.log('Recent dev server stderr:', recentLogs.stderr);
133+
} catch (logError) {
134+
console.log('Recent logs check failed:', logError);
135+
}
136+
137+
// Simplified error detection: if there's stderr output or non-zero exit, it's likely an error
138+
const hasStderr = devServerResult.stderr && devServerResult.stderr.trim().length > 0;
139+
const hasErrorInStdout = devServerResult.stdout && (
140+
devServerResult.stdout.includes('error') ||
141+
devServerResult.stdout.includes('Error') ||
142+
devServerResult.stdout.includes('failed') ||
143+
devServerResult.stdout.includes('Failed')
144+
);
145+
146+
// Check if the errors are just permission issues that don't prevent the server from working
147+
const isPermissionError = devServerResult.stderr &&
148+
devServerResult.stderr.includes('EACCES: permission denied') &&
149+
devServerResult.stderr.includes('/app/node_modules/.vite-temp/');
150+
151+
// Check for actual compilation/build errors in the recent logs
152+
let hasCompilationError = false;
153+
let compilationErrorOutput = '';
154+
155+
console.log('=== COMPILATION ERROR CHECK ===');
156+
157+
// Try multiple approaches to detect compilation errors
158+
try {
159+
// Approach 1: Try to build the project
160+
console.log('Trying npm run build...');
161+
const buildCheck = await sandbox.commands.run('cd /app && timeout 10s npm run build 2>&1 || true');
162+
console.log('Build check output:', buildCheck.stdout);
163+
console.log('Build check stderr:', buildCheck.stderr);
164+
165+
if (buildCheck.stdout && (
166+
buildCheck.stdout.includes('Unterminated string constant') ||
167+
buildCheck.stdout.includes('SyntaxError') ||
168+
buildCheck.stdout.includes('Unexpected token') ||
169+
buildCheck.stdout.includes('Parse error') ||
170+
buildCheck.stdout.includes('[plugin:vite:') ||
171+
buildCheck.stdout.includes('Transform failed') ||
172+
buildCheck.stdout.includes('Build failed')
173+
)) {
174+
hasCompilationError = true;
175+
compilationErrorOutput = buildCheck.stdout;
176+
console.log('✅ Found compilation error in build output');
177+
}
178+
} catch (buildError) {
179+
console.log('Build check failed:', buildError);
180+
}
181+
182+
// Approach 2: Try to check TypeScript compilation
183+
if (!hasCompilationError) {
184+
try {
185+
console.log('Trying tsc --noEmit...');
186+
const tscCheck = await sandbox.commands.run('cd /app && timeout 10s npx tsc --noEmit 2>&1 || true');
187+
console.log('TypeScript check output:', tscCheck.stdout);
188+
console.log('TypeScript check stderr:', tscCheck.stderr);
189+
190+
if (tscCheck.stdout && (
191+
tscCheck.stdout.includes('error TS') ||
192+
tscCheck.stdout.includes('Unterminated string constant') ||
193+
tscCheck.stdout.includes('SyntaxError')
194+
)) {
195+
hasCompilationError = true;
196+
compilationErrorOutput = tscCheck.stdout;
197+
console.log('✅ Found compilation error in TypeScript check');
198+
}
199+
} catch (tscError) {
200+
console.log('TypeScript check failed:', tscError);
201+
}
202+
}
203+
204+
// Approach 3: Try to parse files directly with Babel/ESLint
205+
if (!hasCompilationError) {
206+
try {
207+
console.log('Trying to parse main files...');
208+
const parseCheck = await sandbox.commands.run('cd /app && timeout 10s npx babel src/App.tsx --presets=@babel/preset-typescript 2>&1 || true');
209+
console.log('Parse check output:', parseCheck.stdout);
210+
console.log('Parse check stderr:', parseCheck.stderr);
211+
212+
if (parseCheck.stderr && (
213+
parseCheck.stderr.includes('Unterminated string constant') ||
214+
parseCheck.stderr.includes('SyntaxError') ||
215+
parseCheck.stderr.includes('Unexpected token')
216+
)) {
217+
hasCompilationError = true;
218+
compilationErrorOutput = parseCheck.stderr;
219+
console.log('✅ Found compilation error in parse check');
220+
}
221+
} catch (parseError) {
222+
console.log('Parse check failed:', parseError);
223+
}
224+
}
225+
226+
// Approach 4: Check if the dev server is actually serving errors
227+
if (!hasCompilationError) {
228+
try {
229+
console.log('Checking dev server response for errors...');
230+
const responseCheck = await sandbox.commands.run('cd /app && timeout 5s curl -s http://localhost:5173 2>&1 || true');
231+
console.log('Response check output:', responseCheck.stdout);
232+
233+
if (responseCheck.stdout && (
234+
responseCheck.stdout.includes('SyntaxError') ||
235+
responseCheck.stdout.includes('Unterminated string') ||
236+
responseCheck.stdout.includes('Parse error') ||
237+
responseCheck.stdout.includes('Transform failed')
238+
)) {
239+
hasCompilationError = true;
240+
compilationErrorOutput = responseCheck.stdout;
241+
console.log('✅ Found compilation error in dev server response');
242+
}
243+
} catch (responseError) {
244+
console.log('Response check failed:', responseError);
245+
}
246+
}
247+
248+
// Approach 5: Check Vite logs more thoroughly
249+
if (!hasCompilationError) {
250+
try {
251+
console.log('Checking for Vite process logs...');
252+
const viteLogsCheck = await sandbox.commands.run('cd /app && ps aux | grep vite');
253+
console.log('Vite processes:', viteLogsCheck.stdout);
254+
255+
// Try to get logs from the running Vite process
256+
const viteLogCheck = await sandbox.commands.run('cd /app && timeout 3s strace -p $(pgrep -f "vite --host") 2>&1 | head -20 || true');
257+
console.log('Vite process trace:', viteLogCheck.stdout);
258+
} catch (viteError) {
259+
console.log('Vite log check failed:', viteError);
260+
}
261+
}
262+
263+
console.log('=== COMPILATION ERROR CHECK SUMMARY ===');
264+
console.log('Has compilation error:', hasCompilationError);
265+
console.log('Compilation error output length:', compilationErrorOutput.length);
266+
if (compilationErrorOutput) {
267+
console.log('Compilation error preview:', compilationErrorOutput.substring(0, 500));
118268
}
119269

120270
console.log('Dev server started, output checked');
121271
console.log('Total build errors found:', buildErrors.length);
272+
273+
console.log('=== ERROR ANALYSIS ===');
274+
console.log('Has stderr:', hasStderr);
275+
console.log('Has error in stdout:', hasErrorInStdout);
276+
console.log('Is permission error:', isPermissionError);
277+
console.log('Has compilation error:', hasCompilationError);
278+
279+
if (hasCompilationError || ((hasStderr || hasErrorInStdout) && !isPermissionError)) {
280+
console.log('🔴 REAL ERRORS DETECTED IN DEV SERVER OUTPUT');
281+
282+
// Get the actual error output for display
283+
let errorOutput = '';
284+
285+
if (hasCompilationError) {
286+
// Use the compilation error output we found
287+
errorOutput = compilationErrorOutput;
288+
console.log('Using compilation error output for display');
289+
} else {
290+
// Use the original stderr/stdout
291+
errorOutput = [devServerResult.stderr, devServerResult.stdout]
292+
.filter(Boolean)
293+
.join('\n')
294+
.trim();
295+
console.log('Using dev server stderr/stdout for display');
296+
}
297+
298+
if (errorOutput) {
299+
console.log('Adding build error with message length:', errorOutput.length);
300+
buildErrors.push({
301+
type: 'build',
302+
message: errorOutput
303+
});
304+
}
305+
} else if (isPermissionError) {
306+
console.log('⚠️ Permission errors detected but likely non-critical (E2B sandbox issue)');
307+
} else {
308+
console.log('✅ No errors detected in dev server output');
309+
}
122310
} catch (error) {
123311
console.error('Dev server check failed:', error);
124312
buildErrors.push({
@@ -142,8 +330,6 @@ export async function createSandbox({ files }: { files: z.infer<typeof benchifyF
142330
};
143331
}
144332

145-
146-
147333
function extractNewPackages(packageJsonContent: string): string[] {
148334
try {
149335
const packageJson = JSON.parse(packageJsonContent);

lib/error-detection.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function detectCodeErrors(output: string): ErrorDetectionResult {
2424
const hasSyntaxError = output.includes('SyntaxError');
2525
const hasUnexpectedToken = output.includes('Unexpected token');
2626
const hasParseError = output.includes('Parse error');
27-
const hasUnterminatedString = output.includes('Unterminated string');
27+
const hasUnterminatedString = output.includes('Unterminated string') || output.includes('Unterminated string constant');
2828
const hasModuleError = output.includes('Cannot resolve module') || output.includes('Module not found');
2929
const hasImportError = output.includes('Cannot resolve import');
3030

@@ -122,6 +122,7 @@ function parseErrorsFromOutput(output: string): BuildError[] {
122122
line.includes('Unexpected token') ||
123123
line.includes('Parse error') ||
124124
line.includes('Unterminated string') ||
125+
line.includes('Unterminated string constant') ||
125126
line.includes('Cannot resolve module') ||
126127
line.includes('Module not found') ||
127128
line.includes('Cannot resolve import');

0 commit comments

Comments
 (0)