Skip to content

Commit 3d6e12f

Browse files
authored
fix: more robust solution project check for ts < 5.7 in ts plugin (#2758)
#2756 The problem is that in TypeScript before 5.7, the project files were added later than plugin initialisation. Thus, the IsSolution method will always return true in any project using project references during plugin initialisation.
1 parent bc9a3fd commit 3d6e12f

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

packages/typescript-plugin/src/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import type ts from 'typescript/lib/tsserverlibrary';
77
import { ConfigManager, Configuration } from './config-manager';
88
import { ProjectSvelteFilesManager } from './project-svelte-files';
99
import {
10-
getConfigPathForProject,
1110
getProjectDirectory,
11+
getProjectParsedCommandLine,
1212
importSvelteCompiler,
1313
isSvelteProject
1414
} from './utils';
@@ -47,11 +47,7 @@ function init(modules: { typescript: typeof ts }): ts.server.PluginModule {
4747
logger.log(info.config);
4848
}
4949

50-
// This call the ConfiguredProject.getParsedCommandLine
51-
// where it'll try to load the cached version of the parsedCommandLine
52-
const parsedCommandLine = info.languageServiceHost.getParsedCommandLine?.(
53-
getConfigPathForProject(info.project)
54-
);
50+
const parsedCommandLine = getProjectParsedCommandLine(info.project);
5551

5652
// For some reason it's no longer enough to patch this at the projectService level, so we do it here, too
5753
// TODO investigate if we can use the script snapshot for all Svelte files, too, enabling Svelte file

packages/typescript-plugin/src/utils.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,12 @@ export function isSvelteProject(project: ts.server.Project) {
260260
// The solution project is mostly just a container we don't need to patch it
261261
// and having any files in this project cause TSServer to send config error while it originally won't
262262
if ((project as any).isSolution?.()) {
263-
return false;
263+
// In TypeScript before 5.7, the project files were added later than plugin loading
264+
// so we need to also check if the parsedCommandLine includes any files
265+
const parsedCommandLine = getProjectParsedCommandLine(project);
266+
if (parsedCommandLine?.fileNames.length === 0) {
267+
return false;
268+
}
264269
}
265270

266271
const projectDirectory = getProjectDirectory(project);
@@ -313,3 +318,16 @@ export function importSvelteCompiler(
313318
// ignore
314319
}
315320
}
321+
322+
/**
323+
* This call the ConfiguredProject.getParsedCommandLine
324+
* where it'll try to load the cached version of the parsedCommandLine
325+
*/
326+
export function getProjectParsedCommandLine(project: ts.server.Project) {
327+
const configPath = getConfigPathForProject(project);
328+
const parsedCommandLine = (project as ts.LanguageServiceHost).getParsedCommandLine?.(
329+
configPath
330+
);
331+
332+
return parsedCommandLine;
333+
}

0 commit comments

Comments
 (0)