-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathreport.dart
More file actions
582 lines (521 loc) · 18.2 KB
/
report.dart
File metadata and controls
582 lines (521 loc) · 18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// Copyright (c) 2013, 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 'package:collection/collection.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import '../command_runner.dart';
import '../lock_file.dart';
import '../log.dart' as log;
import '../package_name.dart';
import '../pubspec.dart';
import '../source/hosted.dart';
import '../source/root.dart';
import '../system_cache.dart';
import '../utils.dart';
import 'result.dart';
import 'type.dart';
/// Unlike [SolveResult], which is the static data describing a resolution,
/// this class contains the mutable state used while generating the report
/// itself.
///
/// It's a report builder.
class SolveReport {
final SolveType _type;
// The report will contain "in [_location]" if given.
final String? _location;
final Pubspec _rootPubspec;
final LockFile _previousLockFile;
final LockFile _newLockFile;
final SystemCache _cache;
final bool _dryRun;
final Map<String, PackageRange> _overriddenPackages;
/// If quiet only a single summary line is output.
final bool _quiet;
final bool _enforceLockfile;
/// The available versions of all selected packages from their source.
///
/// An entry here may not include the full list of versions available if the
/// given package was locked and did not need to be unlocked during the solve.
///
/// Version list will not contain any retracted package versions.
final Map<String, List<Version>> _availableVersions;
static const maxAdvisoryFootnotesPerLine = 5;
final advisoryDisplayHandles = <String>[];
SolveReport(
this._type,
this._location,
this._rootPubspec,
this._overriddenPackages,
this._previousLockFile,
this._newLockFile,
this._availableVersions,
this._cache, {
required bool dryRun,
required bool enforceLockfile,
required bool quiet,
}) : _dryRun = dryRun,
_quiet = quiet,
_enforceLockfile = enforceLockfile;
/// Displays a report of the results of the version resolution in
/// [_newLockFile] relative to the [_previousLockFile] file.
///
/// If [summary] is `true` a count of changes and number of
/// discontinued/retracted packages will be shown at the end of the report.
Future<void> show({required bool summary}) async {
final changes = await _reportChanges();
_checkContentHashesMatchOldLockfile();
if (summary) await summarize(changes);
}
void _checkContentHashesMatchOldLockfile() {
final issues = <String>[];
final newPackageNames = _newLockFile.packages.keys.toSet();
final oldPackageNames = _previousLockFile.packages.keys.toSet();
// We only care about packages that exist in both new and old lockfile.
for (final name in newPackageNames.intersection(oldPackageNames)) {
final newId = _newLockFile.packages[name]!;
final oldId = _previousLockFile.packages[name]!;
// We only care about hosted packages
final newDescription = newId.description;
final oldDescription = oldId.description;
if (newDescription is! ResolvedHostedDescription ||
oldDescription is! ResolvedHostedDescription) {
continue;
}
// We don't care about changes in the hash if the version number changed!
if (newId.version != oldId.version) {
continue;
}
// Use the cached content-hashes after downloading to ensure that
// content-hashes from legacy servers gets used.
final cachedHash = newDescription.sha256;
assert(cachedHash != null);
// Ignore cases where the old lockfile doesn't have a content-hash
final oldHash = oldDescription.sha256;
if (oldHash == null) {
continue;
}
if (!fixedTimeBytesEquals(cachedHash, oldHash)) {
issues.add(
'$name-${newId.version} from "${newDescription.description.url}"',
);
}
}
if (issues.isNotEmpty) {
warning('''
The existing content-hash from pubspec.lock doesn't match contents for:
* ${issues.join('\n * ')}
This indicates one of:
* The content has changed on the server since you created the pubspec.lock.
* The pubspec.lock has been corrupted.
${_dryRun || _enforceLockfile ? '' : '\nThe content-hashes in pubspec.lock has been updated.'}
For more information see:
$contentHashesDocumentationUrl
''');
}
}
/// Displays a one-line message summarizing what changes were made (or would
/// be made) to the lockfile.
///
/// If [_dryRun] or [_enforceLockfile] is true, describes it in terms of what
/// would be done.
///
/// [_type] is the type of version resolution that was run.
/// If [_type] is `SolveType.UPGRADE` it also shows the number of packages
/// that are not at the latest available version and the number of outdated
/// packages.
Future<void> summarize(int changes) async {
// Count how many dependencies actually changed.
final dependencies = _newLockFile.packages.keys.toSet();
dependencies.addAll(_previousLockFile.packages.keys);
dependencies.remove(_rootPubspec.name);
var suffix = '';
final dir = _location;
if (dir != null) {
if (dir != '.') {
suffix = ' in `$dir`';
}
}
if (_quiet) {
if (_dryRun) {
log.message('Would get dependencies$suffix.');
} else if (_enforceLockfile) {
if (changes == 0) {
log.message('Got dependencies$suffix.');
}
} else {
log.message('Got dependencies$suffix.');
}
} else {
if (_dryRun) {
if (changes == 0) {
log.message('No dependencies would change$suffix.');
} else if (changes == 1) {
log.message('Would change $changes dependency$suffix.');
} else {
log.message('Would change $changes dependencies$suffix.');
}
} else if (_enforceLockfile) {
if (changes == 0) {
log.message('Got dependencies$suffix!');
} else if (changes == 1) {
log.message('Would change $changes dependency$suffix.');
} else {
log.message('Would change $changes dependencies$suffix.');
}
} else {
if (changes == 0) {
if (_type == SolveType.get) {
log.message('Got dependencies$suffix!');
} else {
log.message('No dependencies changed$suffix.');
}
} else if (changes == 1) {
log.message('Changed $changes dependency$suffix!');
} else {
log.message('Changed $changes dependencies$suffix!');
}
}
await reportDiscontinued();
reportAdvisories();
reportOutdated();
}
}
/// Displays a report of all of the previous and current dependencies and
/// how they have changed.
///
/// Returns the number of changes.
Future<int> _reportChanges() async {
final output = StringBuffer();
// Show the new set of dependencies ordered by name.
final names = _newLockFile.packages.keys.toList();
names.remove(_rootPubspec.name);
names.sort();
var changes = 0;
for (final name in names) {
changes += await _reportPackage(name, output) ? 1 : 0;
}
// Show any removed ones.
final removed = _previousLockFile.packages.keys.toSet();
removed.removeAll(names);
removed.remove(_rootPubspec.name); // Never consider root.
if (removed.isNotEmpty) {
output.writeln('These packages are no longer being depended on:');
for (var name in removed.sorted()) {
await _reportPackage(name, output, alwaysShow: true);
changes += 1;
}
}
message(output.toString());
return changes;
}
/// Displays a single-line message, number of discontinued packages
/// if discontinued packages are detected.
Future<void> reportDiscontinued() async {
var numDiscontinued = 0;
for (var id in _newLockFile.packages.values) {
if (id.description is RootDescription) continue;
final status = await id.source.status(
id.toRef(),
id.version,
_cache,
maxAge: const Duration(days: 3),
);
if (status.isDiscontinued &&
(_rootPubspec.dependencyType(id.name) == DependencyType.direct ||
_rootPubspec.dependencyType(id.name) == DependencyType.dev)) {
numDiscontinued++;
}
}
if (numDiscontinued > 0) {
if (numDiscontinued == 1) {
message('1 package is discontinued.');
} else {
message('$numDiscontinued packages are discontinued.');
}
}
}
/// Displays a two-line message, number of outdated packages and an
/// instruction to run `pub outdated` if outdated packages are detected.
void reportOutdated() {
final outdatedPackagesCount =
_newLockFile.packages.values.where((id) {
final versions = _availableVersions[id.name]!;
// A version is counted:
// - if there is a newer version which is not a pre-release and
// current version is also not a pre-release or,
// - if the current version is pre-release then any upgraded version
// is considered.
return versions.any(
(v) =>
v > id.version && (id.version.isPreRelease || !v.isPreRelease),
);
}).length;
if (outdatedPackagesCount > 0) {
String packageCountString;
if (outdatedPackagesCount == 1) {
packageCountString = '1 package has';
} else {
packageCountString = '$outdatedPackagesCount packages have';
}
message(
'$packageCountString newer versions incompatible with '
'dependency constraints.\n'
'Try `$topLevelProgram pub outdated` for more information.',
);
}
}
void reportAdvisories() {
if (advisoryDisplayHandles.isNotEmpty) {
message('Dependencies are affected by security advisories:');
for (
var footnote = 0;
footnote < advisoryDisplayHandles.length;
footnote++
) {
message(' [^$footnote]: ${advisoryDisplayHandles[footnote]}');
}
}
}
static DependencyType dependencyType(LockFile lockFile, String name) =>
lockFile.mainDependencies.contains(name)
? DependencyType.direct
: lockFile.devDependencies.contains(name)
? DependencyType.dev
: DependencyType.none;
String? _constructAdvisoriesMessage(
List<int> footnotes,
bool advisoriesTruncated,
) {
if (footnotes.isNotEmpty) {
final advisoryString = footnotes.length == 1 ? 'advisory' : 'advisories';
final buffer = StringBuffer('affected by $advisoryString: ');
buffer.write('[^${footnotes.first}]');
for (final footnote in footnotes.getRange(1, footnotes.length)) {
buffer.write(', [^$footnote]');
}
if (advisoriesTruncated) {
buffer.write(', ...');
}
return buffer.toString();
}
return null;
}
/// Reports the results of the upgrade on the package named [name].
///
/// If [alwaysShow] is true, the package is reported even if it didn't change,
/// regardless of [_type].
///
/// Returns true if the package had changed.
Future<bool> _reportPackage(
String name,
StringBuffer output, {
bool alwaysShow = false,
}) async {
final newId = _newLockFile.packages[name];
final oldId = _previousLockFile.packages[name];
final id = newId ?? oldId!;
final isOverridden = _overriddenPackages.containsKey(id.name);
// If the package was previously a dependency but the dependency has
// changed in some way.
var changed = false;
// If the dependency was added or removed.
var addedOrRemoved = false;
// Show a one-character "icon" describing the change. They are:
//
// ! The package is being overridden.
// - The package was removed.
// + The package was added.
// > The package was upgraded from a lower version.
// < The package was downgraded from a higher version.
// ~ Package contents has changed, but not the version number.
// * Any other change between the old and new package.
String icon;
if (isOverridden) {
icon = log.magenta('! ');
} else if (newId == null) {
icon = log.red('- ');
addedOrRemoved = true;
} else if (oldId == null) {
icon = log.green('+ ');
addedOrRemoved = true;
} else if (oldId.description.description != newId.description.description) {
// Eg. a changed source in pubspec.yaml.
icon = log.cyan('* ');
changed = true;
} else if (oldId.version < newId.version) {
icon = log.green('> ');
changed = true;
} else if (oldId.version > newId.version) {
icon = log.cyan('< ');
changed = true;
} else if (oldId.description != newId.description) {
// Eg. a changed hash or revision.
icon = log.cyan('~ ');
changed = true;
} else {
// Unchanged.
icon = ' ';
}
String? message;
// See if there are any newer versions of the package that we were
// unable to upgrade to.
if (newId != null && _type != SolveType.downgrade) {
final versions = _availableVersions[newId.name]!;
var newerStable = false;
var newerUnstable = false;
for (var version in versions) {
if (version > newId.version) {
if (version.isPreRelease) {
newerUnstable = true;
} else {
newerStable = true;
}
}
}
final status = await id.source.status(
id.toRef(),
id.version,
_cache,
maxAge: const Duration(days: 3),
);
final notes = <String>[];
final advisories = await id.source.getAdvisoriesForPackageVersion(
id,
_cache,
const Duration(days: 3),
);
if (advisories != null && advisories.isNotEmpty) {
final advisoryFootnotes = <int>[];
final reportedAdvisories = advisories
.where(
(adv) =>
_rootPubspec.ignoredAdvisories.intersection({
...adv.aliases,
adv.id,
}).isEmpty,
)
.take(maxAdvisoryFootnotesPerLine);
for (final adv in reportedAdvisories) {
advisoryFootnotes.add(advisoryDisplayHandles.length);
advisoryDisplayHandles.add(adv.displayHandle);
}
final advisoriesMessage = _constructAdvisoriesMessage(
advisoryFootnotes,
advisories.length > maxAdvisoryFootnotesPerLine,
);
if (advisoriesMessage != null) {
notes.add(advisoriesMessage);
}
}
if (status.isRetracted) {
if (newerStable) {
notes.add(
'retracted, ${maxAll(versions, Version.prioritize)} available',
);
} else if (newId.version.isPreRelease && newerUnstable) {
notes.add('retracted, ${maxAll(versions)} available');
} else {
notes.add('retracted');
}
} else if (status.isDiscontinued &&
[
DependencyType.direct,
DependencyType.dev,
].contains(_rootPubspec.dependencyType(name))) {
if (status.discontinuedReplacedBy == null) {
notes.add('discontinued');
} else {
notes.add(
'discontinued replaced by ${status.discontinuedReplacedBy}',
);
}
} else if (newerStable) {
// If there are newer stable versions, only show those.
notes.add('${maxAll(versions, Version.prioritize)} available');
} else if (
// Only show newer prereleases for versions where a prerelease is
// already chosen.
newId.version.isPreRelease && newerUnstable) {
notes.add('${maxAll(versions)} available');
}
message = notes.isEmpty ? null : '(${notes.join(', ')})';
}
final oldDependencyType = dependencyType(_previousLockFile, name);
final newDependencyType = dependencyType(_newLockFile, name);
final dependencyTypeChanged =
oldId != null &&
newId != null &&
oldDependencyType != newDependencyType;
if (!(alwaysShow ||
changed ||
addedOrRemoved ||
dependencyTypeChanged ||
message != null ||
isOverridden)) {
return changed || addedOrRemoved || dependencyTypeChanged;
}
output.write(icon);
output.write(log.bold(id.name));
output.write(' ');
_writeId(id, output);
// If the package was upgraded, show what it was upgraded from.
if (changed) {
output.write(' (was ');
_writeId(oldId!, output);
output.write(')');
}
if (dependencyTypeChanged) {
if (dependencyTypeChanged) {
output.write(' (from ');
_writeDependencyType(oldDependencyType, output);
output.write(' dependency to ');
_writeDependencyType(newDependencyType, output);
output.write(' dependency)');
}
}
// Highlight overridden packages.
if (isOverridden) {
final location = _location;
final overrideLocation =
location != null && _rootPubspec.dependencyOverridesFromOverridesFile
? ' in ${p.join(location, Pubspec.pubspecOverridesFilename)}'
: '';
output.write(' ${log.magenta('(overridden$overrideLocation)')}');
}
if (message != null) output.write(' ${log.cyan(message)}');
output.writeln();
return changed || addedOrRemoved || dependencyTypeChanged;
}
/// Writes a terse description of [id] (not including its name) to the output.
void _writeId(PackageId id, StringBuffer output) {
output.write(id.version);
if (id.source != _cache.defaultSource) {
final description = id.description.format();
output.write(' from ${id.source} $description');
}
}
void _writeDependencyType(DependencyType t, StringBuffer output) {
output.write(
log.bold(switch (t) {
DependencyType.direct => 'direct',
DependencyType.dev => 'dev',
DependencyType.none => 'transitive',
}),
);
}
void warning(String message) {
if (_quiet) {
log.fine(message);
} else {
log.warning(message);
}
}
void message(String message) {
if (_quiet) {
log.fine(message);
} else {
log.message(message);
}
}
}