Skip to content

Conversation

@ntotten
Copy link
Member

@ntotten ntotten commented Jan 21, 2026

Summary

  • Fix issue where untitled files cannot be formatted when prettier.requireConfig is enabled
  • For untitled documents, the extension now uses the first workspace folder's path as the starting point for Prettier config file search
  • This allows untitled files to be formatted when there's a .prettierrc or other Prettier config in the workspace

Problem

When prettier.requireConfig is enabled, untitled files couldn't be formatted because:

  1. The extension tried to find a config file starting from the document's fileName
  2. Untitled files have a fileName like "Untitled-1", which is not a real file path
  3. Prettier's resolveConfigFile couldn't find any config, so formatting was disabled

Solution

Modified getResolvedConfig in ModuleResolverNode.ts to detect when a document has the untitled URI scheme and use the first workspace folder's path instead of the document's fileName for config resolution.

Test plan

  • Open a workspace with a .prettierrc file
  • Enable prettier.requireConfig in settings
  • Create a new untitled file (Cmd/Ctrl + N)
  • Type some unformatted JavaScript code
  • Format the document (Cmd/Ctrl + Shift + F or use Format Document command)
  • Verify the code is formatted according to the workspace's Prettier config

Fixes #3885

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings January 21, 2026 23:30
@ntotten ntotten force-pushed the fix/untitled-file-require-config branch from bfca2f7 to 8e17ac9 Compare January 21, 2026 23:31
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes formatting of untitled files when prettier.requireConfig is enabled. Previously, untitled documents couldn't be formatted in this mode because the extension tried to resolve Prettier config files starting from the document's non-existent file path (e.g., "Untitled-1").

Changes:

  • Modified getResolvedConfig in ModuleResolverNode.ts to detect untitled documents and use the first workspace folder's path for config resolution
  • Updated CHANGELOG.md to document the fix

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/ModuleResolverNode.ts Added logic to detect untitled documents via URI scheme and substitute the workspace folder path for config file resolution
CHANGELOG.md Documented the fix for untitled file formatting with requireConfig enabled
Comments suppressed due to low confidence (1)

src/ModuleResolverNode.ts:238

  • Consider adding test coverage for the untitled file formatting scenario with requireConfig enabled. The repository has comprehensive test coverage for other formatting scenarios (see test/suite/config.test.ts, format.test.ts), but this new behavior isn't tested. A test should verify that an untitled document can be formatted when requireConfig is true and a Prettier config exists in the workspace folder.
      (await getBundledPrettier());

    return this.resolveConfig(prettier, fileName, vscodeConfig, isUntitled);
  }

  public async clearModuleCache(filePath: string): Promise<void> {
    const prettier =
      (await this.getPrettierInstance(filePath)) ??
      (await getBundledPrettier());

For untitled files (unsaved documents), the requireConfig setting
now does not apply. Untitled files will be formatted using VS Code
settings since they are not part of any workspace.

Fixes #3885

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@ntotten ntotten force-pushed the fix/untitled-file-require-config branch from 8e17ac9 to 86581ea Compare January 21, 2026 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unsaved files can't format with require config

2 participants