Skip to content

fix(ext/node): return first created path from recursive "node:fs" mkdir call - #32300

Merged
bartlomieju merged 12 commits into
denoland:mainfrom
bartlomieju:fix/node-mkdir-recursive-return-value
Mar 2, 2026
Merged

bartlomieju merged 12 commits into
denoland:mainfrom
bartlomieju:fix/node-mkdir-recursive-return-value

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Feb 23, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix fs.mkdir(), fs.mkdirSync(), and fs/promises.mkdir() with { recursive: true } to return the first directory path created, matching Node.js behavior
  • When all directories already exist, correctly returns undefined
  • Enables test-fs-mkdir.js Node compat test

Closes #24165

Node's `fs.mkdir()` with `{ recursive: true }` should return the path of
the first directory created, or `undefined` if no directory was created.
Deno's polyfill was always returning `undefined`. This fixes the callback,
sync, and promise-based APIs.

Closes denoland#24165

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Tango992

Copy link
Copy Markdown
Contributor

I think based on the changes we could try to enable this test:
https://github.com/nodejs/node/blob/v24.12.0/test/parallel/test-fs-mkdir.js

@bartlomieju

Copy link
Copy Markdown
Member Author

Good call, sadly still fails:

error: Uncaught (in promise) AssertionError: Expected values to be strictly deep-equal:
+ actual - expected

  Comparison {
    code: 'ENOENT',
+   message: 'No such file or directory (os error 2)',
+   name: 'NotFound'
-   message: /ENOENT: .*mkdir/,
-   name: 'Error',
-   syscall: 'mkdir'
  }

  assert.throws(
         ^
    at file:///Users/ib/dev/deno/tests/node_compat/runner/suite/test/parallel/test-fs-mkdir.js:226:10
    at Object.<anonymous> (file:///Users/ib/dev/deno/tests/node_compat/runner/suite/test/parallel/test-fs-mkdir.js:366:3)
    at Module._compile (node:module:1045:34)
    at loadMaybeCjs (node:module:1085:10)
    at Object.Module._extensions..js (node:module:1068:12)
    at Module.load (node:module:932:32)
    at Module._load (node:module:734:12)
    at file:///Users/ib/dev/deno/tests/node_compat/runner/suite/test/parallel/test-fs-mkdir.js:5:26

@Tango992 Tango992 left a comment

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.

I have some minor suggestions that (probably) could make the node compat test work

Comment thread ext/node/polyfills/_fs/_fs_mkdir.ts Resolved
Comment thread ext/node/polyfills/_fs/_fs_mkdir.ts Resolved
Comment thread ext/node/polyfills/_fs/_fs_mkdir.ts Resolved
bartlomieju and others added 5 commits February 26, 2026 10:14
Co-authored-by: Daniel Osvaldo Rahmanto <daniel.rahmanto@gmail.com>
Signed-off-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: Daniel Osvaldo Rahmanto <daniel.rahmanto@gmail.com>
Signed-off-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Co-authored-by: Daniel Osvaldo Rahmanto <daniel.rahmanto@gmail.com>
Signed-off-by: Bartek Iwańczuk <biwanczuk@gmail.com>
@bartlomieju
bartlomieju requested a review from Tango992 February 26, 2026 10:08

@Tango992 Tango992 left a comment

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.

Well I guess the node test won't work😂. Let's just revert the commit and merge it

@Tango992

Tango992 commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

Edited the node compat test config to ignore test-fs-mkdir.js on windows

@bartlomieju

Copy link
Copy Markdown
Member Author

I want to give it one more try on Windows before ignoring

bartlomieju and others added 4 commits March 2, 2026 10:28
On Windows, `mkdir` through a file path returns EEXIST instead of
ENOTDIR. This adds a fixup that detects when a path component is a
file and remaps the error code to match Node.js behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bartlomieju

Copy link
Copy Markdown
Member Author

@Tango992 cheers, it appears to work now

@bartlomieju bartlomieju changed the title fix(node/fs): return first created path from recursive mkdir fix(ext/node): return first created path from recursive "node:fs" mkdir call Mar 2, 2026
@bartlomieju
bartlomieju enabled auto-merge (squash) March 2, 2026 10:37
@bartlomieju
bartlomieju merged commit 7765325 into denoland:main Mar 2, 2026
112 checks passed
@bartlomieju
bartlomieju deleted the fix/node-mkdir-recursive-return-value branch March 2, 2026 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mkdir() incorrectly returns undefined and produces unexpected subsequent results

2 participants