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

Build runner fails due to misplaced import directives in generated files #547

Open
guiyomh opened this issue Feb 22, 2025 · 6 comments
Open

Comments

@guiyomh
Copy link

guiyomh commented Feb 22, 2025

When running dart run build_runner build in a project using brick_offline_first_with_supabase_build, the process fails due to incorrectly placed import directives in the generated file models_and_migrations.brick_aggregate.dart.

Error message:

models_and_migrations.brick_aggregate.dart:124:1: Directives must appear before any declarations.

This suggests that import, export, or part directives are being placed after declarations, causing the build process to break.

I did not modify the model; I was working on a different feature of my app. I created a Cubit using BLoC, where I injected use cases via GetIt. That's why I ran the command to rebuild my dependency injection container.

The code where I work
import 'package:apc_portal/domain/entities/profile_entity.dart';
import 'package:apc_portal/domain/use_cases/get_vehicle_use_case.dart';
import 'package:bloc/bloc.dart';
import 'package:formz/formz.dart';
import 'package:injectable/injectable.dart';
import 'trip_input.dart';

part 'trip_form_state.dart';

@injectable
class TripFormCubit extends Cubit<TripFormState> {
  final GetVehicleUseCase _getVehicleUseCase;

  TripFormCubit(
    this._getVehicleUseCase,
  ) : super(TripFormState());

  void loadVehicle(String vehicleId) async { 
//...

I wonder if the syntax part 'trip_form_state.dart'; is well managed ?

full log details ```bash dart run build_runner build Building package executable... Built build_runner:build_runner. [INFO] Generating build script completed, took 192ms [WARNING] Throwing away cached asset graph because the build phases have changed. This most commonly would happen as a result of adding a new dependency or updating your dependencies. [WARNING] Throwing away cached asset graph because the language version of some package(s) changed. This would most commonly happen when updating dependencies or changing your min sdk constraint. [INFO] Cleaning up outputs from previous builds. completed, took 66ms [INFO] Generating build script completed, took 81ms [WARNING] Invalidated precompiled build script due to missing asset graph. [INFO] Precompiling build script... completed, took 610ms [INFO] Building new asset graph completed, took 884ms [INFO] Found 1 declared outputs which already exist on disk. This is likely because the`.dart_tool/build` folder was deleted, or you are submitting generated files to your source repository. Delete these files? 1 - Delete 2 - Cancel build 3 - List conflicts 1 [INFO] Checking for unexpected pre-existing outputs. completed, took 2.5s [SEVERE] brick_offline_first_with_supabase_build:brick_schema_builder on lib/models_and_migrations.brick_aggregate.dart:

This builder requires Dart inputs without syntax errors.
However, package:apc_portal/models_and_migrations.brick_aggregate.dart (or an existing part) contains the following errors.
models_and_migrations.brick_aggregate.dart:124:1: Directives must appear before any declarations.

Try fixing the errors and re-running the build.

[SEVERE] brick_offline_first_with_supabase_build:brick_new_migration_builder on lib/models_and_migrations.brick_aggregate.dart:

This builder requires Dart inputs without syntax errors.
However, package:apc_portal/models_and_migrations.brick_aggregate.dart (or an existing part) contains the following errors.
models_and_migrations.brick_aggregate.dart:124:1: Directives must appear before any declarations.

Try fixing the errors and re-running the build.

[SEVERE] brick_offline_first_with_supabase_build:brick_model_dictionary_builder on lib/models_and_migrations.brick_aggregate.dart:

This builder requires Dart inputs without syntax errors.
However, package:apc_portal/models_and_migrations.brick_aggregate.dart (or an existing part) contains the following errors.
models_and_migrations.brick_aggregate.dart:124:1: Directives must appear before any declarations.

Try fixing the errors and re-running the build.

[INFO] Running build completed, took 11.0s
[INFO] Caching finalized dependency graph completed, took 47ms
[SEVERE] Failed after 11.1s

</details>
@guiyomh
Copy link
Author

guiyomh commented Feb 22, 2025

I'm trying:

flutter clean
dart run build_runner clean
dart run build_runner build

but tha doesn't solve the issue

@guiyomh
Copy link
Author

guiyomh commented Feb 22, 2025

I found a way to regenerate the code:

rm lib/brick/db/*migration.dart  
rm lib/brick/**/*.g.dart  
dart run build_runner clean  
dart run build_runner build  

The problem is that this deletes the migration files, causing the app to crash on startup because it can't execute them.

Since I'm still in development, it's not a big issue—I simply uninstall and reinstall the app, and it works.

However, once I'm in production, I won’t be able to do that!

@guiyomh
Copy link
Author

guiyomh commented Feb 22, 2025

I can confirm the issue.
I just added a new dependency to my Cubit:

@injectable
class TripFormCubit extends Cubit<TripFormState> {
  final GetVehicleUseCase _getVehicleUseCase;
  final GetDriversUseCase _getDriversUseCase; // this dep
// ...

When I ran the command dart run build_runner build, I got the error message again.

error message
dart run build_runner build
Building package executable... 
Built build_runner:build_runner.
[INFO] Generating build script completed, took 187ms
[WARNING] Throwing away cached asset graph because the build phases have changed. This most commonly would happen as a result of adding a new dependency or updating your dependencies.
[WARNING] Throwing away cached asset graph because the language version of some package(s) changed. This would most commonly happen when updating dependencies or changing your min sdk constraint.
[INFO] Cleaning up outputs from previous builds. completed, took 59ms
[INFO] Generating build script completed, took 53ms
[WARNING] Invalidated precompiled build script due to missing asset graph.
[INFO] Precompiling build script... completed, took 499ms
[INFO] Building new asset graph completed, took 949ms
[INFO] Found 1 declared outputs which already exist on disk. This is likely because the`.dart_tool/build` folder was deleted, or you are submitting generated files to your source repository.
Delete these files?
1 - Delete
2 - Cancel build
3 - List conflicts
1
[INFO] Checking for unexpected pre-existing outputs. completed, took 3.1s
[SEVERE] brick_offline_first_with_supabase_build:brick_schema_builder on lib/models_and_migrations.brick_aggregate.dart:

This builder requires Dart inputs without syntax errors.
However, package:apc_portal/models_and_migrations.brick_aggregate.dart (or an existing part) contains the following errors.
models_and_migrations.brick_aggregate.dart:124:1: Directives must appear before any declarations.

Try fixing the errors and re-running the build.

[SEVERE] brick_offline_first_with_supabase_build:brick_new_migration_builder on lib/models_and_migrations.brick_aggregate.dart:

This builder requires Dart inputs without syntax errors.
However, package:apc_portal/models_and_migrations.brick_aggregate.dart (or an existing part) contains the following errors.
models_and_migrations.brick_aggregate.dart:124:1: Directives must appear before any declarations.

Try fixing the errors and re-running the build.

[SEVERE] brick_offline_first_with_supabase_build:brick_model_dictionary_builder on lib/models_and_migrations.brick_aggregate.dart:

This builder requires Dart inputs without syntax errors.
However, package:apc_portal/models_and_migrations.brick_aggregate.dart (or an existing part) contains the following errors.
models_and_migrations.brick_aggregate.dart:124:1: Directives must appear before any declarations.

Try fixing the errors and re-running the build.

[INFO] Running build completed, took 10.9s
[INFO] Caching finalized dependency graph completed, took 47ms
[SEVERE] Failed after 11.0s

@guiyomh
Copy link
Author

guiyomh commented Feb 22, 2025

hum, may be a start:

Because every version of brick_offline_first_with_supabase_build depends on source_gen ^1.2.2 and injectable_generator >=2.7.0
depends on source_gen ^2.0.0, brick_offline_first_with_supabase_build is incompatible with injectable_generator >=2.7.0.
So, because apc_portal depends on both injectable_generator >=2.7.0 and brick_offline_first_with_supabase_build >=1.1.0, version
solving failed.

@tshedor
Copy link
Collaborator

tshedor commented Feb 22, 2025

@guiyomh thank you for formatting the code in this issue properly.

As far as I can tell, the error is being reported by Brick but there is no Brick code. Build Runner can be finicky because it uses dart:mirrors which is not shipped in Flutter's version of Dart. I see this error most often when some version of dart:ui is imported in the same file as an annotation. Can you verify that none of your files import flutter/*.dart?

hum, may be a start:

I'm still working on upgrading Brick to the latest packages in #544 . Hopefully I'll have this done by the end of next week.

@tshedor
Copy link
Collaborator

tshedor commented Feb 22, 2025

(But I'd also accept PRs into #544 to resolve test cases if you'd like to expedite that work)

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

No branches or pull requests

2 participants