{"record":{"id":"1f7e8bec69de3cbf","repo":"yorukot/superfile","slug":"failed-to-initialize-json-file-s-w","errorCode":null,"errorMessage":"failed to initialize json file %s: %w","messagePattern":"failed to initialize json file (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/pkg/utils/file_utils.go","lineNumber":317,"sourceCode":"\t}\n\tvar sb strings.Builder\n\tlastSegmentStart := 0\n\tfor _, r := range line {\n\t\tif r == '\\t' {\n\t\t\tnewSegmentSize := ansi.StringWidth(sb.String()[lastSegmentStart:])\n\t\t\tsb.WriteString(strings.Repeat(\" \", TabWidth-newSegmentSize%TabWidth))\n\t\t\tlastSegmentStart = sb.Len()\n\t\t\tcontinue\n\t\t}\n\t\tsb.WriteRune(r)\n\t}\n\treturn sb.String()\n}\n\nfunc InitJSONFile(path string) error {\n\tif _, err := os.Stat(path); os.IsNotExist(err) {\n\t\tif err = os.WriteFile(path, []byte(\"null\"), ConfigFilePerm); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to initialize json file %s: %w\", path, err)\n\t\t}\n\t}\n\treturn nil\n}\n","sourceCodeStart":299,"sourceCodeEnd":322,"githubUrl":"https://github.com/yorukot/superfile/blob/b72f550bc6e75913f48eb49fdd258e1a2df2fe88/src/pkg/utils/file_utils.go#L299-L322","documentation":"InitJSONFile creates a JSON file initialized to the literal `null` if it doesn't exist yet, writing with os.WriteFile and ConfigFilePerm; this error wraps a failure of that write. A `null` root is valid JSON that unmarshals into nil pointers/slices, which the pinned-file manager expects. The failure is filesystem-related, not JSON-related.","triggerScenarios":"NewPinnedFileManager calling InitJSONFile when the pinned-list file's parent directory doesn't exist or isn't writable, the path exists as a directory, the disk is full, or the process lacks permission.","commonSituations":"First-run initialization in a read-only or non-existent state directory; pinned-file path configured under a directory never created; running the app under a user that can't write the config dir; containerized setups with ephemeral/read-only volumes.","solutions":["Read the wrapped error for path and errno; check whether the parent directory exists (`ls -la $(dirname <path>)`).","Create the parent directory (CreateDirectories) before InitJSONFile.","Verify write permission on the directory for the runtime user; fix ownership/chmod.","Ensure the path isn't an existing directory; correct the configured path if so.","At the call site, fail gracefully (e.g. start with an empty pinned list) and log the reason."],"exampleFix":"// before\nerr := utils.InitJSONFile(pinnedPath)\n// after\nif err := utils.CreateDirectories(filepath.Dir(pinnedPath)); err != nil { return err }\nif err := utils.InitJSONFile(pinnedPath); err != nil {\n    return fmt.Errorf(\"init pinned list %s: %w\", pinnedPath, err)\n}","handlingStrategy":"validation","validationCode":"func ensureJSONCreatable(path string) error {\n    dir := filepath.Dir(path)\n    if info, err := os.Stat(dir); err != nil || !info.IsDir() {\n        return fmt.Errorf(\"parent dir %s missing\", dir)\n    }\n    f, err := os.CreateTemp(dir, \".t*\")\n    if err != nil { return err }\n    f.Close(); os.Remove(f.Name())\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := utils.InitJSONFile(pinnedPath); err != nil {\n    log.Warnf(\"pinned list unavailable: %v\", err)\n    pinned = nil // degrade to empty pinned list\n}","preventionTips":["Create the parent directory before initializing JSON state files.","Confirm state dirs survive restarts and are writable (avoid read-only volumes).","Treat `null` as a valid empty state when loading so first-run works.","Log the failing path from the wrapped error for quick triage."],"tags":["json","file-write","filesystem","permissions"],"backgroundTag":"file-write-failed","analyzedSha":"b72f550bc6e75913f48eb49fdd258e1a2df2fe88","analyzedAt":"2026-09-01T04:38:08.254Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}