Skip to content

Commit 70140ef

Browse files
committed
fix(SharedPartBuilder) correctly warn about missing part directive
1 parent c2c9cf0 commit 70140ef

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

source_gen/lib/src/builder.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,22 @@ class _Builder extends Builder {
106106
'Consider adding the following to your source file:\n\n'
107107
'library $suggest;');
108108
}
109-
final part = computePartUrl(buildStep.inputId, outputId);
109+
contentBuffer.writeln();
110+
111+
String part;
112+
if (_outputPartOf) {
113+
contentBuffer.writeln('part of $name;');
114+
part = computePartUrl(buildStep.inputId, outputId);
115+
} else {
116+
assert(_generatedExtension.endsWith('.g.part'),
117+
'in the SharedPartBuilder flow');
118+
var finalPartId = buildStep.inputId.changeExtension('.g.dart');
119+
part = computePartUrl(buildStep.inputId, finalPartId);
120+
}
110121
if (!library.parts.map((c) => c.uri).contains(part)) {
111122
// TODO: Upgrade to error in a future breaking change?
112123
log.warning('Missing "part \'$part\';".');
113124
}
114-
contentBuffer.writeln();
115-
if (_outputPartOf) contentBuffer.writeln('part of $name;');
116125
}
117126

118127
for (var item in generatedOutputs) {

source_gen/test/builder_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,21 @@ void main() {
203203
});
204204

205205
group('SharedPartBuilder', () {
206+
test('warns about missing part', () async {
207+
var srcs = _createPackageStub(testLibContent: _testLibContentNoPart);
208+
var builder =
209+
new SharedPartBuilder([const CommentGenerator()], 'comment');
210+
var logs = <String>[];
211+
await testBuilder(
212+
builder,
213+
srcs,
214+
onLog: (log) {
215+
logs.add(log.message);
216+
},
217+
);
218+
expect(logs, ['Missing "part \'test_lib.g.dart\';".']);
219+
});
220+
206221
test('outputs <partId>.g.part files', () async {
207222
await testBuilder(
208223
new SharedPartBuilder(

0 commit comments

Comments
 (0)