1

I have an asar file that is read from an electron app. From the same app, I want to delete this asar file, however I keep getting an EBUSY error. I have found only two places where this asar file is read:

  • A file protocol. It reads a file that is in the asar. foo://fileinasar.js for example. fileinasar.js is a file that was packed into the asar. In order to run this file, it is done by import("foo://fileinasar.js"). This import is stored in an object, but when I do delete this object property, it still locks. (this file is run in renderer) Did some debugging and found out that this isn't causing it to lock
  • Or in the main process when I use fs/promises to readFile the same fileinasar.js file. However, this is only used once and I don't think it should be able to lock it while the project is running. However, readFile should automatically close the file anyway.

These are the only two ways the asar file is read in my project. Neither seem like they should lock it long term.

I have no idea how to unlock the asar package. I found several other potential solutions online, however none helped, or they said to use original-fs, which I want to try to avoid. I have also tried electron/asar's uncache and uncacheAll but that also didn't unlink the file.
How can I unlock the asar so that I can delete it in the same project? This is using electron ^21.2.0 and only happens on windows (I'm not deleting the anti-malware to solve this).

Edit: When I try using the spawn "hack" that @Anonymoose provided, I get this enter image description here
When I try the alternative unlink method, I get thisenter image description here

12944qwerty
  • 2,001
  • 1
  • 10
  • 30
  • 1
    Please clarify: Why do you want to delete the ASAR file? Is your application packaged for distribution? – Alexander Leithner Feb 09 '23 at 10:08
  • @AlexanderLeithner These asar files are plugins, users can choose to delete them while the app is running. Currently, the application isn't packaged, but it will be later. It's just built with esbuild. – 12944qwerty Feb 09 '23 at 14:08

1 Answers1

-3

In my case it helped to get rid of fs.existsSync(asarPath)

I just replaced it with try{} catch(err:any){}