Skip to content

Commit 3a7d31e

Browse files
committed
Download archive in pub_worker before the analysis begins (and work on the downloaded directory).
1 parent dd9f175 commit 3a7d31e

File tree

2 files changed

+32
-90
lines changed

2 files changed

+32
-90
lines changed

pkg/pub_worker/lib/src/bin/pana_wrapper.dart

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import 'package:pana/pana.dart';
1515
import 'package:path/path.dart' as p;
1616
import 'package:pub_semver/pub_semver.dart';
1717
import 'package:pub_worker/src/bin/dartdoc_wrapper.dart';
18-
import 'package:pub_worker/src/fetch_pubspec.dart';
1918
import 'package:pub_worker/src/sdks.dart';
2019
import 'package:pub_worker/src/utils.dart';
2120
import 'package:pubspec_parse/pubspec_parse.dart' as pubspek;
@@ -54,8 +53,11 @@ Future<void> main(List<String> args) async {
5453
final pubHostedUrl =
5554
Platform.environment['PUB_HOSTED_URL'] ?? 'https://pub.dartlang.org';
5655
final pubCache = Platform.environment['PUB_CACHE']!;
57-
final rawDartdocOutputFolder =
58-
await Directory.systemTemp.createTemp('dartdoc-$package');
56+
final tempDir = await Directory.systemTemp.createTemp('pana-$package');
57+
final rawDartdocOutputFolder = Directory(p.join(tempDir.path, 'raw-dartdoc'));
58+
await rawDartdocOutputFolder.create(recursive: true);
59+
final pkgDownloadDir = Directory(p.join(tempDir.path, package));
60+
await pkgDownloadDir.create(recursive: true);
5961

6062
// Setup logging
6163
Logger.root.level = Level.INFO;
@@ -78,16 +80,24 @@ Future<void> main(List<String> args) async {
7880
}
7981
});
8082

81-
// Fetch the pubspec so we detect which SDK to use for analysis
82-
// TODO(https://github.com/dart-lang/pub-dev/issues/7268): Download the archive,
83-
// extract and load the pubspec.yaml, that way we won't have to list versions.
84-
final pubspec = await fetchPubspec(
85-
package: package,
86-
version: version,
87-
pubHostedUrl: pubHostedUrl,
83+
// Download package using the Dart SDK in the path, output will be
84+
// `<output-dir>/<package>-<version>`
85+
await runConstrained(
86+
[
87+
'dart',
88+
'pub',
89+
'unpack',
90+
'$package:$version',
91+
'--output',
92+
pkgDownloadDir.path,
93+
'--no-resolve',
94+
],
95+
environment: {
96+
'PUB_HOSTED_URL': pubHostedUrl,
97+
},
8898
);
89-
90-
final detected = await _detectSdks(pubspec);
99+
final pkgDir = Directory(p.join(pkgDownloadDir.path, '$package-$version'));
100+
final detected = await _detectSdks(pkgDir.path);
91101

92102
final toolEnv = await ToolEnvironment.create(
93103
dartSdkConfig: SdkConfig(
@@ -107,11 +117,8 @@ Future<void> main(List<String> args) async {
107117
final resourcesOutputDir =
108118
await Directory(p.join(outputFolder, 'resources')).create();
109119
final pana = PackageAnalyzer(toolEnv);
110-
// TODO: add a cache purge + retry if the download would fail
111-
// (e.g. the package version cache wasn't invalidated).
112-
var summary = await pana.inspectPackage(
113-
package,
114-
version: version,
120+
var summary = await pana.inspectDir(
121+
pkgDir.path,
115122
options: InspectOptions(
116123
pubHostedUrl: Platform.environment['PUB_HOSTED_URL']!,
117124
dartdocTimeout: _dartdocTimeout,
@@ -168,7 +175,7 @@ Future<void> main(List<String> args) async {
168175
docDir: rawDartdocOutputFolder.path,
169176
);
170177

171-
await rawDartdocOutputFolder.delete(recursive: true);
178+
await tempDir.delete(recursive: true);
172179
}
173180

174181
final _workerConfigDirectory = Directory('/home/worker/config');
@@ -185,7 +192,11 @@ String? _configHomePath(String sdk, String kind) {
185192
}
186193

187194
Future<({String configKind, String? dartSdkPath, String? flutterSdkPath})>
188-
_detectSdks(Pubspec pubspec) async {
195+
_detectSdks(String pkgDir) async {
196+
// Load the pubspec so we detect which SDK to use for analysis
197+
final pubspecFile = File(p.join(pkgDir, 'pubspec.yaml'));
198+
final pubspec = Pubspec.parseYaml(await pubspecFile.readAsString());
199+
189200
// Discover installed Dart and Flutter SDKs.
190201
// This reads sibling folders to the Dart and Flutter SDK.
191202
final dartSdks = await InstalledSdk.scanDirectory(
@@ -266,6 +277,8 @@ Future<({String configKind, String? dartSdkPath, String? flutterSdkPath})>
266277
allowsMissingVersion: true,
267278
);
268279
if (matchesInstalledSdks) {
280+
// TODO(https://github.com/dart-lang/pub-dev/issues/7268): Also use `pub get` for better SDK selection.
281+
269282
return (
270283
configKind: 'stable',
271284
dartSdkPath: installedDartSdk?.path,

pkg/pub_worker/lib/src/fetch_pubspec.dart

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)