Skip to content

Commit bb86bef

Browse files
goderbauerdart-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[frontend_server] Don't crash when recompiling before the first accept
`writeJavaScriptBundle` dereferenced `_generator.lastKnownGoodResult!` whenever `fullComponent` was false. That value is only populated by `IncrementalCompiler.accept()`, so it is null until the client has accepted a compilation at least once. Since `recompileDelta` always passes `fullComponent: false`, a `recompile` request that arrives before the first `accept` request crashed with: Null check operator used on a null value #0 FrontendCompiler.writeJavaScriptBundle dart-lang#1 FrontendCompiler.recompileDelta dart-lang#2 listenAndCompile.<anonymous closure> Nothing in the protocol requires `accept` to precede `recompile`, and flutter_tools can violate that ordering: with `--machine`, the daemon publishes the appId before the initial compile has been accepted, so an `app.restart` can be serviced in between. Only the DDC path is affected; `writeDillFile` never looks at `lastKnownGoodResult`. When there is no accepted result, initialize the bundler from the component instead of diffing against a previous one. This is equivalent rather than a fallback: `IncrementalCompiler.compile()` returns the combination of all deltas that have not been accepted yet, which is the same component the initial `compile` request passes to `bundler.initialize`. Also replaces the same null check in `compileExpressionToJs` with a proper error report. Fixes flutter/flutter#192227 TEST=pkg/frontend_server/test/frontend_server_test.dart Change-Id: I50a2b307acfa58bd796c69df45eba4c3dd271aad Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/548680 Reviewed-by: Sigmund Cherem <[email protected]> Reviewed-by: Ben Konyi <[email protected]> Commit-Queue: Ben Konyi <[email protected]>
1 parent acad943 commit bb86bef

3 files changed

Lines changed: 116 additions & 3 deletions

File tree

‎pkg/front_end/test/spell_checking_list_code.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ delim
476476
delimit
477477
delimiting
478478
delivered
479+
deltas
479480
demands
480481
demangle
481482
demangled

‎pkg/frontend_server/lib/frontend_server.dart‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,12 +1092,20 @@ class FrontendCompiler implements CompilerInterface {
10921092
canaryFeatures: canaryFeatures,
10931093
extraDdcOptions: extraDdcOptions ?? [],
10941094
);
1095-
if (fullComponent) {
1095+
final IncrementalCompilerResult? lastKnownGood =
1096+
_generator.lastKnownGoodResult;
1097+
if (fullComponent || lastKnownGood == null) {
1098+
// [lastKnownGood] is `null` when the client sent a `recompile` request
1099+
// before ever sending an `accept` request, so there is no previously
1100+
// accepted component to diff against. The incremental compiler combines
1101+
// all deltas that have not been accepted yet, which makes [component]
1102+
// equivalent to a full component in that case, so the bundler can simply
1103+
// be (re-)initialized from it.
10961104
await bundler.initialize(component, _mainSource, packageConfig);
10971105
} else {
10981106
await bundler.invalidate(
10991107
component,
1100-
_generator.lastKnownGoodResult!.component,
1108+
lastKnownGood.component,
11011109
_mainSource,
11021110
packageConfig,
11031111
recompileRestart: recompileRestart,
@@ -1411,6 +1419,14 @@ class FrontendCompiler implements CompilerInterface {
14111419
reportError('Cannot find kernel2js compiler for $libraryUri.');
14121420
return;
14131421
}
1422+
final IncrementalCompilerResult? compilerResult =
1423+
_generator.lastKnownGoodResult;
1424+
if (compilerResult == null) {
1425+
// No compilation delta has been accepted yet, so there is no component
1426+
// to evaluate the expression against.
1427+
reportError('No accepted compilation result available.');
1428+
return;
1429+
}
14141430
final String boundaryKey = generateV4UUID();
14151431
_outputStream.writeln('result $boundaryKey');
14161432

@@ -1419,7 +1435,6 @@ class FrontendCompiler implements CompilerInterface {
14191435
);
14201436

14211437
final Compiler kernel2jsCompiler = cachedProgramCompilers[libraryUri]!;
1422-
IncrementalCompilerResult compilerResult = _generator.lastKnownGoodResult!;
14231438
Component component = compilerResult.component;
14241439
_processedOptions.ticker.logMs('Retrieved cached component');
14251440

‎pkg/frontend_server/test/frontend_server_test.dart‎

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,103 @@ extension type Foo(int value) {
22752275
});
22762276
});
22772277

2278+
group('recompile to JavaScript without accepting the last delta', () {
2279+
// Regression test for a crash that occurred when a 'recompile' request
2280+
// arrived before the client had ever sent an 'accept' request: there is
2281+
// no last known good component to compute the JavaScript delta against
2282+
// in that case.
2283+
// See https://github.com/flutter/flutter/issues/192227.
2284+
Future<void> runTests({
2285+
required String moduleFormat,
2286+
bool canary = false,
2287+
}) async {
2288+
File file = new File('${tempDir.path}/foo.dart')..createSync();
2289+
file.writeAsStringSync('main() {\n "<<v1>>";\n}\n');
2290+
2291+
File packageConfig =
2292+
new File('${tempDir.path}/.dart_tool/package_config.json')
2293+
..createSync(recursive: true)
2294+
..writeAsStringSync('''
2295+
{
2296+
"configVersion": 2,
2297+
"packages": [
2298+
{
2299+
"name": "hello",
2300+
"rootUri": "../",
2301+
"packageUri": "./"
2302+
}
2303+
]
2304+
}
2305+
''');
2306+
2307+
String library = 'package:hello/foo.dart';
2308+
2309+
File dillFile = new File('${tempDir.path}/app.dill');
2310+
File sourceFile = new File('${dillFile.path}.sources');
2311+
File incrementalSourceFile = new File(
2312+
'${dillFile.path}.incremental.dill.sources',
2313+
);
2314+
2315+
final List<String> args = <String>[
2316+
'--sdk-root=${sdkRoot.toFilePath()}',
2317+
'--incremental',
2318+
'--platform=${ddcPlatformKernel.path}',
2319+
'--output-dill=${dillFile.path}',
2320+
'--target=dartdevc',
2321+
'--dartdevc-module-format=$moduleFormat',
2322+
if (canary) '--dartdevc-canary',
2323+
'--packages=${packageConfig.path}',
2324+
];
2325+
2326+
FrontendServer frontendServer = new FrontendServer();
2327+
Future<int> result = frontendServer.open(args);
2328+
frontendServer.compile(library);
2329+
int count = 0;
2330+
frontendServer.listen((Result compiledResult) {
2331+
CompilationResult result = new CompilationResult.parse(
2332+
compiledResult.status,
2333+
);
2334+
switch (count) {
2335+
case 0:
2336+
expect(result.errorsCount, equals(0));
2337+
expect(result.filename, dillFile.path);
2338+
expect(sourceFile.readAsStringSync(), contains('<<v1>>'));
2339+
2340+
file.writeAsStringSync('main() {\n "<<v2>>";\n}\n');
2341+
// Deliberately recompile *without* accepting the compilation
2342+
// above first.
2343+
frontendServer.recompile(file.uri, entryPoint: library);
2344+
break;
2345+
case 1:
2346+
expect(result.errorsCount, equals(0));
2347+
expect(result.filename, '${dillFile.path}.incremental.dill');
2348+
String source = incrementalSourceFile.readAsStringSync();
2349+
expect(source, contains('<<v2>>'));
2350+
expect(source, not(contains('<<v1>>')));
2351+
2352+
frontendServer.accept();
2353+
frontendServer.quit();
2354+
break;
2355+
default:
2356+
break;
2357+
}
2358+
count++;
2359+
});
2360+
2361+
expect(await result, 0);
2362+
expect(count, 2);
2363+
frontendServer.close();
2364+
}
2365+
2366+
test('AMD module format', () async {
2367+
await runTests(moduleFormat: 'amd');
2368+
});
2369+
2370+
test('DDC module format and canary', () async {
2371+
await runTests(moduleFormat: 'ddc', canary: true);
2372+
});
2373+
});
2374+
22782375
group('compile to JavaScript with metadata', () {
22792376
Future<void> runTests({
22802377
required String moduleFormat,

0 commit comments

Comments
 (0)