diff --git a/src/e2e/windows-debug.test.ts b/src/e2e/windows-debug.test.ts deleted file mode 100644 index 52322e226..000000000 --- a/src/e2e/windows-debug.test.ts +++ /dev/null @@ -1,166 +0,0 @@ -import path from "path"; -import { spawn } from "child_process"; -import { createTestContext, cleanupTestContext } from "../test-helpers/cli-helpers.js"; - -describe("Windows CI Debug", () => { - // Only run these tests on Windows CI - const skipTests = !(process.platform === "win32" && process.env.CI); - - if (skipTests) { - it.skip("only runs on Windows CI", () => {}); - return; - } - - it("should verify Node.js can execute the CLI file directly", async () => { - const context = await createTestContext(); - - try { - // Test 1: Direct Node execution with simple --version flag - await new Promise((resolve, reject) => { - const proc = spawn(process.execPath, [context.cliPath, "--version"], { - cwd: context.testDir, - stdio: ["pipe", "pipe", "pipe"], - shell: false, - windowsVerbatimArguments: true, - }); - - let stdout = ""; - let stderr = ""; - - proc.stdout.on("data", (data) => { - stdout += data.toString(); - }); - - proc.stderr.on("data", (data) => { - stderr += data.toString(); - }); - - proc.on("error", (error) => { - console.error("Process spawn error:", error); - reject(error); - }); - - proc.on("exit", (code, signal) => { - console.log("Process exit:", { code, signal, stdout, stderr }); - - if (code === 0) { - resolve(); - } else { - reject(new Error(`Process exited with code ${code}, stderr: ${stderr}`)); - } - }); - }); - } finally { - await cleanupTestContext(context); - } - }, 30000); - - it("should capture early CLI errors", async () => { - const context = await createTestContext(); - - try { - // Test 2: Try with a minimal command that should work - await new Promise((resolve, reject) => { - const env = { - ...process.env, - DEBUG_CLI_TESTS: "1", - NODE_OPTIONS: "", // Clear any NODE_OPTIONS that might interfere - }; - - const proc = spawn(process.execPath, [context.cliPath, "--help"], { - cwd: context.testDir, - env, - stdio: ["pipe", "pipe", "pipe"], - shell: false, - }); - - let stdout = ""; - let stderr = ""; - let errorOutput = ""; - - proc.stdout.on("data", (data) => { - const text = data.toString(); - stdout += text; - console.log("[STDOUT]:", text); - }); - - proc.stderr.on("data", (data) => { - const text = data.toString(); - stderr += text; - errorOutput += text; - console.log("[STDERR]:", text); - }); - - proc.on("error", (error) => { - console.error("Process spawn error:", error); - reject(error); - }); - - proc.on("exit", (code, signal) => { - console.log("Process exit details:", { - code, - signal, - stdoutLength: stdout.length, - stderrLength: stderr.length, - hasCliStartLog: stderr.includes("[CLI_START]"), - hasCliErrorLog: stderr.includes("[CLI_ERROR]"), - }); - - // Even if it fails, we want to see what happened - if (stderr.includes("[CLI_ERROR]") || stderr.includes("Error:")) { - console.log("Full error output:", errorOutput); - } - - resolve(); // Always resolve to see the output - }); - }); - } finally { - await cleanupTestContext(context); - } - }, 30000); - - it("should test module resolution", async () => { - const context = await createTestContext(); - - try { - // Test 3: Check if ESM modules are the issue - const testScript = ` - console.log("Test script running on:", process.platform); - console.log("Node version:", process.version); - console.log("__dirname:", import.meta.url); - - try { - await import("commander"); - console.log("SUCCESS: commander module loaded"); - } catch (e) { - console.error("FAILED to load commander:", e.message); - } - `; - - const testFile = path.join(context.testDir, "test-esm.mjs"); - const fs = await import("fs/promises"); - await fs.writeFile(testFile, testScript); - - await new Promise((resolve) => { - const proc = spawn(process.execPath, [testFile], { - cwd: context.testDir, - stdio: ["pipe", "pipe", "pipe"], - }); - - proc.stdout.on("data", (data) => { - console.log("[ESM Test STDOUT]:", data.toString()); - }); - - proc.stderr.on("data", (data) => { - console.log("[ESM Test STDERR]:", data.toString()); - }); - - proc.on("exit", () => { - resolve(); - }); - }); - } finally { - await cleanupTestContext(context); - } - }, 30000); -}); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 6fa275e20..7298c43c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,5 @@ #!/usr/bin/env node -// Add immediate logging to debug Windows CI issues -// Only log on Windows when DEBUG_CLI_TESTS is set -if (process.env.DEBUG_CLI_TESTS === "1" && process.platform === "win32") { - console.error( - `[CLI_START] Node ${process.version} on ${process.platform} ${process.arch}` - ); - console.error(`[CLI_START] Args: ${JSON.stringify(process.argv)}`); - console.error(`[CLI_START] CWD: ${process.cwd()}`); - console.error(`[CLI_START] __dirname: ${import.meta.url}`); -} - import { Command } from "commander"; import { chat } from "./commands/chat.js"; import { login } from "./commands/login.js"; @@ -77,6 +66,7 @@ program } logger.debug("Verbose logging enabled"); } + // Map --print to headless mode options.headless = options.print; options.print = undefined; @@ -106,34 +96,10 @@ program.on("command:*", () => { process.exit(1); }); -// Wrap everything in a try-catch for Windows CI debugging -async function main() { - try { - await program.parseAsync(); - } catch (error: any) { - console.error("[CLI_ERROR] Fatal error during execution:"); - console.error("[CLI_ERROR] Message:", error.message); - console.error("[CLI_ERROR] Stack:", error.stack); - console.error("[CLI_ERROR] Code:", error.code); - - // More specific error messages - if (error.code === "MODULE_NOT_FOUND") { - console.error( - "[CLI_ERROR] Missing module. This might be a build or path issue." - ); - } - - process.exit(1); - } -} - -// Catch any synchronous errors during startup +// Parse arguments and handle errors try { - main().catch((error) => { - console.error("[CLI_ERROR] Unhandled async error:", error); - process.exit(1); - }); -} catch (error: any) { - console.error("[CLI_ERROR] Synchronous startup error:", error); + program.parse(); +} catch (error) { + console.error(error); process.exit(1); } diff --git a/src/test-helpers/cli-helpers.ts b/src/test-helpers/cli-helpers.ts index b6e612e09..3b9a9d3d3 100644 --- a/src/test-helpers/cli-helpers.ts +++ b/src/test-helpers/cli-helpers.ts @@ -66,17 +66,6 @@ export async function runCLI( expectError = false, } = options; - // Windows CI diagnostics - if (process.platform === "win32" && process.env.CI) { - console.log("Windows CI Diagnostics:"); - console.log("- CLI Path:", context.cliPath); - console.log("- Absolute CLI Path:", path.resolve(context.cliPath)); - console.log("- File exists:", await fs.access(context.cliPath).then(() => true).catch(() => false)); - console.log("- Working dir:", context.testDir); - console.log("- Node version:", process.version); - console.log("- Process platform:", process.platform); - } - const execOptions = { cwd: context.testDir, env: { @@ -95,127 +84,25 @@ export async function runCLI( timeout, reject: false, input, - // Ensure proper encoding - encoding: 'utf8' as const, - // Increase buffer size for Windows - maxBuffer: 10 * 1024 * 1024, // 10MB - // Let execa handle Windows argument escaping automatically - windowsHide: true, }; try { - // On Windows CI, log the actual command being executed - if (process.platform === "win32" && process.env.CI) { - console.log("Executing command:", { - node: process.execPath, - script: context.cliPath, - args: args, - }); - - // Check if dist/index.js exists and is readable - try { - const distIndexPath = path.resolve(context.cliPath); - const stats = await fs.stat(distIndexPath); - console.log("CLI file stats:", { - exists: true, - size: stats.size, - isFile: stats.isFile(), - mode: stats.mode.toString(8), - }); - - // Read first few lines to check for shebang issues - const content = await fs.readFile(distIndexPath, 'utf8'); - const firstLines = content.split('\n').slice(0, 5).join('\n'); - console.log("First lines of CLI file:", firstLines); - } catch (checkError) { - console.error("Failed to check CLI file:", checkError); - } - } - const result = await execaNode(context.cliPath, args, execOptions); - // Ensure stdout/stderr are always strings - const stdout = result.stdout ?? ""; - const stderr = result.stderr ?? ""; - - // Log for debugging Windows issues - if (process.env.DEBUG_CLI_TESTS && process.platform === "win32") { - console.log("CLI Result:", { - stdout: stdout.substring(0, 200), - stderr: stderr.substring(0, 200), - exitCode: result.exitCode, - }); - } - return { - stdout, - stderr, + stdout: result.stdout, + stderr: result.stderr, exitCode: result.exitCode ?? 0, }; } catch (error: any) { - // Always capture stdout/stderr even on error - const stdout = error.stdout ?? ""; - const stderr = error.stderr ?? ""; - const exitCode = error.exitCode ?? 1; - if (!expectError) { - // Include more details in error for debugging - const errorDetails = { - message: error.message, - stdout: stdout.substring(0, 500), - stderr: stderr.substring(0, 500), - exitCode, - command: error.command, - cwd: execOptions.cwd, - cliPath: context.cliPath, - args, - // Add more Windows-specific debugging info - nodeVersion: process.version, - platform: process.platform, - arch: process.arch, - // Include the full error object for debugging - fullError: { - name: error.name, - stack: error.stack, - code: error.code, - signal: error.signal, - killed: error.killed, - timedOut: error.timedOut, - } - }; - console.error("CLI execution failed:", errorDetails); - - // On Windows, if we get empty output, it might be a Node.js path issue - if (process.platform === "win32" && !stdout && !stderr) { - console.error("Windows: No output captured. This might be a Node.js execution issue."); - console.error("Attempting direct node execution for diagnostics..."); - - // Try to run with node directly to see if we get better error info - try { - const { execSync } = require('child_process'); - const directResult = execSync(`${process.execPath} "${context.cliPath}" --version`, { - cwd: context.testDir, - encoding: 'utf8', - stdio: 'pipe' - }); - console.error("Direct execution succeeded:", directResult); - } catch (directError: any) { - console.error("Direct execution also failed:", { - message: directError.message, - stdout: directError.stdout?.toString(), - stderr: directError.stderr?.toString(), - status: directError.status, - }); - } - } - throw error; } return { - stdout, - stderr, - exitCode, + stdout: error.stdout || "", + stderr: error.stderr || "", + exitCode: error.exitCode ?? 1, error, }; }