Skip to content

Commit

Permalink
fix(sdks): only patch typescript entry point for >= 5.5 (#6263)
Browse files Browse the repository at this point in the history
**What's the problem this PR addresses?**

The SDK changes in #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.
  • Loading branch information
merceyz authored May 2, 2024
1 parent df5d1c2 commit f994c2b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
7 changes: 6 additions & 1 deletion .yarn/sdks/typescript/lib/tsserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`));
7 changes: 6 additions & 1 deletion .yarn/sdks/typescript/lib/tsserverlibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`));
2 changes: 2 additions & 0 deletions .yarn/versions/f094b4b7.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
"@yarnpkg/sdks": patch
7 changes: 6 additions & 1 deletion packages/yarnpkg-sdks/sources/sdks/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down

0 comments on commit f994c2b

Please sign in to comment.