sindresorhus / electron-store

Simple data persistence for your Electron app or module - Save and load user preferences, app state, cache, etc
MIT License
4.6k stars 150 forks source link

Error: EEXIST file already exists mkdir with electron 15 #203

Open nvdai2401 opened 3 years ago

nvdai2401 commented 3 years ago

Hi, thanks for an awesome library!

After upgrading electron from version 12 to version 15, my windows app threw an error (see the screenshot below) when calling store.set(key, 'value'). Before upgrading, the app works well with electron 12, 13, and 14. By following the error message, I checked the conf codebase and found that the library calls _ensureDirectory before setting a value to store (see more at https://github.com/sindresorhus/conf/blob/main/source/index.ts#L355-L362) and the error was thrown by _ensureDirectory.

Does anyone know what's happening?

{
  ...
  "devDependencies": {
    "@types/jest": "^27.0.1",
    "@types/ws": "^7.4.4",
    "cross-env": "^7.0.3",
    "electron": "14.0.1",
    "electron-builder": "^22.10.5",
    "electron-log": "^4.3.5",
    "electron-notarize": "^1.0.0",
    "electron-rebuild": "^3.2.3",
    "filesize": "^8.0.3",
    "jest": "^27.0.6",
    "jest-extended": "^0.11.5",
    "playwright": "^1.14.1",
    "prettier": "^2.3.0",
    "terser": "^5.9.0",
    "ts-jest": "^27.0.5",
    "typescript": "^4.0.2"
  },
  "dependencies": {
    "axios": "^0.21.1",
    "dotenv": "^10.0.0",
    "electron-deeplink": "^1.0.6",
    "electron-squirrel-startup": "^1.0.0",
    "electron-store": "^8.0.1",
    "electron-updater": "^4.3.9",
    "electron-windows-notifications": "^3.0.8",
    "ws": "^7.4.6"
  }
}
TriPhoenix commented 3 years ago

TL;DR: for me it helped to delete %APPDATA%\yourappname before installing the MSIX package. (you may have to uninstall, then delete, then reinstall)

Starting to pack up an electron app for the first time I encountered the same issue on

However the issue only comes up when using the MSIX package, neither in the source .exe using electron-forge, nor when running directly via npm start (even though all three variants use the same config storage).

Potentially this is related to some sandboxing / containerization that MSIX packaged apps have to work with, it seems that Windows is doing some redirection behind the scenes (https://docs.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-behind-the-scenes).

Digging a bit deeper I found that Windows creates a shadow copy of the app data in %LOCALAPPDATA%\Packages\yourpackageidentifierhere\LocalCache\Roaming, interestingly while the standalone app creates a folder called yourappname in %APPDATA%, the MSIX version creates a file called yourappname (which then seems to trigger the EEXIST error when mkdir is called).

Now interestingly while working with this I found out that (though I'd be happy to have confirmation on that) this only seems to be a problem, if you already have an %APPDATA%\yourappname folder (for example from testing on the same machine) which Windows would "port over". When I delete the "normal" folder in %APPDATA%, the whole thing works flawlessly. Almost seems like something goes wrong when Windows try to shadow copy the existing data.

I'm not sure if this is Windows' fault, something with the way, files are accessed and if not, who of electron-store, electron or node can even fix this. However given that at least for me the end goal is an MSIX file for distribution, this workaround is "good enough" (as normal users will never have installed a non-MSIX version). Maybe this helps someone else