Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
ToNamespacedPath(env, &path);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemWrite, path.ToStringView());
auto file_path = std::filesystem::path(path.ToStringView());
auto file_path = path.ToPath();
std::error_code error;
auto file_status = std::filesystem::status(file_path, error);

Expand All @@ -1649,8 +1649,7 @@ static void RmSync(const FunctionCallbackInfo<Value>& args) {
// File is a directory and recursive is false
if (file_status.type() == std::filesystem::file_type::directory &&
!recursive) {
return THROW_ERR_FS_EISDIR(
isolate, "Path is a directory: %s", file_path.c_str());
return THROW_ERR_FS_EISDIR(isolate, "Path is a directory: %s", path);
}

// Allowed errors are:
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-fs-rmSync-special-char.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';
require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('node:assert');
const fs = require('node:fs');
const path = require('node:path');

// This test ensures that fs.rmSync handles non-ASCII characters in file paths,
// and that errors contain correctly encoded paths and err.path values.

tmpdir.refresh(); // Prepare a clean temporary directory

// Define paths with non-ASCII characters
const dirPath = path.join(tmpdir.path, '速_dir');
const filePath = path.join(tmpdir.path, '速.txt');

// Create a directory and a file with non-ASCII characters
fs.mkdirSync(dirPath);
fs.writeFileSync(filePath, 'This is a test file with special characters.');
fs.rmSync(filePath);
assert.strictEqual(fs.existsSync(filePath), false);

// Ensure rmSync throws an error when trying to remove a directory without recursive
assert.throws(() => {
fs.rmSync(dirPath, { recursive: false });
}, (err) => {
// Assert the error code and check that the error message includes the correct non-ASCII path
assert.strictEqual(err.code, 'ERR_FS_EISDIR');
assert(err.message.includes(dirPath), 'Error message should include the directory path');
assert.strictEqual(err.path, dirPath);
return true;
});
Loading