{"record":{"id":"f1520dd80a009116","repo":"apache/answer","slug":"failed-to-create-destination-file-s-w","errorCode":null,"errorMessage":"failed to create destination file %s: %w","messagePattern":"failed to create destination file (.+?): %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cli/build.go","lineNumber":543,"sourceCode":"\n\t\tif d.IsDir() {\n\t\t\t// Create the directory in the destination\n\t\t\terr := os.MkdirAll(dstPath, os.ModePerm)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to create directory %s: %w\", dstPath, err)\n\t\t\t}\n\t\t} else {\n\t\t\t// Open the source file\n\t\t\tsrcFile, err := sourceFs.Open(srcPath)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to open source file %s: %w\", srcPath, err)\n\t\t\t}\n\t\t\tdefer srcFile.Close()\n\n\t\t\t// Create the destination file\n\t\t\tdstFile, err := os.Create(dstPath)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to create destination file %s: %w\", dstPath, err)\n\t\t\t}\n\t\t\tdefer dstFile.Close()\n\n\t\t\t// Copy the file contents\n\t\t\t_, err = io.Copy(dstFile, srcFile)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to copy file contents from %s to %s: %w\", srcPath, dstPath, err)\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n\n\treturn err\n}\n\n// format plugins dir name from dash to underline\nfunc formatUIPluginsDirName(dirPath string) {","sourceCodeStart":525,"sourceCodeEnd":561,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/cli/build.go#L525-L561","documentation":"Returned by the copy helper in internal/cli/build.go when os.Create(dstPath) fails after the source file was opened successfully. os.Create fails when the destination directory does not exist, the process lacks write permission, dstPath is a directory, or the path is invalid for the OS. The wrapped %w error carries the exact OS reason.","triggerScenarios":"dstPath = filepath.Join(targetDir, path) where targetDir's parent directories were not created (MkdirAll only creates directories for walked dirs, and a skipped ignoreThisDir dir can leave parents missing), targetDir is read-only, or dstPath collides with an existing directory.","commonSituations":"Deploying UI/plugin assets into a target dir owned by another user; running the binary in a container with a read-only mount; ignore rules excluding a parent directory so its children's destination folders never get created.","solutions":["Read the wrapped OS error: 'no such file or directory' means a parent dir is missing; 'permission denied' means fix ownership/chmod","Ensure os.MkdirAll(filepath.Dir(dstPath), os.ModePerm) is called before os.Create for every file","Check that targetDir exists and is writable by the process user (ls -ld, or touch a test file)","Verify dstPath is not itself an existing directory"],"exampleFix":"// before\ndstFile, err := os.Create(dstPath)\n// after\nif err := os.MkdirAll(filepath.Dir(dstPath), os.ModePerm); err != nil {\n    return fmt.Errorf(\"failed to create directory %s: %w\", filepath.Dir(dstPath), err)\n}\ndstFile, err := os.Create(dstPath)","handlingStrategy":"validation","validationCode":"// ensure destination parent exists and is writable before creating the file\nif err := os.MkdirAll(filepath.Dir(dstPath), os.ModePerm); err != nil {\n    return err\n}\nif info, err := os.Stat(filepath.Dir(dstPath)); err != nil || !info.IsDir() {\n    return fmt.Errorf(\"destination dir %s not writable\", filepath.Dir(dstPath))\n}","typeGuard":null,"tryCatchPattern":"dstFile, err := os.Create(dstPath)\nif err != nil {\n    var perr *fs.PathError\n    if errors.As(err, &perr) && errors.Is(perr.Err, syscall.EACCES) {\n        log.Printf(\"permission denied creating %s — check ownership/uid in container\", dstPath)\n    }\n    return err\n}","preventionTips":["Always MkdirAll(filepath.Dir(dstPath)) before os.Create in copy loops","Run build/deploy processes as a user with write access to the target directory","In containers, mount the target volume read-write and set correct UID/GID"],"tags":["go","filesystem","permissions","build"],"backgroundTag":"permission-denied","analyzedSha":"3b9f1370612e690a0b7f230f05e688930db4c6d3","analyzedAt":"2026-09-05T18:18:39.533Z","contentChangedAt":"2026-09-05T18:18:39.533Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}