Skip to content

Commit c2660e4

Browse files
Show warning when loading a config in v3 fails (#1191)
Closes #1124 When IntelliSense loads a config file in v3, if that loading fails, it won't work _at all_ even if your project is an otherwise valid project. There's also no indication to the user that this is the cause of IntelliSense not working. This PR does two things: - Uses the default config if loading the user's config does not work so you're likely to get IntelliSense for at least some utilities. - Detects an issue when loading the config file and surfaces a warning to the user Now, when given this config file: ```js import plugin from "i-do-not-exist"; /** @type {import('tailwindcss').Config} */ export default { content: ["index.html", "./src/**/*.{js,ts,jsx,tsx}"], theme: { extend: { colors: { primary: "orange", }, }, }, plugins: [plugin], }; ``` You'll see a warning like this: <img width="572" alt="Screenshot 2025-02-11 at 15 24 03" src="https://github.com/user-attachments/assets/2364b491-cd3d-448c-b453-37ef04c360ef" /> And IntelliSense will work for any default values (note how `text-primary` doesn't show a color swatch): <img width="655" alt="Screenshot 2025-02-11 at 15 28 12" src="https://github.com/user-attachments/assets/2b683b36-31bc-4832-ad71-69cb30b3b5d4" />
1 parent c35991b commit c2660e4

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
import { FileChangeType } from 'vscode-languageserver/node'
2020
import type { TextDocument } from 'vscode-languageserver-textdocument'
2121
import { URI } from 'vscode-uri'
22-
import { showError, SilentError } from './util/error'
22+
import { showError, showWarning, SilentError } from './util/error'
2323
import * as path from 'node:path'
2424
import * as fs from 'node:fs'
2525
import findUp from 'find-up'
@@ -838,6 +838,15 @@ export async function createProjectService(
838838
originalConfig = await loadConfig.module(state.configPath)
839839
originalConfig = originalConfig.default ?? originalConfig
840840
state.jit = true
841+
} catch (err) {
842+
// The user's config failed to load in v3 so we need to fallback
843+
originalConfig = await resolveConfig.module({})
844+
state.jit = true
845+
846+
// And warn the user
847+
console.error(`Unable to load config file at: ${state.configPath}`)
848+
console.error(err)
849+
showWarning(connection, 'Tailwind CSS is unable to load your config file', err)
841850
} finally {
842851
hook.unhook()
843852
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ export function showError(
3232
// }
3333
}
3434

35+
export function showWarning(
36+
connection: Connection,
37+
message: string = 'Tailwind CSS',
38+
err: any,
39+
): void {
40+
connection.sendNotification('@/tailwindCSS/warn', {
41+
message: formatError(message, err, false),
42+
})
43+
}
44+
3545
export function SilentError(message: string) {
3646
this.name = 'SilentError'
3747
this.message = message

packages/vscode-tailwindcss/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Make sure custom regexes apply in Vue `<script>` blocks ([#1177](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1177))
1111
- Fix suggestion of utilities with slashes in them in v4 ([#1182](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1182))
1212
- Assume 16px font size for `1rem` in media queries ([#1190](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1190))
13+
- Show warning when loading a config in v3 fails ([#1191](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1191))
1314

1415
## 0.14.3
1516

packages/vscode-tailwindcss/src/extension.ts

+7
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ export async function activate(context: ExtensionContext) {
532532
let client = new LanguageClient(CLIENT_ID, CLIENT_NAME, serverOptions, clientOptions)
533533

534534
client.onNotification('@/tailwindCSS/error', showError)
535+
client.onNotification('@/tailwindCSS/warn', showWarning)
535536
client.onNotification('@/tailwindCSS/clearColors', clearColors)
536537
client.onNotification('@/tailwindCSS/projectInitialized', updateActiveTextEditorContext)
537538
client.onNotification('@/tailwindCSS/projectReset', updateActiveTextEditorContext)
@@ -548,6 +549,12 @@ export async function activate(context: ExtensionContext) {
548549
commands.executeCommand('tailwindCSS.showOutput')
549550
}
550551

552+
async function showWarning({ message }: ErrorNotification) {
553+
let action = await Window.showWarningMessage(message, 'Go to output')
554+
if (action !== 'Go to output') return
555+
commands.executeCommand('tailwindCSS.showOutput')
556+
}
557+
551558
interface DocumentSymbolsRequest {
552559
uri: string
553560
}

0 commit comments

Comments
 (0)