Skip to content

Commit 4b8460f

Browse files
committed
fix(cli): Resolve bin/oxfmt as like oxlint (#1326)
- `oxfmt@0.44` added `package.json` exports - oxc-project/oxc#20784, for #1162 - This prevents access to unspecified subpaths - As a result, this type of error will occur - https://github.com/rolldown/rolldown/actions/runs/24068646976/job/70200159260?pr=9018 - At the moment, `vite-plus` bundles `oxfmt@0.43`, so this should not be an issue - It was only causing a problem because rolldown repo happened to overwrite it by installing `oxfmt@0.44` separately - However, this issue will happen for us when updating to bundle `oxfmt@0.44` in the future We can avoid this issue by following the `oxlint` resolution approach. And now, it's ready to update `oxfmt@0.44` safely.
1 parent f47d2a4 commit 4b8460f

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

packages/cli/bin/oxfmt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ if (!process.argv.includes('--lsp')) {
1111
}
1212

1313
import { createRequire } from 'node:module';
14+
import { dirname, join } from 'node:path';
1415
import { pathToFileURL } from 'node:url';
1516

1617
const require = createRequire(import.meta.url);
17-
const oxfmtBin = require.resolve('oxfmt/bin/oxfmt');
18+
const oxfmtMainPath = require.resolve('oxfmt');
19+
const oxfmtBin = join(dirname(dirname(oxfmtMainPath)), 'bin', 'oxfmt');
1820

1921
await import(pathToFileURL(oxfmtBin).href);

packages/cli/src/resolve-fmt.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
* provides high-performance code formatting capabilities.
1212
*/
1313

14+
import { dirname, join } from 'node:path';
15+
1416
import { DEFAULT_ENVS, resolve } from './utils/constants.ts';
1517

1618
/**
@@ -27,8 +29,13 @@ export async function fmt(): Promise<{
2729
binPath: string;
2830
envs: Record<string, string>;
2931
}> {
30-
// Resolve the oxfmt binary directly (it's a native executable)
31-
const binPath = resolve('oxfmt/bin/oxfmt');
32+
// Resolve the oxfmt package path first, then navigate to the bin file.
33+
// The bin/oxfmt subpath is not exported in package.json exports, so we
34+
// resolve the main entry point and derive the bin path from it.
35+
// resolve('oxfmt') returns .../oxfmt/dist/index.js, so we need to go up
36+
// two directories (past 'dist') to reach the package root.
37+
const oxfmtMainPath = resolve('oxfmt');
38+
const binPath = join(dirname(dirname(oxfmtMainPath)), 'bin', 'oxfmt');
3239

3340
return {
3441
binPath,

0 commit comments

Comments
 (0)