Skip to content

Commit a4e0e2a

Browse files
author
Jonah Williams
authored
Revert "[flutter_tools] remove all pub caching logic (flutter#66776)" (flutter#67572)
This reverts commit 76cbc46.
1 parent 5e4c86d commit a4e0e2a

File tree

12 files changed

+180
-43
lines changed

12 files changed

+180
-43
lines changed

packages/flutter_tools/lib/src/cache.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ class PubDependencies extends ArtifactSet {
700700
context: PubContext.pubGet,
701701
directory: _fileSystem.path.join(_flutterRoot(), 'packages', 'flutter_tools'),
702702
generateSyntheticPackage: false,
703+
skipPubspecYamlCheck: true,
704+
checkLastModified: false,
703705
);
704706
}
705707
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class PackagesGetCommand extends FlutterCommand {
117117
directory: directory,
118118
upgrade: upgrade ,
119119
offline: boolArg('offline'),
120+
checkLastModified: false,
120121
generateSyntheticPackage: flutterProject.manifest.generateSyntheticPackage,
121122
);
122123
pubGetTimer.stop();

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import '../asset.dart';
88
import '../base/common.dart';
99
import '../base/file_system.dart';
1010
import '../build_info.dart';
11+
import '../build_system/build_system.dart';
1112
import '../bundle.dart';
1213
import '../cache.dart';
14+
import '../dart/generate_synthetic_packages.dart';
15+
import '../dart/pub.dart';
1316
import '../devfs.dart';
1417
import '../globals.dart' as globals;
1518
import '../project.dart';
@@ -166,6 +169,32 @@ class TestCommand extends FlutterCommand {
166169
'directory (or one of its subdirectories).');
167170
}
168171
final FlutterProject flutterProject = FlutterProject.current();
172+
if (shouldRunPub) {
173+
if (flutterProject.manifest.generateSyntheticPackage) {
174+
final Environment environment = Environment(
175+
artifacts: globals.artifacts,
176+
logger: globals.logger,
177+
cacheDir: globals.cache.getRoot(),
178+
engineVersion: globals.flutterVersion.engineRevision,
179+
fileSystem: globals.fs,
180+
flutterRootDir: globals.fs.directory(Cache.flutterRoot),
181+
outputDir: globals.fs.directory(getBuildDirectory()),
182+
processManager: globals.processManager,
183+
projectDir: flutterProject.directory,
184+
);
185+
186+
await generateLocalizationsSyntheticPackage(
187+
environment: environment,
188+
buildSystem: globals.buildSystem,
189+
);
190+
}
191+
192+
await pub.get(
193+
context: PubContext.getVerifyContext(name),
194+
skipPubspecYamlCheck: true,
195+
generateSyntheticPackage: flutterProject.manifest.generateSyntheticPackage,
196+
);
197+
}
169198
final bool buildTestAssets = boolArg('test-assets');
170199
final List<String> names = stringsArg('name');
171200
final List<String> plainNames = stringsArg('plain-name');

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ class UpdatePackagesCommand extends FlutterCommand {
330330
context: PubContext.updatePackages,
331331
directory: tempDir.path,
332332
upgrade: true,
333+
checkLastModified: false,
333334
offline: offline,
334335
flutterRootOverride: upgrade
335336
? temporaryFlutterSdk.path
@@ -412,6 +413,7 @@ class UpdatePackagesCommand extends FlutterCommand {
412413
await pub.get(
413414
context: PubContext.updatePackages,
414415
directory: dir.path,
416+
checkLastModified: false,
415417
offline: offline,
416418
generateSyntheticPackage: false,
417419
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ class UpgradeCommandRunner {
296296
context: PubContext.pubUpgrade,
297297
directory: projectRoot,
298298
upgrade: true,
299+
checkLastModified: false,
299300
generateSyntheticPackage: false,
300301
);
301302
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class VersionCommand extends FlutterCommand {
151151
context: PubContext.pubUpgrade,
152152
directory: projectRoot,
153153
upgrade: true,
154+
checkLastModified: false,
154155
generateSyntheticPackage: false,
155156
);
156157
}

packages/flutter_tools/lib/src/context_runner.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ Future<T> runInContext<T>(
236236
botDetector: globals.botDetector,
237237
platform: globals.platform,
238238
usage: globals.flutterUsage,
239+
// Avoid a circular dependency by making this access lazy.
240+
toolStampFile: () => globals.cache.getStampFileFor('flutter_tools'),
239241
),
240242
ShutdownHooks: () => ShutdownHooks(logger: globals.logger),
241243
Stdio: () => Stdio(),

packages/flutter_tools/lib/src/dart/pub.dart

Lines changed: 91 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ abstract class Pub {
8080
@required Platform platform,
8181
@required BotDetector botDetector,
8282
@required Usage usage,
83+
File Function() toolStampFile,
8384
}) = _DefaultPub;
8485

8586
/// Runs `pub get`.
@@ -92,6 +93,8 @@ abstract class Pub {
9293
bool skipIfAbsent = false,
9394
bool upgrade = false,
9495
bool offline = false,
96+
bool checkLastModified = true,
97+
bool skipPubspecYamlCheck = false,
9598
bool generateSyntheticPackage = false,
9699
String flutterRootOverride,
97100
});
@@ -138,7 +141,9 @@ class _DefaultPub implements Pub {
138141
@required Platform platform,
139142
@required BotDetector botDetector,
140143
@required Usage usage,
141-
}) : _fileSystem = fileSystem,
144+
File Function() toolStampFile,
145+
}) : _toolStampFile = toolStampFile,
146+
_fileSystem = fileSystem,
142147
_logger = logger,
143148
_platform = platform,
144149
_botDetector = botDetector,
@@ -154,6 +159,7 @@ class _DefaultPub implements Pub {
154159
final Platform _platform;
155160
final BotDetector _botDetector;
156161
final Usage _usage;
162+
final File Function() _toolStampFile;
157163

158164
@override
159165
Future<void> get({
@@ -162,52 +168,88 @@ class _DefaultPub implements Pub {
162168
bool skipIfAbsent = false,
163169
bool upgrade = false,
164170
bool offline = false,
171+
bool checkLastModified = true,
172+
bool skipPubspecYamlCheck = false,
165173
bool generateSyntheticPackage = false,
166174
String flutterRootOverride,
167175
}) async {
168176
directory ??= _fileSystem.currentDirectory.path;
177+
178+
final File pubSpecYaml = _fileSystem.file(
179+
_fileSystem.path.join(directory, 'pubspec.yaml'));
169180
final File packageConfigFile = _fileSystem.file(
170181
_fileSystem.path.join(directory, '.dart_tool', 'package_config.json'));
171182
final Directory generatedDirectory = _fileSystem.directory(
172183
_fileSystem.path.join(directory, '.dart_tool', 'flutter_gen'));
173184

174-
final String command = upgrade ? 'upgrade' : 'get';
175-
final Status status = _logger.startProgress(
176-
'Running "flutter pub $command" in ${_fileSystem.path.basename(directory)}...',
177-
timeout: const TimeoutConfiguration().slowOperation,
178-
);
179-
final bool verbose = _logger.isVerbose;
180-
final List<String> args = <String>[
181-
if (verbose)
182-
'--verbose'
183-
else
184-
'--verbosity=warning',
185-
...<String>[
186-
command,
187-
'--no-precompile',
188-
],
189-
if (offline)
190-
'--offline',
191-
];
192-
try {
193-
await batch(
194-
args,
195-
context: context,
196-
directory: directory,
197-
failureMessage: 'pub $command failed',
198-
retry: true,
199-
flutterRootOverride: flutterRootOverride,
185+
if (!skipPubspecYamlCheck && !pubSpecYaml.existsSync()) {
186+
if (!skipIfAbsent) {
187+
throwToolExit('$directory: no pubspec.yaml found');
188+
}
189+
return;
190+
}
191+
192+
final DateTime originalPubspecYamlModificationTime = pubSpecYaml.lastModifiedSync();
193+
194+
if (!checkLastModified || _shouldRunPubGet(
195+
pubSpecYaml: pubSpecYaml,
196+
packageConfigFile: packageConfigFile,
197+
)) {
198+
final String command = upgrade ? 'upgrade' : 'get';
199+
final Status status = _logger.startProgress(
200+
'Running "flutter pub $command" in ${_fileSystem.path.basename(directory)}...',
201+
timeout: const TimeoutConfiguration().slowOperation,
200202
);
201-
status.stop();
202-
// The exception is rethrown, so don't catch only Exceptions.
203-
} catch (exception) { // ignore: avoid_catches_without_on_clauses
204-
status.cancel();
205-
rethrow;
203+
final bool verbose = _logger.isVerbose;
204+
final List<String> args = <String>[
205+
if (verbose)
206+
'--verbose'
207+
else
208+
'--verbosity=warning',
209+
...<String>[
210+
command,
211+
'--no-precompile',
212+
],
213+
if (offline)
214+
'--offline',
215+
];
216+
try {
217+
await batch(
218+
args,
219+
context: context,
220+
directory: directory,
221+
failureMessage: 'pub $command failed',
222+
retry: true,
223+
flutterRootOverride: flutterRootOverride,
224+
);
225+
status.stop();
226+
// The exception is rethrown, so don't catch only Exceptions.
227+
} catch (exception) { // ignore: avoid_catches_without_on_clauses
228+
status.cancel();
229+
rethrow;
230+
}
206231
}
207232

208233
if (!packageConfigFile.existsSync()) {
209234
throwToolExit('$directory: pub did not create .dart_tools/package_config.json file.');
210235
}
236+
if (pubSpecYaml.lastModifiedSync() != originalPubspecYamlModificationTime) {
237+
throwToolExit(
238+
'$directory: unexpected concurrent modification of '
239+
'pubspec.yaml while running pub.');
240+
}
241+
// We don't check if dotPackages was actually modified, because as far as we can tell sometimes
242+
// pub will decide it does not need to actually modify it.
243+
final DateTime now = DateTime.now();
244+
if (now.isBefore(originalPubspecYamlModificationTime)) {
245+
_logger.printError(
246+
'Warning: File "${_fileSystem.path.absolute(pubSpecYaml.path)}" was created in the future. '
247+
'Optimizations that rely on comparing time stamps will be unreliable. Check your '
248+
'system clock for accuracy.\n'
249+
'The timestamp was: $originalPubspecYamlModificationTime\n'
250+
'The time now is: $now'
251+
);
252+
}
211253
await _updatePackageConfig(
212254
packageConfigFile,
213255
generatedDirectory,
@@ -350,6 +392,23 @@ class _DefaultPub implements Pub {
350392
return <String>[sdkPath, ...arguments];
351393
}
352394

395+
bool _shouldRunPubGet({ @required File pubSpecYaml, @required File packageConfigFile }) {
396+
if (!packageConfigFile.existsSync()) {
397+
return true;
398+
}
399+
final DateTime dotPackagesLastModified = packageConfigFile.lastModifiedSync();
400+
if (pubSpecYaml.lastModifiedSync().isAfter(dotPackagesLastModified)) {
401+
return true;
402+
}
403+
final File toolStampFile = _toolStampFile != null ? _toolStampFile() : null;
404+
if (toolStampFile != null &&
405+
toolStampFile.existsSync() &&
406+
toolStampFile.lastModifiedSync().isAfter(dotPackagesLastModified)) {
407+
return true;
408+
}
409+
return false;
410+
}
411+
353412
// Returns the environment value that should be used when running pub.
354413
//
355414
// Includes any existing environment variable, if one exists.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,9 +950,9 @@ abstract class FlutterCommand extends Command<void> {
950950
// First always update universal artifacts, as some of these (e.g.
951951
// ios-deploy on macOS) are required to determine `requiredArtifacts`.
952952
await globals.cache.updateAll(<DevelopmentArtifact>{DevelopmentArtifact.universal});
953+
953954
await globals.cache.updateAll(await requiredArtifacts);
954955
}
955-
Cache.releaseLock();
956956

957957
await validateCommand();
958958

@@ -979,7 +979,11 @@ abstract class FlutterCommand extends Command<void> {
979979
context: PubContext.getVerifyContext(name),
980980
generateSyntheticPackage: project.manifest.generateSyntheticPackage,
981981
);
982+
// All done updating dependencies. Release the cache lock.
983+
Cache.releaseLock();
982984
await project.ensureReadyForPlatformSpecificTooling(checkProjects: true);
985+
} else {
986+
Cache.releaseLock();
983987
}
984988

985989
setupApplicationPackages();

packages/flutter_tools/test/general.shard/cache_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,9 @@ void main() {
743743
verify(pub.get(
744744
context: PubContext.pubGet,
745745
directory: 'packages/flutter_tools',
746+
generateSyntheticPackage: false,
747+
skipPubspecYamlCheck: true,
748+
checkLastModified: false,
746749
)).called(1);
747750
});
748751
}

0 commit comments

Comments
 (0)