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 c447b1c
Show file tree
Hide file tree
Showing 3 changed files with 47 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
26 changes: 25 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,15 @@ export class Chruby extends VersionManager {
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".rubies"),
];

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

this.rubyInstallationUris.push(...this.extraRubies());
}

async activate(): Promise<ActivationResult> {
const versionInfo = await this.discoverRubyVersion();
const rubyUri = await this.findRubyUri(versionInfo);
Expand All @@ -48,6 +58,19 @@ export class Chruby extends VersionManager {
};
}

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

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

return rubies;
}

// Returns the full URI to the Ruby executable
protected async findRubyUri(rubyVersion: RubyVersion): Promise<vscode.Uri> {
// If an engine was specified in the .ruby-version file, we favor looking for that first and also try just the
Expand All @@ -72,7 +95,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
18 changes: 18 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,21 @@ 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 extraRubiesStub = sinon
.stub(chruby, "extraRubies")
.returns([vscode.Uri.file(path.join(rootPath, "opt", "rubies"))]);

const { env, version, yjit } = await chruby.activate();
extraRubiesStub.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 c447b1c

Please sign in to comment.