[flutter_tools] ensure track-widget-creation can be changed on devcompiler (#56564)

Workaround for cached web builds with --[no-]track-widget-creation flags.
Similar PR: #23299
diff --git a/dev/devicelab/manifest.yaml b/dev/devicelab/manifest.yaml
index 99d8694..bf865db 100644
--- a/dev/devicelab/manifest.yaml
+++ b/dev/devicelab/manifest.yaml
@@ -814,14 +814,12 @@
     stage: devicelab
     required_agent_capabilities: ["linux-vm"]
 
-  # TODO(jonahwilliams): This should stay off until
-  # https://github.com/flutter/flutter/issues/56212 is fixed.
-  # web_enable_twc:
-  #   description: >
-  #     Verifies that track-widget-creation can be enabled and disabled.
-  #   stage: devicelab
-  #   required_agent_capabilities: ["linux-vm"]
-  #   flaky: true
+  web_enable_twc:
+     description: >
+       Verifies that track-widget-creation can be enabled and disabled.
+     stage: devicelab
+     required_agent_capabilities: ["linux-vm"]
+     flaky: true
 
   # run_without_leak_linux:
   #   description: >
diff --git a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart
index a2370bd..b3cb71d 100644
--- a/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart
+++ b/packages/flutter_tools/lib/src/build_runner/resident_web_runner.dart
@@ -640,7 +640,7 @@
       pathToReload: getReloadPath(fullRestart: fullRestart),
       invalidatedFiles: invalidationResult.uris,
       packageConfig: invalidationResult.packageConfig,
-      trackWidgetCreation: true,
+      trackWidgetCreation: debuggingOptions.buildInfo.trackWidgetCreation,
     );
     devFSStatus.stop();
     globals.printTrace('Synced ${getSizeAsMB(report.syncedBytes)}.');
diff --git a/packages/flutter_tools/lib/src/bundle.dart b/packages/flutter_tools/lib/src/bundle.dart
index 0621d9e..1677293 100644
--- a/packages/flutter_tools/lib/src/bundle.dart
+++ b/packages/flutter_tools/lib/src/bundle.dart
@@ -34,6 +34,13 @@
   );
 }
 
+String getDefaultCachedKernelPath({ @required bool trackWidgetCreation }) {
+  return getKernelPathForTransformerOptions(
+    globals.fs.path.join(getBuildDirectory(), 'cache.dill'),
+    trackWidgetCreation: trackWidgetCreation,
+  );
+}
+
 String getKernelPathForTransformerOptions(
   String path, {
   @required bool trackWidgetCreation,
diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart
index e940856..a226c4c 100644
--- a/packages/flutter_tools/lib/src/resident_runner.dart
+++ b/packages/flutter_tools/lib/src/resident_runner.dart
@@ -21,6 +21,7 @@
 import 'base/terminal.dart';
 import 'base/utils.dart';
 import 'build_info.dart';
+import 'bundle.dart';
 import 'codegen.dart';
 import 'compile.dart';
 import 'convert.dart';
@@ -98,7 +99,9 @@
         compilerMessageConsumer:
           (String message, {bool emphasis, TerminalColor color, }) =>
             globals.printTrace(message),
-        initializeFromDill: globals.fs.path.join(getBuildDirectory(), 'cache.dill'),
+        initializeFromDill: getDefaultCachedKernelPath(
+          trackWidgetCreation: buildInfo.trackWidgetCreation,
+        ),
         targetModel: TargetModel.dartdevc,
         experimentalFlags: experimentalFlags,
         platformDill: globals.fs.file(globals.artifacts
@@ -123,7 +126,9 @@
         targetModel: targetModel,
         experimentalFlags: experimentalFlags,
         dartDefines: buildInfo.dartDefines,
-        initializeFromDill: globals.fs.path.join(getBuildDirectory(), 'cache.dill'),
+        initializeFromDill: getDefaultCachedKernelPath(
+          trackWidgetCreation: buildInfo.trackWidgetCreation,
+        ),
         packagesPath: globalPackagesPath,
       );
     }
@@ -723,6 +728,7 @@
   bool get supportsServiceProtocol => isRunningDebug || isRunningProfile;
   bool get supportsCanvasKit => false;
   bool get supportsWriteSkSL => supportsServiceProtocol;
+  bool get trackWidgetCreation => debuggingOptions.buildInfo.trackWidgetCreation;
 
   // Returns the Uri of the first connected device for mobile,
   // and only connected device for web.
@@ -1156,10 +1162,11 @@
   Future<void> preExit() async {
     // If _dillOutputPath is null, we created a temporary directory for the dill.
     if (_dillOutputPath == null && artifactDirectory.existsSync()) {
-      final File outputDill = artifactDirectory.childFile('app.dill');
+      final File outputDill = globals.fs.file(dillOutputPath);
       if (outputDill.existsSync()) {
-        artifactDirectory.childFile('app.dill')
-          .copySync(globals.fs.path.join(getBuildDirectory(), 'cache.dill'));
+        outputDill.copySync(getDefaultCachedKernelPath(
+          trackWidgetCreation: trackWidgetCreation,
+        ));
       }
       artifactDirectory.deleteSync(recursive: true);
     }
diff --git a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart
index 7076062..7a30f97 100644
--- a/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart
+++ b/packages/flutter_tools/test/general.shard/resident_web_runner_test.dart
@@ -141,7 +141,7 @@
       projectRootPath: anyNamed('projectRootPath'),
       pathToReload: anyNamed('pathToReload'),
       invalidatedFiles: anyNamed('invalidatedFiles'),
-      trackWidgetCreation: true,
+      trackWidgetCreation: anyNamed('trackWidgetCreation'),
       packageConfig: anyNamed('packageConfig'),
     )).thenAnswer((Invocation _) async {
       return UpdateFSReport(success: true,  syncedBytes: 0)..invalidatedModules = <String>[];
@@ -583,7 +583,7 @@
       pathToReload: anyNamed('pathToReload'),
       invalidatedFiles: anyNamed('invalidatedFiles'),
       packageConfig: anyNamed('packageConfig'),
-      trackWidgetCreation: true,
+      trackWidgetCreation: anyNamed('trackWidgetCreation'),
     )).thenAnswer((Invocation _) async {
       return UpdateFSReport(success: false,  syncedBytes: 0)..invalidatedModules = <String>[];
     });
@@ -646,7 +646,7 @@
       pathToReload: anyNamed('pathToReload'),
       invalidatedFiles: anyNamed('invalidatedFiles'),
       packageConfig: anyNamed('packageConfig'),
-      trackWidgetCreation: true,
+      trackWidgetCreation: anyNamed('trackWidgetCreation'),
     )).thenAnswer((Invocation _) async {
       return UpdateFSReport(success: false,  syncedBytes: 0)..invalidatedModules = <String>[];
     });