Skip to content

Commit 6a72877

Browse files
authored
Track dart version and invalidate if it changes (#1009)
1 parent 4c92e78 commit 6a72877

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

build_runner/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
`--track-performance` flag to enable it.
55
- The heartbeat logger will now log the current number of completed versus
66
scheduled actions, and it will log once a second instead of every 5 seconds.
7+
- Builds will now be invalidated when the dart SDK is updated.
78

89
## 0.7.10+1
910

build_runner/lib/src/asset_graph/graph.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66
import 'dart:collection';
77
import 'dart:convert';
8+
import 'dart:io';
89

910
import 'package:build/build.dart';
1011
import 'package:convert/convert.dart';
@@ -38,7 +39,11 @@ class AssetGraph {
3839
/// the new [BuildAction]s and throw away the graph if it doesn't.
3940
final Digest buildActionsDigest;
4041

41-
AssetGraph._(this.buildActionsDigest, {Map<int, Set<AssetId>> failedActions})
42+
/// The [Platform.version] this graph was created with.
43+
final String dartVersion;
44+
45+
AssetGraph._(this.buildActionsDigest, this.dartVersion,
46+
{Map<int, Set<AssetId>> failedActions})
4247
: _failedActions = failedActions ?? new Map<int, Set<AssetId>>();
4348

4449
/// Deserializes this graph.
@@ -51,7 +56,8 @@ class AssetGraph {
5156
Set<AssetId> internalSources,
5257
PackageGraph packageGraph,
5358
AssetReader digestReader) async {
54-
var graph = new AssetGraph._(computeBuildActionsDigest(buildActions));
59+
var graph = new AssetGraph._(
60+
computeBuildActionsDigest(buildActions), Platform.version);
5561
var placeholders = graph._addPlaceHolderNodes(packageGraph);
5662
var sourceNodes = graph._addSources(sources);
5763
graph._addBuilderOptionsNodes(buildActions);

build_runner/lib/src/asset_graph/serialization.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class _AssetGraphDeserializer {
2727
}
2828

2929
var graph = new AssetGraph._(
30-
_deserializeDigest(_serializedGraph['buildActionsDigest'] as String));
30+
_deserializeDigest(_serializedGraph['buildActionsDigest'] as String),
31+
_serializedGraph['dart_version'] as String);
3132

3233
var packageNames = _serializedGraph['packages'] as List<String>;
3334

@@ -159,6 +160,7 @@ class _AssetGraphSerializer {
159160

160161
var result = <String, dynamic>{
161162
'version': _version,
163+
'dart_version': _graph.dartVersion,
162164
'nodes': _graph.allNodes.map(_serializeNode).toList(growable: false),
163165
'buildActionsDigest': _serializeDigest(_graph.buildActionsDigest),
164166
'packages': packages,

build_runner/lib/src/generate/build_definition.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ class _Loader {
196196
'Throwing away cached asset graph because the build actions have '
197197
'changed. This most commonly would happen as a result of adding a '
198198
'new dependency or updating your dependencies.');
199-
199+
await _cleanupOldOutputs(cachedGraph);
200+
return null;
201+
}
202+
if (cachedGraph.dartVersion != Platform.version) {
203+
_logger.warning(
204+
'Throwing away cached asset graph due to Dart SDK update.');
200205
await _cleanupOldOutputs(cachedGraph);
201206
return null;
202207
}

build_runner/test/generate/build_definition_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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
import 'dart:async';
5+
import 'dart:convert';
56
import 'dart:io';
67

78
import 'package:build/build.dart';
@@ -340,6 +341,43 @@ main() {
340341
expect(originalAssetGraph.buildActionsDigest,
341342
isNot(newAssetGraph.buildActionsDigest));
342343
});
344+
test('invalidates the graph if the dart sdk version changes', () async {
345+
// Gets rid of console spam during tests, we are setting up a new options
346+
// object.
347+
await options.logListener.cancel();
348+
349+
var buildActions = [
350+
new BuildAction(new TestBuilder(), 'a', hideOutput: true)
351+
];
352+
var logs = <LogRecord>[];
353+
environment = new OverrideableEnvironment(environment, onLog: logs.add);
354+
options = new BuildOptions(environment,
355+
packageGraph: options.packageGraph,
356+
logLevel: Level.WARNING,
357+
skipBuildScriptCheck: true);
358+
359+
var originalAssetGraph = await AssetGraph.build(buildActions,
360+
<AssetId>[].toSet(), new Set(), aPackageGraph, environment.reader);
361+
362+
var bytes = originalAssetGraph.serialize();
363+
var serialized = JSON.decode(UTF8.decode(bytes));
364+
serialized['dart_version'] = 'some_fake_version';
365+
var encoded = UTF8.encode(JSON.encode(serialized));
366+
await createFile(assetGraphPath, encoded);
367+
368+
logs.clear();
369+
370+
await BuildDefinition.prepareWorkspace(
371+
environment, options, buildActions);
372+
expect(
373+
logs.any(
374+
(log) =>
375+
log.level == Level.WARNING &&
376+
log.message.contains(
377+
'Throwing away cached asset graph due to Dart SDK update.'),
378+
),
379+
isTrue);
380+
});
343381

344382
test('does not invalidate the graph if the BuilderOptions change',
345383
() async {

0 commit comments

Comments
 (0)