Skip to content
Merged
Show file tree
Hide file tree
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: 11 additions & 1 deletion packages/rstack/src/fmt/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ import { format } from 'prettier';
import { getPrettierPlugins } from './prettierPlugins.ts';
import type { FmtFileRequest } from './types.ts';

/**
* Formatting output can be regenerated, so avoid waiting for a durability sync
* after every file, which is especially expensive during parallel formatting.
* `atomically` still uses a temporary file and rename for atomic replacement.
*/
const atomicWriteOptions = {
encoding: 'utf8',
fsync: false,
} as const;

const formatFile = async (
{ path, options }: FmtFileRequest,
shouldWrite: boolean,
Expand All @@ -20,7 +30,7 @@ const formatFile = async (
}

if (shouldWrite) {
await writeFile(path, formatted, 'utf8');
await writeFile(path, formatted, atomicWriteOptions);
}

return true;
Expand Down
35 changes: 35 additions & 0 deletions packages/rstack/tests/fmt/worker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect, rs, test } from 'rstack/test';
import { formatFile } from '../../src/fmt/worker.ts';

const mocks = rs.hoisted(() => ({
writeFileCalls: [] as [string, string, unknown][],
}));

rs.mock('atomically', () => ({
readFile: () => Promise.resolve('const value=1'),
writeFile: (path: string, data: string, options: unknown) => {
mocks.writeFileCalls.push([path, data, options]);
return Promise.resolve();
},
}));

test('disables fsync for atomic writes', async () => {
const filePath = '/virtual/example.ts';

await expect(
formatFile(
{
path: filePath,
options: {
filepath: filePath,
parser: 'typescript',
},
},
true,
),
).resolves.toBe(true);

expect(mocks.writeFileCalls).toEqual([
[filePath, 'const value = 1;\n', { encoding: 'utf8', fsync: false }],
]);
});