Repository navigation
perf(@angular/cli): verify the package manager binary on first use - #34800
nigrosimone wants to merge 3 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.
alan-agius4
left a comment
There was a problem hiding this comment.
Thanks for the PR! Deferring the package manager check until first use makes sense, especially on Windows.
Left a few comments below. The main thing is that initializationError and ensureInstalled() are now effectively dead/no-ops because factory.ts was the only caller passing initializationError, so we can simplify this by folding the lazy check into ensureInstalled() and caching #version as a single Promise<string>. That also fixes a few edge cases around semver validation, empty stdout, and dryRun.
alan-agius4
left a comment
There was a problem hiding this comment.
The core implementation looks solid and cleanly addresses the previous feedback. A few test coverage, JSDoc, and minor cleanup items below:
| initializationError: new Error( | ||
| `The project is configured to use 'bun', but it is not installed.`, | ||
| ), | ||
| notInstalledMessage: `The project is configured to use 'bun', but it is not installed.`, |
There was a problem hiding this comment.
Previously initializationError caused ensureInstalled() to throw before host.runCommand was called. Now gatherVersionInfo() calls packageManager.getVersion() -> host.runCommand(), so this test currently passes implicitly because MockHost.runCommand throws Error('Method not implemented.') and gatherVersionInfo() swallows all errors.
Let's explicitly stub spyOn(host, 'runCommand').and.rejectWith(new Error('spawn bun ENOENT')) here so the test intentionally exercises the missing-binary path.
| logger?: Logger; | ||
| dryRun?: boolean; | ||
| tempDirectory?: string; | ||
| host?: Host; |
There was a problem hiding this comment.
Nit: Let's update the createPackageManager JSDoc above to reflect that verification is now deferred until first use (and document host), and switch the Host import to a type-only import (import { type Host, NodeJS_HOST } from './host';).
| expect(runCommandSpy).not.toHaveBeenCalled(); | ||
| expect(await pm.getVersion()).toBe('10.0.0'); | ||
| expect(runCommandSpy).toHaveBeenCalledTimes(1); | ||
| }); |
There was a problem hiding this comment.
Right now this only covers the happy path, so removing notInstalledMessage from new PackageManager(...) in factory.ts wouldn't fail this spec. Could we also add cases for:
- failure on first use verifying both the
defaultandconfigured/discoverederror messages, dryRun: trueskipping verification, and- a pre-configured version (e.g.
['pnpm', '9.0.0']) skipping verification?
| ensureInstalled(): void { | ||
| if (this.#initializationError) { | ||
| throw this.#initializationError; | ||
| async ensureInstalled(): Promise<void> { |
There was a problem hiding this comment.
Since ensureInstalled() is now async and silently a no-op when notInstalledMessage is omitted, dryRun is true, or version was pre-supplied (and has no callers outside PackageManager besides specs), let's either update the JSDoc to document this contract or make it private (#ensureInstalled()).
| await expectAsync(pm.add('foo', 'none', false, false, false)).toBeRejectedWithError( | ||
| notInstalledMessage, | ||
| ); | ||
| await pm.getVersion().catch((e: Error) => expect(e.cause).toBe(cause)); |
There was a problem hiding this comment.
Nit: .catch(...) silently skips the assertion if the promise resolves. Prefer expectAsync(pm.ensureInstalled()).toBeRejectedWith(jasmine.objectContaining({ message: notInstalledMessage, cause })) (or try/catch with fail()).
|
|
||
| await pm.install(); | ||
| await expectAsync(pm.getVersion()).toBeRejectedWithError(/Invalid semver version/); | ||
| await expectAsync(pm.getVersion()).toBeRejectedWithError(/Invalid semver version/); |
There was a problem hiding this comment.
Let's also assert expect(runCommandSpy).toHaveBeenCalledTimes(2) here to prove neither getVersion() call re-spawned versionCommand.
It would also be great to add tests covering:
- happy-path version reuse (
pm.install()followed bypm.getVersion()sharing the single--versionspawn), - concurrent commands exceeding
#maxConcurrent(> 5) before verification settles (ensuring single-spawn deduplication and no throttle deadlock), and - the retry path when
notInstalledMessageis omitted (this.#version = undefined; throw error;).
|
|
||
| await pm.ensureInstalled(); | ||
| await pm.install(); | ||
| expect(runCommandSpy).not.toHaveBeenCalled(); |
There was a problem hiding this comment.
Nit: Spying on logger.info here would also verify that ensureInstalled() doesn't emit a [DRY RUN] ... --version log line before install().
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.