Skip to content

fs: accept all valid utf8 values in fast paths#64341

Open
hamidrezaghavami wants to merge 2 commits into
nodejs:mainfrom
hamidrezaghavami:fix-fs-utf8-fastpath
Open

fs: accept all valid utf8 values in fast paths#64341
hamidrezaghavami wants to merge 2 commits into
nodejs:mainfrom
hamidrezaghavami:fix-fs-utf8-fastpath

Conversation

@hamidrezaghavami

Copy link
Copy Markdown
Contributor

Fixes: #49888

This PR introduces a lightweight isUtf8Encoding helper to ensure fs.readFileSync, fs.writeFileSync, and encodeRealpathResult use the fast C++ path for all valid variations of the utf8 encoding string (utf8, utf-8, UTF8, UTF-8).

Signed-off-by: Hamid Reza Ghavami <hamidr.ghavami@gmail.com>
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module. labels Jul 7, 2026
@hamidrezaghavami hamidrezaghavami force-pushed the fix-fs-utf8-fastpath branch 2 times, most recently from bfaade2 to 1d3f916 Compare July 7, 2026 17:40
@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.24%. Comparing base (e6a8d06) to head (fccaa91).
⚠️ Report is 348 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64341      +/-   ##
==========================================
- Coverage   92.01%   90.24%   -1.77%     
==========================================
  Files         379      741     +362     
  Lines      166972   241706   +74734     
  Branches    25554    45562   +20008     
==========================================
+ Hits       153639   218128   +64489     
- Misses      13041    15107    +2066     
- Partials      292     8471    +8179     
Files with missing lines Coverage Δ
lib/fs.js 98.36% <100.00%> (+3.76%) ⬆️
lib/internal/util/inspect.js 96.94% <100.00%> (+25.39%) ⬆️

... and 551 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@anonrig

anonrig commented Jul 7, 2026

Copy link
Copy Markdown
Member

I don't think we should land this. The more checks we have, the more performance impact it would create. Do you have any examples out there that is impacted by this? Does people really pass UTF8? If not, let's close this.

@hamidrezaghavami

Copy link
Copy Markdown
Contributor Author

Hi @anonrig thanks for the review!

I completely understand the concern regarding the performance impact on the fast path. I picked this up to resolve issue #49888, as there seemed to be some inconsistency in how variations like utf-8 (which is common in HTTP headers/HTML meta tags) bypass the C++ fast path and fall back to the slower route.

That said, if the consensus is that the branch/check overhead for the 99% of users passing standard utf8 outweighs the benefit for the edge cases, I am perfectly happy to close this PR. Let me know what you think is best for the module!

Signed-off-by: Hamid Reza Ghavami <hamidr.ghavami@gmail.com>
@hamidrezaghavami

Copy link
Copy Markdown
Contributor Author

The commit message linting and sign-off trailer are fixed, and all CI checks are passing! Could someone from the @nodejs/fs team please take a look when you have a moment? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. util Issues and PRs related to the built-in util module.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs: utf8 fast paths don't accept all valid utf8 values

3 participants