{"record":{"id":"9effe84a5c2290a7","repo":"kubernetes/kops","slug":"unable-to-open-file-s-error-v","errorCode":null,"errorMessage":"unable to open file: %s, error: %v","messagePattern":"unable to open file: (.+?), error: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/kops/toolbox_template.go","lineNumber":219,"sourceCode":"\t\t\tif len(data) <= 0 {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tformatted, err := yaml.Marshal(&data)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"unable to marhshal formatted content to yaml: %s\", err)\n\t\t\t}\n\t\t\tdocuments = append(documents, string(formatted))\n\t\t}\n\t}\n\t// join in harmony all the YAML documents back together\n\tcontent := strings.Join(documents, \"---\\n\")\n\n\tiowriter := out\n\t// @check if we are writing to a file rather than stdout\n\tif options.outputPath != \"\" {\n\t\tw, err := os.OpenFile(utils.ExpandPath(options.outputPath), os.O_RDWR|os.O_TRUNC|os.O_CREATE, 0o660)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to open file: %s, error: %v\", options.outputPath, err)\n\t\t}\n\t\tdefer try.CloseFile(w)\n\t\tiowriter = w\n\t}\n\n\tif _, err := iowriter.Write([]byte(content)); err != nil {\n\t\treturn fmt.Errorf(\"unable to write template: %s\", err)\n\t}\n\n\treturn nil\n}\n\n// newTemplateContext is responsible for loading the --values and build a context for the template\nfunc newTemplateContext(files []string, values []string, stringValues []string) (map[string]interface{}, error) {\n\tcontext := make(map[string]interface{})\n\n\tfor _, x := range files {\n\t\tlist, err := expandFiles(utils.ExpandPath(x))","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/cmd/kops/toolbox_template.go#L201-L237","documentation":"When --out/-o specifies an output file, RunToolBoxTemplate opens it with os.OpenFile (O_RDWR|O_TRUNC|O_CREATE, 0660). If the file cannot be opened, the command fails with this message including the path and OS error. Common causes are permissions, missing parent directories, or the path being a directory.","triggerScenarios":"Running `kops toolbox template --out <path>` where the path's directory does not exist, the user lacks write permission, the path is a directory, or the filesystem is read-only.","commonSituations":"Typo in output directory; running without permissions in CI containers; output path pointing at an existing directory; mounted read-only volume.","solutions":["Create the parent directory: `mkdir -p $(dirname <path>)`.","Check permissions/ownership on the target path and that it is not a directory.","Write to a writable location or run with appropriate user/permissions."],"exampleFix":"// before\nkops toolbox template --out /nonexistent/dir/out.yaml\n// after\nmkdir -p /nonexistent/dir && kops toolbox template --out /nonexistent/dir/out.yaml","handlingStrategy":"validation","validationCode":"dir := filepath.Dir(outPath)\nif _, err := os.Stat(dir); os.IsNotExist(err) { os.MkdirAll(dir, 0o755) }\nif fi, err := os.Stat(outPath); err == nil && fi.IsDir() { /* choose a file path */ }","typeGuard":null,"tryCatchPattern":"if err := run(\"kops\", \"toolbox\", \"template\", \"--out\", p); strings.Contains(err.Error(), \"unable to open file\") { check perms / mkdir -p and retry }","preventionTips":["mkdir -p the output directory before invoking the command","Use absolute output paths","Ensure the running user has write permission on the target directory"],"tags":["cli","filesystem","file-io"],"backgroundTag":"file-open-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}