Unverified Commit 50f1e182 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

ensure packages file is updated when using build_runner (#29885)

parent 3b3f6c7a
...@@ -21,7 +21,6 @@ import '../base/logger.dart'; ...@@ -21,7 +21,6 @@ import '../base/logger.dart';
import '../base/platform.dart'; import '../base/platform.dart';
import '../base/process_manager.dart'; import '../base/process_manager.dart';
import '../codegen.dart'; import '../codegen.dart';
import '../dart/package_map.dart';
import '../dart/pub.dart'; import '../dart/pub.dart';
import '../globals.dart'; import '../globals.dart';
import '../project.dart'; import '../project.dart';
...@@ -128,7 +127,6 @@ class BuildRunner extends CodeGenerator { ...@@ -128,7 +127,6 @@ class BuildRunner extends CodeGenerator {
List<String> extraFrontEndOptions = const <String> [], List<String> extraFrontEndOptions = const <String> [],
}) async { }) async {
await generateBuildScript(flutterProject); await generateBuildScript(flutterProject);
_generatePackages(flutterProject);
final String engineDartBinaryPath = artifacts.getArtifactPath(Artifact.engineDartBinary); final String engineDartBinaryPath = artifacts.getArtifactPath(Artifact.engineDartBinary);
final File buildSnapshot = flutterProject final File buildSnapshot = flutterProject
.dartTool .dartTool
...@@ -160,18 +158,6 @@ class BuildRunner extends CodeGenerator { ...@@ -160,18 +158,6 @@ class BuildRunner extends CodeGenerator {
})); }));
return _BuildRunnerCodegenDaemon(buildDaemonClient); return _BuildRunnerCodegenDaemon(buildDaemonClient);
} }
// Create generated packages file which adds a multi-root scheme to the user's
// project directory. Currently we only replace the root package with a multiroot
// scheme. To support codegen on arbitrary packages we would need to do
// this for each dependency.
void _generatePackages(FlutterProject flutterProject) {
final String oldPackagesContents = fs.file(PackageMap.globalPackagesPath).readAsStringSync();
final String appName = flutterProject.manifest.appName;
final String newPackagesContents = oldPackagesContents.replaceFirst('$appName:lib/', '$appName:$kMultiRootScheme:/');
final String generatedPackagesPath = fs.path.setExtension(PackageMap.globalPackagesPath, '.generated');
fs.file(generatedPackagesPath).writeAsStringSync(newPackagesContents);
}
} }
class _BuildRunnerCodegenDaemon implements CodegenDaemon { class _BuildRunnerCodegenDaemon implements CodegenDaemon {
......
...@@ -41,6 +41,18 @@ abstract class CodeGenerator { ...@@ -41,6 +41,18 @@ abstract class CodeGenerator {
// Generates a synthetic package under .dart_tool/flutter_tool which is in turn // Generates a synthetic package under .dart_tool/flutter_tool which is in turn
// used to generate a build script. // used to generate a build script.
Future<void> generateBuildScript(FlutterProject flutterProject); Future<void> generateBuildScript(FlutterProject flutterProject);
/// Create generated packages file which adds a multi-root scheme to the user's
/// project directory. Currently we only replace the root package with a multiroot
/// scheme. To support codegen on arbitrary packages we would need to do
/// this for each dependency.
void updatePackages(FlutterProject flutterProject) {
final String oldPackagesContents = fs.file(PackageMap.globalPackagesPath).readAsStringSync();
final String appName = flutterProject.manifest.appName;
final String newPackagesContents = oldPackagesContents.replaceFirst('$appName:lib/', '$appName:$kMultiRootScheme:/');
final String generatedPackagesPath = fs.path.setExtension(PackageMap.globalPackagesPath, '.generated');
fs.file(generatedPackagesPath).writeAsStringSync(newPackagesContents);
}
} }
class UnsupportedCodeGenerator extends CodeGenerator { class UnsupportedCodeGenerator extends CodeGenerator {
...@@ -104,6 +116,7 @@ class CodeGeneratingKernelCompiler implements KernelCompiler { ...@@ -104,6 +116,7 @@ class CodeGeneratingKernelCompiler implements KernelCompiler {
'build* pipeline'); 'build* pipeline');
} }
final FlutterProject flutterProject = await FlutterProject.current(); final FlutterProject flutterProject = await FlutterProject.current();
codeGenerator.updatePackages(flutterProject);
final CodegenDaemon codegenDaemon = await codeGenerator.daemon(flutterProject); final CodegenDaemon codegenDaemon = await codeGenerator.daemon(flutterProject);
codegenDaemon.startBuild(); codegenDaemon.startBuild();
await for (CodegenStatus codegenStatus in codegenDaemon.buildResults) { await for (CodegenStatus codegenStatus in codegenDaemon.buildResults) {
...@@ -141,7 +154,7 @@ class CodeGeneratingKernelCompiler implements KernelCompiler { ...@@ -141,7 +154,7 @@ class CodeGeneratingKernelCompiler implements KernelCompiler {
/// An implementation of a [ResidentCompiler] which runs a [BuildRunner] before /// An implementation of a [ResidentCompiler] which runs a [BuildRunner] before
/// talking to the CFE. /// talking to the CFE.
class CodeGeneratingResidentCompiler implements ResidentCompiler { class CodeGeneratingResidentCompiler implements ResidentCompiler {
CodeGeneratingResidentCompiler._(this._residentCompiler, this._codegenDaemon); CodeGeneratingResidentCompiler._(this._residentCompiler, this._codegenDaemon, this._flutterProject);
/// Creates a new [ResidentCompiler] and configures a [BuildDaemonClient] to /// Creates a new [ResidentCompiler] and configures a [BuildDaemonClient] to
/// run builds. /// run builds.
...@@ -158,6 +171,7 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler { ...@@ -158,6 +171,7 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
String initializeFromDill, String initializeFromDill,
bool runCold = false, bool runCold = false,
}) async { }) async {
codeGenerator.updatePackages(flutterProject);
final ResidentCompiler residentCompiler = ResidentCompiler( final ResidentCompiler residentCompiler = ResidentCompiler(
artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath), artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath),
trackWidgetCreation: trackWidgetCreation, trackWidgetCreation: trackWidgetCreation,
...@@ -182,11 +196,12 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler { ...@@ -182,11 +196,12 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
if (status == CodegenStatus.Failed) { if (status == CodegenStatus.Failed) {
printError('Code generation failed, build may have compile errors.'); printError('Code generation failed, build may have compile errors.');
} }
return CodeGeneratingResidentCompiler._(residentCompiler, codegenDaemon); return CodeGeneratingResidentCompiler._(residentCompiler, codegenDaemon, flutterProject);
} }
final ResidentCompiler _residentCompiler; final ResidentCompiler _residentCompiler;
final CodegenDaemon _codegenDaemon; final CodegenDaemon _codegenDaemon;
final FlutterProject _flutterProject;
@override @override
void accept() { void accept() {
...@@ -206,7 +221,13 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler { ...@@ -206,7 +221,13 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
}); });
} }
if (_codegenDaemon.lastStatus == CodegenStatus.Failed) { if (_codegenDaemon.lastStatus == CodegenStatus.Failed) {
printError('Codegeneration failed, halting build.'); printError('Code generation failed, build may have compile errors.');
}
// Update the generated packages file if the original packages file has changes.
if (fs.statSync(PackageMap.globalPackagesPath).modified.millisecondsSinceEpoch >
fs.statSync(PackageMap.globalGeneratedPackagesPath).modified.millisecondsSinceEpoch) {
codeGenerator.updatePackages(_flutterProject);
invalidatedFiles.add(fs.file(PackageMap.globalGeneratedPackagesPath).uri);
} }
return _residentCompiler.recompile( return _residentCompiler.recompile(
mainPath, mainPath,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment