From f994c2bdbc498b08501209b7a7fcdde8e8590f3a Mon Sep 17 00:00:00 2001 From: Kristoffer K Date: Thu, 2 May 2024 11:01:28 +0200 Subject: [PATCH] fix(sdks): only patch typescript entry point for >= 5.5 (#6263) **What's the problem this PR addresses?** The SDK changes in https://github.com/yarnpkg/berry/pull/6248 broke support for older TypeScript versions. I tested it on `5.4.1-rc` (master) and it was fine but it crashes on 5.2.0-beta (https://github.com/yarnpkg/berry/blob/4308dca8091438e8f88682e59ef5ba5bc72241ca/package.json#L26) **How did you fix it?** Check the TypeScript version and only apply the patch if needed. **Checklist** - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). - [x] I have set the packages that need to be released for my changes to be effective. - [x] I will check that all automated PR checks pass before the PR gets reviewed. --- .yarn/sdks/typescript/lib/tsserver.js | 7 ++++++- .yarn/sdks/typescript/lib/tsserverlibrary.js | 7 ++++++- .yarn/versions/f094b4b7.yml | 2 ++ packages/yarnpkg-sdks/sources/sdks/base.ts | 7 ++++++- 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 .yarn/versions/f094b4b7.yml diff --git a/.yarn/sdks/typescript/lib/tsserver.js b/.yarn/sdks/typescript/lib/tsserver.js index e8acd929c014..ed800750cc18 100644 --- a/.yarn/sdks/typescript/lib/tsserver.js +++ b/.yarn/sdks/typescript/lib/tsserver.js @@ -221,7 +221,12 @@ const moduleWrapper = tsserver => { return tsserver; }; -moduleWrapper(absRequire(`typescript`)); +const [major, minor] = absRequire(`typescript/package.json`).version.split(`.`, 2).map(value => parseInt(value, 10)); +// In TypeScript@>=5.5 the tsserver uses the public TypeScript API so that needs to be patched as well. +// Ref https://github.com/microsoft/TypeScript/pull/55326 +if (major > 5 || (major === 5 && minor >= 5)) { + moduleWrapper(absRequire(`typescript`)); +} // Defer to the real typescript/lib/tsserver.js your application uses module.exports = moduleWrapper(absRequire(`typescript/lib/tsserver.js`)); diff --git a/.yarn/sdks/typescript/lib/tsserverlibrary.js b/.yarn/sdks/typescript/lib/tsserverlibrary.js index 4c3db8f57db6..4d99766952f5 100644 --- a/.yarn/sdks/typescript/lib/tsserverlibrary.js +++ b/.yarn/sdks/typescript/lib/tsserverlibrary.js @@ -221,7 +221,12 @@ const moduleWrapper = tsserver => { return tsserver; }; -moduleWrapper(absRequire(`typescript`)); +const [major, minor] = absRequire(`typescript/package.json`).version.split(`.`, 2).map(value => parseInt(value, 10)); +// In TypeScript@>=5.5 the tsserver uses the public TypeScript API so that needs to be patched as well. +// Ref https://github.com/microsoft/TypeScript/pull/55326 +if (major > 5 || (major === 5 && minor >= 5)) { + moduleWrapper(absRequire(`typescript`)); +} // Defer to the real typescript/lib/tsserverlibrary.js your application uses module.exports = moduleWrapper(absRequire(`typescript/lib/tsserverlibrary.js`)); diff --git a/.yarn/versions/f094b4b7.yml b/.yarn/versions/f094b4b7.yml new file mode 100644 index 000000000000..49cfdd45ad88 --- /dev/null +++ b/.yarn/versions/f094b4b7.yml @@ -0,0 +1,2 @@ +releases: + "@yarnpkg/sdks": patch diff --git a/packages/yarnpkg-sdks/sources/sdks/base.ts b/packages/yarnpkg-sdks/sources/sdks/base.ts index 821bf18231a0..ddbb8855e4a7 100644 --- a/packages/yarnpkg-sdks/sources/sdks/base.ts +++ b/packages/yarnpkg-sdks/sources/sdks/base.ts @@ -250,7 +250,12 @@ export const generateTypescriptBaseWrapper: GenerateBaseWrapper = async (pnpApi: return tsserver; }; - moduleWrapper(absRequire(\`typescript\`)); + const [major, minor] = absRequire(\`typescript/package.json\`).version.split(\`.\`, 2).map(value => parseInt(value, 10)); + // In TypeScript@>=5.5 the tsserver uses the public TypeScript API so that needs to be patched as well. + // Ref https://github.com/microsoft/TypeScript/pull/55326 + if (major > 5 || (major === 5 && minor >= 5)) { + moduleWrapper(absRequire(\`typescript\`)); + } `; const wrapper = new Wrapper(`typescript` as PortablePath, {pnpApi, target});