From e67f9a3f6c02bf8dc9fd3aef9b9d91ee48787b60 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 18 Oct 2019 12:40:50 -0700 Subject: [PATCH] ensure we can disable --track-widget-creation in debug mode (#43016) --- .../lib/src/build_system/targets/dart.dart | 7 +++- packages/flutter_tools/lib/src/bundle.dart | 3 ++ .../permeable/build_bundle_test.dart | 33 +++++++++++++++++++ .../build_system/targets/dart_test.dart | 24 ++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/build_system/targets/dart.dart b/packages/flutter_tools/lib/src/build_system/targets/dart.dart index 6a4cf8ef94b..5260404c695 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/dart.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/dart.dart @@ -31,6 +31,9 @@ const String kTargetFile = 'TargetFile'; /// The define to control whether the AOT snapshot is built with bitcode. const String kBitcodeFlag = 'EnableBitcode'; +/// Whether to enable or disable track widget creation. +const String kTrackWidgetCreation = 'TrackWidgetCreation'; + /// The define to control what iOS architectures are built for. /// /// This is expected to be a comma-separated list of architectures. If not @@ -208,12 +211,14 @@ class KernelSnapshot extends Target { final String targetFile = environment.defines[kTargetFile] ?? fs.path.join('lib', 'main.dart'); final String packagesPath = environment.projectDir.childFile('.packages').path; final String targetFileAbsolute = fs.file(targetFile).absolute.path; + // everything besides 'false' is considered to be enabled. + final bool trackWidgetCreation = environment.defines[kTrackWidgetCreation] != 'false'; final CompilerOutput output = await compiler.compile( sdkRoot: artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath, mode: buildMode), aot: buildMode != BuildMode.debug, buildMode: buildMode, - trackWidgetCreation: buildMode == BuildMode.debug, + trackWidgetCreation: trackWidgetCreation && buildMode == BuildMode.debug, targetModel: TargetModel.flutter, outputFilePath: environment.buildDir.childFile('app.dill').path, packagesPath: packagesPath, diff --git a/packages/flutter_tools/lib/src/bundle.dart b/packages/flutter_tools/lib/src/bundle.dart index 996b166cba0..6624a22f7c6 100644 --- a/packages/flutter_tools/lib/src/bundle.dart +++ b/packages/flutter_tools/lib/src/bundle.dart @@ -80,6 +80,7 @@ class BundleBuilder { outputDir: assetDirPath, depfilePath: depfilePath, precompiled: precompiledSnapshot, + trackWidgetCreation: trackWidgetCreation, ); // Work around for flutter_tester placing kernel artifacts in odd places. if (applicationKernelFilePath != null) { @@ -103,6 +104,7 @@ Future buildWithAssemble({ @required String outputDir, @required String depfilePath, @required bool precompiled, + bool trackWidgetCreation, }) async { // If the precompiled flag was not passed, force us into debug mode. buildMode = precompiled ? buildMode : BuildMode.debug; @@ -114,6 +116,7 @@ Future buildWithAssemble({ kTargetFile: mainPath, kBuildMode: getNameForBuildMode(buildMode), kTargetPlatform: getNameForTargetPlatform(targetPlatform), + kTrackWidgetCreation: trackWidgetCreation?.toString(), }, ); final Target target = buildMode == BuildMode.debug diff --git a/packages/flutter_tools/test/commands.shard/permeable/build_bundle_test.dart b/packages/flutter_tools/test/commands.shard/permeable/build_bundle_test.dart index 224d406407d..167326b25e1 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/build_bundle_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/build_bundle_test.dart @@ -6,6 +6,8 @@ import 'package:args/command_runner.dart'; import 'package:file/memory.dart'; import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/build_system/build_system.dart'; +import 'package:flutter_tools/src/build_system/targets/dart.dart'; import 'package:flutter_tools/src/bundle.dart'; import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/commands/build_bundle.dart'; @@ -202,6 +204,37 @@ void main() { ProcessManager: () => FakeProcessManager([]), FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true), }); + + testUsingContext('passes track widget creation through', () async { + fs.file('lib/main.dart').createSync(recursive: true); + fs.file('pubspec.yaml').createSync(); + fs.file('.packages').createSync(); + final CommandRunner runner = createTestCommandRunner(BuildBundleCommand()); + when(buildSystem.build(any, any)).thenAnswer((Invocation invocation) async { + final Environment environment = invocation.positionalArguments[1]; + expect(environment.defines, { + kTargetFile: fs.path.join('lib', 'main.dart'), + kBuildMode: 'debug', + kTargetPlatform: 'android-arm', + kTrackWidgetCreation: 'true', + }); + + return BuildResult(success: true); + }); + + await runner.run([ + 'bundle', + '--no-pub', + '--debug', + '--target-platform=android-arm', + '--track-widget-creation' + ]); + }, overrides: { + FileSystem: () => MemoryFileSystem(), + BuildSystem: () => MockBuildSystem(), + ProcessManager: () => FakeProcessManager([]), + }); } class MockBundleBuilder extends Mock implements BundleBuilder {} +class MockBuildSystem extends Mock implements BuildSystem {} diff --git a/packages/flutter_tools/test/general.shard/build_system/targets/dart_test.dart b/packages/flutter_tools/test/general.shard/build_system/targets/dart_test.dart index 6e33fa1c52e..5d1cf05b978 100644 --- a/packages/flutter_tools/test/general.shard/build_system/targets/dart_test.dart +++ b/packages/flutter_tools/test/general.shard/build_system/targets/dart_test.dart @@ -168,6 +168,30 @@ flutter_tools:lib/'''); KernelCompilerFactory: () => MockKernelCompilerFactory(), })); + test('kernel_snapshot can disable track-widget-creation on debug builds', () => testbed.run(() async { + final MockKernelCompiler mockKernelCompiler = MockKernelCompiler(); + when(kernelCompilerFactory.create(any)).thenAnswer((Invocation _) async { + return mockKernelCompiler; + }); + when(mockKernelCompiler.compile( + sdkRoot: anyNamed('sdkRoot'), + aot: anyNamed('aot'), + buildMode: anyNamed('buildMode'), + trackWidgetCreation: false, + targetModel: anyNamed('targetModel'), + outputFilePath: anyNamed('outputFilePath'), + depFilePath: anyNamed('depFilePath'), + packagesPath: anyNamed('packagesPath'), + mainPath: anyNamed('mainPath'), + )).thenAnswer((Invocation _) async { + return const CompilerOutput('example', 0, []); + }); + + await const KernelSnapshot().build(androidEnvironment..defines[kTrackWidgetCreation] = 'false'); + }, overrides: { + KernelCompilerFactory: () => MockKernelCompilerFactory(), + })); + test('kernel_snapshot does use track widget creation on debug builds', () => testbed.run(() async { final MockKernelCompiler mockKernelCompiler = MockKernelCompiler(); when(kernelCompilerFactory.create(any)).thenAnswer((Invocation _) async {