Skip to content

Cancel initial file search if it takes too long #1242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 11, 2025
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
1 change: 1 addition & 0 deletions packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Prerelease

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

# 0.14.8

Expand Down
35 changes: 31 additions & 4 deletions packages/vscode-tailwindcss/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
ConfigurationScope,
WorkspaceConfiguration,
Selection,
CancellationToken,
} from 'vscode'
import {
workspace as Workspace,
Expand All @@ -16,6 +17,7 @@ import {
Position,
Range,
RelativePattern,
CancellationTokenSource,
} from 'vscode'
import type {
DocumentFilter,
Expand Down Expand Up @@ -581,26 +583,45 @@ export async function activate(context: ExtensionContext) {
return
}

if (!(await anyFolderNeedsLanguageServer(Workspace.workspaceFolders ?? []))) {
let source: CancellationTokenSource | null = new CancellationTokenSource()
source.token.onCancellationRequested(() => {
source?.dispose()
source = null
outputChannel.appendLine(
'Server was not started. Search for Tailwind CSS-related files was taking too long.',
)
})

// Cancel the search after roughly 15 seconds
setTimeout(() => source?.cancel(), 15_000)

if (!(await anyFolderNeedsLanguageServer(Workspace.workspaceFolders ?? [], source!.token))) {
source?.dispose()
return
}

source?.dispose()

await bootWorkspaceClient()
}

async function anyFolderNeedsLanguageServer(
folders: readonly WorkspaceFolder[],
token: CancellationToken,
): Promise<boolean> {
for (let folder of folders) {
if (await folderNeedsLanguageServer(folder)) {
if (await folderNeedsLanguageServer(folder, token)) {
return true
}
}

return false
}

async function folderNeedsLanguageServer(folder: WorkspaceFolder): Promise<boolean> {
async function folderNeedsLanguageServer(
folder: WorkspaceFolder,
token: CancellationToken,
): Promise<boolean> {
let settings = Workspace.getConfiguration('tailwindCSS', folder)
if (settings.get('experimental.configFile') !== null) {
return true
Expand All @@ -616,13 +637,19 @@ export async function activate(context: ExtensionContext) {
new RelativePattern(folder, `**/${CONFIG_GLOB}`),
exclude,
1,
token,
)

for (let file of configFiles) {
return true
}

let cssFiles = await Workspace.findFiles(new RelativePattern(folder, `**/${CSS_GLOB}`), exclude)
let cssFiles = await Workspace.findFiles(
new RelativePattern(folder, `**/${CSS_GLOB}`),
exclude,
undefined,
token,
)

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