oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.12k stars 2.76k forks source link

`bun patch` fails with EACCES Permission denied mkdir() when adding new files in nested directory #13330

Open shaunek-hero opened 2 months ago

shaunek-hero commented 2 months ago

What version of Bun is running?

1.1.21+70ca2b76c

What platform is your computer?

Darwin 23.5.0 arm64 arm

What steps can reproduce the bug?

Add any package that has multiple levels of directories, this specific test is with the typescript package. Add a random file to any existing directory in the package that is two levels deep.

bun add typescript
bun patch typescript
# ADD a file in any directory that is child directory of typescript/lib, contents can be anything.
vim node_modules/typescript/lib/es/test.txt
bun patch --commit 'node_modules/typescript'

What is the expected behavior?

After running the bun patch --commit command there should be no error, and the file that was created (ie node_modules/typescript/lib/es/test.txt) should be in place.

What do you see instead?

Shell output is like this:

bun patch v1.1.21 (70ca2b76)
error: failed applying patch file: EACCES: lib/es: Permission denied (mkdir())

Checked 8 installs across 10 packages (no changes) [586.00ms]
error: failed to apply patchfile (patches/typescript@5.5.4.patch)

It did successfully edit the package.json file and it created this patch file:

diff --git a/node_modules/typescript/lib/es/test.txt b/lib/es/test.txt
new file mode 100644
index 0000000000000000000000000000000000000000..cca7f3bea8af2ec95ec0f2ca6331f9437d515919
--- /dev/null
+++ b/lib/es/test.txt
@@ -0,0 +1,2 @@
+This is in the lib/es dir
+

However, when it attempts to apply the patch file it fails. Repeated attempts to delete the package and run bun install result in the same error: failed applying patch file: EACCES: lib/es: Permission denied (mkdir()) error.

Additional information

When I try to add a file to the root of the package (ie, node_modules/typescript/) or to a subdirectory (ie, node_modules/typescript/lib) it seems to work just fine. This only seems to arise when the file is being added to a directory multiple levels deep. Note that editing files that are multiple levels deep works fine as well, it is only when creating a new file.

I've replicated this problem on a couple of different packages so far. My actual target package is called qbo and interestingly on that package I am able to add a file within the node_modules/qbo/src/entities/ dir, but I cannot add any files to the node_modules/qbo/dist/entities dir nor can I add to the node_modules/qbo/dist/lib dir.

A couple additional notes:

  1. I tried this same package edit with pnpm patch and it worked without a problem.
  2. I took the patch file that bun created and I and applied it "manually" with patch -p1 < typescript@5.5.4.patch and that worked without a problem.
KilianB commented 2 months ago

Potentially Related: #12882

rmarscher commented 2 months ago

Using v1.1.25 on MacOS 14.5, I patched a few files in the dist/lib/builder folder of a package and then received an error when committing the patch:

error: failed applying patch file: EACCES: dist/lib/builder: Permission denied (mkdir())

It created the patch successfully. The error happens when trying to apply the patch. It seems to be doing something in the cwd. It added a dist folder in my top-level folder. Running ls on that folder gives an error:

% ls dist
ls: fts_read: Permission denied

It created the folder with 644 permissions instead of 755. If I chmod 755 dist and then run bun i again, it fails again with the same error. But now I have a dist/lib folder with 644 permissions. If I jump ahead and create all of the folders that were modified in the package in my project's top-level cwd with proper 755 permissions and run bun i, the patch is applied without error. I can then delete the dist folder and bun i continues to run without error due to having cached the patched package.

shaunek-hero commented 2 months ago

Interesting @rmarscher. I tried out your steps and replicated.

I guess a difference in your use case vs mine is that in my case the package already had a dist/some-dir directory and bun patch commit wouldn't let me create any files in that dir (although I could edit files in the same dir), and in your case the package didn't even have a dist directory to start with and weirdly bun patch commit tried to create directories within your project directory instead of in the package itself. Seems like these could be related but could be slightly different bugs I suppose?

whistlecube commented 2 months ago

Still seeing this on MacOS with bun version 1.1.27-canary.62

erickreutz commented 3 weeks ago

Also seeing this on 1.1.30