electron / windows-installer

Build Windows Installers for Electron apps
MIT License
1.57k stars 263 forks source link

`createWindowsInstaller` Fails Writing to temp/*.nuspec File When "/" in Package Name #503

Open brandonegg opened 6 months ago

brandonegg commented 6 months ago

Background

I don't use this package directly, however I encountered this issue with the @electron-forge/maker-squirrel package. After some investigation I believe the issue resides within this package. The error in question is the following:

No dice: ENOENT: no such file or directory, open 'C:\Users\brand\AppData\Local\Temp\si-2024211-6524-qimd4y.k3l7a\@demo\electron-app.nuspec'

This step occurs when windows-installer is attempting to write the *.nuspec file to the temp directory.

Reproduce

Environment

Windows 11: 23H2 Node: v18.19.1 Npm: 10.2.4

I have created a repository which uses the base electron webpack template and includes steps for reproducing: https://github.com/brandonegg/electron-winstaller-demo?tab=readme-ov-file#environment

The variable which appears to be linked to the error is the "name" key inside of package.json. When the name contains a "/" character the createWindowsInstaller function fails with the message included above. If the forward slash is removed from the name, the function produces no errors.

Analysis

I found this line: https://github.com/electron/windows-installer/blob/598aa70da25b4446dd816d0bd97fa7786bdfe79f/src/index.ts#L148

I believe this is the issue, specifically because of the following behavior which occurs with path.join().

console.log(path.join(__dirname, '@demo/electron-app-win32-x64'));
# output: C:\Users\brand\Documents\my-new-app\@demo\electron-app-win32-x64

node's path.join() is trying to be clever when joining the path and replaces the "/" with a "\". When this path is used by fs.writeFile() on line 150 it is trying to write to a directory that does not exist.

Proposed Solution

As someone who is not very familiar with the low-level interworkings of native node-api's I wanted to present this issue to the maintainers prior to developing my own solution. A simple approach would be a regex to match & replace any of these known troublesome characters with a "-" (I see squirrel doesn't like this) or "_". This appears to be how electron-forge package is producing its artifacts. The output directory is named @demo-electron-app-{platform}-{arch}, aka they replaced "/" with "-". I would be happy to create a PR for this if this sounds like a good work around. I would assume since this is only a temp output directory the naming convention isn't too important.