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

Better error message for unsupported Ruff version #512

Merged
merged 1 commit into from
Jul 4, 2024
Merged
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
61 changes: 61 additions & 0 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,54 @@ function executeCommand(command: string): Promise<string> {
});
}

type VersionInfo = {
major: number;
minor: number;
patch: number;
};

/**
* Convert a version object to a string.
*/
function versionToString(version: VersionInfo): string {
return `${version.major}.${version.minor}.${version.patch}`;
}

/**
* The minimum version of the Ruff executable that supports the native server.
*/
const MINIMUM_RUFF_SERVER_VERSION: VersionInfo = { major: 0, minor: 3, patch: 5 };
Comment on lines +67 to +70
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes from #443 although the minimum supported version if actually 0.3.3 but I guess this is the version we moved away from using the bundled version.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As explained in #508 there are also commands/features that unsupported by old version of nativeServer and/or ruff-lsp. should we declare version constant for them? e.g. ruff.printDebugInformation which was added in astral-sh/ruff#11831 for nativeServer

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I intend to implement the warning part for the settings in #505.

ruff.printDebugInformation which was added in astral-sh/ruff#11831 for nativeServer

Regarding this command, I think we should only register a VS Code command only if it's available in the server capabilities (astral-sh/ruff#11850). This way we don't need to check the Ruff version for at least the server capabilities.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened #513 to keep track of it.


/**
* Check if the given version of the Ruff executable supports the native server.
*/
function supportsNativeServer(version: VersionInfo): boolean {
if (version.major > MINIMUM_RUFF_SERVER_VERSION.major) {
return true;
}
if (version.major === MINIMUM_RUFF_SERVER_VERSION.major) {
if (version.minor > MINIMUM_RUFF_SERVER_VERSION.minor) {
return true;
}
if (version.minor === MINIMUM_RUFF_SERVER_VERSION.minor) {
if (version.patch >= MINIMUM_RUFF_SERVER_VERSION.patch) {
return true;
}
}
}
return false;
}

/**
* Get the version of the Ruff executable at the given path.
*/
async function getRuffVersion(executable: string): Promise<VersionInfo> {
const stdout = await executeCommand(`${executable} --version`);
const version = stdout.trim().split(" ")[1];
const [major, minor, patch] = version.split(".").map((x) => parseInt(x, 10));
return { major, minor, patch };
}

/**
* Finds the Ruff binary path and returns it.
*
Expand Down Expand Up @@ -133,6 +181,19 @@ async function createNativeServer(
initializationOptions: IInitializationOptions,
): Promise<LanguageClient> {
const ruffBinaryPath = await findRuffBinaryPath(settings, outputChannel);
const ruffVersion = await getRuffVersion(ruffBinaryPath);

if (!supportsNativeServer(ruffVersion)) {
const message = `Native server requires Ruff ${versionToString(
MINIMUM_RUFF_SERVER_VERSION,
)}, but found ${versionToString(ruffVersion)} at ${ruffBinaryPath} instead`;
traceError(message);
await vscode.window.showErrorMessage(message);
return Promise.reject();
}

traceInfo(`Found Ruff ${versionToString(ruffVersion)} at ${ruffBinaryPath}`);

const ruffServerArgs = [RUFF_SERVER_SUBCOMMAND, ...RUFF_SERVER_REQUIRED_ARGS];
traceInfo(`Server run command: ${[ruffBinaryPath, ...ruffServerArgs].join(" ")}`);

Expand Down