{"record":{"id":"2afb20b7ab996e05","repo":"lima-vm/lima","slug":"failed-to-replace-original-with-raw-image-w","errorCode":null,"errorMessage":"failed to replace original with raw image: %w","messagePattern":"failed to replace original with raw image: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/downloader/downloader.go","lineNumber":500,"sourceCode":"\t\treturn \"\", \"\", fmt.Errorf(\"failed to open raw tmp file %q: %w\", rawPathTmp, err)\n\t}\n\tfi, err := rawTmpF.Stat()\n\tif err != nil {\n\t\t_ = rawTmpF.Close()\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to stat raw tmp file %q: %w\", rawPathTmp, err)\n\t}\n\tif err := diskUtil.MakeSparse(ctx, rawTmpF, fi.Size()); err != nil {\n\t\tlogrus.WithError(err).Warnf(\"Failed to make %q sparse (non-fatal)\", rawPathTmp)\n\t}\n\tif err := rawTmpF.Close(); err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to close raw tmp file %q: %w\", rawPathTmp, err)\n\t}\n\n\tif err := os.Remove(rawImgConvPath); err != nil && !errors.Is(err, os.ErrNotExist) {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to remove stale raw image %q: %w\", rawImgConvPath, err)\n\t}\n\tif err := os.Rename(rawPathTmp, rawImgConvPath); err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to replace original with raw image: %w\", err)\n\t}\n\n\talgo := digest.Canonical\n\tif originalDigest != \"\" {\n\t\talgo = originalDigest.Algorithm()\n\t}\n\trawDigest, err := calculateFileDigest(rawImgConvPath, algo)\n\tif err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to calculate digest of raw image: %w\", err)\n\t}\n\n\trawDigestPath := filepath.Join(imgConvPath, \"raw.digest\")\n\trawDigestPathTmp := rawDigestPath + \".tmp\"\n\tdefer os.Remove(rawDigestPathTmp)\n\tif err := os.WriteFile(rawDigestPathTmp, []byte(rawDigest.String()), 0o644); err != nil {\n\t\treturn \"\", \"\", fmt.Errorf(\"failed to write raw digest file: %w\", err)\n\t}\n\tif err := os.Remove(rawDigestPath); err != nil && !errors.Is(err, os.ErrNotExist) {","sourceCodeStart":482,"sourceCodeEnd":518,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/downloader/downloader.go#L482-L518","documentation":"After removing any stale entry, ensureRawInCache atomically renames imgconv/raw.tmp to imgconv/raw. If os.Rename fails (raw.tmp vanished, cross-device rename, permissions), the converted image cannot replace the cached original and this error is returned. The defer os.Remove(rawPathTmp) cleans up the tmp file afterwards.","triggerScenarios":"os.Rename(rawPathTmp, rawImgConvPath) returns ENOENT (tmp deleted concurrently by another downloader process), EXDEV (cache spans filesystems), or EACCES on the directory.","commonSituations":"Two limactl processes downloading the same image race and one's deferred cleanup deletes the other's raw.tmp; cache directory placed on a filesystem where rename across the imgconv dir fails; antivirus/indexers removing tmp files.","solutions":["Avoid running multiple limactl downloads of the same image concurrently against the same cache; retry sequentially","Verify the cache directory is on a single local filesystem so rename is atomic","Check directory write permissions on the imgconv folder and fix with chown/chmod","Clear the affected cache entry (<hash> dir) and re-download to get a consistent state"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"err := downloadOnce()\nif err != nil && strings.Contains(err.Error(), \"failed to replace original with raw image\") {\n    time.Sleep(2 * time.Second)\n    err = downloadOnce() // racing tmp cleanup is transient; second attempt usually succeeds\n}","preventionTips":["Don't run concurrent limactl commands against the same LIMA_HOME for the same image","Keep the cache on a single local filesystem","Exclude the Lima cache directory from antivirus/indexer scanning","Retry once on transient rename failures"],"tags":["go","filesystem","cache","race-condition"],"backgroundTag":"rename-atomic-replace-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}