{"record":{"id":"5cb0ac5ce1ceb6ef","repo":"FiloSottile/age","slug":"setconsolemode-failed","errorCode":null,"errorMessage":"SetConsoleMode failed","messagePattern":"SetConsoleMode failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/term/term_windows.go","lineNumber":44,"sourceCode":"\t\t\tENABLE_PROCESSED_OUTPUT            uint32 = 0x1\n\t\t\tENABLE_VIRTUAL_TERMINAL_PROCESSING uint32 = 0x4\n\t\t)\n\n\t\tkernel32DLL := windows.NewLazySystemDLL(\"Kernel32.dll\")\n\t\tsetConsoleMode := kernel32DLL.NewProc(\"SetConsoleMode\")\n\n\t\tvar mode uint32\n\t\tif err := syscall.GetConsoleMode(syscall.Handle(out.Fd()), &mode); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tmode |= ENABLE_PROCESSED_OUTPUT\n\t\tmode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING\n\n\t\t// If the SetConsoleMode function fails, the return value is zero.\n\t\t// See https://learn.microsoft.com/en-us/windows/console/setconsolemode#return-value.\n\t\tif ret, _, _ := setConsoleMode.Call(out.Fd(), uintptr(mode)); ret == 0 {\n\t\t\treturn errors.New(\"SetConsoleMode failed\")\n\t\t}\n\t\treturn nil\n\t}\n}\n","sourceCodeStart":26,"sourceCodeEnd":49,"githubUrl":"https://github.com/FiloSottile/age/blob/b74dce4cdbe35b5e5f66c06d9612b72f89028758/internal/term/term_windows.go#L26-L49","documentation":"On Windows, the terminal enablement code calls the Win32 SetConsoleMode API to turn on processed output and virtual terminal (ANSI) processing for stdout. If the syscall returns zero (failure), it reports \"SetConsoleMode failed\". This typically means the output handle is not a real console (redirected/piped) or the Windows version lacks VT support.","triggerScenarios":"Calling into internal/term's console setup on Windows when out.Fd() is a non-console handle (pipe, file) or the legacy console does not support ENABLE_VIRTUAL_TERMINAL_PROCESSING.","commonSituations":"Running an age plugin with stdout redirected to a file or pipe; very old Windows (pre-Windows 10) consoles; embedded terminal emulators that do not emulate console mode.","solutions":["Check Windows.GetConsoleMode first: only attempt SetConsoleMode when the handle is a real console; treat failure on non-console handles as non-fatal.","Remove ENABLE_VIRTUAL_TERMINAL_PROCESSING and retry with just ENABLE_PROCESSED_OUTPUT on legacy consoles.","Run in a Windows 10+ terminal (Windows Terminal) that supports VT processing."],"exampleFix":"// before\nif ret, _, _ := setConsoleMode.Call(out.Fd(), uintptr(mode)); ret == 0 {\n    return errors.New(\"SetConsoleMode failed\")\n}\n// after\nif ret, _, _ := setConsoleMode.Call(out.Fd(), uintptr(mode)); ret == 0 {\n    return nil // not a console or unsupported: degrade gracefully\n}","handlingStrategy":"fallback","validationCode":"var mode uint32\nif err := windows.GetConsoleMode(windows.Handle(os.Stdout.Fd()), &mode); err != nil {\n    // not a console: skip VT setup entirely\n}","typeGuard":"func isConsole(f *os.File) bool {\n    var mode uint32\n    return windows.GetConsoleMode(windows.Handle(f.Fd()), &mode) == nil\n}","tryCatchPattern":"if err := setupConsole(out); err != nil {\n    // degrade gracefully: ANSI codes may not render, but proceed\n    log.Printf(\"console VT setup unavailable: %v\", err)\n}","preventionTips":["Only attempt SetConsoleMode on handles confirmed to be consoles.","Treat console-mode failure as non-fatal on redirected output.","Test on Windows Terminal and legacy conhost before shipping."],"tags":["windows","terminal","console"],"backgroundTag":"setconsolemode-failed","analyzedSha":"b74dce4cdbe35b5e5f66c06d9612b72f89028758","analyzedAt":"2026-08-31T23:59:31.627Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}