Skip to content

perf(@angular/cli): lazily initialize package manager in command context#32854

Open
alan-agius4 wants to merge 1 commit intoangular:mainfrom
alan-agius4:package-manager-version-lazy
Open

perf(@angular/cli): lazily initialize package manager in command context#32854
alan-agius4 wants to merge 1 commit intoangular:mainfrom
alan-agius4:package-manager-version-lazy

Conversation

@alan-agius4
Copy link
Copy Markdown
Collaborator

@alan-agius4 alan-agius4 commented Mar 26, 2026

This change refactors the CommandContext to defer the initialization of the packageManager instance until it is first accessed. This optimization avoids unnecessary configuration discovery and disk I/O for commands that do not require the package manager, such as ng help, and certain shell completions.

As part of this refactoring:

  • The packageManager property in CommandContext is now a Promise<PackageManager>.
  • Internal command modules and utility functions have been updated to await the package manager's lazy initialization.
  • In @angular-devkit/core, CoreSchemaRegistry was updated to rename _sourceMap to _sourceProvider and optimize the resolution of smart defaults by awaiting values during the _set operation.

@alan-agius4 alan-agius4 requested a review from clydin March 26, 2026 15:06
@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release action: review The PR is still awaiting reviews from at least one requested reviewer labels Mar 26, 2026
@angular-robot angular-robot bot added area: performance Issues related to performance area: @angular/cli labels Mar 26, 2026
@alan-agius4 alan-agius4 force-pushed the package-manager-version-lazy branch from 2d01580 to 94b9bb5 Compare March 26, 2026 15:08
Copy link
Copy Markdown

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

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 refactors the CommandContext to treat the packageManager as a Promise, allowing for lazy initialization. This change requires updating multiple command modules to asynchronously resolve the package manager before use. Furthermore, the SmartDefaultProvider interface and the CoreSchemaRegistry have been updated to support asynchronous providers. Review feedback focuses on improving code readability and performance by avoiding redundant await calls on the packageManager promise in AddCommandModule and gatherVersionInfo.

Comment on lines +497 to 501
manifest = await (
await this.context.packageManager
).getManifest(context.packageIdentifier, {
registry,
});
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

For better readability, you could await the packageManager promise and store it in a local variable before calling getManifest.

      const packageManager = await this.context.packageManager;
      manifest = await packageManager.getManifest(context.packageIdentifier, {
        registry,
      });

Comment on lines 123 to 126
packageManager: {
name: context.packageManager.name,
version: await context.packageManager.getVersion(),
name: (await context.packageManager).name,
version: await (await context.packageManager).getVersion(),
},
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

To avoid awaiting the packageManager promise twice and to improve readability, you can resolve it once. An async IIFE can be used here to keep the logic self-contained within the object property.

      packageManager: await (async () => {
        const pm = await context.packageManager;
        return {
          name: pm.name,
          version: await pm.getVersion(),
        };
      })(),

@alan-agius4 alan-agius4 force-pushed the package-manager-version-lazy branch from 94b9bb5 to 1073ddc Compare March 26, 2026 15:10
This change refactors the `CommandContext` to defer the initialization of the `packageManager` instance until it is first accessed. This optimization avoids unnecessary configuration discovery and disk I/O for commands that do not require the package manager, such as `ng help`, and certain shell completions.

As part of this refactoring:
- The `packageManager` property in `CommandContext` is now a `Promise<PackageManager>`.
- Internal command modules and utility functions have been updated to `await` the package manager's lazy initialization.
- In `@angular-devkit/core`, `CoreSchemaRegistry` was updated to rename `_sourceMap` to `_sourceProvider` and optimize the resolution of smart defaults by awaiting values during the `_set` operation.
@alan-agius4 alan-agius4 force-pushed the package-manager-version-lazy branch from 1073ddc to 920e161 Compare March 26, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: review The PR is still awaiting reviews from at least one requested reviewer area: @angular/cli area: performance Issues related to performance target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant