{"record":{"id":"fdd389fe8afc5273","repo":"hashicorp/nomad","slug":"failed-to-create-file-q-err-w","errorCode":null,"errorMessage":"failed to create file %q, err: %w","messagePattern":"failed to create file %q, err: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/operator_debug.go","lineNumber":1417,"sourceCode":"\tdirPath := filepath.Join(c.collectDir, dir)\n\tfilePath := filepath.Join(dirPath, filename)\n\n\t// Ensure parent directories exist\n\terr := escapingfs.EnsurePath(dirPath, true)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create parent directories of %q: %w\", dirPath, err)\n\t}\n\n\t// Ensure filename doesn't escape the sandbox of the capture directory\n\tescapes := escapingfs.PathEscapesSandbox(c.collectDir, filePath)\n\tif escapes {\n\t\treturn fmt.Errorf(\"file path %q escapes capture directory %q\", filePath, c.collectDir)\n\t}\n\n\t// Create the file\n\tfh, err := os.Create(filePath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create file %q, err: %w\", filePath, err)\n\t}\n\tdefer fh.Close()\n\n\t_, err = fh.Write(data)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"Failed to write data to file %q, err: %w\", filePath, err)\n\t}\n\treturn nil\n}\n\n// newFilePath returns a validated filepath rooted in the provided directory and\n// path. It has been checked that it falls inside the sandbox and has been added\n// to the manifest tracking.\nfunc (c *OperatorDebugCommand) newFilePath(dir, file string) (string, error) {\n\n\t// Replace invalid characters in filename\n\tfilename := helper.CleanFilename(file, \"_\")\n","sourceCodeStart":1399,"sourceCodeEnd":1435,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/operator_debug.go#L1399-L1435","documentation":"Thrown by `writeBytes` when `os.Create(filePath)` fails after the sandbox check has passed, meaning the file could not be opened for writing. Nomad wraps the OS error (permission denied, too many open files, is-a-directory, no space left) with the full file path.","triggerScenarios":"os.Create failure inside the capture directory: permissions deny the user write access even though dirs were created, a directory already exists with the target filename, ulimit -n exhausted (too many open files), or disk full.","commonSituations":"Debug run as non-privileged user into a root-owned capture dir; long-running debug collections leaking file descriptors until EMFILE hits; a stale directory named like the expected capture file.","solutions":["Read the wrapped OS error: EACCES → fix permissions on collectDir; EISDIR → remove the directory at that filename; EMFILE → raise `ulimit -n`.","Check free disk space (`df -h`) — ENOSPC produces this error.","Re-run the debug command as a user with write access to the -output directory.","Check for fd leaks in long-lived processes performing many captures (`lsof -p <pid> | wc -l`)."],"exampleFix":"// before (shell)\nnomad operator debug -output /var/lib/nomad/debug   # root-owned\n// after\nsudo chown $(id -u):$(id -g) /var/lib/nomad/debug && nomad operator debug -output /var/lib/nomad/debug","handlingStrategy":"try-catch","validationCode":"if info, err := os.Stat(targetPath); err == nil && info.IsDir() {\n    return fmt.Errorf(\"target %q is a directory\", targetPath)\n}\nprobe := filepath.Join(collectDir, \".write-test\")\nif err := os.WriteFile(probe, nil, 0o600); err != nil {\n    return fmt.Errorf(\"collect dir not writable: %w\", err)\n}\nos.Remove(probe)","typeGuard":null,"tryCatchPattern":"if err := c.writeBytes(dir, filename, resp, respErr); err != nil {\n    var pe *fs.PathError\n    if errors.As(err, &pe) && errors.Is(pe.Err, syscall.EMFILE) {\n        // raise open-file limit or serialize captures\n    }\n    c.writeError(dir, err)\n}","preventionTips":["Ensure the invoking user owns or can write to the capture directory.","Raise `ulimit -n` for processes running many concurrent captures.","Monitor free disk space before and during debug collection.","Remove stale directories that collide with expected archive filenames."],"tags":["filesystem","permissions","file-create","debug"],"backgroundTag":"file-create-permission-denied","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}