Skip to content

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

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

nigrosimone wants to merge 2 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.

Comment on lines +489 to +491
if (this.#version) {
return this.#version;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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;
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done in 2ca1d1f

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant