{"record":{"id":"177e3521823e65da","repo":"abiosoft/colima","slug":"cannot-read-file-s-w","errorCode":null,"errorMessage":"cannot read file '%s': %w","messagePattern":"cannot read file '(.+?)': %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"environment/vm/lima/file.go","lineNumber":19,"sourceCode":"package lima\n\nimport (\n\t\"bytes\"\n\t\"fmt\"\n\t\"io/fs\"\n\t\"os\"\n\t\"path/filepath\"\n\t\"strconv\"\n\t\"strings\"\n\t\"time\"\n\n\t\"github.com/abiosoft/colima/environment\"\n)\n\nfunc (l limaVM) Read(fileName string) (string, error) {\n\ts, err := l.RunOutput(\"sudo\", \"cat\", fileName)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"cannot read file '%s': %w\", fileName, err)\n\t}\n\treturn s, err\n}\n\nfunc (l *limaVM) Write(fileName string, body []byte) error {\n\tvar stdin = bytes.NewReader(body)\n\tdir := filepath.Dir(fileName)\n\tif err := l.RunQuiet(\"sudo\", \"mkdir\", \"-p\", dir); err != nil {\n\t\treturn fmt.Errorf(\"error creating directory '%s': %w\", dir, err)\n\t}\n\treturn l.RunWith(stdin, nil, \"sudo\", \"sh\", \"-c\", \"cat > \"+fileName)\n}\n\nfunc (l *limaVM) Stat(fileName string) (os.FileInfo, error) {\n\treturn newFileInfo(l, fileName)\n}\n\nvar _ os.FileInfo = (*fileInfo)(nil)","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/abiosoft/colima/blob/c3a5f9184d83a197184f897a9f07eb3c01b3bc88/environment/vm/lima/file.go#L1-L37","documentation":"limaVM.Read is the environment's file-read primitive: it runs `sudo cat <fileName>` inside the guest over SSH and wraps any failure as 'cannot read file'. The wrapped error typically includes the non-zero exit of cat (file missing, permission issues even under sudo, I/O errors) or the underlying SSH/guest execution failure. Any Colima feature that reads a guest file (certs, runtime config detection) funnels through here.","triggerScenarios":"Reading a guest path that does not exist (e.g. a config file not yet created by the runtime); calling Read while the VM is stopping/stopped so the SSH command fails; guest disk I/O errors.","commonSituations":"Automation scripts probing optional files in the guest; races where a file is read before the guest service creates it; profile in a broken half-started state.","solutions":["Confirm the VM is actually running (`colima status`) before issuing reads.","Check the exact guest path exists via `colima ssh -- sudo ls -la <path>` and create/fix it if missing.","Retry after a full stop/start if the guest was in a transient state.","In caller code, treat ENOENT-style wrapped errors for optional files as non-fatal rather than aborting."],"exampleFix":"// before\nbody, err := vm.Read(\"/etc/containerd/config.toml\")\nif err != nil { return err } // aborts on missing optional file\n\n// after\nbody, err := vm.Read(\"/etc/containerd/config.toml\")\nif err != nil {\n    if strings.Contains(err.Error(), \"No such file\") { body = \"\" } else { return err }\n}","handlingStrategy":"validation","validationCode":"// guard reads: vm must be running, and optional files tolerated as absent\nif !vm.Running(ctx) { return errors.New(\"vm not running\") }\nif _, err := vm.Stat(fileName); err != nil && isOptional(fileName) { return \"\", nil }","typeGuard":null,"tryCatchPattern":"Go: read, then on error inspect the chain; for optional configs treat ENOENT-like messages (\"No such file\") as empty result, propagate the rest.","preventionTips":["Check colima status before guest file reads","Distinguish missing-file from ssh failures in handlers","Retry after full stop/start when the guest is mid-boot"],"tags":["file-read","guest","ssh","colima","filesystem"],"backgroundTag":null,"analyzedSha":"c3a5f9184d83a197184f897a9f07eb3c01b3bc88","analyzedAt":"2026-08-15T18:58:08.334Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}