Skip to content

Commit 58cd8e9

Browse files
mcmireMrtenz
andauthored
Ensure refs in tsconfig files are synced with internal deps (#8384)
### Problem We use project references to tell TypeScript and other tools about the structure of the monorepo. Both the root- and package-level tsconfig files have a `references` field which is used to link packages together. All packages that we want to build and publish need to be present in `references` within the root `tsconfig.json` and `tsconfig.build.json` files, and the `references` field in both root-level and package-level tsconfig files need to list all internal dependencies. However, it's a pain to keep references up to date, especially as as packages are added or dependencies are updated. There are cases in the past, in fact, where packages were not published or type errors occurred because the references were not correctly kept in sync. ### Solution We don't need to do this manual work. We can infer root references by scanning workspaces, and we can infer references for an individual package by scanning that package's `dependencies` for other workspace packages. This commit adds a script that can be run on both the root workspace and child workspaces to check and/or regenerate the `references` field for each tsconfig file so that it is kept in sync with dependencies throughout the monorepo. There are also new package scripts for use: - Root level - **`lint:tsconfigs`:** Validates the root `tsconfig.json` and `tsconfig.build.json`, erroring if any are out of sync. - **`lint:tsconfigs:all`:** Validates all `tsconfig.json` and `tsconfig.build.json` across the monorepo, erroring if any are out of sync. - **`lint:tsconfigs:fix`:** Regenerates the root `tsconfig.json` and `tsconfig.build.json`. - **`lint:tsconfigs:fix:all`:** Regenerates `tsconfig.json` and `tsconfig.build.json` files across the monorepo. - Package level - **`lint:tsconfigs`:** Validates `tsconfig.json` and `tsconfig.build.json` in the package, erroring if any are out of sync. - **`lint:tsconfigs:fix`:** Regenerates `tsconfig.json` and `tsconfig.build.json` in the package. As the names indicate, tsconfig file validation is also a part of the lint pipeline, so CI will now fail if any files are out of date. To make sure this doesn't happen in the future, this commit also corrects tsconfig files across the board to add missing references or remove extra references. ### Why hand-roll a solution? There are some existing solutions for syncing references within the TypeScript community, some of which are documented in [this issue][1]. However, I reviewed them and rejected them for various reasons: - **[`typescript-monorepo-toolkit`][2]:** Doesn't update references for individual packages based on `dependencies` - **[`update-ts-references`][3]:** Works, but re-sorts all references, and drops the leading `./` from references in root tsconfig files; also, generated files fail Prettier validation - **[`@monorepo-utils/workspaces-to-typescript-project-references`][4]:** Works, but re-sorts all references, and drops the leading `./` from references in root tsconfig files; also generated files fail Prettier validation My thought is that by building our own script, we get to control the exact changes that are made to tsconfig files, and we get to run all files through Oxfmt so that engineers do not have to reformat them manually. [1]: microsoft/TypeScript#25376 [2]: https://github.com/Bnaya/typescript-monorepo-toolkit [3]: https://github.com/eBayClassifiedsGroup/update-ts-references [4]: https://github.com/azu/monorepo-utils/tree/master/packages/@monorepo-utils/workspaces-to-typescript-project-references ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> Closes #966. Also closes https://consensyssoftware.atlassian.net/browse/WPC-649. ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them --------- Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com>
1 parent b3c1c37 commit 58cd8e9

193 files changed

Lines changed: 2246 additions & 387 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/lint-build-test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ jobs:
4242
- lint:teams
4343
- messenger-action-types:check
4444
- readme-content:check
45+
- lint:tsconfigs:all
4546
steps:
4647
- name: Checkout and setup environment
4748
uses: MetaMask/action-checkout-and-setup@v3

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ linkStyle default opacity:0.5
378378
earn_controller --> network_controller;
379379
earn_controller --> transaction_controller;
380380
eip_5792_middleware --> messenger;
381+
eip_5792_middleware --> preferences_controller;
381382
eip_5792_middleware --> transaction_controller;
382383
eip_5792_middleware --> keyring_controller;
383384
eip_7702_internal_rpc_middleware --> controller_utils;
@@ -412,6 +413,7 @@ linkStyle default opacity:0.5
412413
json_rpc_engine --> messenger;
413414
json_rpc_middleware_stream --> json_rpc_engine;
414415
keyring_controller --> base_controller;
416+
keyring_controller --> controller_utils;
415417
keyring_controller --> messenger;
416418
logging_controller --> base_controller;
417419
logging_controller --> controller_utils;

eslint.config.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ const config = createConfig([
116116
'**/docusaurus.config.ts',
117117
'**/test/**/*.{js,ts}',
118118
'**/tests/**/*.{js,ts}',
119-
'scripts/*.ts',
120-
'scripts/create-package/**/*.ts',
119+
'scripts/**/*.{ts,mts}',
121120
'packages/platform-api-docs/**/*.ts',
122121
],
122+
ignores: ['scripts/create-package/package-template/**/*.ts'],
123123
extends: [nodejs],
124124
},
125125
{
@@ -130,7 +130,7 @@ const config = createConfig([
130130
},
131131
},
132132
{
133-
files: ['**/*.ts'],
133+
files: ['**/*.ts', '**/*.mts'],
134134
extends: [typescript],
135135
languageOptions: {
136136
parserOptions: {

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,18 @@
2525
"docs:platform-api:build": "yarn workspace @metamask/platform-api-docs cli ../.. --build --project-label Core",
2626
"docs:platform-api:dev": "yarn workspace @metamask/platform-api-docs cli ../.. --dev --project-label Core",
2727
"docs:platform-api:serve": "yarn workspace @metamask/platform-api-docs cli ../.. --serve --project-label Core",
28-
"lint": "yarn lint:eslint && echo && yarn lint:misc --check && yarn constraints && yarn lint:dependencies && yarn lint:teams && yarn messenger-action-types:check && yarn readme-content:check",
28+
"lint": "yarn lint:eslint && echo && yarn lint:misc --check && yarn constraints && yarn lint:dependencies && yarn lint:teams && yarn messenger-action-types:check && yarn readme-content:check && yarn lint:tsconfigs:all",
2929
"lint:dependencies": "knip --dependencies && yarn dedupe --check",
3030
"lint:dependencies:fix": "knip --dependencies && yarn dedupe",
3131
"lint:eslint": "yarn build:only-clean && NODE_OPTIONS='--max-old-space-size=8192' yarn eslint",
3232
"lint:fix": "yarn lint:eslint --fix --prune-suppressions && echo && yarn lint:misc --write && yarn constraints --fix && yarn lint:dependencies:fix && yarn messenger-action-types:generate && yarn readme-content:update",
3333
"lint:misc": "oxfmt --ignore-path .gitignore",
3434
"lint:misc:check": "yarn lint:misc --check",
3535
"lint:teams": "tsx scripts/lint-teams-json.ts",
36+
"lint:tsconfigs": "tsx scripts/lint-tsconfigs/lint-tsconfigs.mts",
37+
"lint:tsconfigs:all": "yarn workspaces foreach --all --parallel --interlaced --verbose run lint:tsconfigs",
38+
"lint:tsconfigs:fix": "tsx scripts/lint-tsconfigs/lint-tsconfigs.mts --fix",
39+
"lint:tsconfigs:fix:all": "yarn workspaces foreach --all --parallel --interlaced --verbose run lint:tsconfigs:fix",
3640
"messenger-action-types:check": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run messenger-action-types:check",
3741
"messenger-action-types:generate": "yarn workspaces foreach --all --no-private --parallel --interlaced --verbose run messenger-action-types:generate",
3842
"postinstall": "yarn skills:postinstall",

packages/account-tree-controller/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"build:docs": "typedoc",
4545
"changelog:update": "../../scripts/update-changelog.sh @metamask/account-tree-controller",
4646
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/account-tree-controller",
47+
"lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts",
48+
"lint:tsconfigs:fix": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts --fix",
4749
"messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --check",
4850
"messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --generate",
4951
"since-latest-release": "../../scripts/since-latest-release.sh",

packages/accounts-controller/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"build:docs": "typedoc",
4545
"changelog:update": "../../scripts/update-changelog.sh @metamask/accounts-controller",
4646
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/accounts-controller",
47+
"lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts",
48+
"lint:tsconfigs:fix": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts --fix",
4749
"messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --check",
4850
"messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --generate",
4951
"since-latest-release": "../../scripts/since-latest-release.sh",

packages/accounts-controller/tsconfig.build.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@
1010
{
1111
"path": "../base-controller/tsconfig.build.json"
1212
},
13-
{ "path": "../keyring-controller/tsconfig.build.json" },
14-
{ "path": "../network-controller/tsconfig.build.json" },
15-
{ "path": "../messenger/tsconfig.build.json" }
13+
{
14+
"path": "../keyring-controller/tsconfig.build.json"
15+
},
16+
{
17+
"path": "../network-controller/tsconfig.build.json"
18+
},
19+
{
20+
"path": "../messenger/tsconfig.build.json"
21+
},
22+
{
23+
"path": "../controller-utils/tsconfig.build.json"
24+
}
1625
],
1726
"include": ["../../types", "./src"]
1827
}

packages/accounts-controller/tsconfig.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,15 @@
1010
{
1111
"path": "../keyring-controller"
1212
},
13-
{ "path": "../network-controller" },
14-
{ "path": "../messenger" }
13+
{
14+
"path": "../network-controller"
15+
},
16+
{
17+
"path": "../messenger"
18+
},
19+
{
20+
"path": "../controller-utils"
21+
}
1522
],
1623
"include": ["../../types", "./src", "./tests"]
1724
}

packages/address-book-controller/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"build:docs": "typedoc",
4545
"changelog:update": "../../scripts/update-changelog.sh @metamask/address-book-controller",
4646
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/address-book-controller",
47+
"lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts",
48+
"lint:tsconfigs:fix": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts --fix",
4749
"messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --check",
4850
"messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --generate",
4951
"since-latest-release": "../../scripts/since-latest-release.sh",

packages/ai-controllers/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
"build:docs": "typedoc",
4545
"changelog:update": "../../scripts/update-changelog.sh @metamask/ai-controllers",
4646
"changelog:validate": "../../scripts/validate-changelog.sh @metamask/ai-controllers",
47+
"lint:tsconfigs": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts",
48+
"lint:tsconfigs:fix": "tsx ../../scripts/lint-tsconfigs/lint-tsconfigs.mts --fix",
4749
"messenger-action-types:check": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --check",
4850
"messenger-action-types:generate": "tsx ../../packages/messenger-cli/src/cli.ts --formatter oxfmt --generate",
4951
"since-latest-release": "../../scripts/since-latest-release.sh",

0 commit comments

Comments
 (0)