Skip to content

Commit 5616f23

Browse files
authored
fix: Migrate to electron-rebuild for handling native dependencies to fix compatibility with newer versions of electron (#7196)
1 parent 614dd1d commit 5616f23

File tree

8 files changed

+632
-70
lines changed

8 files changed

+632
-70
lines changed

.changeset/shy-lizards-beam.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"app-builder-lib": major
3+
"electron-builder": major
4+
---
5+
6+
fix: Migrate to electron-rebuild for handling native dependencies

packages/app-builder-lib/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"ejs": "^3.1.7",
6161
"electron-osx-sign": "^0.6.0",
6262
"electron-publish": "workspace:*",
63+
"electron-rebuild": "^3.2.9",
6364
"form-data": "^4.0.0",
6465
"fs-extra": "^10.1.0",
6566
"hosted-git-info": "^4.1.0",

packages/app-builder-lib/src/packager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ export class Packager {
486486
const frameworkInfo = { version: this.framework.version, useCustomDist: true }
487487
const config = this.config
488488
if (config.nodeGypRebuild === true) {
489-
await nodeGypRebuild(platform.nodeName, Arch[arch], frameworkInfo)
489+
await nodeGypRebuild(Arch[arch])
490490
}
491491

492492
if (config.npmRebuild === false) {

packages/app-builder-lib/src/util/packageMetadata.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function checkMetadata(metadata: Metadata, devMetadata: any | null, appPa
7070
const devDependencies = (metadata as any).devDependencies
7171
if (devDependencies != null && "electron-rebuild" in devDependencies) {
7272
log.info(
73-
'electron-rebuild not required if you use electron-builder, please consider to remove excess dependency from devDependencies\n\nTo ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`'
73+
'electron-rebuild is already incorporated into electron-builder, please consider to remove excess dependency from devDependencies\n\nTo ensure your native dependencies are always matched electron version, simply add script `"postinstall": "electron-builder install-app-deps" to your `package.json`'
7474
)
7575
}
7676

packages/app-builder-lib/src/util/yarn.ts

+22-38
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,28 @@ import { Lazy } from "lazy-val"
44
import { homedir } from "os"
55
import * as path from "path"
66
import { Configuration } from "../configuration"
7-
import { executeAppBuilderAndWriteJson } from "./appBuilder"
87
import { NodeModuleDirInfo } from "./packageDependencies"
8+
import * as electronRebuild from "electron-rebuild"
9+
import { getElectronVersion } from "../electron/electronVersion"
910

1011
export async function installOrRebuild(config: Configuration, appDir: string, options: RebuildOptions, forceInstall = false) {
11-
const effectiveOptions = {
12-
buildFromSource: config.buildDependenciesFromSource === true,
13-
additionalArgs: asArray(config.npmArgs),
14-
...options,
15-
}
1612
let isDependenciesInstalled = false
17-
1813
for (const fileOrDir of ["node_modules", ".pnp.js"]) {
1914
if (await pathExists(path.join(appDir, fileOrDir))) {
2015
isDependenciesInstalled = true
21-
2216
break
2317
}
2418
}
2519

2620
if (forceInstall || !isDependenciesInstalled) {
21+
const effectiveOptions: RebuildOptions = {
22+
buildFromSource: config.buildDependenciesFromSource === true,
23+
additionalArgs: asArray(config.npmArgs),
24+
...options,
25+
}
2726
await installDependencies(appDir, effectiveOptions)
2827
} else {
29-
await rebuild(appDir, effectiveOptions)
28+
await rebuild(appDir, config.buildDependenciesFromSource === true, options.arch)
3029
}
3130
}
3231

@@ -119,23 +118,8 @@ function installDependencies(appDir: string, options: RebuildOptions): Promise<a
119118
})
120119
}
121120

122-
export async function nodeGypRebuild(platform: NodeJS.Platform, arch: string, frameworkInfo: DesktopFrameworkInfo) {
123-
log.info({ platform, arch }, "executing node-gyp rebuild")
124-
// this script must be used only for electron
125-
const nodeGyp = `node-gyp${process.platform === "win32" ? ".cmd" : ""}`
126-
const args = ["rebuild"]
127-
// headers of old Electron versions do not have a valid config.gypi file
128-
// and --force-process-config must be passed to node-gyp >= 8.4.0 to
129-
// correctly build modules for them.
130-
// see also https://github.com/nodejs/node-gyp/pull/2497
131-
const [major, minor] = frameworkInfo.version
132-
.split(".")
133-
.slice(0, 2)
134-
.map(n => parseInt(n, 10))
135-
if (major <= 13 || (major == 14 && minor <= 1) || (major == 15 && minor <= 2)) {
136-
args.push("--force-process-config")
137-
}
138-
await spawn(nodeGyp, args, { env: getGypEnv(frameworkInfo, platform, arch, true) })
121+
export async function nodeGypRebuild(arch: string) {
122+
return rebuild(process.cwd(), false, arch)
139123
}
140124

141125
function getPackageToolPath() {
@@ -164,17 +148,17 @@ export interface RebuildOptions {
164148
}
165149

166150
/** @internal */
167-
export async function rebuild(appDir: string, options: RebuildOptions) {
168-
const configuration: any = {
169-
dependencies: await options.productionDeps!.value,
170-
nodeExecPath: process.execPath,
171-
platform: options.platform || process.platform,
172-
arch: options.arch || process.arch,
173-
additionalArgs: options.additionalArgs,
174-
execPath: process.env.npm_execpath || process.env.NPM_CLI_JS,
175-
buildFromSource: options.buildFromSource === true,
151+
export async function rebuild(appDir: string, buildFromSource: boolean, arch = process.arch) {
152+
log.info({ appDir, arch }, "executing electron-rebuild")
153+
const options: electronRebuild.RebuildOptions = {
154+
buildPath: appDir,
155+
electronVersion: await getElectronVersion(appDir),
156+
arch,
157+
force: true,
158+
debug: log.isDebugEnabled,
176159
}
177-
178-
const env = getGypEnv(options.frameworkInfo, configuration.platform, configuration.arch, options.buildFromSource === true)
179-
await executeAppBuilderAndWriteJson(["rebuild-node-modules"], configuration, { env, cwd: appDir })
160+
if (buildFromSource) {
161+
options.prebuildTagPrefix = "totally-not-a-real-prefix-to-force-rebuild"
162+
}
163+
return electronRebuild.rebuild(options)
180164
}

packages/electron-builder/src/cli/cli.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import { InvalidConfigurationError, log } from "builder-util"
44
import * as chalk from "chalk"
5-
import { getElectronVersion } from "app-builder-lib/out/electron/electronVersion"
65
import { readJson } from "fs-extra"
76
import * as isCi from "is-ci"
87
import * as path from "path"
@@ -76,7 +75,6 @@ async function checkIsOutdated() {
7675
}
7776

7877
async function rebuildAppNativeCode(args: any) {
79-
const projectDir = process.cwd()
8078
// this script must be used only for electron
81-
return nodeGypRebuild(args.platform, args.arch, { version: await getElectronVersion(projectDir), useCustomDist: true })
79+
return nodeGypRebuild(args.arch)
8280
}

0 commit comments

Comments
 (0)