Skip to content

fix: handle EEXIST error when creating directories in parallel with node-to-fsa adapter#1205

Merged
streamich merged 3 commits into
masterfrom
copilot/fix-97ba9c15-6996-496b-a9a0-a4699dbcd7ac
Oct 1, 2025
Merged

fix: handle EEXIST error when creating directories in parallel with node-to-fsa adapter#1205
streamich merged 3 commits into
masterfrom
copilot/fix-97ba9c15-6996-496b-a9a0-a4699dbcd7ac

Conversation

Copilot AI commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

Problem

When calling getDirectoryHandle() with {create: true} in parallel for the same folder, the Node.js to FSA adapter was throwing an EEXIST error:

await Promise.all([
  dir.getDirectoryHandle('test', {create: true}),
  dir.getDirectoryHandle('test', {create: true}),
])
// Error: EEXIST: file already exists, mkdir '/test'

This occurred because both parallel calls would:

  1. Check if the directory exists (stat returns ENOENT)
  2. Attempt to create the directory (mkdir)
  3. The second mkdir fails with EEXIST

Solution

Modified NodeFileSystemDirectoryHandle.getDirectoryHandle() to handle the EEXIST error gracefully. When mkdir throws EEXIST during directory creation:

  1. Verify the path is actually a directory using stat
  2. If it's a directory, return a handle to it (no error)
  3. If it's a file, throw TypeMismatchError as before

This aligns with the File System Access API specification where creating a directory that already exists should succeed when using the create option.

Changes

  • src/node-to-fsa/NodeFileSystemDirectoryHandle.ts: Added try-catch around mkdir to handle EEXIST error
  • src/node-to-fsa/tests/NodeFileSystemDirectoryHandle.test.ts: Added test case for parallel folder creation

Testing

  • ✅ New test validates parallel folder creation works correctly
  • ✅ All existing tests pass (83 tests in NodeFileSystemDirectoryHandle, 130 in node-to-fsa module)
  • ✅ Type checking, linting, and formatting pass
  • ✅ Backward compatibility maintained - existing behavior unchanged for all other scenarios

Closes #97ba9c15-6996-496b-a9a0-a4699dbcd7ac

Original prompt

This section details on the original issue you should resolve

<issue_title>Handle existing folders</issue_title>
<issue_description>In Node.js to FS(A) adapter, it should not throw when the same folder is created in parallel. Now it throws EEXIST error.

await Promise.all([
  dir.getDirectoryHandle('test', {create: true}),
  dir.getDirectoryHandle('test', {create: true}),
])
```</issue_description>

<agent_instructions>In `src/node-to-fsa/` make sure when folder is being created but already exists, it does not throw.
</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>
Fixes #1204

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Handle existing folders fix: handle EEXIST error when creating directories in parallel with node-to-fsa adapter Oct 1, 2025
Copilot AI requested a review from streamich October 1, 2025 16:45
@streamich streamich marked this pull request as ready for review October 1, 2025 16:48
@streamich streamich merged commit 363e253 into master Oct 1, 2025
12 checks passed
@streamich streamich deleted the copilot/fix-97ba9c15-6996-496b-a9a0-a4699dbcd7ac branch October 1, 2025 16:55
@github-actions

github-actions Bot commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 4.48.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle existing folders

2 participants