{"record":{"id":"cd22ad9977ce9cf9","repo":"coreybutler/nvm-windows","slug":"failed-to-set-hidden-attribute-w","errorCode":null,"errorMessage":"failed to set hidden attribute: %w","messagePattern":"failed to set hidden attribute: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/upgrade/upgrade.go","lineNumber":1075,"sourceCode":"\nfunc setHidden(path string) error {\n\t// Convert the path to a UTF-16 encoded string\n\tlpFileName, err := syscall.UTF16PtrFromString(path)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to encode path: %w\", err)\n\t}\n\n\t// Call the Windows API function\n\tret, _, err := syscall.NewLazyDLL(\"kernel32.dll\").\n\t\tNewProc(\"SetFileAttributesW\").\n\t\tCall(\n\t\t\tuintptr(unsafe.Pointer(lpFileName)),\n\t\t\tuintptr(FILE_ATTRIBUTE_HIDDEN),\n\t\t)\n\n\t// Check the result\n\tif ret == 0 {\n\t\treturn fmt.Errorf(\"failed to set hidden attribute: %w\", err)\n\t}\n\treturn nil\n}\n","sourceCodeStart":1057,"sourceCodeEnd":1079,"githubUrl":"https://github.com/coreybutler/nvm-windows/blob/5b18223ca19ff50d707f35410dbc6bd440a9f74d/src/upgrade/upgrade.go#L1057-L1079","documentation":"This error is returned when nvm-windows' upgrade routine fails to mark a file as hidden via the Win32 API SetFileAttributesW. The syscall's return value is 0, indicating the attribute was not applied, and the wrapped syscall error (often 'The operation completed successfully' from GetProcAddress-style calls) carries the real reason. Typical causes are missing permissions on the target file, the file being locked by another process, or the path pointer being invalid.","triggerScenarios":"Calling the upgrade path in src/upgrade/upgrade.go that hides the old nvm.exe (or symlink target) after an upgrade, when SetFileAttributesW returns 0: target file is in use (running nvm.exe), path does not exist, or the process lacks WriteAttributes permission on the file.","commonSituations":"Upgrading nvm-windows while an nvm shell/command is still open; antivirus holding the binary; running from a directory where the user lacks NTFS attribute-write rights; UAC-elevated directory but non-elevated nvm.","solutions":["Close all terminals/processes using nvm.exe so the file is not locked, then retry the upgrade.","Run the upgrade from an elevated (Administrator) prompt so SetFileAttributesW has permission.","Verify the file path passed as lpFileName exists and is correctly UTF-16 encoded before the Call.","Check the wrapped 'err' value in the message: a real errno (e.g. ERROR_ACCESS_DENIED) points to permissions; 'operation completed successfully' means ret==0 spuriously — re-check the path argument."],"exampleFix":"// before\nret, _, err := syscall.NewLazyDLL(\"kernel32.dll\").NewProc(\"SetFileAttributesW\").Call(uintptr(unsafe.Pointer(lpFileName)), uintptr(FILE_ATTRIBUTE_HIDDEN))\nif ret == 0 {\n    return fmt.Errorf(\"failed to set hidden attribute: %w\", err)\n}\n\n// after: surface the Win32 error code when ret == 0 and err is the 'success' placeholder\nif ret == 0 {\n    if err == syscall.Errno(0) {\n        return fmt.Errorf(\"failed to set hidden attribute on %s: SetFileAttributesW returned 0 (file locked or inaccessible)\", fileName)\n    }\n    return fmt.Errorf(\"failed to set hidden attribute on %s: %w\", fileName, err)\n}","handlingStrategy":"validation","validationCode":"// Before upgrading / hiding files, confirm the target is not locked and exists\nif _, err := os.Stat(fileName); err != nil {\n    return fmt.Errorf(\"cannot hide %s: %w\", fileName, err)\n}\n// Ensure no running process holds the binary (best-effort on Windows)\ncmd := exec.Command(\"tasklist\", \"/FI\", fmt.Sprintf(\"IMAGENAME eq %s\", filepath.Base(fileName)))\nout, _ := cmd.Output()\nif !strings.Contains(string(out), \"No tasks\") {\n    return errors.New(\"file in use — close processes before upgrading\")\n}","typeGuard":null,"tryCatchPattern":"err := hideFile(path)\nif err != nil {\n    // Non-fatal: hidden attribute is cosmetic for upgrade cleanup\n    log.Printf(\"warning: could not hide %s: %v\", path, err)\n}","preventionTips":["Close all node/npm/nvm processes before running an upgrade.","Run the nvm upgrade from an elevated prompt.","Keep the nvm installation in a user-writable directory so attribute writes succeed."],"tags":["windows","win32-api","filesystem","upgrade","file-attributes"],"backgroundTag":null,"analyzedSha":"5b18223ca19ff50d707f35410dbc6bd440a9f74d","analyzedAt":"2026-08-15T10:06:51.428Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}