Skip to content

perf(@angular/cli): verify the package manager binary on first use - #34800

Open
nigrosimone wants to merge 3 commits into
angular:mainfrom
nigrosimone:cli-lazy-package-manager-check
Open

nigrosimone wants to merge 3 commits into
angular:mainfrom
nigrosimone:cli-lazy-package-manager-check

Conversation

@nigrosimone

Copy link
Copy Markdown

Every ng command awaits createPackageManager(), which runs npm --version (or the configured package manager) to check that the binary is installed. Also commands that never use the package manager, like serve and build, wait for this child process before they start.

Now the factory only prepares the error, and PackageManager verifies the binary the first time a command runs it. The output is kept as the version, so getVersion() does not run it again. The error message is the same.

On Windows the child process takes ~190 ms: ng serve listens at 2.50 s instead of 2.76 s, ng config goes from ~650 ms to ~455 ms.

Tests: new factory_spec.ts checks that createPackageManager does not run the binary (fails without the change), and two specs in package-manager_spec.ts for the lazy check and its error.

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.
@angular-robot angular-robot Bot added area: performance Issues related to performance area: @angular/cli labels Nov 28, 2026
@nigrosimone
nigrosimone marked this pull request as ready for review November 28, 2026 16:43

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 alan-agius4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 alan-agius4 added the target: patch This PR is targeted for the next patch release label Dec 2, 2026

@alan-agius4 alan-agius4 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.`,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 default and configured/discovered error messages,
  • dryRun: true skipping 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> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 by pm.getVersion() sharing the single --version spawn),
  • concurrent commands exceeding #maxConcurrent (> 5) before verification settles (ensuring single-spawn deduplication and no throttle deadlock), and
  • the retry path when notInstalledMessage is omitted (this.#version = undefined; throw error;).

await pm.ensureInstalled();
await pm.install();
expect(runCommandSpy).not.toHaveBeenCalled();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Spying on logger.info here would also verify that ensureInstalled() doesn't emit a [DRY RUN] ... --version log line before install().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: @angular/cli area: performance Issues related to performance target: patch This PR is targeted for the next patch release

2 participants