Problem (+ solution) when signing electron app using electron-sudo
I've had problems when signing my electron app for distribution in macOS. App signing and distribution is still a new world to me, so I will try my best to explain what was happening and how I fixed it.
I was using electron-builder to build my app for macOS. electron-builder needs to pack the app as an .asar file, files inside the .asar package are not a problem. The thing is it needs to smartUnpack folders in the project that have binaries in them in order for them to work. That means, getting those files outside the .asar package and placing them in other folder. node_modules/electron-sudo happen to be a folder that needs to be unpacked because it contains binaries.
Now, electron-builder goes ahead and tries to codesign everything that's outside the .asar file, and encounters with node_modules/electron-sudo/dist/bin/applet.app/Contents/MacOS/applet and node_modules/electron-sudo/dist/src/bin/applet.app/Contents/MacOS/applet and tries to codesign them, and throws the following error: myApp.app/Contents/Resources/app.asar.unpacked/node_modules/electron-sudo/dist/bin/applet.app/Contents/MacOS/applet: unsealed contents present in the bundle root.
After a little bit of research, I found that codesign does not like at all any files in the same folder level as the Contents folder. Aaaand that's where LICENSE lives. Tried to place LICENSE inside Contents, got the following error: myApp.app/Contents/Resources/app.asar.unpacked/node_modules/electron-sudo/dist/bin/applet.app/Contents/MacOS/applet: code object is not signed at all.
And the only thing that worked was removing both LICENSE files, after that electron-builder and codesign worked as expected.
TL;DR: the LICENSE files under dist/bin/applet.app and dist/src/bin/applet.app cause errors when trying to sign an app that uses electron-sudo as a dependency, please consider removing both files.
@rameerez in version 5.0 LICENSE will be removed.
Same error here. Is there any plan to have a minor bump? Unable to build my app. Thanks!
@pronebird workaround: npm install, then manually delete both files node_modules/electron-sudo/dist/bin/applet.app/README and node_modules/electron-sudo/dist/src/bin/applet.app/README, then try to build your app again.
You'll need to do this again every time npm rewrites node_modules/electron-sudo, but hey, at least you can build your app.
@rameerez Just an FYI for googlers the files you need to delete at present are LICENSE rather than README
I have same issue. Inevitably, I use other package. sudo-prompt.
For googlers landing here, you can specify an 'afterPack' hook that cleans up these files in your package.json:
build: {
"afterPack": "./build_hooks/afterPack.js",
}
And do something along the lines of:
const { promises: fs } = require( 'fs' )
const log = ( ...messages ) => console.log( ...messages )
exports.default = async function( context ) {
const troublesome_files = [
`dist/mac-arm64/YOUR_APP.app/Contents/Resources/app.asar.unpacked/node_modules/electron-sudo/LICENSE`,
`dist/mac-arm64/YOUR_APP.app/Contents/Resources/app.asar.unpacked/node_modules/electron-sudo/dist/bin/applet.app/LICENSE`,
`dist/mac-arm64/YOUR_APP.app/Contents/Resources/app.asar.unpacked/node_modules/electron-sudo/src/bin/applet.app/LICENSE`
]
try {
log( '\n\n🪝 afterPack hook triggered: ' )
await Promise.all( troublesome_files.map( file => {
log( `Deleting ${ file }` )
return fs.rm( file )
} ) )
log( 'Cleaned up LICENSE files\n\n' )
return context
} catch( e ) {
log( `afterPack issue: `, e )
}
}
You'll have to edit the file paths depending on how you're building and whether you are using electron-sudo or sudo-prompt.