Skip to content

Commit 5b5e422

Browse files
authored
Fix relative TypeScript path in monorepos (vercel#42586)
When under a monorepo, it's possible that the installed TypeScript isn't under `./node_modules/typescript`, and hence the `tsdk` option for VS Code won't work correctly. This PR fixes that to ensure the path is correct. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
1 parent 0341fb7 commit 5b5e422

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

packages/next/lib/typescript/writeVscodeConfigurations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import * as CommentJson from 'next/dist/compiled/comment-json'
66

77
// Write .vscode settings to enable Next.js typescript plugin.
88
export async function writeVscodeConfigurations(
9-
baseDir: string
9+
baseDir: string,
10+
tsPath: string
1011
): Promise<void> {
1112
try {
1213
const vscodeSettings = path.join(baseDir, '.vscode', 'settings.json')
@@ -24,7 +25,7 @@ export async function writeVscodeConfigurations(
2425
}
2526
}
2627

27-
const libPath = './node_modules/typescript/lib'
28+
const libPath = path.relative(baseDir, path.dirname(tsPath))
2829
if (
2930
settings['typescript.tsdk'] === libPath &&
3031
settings['typescript.enablePromptUseWorkspaceTsdk']

packages/next/lib/verifyTypeScriptSetup.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ export async function verifyTypeScriptSetup({
103103
}
104104

105105
// Load TypeScript after we're sure it exists:
106+
const tsPath = deps.resolved.get('typescript')!
106107
const ts = (await Promise.resolve(
107-
require(deps.resolved.get('typescript')!)
108+
require(tsPath)
108109
)) as typeof import('typescript')
109110

110111
if (semver.lt(ts.version, '4.3.2')) {
@@ -125,7 +126,7 @@ export async function verifyTypeScriptSetup({
125126
await writeAppTypeDeclarations(dir, !disableStaticImages)
126127

127128
if (isAppDirEnabled) {
128-
await writeVscodeConfigurations(dir)
129+
await writeVscodeConfigurations(dir, tsPath)
129130
}
130131

131132
let result

0 commit comments

Comments
 (0)