Make eager ast resolving behavior opt-in

This commit is contained in:
Simon Binder 2020-02-17 20:59:06 +01:00
parent e2b65e968a
commit d43e227757
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 29 additions and 6 deletions

View File

@ -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. 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. __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. 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 ## Available extensions

View File

@ -9,6 +9,7 @@ targets:
compact_query_methods: true compact_query_methods: true
write_from_json_string_constructor: true write_from_json_string_constructor: true
use_experimental_inference: true use_experimental_inference: true
eagerly_load_dart_ast: true
sqlite_modules: sqlite_modules:
- json1 - json1
- fts5 - fts5

View File

@ -63,6 +63,9 @@ class MoorOptions {
@JsonKey(name: 'sqlite_modules', defaultValue: []) @JsonKey(name: 'sqlite_modules', defaultValue: [])
final List<SqlModule> modules; final List<SqlModule> modules;
@JsonKey(name: 'eagerly_load_dart_ast', defaultValue: false)
final bool eagerlyLoadDartAst;
/// Whether the [module] has been enabled in this configuration. /// Whether the [module] has been enabled in this configuration.
bool hasModule(SqlModule module) => modules.contains(module); bool hasModule(SqlModule module) => modules.contains(module);
@ -75,6 +78,7 @@ class MoorOptions {
this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = false, this.useColumnNameAsJsonKeyWhenDefinedInMoorFile = false,
this.generateConnectConstructor = false, this.generateConnectConstructor = false,
this.useExperimentalInference = false, this.useExperimentalInference = false,
this.eagerlyLoadDartAst = false,
this.modules = const []}); this.modules = const []});
factory MoorOptions.fromJson(Map<String, dynamic> json) => factory MoorOptions.fromJson(Map<String, dynamic> json) =>

View File

@ -17,7 +17,8 @@ MoorOptions _$MoorOptionsFromJson(Map<String, dynamic> json) {
'use_column_name_as_json_key_when_defined_in_moor_file', 'use_column_name_as_json_key_when_defined_in_moor_file',
'generate_connect_constructor', 'generate_connect_constructor',
'use_experimental_inference', 'use_experimental_inference',
'sqlite_modules' 'sqlite_modules',
'eagerly_load_dart_ast',
]); ]);
final val = MoorOptions( final val = MoorOptions(
generateFromJsonStringConstructor: $checkedConvert( generateFromJsonStringConstructor: $checkedConvert(
@ -46,6 +47,9 @@ MoorOptions _$MoorOptionsFromJson(Map<String, dynamic> json) {
useExperimentalInference: $checkedConvert( useExperimentalInference: $checkedConvert(
json, 'use_experimental_inference', (v) => v as bool) ?? json, 'use_experimental_inference', (v) => v as bool) ??
false, false,
eagerlyLoadDartAst:
$checkedConvert(json, 'eagerly_load_dart_ast', (v) => v as bool) ??
false,
modules: $checkedConvert( modules: $checkedConvert(
json, json,
'sqlite_modules', 'sqlite_modules',

View File

@ -5,10 +5,15 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:build/build.dart' hide log; import 'package:build/build.dart' hide log;
import 'package:build/build.dart' as build show log; import 'package:build/build.dart' as build show log;
import 'package:logging/logging.dart'; 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/backend.dart';
import 'package:moor_generator/src/backends/build/serialized_types.dart'; import 'package:moor_generator/src/backends/build/serialized_types.dart';
class BuildBackend extends Backend { class BuildBackend extends Backend {
final MoorOptions options;
BuildBackend([this.options = const MoorOptions()]);
BuildBackendTask createTask(BuildStep step) { BuildBackendTask createTask(BuildStep step) {
return BuildBackendTask(step, this); return BuildBackendTask(step, this);
} }
@ -47,8 +52,11 @@ class BuildBackendTask extends BackendTask {
try { try {
final asset = _resolve(uri); final asset = _resolve(uri);
final library = await step.resolver.libraryFor(asset); 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; return library;
} on NonLibraryAssetException catch (_) { } on NonLibraryAssetException catch (_) {
@ -63,8 +71,12 @@ class BuildBackendTask extends BackendTask {
// dart file was read... // dart file was read...
final assetId = await step.resolver.assetIdForElement(element); final assetId = await step.resolver.assetIdForElement(element);
final result = _cachedResults[assetId]; 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 @override

View File

@ -19,7 +19,7 @@ mixin MoorBuilder on Builder {
Task task; Task task;
FoundFile input; FoundFile input;
try { try {
final backend = BuildBackend(); final backend = BuildBackend(options);
final backendTask = backend.createTask(step); final backendTask = backend.createTask(step);
final session = MoorSession(backend, options: options); final session = MoorSession(backend, options: options);