Skip to content

Commit 82a6f9b

Browse files
[flutter_tools] remove most use of global packages path (flutter#60231)
The global packages path could cause tests to fail when it would be overriden to unexpected (in test setup) values. Remove most usage and make it a configuration on buildInfo, along with most other build information. Cleanup the asset builder to require the .packages path and the resident runners to no longer require it, since they already have the information in build_info. It needs to stick around for the fuchsia deps we do not control. Filled flutter#60232 for remaining work.
1 parent 9322c80 commit 82a6f9b

32 files changed

+128
-108
lines changed

packages/flutter_tools/lib/src/android/gradle.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,7 @@ Future<void> buildPluginsAsAar(
790790
BuildMode.release, // Plugins are built as release.
791791
null, // Plugins don't define flavors.
792792
treeShakeIcons: androidBuildInfo.buildInfo.treeShakeIcons,
793+
packagesPath: androidBuildInfo.buildInfo.packagesPath,
793794
),
794795
),
795796
target: '',

packages/flutter_tools/lib/src/asset.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ abstract class AssetBundle {
5151
Future<int> build({
5252
String manifestPath = defaultManifestPath,
5353
String assetDirPath,
54-
String packagesPath,
54+
@required String packagesPath,
5555
bool includeDefaultFonts = true,
5656
bool reportLicensedPackages = false,
5757
});
@@ -122,12 +122,11 @@ class ManifestAssetBundle implements AssetBundle {
122122
Future<int> build({
123123
String manifestPath = defaultManifestPath,
124124
String assetDirPath,
125-
String packagesPath,
125+
@required String packagesPath,
126126
bool includeDefaultFonts = true,
127127
bool reportLicensedPackages = false,
128128
}) async {
129129
assetDirPath ??= getAssetBuildDirectory();
130-
packagesPath ??= globals.fs.path.absolute(globalPackagesPath);
131130
FlutterManifest flutterManifest;
132131
try {
133132
flutterManifest = FlutterManifest.createFromPath(

packages/flutter_tools/lib/src/build_info.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class BuildInfo {
2929
this.dartExperiments = const <String>[],
3030
@required this.treeShakeIcons,
3131
this.performanceMeasurementFile,
32+
this.packagesPath = '.packages',
3233
});
3334

3435
final BuildMode mode;
@@ -44,6 +45,12 @@ class BuildInfo {
4445
/// Mode-Flavor (e.g. Release-Paid).
4546
final String flavor;
4647

48+
/// The path to the .packages file to use for compilation.
49+
///
50+
/// This is used by package:package_config to locate the actual package_config.json
51+
/// file. If not provded, defaults to `.packages`.
52+
final String packagesPath;
53+
4754
final List<String> fileSystemRoots;
4855
final String fileSystemScheme;
4956

@@ -156,6 +163,8 @@ class BuildInfo {
156163
'PERFORMANCE_MEASUREMENT_FILE': performanceMeasurementFile,
157164
if (bundleSkSLPath != null)
158165
'BUNDLE_SKSL_PATH': bundleSkSLPath,
166+
if (packagesPath != null)
167+
'PACKAGE_CONFIG': packagesPath,
159168
};
160169
}
161170
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class WebAssetServer implements AssetReader {
140140
int port,
141141
UrlTunneller urlTunneller,
142142
bool useSseForDebugProxy,
143-
BuildMode buildMode,
143+
BuildInfo buildInfo,
144144
bool enableDwds,
145145
Uri entrypoint,
146146
ExpressionCompiler expressionCompiler, {
@@ -156,7 +156,7 @@ class WebAssetServer implements AssetReader {
156156
}
157157
final HttpServer httpServer = await HttpServer.bind(address, port);
158158
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
159-
globals.fs.file(globalPackagesPath),
159+
globals.fs.file(buildInfo.packagesPath),
160160
logger: globals.logger,
161161
);
162162
final Map<String, String> digests = <String, String>{};
@@ -173,7 +173,7 @@ class WebAssetServer implements AssetReader {
173173
}
174174

175175
// In release builds deploy a simpler proxy server.
176-
if (buildMode != BuildMode.debug) {
176+
if (buildInfo.mode != BuildMode.debug) {
177177
final ReleaseAssetServer releaseAssetServer = ReleaseAssetServer(
178178
entrypoint,
179179
fileSystem: globals.fs,
@@ -589,7 +589,7 @@ class WebDevFS implements DevFS {
589589
@required this.packagesFilePath,
590590
@required this.urlTunneller,
591591
@required this.useSseForDebugProxy,
592-
@required this.buildMode,
592+
@required this.buildInfo,
593593
@required this.enableDwds,
594594
@required this.entrypoint,
595595
@required this.expressionCompiler,
@@ -603,7 +603,7 @@ class WebDevFS implements DevFS {
603603
final String packagesFilePath;
604604
final UrlTunneller urlTunneller;
605605
final bool useSseForDebugProxy;
606-
final BuildMode buildMode;
606+
final BuildInfo buildInfo;
607607
final bool enableDwds;
608608
final bool testMode;
609609
final ExpressionCompiler expressionCompiler;
@@ -670,7 +670,7 @@ class WebDevFS implements DevFS {
670670
port,
671671
urlTunneller,
672672
useSseForDebugProxy,
673-
buildMode,
673+
buildInfo,
674674
enableDwds,
675675
entrypoint,
676676
expressionCompiler,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class DwdsWebRunnerFactory extends WebRunnerFactory {
6666
}
6767
}
6868

69-
const String kExitMessage = 'Failed to establish connection with the application '
69+
const String kExitMessage = 'Failed to establish connection with the application '
7070
'instance in Chrome.\nThis can happen if the websocket connection used by the '
7171
'web tooling is unable to correctly establish a connection, for example due to a firewall.';
7272

@@ -447,7 +447,7 @@ class _ResidentWebRunner extends ResidentWebRunner {
447447
packagesFilePath: packagesFilePath,
448448
urlTunneller: urlTunneller,
449449
useSseForDebugProxy: debuggingOptions.webUseSseForDebugProxy,
450-
buildMode: debuggingOptions.buildInfo.mode,
450+
buildInfo: debuggingOptions.buildInfo,
451451
enableDwds: _enableDwds,
452452
entrypoint: globals.fs.file(target).uri,
453453
expressionCompiler: expressionCompiler,
@@ -639,7 +639,7 @@ class _ResidentWebRunner extends ResidentWebRunner {
639639
final bool rebuildBundle = assetBundle.needsBuild();
640640
if (rebuildBundle) {
641641
globals.printTrace('Updating assets');
642-
final int result = await assetBundle.build();
642+
final int result = await assetBundle.build(packagesPath: debuggingOptions.buildInfo.packagesPath);
643643
if (result != 0) {
644644
return UpdateFSReport(success: false);
645645
}

packages/flutter_tools/lib/src/build_system/targets/web.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ class WebEntrypointTarget extends Target {
5858
final bool shouldInitializePlatform = environment.defines[kInitializePlatform] == 'true';
5959
final bool hasPlugins = environment.defines[kHasWebPlugins] == 'true';
6060
final Uri importUri = environment.fileSystem.file(targetFile).absolute.uri;
61+
// TODO(jonahwilliams): support configuration of this file.
62+
const String packageFile = '.packages';
6163
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
62-
environment.projectDir.childFile('.packages'),
64+
environment.fileSystem.file(packageFile),
6365
logger: environment.logger,
6466
);
6567

@@ -156,7 +158,8 @@ class Dart2JSTarget extends Target {
156158
final BuildMode buildMode = getBuildModeForName(environment.defines[kBuildMode]);
157159
final String specPath = globals.fs.path.join(
158160
globals.artifacts.getArtifactPath(Artifact.flutterWebSdk), 'libraries.json');
159-
final String packageFile = globalPackagesPath;
161+
// TODO(jonahwilliams): support configuration of this file.
162+
const String packageFile = '.packages';
160163
final File outputKernel = environment.buildDir.childFile('app.dill');
161164
final File outputFile = environment.buildDir.childFile('main.dart.js');
162165
final List<String> dartDefines = decodeDartDefines(environment.defines, kDartDefines);

packages/flutter_tools/lib/src/bundle.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import 'build_system/targets/common.dart';
2020
import 'build_system/targets/icon_tree_shaker.dart';
2121
import 'cache.dart';
2222
import 'convert.dart';
23-
import 'dart/package_map.dart';
2423
import 'devfs.dart';
2524
import 'globals.dart' as globals;
2625
import 'project.dart';
@@ -95,7 +94,7 @@ class BundleBuilder {
9594
mainPath ??= defaultMainPath;
9695
depfilePath ??= defaultDepfilePath;
9796
assetDirPath ??= getAssetBuildDirectory();
98-
packagesPath ??= globals.fs.path.absolute(globalPackagesPath);
97+
packagesPath ??= globals.fs.path.absolute('.packages');
9998
final FlutterProject flutterProject = FlutterProject.current();
10099
await buildWithAssemble(
101100
buildMode: buildInfo.mode,
@@ -192,12 +191,12 @@ Future<void> buildWithAssemble({
192191
Future<AssetBundle> buildAssets({
193192
String manifestPath,
194193
String assetDirPath,
195-
String packagesPath,
194+
@required String packagesPath,
196195
bool includeDefaultFonts = true,
197196
bool reportLicensedPackages = false,
198197
}) async {
199198
assetDirPath ??= getAssetBuildDirectory();
200-
packagesPath ??= globals.fs.path.absolute(globalPackagesPath);
199+
packagesPath ??= globals.fs.path.absolute(packagesPath);
201200

202201
// Build the asset bundle.
203202
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();

packages/flutter_tools/lib/src/commands/attach.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ class HotRunnerFactory {
415415
applicationBinary: applicationBinary,
416416
hostIsIde: hostIsIde,
417417
projectRootPath: projectRootPath,
418-
packagesFilePath: packagesFilePath,
419418
dillOutputPath: dillOutputPath,
420419
stayResident: stayResident,
421420
ipv6: ipv6,

packages/flutter_tools/lib/src/commands/daemon.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ class AppDomain extends Domain {
486486
debuggingOptions: options,
487487
applicationBinary: applicationBinary,
488488
projectRootPath: projectRootPath,
489-
packagesFilePath: packagesFilePath,
490489
dillOutputPath: dillOutputPath,
491490
ipv6: ipv6,
492491
hostIsIde: true,

packages/flutter_tools/lib/src/commands/run.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,6 @@ class RunCommand extends RunCommandBase {
536536
? null
537537
: globals.fs.file(applicationBinaryPath),
538538
projectRootPath: stringArg('project-root'),
539-
packagesFilePath: globalResults['packages'] as String,
540539
dillOutputPath: stringArg('output-dill'),
541540
stayResident: stayResident,
542541
ipv6: ipv6,

packages/flutter_tools/lib/src/commands/test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class TestCommand extends FlutterCommand {
296296

297297
Future<void> _buildTestAsset() async {
298298
final AssetBundle assetBundle = AssetBundleFactory.instance.createBundle();
299-
final int build = await assetBundle.build();
299+
final int build = await assetBundle.build(packagesPath: '.packages');
300300
if (build != 0) {
301301
throwToolExit('Error: Failed to build asset bundle');
302302
}

packages/flutter_tools/lib/src/dart/package_map.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import '../base/logger.dart';
1313

1414
const String kPackagesFileName = '.packages';
1515

16+
// No touching!
1617
String get globalPackagesPath => _globalPackagesPath ?? kPackagesFileName;
1718

1819
set globalPackagesPath(String value) {
@@ -55,7 +56,7 @@ Future<PackageConfig> loadPackageConfigWithLogging(File file, {
5556
message += '\nDid you run this command from the same directory as your pubspec.yaml file?';
5657
}
5758
logger.printError(message);
58-
throwToolExit(null);
59+
throwToolExit(file.path);
5960
}
6061
);
6162
}

packages/flutter_tools/lib/src/plugins.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ Future<List<Plugin>> findPlugins(FlutterProject project) async {
321321
final List<Plugin> plugins = <Plugin>[];
322322
final String packagesFile = globals.fs.path.join(
323323
project.directory.path,
324-
globalPackagesPath,
324+
'.packages',
325325
);
326326
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
327327
globals.fs.file(packagesFile),

packages/flutter_tools/lib/src/resident_runner.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import 'bundle.dart';
2626
import 'cache.dart';
2727
import 'codegen.dart';
2828
import 'compile.dart';
29-
import 'dart/package_map.dart';
3029
import 'devfs.dart';
3130
import 'device.dart';
3231
import 'features.dart';
@@ -60,7 +59,7 @@ class FlutterDevice {
6059
fileSystemScheme: fileSystemScheme,
6160
targetModel: targetModel,
6261
dartDefines: buildInfo.dartDefines,
63-
packagesPath: globalPackagesPath,
62+
packagesPath: buildInfo.packagesPath,
6463
extraFrontEndOptions: buildInfo.extraFrontEndOptions,
6564
artifacts: globals.artifacts,
6665
processManager: globals.processManager,
@@ -113,7 +112,7 @@ class FlutterDevice {
113112
dartDefines: buildInfo.dartDefines,
114113
librariesSpec: globals.fs.file(globals.artifacts
115114
.getArtifactPath(Artifact.flutterWebLibrariesJson)).uri.toString(),
116-
packagesPath: globalPackagesPath,
115+
packagesPath: buildInfo.packagesPath,
117116
artifacts: globals.artifacts,
118117
processManager: globals.processManager,
119118
logger: globals.logger,
@@ -136,7 +135,7 @@ class FlutterDevice {
136135
trackWidgetCreation: buildInfo.trackWidgetCreation,
137136
dartDefines: buildInfo.dartDefines,
138137
),
139-
packagesPath: globalPackagesPath,
138+
packagesPath: buildInfo.packagesPath,
140139
artifacts: globals.artifacts,
141140
processManager: globals.processManager,
142141
logger: globals.logger,
@@ -683,16 +682,15 @@ abstract class ResidentRunner {
683682
ResidentRunner(
684683
this.flutterDevices, {
685684
this.target,
686-
this.debuggingOptions,
685+
@required this.debuggingOptions,
687686
String projectRootPath,
688-
String packagesFilePath,
689687
this.ipv6,
690688
this.stayResident = true,
691689
this.hotMode = true,
692690
String dillOutputPath,
693691
}) : mainPath = findMainDartFile(target),
692+
packagesFilePath = debuggingOptions.buildInfo.packagesPath,
694693
projectRootPath = projectRootPath ?? globals.fs.currentDirectory.path,
695-
packagesFilePath = packagesFilePath ?? globals.fs.path.absolute(globalPackagesPath),
696694
_dillOutputPath = dillOutputPath,
697695
artifactDirectory = dillOutputPath == null
698696
? globals.fs.systemTempDirectory.createTempSync('flutter_tool.')

packages/flutter_tools/lib/src/run_cold.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ import 'resident_runner.dart';
1313
import 'tracing.dart';
1414
import 'vmservice.dart';
1515

16-
// TODO(mklim): Test this, flutter/flutter#23031.
1716
class ColdRunner extends ResidentRunner {
1817
ColdRunner(
1918
List<FlutterDevice> devices, {
2019
String target,
21-
DebuggingOptions debuggingOptions,
20+
@required DebuggingOptions debuggingOptions,
2221
this.traceStartup = false,
2322
this.awaitFirstFrameWhenTracing = true,
2423
this.applicationBinary,

packages/flutter_tools/lib/src/run_hot.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,18 @@ class HotRunner extends ResidentRunner {
6767
HotRunner(
6868
List<FlutterDevice> devices, {
6969
String target,
70-
DebuggingOptions debuggingOptions,
70+
@required DebuggingOptions debuggingOptions,
7171
this.benchmarkMode = false,
7272
this.applicationBinary,
7373
this.hostIsIde = false,
7474
String projectRootPath,
75-
String packagesFilePath,
7675
String dillOutputPath,
7776
bool stayResident = true,
7877
bool ipv6 = false,
7978
}) : super(devices,
8079
target: target,
8180
debuggingOptions: debuggingOptions,
8281
projectRootPath: projectRootPath,
83-
packagesFilePath: packagesFilePath,
8482
stayResident: stayResident,
8583
hotMode: true,
8684
dillOutputPath: dillOutputPath,
@@ -157,7 +155,7 @@ class HotRunner extends ResidentRunner {
157155
final UpdateFSReport results = UpdateFSReport(success: true);
158156
final List<Uri> invalidated = <Uri>[Uri.parse(libraryId)];
159157
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
160-
globals.fs.file(globalPackagesPath),
158+
globals.fs.file(debuggingOptions.buildInfo.packagesPath),
161159
logger: globals.logger,
162160
);
163161
for (final FlutterDevice device in flutterDevices) {
@@ -343,7 +341,7 @@ class HotRunner extends ResidentRunner {
343341

344342
final List<Future<bool>> startupTasks = <Future<bool>>[];
345343
final PackageConfig packageConfig = await loadPackageConfigWithLogging(
346-
globals.fs.file(globalPackagesPath),
344+
globals.fs.file(debuggingOptions.buildInfo.packagesPath),
347345
logger: globals.logger,
348346
);
349347
for (final FlutterDevice device in flutterDevices) {
@@ -407,7 +405,7 @@ class HotRunner extends ResidentRunner {
407405
final bool rebuildBundle = assetBundle.needsBuild();
408406
if (rebuildBundle) {
409407
globals.printTrace('Updating assets');
410-
final int result = await assetBundle.build();
408+
final int result = await assetBundle.build(packagesPath: '.packages');
411409
if (result != 0) {
412410
return UpdateFSReport(success: false);
413411
}
@@ -1289,7 +1287,7 @@ class ProjectFileInvalidator {
12891287

12901288
Future<PackageConfig> _createPackageConfig(String packagesPath) {
12911289
return loadPackageConfigWithLogging(
1292-
_fileSystem.file(globalPackagesPath),
1290+
_fileSystem.file(packagesPath),
12931291
logger: _logger,
12941292
);
12951293
}

packages/flutter_tools/lib/src/runner/flutter_command.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ abstract class FlutterCommand extends Command<void> {
685685
bundleSkSLPath: bundleSkSLPath,
686686
dartExperiments: experiments,
687687
performanceMeasurementFile: performanceMeasurementFile,
688+
packagesPath: globalResults['packages'] as String ?? '.packages'
688689
);
689690
}
690691

0 commit comments

Comments
 (0)