Skip to content

Commit b66d51a

Browse files
committed
fix(server): resolve tsdk correctly when settings specify a relative location (#1765)
Previously, we only supported absolute paths for `tsdk` specified in the settings.json. However, with Yarn PnP, this might instead be a path relative to the project/workspace. For example, in a a project using Yarn PnP, the tsdk setting might be ``` "typescript.tsdk": ".yarn/sdks/typescript/lib", ``` The user would then run `yarn dlx @yarnpkg/sdks vscode` and the typescript modules would then exist in the project folder `.yarn/sdks/typescript/...`. We should then resolve tsdk as a relative path from the given probe locations, just like we do when looking for other packages in a regular project (in the `node_modules`, relative to the given probe locations). Fixes #1748 (cherry picked from commit 05ef44e)
1 parent 9e6187f commit b66d51a

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

server/src/version_provider.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -50,39 +50,46 @@ function resolveWithMinVersion(
5050
export function resolveTsServer(probeLocations: string[]): NodeModule {
5151
if (probeLocations.length > 0) {
5252
// The first probe location is `typescript.tsdk` if it is specified.
53-
const resolvedFromTsdk = resolveTsServerFromTsdk(probeLocations[0]);
53+
const resolvedFromTsdk = resolveTsServerFromTsdk(probeLocations[0], probeLocations.slice(1));
5454
if (resolvedFromTsdk !== undefined) {
5555
return resolvedFromTsdk;
5656
}
5757
}
5858
return resolveWithMinVersion(TSSERVERLIB, MIN_TS_VERSION, probeLocations, 'typescript');
5959
}
6060

61-
function resolveTsServerFromTsdk(tsdk: string): NodeModule|undefined {
61+
function resolveTsServerFromTsdk(tsdk: string, probeLocations: string[]): NodeModule|undefined {
6262
// `tsdk` is the folder path to the tsserver and lib*.d.ts files under a
6363
// TypeScript install, for example
6464
// - /google/src/head/depot/google3/third_party/javascript/node_modules/typescript/stable/lib
65-
if (!path.isAbsolute(tsdk)) {
66-
return undefined;
65+
// When the `tsdk` is an absolute path, we only look there for TS Server.
66+
// When it is a relative path, we look for that tsdk relative to the rest of the probe locations.
67+
if (path.isAbsolute(tsdk)) {
68+
probeLocations = [tsdk];
69+
} else {
70+
probeLocations = probeLocations.map(location => path.join(location, tsdk));
6771
}
68-
const tsserverlib = path.join(tsdk, 'tsserverlibrary.js');
69-
if (!fs.existsSync(tsserverlib)) {
70-
return undefined;
71-
}
72-
const packageJson = path.resolve(tsserverlib, '../../package.json');
73-
if (!fs.existsSync(packageJson)) {
74-
return undefined;
75-
}
76-
try {
77-
const json = JSON.parse(fs.readFileSync(packageJson, 'utf8'));
78-
return {
79-
name: TSSERVERLIB,
80-
resolvedPath: tsserverlib,
81-
version: new Version(json.version),
82-
};
83-
} catch {
84-
return undefined;
72+
for (const location of probeLocations) {
73+
const tsserverlib = path.join(location, 'tsserverlibrary.js');
74+
if (!fs.existsSync(tsserverlib)) {
75+
continue;
76+
}
77+
const packageJson = path.resolve(tsserverlib, '../../package.json');
78+
if (!fs.existsSync(packageJson)) {
79+
continue;
80+
}
81+
try {
82+
const json = JSON.parse(fs.readFileSync(packageJson, 'utf8'));
83+
return {
84+
name: TSSERVERLIB,
85+
resolvedPath: tsserverlib,
86+
version: new Version(json.version),
87+
};
88+
} catch {
89+
continue;
90+
}
8591
}
92+
return undefined;
8693
}
8794

8895
/**

0 commit comments

Comments
 (0)