Fixes to incremental DDK/blaze

This CL fixes a number of issues:
* Semi-leaking old components when starting over
  (i.e. when loading a new sdk)
* Setting new filesystem not really working because the old one was
  cached in the ProcessedOptions
* Leaking old state because of an apparent VM bug (http://dartbug.com/36983)

Change-Id: Ib124257c456b019941e253409c1ef56eedcf66b2
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103121
Reviewed-by: Kevin Millikin <kmillikin@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
diff --git a/pkg/front_end/lib/src/api_unstable/bazel_worker.dart b/pkg/front_end/lib/src/api_unstable/bazel_worker.dart
index fa64f22..3606b3d 100644
--- a/pkg/front_end/lib/src/api_unstable/bazel_worker.dart
+++ b/pkg/front_end/lib/src/api_unstable/bazel_worker.dart
@@ -70,8 +70,11 @@
   if (oldState == null ||
       oldState.incrementalCompiler == null ||
       oldState.incrementalCompiler.outlineOnly != outlineOnly) {
-    // No previous state.
+    // No - or immediately not correct - previous state.
     startOver = true;
+
+    // We'll load a new sdk, anything loaded already will have a wrong root.
+    workerInputCache.clear();
   } else {
     // We do have a previous state.
     cachedSdkInput = workerInputCache[sdkSummary];
@@ -79,6 +82,8 @@
         !digestsEqual(cachedSdkInput.digest, sdkDigest)) {
       // The sdk is out of date.
       startOver = true;
+      // We'll load a new sdk, anything loaded already will have a wrong root.
+      workerInputCache.clear();
     }
   }
 
@@ -120,6 +125,7 @@
     incrementalCompiler.invalidateAllSources();
     options.packagesFileUri = packagesFile;
     options.fileSystem = fileSystem;
+    processedOpts.clearFileSystemCache();
   }
 
   // Then read all the input summary components.
diff --git a/pkg/front_end/lib/src/api_unstable/ddc.dart b/pkg/front_end/lib/src/api_unstable/ddc.dart
index ef61212..3230a6b 100644
--- a/pkg/front_end/lib/src/api_unstable/ddc.dart
+++ b/pkg/front_end/lib/src/api_unstable/ddc.dart
@@ -165,6 +165,9 @@
       ..fileSystem = fileSystem ?? StandardFileSystem.instance;
     if (experiments != null) options.experimentalFlags = experiments;
 
+    // We'll load a new sdk, anything loaded already will have a wrong root.
+    workerInputCache.clear();
+
     processedOpts = new ProcessedOptions(options: options);
 
     cachedSdkInput = new WorkerInputComponent(null /* not compared anyway */,
@@ -190,6 +193,7 @@
     incrementalCompiler.invalidateAllSources();
     options.packagesFileUri = packagesFile;
     options.fileSystem = fileSystem;
+    processedOpts.clearFileSystemCache();
   }
   InitializedCompilerState compilerState = new InitializedCompilerState(
       options, processedOpts,
diff --git a/utils/bazel/kernel_worker.dart b/utils/bazel/kernel_worker.dart
index d1e6924..8ebfaee 100644
--- a/utils/bazel/kernel_worker.dart
+++ b/utils/bazel/kernel_worker.dart
@@ -299,6 +299,8 @@
 
       return Future.value(fe.serializeComponent(incrementalComponent));
     });
+
+    state.options.onDiagnostic = null; // See http://dartbug.com/36983.
   } else if (summaryOnly) {
     kernel = await fe.compileSummary(state, sources, onDiagnostic,
         includeOffsets: false);