fs: rmSync() reports UV_UNKNOWN with an empty code for unmapped errno values
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.4k
- 平均合并
- 4 天 3 小时
- 30 天内合并 PR
- 272
描述
Version
v26.7.0
Platform
Linux x64
Subsystem
fs / src/node_file.cc
What steps will reproduce the bug?
const fs = require('node:fs');
fs.mkdirSync('a/b', { recursive: true });
try {
fs.rmSync('a/.', { recursive: true, force: true });
} catch (err) {
console.log('code :', JSON.stringify(err.code));
console.log('errno :', err.errno);
console.log('syscall:', err.syscall);
console.log('message:', err.message);
}
Output:
code : ""
errno : -4094
syscall: "rm"
message: , Unknown error: Invalid argument 'a/.'
What is the expected behavior? Why is that the expected behavior?
err.code should be 'EINVAL' and err.errno should be -22. The underlying
failure is a plain EINVAL from rmdir(2), which POSIX requires for a path whose
final component is ..
Every other fs error carries a usable code, and err.code === 'EINVAL' is the
documented way to branch on fs failures, so an empty string breaks ordinary error
handling. The message is also malformed: it begins with , because the empty code
is interpolated into it.
What do you see instead?
errno is -4094 (UV_UNKNOWN) and code is the empty string.
Additional information
This is not specific to EINVAL. RmSync() in src/node_file.cc translates the
std::error_code from std::filesystem::remove_all() with a hardcoded chain that
covers four values:
operation_not_permitted->EPERMdirectory_not_empty->ENOTEMPTYnot_a_directory->ENOTDIRpermission_denied->EACCES(EPERMon Windows)
Anything else falls through to the tail of the function:
std::string message = "Unknown error: " + error.message();
return env->ThrowErrnoException(
UV_UNKNOWN, "rm", message.c_str(), path_c_str);
So the mapping is an allowlist rather than a translation, and any errno outside
those four surfaces as UV_UNKNOWN with an empty code. EINVAL is simply the
one that is easiest to trigger from user code.
The async form is unaffected, because fs.rm() and fsPromises.rm() use the JS
rimraf and report EINVAL correctly.
Noting for context that the dot segment handling in #61958 is being addressed
separately at the JS layer. That change stops a/. from reaching this code path,
but it does not fix the mapping, which stays reachable for other errnos.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
阅读 src/node_file.cc 中的 RmSync(),跟踪 std::filesystem::remove_all() 返回的 std::error_code 如何变成 Node.js 错误。为类似 EINVAL 的未映射 errno 添加回归覆盖,验证返回的 code、errno 和 message 是可用的,而不是带有空 code 的 UV_UNKNOWN。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- cpp, javascript, nodejs
- 领域
- operating-systems
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 活跃
- 描述清晰度
- 描述清楚
- 新手友好度
- 76/100