Skip to content

Commit

Permalink
Try all path, consider useBundled import strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jul 2, 2024
1 parent 3b56f9a commit 9a2654b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 35 deletions.
54 changes: 51 additions & 3 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,67 @@
import * as path from "path";

const folderName = path.basename(__dirname);

/**
* Path to the root directory of this extension.
*/
export const EXTENSION_ROOT_DIR =
folderName === "common" ? path.dirname(path.dirname(__dirname)) : path.dirname(__dirname);

/**
* Name of the `ruff` binary based on the current platform.
*/
export const RUFF_BINARY_NAME = process.platform == "win32" ? "ruff.exe" : "ruff";

/**
* Path to the directory containing the bundled Python scripts.
*/
export const BUNDLED_PYTHON_SCRIPTS_DIR = path.join(EXTENSION_ROOT_DIR, "bundled");
export const SERVER_SCRIPT_PATH = path.join(BUNDLED_PYTHON_SCRIPTS_DIR, "tool", `server.py`);

/**
* Path to the `ruff` executable that is bundled with the extension.
*/
export const BUNDLED_RUFF_EXECUTABLE = path.join(
BUNDLED_PYTHON_SCRIPTS_DIR,
"libs",
"bin",
RUFF_BINARY_NAME,
);

/**
* Path to the Python script that starts the `ruff-lsp` language server.
*/
export const RUFF_LSP_SERVER_SCRIPT_PATH = path.join(
BUNDLED_PYTHON_SCRIPTS_DIR,
"tool",
`server.py`,
);

export const DEBUG_SERVER_SCRIPT_PATH = path.join(
BUNDLED_PYTHON_SCRIPTS_DIR,
"tool",
`_debug_server.py`,
);
export const EXPERIMENTAL_SERVER_SCRIPT_PATH = path.join(

/**
* Path to the Python script that starts the native language server with an
* appropriate `ruff` executable.
*
* This should only be used as a fallback if the user has not specified either
* the `path` setting or is not using the bundled import strategy.
*/
export const NATIVE_SERVER_SCRIPT_PATH = path.join(
BUNDLED_PYTHON_SCRIPTS_DIR,
"tool",
"ruff_server.py",
);
export const RUFF_SERVER_CMD = "server";

/**
* The subcommand for the `ruff` binary that starts the language server.
*/
export const RUFF_SERVER_SUBCOMMAND = "server";

/**
* Required arguments for the `ruff server` command.
*/
export const RUFF_SERVER_REQUIRED_ARGS = ["--preview"];
77 changes: 45 additions & 32 deletions src/common/server.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import * as fsapi from "fs-extra";
import { Disposable, env, l10n, LanguageStatusSeverity, LogOutputChannel } from "vscode";
import { Disposable, l10n, LanguageStatusSeverity, LogOutputChannel } from "vscode";
import { State } from "vscode-languageclient";
import {
Executable,
LanguageClient,
LanguageClientOptions,
RevealOutputChannelOn,
ServerOptions,
} from "vscode-languageclient/node";
import {
BUNDLED_PYTHON_SCRIPTS_DIR,
BUNDLED_RUFF_EXECUTABLE,
DEBUG_SERVER_SCRIPT_PATH,
RUFF_SERVER_REQUIRED_ARGS,
RUFF_SERVER_CMD,
SERVER_SCRIPT_PATH,
EXPERIMENTAL_SERVER_SCRIPT_PATH,
RUFF_SERVER_SUBCOMMAND,
RUFF_LSP_SERVER_SCRIPT_PATH,
NATIVE_SERVER_SCRIPT_PATH,
} from "./constants";
import { traceError, traceInfo, traceVerbose } from "./log/logging";
import { getDebuggerPath } from "./python";
Expand All @@ -32,40 +33,52 @@ export type IInitOptions = {
globalSettings: ISettings;
};

function createNativeServerOptions(settings: ISettings): Executable {
// 'path' setting takes priority over everything.
if (settings.path.length > 0) {
for (const path of settings.path) {
if (fsapi.existsSync(path)) {
traceInfo(`Using 'path' setting: ${path}`);
return {
command: path,
args: [RUFF_SERVER_SUBCOMMAND, ...RUFF_SERVER_REQUIRED_ARGS],
options: { cwd: settings.cwd, env: process.env },
};
}
}
traceInfo(`Could not find executable in 'path': ${settings.path}`);
}

if (settings.importStrategy === "useBundled") {
traceInfo(`Using bundled executable: ${BUNDLED_RUFF_EXECUTABLE}`);
return {
command: BUNDLED_RUFF_EXECUTABLE,
args: [RUFF_SERVER_SUBCOMMAND, ...RUFF_SERVER_REQUIRED_ARGS],
options: { cwd: settings.cwd, env: process.env },
};
}

// Otherwise, we'll call a Python script that tries to locate a binary,
// falling back to the bundled version if no local executable is found.
return {
command: settings.interpreter[0],
args: [NATIVE_SERVER_SCRIPT_PATH, RUFF_SERVER_SUBCOMMAND, ...RUFF_SERVER_REQUIRED_ARGS],
options: { cwd: settings.cwd, env: process.env },
};
}

async function createNativeServer(
settings: ISettings,
serverId: string,
serverName: string,
outputChannel: LogOutputChannel,
initializationOptions: IInitOptions,
): Promise<LanguageClient> {
let serverOptions: ServerOptions;
// If the user provided a binary path, we'll try to call that path directly.
if (settings.path[0]) {
const command = settings.path[0];
const cwd = settings.cwd;
const args = [RUFF_SERVER_CMD, ...RUFF_SERVER_REQUIRED_ARGS];
serverOptions = {
command,
args,
options: { cwd, env: process.env },
};

traceInfo(`Server run command: ${[command, ...args].join(" ")}`);
let serverOptions = createNativeServerOptions(settings);
if (serverOptions.args) {
traceInfo(`Server run command: ${[serverOptions.command, ...serverOptions.args].join(" ")}`);
} else {
// Otherwise, we'll call a Python script that tries to locate
// a binary, falling back to the bundled version if no local executable is found.
const command = settings.interpreter[0];
const cwd = settings.cwd;
const args = [EXPERIMENTAL_SERVER_SCRIPT_PATH, RUFF_SERVER_CMD, ...RUFF_SERVER_REQUIRED_ARGS];

serverOptions = {
command,
args,
options: { cwd, env: process.env },
};

traceInfo(`Server run command: ${[command, ...args].join(" ")}`);
traceInfo(`Server run command: ${serverOptions.command}`);
}

const clientOptions = {
Expand Down Expand Up @@ -112,7 +125,7 @@ async function createServer(

const args =
newEnv.USE_DEBUGPY === "False" || !isDebugScript
? settings.interpreter.slice(1).concat([SERVER_SCRIPT_PATH])
? settings.interpreter.slice(1).concat([RUFF_LSP_SERVER_SCRIPT_PATH])
: settings.interpreter.slice(1).concat([DEBUG_SERVER_SCRIPT_PATH]);
traceInfo(`Server run command: ${[command, ...args].join(" ")}`);

Expand Down

0 comments on commit 9a2654b

Please sign in to comment.