Skip to content

Commit

Permalink
Avoid overwriting pre-existing VSCode settings (#45311)
Browse files Browse the repository at this point in the history
## Bug

- [x] Fixes #42419

---

This PR checks to see if the VSCode setting for `typescript.tsdk` has already been set, and avoids overwriting if so. This is needed for Yarn Berry PNP (and likely other uses) that do not use `./node_modules/typescript/lib` as the path for the Typescript SDK.
  • Loading branch information
cravend committed Feb 3, 2023
1 parent eedceaf commit 0d7f765
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/next/src/lib/typescript/writeVscodeConfigurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,28 @@ export async function writeVscodeConfigurations(
try {
const vscodeSettings = path.join(baseDir, '.vscode', 'settings.json')
let settings: any = {}
let configExisted = false
let configExists = false
let currentContent: string = ''

try {
currentContent = await fs.readFile(vscodeSettings, 'utf8')
settings = CommentJson.parse(currentContent)
configExisted = true
configExists = true
} catch (err) {
if (isError(err) && err.code !== 'ENOENT') {
throw err
}
}

const libPath = path.relative(baseDir, path.dirname(tsPath))
if (
settings['typescript.tsdk'] === libPath &&
settings['typescript.tsdk'] &&
settings['typescript.enablePromptUseWorkspaceTsdk']
) {
return
}

const libPath = path.relative(baseDir, path.dirname(tsPath))

settings['typescript.tsdk'] = libPath
settings['typescript.enablePromptUseWorkspaceTsdk'] = true

Expand All @@ -48,7 +49,7 @@ export async function writeVscodeConfigurations(

Log.info(
`VS Code settings.json has been ${
configExisted ? 'updated' : 'created'
configExists ? 'updated' : 'created'
} for Next.js' automatic app types, this file can be added to .gitignore if desired`
)
} catch (err) {
Expand Down

0 comments on commit 0d7f765

Please sign in to comment.