{"record":{"id":"52d1d5720f1ac52e","repo":"coreybutler/nvm-windows","slug":"panic-err","errorCode":null,"errorMessage":"panic(err)","messagePattern":"panic\\(err\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/nvm.go","lineNumber":79,"sourceCode":"\nvar env = &Environment{\n\tsettings:        home,\n\troot:            \"\",\n\tsymlink:         symlink,\n\tarch:            strings.ToLower(os.Getenv(\"PROCESSOR_ARCHITECTURE\")),\n\tnode_mirror:     \"\",\n\tnpm_mirror:      \"\",\n\tproxy:           \"none\",\n\toriginalpath:    \"\",\n\toriginalversion: \"\",\n\tverifyssl:       true,\n}\n\nfunc writeToErrorLog(i interface{}, abort ...bool) {\n\texe, _ := os.Executable()\n\tfile, err := os.OpenFile(filepath.Join(filepath.Dir(exe), \"error.log\"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tdefer file.Close()\n\n\tmsg := fmt.Sprintf(\"%v\\n\", i)\n\tif _, ferr := file.WriteString(msg); ferr != nil {\n\t\tpanic(ferr)\n\t}\n\n\tif len(abort) > 0 && abort[0] {\n\t\tfmt.Println(msg)\n\t\tos.Exit(1)\n\t}\n}\n\ntype Notification struct {\n\tAppID    string   `json:\"app_id\"`\n\tTitle    string   `json:\"title\"`\n\tMessage  string   `json:\"message\"`","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/coreybutler/nvm-windows/blob/5b18223ca19ff50d707f35410dbc6bd440a9f74d/src/nvm.go#L61-L97","documentation":"writeToErrorLog() is nvm-windows' last-resort logger: it opens error.log next to the nvm executable. If os.OpenFile fails, it panics — crashing the whole command with a Go runtime panic instead of a clean error. It fires when the nvm installation directory is not writable: installed under Program Files without elevation, directory locked down by policy, or NVM_HOME on read-only media.","triggerScenarios":"Any code path that logs via writeToErrorLog while the folder containing nvm.exe denies write access — most commonly running nvm non-elevated after installing to C:\\Program Files\\nodejs or similar protected locations.","commonSituations":"Corporate machines with locked-down Program Files; nvm.exe run from a read-only network share; NVM_HOME on a full or write-protected drive; antivirus blocking creation of error.log.","solutions":["Run the nvm command from an elevated (Administrator) prompt so error.log can be created.","Install/relocate nvm to a user-writable directory (e.g. %APPDATA%\\nvm or C:\\nvm) and update NVM_HOME/NVM_SYMLINK and PATH.","Verify the directory holding nvm.exe is writable: try creating a file there manually.","As a code fix, replace panic with a graceful fallback (stderr + os.Exit(1)) so logging failure never crashes harder than the original error."],"exampleFix":"// before\nfile, err := os.OpenFile(filepath.Join(filepath.Dir(exe), \"error.log\"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)\nif err != nil {\n    panic(err)\n}\n\n// after: degrade gracefully — never crash the tool because logging failed\nfile, err := os.OpenFile(filepath.Join(filepath.Dir(exe), \"error.log\"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)\nif err != nil {\n    fmt.Fprintf(os.Stderr, \"nvm: %v (additionally, could not write error.log: %v)\\n\", i, err)\n    if len(abort) > 0 && abort[0] {\n        os.Exit(1)\n    }\n    return\n}","handlingStrategy":"fallback","validationCode":"// Before running nvm, verify its directory is writable\nexe, _ := os.Executable()\nprobe := filepath.Join(filepath.Dir(exe), \".write-probe\")\nif f, err := os.Create(probe); err != nil {\n    return errors.New(\"nvm directory not writable — run elevated or relocate nvm\")\n} else { f.Close(); os.Remove(probe) }","typeGuard":null,"tryCatchPattern":"// Wrap nvm invocations and treat a Go panic in writeToErrorLog as a permissions problem\nout, err := cmd.CombinedOutput()\nif err != nil && strings.Contains(string(out), \"panic: \") && strings.Contains(string(out), \"error.log\") {\n    return errors.New(\"nvm cannot write error.log — run from an elevated prompt\")\n}","preventionTips":["Install nvm in a user-writable directory (e.g. C:\\nvm or %APPDATA%\\nvm), not Program Files.","Run nvm elevated when its home directory is protected.","Ensure NVM_HOME points to a writable, mounted drive."],"tags":["windows","logging","panic","permissions","installation"],"backgroundTag":null,"analyzedSha":"5b18223ca19ff50d707f35410dbc6bd440a9f74d","analyzedAt":"2026-08-15T10:06:51.428Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}