Skip to content

Commit b2c03b4

Browse files
authored
Don't rewrite package_config and package_graph if unchanged (#4549)
1 parent 528f510 commit b2c03b4

File tree

5 files changed

+61
-27
lines changed

5 files changed

+61
-27
lines changed

lib/src/entrypoint.dart

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,18 @@ See $workspacesDocUrl for more information.''',
392392
/// Writes the .dart_tool/package_config.json file and workspace references to
393393
/// it.
394394
///
395+
/// Compares it to the existing .dart_tool/package_config.json and does not
396+
/// rewrite it unless it is
397+
///
395398
/// Also writes the .dart_tool.package_graph.json file.
396399
///
397400
/// If the workspace is non-trivial: For each package in the workspace write:
398401
/// `.dart_tool/pub/workspace_ref.json` with a pointer to the workspace root
399402
/// package dir.
400403
Future<void> writePackageConfigFiles() async {
401404
ensureDir(p.dirname(packageConfigPath));
402-
writeTextFile(
405+
406+
_writeIfDifferent(
403407
packageConfigPath,
404408
await _packageConfigFile(
405409
cache,
@@ -410,7 +414,8 @@ See $workspacesDocUrl for more information.''',
410414
?.effectiveConstraint,
411415
),
412416
);
413-
writeTextFile(packageGraphPath, await _packageGraphFile(cache));
417+
_writeIfDifferent(packageGraphPath, await _packageGraphFile(cache));
418+
414419
if (workspaceRoot.workspaceChildren.isNotEmpty) {
415420
for (final package in workspaceRoot.transitiveWorkspace) {
416421
final workspaceRefDir = p.join(package.dir, '.dart_tool', 'pub');
@@ -501,7 +506,6 @@ See $workspacesDocUrl for more information.''',
501506
final packageConfig = PackageConfig(
502507
configVersion: 2,
503508
packages: entries,
504-
generated: DateTime.now(),
505509
generator: 'pub',
506510
generatorVersion: sdk.version,
507511
additionalProperties: {
@@ -520,6 +524,17 @@ See $workspacesDocUrl for more information.''',
520524
return '$jsonText\n';
521525
}
522526

527+
void _writeIfDifferent(String path, String newContent) {
528+
// Compare to the present package_config.json
529+
// For purposes of equality we don't care about the `generated` timestamp.
530+
final originalText = tryReadTextFile(path);
531+
if (originalText != newContent) {
532+
writeTextFile(path, newContent);
533+
} else {
534+
log.fine('`$path` is unchanged. Not rewriting.');
535+
}
536+
}
537+
523538
/// Gets all dependencies of the [workspaceRoot] package.
524539
///
525540
/// Performs version resolution according to [SolveType].

lib/src/package_config.dart

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ class PackageConfig {
1919
/// Packages configured.
2020
List<PackageConfigEntry> packages;
2121

22-
/// Date-time the `.dart_tool/package_config.json` file was generated.
23-
///
24-
/// `null` if not given.
25-
DateTime? generated;
26-
2722
/// Tool that generated the `.dart_tool/package_config.json` file.
2823
///
2924
/// For `pub` this is always `'pub'`.
@@ -46,7 +41,6 @@ class PackageConfig {
4641
PackageConfig({
4742
required this.configVersion,
4843
required this.packages,
49-
this.generated,
5044
this.generator,
5145
this.generatorVersion,
5246
Map<String, dynamic>? additionalProperties,
@@ -98,16 +92,6 @@ class PackageConfig {
9892
packages.add(PackageConfigEntry.fromJson(entry as Object));
9993
}
10094

101-
// Read the 'generated' property
102-
DateTime? generated;
103-
final generatedRaw = root['generated'];
104-
if (generatedRaw != null) {
105-
if (generatedRaw is! String) {
106-
throwFormatException('generated', 'must be a string, if given');
107-
}
108-
generated = DateTime.parse(generatedRaw);
109-
}
110-
11195
// Read the 'generator' property
11296
final generator = root['generator'];
11397
if (generator is! String?) {
@@ -136,7 +120,6 @@ class PackageConfig {
136120
return PackageConfig(
137121
configVersion: configVersion,
138122
packages: packages,
139-
generated: generated,
140123
generator: generator,
141124
generatorVersion: generatorVersion,
142125
additionalProperties: Map.fromEntries(
@@ -158,7 +141,6 @@ class PackageConfig {
158141
Map<String, Object?> toJson() => {
159142
'configVersion': configVersion,
160143
'packages': packages.map((p) => p.toJson()).toList(),
161-
'generated': generated?.toUtc().toIso8601String(),
162144
'generator': generator,
163145
'generatorVersion': generatorVersion?.toString(),
164146
}..addAll(additionalProperties);

test/descriptor/package_config.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class PackageConfigFileDescriptor extends Descriptor {
2828
packages: _packages,
2929
generatorVersion: Version.parse(_generatorVersion),
3030
generator: 'pub',
31-
generated: DateTime.now().toUtc(),
3231
additionalProperties: {
3332
'pubCache': p.toUri(_pubCache).toString(),
3433
if (_flutterRoot != null)
@@ -94,9 +93,6 @@ class PackageConfigFileDescriptor extends Descriptor {
9493
);
9594

9695
final expected = PackageConfig.fromJson(_config.toJson());
97-
// omit generated date-time and packages
98-
expected.generated = null; // comparing timestamps is unnecessary.
99-
config.generated = null;
10096
expected.packages = []; // Already compared packages (ignoring ordering)
10197
config.packages = [];
10298
expect(

test/package_config_file_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert';
6+
import 'dart:io';
7+
58
import 'package:path/path.dart' as p;
69
import 'package:pub/src/exit_codes.dart' as exit_codes;
710
import 'package:pub/src/package_config.dart';
811
import 'package:test/test.dart';
12+
import 'package:test_descriptor/test_descriptor.dart';
913

1014
import 'descriptor.dart' as d;
1115
import 'test_pub.dart';
@@ -308,4 +312,43 @@ void main() {
308312
]).validate();
309313
});
310314
});
315+
316+
test(
317+
'package_config and package_graph are not rewritten if unchanged',
318+
() async {
319+
final server = await servePackages();
320+
server.serve('foo', '1.0.0');
321+
322+
await d.appDir(dependencies: {'foo': 'any'}).create();
323+
324+
await pubGet();
325+
final packageConfigFile = File(
326+
p.join(sandbox, appPath, '.dart_tool', 'package_config.json'),
327+
);
328+
final packageConfig = jsonDecode(packageConfigFile.readAsStringSync());
329+
final packageConfigTimestamp = packageConfigFile.lastModifiedSync();
330+
final packageGraphFile = File(
331+
p.join(sandbox, appPath, '.dart_tool', 'package_graph.json'),
332+
);
333+
final packageGraph = jsonDecode(packageGraphFile.readAsStringSync());
334+
final packageGraphTimestamp = packageGraphFile.lastModifiedSync();
335+
final s = p.separator;
336+
await pubGet(
337+
silent: allOf(
338+
contains(
339+
'`.dart_tool${s}package_config.json` is unchanged. Not rewriting.',
340+
),
341+
contains(
342+
'`.dart_tool${s}package_graph.json` is unchanged. Not rewriting.',
343+
),
344+
),
345+
);
346+
347+
expect(packageConfig, jsonDecode(packageConfigFile.readAsStringSync()));
348+
expect(packageConfigFile.lastModifiedSync(), packageConfigTimestamp);
349+
350+
expect(packageGraph, jsonDecode(packageGraphFile.readAsStringSync()));
351+
expect(packageGraphFile.lastModifiedSync(), packageGraphTimestamp);
352+
},
353+
);
311354
}

test/testdata/goldens/embedding/embedding_test/logfile is written with --verbose and on unexpected exceptions.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ MSG : Logs written to $SANDBOX/cache/log/pub_log.txt.
122122
[E] | "languageVersion": "3.0"
123123
[E] | }
124124
[E] | ],
125-
[E] | "generated": "$TIME",
126125
[E] | "generator": "pub",
127126
[E] | "generatorVersion": "3.1.2+3",
128127
[E] | "pubCache": "file://$SANDBOX/cache"
@@ -309,7 +308,6 @@ FINE: Contents:
309308
| "languageVersion": "3.0"
310309
| }
311310
| ],
312-
| "generated": "$TIME",
313311
| "generator": "pub",
314312
| "generatorVersion": "3.1.2+3",
315313
| "pubCache": "file://$SANDBOX/cache"

0 commit comments

Comments
 (0)