Skip to content

Commit 276cf49

Browse files
committed
Try fine grained dependencies.
1 parent 1c3d472 commit 276cf49

File tree

6 files changed

+164
-7
lines changed

6 files changed

+164
-7
lines changed

build_runner/lib/src/build/resolver/analysis_driver.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
import 'dart:io';
66

77
import 'package:analyzer/file_system/file_system.dart' show ResourceProvider;
8-
// ignore: implementation_imports
9-
import 'package:analyzer/src/clients/build_resolvers/build_resolvers.dart';
108
import 'package:package_config/package_config.dart' show PackageConfig;
119
import 'package:path/path.dart' as p;
1210
import 'package:pub_semver/pub_semver.dart';
1311

1412
import 'analysis_driver_filesystem.dart';
1513
import 'analysis_driver_model.dart';
14+
import 'build_resolvers.dart';
1615

1716
/// Builds an [AnalysisDriverForPackageBuild] backed by a summary SDK.
1817
///

build_runner/lib/src/build/resolver/analysis_driver_filesystem.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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:io' as io;
6+
57
import 'dart:convert';
68
import 'dart:typed_data';
79

@@ -28,7 +30,12 @@ class AnalysisDriverFilesystem implements UriResolver, ResourceProvider {
2830
/// Reads the data previously written to [path].
2931
///
3032
/// Throws if ![exists].
31-
String read(String path) => _data[path]!;
33+
String read(String path) {
34+
if (path.contains('/sdk/')) {
35+
return io.File(path).readAsStringSync();
36+
}
37+
return _data[path]!;
38+
}
3239

3340
/// Deletes the data previously written to [path].
3441
///
@@ -171,6 +178,9 @@ class _Resource implements File, Folder {
171178
@override
172179
String get shortName => filesystem.pathContext.basename(path);
173180

181+
@override
182+
Folder get parent => _Resource(filesystem, p.dirname(path));
183+
174184
// `File` methods.
175185
@override
176186
Uint8List readAsBytesSync() {
@@ -198,6 +208,14 @@ class _Resource implements File, Folder {
198208
bool contains(String path) =>
199209
filesystem.pathContext.isWithin(this.path, path);
200210

211+
@override
212+
File getChildAssumingFile(String relPath) =>
213+
_Resource(filesystem, '$path/$relPath');
214+
215+
@override
216+
Folder getChildAssumingFolder(String relPath) =>
217+
_Resource(filesystem, '$path/$relPath');
218+
201219
// Most `File` and/or `Folder` methods are not needed.
202220

203221
@override

build_runner/lib/src/build/resolver/analysis_driver_model.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import 'dart:async';
66

7-
// ignore: implementation_imports
8-
import 'package:analyzer/src/clients/build_resolvers/build_resolvers.dart';
97
import 'package:build/build.dart';
108

119
import '../../logging/timed_activities.dart';
@@ -15,6 +13,8 @@ import '../library_cycle_graph/library_cycle_graph_loader.dart';
1513
import '../library_cycle_graph/phased_asset_deps.dart';
1614
import 'analysis_driver_filesystem.dart';
1715

16+
import 'build_resolvers.dart';
17+
1818
/// Manages analysis driver and related build state.
1919
///
2020
/// - Tracks the import graph of all sources needed for analysis.
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// ignore_for_file: implementation_imports
6+
7+
import 'dart:io';
8+
import 'dart:typed_data';
9+
10+
import 'package:analyzer/dart/analysis/analysis_context_collection.dart';
11+
import 'package:analyzer/dart/analysis/analysis_options.dart';
12+
import 'package:analyzer/dart/analysis/session.dart';
13+
import 'package:analyzer/file_system/file_system.dart';
14+
import 'package:analyzer/src/context/packages.dart';
15+
import 'package:analyzer/src/dart/analysis/analysis_options.dart';
16+
import 'package:analyzer/src/dart/analysis/analysis_options_map.dart';
17+
import 'package:analyzer/src/dart/analysis/byte_store.dart';
18+
import 'package:analyzer/src/dart/analysis/driver.dart';
19+
import 'package:analyzer/src/dart/analysis/file_content_cache.dart';
20+
import 'package:analyzer/src/dart/analysis/performance_logger.dart';
21+
import 'package:analyzer/src/dart/sdk/sdk.dart';
22+
import 'package:analyzer/src/generated/source.dart';
23+
import 'package:analyzer/src/summary/summary_sdk.dart';
24+
import 'package:analyzer/src/summary2/package_bundle_format.dart';
25+
import 'package:path/path.dart' as p;
26+
27+
export 'package:analyzer/dart/analysis/analysis_options.dart'
28+
show AnalysisOptions;
29+
export 'package:analyzer/source/source.dart' show Source;
30+
export 'package:analyzer/src/context/packages.dart' show Package, Packages;
31+
export 'package:analyzer/src/dart/analysis/analysis_options.dart'
32+
show AnalysisOptionsImpl;
33+
export 'package:analyzer/src/dart/analysis/byte_store.dart' show ByteStore;
34+
export 'package:analyzer/src/dart/analysis/experiments.dart'
35+
show ExperimentStatus;
36+
export 'package:analyzer/src/generated/source.dart' show UriResolver;
37+
38+
/// A somewhat low level API to create [AnalysisSession].
39+
///
40+
/// Ideally we want clients to use [AnalysisContextCollection], which
41+
/// encapsulates any internals and is driven by `package_config.json` and
42+
/// `analysis_options.yaml` files. But so far it looks that `build_resolvers`
43+
/// wants to provide [UriResolver], and push [Packages] created by other means
44+
/// than parsing `package_config.json`.
45+
AnalysisDriverForPackageBuild createAnalysisDriver({
46+
required ResourceProvider resourceProvider,
47+
required Uint8List sdkSummaryBytes,
48+
required AnalysisOptions analysisOptions,
49+
FileContentCache? fileContentCache,
50+
required List<UriResolver> uriResolvers,
51+
required Packages packages,
52+
ByteStore? byteStore,
53+
}) {
54+
final sdkBundle = PackageBundleReader(sdkSummaryBytes);
55+
final bundleSdk = SummaryBasedDartSdk.forBundle(sdkBundle);
56+
57+
final runningDarkSdkPath = p.dirname(p.dirname(Platform.resolvedExecutable));
58+
final sdk = FolderBasedDartSdk(
59+
resourceProvider,
60+
resourceProvider.getFolder(runningDarkSdkPath),
61+
);
62+
63+
//final sourceFactory = SourceFactory([DartUriResolver(sdk), ...uriResolvers]);
64+
final sourceFactory = SourceFactory([DartUriResolver(sdk), ...uriResolvers]);
65+
66+
//final dataStore = SummaryDataStore();
67+
//dataStore.addBundle('', sdkBundle);
68+
69+
final logger = PerformanceLog(null);
70+
byteStore ??= MemoryByteStore();
71+
72+
final scheduler = AnalysisDriverScheduler(logger);
73+
scheduler.events.drain<void>().ignore();
74+
75+
final sharedOptions = analysisOptions as AnalysisOptionsImpl;
76+
final optionsMap = AnalysisOptionsMap.forSharedOptions(sharedOptions);
77+
final driver = AnalysisDriver(
78+
scheduler: scheduler,
79+
logger: logger,
80+
resourceProvider: resourceProvider,
81+
byteStore: byteStore,
82+
sourceFactory: sourceFactory,
83+
analysisOptionsMap: optionsMap,
84+
fileContentCache: fileContentCache,
85+
// externalSummaries: dataStore,
86+
packages: packages,
87+
withFineDependencies: true,
88+
shouldReportInconsistentAnalysisException: false,
89+
);
90+
91+
scheduler.start();
92+
93+
return AnalysisDriverForPackageBuild._(bundleSdk.libraryUris, driver);
94+
}
95+
96+
/// [AnalysisSession] plus a tiny bit more.
97+
class AnalysisDriverForPackageBuild {
98+
final List<Uri> _sdkLibraryUris;
99+
final AnalysisDriver _driver;
100+
101+
AnalysisDriverForPackageBuild._(this._sdkLibraryUris, this._driver);
102+
103+
AnalysisSession get currentSession {
104+
return _driver.currentSession;
105+
}
106+
107+
/// Returns URIs of libraries in the given SDK.
108+
List<Uri> get sdkLibraryUris {
109+
return _sdkLibraryUris;
110+
}
111+
112+
/// Return a [Future] that completes after pending file changes are applied,
113+
/// so that [currentSession] can be used to compute results.
114+
Future<void> applyPendingFileChanges() {
115+
return _driver.applyPendingFileChanges();
116+
}
117+
118+
/// The file with the given [path] might have changed - updated, added or
119+
/// removed. Or not, we don't know. Or it might have, but then changed back.
120+
///
121+
/// The [path] must be absolute and normalized.
122+
///
123+
/// The [currentSession] most probably will be invalidated.
124+
/// Note, is does NOT at the time of writing this comment.
125+
/// But we are going to fix this.
126+
void changeFile(String path) {
127+
_driver.changeFile(path);
128+
}
129+
130+
/// Return `true` if the [uri] can be resolved to an existing file.
131+
bool isUriOfExistingFile(Uri uri) {
132+
final source = _driver.sourceFactory.forUri2(uri);
133+
return source != null && source.exists();
134+
}
135+
}

build_runner/lib/src/build/resolver/resolver.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import 'package:analyzer/dart/ast/ast.dart';
1212
import 'package:analyzer/dart/element/element.dart';
1313
import 'package:analyzer/diagnostic/diagnostic.dart';
1414
import 'package:analyzer/error/error.dart';
15-
// ignore: implementation_imports
16-
import 'package:analyzer/src/clients/build_resolvers/build_resolvers.dart';
1715
import 'package:async/async.dart';
1816
import 'package:build/build.dart';
1917
import 'package:build/experiments.dart';
@@ -27,6 +25,7 @@ import '../../logging/timed_activities.dart';
2725
import 'analysis_driver.dart';
2826
import 'analysis_driver_filesystem.dart';
2927
import 'analysis_driver_model.dart';
28+
import 'build_resolvers.dart';
3029
import 'sdk_summary.dart';
3130
import 'shared_resource_pool.dart';
3231

build_runner/pubspec.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,11 @@ dev_dependencies:
5252
test: ^1.25.5
5353
test_descriptor: ^2.0.0
5454

55+
dependency_overrides:
56+
analyzer:
57+
path: ../../dart-sdk/sdk/pkg/analyzer
58+
_fe_analyzer_shared:
59+
path: ../../dart-sdk/sdk/pkg/_fe_analyzer_shared
60+
5561
topics:
5662
- build-runner

0 commit comments

Comments
 (0)