{"record":{"id":"150b1fd31a6a7194","repo":"nodejs/node","slug":"failed-to-open-output-file-s-s-n","errorCode":null,"errorMessage":"failed to open output file [%s]: %s\\n","messagePattern":"failed to open output file \\[(.+?)\\]: (.+?)\\\\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"deps/brotli/c/tools/brotli.c","lineNumber":806,"sourceCode":"    fprintf(stderr, \"failed to open input file [%s]: %s\\n\",\n            PrintablePath(input_path), strerror(errno));\n    return BROTLI_FALSE;\n  }\n  return BROTLI_TRUE;\n}\n\nstatic BROTLI_BOOL OpenOutputFile(const char* output_path, FILE** f,\n                                  BROTLI_BOOL force) {\n  int fd;\n  *f = NULL;\n  if (!output_path) {\n    *f = fdopen(MAKE_BINARY(STDOUT_FILENO), \"wb\");\n    return BROTLI_TRUE;\n  }\n  fd = open(output_path, O_CREAT | (force ? 0 : O_EXCL) | O_WRONLY | O_TRUNC,\n            S_IRUSR | S_IWUSR);\n  if (fd < 0) {\n    fprintf(stderr, \"failed to open output file [%s]: %s\\n\",\n            PrintablePath(output_path), strerror(errno));\n    return BROTLI_FALSE;\n  }\n  *f = fdopen(fd, \"wb\");\n  if (!*f) {\n    fprintf(stderr, \"failed to open output file [%s]: %s\\n\",\n            PrintablePath(output_path), strerror(errno));\n    return BROTLI_FALSE;\n  }\n  return BROTLI_TRUE;\n}\n\nstatic int64_t FileSize(const char* path) {\n  FILE* f = fopen(path, \"rb\");\n  int64_t retval;\n  if (f == NULL) {\n    return -1;\n  }","sourceCodeStart":788,"sourceCodeEnd":824,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/brotli/c/tools/brotli.c#L788-L824","documentation":"`OpenOutputFile` failed at the `open()` system call (fd returned < 0). The `open` uses `O_CREAT | O_WRONLY | O_TRUNC` plus `O_EXCL` unless `--force` was passed, meaning it refuses to clobber an existing file by default. The message shows the path and the errno description.","triggerScenarios":"The output file already exists and `--force` (`-f`) was not provided, triggering EEXIST via O_EXCL. Alternatively: the parent directory does not exist (ENOENT), write permission denied (EACCES), disk full (ENOSPC at open time on some filesystems), or read-only filesystem (EROFS).","commonSituations":"Re-running a compress/decompress command without `-f`; piping output into a directory that was never created; running as a different user than the one that owns the existing output file.","solutions":["Add `--force` / `-f` to allow overwriting the existing output file.","Delete or rename the existing output file first.","Verify the output directory exists and is writable.","Use `--output -` to write to stdout if no output file is needed."],"exampleFix":"// before\nbrotli input.txt  # input.txt.br already exists\n// after\nbrotli -f input.txt","handlingStrategy":"validation","validationCode":"#include <unistd.h>\n#include <stdbool.h>\n\nbool can_write_output(const char *path, bool force) {\n    if (!path) return true; // stdout\n    if (!force && access(path, F_OK) == 0) {\n        // File exists and --force not set -> O_EXCL will fail\n        return false;\n    }\n    // Check parent dir is writable\n    // (simplified; in production extract dirname and check)\n    return true;\n}\n\n// Before brotli: if (!can_write_output(out, force)) { ... }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pre-check for output file existence and pass `-f` if overwrite is intended.","Ensure the output directory exists and is writable before running brotli.","In pipelines, prefer `--output -` or `-c` to avoid output-file conflicts."],"tags":["filesystem","io","brotli"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}