Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 19, 2025

Angular's inject() function only works within an injection context. While it functioned in provider factory functions, it failed when called inside lifecycle callback functions (APP_INITIALIZER, APP_BOOTSTRAP_LISTENER, ENVIRONMENT_INITIALIZER, PLATFORM_INITIALIZER) because those callbacks execute outside the injection context.

Changes

  • New utility: wrapInitializerProviders intercepts lifecycle providers and wraps callbacks with runInInjectionContext() to maintain injection context during execution
  • useValue support: Converts function-valued useValue providers to useFactory to access the injector
  • Tests: 14 new tests covering all lifecycle providers with factory, callback, and useValue patterns
  • Documentation: Comprehensive guide with examples and async boundary limitations

Usage

import { inject, APP_INITIALIZER, InjectionToken } from '@angular/core';
import { createApplicationHarness } from '@ngworker/spectacular';

const CONFIG = new InjectionToken<Config>('CONFIG');

// Now works: inject() in callback function
await createApplicationHarness({
  providers: [
    { provide: CONFIG, useValue: { apiUrl: 'https://api.example.com' } },
    {
      provide: APP_INITIALIZER,
      useFactory: () => () => {
        const config = inject(CONFIG); // Previously failed, now works
        console.log('Initializing with', config.apiUrl);
      },
      multi: true,
    },
  ],
});

Note: Injection context cannot cross async boundaries—call inject() before any await.

Original prompt

This section details on the original issue you should resolve

<issue_title>Support inject in application lifecycle providers</issue_title>
<issue_description>## What is the motivation/use case for changing the behavior?

Ensure that createApplicationHarness supports using inject in specified providers, in particular providers for APP_INITIALIZER, APP_BOOTSTRAP_LISTENER, ENVIRONMENT_INITIALIZER, and PLATFORM_INITIALIZER.

Describe the solution you'd like

Describe alternatives you've considered

Additional context

</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 19, 2025 12:16
- Add wrapInitializerProviders utility to wrap lifecycle initializers
- Support inject() in factory functions (already worked, now tested)
- Support inject() in callback functions via runInInjectionContext
- Support inject() with useValue providers (converted to useFactory)
- Add comprehensive tests for all 4 lifecycle providers
- Tests cover APP_INITIALIZER, APP_BOOTSTRAP_LISTENER, ENVIRONMENT_INITIALIZER, PLATFORM_INITIALIZER

Co-authored-by: LayZeeDK <6364586+LayZeeDK@users.noreply.github.com>
- Create comprehensive guide with examples
- Document all supported lifecycle providers
- Explain async boundary limitations
- Add code examples for factory, callback, and useValue patterns

Co-authored-by: LayZeeDK <6364586+LayZeeDK@users.noreply.github.com>
Copilot AI changed the title [WIP] Support inject in application lifecycle providers Support inject() in application lifecycle provider callbacks Nov 19, 2025
Copilot AI requested a review from LayZeeDK November 19, 2025 12:24
Copilot finished work on behalf of LayZeeDK November 19, 2025 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support inject in application lifecycle providers

2 participants