Skip to content

Commit 95689ea

Browse files
authored
feat(jsii-diff): allow external stability to be treated as error (#4076)
In certain situations one might want to treat changes to `external` APIs as errors. This could be to check the external APIs for any breaking changes and have an opportunity to selectively reject, heal or soften an upstream change. This change introduces a new cli option `--error-on` which can be one of three classes: | `--error-on` | Stabilities that cause an ERROR | | ------------------ | -------------------------------------------------- | | `prod` (default) | `stable`, `deprecated` | | `non-experimental` | `stable`, `deprecated`, `external` | | `all` | `stable`, `deprecated`, `experimental`, `external` | **Fixes `deprecated` APIs not being treated as `stable`.** In jsii, deprecations are treated as a stability. However for the purpose of jsii-diff they should be treated as stable. Otherwise one could do `stable -> deprecated, make breaking change, deprecated -> stable`, which should not be allowed. We also can't prohibit the transition from deprecated back to stable, as it's perfectly okay to un-deprecate an API. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent 4fd596a commit 95689ea

File tree

4 files changed

+185
-61
lines changed

4 files changed

+185
-61
lines changed

packages/jsii-diff/README.md

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,97 @@
22

33
__jsii-diff__ compares two jsii assemblies for compatibility.
44

5-
In the future, it will be able to do generic comparisons, but for
5+
In the future, it will be able to do generic comparisons.
6+
But for
67
now it will compare assemblies for API compatibility, and exit
7-
with a non-zero exit code if any **stable** APIs have had incompatible
8-
changes.
8+
with a non-zero exit code if any __stable__ or __deprecated__ APIs have had incompatible changes.
99

10-
API items that have no stability are treated as **stable**. To treat
11-
unmarked API items as experimental, pass the `--default-experimental` flag.
10+
API items that have no stability are treated as __stable__.
11+
To treat unmarked API items as experimental, pass the `--default-experimental` flag.
1212

1313
## Usage
1414

1515
To compare two JSII packages:
1616

17-
jsii-diff <old> [new]
17+
```console
18+
jsii-diff <old> [new]
19+
```
1820

1921
Packages can be identified by either:
2022

21-
* **A path**, in which case it should be the path to a JSII package directory,
23+
* __A path__, in which case it should be the path to a JSII package directory,
2224
or to a `.jsii` file.
23-
* **An NPM package specifier** of the form `npm:[<package>[@version]]`, in
25+
* __An NPM package specifier__ of the form `npm:[<package>[@version]]`, in
2426
which case the indicated version is downloaded and used. If `@version` is
2527
left out, the latest version will be used. If `package` is left out,
2628
the assembly name of `.jsii` in the current directory will be used.
2729

2830
To compare current package against latest published NPM release:
2931

30-
jsii-diff npm:
32+
```console
33+
jsii-diff npm:<package>
34+
```
35+
36+
### Stability Error Classes
37+
38+
By default only incompatible changes to `stable` or `deprecated` APIs are treated as errors and will fail the command.
39+
Changes to `experimental` or `external` APIs emit a warning.
40+
41+
Change this behavior with the `--error-on` flag:
42+
43+
```console
44+
jsii-diff npm:<package> --error-on=all
45+
```
46+
47+
The following `--error-on` groups are available:
48+
49+
| `--error-on` | Stabilities that cause an ERROR |
50+
| ------------------ | -------------------------------------------------- |
51+
| `prod` (default) | `stable`, `deprecated` |
52+
| `non-experimental` | `stable`, `deprecated`, `external` |
53+
| `all` | `stable`, `deprecated`, `experimental`, `external` |
3154

3255
## Details
3356

34-
__jsii-diff__ will assert that code written against version **A** of a library
35-
will still typecheck when compiled against version **B** of that library. It
57+
__jsii-diff__ will assert that code written against version __A__ of a library
58+
will still typecheck when compiled against version __B__ of that library. It
3659
does this by verifying the following properties:
3760

38-
- Any type (class/interface/enum) in **A** must also exist in **B**.
39-
- Enums have only added members.
40-
- Classes and interfaces have only added members, or modified existing
61+
* Any type (class/interface/enum) in __A__ must also exist in __B__.
62+
* Enums have only added members.
63+
* Classes and interfaces have only added members, or modified existing
4164
members in an allowed way.
42-
- Property types are the same or have been strengthened (see below).
43-
- Methods have only added optional arguments, existing argument types have
65+
* Property types are the same or have been strengthened (see below).
66+
* Methods have only added optional arguments, existing argument types have
4467
only been weakened, and the return type has only been strengthened (see below).
4568

4669
### Strengthening and weakening
4770

48-
- *Strengthening* a type refers to *excluding* more possible values. Changing
71+
* *Strengthening* a type refers to *excluding* more possible values. Changing
4972
a field from `optional` to `required`, or changing a type from `any` to
5073
`string` are examples of strengthening.
5174

52-
- As the opposite of strengthening, *weakening* refers to *allowing* more
75+
* As the opposite of strengthening, *weakening* refers to *allowing* more
5376
possible values. Changing a field from `required` to `optional`, or
5477
changing a type to a superclass or interface are examples of weakening.
5578

5679
An API can change in the following way without breaking its consumer:
5780

58-
- It can *weaken* its input (require *less* from the caller); and
59-
- It can *strengthen* its output (guarantee *more* to the caller).
81+
* It can *weaken* its input (require *less* from the caller); and
82+
* It can *strengthen* its output (guarantee *more* to the caller).
6083

6184
### Struct types
6285

6386
Structs (interfaces consisting completely of `readonly` properties) are
6487
treated as bags of data. Their API compatibility will be evaluated depending
6588
on whether they appear in input or output position of operations.
6689

67-
- Structs are *weakened* if all types of all of its properties are weakened.
90+
* Structs are *weakened* if all types of all of its properties are weakened.
6891
Normally removing properties would also be considered weakening, but
6992
because that may cause references to the fields in existing code bases to
7093
become undefined (which is not allowed in most programming languages) we
7194
disallow removing properties.
72-
- Structs are *strengthened* if all types of all of its properties are
95+
* Structs are *strengthened* if all types of all of its properties are
7396
strengthened, or if fields are added.
7497

7598
__jsii-diff__ will check the evolution of structs against their position

packages/jsii-diff/bin/jsii-diff.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import * as yargs from 'yargs';
99
import { compareAssemblies } from '../lib';
1010
import {
1111
classifyDiagnostics,
12+
treatAsError,
1213
formatDiagnostic,
1314
hasErrors,
15+
ErrorClass,
16+
ERROR_CLASSES,
1417
} from '../lib/diagnostics';
1518
import {
1619
DownloadFailure,
@@ -42,6 +45,13 @@ async function main(): Promise<number> {
4245
type: 'boolean',
4346
default: false,
4447
desc: 'Error on experimental API changes',
48+
deprecate: 'Use `--error-on` instead',
49+
})
50+
.option('error-on', {
51+
type: 'string',
52+
default: 'prod',
53+
choices: ERROR_CLASSES,
54+
desc: 'Which type of API changes should be treated as an error',
4555
})
4656
.option('ignore-file', {
4757
alias: 'i',
@@ -119,15 +129,15 @@ async function main(): Promise<number> {
119129
if (mismatches.count > 0) {
120130
const diags = classifyDiagnostics(
121131
mismatches,
122-
argv['experimental-errors'],
132+
treatAsError(argv['error-on'] as ErrorClass, argv['experimental-errors']),
123133
await loadFilter(argv['ignore-file']),
124134
);
125135

126136
process.stderr.write(
127137
`Original assembly: ${original.name}@${original.version}\n`,
128138
);
129139
process.stderr.write(
130-
`Updated assembly: ${updated.name}@${updated.version}\n`,
140+
`Updated assembly: ${updated.name}@${updated.version}\n`,
131141
);
132142
process.stderr.write('API elements with incompatible changes:\n');
133143
for (const diag of diags) {

packages/jsii-diff/lib/diagnostics.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,57 @@ export function hasErrors(diags: Diagnostic[]) {
3636
return diags.some((diag) => diag.level === DiagLevel.Error);
3737
}
3838

39+
export function onlyErrors(diags: Diagnostic[]) {
40+
return diags.filter((diag) => diag.level === DiagLevel.Error);
41+
}
42+
43+
export function onlyWarnings(diags: Diagnostic[]) {
44+
return diags.filter((diag) => diag.level === DiagLevel.Warning);
45+
}
46+
47+
export const ERROR_CLASSES = ['prod', 'non-experimental', 'all'] as const;
48+
49+
export type ErrorClass = (typeof ERROR_CLASSES)[number];
50+
51+
export const ERROR_CLASSES_TO_STABILITIES: Record<ErrorClass, Stability[]> = {
52+
prod: [Stability.Stable, Stability.Deprecated],
53+
'non-experimental': [
54+
Stability.Stable,
55+
Stability.Deprecated,
56+
Stability.External,
57+
],
58+
all: [
59+
Stability.Stable,
60+
Stability.Experimental,
61+
Stability.External,
62+
Stability.Deprecated,
63+
],
64+
};
65+
66+
export function treatAsError(
67+
errorClass: ErrorClass,
68+
deprecatedExperimentalErrors = false,
69+
): Set<Stability> {
70+
const shouldError = new Set<Stability>();
71+
72+
for (const stability of ERROR_CLASSES_TO_STABILITIES[errorClass]) {
73+
shouldError.add(stability);
74+
}
75+
76+
if (deprecatedExperimentalErrors) {
77+
shouldError.add(Stability.Experimental);
78+
}
79+
80+
return shouldError;
81+
}
82+
3983
/**
4084
* Classify API mismatches into a set of warnings and errors
4185
*/
4286
export function classifyDiagnostics(
4387
mismatches: Mismatches,
44-
experimentalErrors: boolean,
45-
skipFilter: Set<string>,
88+
shouldError: Set<Stability>,
89+
skipFilter: Set<string> = new Set(),
4690
): Diagnostic[] {
4791
const ret = mismatches.mismatches.map((mis) => ({
4892
level: level(mis),
@@ -56,12 +100,7 @@ export function classifyDiagnostics(
56100
if (skipFilter.has(mis.violationKey)) {
57101
return DiagLevel.Skipped;
58102
}
59-
if (
60-
mis.stability === Stability.Stable ||
61-
(mis.stability === Stability.Experimental && experimentalErrors)
62-
) {
63-
return DiagLevel.Error;
64-
}
65-
return DiagLevel.Warning;
103+
104+
return shouldError.has(mis.stability) ? DiagLevel.Error : DiagLevel.Warning;
66105
}
67106
}

0 commit comments

Comments
 (0)