`package.json` "overrides" technique to suppress optional dependencies not working; breaks Electron
s100 opened this issue · 5 comments
Q&A (please complete the following information)
- OS: Windows 11
- Environment: Node.js 18.14.0
- Method of installation: 9.3.1
- Swagger-Client version: 3.19.0 (congrats on the new release)
- Swagger/OpenAPI version: N/A
Content & configuration
N/A
Describe the bug you're encountering
To reproduce...
Steps to reproduce the behavior:
npm install swagger-client@^3.19.0
npm list @swagger-api/apidom-parser-adapter-json
or similar
Expected behavior
Thanks to your cunning "overrides"
gambit (also see your documentation). I would expect this listing to be empty. These dependencies, which are optional dependencies of your own dependency @swagger-api/apidom-reference
, should not be installed and should not appear in my node_modules
.
In practice, I see that all of these indirect @swagger-api/*
dependencies have been installed. Meanwhile, the dummy -
package has not been installed. So, it looks like these overrides aren't having the intended effect.
Screenshots
N/A
Additional context or thoughts
This could be a problem with the way "overrides"
is laid out, but the documentation seems to agree with what you've done.
It is also possible that this is user error on my part. Does this work for you when you try to npm install swagger-client
? If so, this could be a difference between your environment and mine - would appreciate a little help to try to spot the difference.
If this doesn't work for you either, then this could also be an issue with npm
itself. "overrides"
is a relatively new feature and "-" is a very unusual name for a package - it wouldn't surprise me if this was tripping some kind of edge case in npm
. In any case, I would appreciate your input on this.
Please note that those @swagger-api/*
packages use tree-sitter
, and as a result of this issue, swagger-client@3.19.0
no longer works in Electron, whereas swagger-client@3.18.5
worked fine.
Hi @s100,
Thanks to your cunning "overrides" gambit (also see your documentation). I would expect this listing to be empty. These dependencies, which are optional dependencies of your own dependency @swagger-api/apidom-reference, should not be installed and should not appear in my node_modules.
In practice, I see that all of these indirect @swagger-api/* dependencies have been installed. Meanwhile, the dummy - package has not been installed. So, it looks like these overrides aren't having the intended effect.
overrides
field from swagger-client package.json
is ignored, when you do the npm i swagger-client@3.19.x
. This is how the field works even though it's not precisely described in the field documentation.
The overrides
field is only processed in following situation:
- let's say your local project is in
electron
directory and it's a github repo - you integrated swagger-client@3.19.x by doing
npm i --save swagger-client@3.19.0
- copy paste the
overrides
I've prepared in https://github.com/swagger-api/swagger-js/blob/master/docs/usage/installation.md to yourpackage.json
file - run
npm i
again to process the overrides - if you inspect your
node_modules
you'll see the-
package replaced the@apidom/*
ones
You are not limited to -
package. You can use any empty package that can be used for that purpose (there are number of them in npmjs.com registry.
"overrides" is a relatively new feature and "-" is a very unusual name for a package - it wouldn't surprise me if this was tripping some kind of edge case in npm.
The overrides
is pure local dev or CI optimization (by running npm i
or npm ci
)
Please note that those @swagger-api/* packages use tree-sitter, and as a result of tree-sitter/node-tree-sitter#126, swagger-client@3.19.0 no longer works in Electron, whereas swagger-client@3.18.5 worked fine.
tree-sitter
is a dependency of optional dependencies of @swagger-api/apidom-reference
package. If it fails to install, it shouldn't matter, swagger-client should work without it with no issues.
Inside the documentation I've put a following note, that explicitly states it:
When you say swagger-client@3.19.0 no longer works in Electron
- what does it actually mean?
Interesting. This behaviour feels like a bug in npm to me. The npm documentation is unclear, but I would have expected your overrides to just work in place, without us having to manually copy them over from your package.json
to ours. It looks like there's a bug open for this here, so I have subscribed to that bug and I will see whether it gets fixed sometime.
In the meantime, we will consider copying those overrides over as you suggested. Alternatively, we may stay pinned to swagger-client@3.18.5
.
When you say
swagger-client@3.19.0 no longer works in Electron
- what does it actually mean?
We indirectly use swagger-client
as part of an Electron application we are developing. If I create a package.json
like:
{
"name": "test",
"version": "1.0.0",
"description": "test",
"author": "s100"
}
And an index.js
like:
console.log('hello world')
Then run:
npm install swagger-client@3.18.5
npm install --save-dev electron electron-builder
npx electron-builder
...then the Electron build runs to completion successfully. If instead we do
npm install swagger-client@3.19.0
npm install --save-dev electron electron-builder
npx electron-builder
then the Electron build fails with a message like:
• electron-builder version=23.6.0 os=10.0.22000
• writing effective config file=dist\builder-effective-config.yaml
• rebuilding native dependencies dependencies=tree-sitter@0.20.1 platform=win32 arch=x64
• install prebuilt binary name=tree-sitter version=0.20.1 platform=win32 arch=x64 napi=
• build native dependency from sources name=tree-sitter
version=0.20.1
platform=win32
arch=x64
napi=
reason=prebuild-install failed with error (run with env DEBUG=electron-builder to get more information)
error=C:\...\test\node_modules\node-abi\index.js:36
throw new Error('Could not detect abi for version ' + target + ' and runtime ' + runtime + '. Updating "node-abi" might help solve this issue if it is a new release of ' + runtime)
^
Error: Could not detect abi for version 22.3.2 and runtime electron. Updating "node-abi" might help solve this issue if it is a new release of electron
at getAbi (C:\...\test\Downloads\a\node_modules\node-abi\index.js:36:9)
at module.exports (C:\...\test\Downloads\a\node_modules\prebuild-install\rc.js:52:57)
at Object.<anonymous> (C:\...\test\Downloads\a\node_modules\prebuild-install\bin.js:8:25)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
In other words, this minor version bump in swagger-client
caused our Electron build to start failing. As I say, I have fixed our build by pinning swagger-client
and I have raised an issue upstream against tree-sitter
.
Right, so npx electron-builder
seems to be traversing the deps and building them as it sees fit.
Utilizing the copy of the overrides
in your package.json should resolve the issue and allow to use the latest swagger-client@3.19.1
. If you want to have support for OpenAPI 3.1.0 and receive bugfixes, I'd say that's currently the only option.
We invested a lot of effort to make sure, swagger-client@3.19.x is fully backward compatible, but there are some many other consuming softwares that works in it's own way, that we cannot always handle everything. At least in this particular case, we have an override option and as long as you use Node.js >= 16.6.0
(and associated npm versions, that supports overrides
field) it should solve the issue for you, until fixes are provided from upstream (tree-sitter).
Originally we opted to use bundleDependencies
package.json field, which would avoid the overrides
"gambit" for corner-cases, but found out that yarn@2
chooses not to support that field anymore.
Thank for reporting this. I'm closing this issue for now, as there is very little we can do from our side right now (except changing the architecture of ApiDOM).
Keep me posted please, if overrides
resolved the issue for you for now.
请注意,这些
@swagger-api/*
包使用tree-sitter
,由于这个问题,swagger-client@3.19.0
它们不再在 Electron 中工作,但swagger-client@3.18.5
工作得很好。
After I replaced the swagger client to 3.18.5, my problem was resolved. Thank you very much!