Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin-typescript): check workspace tsconfig.json #6175

Merged
merged 9 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .yarn/versions/dca3f277.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
releases:
"@yarnpkg/cli": minor
"@yarnpkg/plugin-typescript": minor

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe(`Plugins`, () => {
);

test(
`it should automatically enable automatic @types insertion when a tsconfig is detected`,
`it should automatically enable automatic @types insertion when a tsconfig is detected at the root of the project`,
makeTemporaryEnv({}, async ({path, run, source}) => {
await xfs.writeFilePromise(ppath.join(path, `tsconfig.json`), ``);

Expand All @@ -44,6 +44,66 @@ describe(`Plugins`, () => {
}),
);

test(
`it should automatically enable automatic @types insertion when a tsconfig is detected in the current workspace`,
makeTemporaryMonorepoEnv({
workspaces: [`packages/*`],
}, {[`packages/foo`]: {}}, async ({path, run, source}) => {
await xfs.writeFilePromise(ppath.join(path, `packages/foo/tsconfig.json`), ``);

await run(`add`, `is-number`, {
cwd: `${path}/packages/foo` as PortablePath,
});

await expect(readManifest(`${path}/packages/foo` as PortablePath)).resolves.toMatchObject({
dependencies: {
[`is-number`]: `^2.0.0`,
},
devDependencies: {
[`@types/is-number`]: `^2`,
},
});
}),
);

test(
`it should not automatically enable automatic @types insertion when a tsconfig is present in a sibling workspace`,
makeTemporaryMonorepoEnv({
workspaces: [`packages/*`],
}, {[`packages/foo`]: {}, [`packages/bar`]: {}}, async ({path, run, source}) => {
await xfs.writeFilePromise(ppath.join(path, `packages/foo/tsconfig.json`), ``);

await run(`add`, `is-number`, {
cwd: `${path}/packages/bar` as PortablePath,
});

await expect(readManifest(`${path}/packages/bar` as PortablePath)).resolves.toMatchObject({
dependencies: {
[`is-number`]: `^2.0.0`,
},
});
}),
);

test(
`it should not automatically enable automatic @types insertion when a tsconfig is detected in the root project of the current workspace`,
nmussy marked this conversation as resolved.
Show resolved Hide resolved
makeTemporaryMonorepoEnv({
workspaces: [`packages/*`],
}, {[`packages/foo`]: {}}, async ({path, run, source}) => {
await xfs.writeFilePromise(ppath.join(path, `tsconfig.json`), ``);

await run(`add`, `is-number`, {
cwd: `${path}/packages/foo` as PortablePath,
});

await expect(readManifest(`${path}/packages/foo` as PortablePath)).resolves.toMatchObject({
dependencies: {
[`is-number`]: `^2.0.0`,
},
});
}),
);

test(
`it should automatically add @types to devDependencies when package doesn't provide types`,
makeTemporaryEnv({}, {
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus/static/configuration/yarnrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@
"tsEnableAutoTypes": {
"_package": "@yarnpkg/plugin-typescript",
"title": "Define whether to automatically install @types dependencies.",
"description": "If true, Yarn will automatically add `@types` dependencies when running `yarn add` with packages that don't provide their own typings (as reported by the Algolia npm database). This behavior is enabled by default if you have a tsconfig file at the root of your project.",
"description": "If true, Yarn will automatically add `@types` dependencies when running `yarn add` with packages that don't provide their own typings (as reported by the Algolia npm database). This behavior is enabled by default if you have a tsconfig.json file at the root of your project, or in your current workspace.",
"type": "boolean",
"examples": [true]
},
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

This plugin is included by default starting from Yarn 4.

## Configuration

This plugin is enabled by default if you have a `tsconfig.json` file at the root of your project, or in your current workspace. See [`tsEnableAutoTypes`](https://yarnpkg.com/configuration/yarnrc#tsEnableAutoTypes) for more information.

## Example

```
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-typescript/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const afterWorkspaceDependencyAddition = async (
const {configuration} = project;

const tsEnableAutoTypes = configuration.get(`tsEnableAutoTypes`)
?? xfs.existsSync(ppath.join(workspace.cwd, `tsconfig.json`))
?? xfs.existsSync(ppath.join(project.cwd, `tsconfig.json`));
Copy link
Member

@merceyz merceyz Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
?? xfs.existsSync(ppath.join(workspace.cwd, `tsconfig.json`))
?? xfs.existsSync(ppath.join(project.cwd, `tsconfig.json`));
?? (xfs.existsSync(ppath.join(workspace.cwd, `tsconfig.json`))
|| xfs.existsSync(ppath.join(project.cwd, `tsconfig.json`)));

Otherwise it doesn't check the project root as well, if that's on purpose then:

Suggested change
?? xfs.existsSync(ppath.join(workspace.cwd, `tsconfig.json`))
?? xfs.existsSync(ppath.join(project.cwd, `tsconfig.json`));
?? xfs.existsSync(ppath.join(workspace.cwd, `tsconfig.json`));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, definitely a bug, it was causing #6175 (comment). I'm still not sure if I should re-introduce it though. If you're in a monorepo, and do not have a tsconfig.json in one of your workspaces, it might be safe to assume you're not using TypeScript in that specific workspace

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're in a monorepo, and do not have a tsconfig.json in one of your workspaces, it might be safe to assume you're not using TypeScript in that specific workspace

Not necessarily, this repo is an example of that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright thanks, that settles it then, it would have been an odd behavior anyway 👍


if (!tsEnableAutoTypes)
Expand Down
Loading