Skip to content

Commit 23a03cb

Browse files
Cancel initial file search if it takes too long (#1242)
Fixes #986 (hopefully) There's a lot of detail about the problem in the linked issue and my response at the bottom. But briefly: - Searches can take way too long; even spinning "forever" for some people - This occurs more often if following symlinks is enabled in VSCode - We can cancel in-flight searches This PR starts the searches and cancels them after ~15s. If the extension hasn't found anything within 15s then it's likely it'll take ~15s any time you open that project and that would've been an absolutely horrible dev experience that we probably would've been notified about. It seems reasonable to set a time limit on the search do determine if we need to start a language server
1 parent c8efeca commit 23a03cb

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

packages/vscode-tailwindcss/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Prerelease
44

55
- v4: Support loading bundled versions of some first-party plugins ([#1240](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1240))
6+
- Cancel initial file search if it takes too long ([#1242](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1242))
67

78
# 0.14.8
89

packages/vscode-tailwindcss/src/extension.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type {
66
ConfigurationScope,
77
WorkspaceConfiguration,
88
Selection,
9+
CancellationToken,
910
} from 'vscode'
1011
import {
1112
workspace as Workspace,
@@ -16,6 +17,7 @@ import {
1617
Position,
1718
Range,
1819
RelativePattern,
20+
CancellationTokenSource,
1921
} from 'vscode'
2022
import type {
2123
DocumentFilter,
@@ -581,26 +583,45 @@ export async function activate(context: ExtensionContext) {
581583
return
582584
}
583585

584-
if (!(await anyFolderNeedsLanguageServer(Workspace.workspaceFolders ?? []))) {
586+
let source: CancellationTokenSource | null = new CancellationTokenSource()
587+
source.token.onCancellationRequested(() => {
588+
source?.dispose()
589+
source = null
590+
outputChannel.appendLine(
591+
'Server was not started. Search for Tailwind CSS-related files was taking too long.',
592+
)
593+
})
594+
595+
// Cancel the search after roughly 15 seconds
596+
setTimeout(() => source?.cancel(), 15_000)
597+
598+
if (!(await anyFolderNeedsLanguageServer(Workspace.workspaceFolders ?? [], source!.token))) {
599+
source?.dispose()
585600
return
586601
}
587602

603+
source?.dispose()
604+
588605
await bootWorkspaceClient()
589606
}
590607

591608
async function anyFolderNeedsLanguageServer(
592609
folders: readonly WorkspaceFolder[],
610+
token: CancellationToken,
593611
): Promise<boolean> {
594612
for (let folder of folders) {
595-
if (await folderNeedsLanguageServer(folder)) {
613+
if (await folderNeedsLanguageServer(folder, token)) {
596614
return true
597615
}
598616
}
599617

600618
return false
601619
}
602620

603-
async function folderNeedsLanguageServer(folder: WorkspaceFolder): Promise<boolean> {
621+
async function folderNeedsLanguageServer(
622+
folder: WorkspaceFolder,
623+
token: CancellationToken,
624+
): Promise<boolean> {
604625
let settings = Workspace.getConfiguration('tailwindCSS', folder)
605626
if (settings.get('experimental.configFile') !== null) {
606627
return true
@@ -616,13 +637,19 @@ export async function activate(context: ExtensionContext) {
616637
new RelativePattern(folder, `**/${CONFIG_GLOB}`),
617638
exclude,
618639
1,
640+
token,
619641
)
620642

621643
for (let file of configFiles) {
622644
return true
623645
}
624646

625-
let cssFiles = await Workspace.findFiles(new RelativePattern(folder, `**/${CSS_GLOB}`), exclude)
647+
let cssFiles = await Workspace.findFiles(
648+
new RelativePattern(folder, `**/${CSS_GLOB}`),
649+
exclude,
650+
undefined,
651+
token,
652+
)
626653

627654
for (let file of cssFiles) {
628655
outputChannel.appendLine(`Checking if ${file.fsPath} may be Tailwind-related…`)

0 commit comments

Comments
 (0)