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

Use rbenv executable directly if available #2824

Merged
merged 1 commit into from
Nov 5, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading