Skip to content

Commit 71fa4e2

Browse files
nmetulevCopilotazchohfi
authored
fixed a bug where electron.backup.exe was getting poisoned and added a clear-electron-debug-identity command (#248)
Co-authored-by: Nikola Metulev <711864+nmetulev@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Alexandre Zollinger Chohfi <alzollin@microsoft.com>
1 parent c418c4f commit 71fa4e2

File tree

8 files changed

+236
-32
lines changed

8 files changed

+236
-32
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ npx winapp --help
151151

152152
- [`node create-addon`](./docs/usage.md#node-create-addon) - Generate native C# or C++ addons
153153
- [`node add-electron-debug-identity`](./docs/usage.md#node-add-electron-debug-identity) - Add identity to Electron processes
154+
- [`node clear-electron-debug-identity`](./docs/usage.md#node-clear-electron-debug-identity) - Remove identity from Electron processes
154155
- [Windows AI Addon for Electron](https://github.com/microsoft/winapp-windows-ai) - The Windows AI Addon for Electron is a Node.js native addon that provides access to the [Windows AI APIs](https://learn.microsoft.com/en-us/windows/ai/apis/) directly from JavaScript.
155156

156157
The full CLI usage can be found here: [Documentation](/docs/usage.md)

docs/guides/electron/setup.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ Add the `--no-sandbox` flag to your start script in `package.json`. This works a
193193
**Important:** This issue does **not** affect full MSIX packaging - only debug identity during development.
194194

195195
**To undo debug identity** (if needed for troubleshooting):
196-
1. Navigate to `node_modules\electron\dist\` in your project
197-
2. Delete `electron.exe`
198-
3. Rename `electron.backup.exe` to `electron.exe`
196+
```bash
197+
npx winapp node clear-electron-debug-identity
198+
```
199199

200200
This restores the original Electron executable without the debug identity.
201201

docs/usage.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ npx winapp node create-addon --name myWindowsAddon
444444
> [!IMPORTANT]
445445
> There is a known issue with sparse packaging Electron applications which causes the app to crash on start or not render the web content. The issue has been fixed in Windows but it has not propagated to external Windows devices yet. If you are seeing this issue after calling `add-electron-debug-identity`, you can [disable sandboxing in your Electron app](https://www.electronjs.org/docs/latest/tutorial/sandbox#disabling-chromiums-sandbox-testing-only) for debug purposes with the `--no-sandbox` flag. This issue does not affect full MSIX packaging.
446446
<br /><br />
447-
To undo the Electron debug identity, the easiest approach is to clear your node_modules folder (or just the electron folder) and run `npm install` again.
447+
To undo the Electron debug identity, use `winapp node clear-electron-debug-identity`.
448448

449449
```bash
450450
npx winapp node add-electron-debug-identity [options]
@@ -465,6 +465,35 @@ npx winapp node add-electron-debug-identity
465465

466466
---
467467

468+
### node clear-electron-debug-identity
469+
470+
*(Available in NPM package only)* Remove package identity from the Electron debug process by restoring the original electron.exe from backup.
471+
472+
```bash
473+
npx winapp node clear-electron-debug-identity [options]
474+
```
475+
476+
**Options:**
477+
478+
| Option | Description |
479+
|--------|-------------|
480+
| `--verbose` | Enable verbose output |
481+
482+
**What it does:**
483+
484+
- Restores electron.exe from the backup created by `add-electron-debug-identity`
485+
- Removes the backup files after restoration
486+
- Returns Electron to its original state without package identity
487+
488+
**Examples:**
489+
490+
```bash
491+
# Remove identity from Electron development process
492+
npx winapp node clear-electron-debug-identity
493+
```
494+
495+
---
496+
468497
### Global Options
469498

470499
All commands support these global options:

src/winapp-CLI/WinApp.Cli/Commands/CreateDebugIdentityCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public override async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
6767
}
6868
catch (Exception error)
6969
{
70-
return (1, $"{UiSymbols.Error} Failed to add MSIX identity: {error.Message}");
70+
return (1, $"{UiSymbols.Error} Failed to add package identity: {error.Message}");
7171
}
7272

73-
return (0, "MSIX identity created successfully.");
73+
return (0, "Package identity created successfully.");
7474
}, cancellationToken);
7575
}
7676
}

src/winapp-CLI/WinApp.Cli/Services/MsixService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public static async Task<MsixIdentityResult> ParseAppxManifestFromPathAsync(File
224224
throw new FileNotFoundException($"AppX manifest not found at: {appxManifestPath}");
225225
}
226226

227-
// Read and extract MSIX identity from appxmanifest.xml
227+
// Read and extract package identity from appxmanifest.xml
228228
var appxManifestContent = await File.ReadAllTextAsync(appxManifestPath.FullName, Encoding.UTF8, cancellationToken);
229229

230230
return ParseAppxManifestAsync(appxManifestContent);

src/winapp-npm/src/cli.ts

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { generateCppAddonFiles } from './cpp-addon-utils';
44
import { generateCsAddonFiles } from './cs-addon-utils';
5-
import { addElectronDebugIdentity } from './msix-utils';
5+
import { addElectronDebugIdentity, clearElectronDebugIdentity } from './msix-utils';
66
import { getWinappCliPath, callWinappCli, WINAPP_CLI_CALLER_VALUE } from './winapp-cli-utils';
77
import { spawn } from 'child_process';
88
import * as fs from 'fs';
@@ -125,12 +125,14 @@ async function showCombinedHelp(): Promise<void> {
125125
console.log('');
126126
console.log('Node.js Subcommands:');
127127
console.log(' node create-addon Generate native addon files for Electron');
128-
console.log(' node add-electron-debug-identity Add MSIX identity to Electron debug process');
128+
console.log(' node add-electron-debug-identity Add package identity to Electron debug process');
129+
console.log(' node clear-electron-debug-identity Remove package identity from Electron debug process');
129130
console.log('');
130131
console.log('Examples:');
131132
console.log(` ${CLI_NAME} node create-addon --name myAddon`);
132133
console.log(` ${CLI_NAME} node create-addon --template cs --name myAddon`);
133134
console.log(` ${CLI_NAME} node add-electron-debug-identity`);
135+
console.log(` ${CLI_NAME} node clear-electron-debug-identity`);
134136
}
135137

136138
async function showVersion(): Promise<void> {
@@ -186,14 +188,16 @@ async function handleNode(args: string[]): Promise<void> {
186188
console.log('Node.js-specific commands');
187189
console.log('');
188190
console.log('Subcommands:');
189-
console.log(' create-addon Generate native addon files for Electron');
190-
console.log(' add-electron-debug-identity Add MSIX identity to Electron debug process');
191+
console.log(' create-addon Generate native addon files for Electron');
192+
console.log(' add-electron-debug-identity Add package identity to Electron debug process');
193+
console.log(' clear-electron-debug-identity Remove package identity from Electron debug process');
191194
console.log('');
192195
console.log('Examples:');
193196
console.log(` ${CLI_NAME} node create-addon --help`);
194197
console.log(` ${CLI_NAME} node create-addon --name myAddon`);
195198
console.log(` ${CLI_NAME} node create-addon --name myCsAddon --template cs`);
196199
console.log(` ${CLI_NAME} node add-electron-debug-identity`);
200+
console.log(` ${CLI_NAME} node clear-electron-debug-identity`);
197201
console.log('');
198202
console.log(`Use "${CLI_NAME} node <subcommand> --help" for detailed help on each subcommand.`);
199203
return;
@@ -211,6 +215,10 @@ async function handleNode(args: string[]): Promise<void> {
211215
await handleAddonElectronDebugIdentity(subcommandArgs);
212216
break;
213217

218+
case 'clear-electron-debug-identity':
219+
await handleClearElectronDebugIdentity(subcommandArgs);
220+
break;
221+
214222
default:
215223
console.error(`❌ Unknown node subcommand: ${subcommand}`);
216224
console.error(`Run "${CLI_NAME} node" for available subcommands.`);
@@ -340,14 +348,14 @@ async function handleAddonElectronDebugIdentity(args: string[]): Promise<void> {
340348
if (options.help) {
341349
console.log(`Usage: ${CLI_NAME} node add-electron-debug-identity [options]`);
342350
console.log('');
343-
console.log('Add MSIX identity to Electron debug process');
351+
console.log('Add package identity to Electron debug process');
344352
console.log('');
345353
console.log('This command will:');
346354
console.log(' 1. Create a backup of node_modules/electron/dist/electron.exe');
347355
console.log(
348356
' 2. Generate a sparse MSIX manifest in .winapp/debug folder, and assets in node_modules/electron/dist/ folder'
349357
);
350-
console.log(' 3. Add MSIX identity to the Electron executable');
358+
console.log(' 3. Add package identity to the Electron executable');
351359
console.log(' 4. Register the sparse package with external location');
352360
console.log('');
353361
console.log('Options:');
@@ -372,6 +380,46 @@ async function handleAddonElectronDebugIdentity(args: string[]): Promise<void> {
372380
}
373381
}
374382

383+
async function handleClearElectronDebugIdentity(args: string[]): Promise<void> {
384+
const options = parseArgs(args, {
385+
verbose: false,
386+
});
387+
388+
if (options.help) {
389+
console.log(`Usage: ${CLI_NAME} node clear-electron-debug-identity [options]`);
390+
console.log('');
391+
console.log('Remove package identity from Electron debug process');
392+
console.log('');
393+
console.log('This command will:');
394+
console.log(' 1. Restore electron.exe from the backup created by add-electron-debug-identity');
395+
console.log(' 2. Remove the backup files');
396+
console.log('');
397+
console.log('Options:');
398+
console.log(' --verbose Enable verbose output (default: false)');
399+
console.log(' --help Show this help');
400+
console.log('');
401+
console.log('Note: This command must be run from the root of an Electron project');
402+
console.log(' (directory containing node_modules/electron)');
403+
return;
404+
}
405+
406+
try {
407+
const result = await clearElectronDebugIdentity({
408+
verbose: options.verbose as boolean,
409+
});
410+
411+
if (result.restoredFromBackup) {
412+
console.log(`✅ Electron debug identity cleared successfully!`);
413+
} else {
414+
console.log(`ℹ️ No backup found - electron.exe may already be clean.`);
415+
}
416+
} catch (error) {
417+
const err = error as Error;
418+
console.error(`❌ Failed to clear Electron debug identity: ${err.message}`);
419+
process.exit(1);
420+
}
421+
}
422+
375423
function parseArgs(args: string[], defaults: ParsedArgs = {}): ParsedArgs {
376424
const result: ParsedArgs = { ...defaults };
377425

src/winapp-npm/src/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
// Main entry point for the Windows SDK BuildTools package
22
import { execSyncWithBuildTools } from './buildtools-utils';
3-
import { addMsixIdentityToExe, addElectronDebugIdentity } from './msix-utils';
3+
import { addMsixIdentityToExe, addElectronDebugIdentity, clearElectronDebugIdentity } from './msix-utils';
44
import { getGlobalWinappPath, getLocalWinappPath } from './winapp-path-utils';
55

66
// Re-export types from child_process for convenience
77
export type { ExecSyncOptions } from 'child_process';
88

99
// Re-export types
10-
export { MsixIdentityOptions, MsixIdentityResult, ElectronDebugIdentityResult } from './msix-utils';
10+
export {
11+
MsixIdentityOptions,
12+
MsixIdentityResult,
13+
ElectronDebugIdentityResult,
14+
ClearElectronDebugIdentityResult,
15+
} from './msix-utils';
1116
export { CallWinappCliOptions, CallWinappCliResult } from './winapp-cli-utils';
1217
export { GenerateCppAddonOptions, GenerateCppAddonResult } from './cpp-addon-utils';
1318
export { GenerateCsAddonOptions, GenerateCsAddonResult } from './cs-addon-utils';
@@ -20,6 +25,7 @@ export {
2025
// MSIX manifest utilities
2126
addMsixIdentityToExe,
2227
addElectronDebugIdentity,
28+
clearElectronDebugIdentity,
2329

2430
// winapp directory utilities
2531
getGlobalWinappPath,
@@ -31,6 +37,7 @@ export default {
3137
execWithBuildTools: execSyncWithBuildTools,
3238
addMsixIdentityToExe,
3339
addElectronDebugIdentity,
40+
clearElectronDebugIdentity,
3441
getGlobalWinappPath,
3542
getLocalWinappPath,
3643
};

0 commit comments

Comments
 (0)