Skip to content

Commit d59fb52

Browse files
authored
Don't rewrite pubspec.lock and workspace_ref if not modified (#4589)
1 parent 59406fa commit d59fb52

File tree

4 files changed

+74
-49
lines changed

4 files changed

+74
-49
lines changed

lib/src/entrypoint.dart

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ See $workspacesDocUrl for more information.''',
405405
Future<void> writePackageConfigFiles() async {
406406
ensureDir(p.dirname(packageConfigPath));
407407

408-
_writeIfDifferent(
408+
writeTextFileIfDifferent(
409409
packageConfigPath,
410410
await _packageConfigFile(
411411
cache,
@@ -416,7 +416,7 @@ See $workspacesDocUrl for more information.''',
416416
?.effectiveConstraint,
417417
),
418418
);
419-
_writeIfDifferent(packageGraphPath, await _packageGraphFile(cache));
419+
writeTextFileIfDifferent(packageGraphPath, await _packageGraphFile(cache));
420420

421421
if (workspaceRoot.workspaceChildren.isNotEmpty) {
422422
for (final package in workspaceRoot.transitiveWorkspace) {
@@ -430,7 +430,7 @@ See $workspacesDocUrl for more information.''',
430430
final workspaceRef = const JsonEncoder.withIndent(
431431
' ',
432432
).convert({'workspaceRoot': relativeRootPath});
433-
writeTextFile(workspaceRefPath, '$workspaceRef\n');
433+
writeTextFileIfDifferent(workspaceRefPath, '$workspaceRef\n');
434434
}
435435
}
436436
}
@@ -526,17 +526,6 @@ See $workspacesDocUrl for more information.''',
526526
return '$jsonText\n';
527527
}
528528

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

lib/src/io.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,17 @@ void writeTextFile(
257257
File(file).writeAsStringSync(contents, encoding: encoding);
258258
}
259259

260+
void writeTextFileIfDifferent(String path, String newContent) {
261+
// Compare to the present package_config.json
262+
// For purposes of equality we don't care about the `generated` timestamp.
263+
final originalText = tryReadTextFile(path);
264+
if (originalText != newContent) {
265+
writeTextFile(path, newContent);
266+
} else {
267+
log.fine('`$path` is unchanged. Not rewriting.');
268+
}
269+
}
270+
260271
/// Reads the contents of the binary file [file].
261272
void writeBinaryFile(String file, Uint8List data) {
262273
log.io('Writing ${data.length} bytes to file $file.');

lib/src/lock_file.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ ${yamlToString(data)}
406406
detectWindowsLineEndings(readTextFile(lockFilePath));
407407

408408
final serialized = serialize(p.dirname(lockFilePath), cache);
409-
writeTextFile(
409+
writeTextFileIfDifferent(
410410
lockFilePath,
411411
windowsLineEndings ? serialized.replaceAll('\n', '\r\n') : serialized,
412412
);

test/package_config_file_test.dart

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -313,42 +313,67 @@ void main() {
313313
});
314314
});
315315

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();
316+
test('pubspec.lock, package_config, package_graph and workspace_ref '
317+
'are not rewritten if unchanged', () async {
318+
final server = await servePackages();
319+
server.serve('foo', '1.0.0');
320+
321+
await d.dir(appPath, [
322+
d.appPubspec(
323+
dependencies: {'foo': 'any'},
324+
extras: {
325+
'workspace': ['foo'],
326+
'environment': {'sdk': '^3.5.0'},
327+
},
328+
),
329+
d.dir('foo', [d.libPubspec('foo', '1.0.0', resolutionWorkspace: true)]),
330+
]).create();
323331

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-
),
332+
await pubGet(environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'});
333+
final packageConfigFile = File(
334+
p.join(sandbox, appPath, '.dart_tool', 'package_config.json'),
335+
);
336+
final packageConfig = jsonDecode(packageConfigFile.readAsStringSync());
337+
final packageConfigTimestamp = packageConfigFile.lastModifiedSync();
338+
final lockFile = File(p.join(sandbox, appPath, 'pubspec.lock'));
339+
final lockfileTimestamp = lockFile.lastModifiedSync();
340+
final packageGraphFile = File(
341+
p.join(sandbox, appPath, '.dart_tool', 'package_graph.json'),
342+
);
343+
final packageGraph = jsonDecode(packageGraphFile.readAsStringSync());
344+
final packageGraphTimestamp = packageGraphFile.lastModifiedSync();
345+
final workspaceRefFile = File(
346+
p.join(
347+
sandbox,
348+
appPath,
349+
'foo',
350+
'.dart_tool',
351+
'pub',
352+
'workspace_ref.json',
353+
),
354+
);
355+
final workspaceRefTimestamp = workspaceRefFile.lastModifiedSync();
356+
final s = p.separator;
357+
await pubGet(
358+
silent: allOf(
359+
contains(
360+
'`.dart_tool${s}package_config.json` is unchanged. Not rewriting.',
344361
),
345-
);
362+
contains(
363+
'`.dart_tool${s}package_graph.json` is unchanged. Not rewriting.',
364+
),
365+
),
366+
environment: {'_PUB_TEST_SDK_VERSION': '3.5.0'},
367+
);
368+
// The resolution of timestamps is not that good.
369+
await Future<Null>.delayed(const Duration(seconds: 1));
370+
expect(packageConfig, jsonDecode(packageConfigFile.readAsStringSync()));
371+
expect(packageConfigFile.lastModifiedSync(), packageConfigTimestamp);
346372

347-
expect(packageConfig, jsonDecode(packageConfigFile.readAsStringSync()));
348-
expect(packageConfigFile.lastModifiedSync(), packageConfigTimestamp);
373+
expect(packageGraph, jsonDecode(packageGraphFile.readAsStringSync()));
374+
expect(packageGraphFile.lastModifiedSync(), packageGraphTimestamp);
349375

350-
expect(packageGraph, jsonDecode(packageGraphFile.readAsStringSync()));
351-
expect(packageGraphFile.lastModifiedSync(), packageGraphTimestamp);
352-
},
353-
);
376+
expect(lockFile.lastModifiedSync(), lockfileTimestamp);
377+
expect(workspaceRefFile.lastModifiedSync(), workspaceRefTimestamp);
378+
});
354379
}

0 commit comments

Comments
 (0)