Skip to content

Commit cb52c0b

Browse files
GGGLHHHfengmk2
andauthored
fix(editor): align OXC formatter config with vite.config.ts (#862)
## Summary - point generated editor formatter config at `./vite.config.ts` for VS Code and Zed - document the same setup in the IDE integration and formatter guides - add a focused test covering generated editor config output ## Why This fixes the config drift described in #861, where `vp fmt` / `vp check --fix` read the `fmt` block from `vite.config.ts`, but editor format-on-save could still use a different formatter config source. Oxfmt already supports loading formatter settings from `vite.config.ts` through `fmt.configPath`, so the editor setup can use the same single source of truth instead of requiring a separate `.oxfmtrc.json`. ## Testing - local project verification: confirmed that `vp fmt` applies `singleQuote: true` when formatting from `vite.config.ts` - repository test run: not completed in this clone because `vp install` currently fails before dependency setup due missing `rolldown-vite/patches/*` files in the public checkout Co-authored-by: MK (fengmk2) <fengmk2@gmail.com>
1 parent 061d3ea commit cb52c0b

4 files changed

Lines changed: 83 additions & 2 deletions

File tree

docs/guide/fmt.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ vp fmt . --write
2020

2121
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+.
2222

23+
For editors, point the formatter config path at `./vite.config.ts` so format-on-save uses the same `fmt` block:
24+
25+
```json
26+
{
27+
"oxc.fmt.configPath": "./vite.config.ts"
28+
}
29+
```
30+
2331
For the upstream formatter behavior and configuration reference, see the [Oxfmt docs](https://oxc.rs/docs/guide/usage/formatter.html).
2432

2533
```ts

docs/guide/ide-integration.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ When you create or migrate a project, Vite+ prompts whether you want editor conf
2424
```json
2525
{
2626
"editor.defaultFormatter": "oxc.oxc-vscode",
27+
"oxc.fmt.configPath": "./vite.config.ts",
2728
"editor.formatOnSave": true,
2829
"editor.formatOnSaveMode": "file",
2930
"editor.codeActionsOnSave": {
@@ -32,4 +33,4 @@ When you create or migrate a project, Vite+ prompts whether you want editor conf
3233
}
3334
```
3435

35-
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.
36+
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.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import fs from 'node:fs';
2+
import os from 'node:os';
3+
import path from 'node:path';
4+
5+
import { afterEach, describe, expect, it } from 'vitest';
6+
7+
import { writeEditorConfigs } from '../editor.js';
8+
9+
const tempDirs: string[] = [];
10+
11+
function createTempDir() {
12+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-editor-config-'));
13+
tempDirs.push(dir);
14+
return dir;
15+
}
16+
17+
afterEach(() => {
18+
for (const dir of tempDirs.splice(0, tempDirs.length)) {
19+
fs.rmSync(dir, { recursive: true, force: true });
20+
}
21+
});
22+
23+
describe('writeEditorConfigs', () => {
24+
it('writes vscode settings that align formatter config with vite.config.ts', async () => {
25+
const projectRoot = createTempDir();
26+
27+
await writeEditorConfigs({
28+
projectRoot,
29+
editorId: 'vscode',
30+
interactive: false,
31+
silent: true,
32+
});
33+
34+
const settings = JSON.parse(
35+
fs.readFileSync(path.join(projectRoot, '.vscode', 'settings.json'), 'utf8'),
36+
) as Record<string, unknown>;
37+
38+
expect(settings['editor.defaultFormatter']).toBe('oxc.oxc-vscode');
39+
expect(settings['oxc.fmt.configPath']).toBe('./vite.config.ts');
40+
expect(settings['editor.formatOnSave']).toBe(true);
41+
});
42+
43+
it('writes zed settings that align formatter config with vite.config.ts', async () => {
44+
const projectRoot = createTempDir();
45+
46+
await writeEditorConfigs({
47+
projectRoot,
48+
editorId: 'zed',
49+
interactive: false,
50+
silent: true,
51+
});
52+
53+
const settings = JSON.parse(
54+
fs.readFileSync(path.join(projectRoot, '.zed', 'settings.json'), 'utf8'),
55+
) as {
56+
lsp?: {
57+
oxfmt?: {
58+
initialization_options?: {
59+
settings?: {
60+
configPath?: string;
61+
};
62+
};
63+
};
64+
};
65+
};
66+
67+
expect(settings.lsp?.oxfmt?.initialization_options?.settings?.configPath).toBe(
68+
'./vite.config.ts',
69+
);
70+
});
71+
});

packages/cli/src/utils/editor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { readJsonFile, writeJsonFile } from './json.js';
99
const VSCODE_SETTINGS = {
1010
// Set as default over per-lang to avoid conflicts with other formatters
1111
'editor.defaultFormatter': 'oxc.oxc-vscode',
12+
'oxc.fmt.configPath': './vite.config.ts',
1213
'editor.formatOnSave': true,
1314
// Oxfmt does not support partial formatting
1415
'editor.formatOnSaveMode': 'file',
@@ -38,7 +39,7 @@ const ZED_SETTINGS = {
3839
oxfmt: {
3940
initialization_options: {
4041
settings: {
41-
configPath: './.oxfmtrc.jsonc',
42+
configPath: './vite.config.ts',
4243
run: 'onSave',
4344
},
4445
},

0 commit comments

Comments
 (0)