Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ import { cacheForDirs } from './utils.js'

let prettierConfigCache = expiringMap<string, string | null>(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,
): Promise<string> {
// 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 () => {
Expand All @@ -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()
}
}

Expand Down