diff --git a/docs/guide/fmt.md b/docs/guide/fmt.md index eb6418f681..bb9a59d02a 100644 --- a/docs/guide/fmt.md +++ b/docs/guide/fmt.md @@ -20,6 +20,14 @@ vp fmt . --write Put formatting configuration directly in the `fmt` block in `vite.config.ts` so all your configuration stays in one place. We do not recommend using `.oxfmtrc.json` with Vite+. +For editors, point the formatter config path at `./vite.config.ts` so format-on-save uses the same `fmt` block: + +```json +{ + "oxc.fmt.configPath": "./vite.config.ts" +} +``` + For the upstream formatter behavior and configuration reference, see the [Oxfmt docs](https://oxc.rs/docs/guide/usage/formatter.html). ```ts diff --git a/docs/guide/ide-integration.md b/docs/guide/ide-integration.md index d6b0befcfe..e2e1c64924 100644 --- a/docs/guide/ide-integration.md +++ b/docs/guide/ide-integration.md @@ -24,6 +24,7 @@ When you create or migrate a project, Vite+ prompts whether you want editor conf ```json { "editor.defaultFormatter": "oxc.oxc-vscode", + "oxc.fmt.configPath": "./vite.config.ts", "editor.formatOnSave": true, "editor.formatOnSaveMode": "file", "editor.codeActionsOnSave": { @@ -32,4 +33,4 @@ When you create or migrate a project, Vite+ prompts whether you want editor conf } ``` -This gives the project a shared default formatter and enables Oxc-powered fix actions on save. Vite+ uses `formatOnSaveMode: "file"` because Oxfmt does not support partial formatting. +This gives the project a shared default formatter and enables Oxc-powered fix actions on save. Setting `oxc.fmt.configPath` to `./vite.config.ts` keeps editor format-on-save aligned with the `fmt` block in your Vite+ config. Vite+ uses `formatOnSaveMode: "file"` because Oxfmt does not support partial formatting. diff --git a/packages/cli/src/utils/__tests__/editor.spec.ts b/packages/cli/src/utils/__tests__/editor.spec.ts new file mode 100644 index 0000000000..043d4d6803 --- /dev/null +++ b/packages/cli/src/utils/__tests__/editor.spec.ts @@ -0,0 +1,71 @@ +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; + +import { afterEach, describe, expect, it } from 'vitest'; + +import { writeEditorConfigs } from '../editor.js'; + +const tempDirs: string[] = []; + +function createTempDir() { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-editor-config-')); + tempDirs.push(dir); + return dir; +} + +afterEach(() => { + for (const dir of tempDirs.splice(0, tempDirs.length)) { + fs.rmSync(dir, { recursive: true, force: true }); + } +}); + +describe('writeEditorConfigs', () => { + it('writes vscode settings that align formatter config with vite.config.ts', async () => { + const projectRoot = createTempDir(); + + await writeEditorConfigs({ + projectRoot, + editorId: 'vscode', + interactive: false, + silent: true, + }); + + const settings = JSON.parse( + fs.readFileSync(path.join(projectRoot, '.vscode', 'settings.json'), 'utf8'), + ) as Record; + + expect(settings['editor.defaultFormatter']).toBe('oxc.oxc-vscode'); + expect(settings['oxc.fmt.configPath']).toBe('./vite.config.ts'); + expect(settings['editor.formatOnSave']).toBe(true); + }); + + it('writes zed settings that align formatter config with vite.config.ts', async () => { + const projectRoot = createTempDir(); + + await writeEditorConfigs({ + projectRoot, + editorId: 'zed', + interactive: false, + silent: true, + }); + + const settings = JSON.parse( + fs.readFileSync(path.join(projectRoot, '.zed', 'settings.json'), 'utf8'), + ) as { + lsp?: { + oxfmt?: { + initialization_options?: { + settings?: { + configPath?: string; + }; + }; + }; + }; + }; + + expect(settings.lsp?.oxfmt?.initialization_options?.settings?.configPath).toBe( + './vite.config.ts', + ); + }); +}); diff --git a/packages/cli/src/utils/editor.ts b/packages/cli/src/utils/editor.ts index ce25c25043..3f101890ff 100644 --- a/packages/cli/src/utils/editor.ts +++ b/packages/cli/src/utils/editor.ts @@ -9,6 +9,7 @@ import { readJsonFile, writeJsonFile } from './json.js'; const VSCODE_SETTINGS = { // Set as default over per-lang to avoid conflicts with other formatters 'editor.defaultFormatter': 'oxc.oxc-vscode', + 'oxc.fmt.configPath': './vite.config.ts', 'editor.formatOnSave': true, // Oxfmt does not support partial formatting 'editor.formatOnSaveMode': 'file', @@ -38,7 +39,7 @@ const ZED_SETTINGS = { oxfmt: { initialization_options: { settings: { - configPath: './.oxfmtrc.jsonc', + configPath: './vite.config.ts', run: 'onSave', }, },