Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manually inject local micro package modules #483

Open
Jaew00Shin opened this issue Oct 13, 2024 · 2 comments
Open

manually inject local micro package modules #483

Jaew00Shin opened this issue Oct 13, 2024 · 2 comments

Comments

@Jaew00Shin
Copy link

import 'package:app_factory_core/app_factory_core.dart';
import 'package:app_factory_dev/src/di/configure_dependencies.config.dart';
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';

final getIt = GetIt.instance;

@InjectableInit(
  asExtension: false,
  generateForDir: ['lib/src/di'],
  initializerName: r'$initModuleGetIt',
  includeMicroPackages: false,
  externalPackageModulesAfter: [
    ExternalModule(AppFactoryCorePackageModule),
  ],
)
Future<void> configureDependencies() async {
  $initModuleGetIt(getIt);
}

The above one is working well.
AppFactoryCorePackageModule is external package from the other package.

dependencies:
  app_factory_core:
    path: ../app_factory_core

AppFactoryCorePakcageModule is defined and is generated in app_factory_core.

But,

import 'package:app_factory_core/app_factory_core.dart';
import 'package:app_factory_dev/src/di/configure_dependencies.config.dart';
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';

import 'initialize_app_dependencies.module.dart';

final getIt = GetIt.instance;

@InjectableInit(
  asExtension: false,
  generateForDir: ['lib/src/di'],
  initializerName: r'$initModuleGetIt',
  includeMicroPackages: false,
  externalPackageModulesAfter: [
    ExternalModule(AppFactoryCorePackageModule),
    ExternalModule(AppFactoryDevPackageModule),
  ],
)
Future<void> configureDependencies() async {
  $initModuleGetIt(getIt);
}

If I add local micro package AppFactoryDevPackageModule, FormatException occurs.

[INFO] Running build...
[app_factory_dev]: [SEVERE] injectable_generator:injectable_config_builder on lib/src/di/configure_dependencies.dart:
[app_factory_dev]: 
[app_factory_dev]: FormatException: Not an instance of Type.
[app_factory_dev]: [INFO] Running build completed, took 287ms
[app_factory_dev]: 
[app_factory_dev]: [INFO] Caching finalized dependency graph...
[app_factory_dev]: [INFO] Caching finalized dependency graph completed, took 51ms
[app_factory_dev]: 
[app_factory_dev]: [SEVERE] Failed after 341ms

The document in pub.dev said that "it's also possible to include micro local or external modules manually by passing them to the externalPackageModules property inside of @injectableInit so they're initialized with the rest of the local dependencies."
But it doesn't work to manually pass local modules.

How can I add local modules to externalPackagesModule?

@ryanhanks-bestow
Copy link

@ Jaew00Shin I think it's because the same builder that's inspecting the @InjectableInit and thus attempting to resolve AppFactoryDevPackageModule is also the builder responsible for generating .module.dart asset, so from the builder's perspective that asset doesn't exist yet at this moment in the build cycle.

My hunch comes from reproducing what you demonstrated above and then testing the theory by:

  1. Allowing the builder to successfully finish a production of AppFactoryDevPackageModule (in this example, we assume it must be being built to initialize_app_dependencies.module.dart)
  2. Copying that file to another location with an extension not associated with this builders output (i.e., anything other than .module.dart – I copied by test from package.module.dart to package.module2.dart)
  3. importing the copy of the generated asset into the file I was building initModuleGetIt from

This copied version of the produced asset now lives outside of the build lifecycle and the builder is properly able to resolve and inspect it, and the build for $initModuleGetIt then successfully completes.

There could be more nuance around the timing to this, as there's a couple builders involved in the production here, one of which produces an interim asset in the cache (https://github.com/Milad-Akarie/injectable/blob/2f3422bc5d0f35b9f08abbb7bce2388219df2603/injectable_generator/build.yaml#L5C1-L12C67). In the config for these builders we do notice the 2nd builder produces both .configure.dart and .module.dart – if you're gung ho on solving this you could probably write your own configuration for that set of outputs as two separate builders configurations and use required_inputs to declare the .configure.dart asset to require the .module.dart output as input (https://github.com/dart-lang/build/blob/master/docs/build_yaml_format.md#builderdefinition), but idk  🤷‍♂️

[SEVERE] injectable_generator:injectable_config_builder on lib/configure.dart:

FormatException: Not an instance of Type.
[INFO] Running build completed, took 34ms
[INFO] Caching finalized dependency graph completed, took 4ms
[SEVERE] Failed after 40ms
[INFO] ------------------------------------------------------------------------
[INFO] Starting Build
[INFO] Updating asset graph completed, took 0ms
[INFO] Running build completed, took 27ms
[INFO] Caching finalized dependency graph completed, took 4ms
[SEVERE] injectable_generator:injectable_config_builder on lib/configure.dart (cached):

FormatException: Not an instance of Type.
[SEVERE] Failed after 36ms
[INFO] ------------------------------------------------------------------------
[INFO] Starting Build
[INFO] Updating asset graph completed, took 0ms
[INFO] Running build completed, took 61ms
[INFO] Caching finalized dependency graph completed, took 5ms
[INFO] Succeeded after 67ms with 1 outputs (4 actions)

@ryanhanks-bestow
Copy link

This might be another hacky approach you could try https://github.com/dart-lang/build/blob/master/docs/faq.md#how-can-i-adjust-builder-ordering

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

No branches or pull requests

2 participants