Skip to content

Commit 5c79a60

Browse files
Add targeted fix for stylesheets named tailwindcss.css (#1517)
Fixes #1427
1 parent 83ce817 commit 5c79a60

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

packages/tailwindcss-language-server/src/project-locator.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,22 @@ testLocator({
588588
],
589589
})
590590

591+
testLocator({
592+
name: 'A stylesheet can be named tailwindcss.css',
593+
fs: {
594+
'src/tailwindcss.css': css`
595+
@import 'tailwindcss';
596+
`,
597+
},
598+
expected: [
599+
{
600+
version: '4.1.18 (bundled)',
601+
config: '/src/tailwindcss.css',
602+
content: [],
603+
},
604+
],
605+
})
606+
591607
// ---
592608

593609
function testLocator({

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ export async function createResolver(opts: ResolverOptions): Promise<Resolver> {
170170
preferRelative: true,
171171
})
172172

173+
let absoluteCssResolver = ResolverFactory.createResolver({
174+
fileSystem,
175+
extensions: ['.css'],
176+
mainFields: ['style'],
177+
conditionNames: ['style'],
178+
pnpApi,
179+
180+
// Used as a fallback when a file ends up importing itself
181+
preferRelative: false,
182+
})
183+
173184
async function resolveId(
174185
resolver: BaseResolver,
175186
id: string,
@@ -235,7 +246,22 @@ export async function createResolver(opts: ResolverOptions): Promise<Resolver> {
235246
}
236247

237248
async function resolveCssId(id: string, base: string): Promise<string> {
238-
return (await resolveId(cssResolver, id, base)) || id
249+
// If the ID matches `tailwindcss` exactly we tell the CSS resolver to
250+
// ignore relative file paths. This ensures that Tailwind CSS itself is
251+
// found even when a stylesheet named `tailwindcss.css` exists.
252+
//
253+
// If someone needs to import that stylesheet it must be done by:
254+
// - relative path: `@import "./tailwindcss"`
255+
// - adding an extension: `@import "tailwindcss.css"`
256+
//
257+
// Ideally this code would only be in place if the _importer_ is
258+
// `tailwindcss.css` but that data is not available. Only the base
259+
// path is so we enable it all the time.
260+
//
261+
// https://github.com/tailwindlabs/tailwindcss-intellisense/issues/1427
262+
let resolver = id === 'tailwindcss' ? absoluteCssResolver : cssResolver
263+
264+
return (await resolveId(resolver, id, base)) || id
239265
}
240266

241267
// Takes a path which may or may not be complete and returns the aliased path

packages/vscode-tailwindcss/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add a source to all emitted diagnostics ([#1491](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1491))
66
- Improve performance in large files ([#1507](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1507))
77
- Improve utility lookup performance when using v4 ([#1509](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1509))
8+
- Fix project initalization when stylesheet is named `tailwindcss.css` ([#1517](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1517))
89

910
## 0.14.29
1011

0 commit comments

Comments
 (0)