Don't further analyze part files (#2185)

This commit is contained in:
Simon Binder 2022-12-05 17:59:51 +01:00
parent c0d5188c18
commit dafa07a88a
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
3 changed files with 84 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import 'package:analyzer/dart/element/visitor.dart';
import 'package:source_gen/source_gen.dart';
import 'package:sqlparser/sqlparser.dart' hide AnalysisError;
import '../backend.dart';
import '../driver/driver.dart';
import '../driver/error.dart';
import '../driver/state.dart';
@ -60,6 +61,14 @@ class DiscoverStep {
try {
library = await _driver.backend.readDart(_file.ownUri);
} catch (e) {
if (e is! NotALibraryException) {
// Backends are supposed to throw NotALibraryExceptions if the
// library is a part file. For other exceptions, we better report
// the error.
_driver.backend.log
.warning('Could not resolve Dart library ${_file.ownUri}', e);
}
_file.discovery = NotADartLibrary();
break;
}

View File

@ -185,7 +185,14 @@ class _DriftBuildRun {
/// Checks if the input file contains elements drift should generate code for.
Future<bool> _checkForElementsToBuild() async {
if (!mode.embeddedAnalyzer) {
if (mode.embeddedAnalyzer) {
// Run the discovery step, which we'll have to run either way, to check if
// there are any elements to generate code for.
final state = await driver.prepareFileForAnalysis(buildStep.inputId.uri,
needsDiscovery: true);
return state.discovery?.locallyDefinedElements.isNotEmpty == true;
} else {
// An analysis step should have already run for this asset. If we can't
// pick up results from that, there is no code for drift to generate.
final fromCache =

View File

@ -1,3 +1,4 @@
import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
import 'package:logging/logging.dart';
import 'package:test/test.dart';
@ -104,4 +105,70 @@ selectAll: SELECT * FROM foo;
'a|lib/database.drift.dart': decodedMatches(contains('selectAll')),
}, result.dartOutputs, result);
});
test('can work with existing part files', () async {
final logger = Logger.detached('build');
expect(logger.onRecord, neverEmits(anything));
final result = await emulateDriftBuild(
inputs: {
'a|lib/main.dart': '''
import 'package:drift/drift.dart';
part 'table.dart';
@DriftDatabase(tables: [Users])
class MyDatabase {}
''',
'a|lib/table.dart': '''
part of 'main.dart';
class Users extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
}
''',
},
logger: logger,
);
checkOutputs(
{'a|lib/main.drift.dart': decodedMatches(contains('class User'))},
result.dartOutputs,
result,
);
});
test('handles syntax error in source file', () async {
final logger = Logger.detached('build');
expect(
logger.onRecord,
emits(
isA<LogRecord>()
.having((e) => e.message, 'message',
contains('Could not resolve Dart library package:a/main.dart'))
.having(
(e) => e.error, 'error', isA<SyntaxErrorInAssetException>()),
),
);
final result = await emulateDriftBuild(
inputs: {
'a|lib/main.dart': '''
import 'package:drift/drift.dart';
class Users extends Table {
IntColumn id => integer().autoIncrement()();
TextColumn name => text()();
}
@DriftDatabase(tables: [Users])
class MyDatabase {}
''',
},
logger: logger,
);
checkOutputs({}, result.dartOutputs, result);
});
}