Skip to content

Commit ad151b9

Browse files
fix: pnpm collection of optional dependencies (electron-userland#8957)
node-module-collector should not expect optional dependencies to be present on disk (due to OS-limitations, such as node-mac-permissions not being installable on linux -> new unit test). If `require('dep/package.json')` throws for optional dependency - it is completley safe to continue as if it was never installed
1 parent 1d47cb1 commit ad151b9

File tree

6 files changed

+734
-33
lines changed

6 files changed

+734
-33
lines changed

.changeset/witty-queens-trade.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"app-builder-lib": patch
3+
---
4+
5+
fix: pnpm collection of optional dependencies

packages/app-builder-lib/src/node-module-collector/pnpmNodeModulesCollector.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,30 @@ export class PnpmNodeModulesCollector extends NodeModulesCollector<PnpmDependenc
3939
extractProductionDependencyTree(tree: PnpmDependency): DependencyTree {
4040
const p = path.normalize(this.resolvePath(tree.path))
4141
const packageJson: Dependency<string, string> = require(path.join(p, "package.json"))
42-
const prodDependencies = { ...(packageJson.dependencies || {}), ...(packageJson.optionalDependencies || {}) }
4342

4443
const deps = { ...(tree.dependencies || {}), ...(tree.optionalDependencies || {}) }
4544
const dependencies = Object.entries(deps).reduce<DependencyTree["dependencies"]>((acc, curr) => {
4645
const [packageName, dependency] = curr
47-
if (!prodDependencies[packageName]) {
46+
47+
let isOptional: boolean
48+
if (packageJson.dependencies?.[packageName]) {
49+
isOptional = false
50+
} else if (packageJson.optionalDependencies?.[packageName]) {
51+
isOptional = true
52+
} else {
4853
return acc
4954
}
50-
return {
51-
...acc,
52-
[packageName]: this.extractProductionDependencyTree(dependency),
55+
56+
try {
57+
return {
58+
...acc,
59+
[packageName]: this.extractProductionDependencyTree(dependency),
60+
}
61+
} catch (error) {
62+
if (isOptional) {
63+
return acc
64+
}
65+
throw error
5366
}
5467
}, {})
5568

test/fixtures/lockfiles/HoistedNodeModuleTest/isInstallDepsBefore=true pnpm optional dependencies.txt

+18-18
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ importers:
1010
dependencies:
1111
electron-clear-data:
1212
specifier: ^1.0.5
13-
version: 1.0.5(electron@34.2.0)
13+
version: 1.0.5(electron@35.0.1)
1414
optionalDependencies:
1515
debug:
1616
specifier: 3.1.0
@@ -39,8 +39,8 @@ packages:
3939
4040
resolution: {integrity: sha512-BQ5aZNSCpj7D6K2ksrRCTmKRLEpnPvWDiLPfoGyhZ++8YtiK9d/3DBKPJgry359X/P1PfruyYwvnvwFjuEiEIg==}
4141

42-
'@types/node@20.17.19':
43-
resolution: {integrity: sha512-LEwC7o1ifqg/6r2gn9Dns0f1rhK+fPFDoMiceTJ6kWmVk6bgXBI/9IOWfVan4WiAavK9pIVWdX0/e3J+eEUh5A==}
42+
'@types/node@22.13.10':
43+
resolution: {integrity: sha512-I6LPUvlRH+O6VRUqYOcMudhaIdUVWfsjnZavnsraHvpBwaEyMN29ry+0UVJhImYL16xsscu0aske3yA+uPOWfw==}
4444

4545
4646
resolution: {integrity: sha512-H/+L+UkTV33uf49PH5pCAUBVPNj2nDBXTN+qS1dOwyyg24l3CcicicCA7ca+HMvJBZcFgl5r8e+RR6elsb4Lyw==}
@@ -108,8 +108,8 @@ packages:
108108
peerDependencies:
109109
electron: '>= 1.2.2'
110110

111-
electron@34.2.0:
112-
resolution: {integrity: sha512-SYwBJNeXBTm1q/ErybQMUBZAYqEreBUqBwTrNkw1rV4YatDZk5Aittpcus3PPeC4UoI/tqmJ946uG8AKHTd6CA==}
111+
electron@35.0.1:
112+
resolution: {integrity: sha512-iQonj6lnPhqfqha2KXx6LzV1dnu6UPTCWK+b7f9Zvg828umGemi22DKbcJ3/q+Opn7iUVTWyqp9z1JQqkIi6OA==}
113113
engines: {node: '>= 12.20.55'}
114114
hasBin: true
115115

@@ -280,8 +280,8 @@ packages:
280280
resolution: {integrity: sha512-34R7HTnG0XIJcBSn5XhDd7nNFPRcXYRZrBB2O2jdKqYODldSzBAqzsWoZYYvduky73toYS/ESqxPvkDf/F0XMg==}
281281
engines: {node: '>=10'}
282282

283-
undici-types@6.19.8:
284-
resolution: {integrity: sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==}
283+
undici-types@6.20.0:
284+
resolution: {integrity: sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg==}
285285

286286
287287
resolution: {integrity: sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==}
@@ -319,26 +319,26 @@ snapshots:
319319
dependencies:
320320
'@types/http-cache-semantics': 4.0.4
321321
'@types/keyv': 3.1.4
322-
'@types/node': 20.17.19
322+
'@types/node': 22.13.10
323323
'@types/responselike': 1.0.3
324324

325325
'@types/[email protected]': {}
326326

327327
328328
dependencies:
329-
'@types/node': 20.17.19
329+
'@types/node': 22.13.10
330330

331-
'@types/node@20.17.19':
331+
'@types/node@22.13.10':
332332
dependencies:
333-
undici-types: 6.19.8
333+
undici-types: 6.20.0
334334

335335
336336
dependencies:
337-
'@types/node': 20.17.19
337+
'@types/node': 22.13.10
338338

339339
340340
dependencies:
341-
'@types/node': 20.17.19
341+
'@types/node': 22.13.10
342342
optional: true
343343

344344
@@ -394,14 +394,14 @@ snapshots:
394394
395395
optional: true
396396

397-
[email protected](electron@34.2.0):
397+
[email protected](electron@35.0.1):
398398
dependencies:
399-
electron: 34.2.0
399+
electron: 35.0.1
400400

401-
electron@34.2.0:
401+
electron@35.0.1:
402402
dependencies:
403403
'@electron/get': 2.0.3
404-
'@types/node': 20.17.19
404+
'@types/node': 22.13.10
405405
extract-zip: 2.0.1
406406
transitivePeerDependencies:
407407
- supports-color
@@ -587,7 +587,7 @@ snapshots:
587587
588588
optional: true
589589

590-
undici-types@6.19.8: {}
590+
undici-types@6.20.0: {}
591591

592592
593593

0 commit comments

Comments
 (0)