-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
fs: fix rmSync error messages for non-ASCII paths #61233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yeaseen
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
Yeaseen:fix-additional-error-message-rmsync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−24
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| #include "v8.h" | ||
|
|
||
| #include <cstring> | ||
| #include <string_view> | ||
|
|
||
| namespace node { | ||
|
|
||
|
|
@@ -20,6 +21,33 @@ using v8::Object; | |
| using v8::String; | ||
| using v8::Value; | ||
|
|
||
| static Local<String> StringFromPath(Isolate* isolate, std::string_view path) { | ||
| auto to_v8 = [&](std::string_view s) { | ||
| return String::NewFromUtf8(isolate, | ||
| s.data(), | ||
| v8::NewStringType::kNormal, | ||
| static_cast<int>(s.size())) | ||
| .ToLocalChecked(); | ||
| }; | ||
|
|
||
| #ifdef _WIN32 | ||
| constexpr std::string_view kUncPrefix = "\\\\?\\UNC\\"; | ||
| constexpr std::string_view kLongPrefix = "\\\\?\\"; | ||
|
|
||
| if (path.starts_with(kUncPrefix)) { | ||
| return String::Concat(isolate, | ||
| FIXED_ONE_BYTE_STRING(isolate, "\\\\"), | ||
| to_v8(path.substr(kUncPrefix.size()))); | ||
|
Comment on lines
+38
to
+40
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need to call concat. you can just concat them on C++ by copying the string using |
||
| } | ||
|
|
||
| if (path.starts_with(kLongPrefix)) { | ||
| return to_v8(path.substr(kLongPrefix.size())); | ||
| } | ||
| #endif | ||
|
|
||
| return to_v8(path); | ||
| } | ||
|
|
||
| Local<Value> ErrnoException(Isolate* isolate, | ||
| int errorno, | ||
| const char* syscall, | ||
|
|
@@ -42,7 +70,7 @@ Local<Value> ErrnoException(Isolate* isolate, | |
| Local<String> path_string; | ||
| if (path != nullptr) { | ||
| // FIXME(bnoordhuis) It's questionable to interpret the file path as UTF-8. | ||
| path_string = String::NewFromUtf8(isolate, path).ToLocalChecked(); | ||
| path_string = StringFromPath(isolate, std::string_view(path)); | ||
| } | ||
|
|
||
| if (path_string.IsEmpty() == false) { | ||
|
|
@@ -72,22 +100,6 @@ Local<Value> ErrnoException(Isolate* isolate, | |
| return e; | ||
| } | ||
|
|
||
| static Local<String> StringFromPath(Isolate* isolate, const char* path) { | ||
| #ifdef _WIN32 | ||
| if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) { | ||
| return String::Concat( | ||
| isolate, | ||
| FIXED_ONE_BYTE_STRING(isolate, "\\\\"), | ||
| String::NewFromUtf8(isolate, path + 8).ToLocalChecked()); | ||
| } else if (strncmp(path, "\\\\?\\", 4) == 0) { | ||
| return String::NewFromUtf8(isolate, path + 4).ToLocalChecked(); | ||
| } | ||
| #endif | ||
|
|
||
| return String::NewFromUtf8(isolate, path).ToLocalChecked(); | ||
| } | ||
|
|
||
|
|
||
| Local<Value> UVException(Isolate* isolate, | ||
| int errorno, | ||
| const char* syscall, | ||
|
|
@@ -114,7 +126,7 @@ Local<Value> UVException(Isolate* isolate, | |
| js_msg = String::Concat(isolate, js_msg, js_syscall); | ||
|
|
||
| if (path != nullptr) { | ||
| js_path = StringFromPath(isolate, path); | ||
| js_path = StringFromPath(isolate, std::string_view(path)); | ||
|
|
||
| js_msg = | ||
| String::Concat(isolate, js_msg, FIXED_ONE_BYTE_STRING(isolate, " '")); | ||
|
|
@@ -124,7 +136,7 @@ Local<Value> UVException(Isolate* isolate, | |
| } | ||
|
|
||
| if (dest != nullptr) { | ||
| js_dest = StringFromPath(isolate, dest); | ||
| js_dest = StringFromPath(isolate, std::string_view(dest)); | ||
|
|
||
| js_msg = String::Concat( | ||
| isolate, js_msg, FIXED_ONE_BYTE_STRING(isolate, " -> '")); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
test/parallel/test-fs-rmSync-special-char-additional-error.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 'use strict'; | ||
| require('../common'); | ||
| const tmpdir = require('../common/tmpdir'); | ||
| const assert = require('node:assert'); | ||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
|
|
||
| tmpdir.refresh(); | ||
|
|
||
| const file = path.join(tmpdir.path, '速_file'); | ||
| fs.writeFileSync(file, 'x'); | ||
|
|
||
| // Treat that file as if it were a directory so the error message | ||
| // includes the path treated as a directory, not the file. | ||
| const badPath = path.join(file, 'child'); | ||
|
|
||
| // Attempt to delete the directory which should now fail | ||
| try { | ||
| fs.rmSync(badPath, { recursive: true }); | ||
| } catch (err) { | ||
| // Verify that the error is due to the path being treated as a directory | ||
| assert.strictEqual(err.code, 'ENOTDIR'); | ||
| assert.strictEqual(err.path, badPath); | ||
| assert(err.message.includes(badPath), 'Error message should include the path treated as a directory'); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have this method:
node/src/util-inl.h
Line 344 in 94b1f66
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anonrig one thing to confirm: do you want this to use
ToV8Valueinstead of callingString::NewFromUtf8directly? just confirming the preferred approach.Like, doing these steps?
getting the context first,
Local<Context> context = isolate->GetCurrentContext();... then after c++ concatenation using
std::string s;and append,at the time of return this way
Local<Value> v; if (!ToV8Value(context, s, isolate).ToLocal(&v)) return Local<String>(); return v.As<String>();