Skip to content

Commit

Permalink
Allow configuring extra chruby paths
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Apr 30, 2024
1 parent 722e4a8 commit a5ad0d0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
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
20 changes: 20 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,23 @@ 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 chruby = new Chruby(workspaceFolder, outputChannel);
const configStub = sinon
.stub(vscode.workspace, "getConfiguration")
.returns({
get: () => [path.join(rootPath, "opt", "rubies")],
} as any);

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

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);
});
});

0 comments on commit a5ad0d0

Please sign in to comment.