Skip to content

Commit 06a57d3

Browse files
committed
Try “roots” first instead of discarding non-roots
1 parent a748a1f commit 06a57d3

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,48 @@ 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({

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',

0 commit comments

Comments
 (0)