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
29 changes: 0 additions & 29 deletions packages/rstack/THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## atomically

This package includes bundled code from
[atomically](https://github.com/fabiospampinato/atomically).

License: MIT

The MIT License (MIT)

Copyright (c) 2020-present Fabio Spampinato

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

## fast-ignore

This package includes bundled code from [fast-ignore](https://github.com/fabiospampinato/fast-ignore).
Expand Down
1 change: 0 additions & 1 deletion packages/rstack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"@rstest/adapter-rslib": "catalog:",
"@types/micromatch": "catalog:",
"@types/node": "catalog:",
"atomically": "catalog:",
"fast-ignore": "catalog:",
"ignore": "catalog:",
"import-meta-resolve": "catalog:",
Expand Down
16 changes: 5 additions & 11 deletions packages/rstack/src/fmt/worker.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
// Derived from @prettier/cli, see THIRD_PARTY_NOTICES.md

import { readFile, writeFile } from 'atomically';
import { readFileSync, writeFileSync } from 'node:fs';
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.
* Use synchronous direct I/O inside the dedicated worker to avoid libuv
* scheduling overhead. This prioritizes throughput over crash-safe replacement.
*/
const atomicWriteOptions = {
encoding: 'utf8',
fsync: false,
} as const;

const formatFile = async (
{ path, options }: FmtFileRequest,
shouldWrite: boolean,
): Promise<boolean> => {
const source = await readFile(path, 'utf8');
const source = readFileSync(path, 'utf8');
const formatted = await format(source, {
...options,
plugins: await getPrettierPlugins(options),
Expand All @@ -30,7 +24,7 @@ const formatFile = async (
}

if (shouldWrite) {
await writeFile(path, formatted, atomicWriteOptions);
writeFileSync(path, formatted, 'utf8');
Comment thread
chenjiahan marked this conversation as resolved.
}

return true;
Expand Down
6 changes: 3 additions & 3 deletions packages/rstack/tests/fmt/runnerWriteFailure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const mocks = rs.hoisted(() => ({
rs.mock('../../src/fmt/parallel.ts', () => ({
createFmtWorker: () =>
Promise.resolve({
formatFile: () => Promise.reject(new Error('atomic write failed')),
formatFile: () => Promise.reject(new Error('file write failed')),
terminate: () => {
mocks.terminateCalls++;
},
}),
}));

test('returns an error when the atomic write fails', async () => {
test('returns an error when a file write fails', async () => {
const filePath = '/virtual/example.ts';

const result = await runFmtFiles({
Expand All @@ -38,7 +38,7 @@ test('returns an error when the atomic write fails', async () => {
{
path: filePath,
status: 'error',
error: { message: 'atomic write failed' },
error: { message: 'file write failed' },
},
],
});
Expand Down
48 changes: 19 additions & 29 deletions packages/rstack/tests/fmt/worker.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
import { expect, rs, test } from 'rstack/test';
import { readFileSync } from 'node:fs';
import { expect, test } from 'rstack/test';
import { formatFile } from '../../src/fmt/worker.ts';
import { withTempProject, writeProjectFile } from './helpers.ts';

const mocks = rs.hoisted(() => ({
writeFileCalls: [] as [string, string, unknown][],
}));
test('writes formatted files', async () => {
await withTempProject(async (rootPath) => {
const filePath = writeProjectFile(rootPath, 'example.ts', 'const value=1');

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',
await expect(
formatFile(
{
path: filePath,
options: {
filepath: filePath,
parser: 'typescript',
},
},
},
true,
),
).resolves.toBe(true);
true,
),
).resolves.toBe(true);

expect(mocks.writeFileCalls).toEqual([
[filePath, 'const value = 1;\n', { encoding: 'utf8', fsync: false }],
]);
expect(readFileSync(filePath, 'utf8')).toBe('const value = 1;\n');
});
});
31 changes: 0 additions & 31 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ catalog:
'@types/react': '^19.2.18'
'@types/react-dom': '^19.2.4'
'@shikijs/transformers': '^4.3.1'
atomically: '2.1.1'
'cspell-ban-words': '^0.0.4'
'fast-ignore': '2.0.0'
'happy-dom': '^20.11.1'
Expand Down