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
26 changes: 19 additions & 7 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const serviceSizeMap = new FileMap<number>();
const configWatchers = new FileMap<ts.FileWatcher>();
const extendedConfigWatchers = new FileMap<ts.FileWatcher>();
const extendedConfigToTsConfigPath = new FileMap<FileSet>();
const configFileForOpenFiles = new FileMap<string>();
const pendingReloads = new FileSet();

/**
Expand Down Expand Up @@ -81,24 +82,32 @@ export async function getService(
docContext.tsSystem.useCaseSensitiveFileNames
);

const tsconfigPath = findTsConfigPath(
path,
workspaceUris,
docContext.tsSystem.fileExists,
getCanonicalFileName
);
const tsconfigPath =
configFileForOpenFiles.get(path) ??
findTsConfigPath(path, workspaceUris, docContext.tsSystem.fileExists, getCanonicalFileName);

if (tsconfigPath) {
configFileForOpenFiles.set(path, tsconfigPath);
return getServiceForTsconfig(tsconfigPath, dirname(tsconfigPath), docContext);
}

// Find closer boundary: workspace uri or node_modules
const nearestWorkspaceUri = workspaceUris.find((workspaceUri) =>
isSubPath(workspaceUri, path, getCanonicalFileName)
);
const lastNodeModulesIdx = path.split('/').lastIndexOf('node_modules') + 2;
const nearestNodeModulesBoundary =
lastNodeModulesIdx === 1
? undefined
: path.split('/').slice(0, lastNodeModulesIdx).join('/');
const nearestBoundary =
(nearestNodeModulesBoundary?.length ?? 0) > (nearestWorkspaceUri?.length ?? 0)
? nearestNodeModulesBoundary
: nearestWorkspaceUri;

return getServiceForTsconfig(
tsconfigPath,
(nearestWorkspaceUri && urlToPath(nearestWorkspaceUri)) ??
(nearestBoundary && urlToPath(nearestBoundary)) ??
docContext.tsSystem.getCurrentDirectory(),
docContext
);
Expand Down Expand Up @@ -243,6 +252,7 @@ async function createLanguageService(
function deleteSnapshot(filePath: string): void {
svelteModuleLoader.deleteFromModuleCache(filePath);
snapshotManager.delete(filePath);
configFileForOpenFiles.delete(filePath);
}

function updateSnapshot(documentOrFilePath: Document | string): DocumentSnapshot {
Expand Down Expand Up @@ -517,6 +527,7 @@ async function createLanguageService(
snapshotManager.dispose();
configWatchers.get(tsconfigPath)?.close();
configWatchers.delete(tsconfigPath);
configFileForOpenFiles.clear();
docContext.globalSnapshotsManager.removeChangeListener(onSnapshotChange);
}

Expand Down Expand Up @@ -565,6 +576,7 @@ async function createLanguageService(
scheduleReload(fileName);
} else if (kind === ts.FileWatcherEventKind.Deleted) {
services.delete(fileName);
configFileForOpenFiles.clear();
}

docContext.onProjectReloaded?.();
Expand Down
20 changes: 13 additions & 7 deletions packages/language-server/src/plugins/typescript/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,19 @@ export function findTsConfigPath(
) {
const searchDir = dirname(fileName);

const path =
ts.findConfigFile(searchDir, fileExists, 'tsconfig.json') ||
ts.findConfigFile(searchDir, fileExists, 'jsconfig.json') ||
'';
// Don't return config files that exceed the current workspace context.
return !!path && rootUris.some((rootUri) => isSubPath(rootUri, path, getCanonicalFileName))
? path
const tsconfig = ts.findConfigFile(searchDir, fileExists, 'tsconfig.json') || '';
const jsconfig = ts.findConfigFile(searchDir, fileExists, 'jsconfig.json') || '';
// Prefer closest config file
const config = tsconfig.length >= jsconfig.length ? tsconfig : jsconfig;

// Don't return config files that exceed the current workspace context or cross a node_modules folder
return !!config &&
rootUris.some((rootUri) => isSubPath(rootUri, config, getCanonicalFileName)) &&
!fileName
.substring(config.length - 13)
.split('/')
.includes('node_modules')
? config
: '';
}

Expand Down