Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ export class LSAndTSDocResolver {
* @param docManager
* @param workspaceUris
* @param configManager
* @param notifyExceedSizeLimit
* @param isSvelteCheck True, if used in the context of svelte-check
* @param tsconfigPath This should only be set via svelte-check. Makes sure all documents are resolved to that tsconfig. Has to be absolute.
*/
constructor(
private readonly docManager: DocumentManager,
private readonly workspaceUris: string[],
private readonly configManager: LSConfigManager,
private readonly notifyExceedSizeLimit?: () => void,
private readonly isSvelteCheck = false,
private readonly tsconfigPath?: string
) {
Expand Down Expand Up @@ -69,7 +71,8 @@ export class LSAndTSDocResolver {
ambientTypesSource: this.isSvelteCheck ? 'svelte-check' : 'svelte2tsx',
createDocument: this.createDocument,
transformOnTemplateError: !this.isSvelteCheck,
globalSnapshotsManager: this.globalSnapshotsManager
globalSnapshotsManager: this.globalSnapshotsManager,
notifyExceedSizeLimit: this.notifyExceedSizeLimit
};
}

Expand Down
83 changes: 81 additions & 2 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ignoredBuildDirectories,
SnapshotManager
} from './SnapshotManager';
import { ensureRealSvelteFilePath, findTsConfigPath } from './utils';
import { ensureRealSvelteFilePath, findTsConfigPath, hasTsExtensions } from './utils';

export interface LanguageServiceContainer {
readonly tsconfigPath: string;
Expand All @@ -39,13 +39,16 @@ export interface LanguageServiceContainer {
fileBelongsToProject(filePath: string): boolean;
}

const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; // 20 MB
const services = new Map<string, Promise<LanguageServiceContainer>>();
const serviceSizeMap: Map<string, number> = new Map();

export interface LanguageServiceDocumentContext {
ambientTypesSource: string;
transformOnTemplateError: boolean;
createDocument: (fileName: string, content: string) => Document;
globalSnapshotsManager: GlobalSnapshotsManager;
notifyExceedSizeLimit: (() => void) | undefined;
}

export async function getService(
Expand Down Expand Up @@ -123,12 +126,14 @@ async function createLanguageService(
'./svelte-native-jsx.d.ts'
].map((f) => ts.sys.resolvePath(resolve(svelteTsPath, f)));

let languageServiceReducedMode = false;

const host: ts.LanguageServiceHost = {
getCompilationSettings: () => compilerOptions,
getScriptFileNames: () =>
Array.from(
new Set([
...snapshotManager.getProjectFileNames(),
...(languageServiceReducedMode ? [] : snapshotManager.getProjectFileNames()),
...snapshotManager.getFileNames(),
...svelteTsxFiles
])
Expand All @@ -150,6 +155,8 @@ async function createLanguageService(
transformOnTemplateError: docContext.transformOnTemplateError
};

reduceLanguageServiceCapabilityIfFileSizeTooBig();

return {
tsconfigPath,
compilerOptions,
Expand Down Expand Up @@ -232,7 +239,15 @@ async function createLanguageService(
}

function updateProjectFiles(): void {
const projectFileCountBefore = snapshotManager.getProjectFileNames().length;
snapshotManager.updateProjectFiles();
const projectFileCountAfter = snapshotManager.getProjectFileNames().length;

if (projectFileCountAfter <= projectFileCountBefore) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return;
}

reduceLanguageServiceCapabilityIfFileSizeTooBig();
}

function hasFile(filePath: string): boolean {
Expand Down Expand Up @@ -350,4 +365,68 @@ async function createLanguageService(
function getDefaultExclude() {
return ['node_modules', ...ignoredBuildDirectories];
}

/**
* Disable usage of project files.
* running language service in a reduced mode for
* large projects with improperly excluded tsconfig.
*/
function reduceLanguageServiceCapabilityIfFileSizeTooBig() {
if (exceedsTotalSizeLimitForNonTsFiles(compilerOptions, tsconfigPath, snapshotManager)) {
languageService.cleanupSemanticCache();
languageServiceReducedMode = true;
docContext.notifyExceedSizeLimit?.();
}
}
}

/**
* adopted from https://github.com/microsoft/TypeScript/blob/3c8e45b304b8572094c5d7fbb9cd768dbf6417c0/src/server/editorServices.ts#L1955
*/
function exceedsTotalSizeLimitForNonTsFiles(
compilerOptions: ts.CompilerOptions,
tsconfigPath: string,
snapshotManager: SnapshotManager
): boolean {
if (compilerOptions.disableSizeLimit) {
return false;
}

let availableSpace = maxProgramSizeForNonTsFiles;
serviceSizeMap.set(tsconfigPath, 0);

serviceSizeMap.forEach((size) => {
availableSpace -= size;
});

let totalNonTsFileSize = 0;

const fileNames = snapshotManager.getProjectFileNames();
for (const fileName of fileNames) {
if (hasTsExtensions(fileName)) {
continue;
}

totalNonTsFileSize += ts.sys.getFileSize?.(fileName) ?? 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity: Is this performance heavy to check that? How often is this called? My fear is that if this results in many IO operations, that it slows down the intellisense for people.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, it only runs on startup and when there're new .ts or .js files added. With the file-count-check, it should not be very often. It's 10-20ms on my SSD with 300-500 project files workspaces, and 20-30ms on a hard drive. So I guess it shouldn't be too noticeable to most people.


if (totalNonTsFileSize > availableSpace) {
const top5LargestFiles = fileNames
.filter((name) => !hasTsExtensions(name))
.map((name) => ({ name, size: ts.sys.getFileSize?.(name) ?? 0 }))
.sort((a, b) => b.size - a.size)
.slice(0, 5);

Logger.log(
`Non TS file size exceeded limit (${totalNonTsFileSize}). ` +
`Largest files: ${top5LargestFiles
.map((file) => `${file.name}:${file.size}`)
.join(', ')}`
);

return true;
}
}

serviceSizeMap.set(tsconfigPath, totalNonTsFileSize);
return false;
}
8 changes: 8 additions & 0 deletions packages/language-server/src/plugins/typescript/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,11 @@ export function getDiagnosticTag(diagnostic: ts.Diagnostic): DiagnosticTag[] {
}
return tags;
}

export function hasTsExtensions(fileName: string) {
return (
fileName.endsWith(ts.Extension.Dts) ||
fileName.endsWith(ts.Extension.Tsx) ||
fileName.endsWith(ts.Extension.Ts)
);
}
17 changes: 16 additions & 1 deletion packages/language-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,12 @@ export function startServer(options?: LSOptions) {
pluginHost.register(
new TypeScriptPlugin(
configManager,
new LSAndTSDocResolver(docManager, workspaceUris.map(normalizeUri), configManager)
new LSAndTSDocResolver(
docManager,
workspaceUris.map(normalizeUri),
configManager,
notifyTsServiceExceedSizeLimit
)
)
);

Expand Down Expand Up @@ -241,6 +246,16 @@ export function startServer(options?: LSOptions) {
};
});

function notifyTsServiceExceedSizeLimit() {
connection?.sendNotification(ShowMessageNotification.type, {
message:
'Svelte language server detected a large amount of JS/Svelte files. ' +
'To enable project-wide JavaScript/TypeScript language features for Svelte files,' +
'exclude large folders in the tsconfig.json or jsconfig.json with source files that you do not work on.',
type: MessageType.Warning
});
}

connection.onExit(() => {
watcher?.dispose();
});
Expand Down
1 change: 1 addition & 0 deletions packages/language-server/src/svelte-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class SvelteCheck {
this.docManager,
[pathToUrl(workspacePath)],
this.configManager,
undefined,
true,
options.tsconfig
);
Expand Down