diff --git a/docs/content/en/docs/Advanced Features/builder_options.md b/docs/content/en/docs/Advanced Features/builder_options.md index 39c474ff..a79fd7a7 100644 --- a/docs/content/en/docs/Advanced Features/builder_options.md +++ b/docs/content/en/docs/Advanced Features/builder_options.md @@ -63,6 +63,8 @@ At the moment, moor supports these options: However, it's still in development and may not work in all cases yet. Please report any issues you can find. __Warning:__ The new type inference algorithm is in development and does not obey to semantic versioning. Results and generated code might change in moor versions not declared as breaking. +* `eagerly_load_dart_ast`: Moor's builder will load the resolved AST whenever it encounters a Dart file, + instead of lazily when it reads a table. This is used to investigate rare builder crashes. ## Available extensions diff --git a/moor/build.yaml b/moor/build.yaml index 38217a37..63e6357b 100644 --- a/moor/build.yaml +++ b/moor/build.yaml @@ -9,6 +9,7 @@ targets: compact_query_methods: true write_from_json_string_constructor: true use_experimental_inference: true + eagerly_load_dart_ast: true sqlite_modules: - json1 - fts5 \ No newline at end of file diff --git a/moor_generator/lib/src/analyzer/options.dart b/moor_generator/lib/src/analyzer/options.dart index 0af539a3..84c6f748 100644 --- a/moor_generator/lib/src/analyzer/options.dart +++ b/moor_generator/lib/src/analyzer/options.dart @@ -63,6 +63,9 @@ class MoorOptions { @JsonKey(name: 'sqlite_modules', defaultValue: []) final List modules; + @JsonKey(name: 'eagerly_load_dart_ast', defaultValue: false) + final bool eagerlyLoadDartAst; + /// Whether the [module] has been enabled in this configuration. bool hasModule(SqlModule module) => modules.contains(module); @@ -75,6 +78,7 @@ class MoorOptions { this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = false, this.generateConnectConstructor = false, this.useExperimentalInference = false, + this.eagerlyLoadDartAst = false, this.modules = const []}); factory MoorOptions.fromJson(Map json) => diff --git a/moor_generator/lib/src/analyzer/options.g.dart b/moor_generator/lib/src/analyzer/options.g.dart index 90a4151c..fe5c0e54 100644 --- a/moor_generator/lib/src/analyzer/options.g.dart +++ b/moor_generator/lib/src/analyzer/options.g.dart @@ -17,7 +17,8 @@ MoorOptions _$MoorOptionsFromJson(Map json) { 'use_column_name_as_json_key_when_defined_in_moor_file', 'generate_connect_constructor', 'use_experimental_inference', - 'sqlite_modules' + 'sqlite_modules', + 'eagerly_load_dart_ast', ]); final val = MoorOptions( generateFromJsonStringConstructor: $checkedConvert( @@ -46,6 +47,9 @@ MoorOptions _$MoorOptionsFromJson(Map json) { useExperimentalInference: $checkedConvert( json, 'use_experimental_inference', (v) => v as bool) ?? false, + eagerlyLoadDartAst: + $checkedConvert(json, 'eagerly_load_dart_ast', (v) => v as bool) ?? + false, modules: $checkedConvert( json, 'sqlite_modules', diff --git a/moor_generator/lib/src/backends/build/build_backend.dart b/moor_generator/lib/src/backends/build/build_backend.dart index 509c5526..379cd798 100644 --- a/moor_generator/lib/src/backends/build/build_backend.dart +++ b/moor_generator/lib/src/backends/build/build_backend.dart @@ -5,10 +5,15 @@ import 'package:analyzer/dart/element/type.dart'; import 'package:build/build.dart' hide log; import 'package:build/build.dart' as build show log; import 'package:logging/logging.dart'; +import 'package:moor_generator/src/analyzer/options.dart'; import 'package:moor_generator/src/backends/backend.dart'; import 'package:moor_generator/src/backends/build/serialized_types.dart'; class BuildBackend extends Backend { + final MoorOptions options; + + BuildBackend([this.options = const MoorOptions()]); + BuildBackendTask createTask(BuildStep step) { return BuildBackendTask(step, this); } @@ -47,8 +52,11 @@ class BuildBackendTask extends BackendTask { try { final asset = _resolve(uri); final library = await step.resolver.libraryFor(asset); - _cachedResults[asset] = - await library.session.getResolvedLibraryByElement(library); + + if (backend.options.eagerlyLoadDartAst) { + _cachedResults[asset] = + await library.session.getResolvedLibraryByElement(library); + } return library; } on NonLibraryAssetException catch (_) { @@ -63,8 +71,12 @@ class BuildBackendTask extends BackendTask { // dart file was read... final assetId = await step.resolver.assetIdForElement(element); final result = _cachedResults[assetId]; - return result.getElementDeclaration(element) ?? - await super.loadElementDeclaration(element); + + if (result != null) { + return result.getElementDeclaration(element); + } else { + return super.loadElementDeclaration(element); + } } @override diff --git a/moor_generator/lib/src/backends/build/moor_builder.dart b/moor_generator/lib/src/backends/build/moor_builder.dart index c994884a..924727e4 100644 --- a/moor_generator/lib/src/backends/build/moor_builder.dart +++ b/moor_generator/lib/src/backends/build/moor_builder.dart @@ -19,7 +19,7 @@ mixin MoorBuilder on Builder { Task task; FoundFile input; try { - final backend = BuildBackend(); + final backend = BuildBackend(options); final backendTask = backend.createTask(step); final session = MoorSession(backend, options: options);