Skip to content

Track resolver dependencies as library cycle graphs #3996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion _test_common/lib/test_phases.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ Future<void> wait(int milliseconds) =>
Future.delayed(Duration(milliseconds: milliseconds));

void _printOnFailure(LogRecord record) {
printOnFailure('$record');
printOnFailure(
'$record'
'${record.error == null ? '' : ' ${record.error}'}'
'${record.stackTrace == null ? '' : ' ${record.stackTrace}'}',
);
}

/// Runs [builders] in a test environment.
Expand Down
1 change: 1 addition & 0 deletions build/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Refactor `FileBasedAssetReader` and `FileBasedAssetWriter` to `ReaderWriter`.
- Move `BuildStepImpl` to `build_runner_core`, use `SingleStepReader` directly.
- Add `LibraryCycleGraphLoader` for loading transitive deps for analysis.
- Track resolver dependencies as library cycle graphs.

## 2.4.2

Expand Down
1 change: 1 addition & 0 deletions build/lib/src/internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export 'library_cycle_graph/asset_deps_loader.dart';
export 'library_cycle_graph/library_cycle.dart';
export 'library_cycle_graph/library_cycle_graph.dart';
export 'library_cycle_graph/library_cycle_graph_loader.dart';
export 'library_cycle_graph/phased_asset_deps.dart';
export 'library_cycle_graph/phased_reader.dart';
export 'library_cycle_graph/phased_value.dart';
export 'state/asset_finder.dart';
Expand Down
5 changes: 5 additions & 0 deletions build/lib/src/library_cycle_graph/asset_deps.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

import '../../build.dart' hide Builder;

Expand All @@ -17,11 +18,15 @@ part 'asset_deps.g.dart';
/// Missing or not-yet-generated sources can be represented by this class: they
/// have no deps.
abstract class AssetDeps implements Built<AssetDeps, AssetDepsBuilder> {
static Serializer<AssetDeps> get serializer => _$assetDepsSerializer;

static final AssetDeps empty = _$AssetDeps._(deps: BuiltSet());

BuiltSet<AssetId> get deps;

factory AssetDeps(Iterable<AssetId> deps) =>
_$AssetDeps._(deps: BuiltSet.of(deps));
factory AssetDeps.build(void Function(AssetDepsBuilder) updates) =
_$AssetDeps;
AssetDeps._();
}
59 changes: 59 additions & 0 deletions build/lib/src/library_cycle_graph/asset_deps.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions build/lib/src/library_cycle_graph/asset_deps_loader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:analyzer/dart/ast/ast.dart';

import '../asset/id.dart';
import 'asset_deps.dart';
import 'phased_asset_deps.dart';
import 'phased_reader.dart';
import 'phased_value.dart';

Expand All @@ -17,6 +18,8 @@ class AssetDepsLoader {
final PhasedReader _reader;

AssetDepsLoader(this._reader);
factory AssetDepsLoader.fromDeps(PhasedAssetDeps deps) =>
_InMemoryAssetDepsLoader(deps);

/// The phase that this loader is reading build state at.
int get phase => _reader.phase;
Expand Down Expand Up @@ -57,3 +60,32 @@ class AssetDepsLoader {
return result.build();
}
}

// An [AssetDepsLoader] from already-loaded asset deps.
class _InMemoryAssetDepsLoader implements AssetDepsLoader {
final Future<PhasedValue<AssetDeps>> _empty = Future.value(
PhasedValue.fixed(AssetDeps.empty),
);
PhasedAssetDeps phasedAssetDeps;

_InMemoryAssetDepsLoader(this.phasedAssetDeps);

// This is important: it prevents LibraryCycleGraphLoader from trying to load
// data that is not in an incomplete [phasedAssetDeps].
@override
int get phase => phasedAssetDeps.phase;

@override
ExpiringValue<AssetDeps> _parse(AssetId id, ExpiringValue<String> content) =>
throw UnimplementedError();

@override
PhasedReader get _reader => throw UnimplementedError();

@override
Future<PhasedValue<AssetDeps>> load(AssetId id) {
var result = phasedAssetDeps.assetDeps[id];
if (result == null) return _empty;
return Future.value(result);
}
}
3 changes: 3 additions & 0 deletions build/lib/src/library_cycle_graph/library_cycle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ abstract class LibraryCycle
factory LibraryCycle([void Function(LibraryCycleBuilder) updates]) =
_$LibraryCycle;
LibraryCycle._();

factory LibraryCycle.of(Iterable<AssetId> ids) =>
_$LibraryCycle._(ids: ids.toBuiltSet());
}
9 changes: 6 additions & 3 deletions build/lib/src/library_cycle_graph/library_cycle_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

import '../asset/id.dart';
import 'library_cycle.dart';
Expand All @@ -13,6 +14,8 @@ part 'library_cycle_graph.g.dart';
/// A directed acyclic graph of [LibraryCycle]s.
abstract class LibraryCycleGraph
implements Built<LibraryCycleGraph, LibraryCycleGraphBuilder> {
static Serializer<LibraryCycleGraph> get serializer =>
_$libraryCycleGraphSerializer;
LibraryCycle get root;
BuiltList<LibraryCycleGraph> get children;

Expand All @@ -21,7 +24,7 @@ abstract class LibraryCycleGraph
LibraryCycleGraph._();

/// All subgraphs in the graph, including the root.
Iterable<LibraryCycleGraph> get transitiveGraphs {
Iterable<LibraryCycleGraph> transitiveGraphs() {
final result = Set<LibraryCycleGraph>.identity();
final nextGraphs = [this];

Expand All @@ -40,8 +43,8 @@ abstract class LibraryCycleGraph
// graph rather than being expanded into an explicit set of nodes. So, remove
// uses of this. If in the end it's still needed, investigate if it needs to
// be optimized.
Iterable<AssetId> get transitiveDeps sync* {
for (final graph in transitiveGraphs) {
Iterable<AssetId> transitiveDeps() sync* {
for (final graph in transitiveGraphs()) {
yield* graph.root.ids;
}
}
Expand Down
75 changes: 75 additions & 0 deletions build/lib/src/library_cycle_graph/library_cycle_graph.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'asset_deps.dart';
import 'asset_deps_loader.dart';
import 'library_cycle.dart';
import 'library_cycle_graph.dart';
import 'phased_asset_deps.dart';
import 'phased_value.dart';

/// Loads [LibraryCycleGraph]s during a phased build.
Expand Down Expand Up @@ -482,9 +483,14 @@ class LibraryCycleGraphLoader {
AssetId id,
) async {
final graph = await libraryCycleGraphOf(assetDepsLoader, id);
return graph.valueAt(phase: assetDepsLoader.phase).transitiveDeps;
return graph.valueAt(phase: assetDepsLoader.phase).transitiveDeps();
}

/// Serializable data from which the library cycle graphs can be
/// reconstructed.
PhasedAssetDeps phasedAssetDeps() =>
PhasedAssetDeps((b) => b.assetDeps.addAll(_assetDeps));

@override
String toString() => '''
LibraryCycleGraphLoader(
Expand Down
60 changes: 60 additions & 0 deletions build/lib/src/library_cycle_graph/phased_asset_deps.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:math';

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:built_value/serializer.dart';

import '../asset/id.dart';
import 'asset_deps.dart';
import 'phased_value.dart';

part 'phased_asset_deps.g.dart';

/// Serializable data from which library cycle graphs can be reconstructed.
///
/// Pass to `AssetDepsLoader.fromDeps` then use that to create a
/// `LibraryCycleGraphLoader`.
abstract class PhasedAssetDeps
implements Built<PhasedAssetDeps, PhasedAssetDepsBuilder> {
static Serializer<PhasedAssetDeps> get serializer =>
_$phasedAssetDepsSerializer;

BuiltMap<AssetId, PhasedValue<AssetDeps>> get assetDeps;

factory PhasedAssetDeps([void Function(PhasedAssetDepsBuilder) b]) =
_$PhasedAssetDeps;
PhasedAssetDeps._();

factory PhasedAssetDeps.of(Map<AssetId, PhasedValue<AssetDeps>> assetDeps) =>
_$PhasedAssetDeps._(assetDeps: assetDeps.build());

/// Returns this data with [other] added to it.
PhasedAssetDeps addAll(PhasedAssetDeps other) {
final result = toBuilder();
for (final entry in other.assetDeps.entries) {
result.assetDeps[entry.key] = entry.value;
}
return result.build();
}

/// The max phase before there is any incomplete data, or 0xffffffff if there
/// is no incomplete data.
@memoized
int get phase {
int? result;
for (final entry in assetDeps.values) {
if (!entry.isComplete) {
if (result == null) {
result = entry.expiresAfter;
} else {
result = min(result, entry.expiresAfter!);
}
}
}
return result ?? 0xffffffff;
}
}
Loading
Loading