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

Allow configuring extra chruby paths #1976

Merged
merged 1 commit into from May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions vscode/package.json
Expand Up @@ -253,6 +253,10 @@
"miseExecutablePath": {
"description": "The path to the Mise executable, if not installed in ~/.local/bin/mise",
"type": "string"
},
"chrubyRubies": {
"description": "An array of extra directories to search for Ruby installations when using chruby. Equivalent to the RUBIES environment variable",
"type": "array"
}
},
"default": {
Expand Down
21 changes: 20 additions & 1 deletion vscode/src/ruby/chruby.ts
Expand Up @@ -5,6 +5,7 @@ import path from "path";
import * as vscode from "vscode";

import { asyncExec } from "../common";
import { WorkspaceChannel } from "../workspaceChannel";

import { ActivationResult, VersionManager } from "./versionManager";

Expand All @@ -22,6 +23,23 @@ export class Chruby extends VersionManager {
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".rubies"),
];

constructor(
workspaceFolder: vscode.WorkspaceFolder,
outputChannel: WorkspaceChannel,
) {
super(workspaceFolder, outputChannel);

const configuredRubies = vscode.workspace
.getConfiguration("rubyLsp")
.get<string[] | undefined>("rubyVersionManager.chrubyRubies");

if (configuredRubies) {
this.rubyInstallationUris.push(
...configuredRubies.map((path) => vscode.Uri.file(path)),
);
}
}

async activate(): Promise<ActivationResult> {
const versionInfo = await this.discoverRubyVersion();
const rubyUri = await this.findRubyUri(versionInfo);
Expand Down Expand Up @@ -72,7 +90,8 @@ export class Chruby extends VersionManager {
}

throw new Error(
`Cannot find installation directory for Ruby version ${possibleVersionNames.join(" or ")}`,
`Cannot find installation directory for Ruby version ${possibleVersionNames.join(" or ")}.
Searched in ${this.rubyInstallationUris.map((uri) => uri.fsPath).join(", ")}`,
);
}

Expand Down
24 changes: 24 additions & 0 deletions vscode/src/test/suite/ruby/chruby.test.ts
Expand Up @@ -6,6 +6,7 @@ import os from "os";

import { before, after } from "mocha";
import * as vscode from "vscode";
import sinon from "sinon";

import { Chruby } from "../../../ruby/chruby";
import { WorkspaceChannel } from "../../../workspaceChannel";
Expand Down Expand Up @@ -157,4 +158,27 @@ suite("Chruby", () => {
assert.strictEqual(version, RUBY_VERSION);
assert.notStrictEqual(yjit, undefined);
});

test("Finds Ruby when extra RUBIES are configured", async () => {
fs.writeFileSync(path.join(workspacePath, ".ruby-version"), RUBY_VERSION);

const configStub = sinon
.stub(vscode.workspace, "getConfiguration")
.returns({
get: (name: string) =>
name === "rubyVersionManager.chrubyRubies"
? [path.join(rootPath, "opt", "rubies")]
: "",
} as any);

const chruby = new Chruby(workspaceFolder, outputChannel);
configStub.restore();

const { env, version, yjit } = await chruby.activate();

assert.match(env.GEM_PATH!, new RegExp(`ruby/${VERSION_REGEX}`));
assert.match(env.GEM_PATH!, new RegExp(`lib/ruby/gems/${VERSION_REGEX}`));
assert.strictEqual(version, RUBY_VERSION);
assert.notStrictEqual(yjit, undefined);
});
});