{"record":{"id":"efd537c931a7a054","repo":"sinelaw/fresh","slug":"failed-to-open-conin","errorCode":null,"errorMessage":"Failed to open CONIN$: {}","messagePattern":"Failed to open CONIN\\$: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/fresh-editor/src/main.rs","lineNumber":835,"sourceCode":"\n    // \"CONIN$\" is the console input device on Windows\n    // This is analogous to /dev/tty on Unix\n    let conin: Vec<u16> = \"CONIN$\\0\".encode_utf16().collect();\n\n    let conin_handle = unsafe {\n        CreateFileW(\n            conin.as_ptr(),\n            FILE_GENERIC_READ,\n            FILE_SHARE_READ,\n            std::ptr::null(),\n            OPEN_EXISTING,\n            0,\n            std::ptr::null_mut(),\n        )\n    };\n\n    if conin_handle == INVALID_HANDLE_VALUE {\n        anyhow::bail!(\"Failed to open CONIN$: {}\", io::Error::last_os_error());\n    }\n\n    // Replace stdin with the console input handle\n    let success = unsafe { SetStdHandle(STD_INPUT_HANDLE, conin_handle) };\n    if success == 0 {\n        anyhow::bail!(\n            \"Failed to set stdin to CONIN$: {}\",\n            io::Error::last_os_error()\n        );\n    }\n\n    Ok(())\n}\n\nfn handle_first_run_setup(\n    editor: &mut Editor,\n    args: &Args,\n    file_locations: &[FileLocation],","sourceCodeStart":817,"sourceCodeEnd":853,"githubUrl":"https://github.com/sinelaw/fresh/blob/67894ca5463dbd7a89bb31add4627c27d6b79d83/crates/fresh-editor/src/main.rs#L817-L853","documentation":"On Windows, the editor opens CONIN$ (the console input device) with CreateFileW to use as a fresh stdin source. If CreateFileW returns INVALID_HANDLE_VALUE the OS error is reported. This happens when no console is attached to the process or access to the console device is denied.","triggerScenarios":"CreateFileW(\"CONIN$\", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, security_attributes, OPEN_EXISTING, 0, null) returns INVALID_HANDLE_VALUE — typically when the process has no attached console.","commonSituations":"Launching the editor from a GUI/launcher without a console, from a Windows service, or in an environment where console devices are unavailable (some ssh sessions without terminal allocation).","solutions":["Run the editor from a console session (cmd/Windows Terminal) so CONIN$ exists","Allocate a console first with AllocConsole or attach to the parent's console with AttachConsole(ATTACH_PARENT_PROCESS)","For ssh scenarios, request a PTY/pseudo-terminal allocation (ssh -t)","Check the reported last_os_error for access-denied and adjust how the process is spawned"],"exampleFix":"// before\nlet conin_handle = unsafe { CreateFileW(CONIN$, GENERIC_READ|GENERIC_WRITE, SHARE_RW, null, OPEN_EXISTING, 0, null_mut()) };\n// after\nunsafe { AllocConsole(); } // ensure a console exists before opening CONIN$\nlet conin_handle = unsafe { CreateFileW(CONIN$, GENERIC_READ|GENERIC_WRITE, SHARE_RW, null, OPEN_EXISTING, 0, null_mut()) };\nif conin_handle == INVALID_HANDLE_VALUE {\n    anyhow::bail!(\"Failed to open CONIN$: {}\", io::Error::last_os_error());\n}","handlingStrategy":"fallback","validationCode":"fn console_attached() -> bool {\n    unsafe { GetConsoleWindow() != std::ptr::null_mut() }\n}","typeGuard":"fn is_valid_handle(h: HANDLE) -> bool { h != INVALID_HANDLE_VALUE && !h.is_null() }","tryCatchPattern":"match open_conin() {\n    Ok(h) => use_conin(h),\n    Err(e) => { unsafe { AllocConsole(); } retry_or_report(e) }\n}","preventionTips":["Attach or allocate a console (AttachConsole/AllocConsole) before console I/O","For ssh use PTY allocation (ssh -t)","Launch from cmd/Windows Terminal rather than GUI launchers"],"tags":["windows","console","conin"],"backgroundTag":"file-open-failed","analyzedSha":"67894ca5463dbd7a89bb31add4627c27d6b79d83","analyzedAt":"2026-09-13T15:04:03.701Z","contentChangedAt":"2026-09-13T15:04:03.701Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}