Skip to content

Fix problem with too many ripgrep processes being spawned by VSCode #1287

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 6 commits into from
Apr 7, 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
2 changes: 1 addition & 1 deletion packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Prerelease

- Nothing yet!
- Only scan the file system once when needed ([#1287](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1287))

# 0.14.13

Expand Down
93 changes: 93 additions & 0 deletions packages/vscode-tailwindcss/src/analyze.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { workspace, RelativePattern, CancellationToken, Uri, WorkspaceFolder } from 'vscode'
import braces from 'braces'
import { CONFIG_GLOB, CSS_GLOB } from '@tailwindcss/language-server/src/lib/constants'
import { getExcludePatterns } from './exclusions'

export interface SearchOptions {
folders: readonly WorkspaceFolder[]
token: CancellationToken
}

export async function anyWorkspaceFoldersNeedServer({ folders, token }: SearchOptions) {
// An explicit config file setting means we need the server
for (let folder of folders) {
let settings = workspace.getConfiguration('tailwindCSS', folder)
let configFilePath = settings.get('experimental.configFile')

// No setting provided
if (!configFilePath) continue

// Ths config file may be a string:
// A path pointing to a CSS or JS config file
if (typeof configFilePath === 'string') return true

// Ths config file may be an object:
// A map of config files to one or more globs
//
// If we get an empty object the language server will do a search anyway so
// we'll act as if no option was passed to be consistent
if (typeof configFilePath === 'object' && Object.values(configFilePath).length > 0) return true
}

let configs: Array<() => Thenable<Uri[]>> = []
let stylesheets: Array<() => Thenable<Uri[]>> = []

for (let folder of folders) {
let exclusions = getExcludePatterns(folder).flatMap((pattern) => braces.expand(pattern))
let exclude = `{${exclusions.join(',').replace(/{/g, '%7B').replace(/}/g, '%7D')}}`

configs.push(() =>
workspace.findFiles(
new RelativePattern(folder, `**/${CONFIG_GLOB}`),
exclude,
undefined,
token,
),
)

stylesheets.push(() =>
workspace.findFiles(new RelativePattern(folder, `**/${CSS_GLOB}`), exclude, undefined, token),
)
}

// If we find a config file then we need the server
let configUrls = await Promise.all(configs.map((fn) => fn()))
for (let group of configUrls) {
if (group.length > 0) {
return true
}
}

// If we find a possibly-related stylesheet then we need the server
// The step is done last because it requires reading individual files
// to determine if the server should be started.
//
// This is also, unfortunately, prone to starting the server unncessarily
// in projects that don't use TailwindCSS so we do this one-by-one instead
// of all at once to keep disk I/O low.
let stylesheetUrls = await Promise.all(stylesheets.map((fn) => fn()))
for (let group of stylesheetUrls) {
for (let file of group) {
if (await fileMayBeTailwindRelated(file)) {
return true
}
}
}
}

let HAS_CONFIG = /@config\s*['"]/
let HAS_IMPORT = /@import\s*['"]/
let HAS_TAILWIND = /@tailwind\s*[^;]+;/
let HAS_THEME = /@theme\s*\{/

export async function fileMayBeTailwindRelated(uri: Uri) {
let buffer = await workspace.fs.readFile(uri)
let contents = buffer.toString()

return (
HAS_CONFIG.test(contents) ||
HAS_IMPORT.test(contents) ||
HAS_TAILWIND.test(contents) ||
HAS_THEME.test(contents)
)
}
49 changes: 49 additions & 0 deletions packages/vscode-tailwindcss/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { workspace, CancellationTokenSource, OutputChannel, ExtensionContext, Uri } from 'vscode'
import { anyWorkspaceFoldersNeedServer, fileMayBeTailwindRelated } from './analyze'

interface ApiOptions {
context: ExtensionContext
outputChannel: OutputChannel
}

export async function createApi({ context, outputChannel }: ApiOptions) {
let folderAnalysis: Promise<boolean> | null = null

async function workspaceNeedsLanguageServer() {
if (folderAnalysis) return folderAnalysis

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)
context.subscriptions.push(source)

folderAnalysis ??= anyWorkspaceFoldersNeedServer({
token: source.token,
folders: workspace.workspaceFolders ?? [],
})

let result = await folderAnalysis
source?.dispose()
return result
}

async function stylesheetNeedsLanguageServer(uri: Uri) {
outputChannel.appendLine(`Checking if ${uri.fsPath} may be Tailwind-related…`)

return fileMayBeTailwindRelated(uri)
}

return {
workspaceNeedsLanguageServer,
stylesheetNeedsLanguageServer,
}
}
49 changes: 49 additions & 0 deletions packages/vscode-tailwindcss/src/exclusions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
workspace,
type WorkspaceConfiguration,
type ConfigurationScope,
type WorkspaceFolder,
} from 'vscode'
import picomatch from 'picomatch'
import * as path from 'node:path'

function getGlobalExcludePatterns(scope: ConfigurationScope | null): string[] {
return Object.entries(workspace.getConfiguration('files', scope)?.get('exclude') ?? [])
.filter(([, value]) => value === true)
.map(([key]) => key)
.filter(Boolean)
}

export function getExcludePatterns(scope: ConfigurationScope | null): string[] {
return [
...getGlobalExcludePatterns(scope),
...(<string[]>workspace.getConfiguration('tailwindCSS', scope).get('files.exclude')).filter(
Boolean,
),
]
}

export function isExcluded(file: string, folder: WorkspaceFolder): boolean {
for (let pattern of getExcludePatterns(folder)) {
let matcher = picomatch(path.join(folder.uri.fsPath, pattern))

if (matcher(file)) {
return true
}
}

return false
}

export function mergeExcludes(
settings: WorkspaceConfiguration,
scope: ConfigurationScope | null,
): any {
return {
...settings,
files: {
...settings.files,
exclude: getExcludePatterns(scope),
},
}
}
Loading