{"record":{"id":"4c83eda56d45fd99","repo":"apache/answer","slug":"failed-to-create-directory-s-w","errorCode":null,"errorMessage":"failed to create directory %s: %w","messagePattern":"failed to create directory (.+?): %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cli/build.go","lineNumber":530,"sourceCode":"\t\tif ignoreThisDir(path) {\n\t\t\treturn nil\n\t\t}\n\n\t\t// Convert the path to use forward slashes, important because we use embedded FS which always uses forward slashes\n\t\tpath = filepath.ToSlash(path)\n\n\t\t// Construct the absolute path for the source file/directory\n\t\tsrcPath := filepath.Join(sourceDir, path)\n\t\tsrcPath = filepath.ToSlash(srcPath)\n\n\t\t// Construct the absolute path for the destination file/directory\n\t\tdstPath := filepath.Join(targetDir, path)\n\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)","sourceCodeStart":512,"sourceCodeEnd":548,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/cli/build.go#L512-L548","documentation":"Inside copyDirEntries' fs.WalkDir callback, for every directory entry the destination directory is created with os.MkdirAll(dstPath, os.ModePerm). This error wraps a failed MkdirAll for the destination path — the target directory could not be created (or an intermediate parent exists as a file). It is the first failure point of any directory copy done by the build (UI files, plugins, vendor moves).","triggerScenarios":"os.MkdirAll(dstPath, os.ModePerm) fails during a WalkDir over the source FS: destination parent path exists as a regular file, EACCES/EACCES-like permission denial on the target volume, ENOSPC or filesystem errors, or an overly long path.","commonSituations":"Previous build left a file where this build expects to create a directory (classic ENOTDIR); running the build in a read-only container layer or as a non-privileged user; tmp dir on a full/quota-limited disk; copying a dir tree whose deep paths exceed the filesystem's NAME_MAX/path limits.","solutions":["Look at the wrapped %w cause and the %s path: if a file exists where a directory is needed, delete that stale file and re-run.","Ensure the build user has write permission on the target parent directory (`chmod u+w` / run as the directory owner).","Free disk space or raise the quota on the volume holding the build tmp dir.","Clear the whole tmp build directory and rebuild from scratch to eliminate leftovers from prior failed runs."],"exampleFix":"// caller-side prevention: start from a clean target\nos.RemoveAll(targetDir)\nos.MkdirAll(targetDir, os.ModePerm)\nerr := copyDirEntries(os.DirFS(src), \".\", targetDir, \"node_modules\")","handlingStrategy":"try-catch","validationCode":"if fi, err := os.Lstat(targetDir); err == nil && !fi.IsDir() {\n\treturn fmt.Errorf(\"%s exists and is not a directory; remove it\", targetDir)\n}\nparent := filepath.Dir(targetDir)\nprobe := filepath.Join(parent, \".mkdir-probe\")\nif err := os.WriteFile(probe, nil, 0o600); err != nil {\n\treturn fmt.Errorf(\"cannot write to %s: %w\", parent, err)\n}\nos.Remove(probe)","typeGuard":"func canMkdir(target string) bool {\n\t// true if target is absent (creatable) or already a directory\n\tfi, err := os.Lstat(target)\n\tif err != nil { return errors.Is(err, os.ErrNotExist) }\n\treturn fi.IsDir()\n}","tryCatchPattern":"var perr *fs.PathError\nerr := copyDirEntries(sourceFs, sourceDir, targetDir, \"node_modules\")\nif errors.As(err, &perr) && errors.Is(perr.Err, syscall.ENOTDIR) {\n\tos.RemoveAll(perr.Path) // stale file where a dir is needed\n\terr = copyDirEntries(sourceFs, sourceDir, targetDir, \"node_modules\")\n}\nif err != nil {\n\tlog.Printf(\"mkdir/copy failed: %v — check permissions and disk space on %s\", err, targetDir)\n}\nreturn err","preventionTips":["Start every build from a clean tmp/target directory so no file sits where a directory must be created.","Run builds as a user with write access to the destination volume; avoid read-only container layers.","Monitor disk space and inode usage before long builds.","Keep source paths short enough to stay under filesystem path/NAME_MAX limits.","Fix the copyDirEntries wrapper to include the wrapped path in the message for faster diagnosis."],"tags":["filesystem","mkdir","permissions","build"],"backgroundTag":"mkdir-failed","analyzedSha":"3b9f1370612e690a0b7f230f05e688930db4c6d3","analyzedAt":"2026-09-05T18:18:39.533Z","contentChangedAt":"2026-09-05T18:18:39.533Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}