Skip to content

Commit 07252bd

Browse files
Fix detection when project contains stylesheets that import the "main" stylesheet (#1218)
Fixes #1217 This issue kinda reveals that the entire detection around "project roots" or "root stylesheets" needs to be rethought because files at the top of a CSS graph may not actually be the intended stylesheet — especially when `@reference` is used. But… like anything… they also _might be_. In the meantime we'll "prefer" files we can detect as definite root stylesheets before trying other files.
1 parent a748a1f commit 07252bd

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from 'vitest'
1+
import { expect, test, TestOptions } from 'vitest'
22
import * as path from 'node:path'
33
import { ProjectLocator } from './project-locator'
44
import { URL, fileURLToPath } from 'url'
@@ -279,23 +279,68 @@ testLocator({
279279
],
280280
})
281281

282+
testLocator({
283+
name: 'Roots are detected when they indirectly use Tailwind features',
284+
fs: {
285+
'package.json': json`
286+
{
287+
"dependencies": {
288+
"tailwindcss": "4.0.6"
289+
}
290+
}
291+
`,
292+
// TODO: This is marked as the root which is… maybe fine but not sure
293+
// The intention in this example is that src/globals.css is the real root
294+
// but if src/articles.css suddenly gained `@theme` blocks then maybe it'd
295+
// need to be the root instead.
296+
'src/articles/articles.css': css`
297+
@reference "../globals.css";
298+
.article-title {
299+
@apply text-primary;
300+
}
301+
`,
302+
'src/articles/layout.js': js`
303+
import "./articles.css";
304+
export default function Layout(children) {
305+
return children;
306+
}
307+
`,
308+
'src/globals.css': scss`
309+
@import "tailwindcss";
310+
@theme {
311+
--color-primary: #3490dc;
312+
}
313+
`,
314+
},
315+
expected: [
316+
{
317+
version: '4.0.6',
318+
config: '/src/articles/articles.css',
319+
content: [],
320+
},
321+
],
322+
})
323+
282324
// ---
283325

284326
function testLocator({
285327
name,
286328
fs,
287329
expected,
288330
settings,
331+
options,
289332
}: {
290333
name: string
291334
fs: Storage
292335
settings?: Partial<Settings>
293336
expected: any[]
337+
options?: TestOptions
294338
}) {
295339
defineTest({
296340
name,
297341
fs,
298342
prepare,
343+
options,
299344
async handle({ search }) {
300345
let projects = await search(settings)
301346

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,18 @@ export class ProjectLocator {
427427
if (indexPath && themePath) graph.connect(indexPath, themePath)
428428
if (indexPath && utilitiesPath) graph.connect(indexPath, utilitiesPath)
429429

430-
for (let root of graph.roots()) {
431-
if (!root.meta) continue
430+
// Sort the graph so potential "roots" appear first
431+
// The entire concept of roots needs to be rethought because it's not always
432+
// clear what the root of a project is. Even when imports are present a file
433+
// may import a file that is the actual "root" of the project.
434+
let roots = Array.from(graph.roots())
435+
436+
roots.sort((a, b) => {
437+
return a.meta.root === b.meta.root ? 0 : a.meta.root ? -1 : 1
438+
})
432439

433-
// This file is not eligible to act as a root of the CSS graph
434-
if (root.meta.root === false) continue
440+
for (let root of roots) {
441+
if (!root.meta) continue
435442

436443
let config: ConfigEntry = configs.remember(root.path, () => ({
437444
source: 'css',

packages/vscode-tailwindcss/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Prerelease
44

5-
- Nothing yet!
5+
- Fix detection when project contains stylesheets that import the "main" stylesheet ([#1218](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1218))
66

77
## 0.14.5
88

0 commit comments

Comments
 (0)