Skip to content

Commit

Permalink
[ts/checkProjects] validate extends of all ts projects (#145176)
Browse files Browse the repository at this point in the history
Fixes #145129

In order to make our TS setup in the repo far more efficient we needed
to make it a little more complicated than it used to be. This includes a
few rules that all tsconfig files need to follow which were currently
assumed to be true, but with this PR will now be validated:

1. No tsconfig.json files are allowed to extend the `tsconfig.json`
file. This file is used to produce types for the root of the repo, and
include the `package.json` file in a project so it can be referenced in
projects directly. Files which violate this rule were updated to point
to the root `tsconfig.base.json` file.
2. Every tsconfig.json file must extend the `tsconfig.base.json` file,
either directly or indirectly.
  • Loading branch information
Spencer authored Nov 16, 2022
1 parent 2a69211 commit 12aeaa8
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/field_formats_example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./target",
"skipLibCheck": true
Expand Down
2 changes: 1 addition & 1 deletion examples/files_example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./target/types"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./target/types"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/partial_results_example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./target",
"skipLibCheck": true
Expand Down
4 changes: 4 additions & 0 deletions src/dev/typescript/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,8 @@ export class Project {
? [this.tsConfigPath, ...this.baseProject.getConfigPaths()]
: [this.tsConfigPath];
}

public getProjectsDeep(): Project[] {
return this.baseProject ? [this, ...this.baseProject.getProjectsDeep()] : [this];
}
}
20 changes: 20 additions & 0 deletions src/dev/typescript/run_check_ts_projects_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { run } from '@kbn/dev-cli-runner';
import { asyncMapWithLimit } from '@kbn/std';
import { createFailError } from '@kbn/dev-cli-errors';
import { getRepoFiles } from '@kbn/get-repo-files';
import { REPO_ROOT } from '@kbn/utils';
import globby from 'globby';

import { File } from '../file';
Expand All @@ -37,6 +38,25 @@ export async function runCheckTsProjectsCli() {
const stats = new Stats();
let failed = false;

const everyProjectDeep = new Set(PROJECTS.flatMap((p) => p.getProjectsDeep()));
for (const proj of everyProjectDeep) {
const [, ...baseConfigRels] = proj.getConfigPaths().map((p) => Path.relative(REPO_ROOT, p));
const configRel = Path.relative(REPO_ROOT, proj.tsConfigPath);

if (baseConfigRels[0] === 'tsconfig.json') {
failed = true;
log.error(
`[${configRel}]: This tsconfig extends the root tsconfig.json file and shouldn't. The root tsconfig.json file is not a valid base config, you probably want to point to the tsconfig.base.json file.`
);
}
if (configRel !== 'tsconfig.base.json' && !baseConfigRels.includes('tsconfig.base.json')) {
failed = true;
log.error(
`[${configRel}]: This tsconfig does not extend the tsconfig.base.json file either directly or indirectly. The TS config setup for the repo expects every tsconfig file to extend this base config file.`
);
}
}

const pathsAndProjects = await asyncMapWithLimit(PROJECTS, 5, async (proj) => {
const paths = await globby(proj.getIncludePatterns(), {
ignore: proj.getExcludePatterns(),
Expand Down

0 comments on commit 12aeaa8

Please sign in to comment.