Skip to content

Commit d9c3edc

Browse files
authored
ci: add public TypeScript interface restrictiveness check (#9826)
## Description Adds a TypeScript public interface restrictiveness check in `core/test-utils`. ## Impact Detects breaking changes when public TypeScript inputs narrow or outputs expand. ## Testing - Verified `pnpm --dir core/test-utils test` across Node.js and Bun (`4 passed, 4 total` suites, `35 passed` tests, `>28,000` compiled consumer probes across public packages with `<1%` base invalid ratio). - Verified `pnpm --dir core/test-utils run check` and `node ./bin/linter.mjs --strict`. Note: this was opened in favour of #9825
1 parent 3efde16 commit d9c3edc

3 files changed

Lines changed: 4440 additions & 90 deletions

File tree

‎.github/workflows/breaking-change-check.yaml‎

Lines changed: 22 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: breaking-change-check
33

44
on:
55
pull_request:
6+
types: [opened, synchronize, reopened]
67
workflow_dispatch:
78

89
concurrency:
@@ -27,104 +28,35 @@ jobs:
2728
filters: |
2829
check:
2930
- 'core/**/*.ts'
31+
- 'core/**/package.json'
32+
- 'core/**/tsconfig*.json'
3033
- 'handwritten/**/*.ts'
34+
- 'handwritten/**/package.json'
35+
- 'handwritten/**/tsconfig*.json'
36+
- '.github/workflows/breaking-change-check.yaml'
37+
self:
38+
- 'core/test-utils/**'
3139
- '.github/workflows/breaking-change-check.yaml'
3240
- name: Use Node.js 24
33-
if: steps.changes.outputs.check == 'true' || github.event_name == 'workflow_dispatch'
41+
if: steps.changes.outputs.check == 'true' || steps.changes.outputs.self == 'true' || github.event_name == 'workflow_dispatch'
3442
uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0
3543
with:
3644
node-version: 24
3745
- uses: ./.github/actions/pnpm-lockfile-check
38-
if: steps.changes.outputs.check == 'true' || github.event_name == 'workflow_dispatch'
46+
if: steps.changes.outputs.check == 'true' || steps.changes.outputs.self == 'true' || github.event_name == 'workflow_dispatch'
3947
- name: Compile affected packages and workspace dependents via pnpm
40-
if: steps.changes.outputs.check == 'true' || github.event_name == 'workflow_dispatch'
48+
if: steps.changes.outputs.check == 'true' || steps.changes.outputs.self == 'true' || github.event_name == 'workflow_dispatch'
49+
run: |
50+
BASE_SHA=$(git merge-base HEAD^1 HEAD 2>/dev/null || git rev-parse HEAD^1)
51+
pnpm --filter "...[${BASE_SHA}...HEAD]..." --filter "!./packages/*" --if-present run compile
52+
- name: Run breaking-change-check unit and harness tests
53+
if: steps.changes.outputs.self == 'true' || github.event_name == 'workflow_dispatch'
4154
run: |
42-
pnpm --config.link-workspace-packages=deep --config.prefer-workspace-packages=true --filter "...[HEAD^1...HEAD]" --filter "!./packages/*" --if-present run compile
43-
- name: Check declaration changes with pnpm dlx @api-extractor-tools/change-detector
55+
pnpm --dir core/test-utils exec jest test/breaking-change-check.test.ts
56+
- name: Check public TypeScript interfaces with core/test-utils
4457
if: steps.changes.outputs.check == 'true' || github.event_name == 'workflow_dispatch'
58+
env:
59+
GITHUB_EVENT_NAME: ${{ github.event_name }}
60+
BREAKING_CHANGE_BASE_REF: ${{ github.event_name == 'workflow_dispatch' && 'HEAD^1' || '' }}
4561
run: |
46-
node --input-type=module - <<'EOF'
47-
import {execFileSync} from 'node:child_process';
48-
import fs from 'node:fs';
49-
import {createRequire} from 'node:module';
50-
import path from 'node:path';
51-
52-
const require = createRequire(path.join(process.cwd(), 'package.json'));
53-
const ts = require('typescript');
54-
55-
const diff = execFileSync(
56-
'git',
57-
[
58-
'diff',
59-
'--name-status',
60-
'HEAD^1...HEAD',
61-
'--',
62-
'*.ts',
63-
':!packages',
64-
':!**/test/**',
65-
':!**/tests/**',
66-
':!**/system-test/**',
67-
':!**/samples/**',
68-
':!**/fixtures/**',
69-
':!**/benchmark/**',
70-
],
71-
{encoding: 'utf8'},
72-
);
73-
74-
const findCompiledDts = file => {
75-
let dir = path.dirname(path.resolve(file));
76-
while (dir !== path.dirname(dir) && !fs.existsSync(path.join(dir, 'package.json'))) {
77-
dir = path.dirname(dir);
78-
}
79-
const relInPkg = path.relative(dir, path.resolve(file));
80-
const candidates = [
81-
path.join(dir, 'build', relInPkg.replace(/\.ts$/, '.d.ts')),
82-
path.join(dir, 'build', relInPkg.replace(/^src[\\/]/, '').replace(/\.ts$/, '.d.ts')),
83-
];
84-
return candidates.find(c => fs.existsSync(c));
85-
};
86-
87-
const breaking = [];
88-
for (const line of diff.split('\n').map(l => l.trim()).filter(Boolean)) {
89-
const [status, oldPath, newPath] = line.split(/\t+/);
90-
const file = status?.startsWith('R') ? newPath : oldPath;
91-
if (!status || !file || !oldPath || !file.endsWith('.ts') || /\.(test|spec)\.ts$/.test(file)) continue;
92-
if (status.startsWith('A')) continue;
93-
if (status.startsWith('D')) {
94-
breaking.push({file: oldPath, explanation: 'Public source file was deleted.'});
95-
continue;
96-
}
97-
const baseSrc = execFileSync('git', ['show', `HEAD^1:${oldPath}`], {encoding: 'utf8'});
98-
fs.writeFileSync('/tmp/base.d.ts', ts.transpileDeclaration(baseSrc, {fileName: oldPath, compilerOptions: {stripInternal: true}}).outputText);
99-
const compiledDts = findCompiledDts(file);
100-
const headDts = compiledDts
101-
? fs.readFileSync(compiledDts, 'utf8')
102-
: ts.transpileDeclaration(fs.readFileSync(file, 'utf8'), {fileName: file, compilerOptions: {stripInternal: true}}).outputText;
103-
fs.writeFileSync('/tmp/head.d.ts', headDts);
104-
105-
const out = execFileSync(
106-
'pnpm',
107-
['dlx', '@api-extractor-tools/change-detector', '/tmp/base.d.ts', '/tmp/head.d.ts', '--json'],
108-
{encoding: 'utf8'},
109-
);
110-
const report = JSON.parse(out.slice(out.indexOf('{')));
111-
const visit = list => {
112-
for (const c of list || []) {
113-
if (c.action === 'removed' || c.impact === 'narrowing') {
114-
breaking.push({file, explanation: c.explanation});
115-
}
116-
if (c.nestedChanges) visit(c.nestedChanges);
117-
}
118-
};
119-
for (const group of Object.values(report.changes || {})) visit(group);
120-
}
121-
122-
if (breaking.length > 0) {
123-
for (const b of breaking) {
124-
console.log(`::error file=${b.file},title=Breaking Change Detected::${b.explanation}`);
125-
}
126-
process.exitCode = 1;
127-
} else {
128-
console.log('No breaking changes detected.');
129-
}
130-
EOF
62+
pnpm --dir core/test-utils run check

‎core/test-utils/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"scripts": {
1313
"test": "jest --coverage",
14+
"check": "BREAKING_CHANGE_CHECK_MODE=diff jest test/breaking-change-check.test.ts -t \"public TypeScript interface breaking-change check\"",
1415
"compile": "tsc -p .",
1516
"fix": "gts fix",
1617
"lint": "gts check",

0 commit comments

Comments
 (0)