Repository navigation
perf(@angular/cli): verify the package manager binary on first use - #34800
nigrosimone wants to merge 2 commits into
Conversation
Every command awaited the package manager version command before running, also commands that never use the package manager like serve and build. The binary is now verified the first time a command needs it.
There was a problem hiding this comment.
Code Review
This pull request defers the verification of package manager binaries to their first use rather than performing it during initialization. This is achieved by introducing a missingBinaryError option in PackageManager and checking the binary lazily via a new checkBinary method. While this improves startup performance for commands that do not require the package manager, a review comment points out that the lazily resolved version string in getVersion bypasses the semver validation check, which could lead to downstream errors if the version is invalid.
| if (this.#version) { | ||
| return this.#version; | ||
| } |
There was a problem hiding this comment.
When this.#version is resolved lazily via #checkBinary(), the returned version string is not validated using valid(). This bypasses the semver validation check that was previously always performed when fetching the version, which could lead to unexpected errors or crashes downstream (e.g., when calling major(version) on an invalid semver string).
We should ensure that the version resolved from #checkBinary() is also validated before being returned.
| if (this.#version) { | |
| return this.#version; | |
| } | |
| if (this.#version) { | |
| if (!valid(this.#version)) { | |
| throw new Error('Invalid semver version for ' + this.name + ': "' + this.#version + '"'); | |
| } | |
| return this.#version; | |
| } |
Every
ngcommand awaitscreatePackageManager(), which runsnpm --version(or the configured package manager) to check that the binary is installed. Also commands that never use the package manager, likeserveandbuild, wait for this child process before they start.Now the factory only prepares the error, and
PackageManagerverifies the binary the first time a command runs it. The output is kept as the version, sogetVersion()does not run it again. The error message is the same.On Windows the child process takes ~190 ms:
ng servelistens at 2.50 s instead of 2.76 s,ng configgoes from ~650 ms to ~455 ms.Tests: new
factory_spec.tschecks thatcreatePackageManagerdoes not run the binary (fails without the change), and two specs inpackage-manager_spec.tsfor the lazy check and its error.