Skip to content

fs: return Buffer from mkdtemp when prefix is a Buffer#64397

Open
hamidrezaghavami wants to merge 2 commits into
nodejs:mainfrom
hamidrezaghavami:fix-fs-mkdtemp-buffer
Open

fs: return Buffer from mkdtemp when prefix is a Buffer#64397
hamidrezaghavami wants to merge 2 commits into
nodejs:mainfrom
hamidrezaghavami:fix-fs-mkdtemp-buffer

Conversation

@hamidrezaghavami

Copy link
Copy Markdown
Contributor

This PR fixes an issue where passing a Buffer as the prefix to the fs.mkdtemp family of functions incorrectly returned a string, which could mangle non-UTF8 paths.

The options encoding is now safely forced to 'buffer' when a Buffer prefix is detected for both the asynchronous, synchronous, and disposable versions.

Fixes: #58795

@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. labels Jul 9, 2026
@bakkot

bakkot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Thanks!

  • Should be BufferIsBuffer not Buffer.isBuffer
  • Can't mutate the options object; you'll need to do options = { ...options, encoding: buffer } or something
  • Needs tests
  • Possibly it should be an error if encoding is explicitly specified (and not buffer)?

@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.24%. Comparing base (8fec65d) to head (3e3da50).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/fs/promises.js 50.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64397      +/-   ##
==========================================
- Coverage   90.25%   90.24%   -0.01%     
==========================================
  Files         741      741              
  Lines      241207   241225      +18     
  Branches    45430    45442      +12     
==========================================
+ Hits       217698   217704       +6     
+ Misses      15084    15082       -2     
- Partials     8425     8439      +14     
Files with missing lines Coverage Δ
lib/fs.js 98.36% <100.00%> (+<0.01%) ⬆️
lib/internal/fs/promises.js 92.69% <50.00%> (-0.13%) ⬇️

... and 30 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.

@Renegade334 Renegade334 added the semver-major PRs that contain breaking changes and should be released in the next major version. label Jul 9, 2026
@hamidrezaghavami hamidrezaghavami force-pushed the fix-fs-mkdtemp-buffer branch 4 times, most recently from 40f2ae2 to 3e3da50 Compare July 10, 2026 10:27
@hamidrezaghavami

Copy link
Copy Markdown
Contributor Author

@bakkot The CI is green! Here is a quick update:

Buffer check: Switched to using the internal BufferIsBuffer.
Immutability: Fixed the code to avoid mutating the user's options object.
Regression test: Added a full test suite for the Buffer prefix (test-fs-mkdtemp-buffer.js).
Original tests: Patched a string coercion bug in the original test-fs-mkdtemp.js file that was crashing the CI.

Let me know if there is anything else I need to adjust!

fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.')), {}, common.mustCall(handler));

// Warning fires only once
fs.mkdtemp(Buffer.from(tmpdir.resolve('bar.X')), common.mustCall(handler));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happened to these tests?

@bakkot

bakkot commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

I'm not a maintainer but the new tests look good, thanks.

Still not sure about, should it be an error if encoding is explicitly specified and not buffer. I don't know if this comes up anywhere else; if so we could look at what the established pattern is. If there isn't an established pattern I would be inclined to throw here.

Signed-off-by: Hamid Reza Ghavami <hamidr.ghavami@gmail.com>
@hamidrezaghavami hamidrezaghavami requested a review from a team as a code owner July 10, 2026 16:49
@hamidrezaghavami

Copy link
Copy Markdown
Contributor Author

My mistake! I accidentally deleted them while fixing the string coercion bug above. Just restored them in the latest push.

@hamidrezaghavami hamidrezaghavami force-pushed the fix-fs-mkdtemp-buffer branch 3 times, most recently from a3b52bb to f06208f Compare July 10, 2026 18:32
@hamidrezaghavami

Copy link
Copy Markdown
Contributor Author

@nodejs-github-bot All JavaScript changes and tests are perfectly clean locally!
However, I am getting a format-cpp and lint-js-and-md error from the recent upstream merge conflict on SECURITY.md and node_trace_buffer.h. Could a maintainer please help run the formatters for me since I don't have the local C++ tooling installed? (Also, test-internet appears to be a flaky failure).
Ready for your final review!

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. semver-major PRs that contain breaking changes and should be released in the next major version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs.mkdtemp should return a Buffer when prefix argument is a Buffer

4 participants