Skip to content

Commit 7f503d5

Browse files
Don't break when generating rules produces an error (#954)
* Use `node:util` to format LSP console messages * Don’t break intellisense when generating rules throws an error for a single utility
1 parent daa8bb2 commit 7f503d5

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

packages/tailwindcss-language-server/src/language/cssServer.ts

+3-15
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,14 @@ import { Utils, URI } from 'vscode-uri'
1919
import { getLanguageModelCache } from './languageModelCache'
2020
import { Stylesheet } from 'vscode-css-languageservice'
2121
import dlv from 'dlv'
22+
import { interceptLogs } from '../util/logs'
2223

2324
let connection = createConnection(ProposedFeatures.all)
2425

25-
console.log = connection.console.log.bind(connection.console)
26-
console.error = connection.console.error.bind(connection.console)
27-
28-
function formatError(message: string, err: any): string {
29-
if (err instanceof Error) {
30-
let error = <Error>err
31-
return `${message}: ${error.message}\n${error.stack}`
32-
} else if (typeof err === 'string') {
33-
return `${message}: ${err}`
34-
} else if (err) {
35-
return `${message}: ${err.toString()}`
36-
}
37-
return message
38-
}
26+
interceptLogs(console, connection)
3927

4028
process.on('unhandledRejection', (e: any) => {
41-
connection.console.error(formatError(`Unhandled exception`, e))
29+
console.error("Unhandled exception", e)
4230
})
4331

4432
let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument)

packages/tailwindcss-language-server/src/server.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import './lib/env'
22
import { createConnection } from 'vscode-languageserver/node'
3-
import { formatError } from './util/error'
43
// @ts-ignore
54
import preflight from 'tailwindcss/lib/css/preflight.css'
65
import { TW } from './tw'
6+
import { interceptLogs } from './util/logs'
77

88
// @ts-ignore
99
// new Function(…) is used to work around issues with esbuild
@@ -25,11 +25,10 @@ new Function(
2525
const connection =
2626
process.argv.length <= 2 ? createConnection(process.stdin, process.stdout) : createConnection()
2727

28-
console.log = connection.console.log.bind(connection.console)
29-
console.error = connection.console.error.bind(connection.console)
28+
interceptLogs(console, connection)
3029

3130
process.on('unhandledRejection', (e: any) => {
32-
connection.console.error(formatError(`Unhandled exception`, e))
31+
console.error(`Unhandled rejection`, e)
3332
})
3433

3534
const tw = new TW(connection)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { Connection } from 'vscode-languageserver'
2+
import { format } from 'node:util'
3+
4+
function formatForLogging(params: any[]): string {
5+
return params.map((item) => format(item)).join(' ')
6+
}
7+
8+
export function interceptLogs(console: Console, connection: Connection) {
9+
console.debug = (...params: any[]) => connection.console.info(formatForLogging(params))
10+
console.error = (...params: any[]) => connection.console.error(formatForLogging(params))
11+
console.warn = (...params: any[]) => connection.console.warn(formatForLogging(params))
12+
console.info = (...params: any[]) => connection.console.info(formatForLogging(params))
13+
console.log = (...params: any[]) => connection.console.log(formatForLogging(params))
14+
}

packages/tailwindcss-language-service/src/util/color.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,16 @@ export function getColor(state: State, className: string): culori.Color | Keywor
167167
}
168168
}
169169

170-
let { root, rules } = jit.generateRules(state, [className])
170+
let result!: ReturnType<typeof jit.generateRules>
171+
try {
172+
result = jit.generateRules(state, [className])
173+
} catch (err) {
174+
console.error(`Error generating rules for className: ${className}`)
175+
console.error(err)
176+
return null
177+
}
178+
179+
let { root, rules } = result
171180
if (rules.length === 0) return null
172181

173182
let decls: Record<string, string | string[]> = {}

0 commit comments

Comments
 (0)