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: EPERM error on windows installation #735

Closed
wants to merge 5 commits into from
Closed
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
91 changes: 71 additions & 20 deletions editors/vscode/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { type ChildProcess, spawn } from "child_process";
import { type Socket, connect } from "net";
import { dirname, isAbsolute } from "path";
import { TextDecoder, promisify } from "util";
import { promisify } from "util";
import {
ExtensionContext,
OutputChannel,
Expand All @@ -25,7 +25,10 @@ import { StatusBar } from "./statusBar";
import { setContextValue } from "./utils";

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

const resolveAsync = promisify<string, resolve.AsyncOpts, string | undefined>(
Expand Down Expand Up @@ -72,16 +75,8 @@ export async function activate(context: ExtensionContext) {
return;
}

outputChannel.appendLine(`Using Biome from ${command}`);

const statusBar = new StatusBar();

const serverOptions: ServerOptions = createMessageTransports.bind(
undefined,
outputChannel,
command,
);

const documentSelector: DocumentFilter[] = [
{ language: "javascript", scheme: "file" },
{ language: "typescript", scheme: "file" },
Expand All @@ -97,11 +92,73 @@ export async function activate(context: ExtensionContext) {
traceOutputChannel,
};

client = new LanguageClient(
"biome_lsp",
"Biome",
serverOptions,
clientOptions,
const reloadClient = async () => {
outputChannel.appendLine(`Using Biome from ${command}`);

let tmpDestination: string;

try {
tmpDestination = path.resolve(
os.tmpdir(),
`./biome${process.platform === "win32" ? ".exe" : ""}`,
);
outputChannel.appendLine(`Copying file to tmp folder: ${tmpDestination}`);
copyFileSync(command, tmpDestination);
} catch (error) {
if (error.code === "EBUSY") {
// The file is busy, but attempt to use it anyways.
tmpDestination = path.resolve(
os.tmpdir(),
`./biome${process.platform === "win32" ? ".exe" : ""}`,
);
} else {
outputChannel.appendLine(`Error copying file: ${error}`);
}
}

const serverOptions: ServerOptions = createMessageTransports.bind(
undefined,
outputChannel,
tmpDestination ?? command,
);

client = new LanguageClient(
"biome_lsp",
"Biome",
serverOptions,
clientOptions,
);

context.subscriptions.push(
client.onDidChangeState((evt) => {
statusBar.setServerState(client, evt.newState);
}),
);
};

reloadClient();

// Best way to determine package updates. Will work for npm, yarn, pnpm and bun. (Might work for more files also).
// It is not possible to listen node_modules, because it is usually gitignored.
const watcher = workspace.createFileSystemWatcher("**/*lock*");
context.subscriptions.push(
watcher.onDidChange(async () => {
try {
// When the lockfile changes, reload the biome executable.
outputChannel.appendLine("Reloading biome executable..");
if (client.isRunning()) {
await client.stop();
}
await reloadClient();
if (client.isRunning()) {
await client.restart();
} else {
await client.start();
}
} catch (error) {
outputChannel.appendLine(`Reloading client failed: ${error}`);
}
}),
);

const session = new Session(context, client);
Expand All @@ -128,12 +185,6 @@ export async function activate(context: ExtensionContext) {
}
});

context.subscriptions.push(
client.onDidChangeState((evt) => {
statusBar.setServerState(client, evt.newState);
}),
);

const handleActiveTextEditorChanged = (textEditor?: TextEditor) => {
if (!textEditor) {
statusBar.setActive(false);
Expand Down