Skip to content
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

fix(vscode): biome resolution #492

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Changes from 1 commit
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
32 changes: 22 additions & 10 deletions editors/vscode/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ChildProcess, spawn } from "child_process";
import { type Socket, connect } from "net";
import { isAbsolute } from "path";
import { dirname, isAbsolute } from "path";
import { TextDecoder, promisify } from "util";
import {
ExtensionContext,
Expand All @@ -26,6 +26,7 @@ import { setContextValue } from "./utils";

import resolveImpl = require("resolve/async");
import type * as resolve from "resolve";
import { createRequire } from "module";

const resolveAsync = promisify<string, resolve.AsyncOpts, string | undefined>(
resolveImpl,
Expand Down Expand Up @@ -243,15 +244,27 @@ async function getWorkspaceDependency(
outputChannel: OutputChannel,
): Promise<string | undefined> {
for (const workspaceFolder of workspace.workspaceFolders) {
const path = Uri.joinPath(
workspaceFolder.uri,
"node_modules",
".bin",
"biome",
// To resolve the @biomejs/cli-*, which is a transitive dependency of the
// @biomejs/biome package, we need to create a custom require function that
// is scoped to @biomejs/biome. This allows us to reliably resolve the
// package regardless of the package manager used by the user.
const requireFromBiome = createRequire(
require.resolve("@biomejs/biome/package.json", {
nhedger marked this conversation as resolved.
Show resolved Hide resolved
paths: [workspaceFolder.uri.fsPath],
}),
);
const binaryPackage = dirname(
requireFromBiome.resolve(
`@biomejs/cli-${process.platform}-${process.arch}/package.json`,
),
);

const biomePath = Uri.file(
`${binaryPackage}/biome${process.platform === "win32" ? ".exe" : ""}`,
);

if (await fileExists(path)) {
return path.fsPath;
if (await fileExists(biomePath)) {
return biomePath.fsPath;
}
}

Expand Down Expand Up @@ -351,9 +364,8 @@ async function getSocket(
outputChannel: OutputChannel,
command: string,
): Promise<string> {
const process = spawn(`"${command}"`, ["__print_socket"], {
const process = spawn(command, ["__print_socket"], {
stdio: [null, "pipe", "pipe"],
shell: true,
});

const stdout = { content: "" };
Expand Down