Skip to content

Commit

Permalink
Analyze and report 'pub downgrade' issues. (#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored May 24, 2024
1 parent bdf59b4 commit e5d79d8
Show file tree
Hide file tree
Showing 37 changed files with 381 additions and 133 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.22.4

- Report and score `pub downgrade` + `dart analyze`.

## 0.22.3

- Do not emit package names in `allDependencies` with trivial syntax issues.
Expand Down
42 changes: 42 additions & 0 deletions lib/src/package_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,48 @@ class PackageContext {
return errorMessage;
}();

/// Runs `pub downgrade` and then static analysis.
/// Returns `null` when no issues found or a String description of the issues.
late final downgradeAnalysisErrorMessage = () async {
try {
log.info('Analyzing pub downgrade...');
final tool = usesFlutter ? 'flutter' : 'dart';
final pr = await toolEnvironment.runPub(
packageDir,
usesFlutter: usesFlutter,
command: 'downgrade',
);
if (pr.exitCode != 0) {
return '`$tool pub downgrade` failed with:\n\n```\n${pr.asTrimmedOutput}\n```\n';
}

final problems = await _staticAnalysis(packageDir: packageDir);
final errors = problems.where((e) => e.isError).toList();
if (errors.isEmpty) {
// success returning `null`
return null;
} else {
final issueLines = errors
.take(3)
.map((cp) =>
' - `${cp.errorCode}` - `${cp.file}:${cp.line}:${cp.col}` - ${cp.description}\n')
.join();
final issueLabel = errors.length == 1 ? 'error' : 'errors';
return 'downgrade analysis failed failed with ${errors.length} $issueLabel:\n\n$issueLines';
}
} on ToolException catch (e) {
return 'downgrade analysis failed with:\n${e.message}';
} finally {
try {
await toolEnvironment.runPub(packageDir,
usesFlutter: usesFlutter, command: 'upgrade');
} on ToolException catch (e, st) {
errors.add('`dart upgrade` failed');
log.warning('dart upgrade failed', e, st);
}
}
}();

Future<List<CodeProblem>> staticAnalysis() async {
if (_codeProblems != null) return _codeProblems!;
try {
Expand Down
30 changes: 28 additions & 2 deletions lib/src/report/dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,39 @@ Future<ReportSection> trustworthyDependency(PackageContext context) async {
);
}

Future<Subsection> downgrade() async {
final message = await context.downgradeAnalysisErrorMessage;
final isPassed = message == null;
final tool = context.usesFlutter ? 'flutter' : 'dart';
final issues = isPassed
? [
RawParagraph(
'`pub downgrade` does not expose any static analysis error.'),
]
: [
Issue(message),
RawParagraph(
'Run `$tool pub downgrade` and then `$tool analyze` to reproduce the above problem.\n\n'
'You may run `dart pub upgrade --tighten` to update your dependency constraints, '
'or visit http://dart.dev/go/downgrade-testing for further help.'),
];
return Subsection(
'Compatible with dependency constraint lower bounds',
issues,
isPassed ? 20 : 0,
20,
isPassed ? ReportStatus.passed : ReportStatus.failed,
);
}

final dependencySection = await dependencies();
final sdkSection = await sdkSupport();
final subsections = [dependencySection, sdkSection];
final downgradeSection = await downgrade();
final subsections = [dependencySection, sdkSection, downgradeSection];
return makeSection(
id: ReportSectionId.dependency,
title: 'Support up-to-date dependencies',
maxPoints: 20,
maxPoints: subsections.map((e) => e.maxPoints).fold(0, (a, b) => a + b),
subsections: subsections,
basePath: packageDir,
);
Expand Down
69 changes: 25 additions & 44 deletions lib/src/sdk_env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'dart:io';
import 'package:cli_util/cli_util.dart' as cli;
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'package:retry/retry.dart';

import 'analysis_options.dart';
import 'internal_model.dart';
Expand Down Expand Up @@ -305,54 +304,36 @@ class ToolEnvironment {
Future<PanaProcessResult> runUpgrade(
String packageDir,
bool usesFlutter, {
int retryCount = 3,
@Deprecated('retryCount is ignored') int retryCount = 3,
}) async {
return await _withStripAndAugmentPubspecYaml(packageDir, () async {
final retryOptions = const RetryOptions(maxAttempts: 3);
bool retryIf(PanaProcessResult result) {
final errOutput = result.stderr.asString;
// find cases where retrying is not going to help – and short-circuit
if (errOutput.contains('Could not get versions for flutter from sdk')) {
return false;
}
if (errOutput.contains('FINE: Exception type: NoVersionException')) {
return false;
}
return true;
}
return await runPub(
packageDir,
usesFlutter: usesFlutter,
command: 'upgrade',
);
}

if (usesFlutter) {
return await runConstrained(
[
Future<PanaProcessResult> runPub(
String packageDir, {
required bool usesFlutter,
required String command,
}) async {
return await _withStripAndAugmentPubspecYaml(packageDir, () async {
return await runConstrained(
[
if (usesFlutter) ...[
..._flutterSdk.flutterCmd,
'packages',
'pub',
'upgrade',
'--no-example',
'--verbose',
],
workingDirectory: packageDir,
environment:
usesFlutter ? _flutterSdk.environment : _dartSdk.environment,
retryIf: retryIf,
retryOptions: retryOptions,
);
} else {
return await runConstrained(
[
] else
..._dartSdk.dartCmd,
'pub',
'upgrade',
'--no-example',
'--verbose',
],
workingDirectory: packageDir,
environment:
usesFlutter ? _flutterSdk.environment : _dartSdk.environment,
retryIf: retryIf,
retryOptions: retryOptions,
);
}
'pub',
command,
'--no-example',
],
workingDirectory: packageDir,
environment:
usesFlutter ? _flutterSdk.environment : _dartSdk.environment,
);
});
}

Expand Down
2 changes: 1 addition & 1 deletion lib/src/version.dart

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

2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: pana
description: PAckage aNAlyzer - produce a report summarizing the health and quality of a Dart package.
version: 0.22.3
version: 0.22.4
repository: https://github.com/dart-lang/pana
topics:
- tool
Expand Down
6 changes: 3 additions & 3 deletions test/goldens/end2end/_dummy_pkg-1.0.0-null-safety.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@
"id": "dependency",
"title": "Support up-to-date dependencies",
"grantedPoints": 0,
"maxPoints": 20,
"maxPoints": 40,
"status": "failed",
"summary": "### [x] 0/10 points: All of the package dependencies are supported in the latest version\n\n<details>\n<summary>\nSdk constraint doesn't support current Dart version {{sdk-version}}. Cannot run `dart pub outdated`.\n</summary>\n\n`pubspec.yaml:6:8`\n\n```\n\n6 │ sdk: \">=2.12.0-0 <2.12.0\"\n │ ^^^^^^^^^^^^^^^^^^^^\n\n```\n\n</details>\n\n### [x] 0/10 points: Package supports latest stable Dart and Flutter SDKs\n\n<details>\n<summary>\nSdk constraint doesn't support current Dart version {{sdk-version}}.\n</summary>\n\n`pubspec.yaml:6:8`\n\n```\n\n6 │ sdk: \">=2.12.0-0 <2.12.0\"\n │ ^^^^^^^^^^^^^^^^^^^^\n\n```\n\nTry widening the upper boundary of the constraint.\n</details>"
"summary": "### [x] 0/10 points: All of the package dependencies are supported in the latest version\n\n<details>\n<summary>\nSdk constraint doesn't support current Dart version {{sdk-version}}. Cannot run `dart pub outdated`.\n</summary>\n\n`pubspec.yaml:6:8`\n\n```\n ╷\n6 │ sdk: \">=2.12.0-0 <2.12.0\"\n │ ^^^^^^^^^^^^^^^^^^^^\n ╵\n```\n\n</details>\n\n### [x] 0/10 points: Package supports latest stable Dart and Flutter SDKs\n\n<details>\n<summary>\nSdk constraint doesn't support current Dart version {{sdk-version}}.\n</summary>\n\n`pubspec.yaml:6:8`\n\n```\n ╷\n6 │ sdk: \">=2.12.0-0 <2.12.0\"\n │ ^^^^^^^^^^^^^^^^^^^^\n ╵\n```\n\nTry widening the upper boundary of the constraint.\n</details>\n\n### [x] 0/20 points: Compatible with dependency constraint lower bounds\n\n* `dart pub downgrade` failed with:\n\n```\nOUT:\nResolving dependencies...\nERR:\nThe current Dart SDK version is {{sdk-version}}.\n\nBecause _dummy_pkg requires SDK version >=2.12.0-0 <2.12.0, version solving failed.\n```\n\nRun `dart pub downgrade` and then `dart analyze` to reproduce the above problem.\n\nYou may run `dart pub upgrade --tighten` to update your dependency constraints, or visit http://dart.dev/go/downgrade-testing for further help."
}
]
},
"screenshots": [],
"result": {
"homepageUrl": "https://github.com/dart-lang/pub-dev",
"grantedPoints": 15,
"maxPoints": 140
"maxPoints": 160
},
"urlProblems": [],
"errorMessage": "Running `dart pub outdated` failed with the following output:\n\n```\nThe current Dart SDK version is {{sdk-version}}.\nBecause _dummy_pkg requires SDK version >=2.12.0-0 <2.12.0, version solving failed.\n```"
Expand Down
21 changes: 19 additions & 2 deletions test/goldens/end2end/_dummy_pkg-1.0.0-null-safety.1.json_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Because _dummy_pkg requires SDK version >=2.12.0-0 <2.12.0, version solving fail
```


## 0/20 Support up-to-date dependencies
## 0/40 Support up-to-date dependencies

### [x] 0/10 points: All of the package dependencies are supported in the latest version

Expand Down Expand Up @@ -134,4 +134,21 @@ Sdk constraint doesn't support current Dart version {{sdk-version}}.
```

Try widening the upper boundary of the constraint.
</details>
</details>

### [x] 0/20 points: Compatible with dependency constraint lower bounds

* `dart pub downgrade` failed with:

```
OUT:
Resolving dependencies...
ERR:
The current Dart SDK version is {{sdk-version}}.
Because _dummy_pkg requires SDK version >=2.12.0-0 <2.12.0, version solving failed.
```

Run `dart pub downgrade` and then `dart analyze` to reproduce the above problem.

You may run `dart pub upgrade --tighten` to update your dependency constraints, or visit http://dart.dev/go/downgrade-testing for further help.
10 changes: 5 additions & 5 deletions test/goldens/end2end/async-2.11.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@
{
"id": "dependency",
"title": "Support up-to-date dependencies",
"grantedPoints": 20,
"maxPoints": 20,
"grantedPoints": 40,
"maxPoints": 40,
"status": "passed",
"summary": "### [*] 10/10 points: All of the package dependencies are supported in the latest version\n\n|Package|Constraint|Compatible|Latest|\n|:-|:-|:-|:-|\n|[`collection`]|`^1.15.0`|1.18.0|1.18.0|\n|[`meta`]|`^1.1.7`|1.11.0|1.11.0|\n\nTo reproduce run `dart pub outdated --no-dev-dependencies --up-to-date --no-dependency-overrides`.\n\n[`collection`]: https://pub.dev/packages/collection\n[`meta`]: https://pub.dev/packages/meta\n\n\n### [*] 10/10 points: Package supports latest stable Dart and Flutter SDKs\n"
"summary": "### [*] 10/10 points: All of the package dependencies are supported in the latest version\n\n|Package|Constraint|Compatible|Latest|\n|:-|:-|:-|:-|\n|[`collection`]|`^1.15.0`|1.18.0|1.18.0|\n|[`meta`]|`^1.1.7`|1.11.0|1.11.0|\n\nTo reproduce run `dart pub outdated --no-dev-dependencies --up-to-date --no-dependency-overrides`.\n\n[`collection`]: https://pub.dev/packages/collection\n[`meta`]: https://pub.dev/packages/meta\n\n\n### [*] 10/10 points: Package supports latest stable Dart and Flutter SDKs\n\n\n### [*] 20/20 points: Compatible with dependency constraint lower bounds\n\n`pub downgrade` does not expose any static analysis error."
}
]
},
Expand All @@ -118,8 +118,8 @@
"branch": "master"
},
"contributingUrl": "https://github.com/dart-lang/async/blob/master/CONTRIBUTING.md",
"grantedPoints": 130,
"maxPoints": 140
"grantedPoints": 150,
"maxPoints": 160
},
"urlProblems": []
}
7 changes: 6 additions & 1 deletion test/goldens/end2end/async-2.11.0.json_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ See [package layout](https://dart.dev/tools/pub/package-layout#examples) guideli
### [*] 50/50 points: code has no errors, warnings, lints, or formatting issues


## 20/20 Support up-to-date dependencies
## 40/40 Support up-to-date dependencies

### [*] 10/10 points: All of the package dependencies are supported in the latest version

Expand All @@ -61,3 +61,8 @@ To reproduce run `dart pub outdated --no-dev-dependencies --up-to-date --no-depe


### [*] 10/10 points: Package supports latest stable Dart and Flutter SDKs


### [*] 20/20 points: Compatible with dependency constraint lower bounds

`pub downgrade` does not expose any static analysis error.
6 changes: 3 additions & 3 deletions test/goldens/end2end/audio_service-0.18.10.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@
"id": "dependency",
"title": "Support up-to-date dependencies",
"grantedPoints": 10,
"maxPoints": 20,
"maxPoints": 40,
"status": "failed",
"summary": "### [x] 0/10 points: All of the package dependencies are supported in the latest version\n\n* Could not run `flutter pub outdated`: `flutter pub get` failed:\n\n```\nOUT:\nResolving dependencies...\nERR:\nNote: meta is pinned to version 1.12.0 by flutter_web_plugins from the flutter SDK.\nSee https://dart.dev/go/sdk-version-pinning for details.\n\n\nBecause every version of flutter_web_plugins from sdk depends on meta 1.12.0 which doesn't match any versions, flutter_web_plugins from sdk is forbidden.\nSo, because audio_service depends on flutter_web_plugins from sdk, version solving failed.\n```\n\n### [*] 10/10 points: Package supports latest stable Dart and Flutter SDKs\n"
"summary": "### [x] 0/10 points: All of the package dependencies are supported in the latest version\n\n* Could not run `flutter pub outdated`: `flutter pub get` failed:\n\n```\nOUT:\nResolving dependencies...\nERR:\nNote: meta is pinned to version 1.12.0 by flutter_web_plugins from the flutter SDK.\nSee https://dart.dev/go/sdk-version-pinning for details.\n\n\nBecause every version of flutter_web_plugins from sdk depends on meta 1.12.0 which doesn't match any versions, flutter_web_plugins from sdk is forbidden.\nSo, because audio_service depends on flutter_web_plugins from sdk, version solving failed.\n```\n\n### [*] 10/10 points: Package supports latest stable Dart and Flutter SDKs\n\n\n### [x] 0/20 points: Compatible with dependency constraint lower bounds\n\n* `flutter pub downgrade` failed with:\n\n```\nOUT:\nResolving dependencies...\nERR:\nNote: meta is pinned to version 1.12.0 by flutter_web_plugins from the flutter SDK.\nSee https://dart.dev/go/sdk-version-pinning for details.\n\n\nBecause every version of flutter_web_plugins from sdk depends on meta 1.12.0 which doesn't match any versions, flutter_web_plugins from sdk is forbidden.\nSo, because audio_service depends on flutter_web_plugins from sdk, version solving failed.\n```\n\nRun `flutter pub downgrade` and then `flutter analyze` to reproduce the above problem.\n\nYou may run `dart pub upgrade --tighten` to update your dependency constraints, or visit http://dart.dev/go/downgrade-testing for further help."
}
]
},
Expand All @@ -147,7 +147,7 @@
},
"contributingUrl": "https://github.com/ryanheise/audio_service/blob/minor/CONTRIBUTING.md",
"grantedPoints": 50,
"maxPoints": 130
"maxPoints": 150
},
"urlProblems": [],
"errorMessage": "Running `flutter pub outdated` failed with the following output:\n\n```\nNote: meta is pinned to version 1.12.0 by flutter_web_plugins from the flutter SDK.\nSee https://dart.dev/go/sdk-version-pinning for details.\n```"
Expand Down
23 changes: 22 additions & 1 deletion test/goldens/end2end/audio_service-0.18.10.json_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ See https://dart.dev/go/sdk-version-pinning for details.
```


## 10/20 Support up-to-date dependencies
## 10/40 Support up-to-date dependencies

### [x] 0/10 points: All of the package dependencies are supported in the latest version

Expand All @@ -61,3 +61,24 @@ So, because audio_service depends on flutter_web_plugins from sdk, version solvi
```

### [*] 10/10 points: Package supports latest stable Dart and Flutter SDKs


### [x] 0/20 points: Compatible with dependency constraint lower bounds

* `flutter pub downgrade` failed with:

```
OUT:
Resolving dependencies...
ERR:
Note: meta is pinned to version 1.12.0 by flutter_web_plugins from the flutter SDK.
See https://dart.dev/go/sdk-version-pinning for details.
Because every version of flutter_web_plugins from sdk depends on meta 1.12.0 which doesn't match any versions, flutter_web_plugins from sdk is forbidden.
So, because audio_service depends on flutter_web_plugins from sdk, version solving failed.
```

Run `flutter pub downgrade` and then `flutter analyze` to reproduce the above problem.

You may run `dart pub upgrade --tighten` to update your dependency constraints, or visit http://dart.dev/go/downgrade-testing for further help.
6 changes: 3 additions & 3 deletions test/goldens/end2end/bulma_min-0.7.4.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
"id": "dependency",
"title": "Support up-to-date dependencies",
"grantedPoints": 0,
"maxPoints": 20,
"maxPoints": 40,
"status": "failed",
"summary": "### [x] 0/10 points: All of the package dependencies are supported in the latest version\n\n<details>\n<summary>\nSdk constraint doesn't support current Dart version {{sdk-version}}. Cannot run `dart pub outdated`.\n</summary>\n\n`pubspec.yaml:8:8`\n\n```\n\n8 │ sdk: '>=1.0.0 <3.0.0'\n │ ^^^^^^^^^^^^^^^^\n\n```\n\n</details>\n\n### [x] 0/10 points: Package supports latest stable Dart and Flutter SDKs\n\n<details>\n<summary>\nSdk constraint doesn't support current Dart version {{sdk-version}}.\n</summary>\n\n`pubspec.yaml:8:8`\n\n```\n\n8 │ sdk: '>=1.0.0 <3.0.0'\n │ ^^^^^^^^^^^^^^^^\n\n```\n\nTry widening the upper boundary of the constraint.\n</details>"
"summary": "### [x] 0/10 points: All of the package dependencies are supported in the latest version\n\n<details>\n<summary>\nSdk constraint doesn't support current Dart version {{sdk-version}}. Cannot run `dart pub outdated`.\n</summary>\n\n`pubspec.yaml:8:8`\n\n```\n ╷\n8 │ sdk: '>=1.0.0 <3.0.0'\n │ ^^^^^^^^^^^^^^^^\n ╵\n```\n\n</details>\n\n### [x] 0/10 points: Package supports latest stable Dart and Flutter SDKs\n\n<details>\n<summary>\nSdk constraint doesn't support current Dart version {{sdk-version}}.\n</summary>\n\n`pubspec.yaml:8:8`\n\n```\n ╷\n8 │ sdk: '>=1.0.0 <3.0.0'\n │ ^^^^^^^^^^^^^^^^\n ╵\n```\n\nTry widening the upper boundary of the constraint.\n</details>\n\n### [x] 0/20 points: Compatible with dependency constraint lower bounds\n\n* `dart pub downgrade` failed with:\n\n```\nOUT:\nResolving dependencies...\nERR:\nThe lower bound of \"sdk: '>=1.0.0 <3.0.0'\" must be 2.12.0'\nor higher to enable null safety.\n\nThe current Dart SDK (3.4.0) only supports null safety.\n\nFor details, see https://dart.dev/null-safety\n```\n\nRun `dart pub downgrade` and then `dart analyze` to reproduce the above problem.\n\nYou may run `dart pub upgrade --tighten` to update your dependency constraints, or visit http://dart.dev/go/downgrade-testing for further help."
}
]
},
Expand All @@ -89,7 +89,7 @@
"branch": "master"
},
"grantedPoints": 30,
"maxPoints": 130
"maxPoints": 150
},
"urlProblems": [],
"errorMessage": "Running `dart pub outdated` failed with the following output:\n\n```\nThe lower bound of \"sdk: '>=1.0.0 <3.0.0'\" must be 2.12.0'\nor higher to enable null safety.\n```"
Expand Down
Loading

0 comments on commit e5d79d8

Please sign in to comment.