@@ -640,6 +640,18 @@ function escapeRegExp(string: string): string {
640640 return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
641641}
642642
643+ /**
644+ * Remove one trailing newline from stdout
645+ * This handles the common case where print() or console.log() adds a trailing \n
646+ * that users don't expect to see in the output
647+ */
648+ function cleanStdout ( stdout : string ) : string {
649+ if ( stdout . endsWith ( '\n' ) ) {
650+ return stdout . slice ( 0 , - 1 )
651+ }
652+ return stdout
653+ }
654+
643655export async function POST ( req : NextRequest ) {
644656 const requestId = generateRequestId ( )
645657 const startTime = Date . now ( )
@@ -820,7 +832,7 @@ export async function POST(req: NextRequest) {
820832
821833 return NextResponse . json ( {
822834 success : true ,
823- output : { result : e2bResult ?? null , stdout, executionTime } ,
835+ output : { result : e2bResult ?? null , stdout : cleanStdout ( stdout ) , executionTime } ,
824836 } )
825837 }
826838 // Track prologue lines for error adjustment
@@ -884,7 +896,7 @@ export async function POST(req: NextRequest) {
884896
885897 return NextResponse . json ( {
886898 success : true ,
887- output : { result : e2bResult ?? null , stdout, executionTime } ,
899+ output : { result : e2bResult ?? null , stdout : cleanStdout ( stdout ) , executionTime } ,
888900 } )
889901 }
890902
@@ -948,7 +960,7 @@ export async function POST(req: NextRequest) {
948960
949961 return NextResponse . json ( {
950962 success : true ,
951- output : { result, stdout, executionTime } ,
963+ output : { result, stdout : cleanStdout ( stdout ) , executionTime } ,
952964 } )
953965 } catch ( error : any ) {
954966 const executionTime = Date . now ( ) - startTime
@@ -981,7 +993,7 @@ export async function POST(req: NextRequest) {
981993 error : userFriendlyErrorMessage ,
982994 output : {
983995 result : null ,
984- stdout,
996+ stdout : cleanStdout ( stdout ) ,
985997 executionTime,
986998 } ,
987999 // Include debug information in development or for debugging
0 commit comments