Skip to content

Commit 50f1e18

Browse files
ensure packages file is updated when using build_runner (flutter#29885)
1 parent 3b3f6c7 commit 50f1e18

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

packages/flutter_tools/lib/src/build_runner/build_runner.dart

-14
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import '../base/logger.dart';
2121
import '../base/platform.dart';
2222
import '../base/process_manager.dart';
2323
import '../codegen.dart';
24-
import '../dart/package_map.dart';
2524
import '../dart/pub.dart';
2625
import '../globals.dart';
2726
import '../project.dart';
@@ -128,7 +127,6 @@ class BuildRunner extends CodeGenerator {
128127
List<String> extraFrontEndOptions = const <String> [],
129128
}) async {
130129
await generateBuildScript(flutterProject);
131-
_generatePackages(flutterProject);
132130
final String engineDartBinaryPath = artifacts.getArtifactPath(Artifact.engineDartBinary);
133131
final File buildSnapshot = flutterProject
134132
.dartTool
@@ -160,18 +158,6 @@ class BuildRunner extends CodeGenerator {
160158
}));
161159
return _BuildRunnerCodegenDaemon(buildDaemonClient);
162160
}
163-
164-
// Create generated packages file which adds a multi-root scheme to the user's
165-
// project directory. Currently we only replace the root package with a multiroot
166-
// scheme. To support codegen on arbitrary packages we would need to do
167-
// this for each dependency.
168-
void _generatePackages(FlutterProject flutterProject) {
169-
final String oldPackagesContents = fs.file(PackageMap.globalPackagesPath).readAsStringSync();
170-
final String appName = flutterProject.manifest.appName;
171-
final String newPackagesContents = oldPackagesContents.replaceFirst('$appName:lib/', '$appName:$kMultiRootScheme:/');
172-
final String generatedPackagesPath = fs.path.setExtension(PackageMap.globalPackagesPath, '.generated');
173-
fs.file(generatedPackagesPath).writeAsStringSync(newPackagesContents);
174-
}
175161
}
176162

177163
class _BuildRunnerCodegenDaemon implements CodegenDaemon {

packages/flutter_tools/lib/src/codegen.dart

+24-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ abstract class CodeGenerator {
4141
// Generates a synthetic package under .dart_tool/flutter_tool which is in turn
4242
// used to generate a build script.
4343
Future<void> generateBuildScript(FlutterProject flutterProject);
44+
45+
/// Create generated packages file which adds a multi-root scheme to the user's
46+
/// project directory. Currently we only replace the root package with a multiroot
47+
/// scheme. To support codegen on arbitrary packages we would need to do
48+
/// this for each dependency.
49+
void updatePackages(FlutterProject flutterProject) {
50+
final String oldPackagesContents = fs.file(PackageMap.globalPackagesPath).readAsStringSync();
51+
final String appName = flutterProject.manifest.appName;
52+
final String newPackagesContents = oldPackagesContents.replaceFirst('$appName:lib/', '$appName:$kMultiRootScheme:/');
53+
final String generatedPackagesPath = fs.path.setExtension(PackageMap.globalPackagesPath, '.generated');
54+
fs.file(generatedPackagesPath).writeAsStringSync(newPackagesContents);
55+
}
4456
}
4557

4658
class UnsupportedCodeGenerator extends CodeGenerator {
@@ -104,6 +116,7 @@ class CodeGeneratingKernelCompiler implements KernelCompiler {
104116
'build* pipeline');
105117
}
106118
final FlutterProject flutterProject = await FlutterProject.current();
119+
codeGenerator.updatePackages(flutterProject);
107120
final CodegenDaemon codegenDaemon = await codeGenerator.daemon(flutterProject);
108121
codegenDaemon.startBuild();
109122
await for (CodegenStatus codegenStatus in codegenDaemon.buildResults) {
@@ -141,7 +154,7 @@ class CodeGeneratingKernelCompiler implements KernelCompiler {
141154
/// An implementation of a [ResidentCompiler] which runs a [BuildRunner] before
142155
/// talking to the CFE.
143156
class CodeGeneratingResidentCompiler implements ResidentCompiler {
144-
CodeGeneratingResidentCompiler._(this._residentCompiler, this._codegenDaemon);
157+
CodeGeneratingResidentCompiler._(this._residentCompiler, this._codegenDaemon, this._flutterProject);
145158

146159
/// Creates a new [ResidentCompiler] and configures a [BuildDaemonClient] to
147160
/// run builds.
@@ -158,6 +171,7 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
158171
String initializeFromDill,
159172
bool runCold = false,
160173
}) async {
174+
codeGenerator.updatePackages(flutterProject);
161175
final ResidentCompiler residentCompiler = ResidentCompiler(
162176
artifacts.getArtifactPath(Artifact.flutterPatchedSdkPath),
163177
trackWidgetCreation: trackWidgetCreation,
@@ -182,11 +196,12 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
182196
if (status == CodegenStatus.Failed) {
183197
printError('Code generation failed, build may have compile errors.');
184198
}
185-
return CodeGeneratingResidentCompiler._(residentCompiler, codegenDaemon);
199+
return CodeGeneratingResidentCompiler._(residentCompiler, codegenDaemon, flutterProject);
186200
}
187201

188202
final ResidentCompiler _residentCompiler;
189203
final CodegenDaemon _codegenDaemon;
204+
final FlutterProject _flutterProject;
190205

191206
@override
192207
void accept() {
@@ -206,7 +221,13 @@ class CodeGeneratingResidentCompiler implements ResidentCompiler {
206221
});
207222
}
208223
if (_codegenDaemon.lastStatus == CodegenStatus.Failed) {
209-
printError('Codegeneration failed, halting build.');
224+
printError('Code generation failed, build may have compile errors.');
225+
}
226+
// Update the generated packages file if the original packages file has changes.
227+
if (fs.statSync(PackageMap.globalPackagesPath).modified.millisecondsSinceEpoch >
228+
fs.statSync(PackageMap.globalGeneratedPackagesPath).modified.millisecondsSinceEpoch) {
229+
codeGenerator.updatePackages(_flutterProject);
230+
invalidatedFiles.add(fs.file(PackageMap.globalGeneratedPackagesPath).uri);
210231
}
211232
return _residentCompiler.recompile(
212233
mainPath,

0 commit comments

Comments
 (0)