Skip to content

Commit

Permalink
Use rbenv executable directly if available (#2824)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock authored Nov 5, 2024
1 parent d4064db commit e39ca68
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
10 changes: 9 additions & 1 deletion vscode/src/ruby/rbenv.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable no-process-env */
import * as vscode from "vscode";

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

Expand All @@ -7,7 +8,14 @@ import { VersionManager, ActivationResult } from "./versionManager";
// Learn more: https://github.com/rbenv/rbenv
export class Rbenv extends VersionManager {
async activate(): Promise<ActivationResult> {
const parsedResult = await this.runEnvActivationScript("rbenv exec ruby");
const rbenvExec = await this.findExec(
[vscode.Uri.file("/opt/homebrew/bin"), vscode.Uri.file("/usr/local/bin")],
"rbenv",
);

const parsedResult = await this.runEnvActivationScript(
`${rbenvExec} exec ruby`,
);

return {
env: { ...process.env, ...parsedResult.env },
Expand Down
25 changes: 4 additions & 21 deletions vscode/src/ruby/shadowenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export class Shadowenv extends VersionManager {
);
}

const shadowenvExec = await this.findShadowenvExec();
const shadowenvExec = await this.findExec(
[vscode.Uri.file("/opt/homebrew/bin")],
"shadowenv",
);

try {
const parsedResult = await this.runEnvActivationScript(
Expand Down Expand Up @@ -71,24 +74,4 @@ export class Shadowenv extends VersionManager {
);
}
}

// Tries to find the Shadowenv executable either directly in known paths or in the PATH
async findShadowenvExec() {
// If we can find the Shadowenv executable in the Homebrew installation path, then prefer it over relying on the
// executable being available in the PATH. Sometimes, users might have shell scripts or other extensions that can
// mess up the PATH and then we can't find the Shadowenv executable
const possibleUris = [vscode.Uri.file("/opt/homebrew/bin/shadowenv")];

for (const uri of possibleUris) {
try {
await vscode.workspace.fs.stat(uri);
this.outputChannel.info(`Found Shadowenv executable at ${uri.fsPath}`);
return uri.fsPath;
} catch (error: any) {
// continue searching
}
}

return "shadowenv";
}
}
19 changes: 19 additions & 0 deletions vscode/src/ruby/versionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,23 @@ export abstract class VersionManager {
env: process.env,
});
}

// Tries to find `execName` within the given directories. Prefers the executables found in the given directories over
// finding the executable in the PATH
protected async findExec(directories: vscode.Uri[], execName: string) {
for (const uri of directories) {
try {
const fullUri = vscode.Uri.joinPath(uri, execName);
await vscode.workspace.fs.stat(fullUri);
this.outputChannel.info(
`Found ${execName} executable at ${uri.fsPath}`,
);
return fullUri.fsPath;
} catch (error: any) {
// continue searching
}
}

return execName;
}
}

0 comments on commit e39ca68

Please sign in to comment.