{"record":{"id":"5cf2d51c673708eb","repo":"cilium/cilium","slug":"failed-to-open-original-source-file-for-conversion","errorCode":null,"errorMessage":"failed to open original source file for conversion: %w","messagePattern":"failed to open original source file for conversion: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tools/spdxconv/main.go","lineNumber":102,"sourceCode":"\t}\n\tvar spdx string\n\tfor exp, tmp := range RegexpMap {\n\t\tif exp.MatchString(cg.Text()) {\n\t\t\tspdx = tmp\n\t\t}\n\t}\n\tif spdx == \"\" {\n\t\tlog.Printf(\"could not determine license for %v\", path)\n\t\treturn nil\n\t}\n\n\tcopyRight := cg.List[0].Text\n\tcg.List[0].Text, cg.List[1].Text = spdx, copyRight\n\tcg.List = cg.List[:2]\n\n\tfd, err := os.OpenFile(path, os.O_TRUNC|os.O_RDWR, 0660)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to open original source file for conversion: %w\", err)\n\t}\n\tdefer fd.Close()\n\n\terr = format.Node(fd, fset, f)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to write converted file %v: %w\", path, err)\n\t}\n\n\treturn nil\n}\n\nfunc main() {\n\tswitch {\n\tcase len(os.Args) != 2:\n\t\tfmt.Print(usage)\n\t\tos.Exit(1)\n\tcase os.Args[1] == \"help\":\n\t\tfmt.Print(usage)","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/cilium/cilium/blob/ac7b90affa4baf0642e6685319d56907b3a73a6d/tools/spdxconv/main.go#L84-L120","documentation":"After rewriting the copyright/SPDX comment lines in the parsed AST, conv opens the original source file with os.OpenFile(path, os.O_TRUNC|os.O_RDWR, 0660) to rewrite it in place. If the file cannot be opened — missing permissions, read-only filesystem, or the file vanished between parsing and opening — this wrapped error is returned.","triggerScenarios":"os.OpenFile fails on the target .go file: no write permission (non-root running against files owned by another user), the file was deleted after parsing, a device/permission mismatch, or O_TRUNC on a path that is not a regular file.","commonSituations":"Running spdxconv in CI as a non-privileged user against a checked-out tree with restricted permissions; read-only container mounts; converting files under /proc or similar pseudo-filesystems.","solutions":["Check and fix file permissions: chmod u+w <file> or run the tool as the file owner.","Ensure the filesystem/mount is writable (remount rw, drop the read-only flag in CI).","Verify the file still exists; re-run the tool on a clean checkout."],"exampleFix":"// before\nchmod 444 pkg/foo.go && spdxconv ./pkg\n// after\nchmod 644 pkg/foo.go && spdxconv ./pkg","handlingStrategy":"validation","validationCode":"info, err := os.Stat(path)\nif err != nil || !info.Mode().IsRegular() {\n\treturn fmt.Errorf(\"%s not a writable regular file\", path)\n}\nif f, err := os.OpenFile(path, os.O_WRONLY, 0); err != nil { return err } else { f.Close() }","typeGuard":null,"tryCatchPattern":"if err := conv(path, fset, f); err != nil {\n\tif errors.Is(err, fs.ErrPermission) {\n\t\t// escalate or skip file\n\t}\n\treturn err\n}","preventionTips":["Run the tool as a user with write access to all target files.","Ensure the workspace mount is read-write (CI checkout, container volumes).","Stat paths before conversion to confirm they are regular writable files."],"tags":["go","filesystem","permissions","file-io"],"backgroundTag":"file-open-permission-denied","analyzedSha":"ac7b90affa4baf0642e6685319d56907b3a73a6d","analyzedAt":"2026-08-31T18:27:15.868Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}