{"record":{"id":"df4e45a9aa5f404c","repo":"JanDeDobbeleer/oh-my-posh","slug":"failed-to-convert-file-path-to-utf16-v","errorCode":null,"errorMessage":"failed to convert file path to UTF16: %v","messagePattern":"failed to convert file path to UTF16: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cache/file_map_windows.go","lineNumber":163,"sourceCode":"\t\treturn nil, fmt.Errorf(\"failed to get file size\")\n\t}\n\n\tactualSize := int(fileSize) - 5 // Subtract header (4 bytes length + 1 null terminator)\n\tif actualSize < requiredSize {\n\t\t// Existing file is too small, close and recreate\n\t\t_, _, _ = closeHandle.Call(fileHandle)\n\t\treturn nil, fmt.Errorf(\"existing file is too small (%d < %d)\", actualSize, requiredSize)\n\t}\n\n\treturn createMappingFromFileWithSize(filePath, fileHandle, actualSize)\n}\n\n// Created with FILE_SHARE_READ|FILE_SHARE_WRITE so subsequent concurrent\n// opens by other processes don't fail with a sharing violation.\nfunc createNewFileWithSize(filePath string, size int) (*PersistentSharedString, error) {\n\tfilePathPtr, err := syscall.UTF16PtrFromString(filePath)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to convert file path to UTF16: %v\", err)\n\t}\n\n\t// Create new file\n\tfileHandle, _, err := createFileW.Call(\n\t\tuintptr(unsafe.Pointer(filePathPtr)), // lpFileName\n\t\tgenericRead|genericWrite,             // dwDesiredAccess\n\t\tfileShareRead|fileShareWrite,         // dwShareMode\n\t\t0,                                    // lpSecurityAttributes\n\t\tcreateAlways,                         // dwCreationDisposition (overwrites if exists)\n\t\tfileAttributeNormal,                  // dwFlagsAndAttributes\n\t\t0,                                    // hTemplateFile\n\t)\n\n\tif fileHandle == uintptr(0xFFFFFFFFFFFFFFFF) { // INVALID_HANDLE_VALUE\n\t\tif errno, ok := err.(syscall.Errno); ok && uintptr(errno) == errorSharingViolation {\n\t\t\tlog.Debugf(\"cache file %s locked by another process during create\", filePath)\n\t\t\treturn nil, ErrLocked\n\t\t}","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/cache/file_map_windows.go#L145-L181","documentation":"On Windows, the cache layer's createNewFileWithSize converts the file path to a UTF-16 pointer via syscall.UTF16PtrFromString before calling CreateFileW. If the path contains an unpaired surrogate or otherwise can't be represented as valid UTF-16, it fails with 'failed to convert file path to UTF16: %v'.","triggerScenarios":"createOrOpenFile/createOrOpenPersistentStringWithSize is invoked with a cache file path containing invalid UTF-8 sequences or an embedded NUL character, making UTF16PtrFromString fail (ErrInvalidUtf8 or unexpected NUL).","commonSituations":"Cache directory (TMP/USERPROFILE/XDG_CACHE_HOME) containing non-representable characters from a corrupted env var; a username with invalid encoding; a path built by concatenating user input containing a NUL byte.","solutions":["Fix the cache directory path env var (TMP, TEMP, USERPROFILE, XDG_CACHE_HOME) to a valid location","Inspect the printed path for a NUL byte or invalid characters and remove its source","Point the cache to a plain ASCII path via the appropriate oh-my-posh cache env setting","Clear any corrupted cache path configuration and recreate the cache directory"],"exampleFix":"// before: TMP set to a path with a NUL byte\nTMP=C:\\Users\\bad\\x00cache\n// after\nTMP=C:\\Users\\me\\AppData\\Local\\Temp","handlingStrategy":"validation","validationCode":"// validate the path before handing it to the API (Go)\nfunc validUTF16Path(p string) bool {\n    _, err := syscall.UTF16PtrFromString(p)\n    return err == nil\n}","typeGuard":null,"tryCatchPattern":"// wrap creation and fall back to a sanitized path\nps, err := createNewFileWithSize(filePath, size)\nif err != nil {\n    clean := filepath.Clean(strings.ReplaceAll(filePath, \"\\x00\", \"\"))\n    ps, err = createNewFileWithSize(clean, size)\n}","preventionTips":["Keep TMP/TEMP/USERPROFILE free of invalid characters","Never concatenate user input with NUL bytes into file paths","Sanitize cache paths with filepath.Clean and UTF-8 validation","Point the cache at a stable ASCII directory on Windows"],"tags":["windows","cache","filesystem","encoding"],"backgroundTag":"invalid-file-path-encoding","analyzedSha":"0976794618c5ed95de0985dded50de1b4dc914cb","analyzedAt":"2026-08-31T23:41:19.708Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}