From a3c70af122626651d92d77f9e3ec8e600a063cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoi=CC=82t=20Rouleau?= Date: Mon, 23 Mar 2026 22:35:11 -0400 Subject: [PATCH] Fix class sorting in embedded code blocks when cwd differs from project dir When Prettier formats embedded code blocks (e.g. tsx inside markdown), it sets a synthetic filepath like 'dummy.tsx'. The config resolution falls back to process.cwd() which may not be the project directory (e.g. in the VS Code extension host), causing the tailwindStylesheet to resolve to the wrong path and class sorting to silently fail. This stores the last successfully resolved config directory and uses it as a fallback instead of process.cwd(). --- src/config.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index fed19e3..3ec4ca0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -10,6 +10,13 @@ import { cacheForDirs } from './utils.js' let prettierConfigCache = expiringMap(10_000) +// Tracks the last successfully resolved config directory. This is used as a +// fallback when formatting embedded code blocks (e.g., tsx inside markdown) +// where Prettier sets a synthetic filepath like "dummy.tsx" that cannot be used +// for config resolution — especially in environments where process.cwd() does +// not point to the project directory (e.g., the VS Code extension host). +let lastResolvedConfigDir: string | null = null + async function resolvePrettierConfigDir( filePath: string, inputDir: string, @@ -17,7 +24,7 @@ async function resolvePrettierConfigDir( // Check cache for this directory let cached = prettierConfigCache.get(inputDir) if (cached !== undefined) { - return cached ?? process.cwd() + return cached ?? lastResolvedConfigDir ?? process.cwd() } const resolve = async () => { @@ -35,11 +42,12 @@ async function resolvePrettierConfigDir( // Cache all directories from inputDir up to config location if (prettierConfig) { let configDir = path.dirname(prettierConfig) + lastResolvedConfigDir = configDir cacheForDirs(prettierConfigCache, inputDir, configDir, configDir) return configDir } else { prettierConfigCache.set(inputDir, null) - return process.cwd() + return lastResolvedConfigDir ?? process.cwd() } }