Skip to content

Commit bfca2f7

Browse files
ntottenclaude
andcommitted
Allow formatting untitled files when requireConfig is enabled
When `prettier.requireConfig` is enabled, untitled files couldn't be formatted because the extension tried to find a config file starting from the file's path, but untitled files don't have a real path. This fix modifies `getResolvedConfig` to detect untitled documents (scheme === "untitled") and use the first workspace folder's path as the starting point for config file search instead. Fixes #3885 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 61b4780 commit bfca2f7

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ All notable changes to the "prettier-vscode" extension will be documented in thi
66

77
## [Unreleased]
88

9+
- Fixed formatting of untitled files when `requireConfig` is enabled. The extension now uses the workspace folder to search for Prettier config files when formatting untitled documents (#3885).
10+
911
## [12.2.0]
1012

1113
- Fixed `source.fixAll.prettier` code action running even when `editor.defaultFormatter` was set to a different extension (#3908). The code action now respects the user's formatter choice and only runs when Prettier is the default formatter or when `source.fixAll.prettier` is explicitly enabled.

src/ModuleResolverNode.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,21 @@ export class ModuleResolver implements ModuleResolverInterface {
222222
doc: TextDocument,
223223
vscodeConfig: PrettierVSCodeConfig,
224224
): Promise<"error" | "disabled" | PrettierOptions | null> {
225-
const fileName = doc.fileName;
225+
let fileName = doc.fileName;
226+
227+
// For untitled documents, use the workspace folder path to search for config files.
228+
// This allows untitled files to be formatted when requireConfig is enabled,
229+
// as long as there's a Prettier config in the workspace.
230+
if (doc.uri.scheme === "untitled") {
231+
const workspaceFolder =
232+
workspace.workspaceFolders && workspace.workspaceFolders.length > 0
233+
? workspace.workspaceFolders[0]
234+
: undefined;
235+
if (workspaceFolder) {
236+
fileName = workspaceFolder.uri.fsPath;
237+
}
238+
}
239+
226240
const prettier =
227241
(await this.getPrettierInstance(fileName)) ??
228242
(await getBundledPrettier());

0 commit comments

Comments
 (0)