-
-
Notifications
You must be signed in to change notification settings - Fork 862
feat(vscode): fallback to globally installed oxlint/oxfmt packages #18007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
graphite-app
merged 1 commit into
main
from
01-14-feat_vscode_fallback_to_globally_installed_oxlint_oxfmt_packages
Jan 26, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { spawnSync } from "node:child_process"; | ||
| import { homedir } from "node:os"; | ||
| import * as path from "node:path"; | ||
| import { Uri, workspace } from "vscode"; | ||
| import { validateSafeBinaryPath } from "./PathValidator"; | ||
|
|
||
| function replaceTargetFromMainToBin(resolvedPath: string, binaryName: string): string { | ||
| // we want to target the binary instead of the main index file | ||
| // Improvement: search inside package.json "bin" and `main` field for more reliability | ||
| return resolvedPath.replace( | ||
| `${binaryName}${path.sep}dist${path.sep}index.js`, | ||
| `${binaryName}${path.sep}bin${path.sep}${binaryName}`, | ||
| ); | ||
| } | ||
| /** | ||
| * Search for the binary in all workspaces' node_modules/.bin directories. | ||
| * If multiple workspaces contain the binary, the first one found is returned. | ||
| */ | ||
| export async function searchProjectNodeModulesBin(binaryName: string): Promise<string | undefined> { | ||
| // try to resolve via require.resolve | ||
| try { | ||
| const resolvedPath = replaceTargetFromMainToBin( | ||
| require.resolve(binaryName, { | ||
| paths: workspace.workspaceFolders?.map((folder) => folder.uri.fsPath) ?? [], | ||
| }), | ||
| binaryName, | ||
| ); | ||
| return resolvedPath; | ||
| } catch {} | ||
| } | ||
|
|
||
| /** | ||
| * Search for the binary in global node_modules. | ||
| * Returns undefined if not found. | ||
| */ | ||
| export async function searchGlobalNodeModulesBin(binaryName: string): Promise<string | undefined> { | ||
| // try to resolve via require.resolve | ||
| try { | ||
| const resolvedPath = replaceTargetFromMainToBin( | ||
| require.resolve(binaryName, { paths: globalNodeModulesPaths() }), | ||
| binaryName, | ||
| ); | ||
| return resolvedPath; | ||
| } catch {} | ||
| } | ||
|
|
||
| /** | ||
| * Search for the binary based on user settings. | ||
| * If the path is relative, it is resolved against the first workspace folder. | ||
| * Returns undefined if no valid binary is found or the path is unsafe. | ||
| */ | ||
| export async function searchSettingsBin(settingsBinary: string): Promise<string | undefined> { | ||
| if (!workspace.isTrusted) { | ||
| return; | ||
| } | ||
|
|
||
| // validates the given path is safe to use | ||
| if (!validateSafeBinaryPath(settingsBinary)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (!path.isAbsolute(settingsBinary)) { | ||
| const cwd = workspace.workspaceFolders?.[0]?.uri.fsPath; | ||
| if (!cwd) { | ||
| return undefined; | ||
| } | ||
| // if the path is not absolute, resolve it to the first workspace folder | ||
| settingsBinary = path.normalize(path.join(cwd, settingsBinary)); | ||
Sysix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (process.platform !== "win32" && settingsBinary.endsWith(".exe")) { | ||
| // on non-Windows, remove `.exe` extension if present | ||
| settingsBinary = settingsBinary.slice(0, -4); | ||
| } | ||
|
|
||
| try { | ||
| await workspace.fs.stat(Uri.file(settingsBinary)); | ||
| return settingsBinary; | ||
| } catch {} | ||
|
|
||
Sysix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // on Windows, also check for `.exe` extension (bun uses `.exe` for its binaries) | ||
| if (process.platform === "win32") { | ||
| if (!settingsBinary.endsWith(".exe")) { | ||
| settingsBinary += ".exe"; | ||
| } | ||
|
|
||
| try { | ||
| await workspace.fs.stat(Uri.file(settingsBinary)); | ||
| return settingsBinary; | ||
| } catch {} | ||
| } | ||
|
|
||
| // no valid binary found | ||
| return undefined; | ||
| } | ||
|
|
||
| // copied from: https://github.com/biomejs/biome-vscode/blob/ae9b6df2254d0ff8ee9d626554251600eb2ca118/src/locator.ts#L28-L49 | ||
| function globalNodeModulesPaths(): string[] { | ||
| const npmGlobalNodeModulesPath = safeSpawnSync("npm", ["root", "-g"]); | ||
| const pnpmGlobalNodeModulesPath = safeSpawnSync("pnpm", ["root", "-g"]); | ||
| const bunGlobalNodeModulesPath = path.resolve(homedir(), ".bun/install/global/node_modules"); | ||
|
|
||
| return [npmGlobalNodeModulesPath, pnpmGlobalNodeModulesPath, bunGlobalNodeModulesPath].filter( | ||
| Boolean, | ||
| ) as string[]; | ||
| } | ||
|
|
||
| // only use this function with internal code, because it executes shell commands | ||
| // which could be a security risk if the command or args are user-controlled | ||
| const safeSpawnSync = (command: string, args: readonly string[] = []): string | undefined => { | ||
| let output: string | undefined; | ||
|
|
||
| try { | ||
| const result = spawnSync(command, args, { | ||
| shell: true, | ||
Sysix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| encoding: "utf8", | ||
| }); | ||
|
|
||
| if (result.error || result.status !== 0) { | ||
| output = undefined; | ||
| } else { | ||
| const trimmed = result.stdout.trim(); | ||
| output = trimmed ? trimmed : undefined; | ||
| } | ||
| } catch { | ||
| output = undefined; | ||
| } | ||
|
|
||
| return output; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { strictEqual } from "assert"; | ||
| import * as path from "node:path"; | ||
| import { searchGlobalNodeModulesBin, searchProjectNodeModulesBin } from "../../client/findBinary"; | ||
|
|
||
| suite("findBinary", () => { | ||
| const binaryName = "oxlint"; | ||
|
|
||
| suite("searchProjectNodeModulesBin", () => { | ||
| test("should return undefined when binary is not found in project node_modules", async () => { | ||
| const result = await searchProjectNodeModulesBin("non-existent-binary-package-name-12345"); | ||
| strictEqual(result, undefined); | ||
| }); | ||
|
|
||
| // this depends on the binary being installed in the oxc project's node_modules | ||
| test("should replace dist/index.js with bin/<binary-name> in resolved path", async () => { | ||
Sysix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const result = (await searchProjectNodeModulesBin(binaryName))!; | ||
|
|
||
| strictEqual(result.includes(`${path.sep}dist${path.sep}index.js`), false); | ||
| strictEqual(result.includes(`${path.sep}bin${path.sep}${binaryName}`), true); | ||
| }); | ||
| }); | ||
|
|
||
| suite("searchGlobalNodeModulesBin", () => { | ||
| test("should return undefined when binary is not found in global node_modules", async () => { | ||
| const result = await searchGlobalNodeModulesBin("non-existent-binary-package-name-12345"); | ||
| strictEqual(result, undefined); | ||
| }); | ||
|
|
||
| // Skipping this test as it may depend on the actual global installation of the binary | ||
| test.skip("should replace dist/index.js with bin/<binary-name> in resolved path", async () => { | ||
Sysix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const result = (await searchGlobalNodeModulesBin(binaryName))!; | ||
|
|
||
| strictEqual(result.includes(`${path.sep}dist${path.sep}index.js`), false); | ||
| strictEqual(result.includes(`${path.sep}bin${path.sep}${binaryName}`), true); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.