Skip to content

Commit e39ca68

Browse files
authored
Use rbenv executable directly if available (#2824)
1 parent d4064db commit e39ca68

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

vscode/src/ruby/rbenv.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable no-process-env */
2+
import * as vscode from "vscode";
23

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

@@ -7,7 +8,14 @@ import { VersionManager, ActivationResult } from "./versionManager";
78
// Learn more: https://github.com/rbenv/rbenv
89
export class Rbenv extends VersionManager {
910
async activate(): Promise<ActivationResult> {
10-
const parsedResult = await this.runEnvActivationScript("rbenv exec ruby");
11+
const rbenvExec = await this.findExec(
12+
[vscode.Uri.file("/opt/homebrew/bin"), vscode.Uri.file("/usr/local/bin")],
13+
"rbenv",
14+
);
15+
16+
const parsedResult = await this.runEnvActivationScript(
17+
`${rbenvExec} exec ruby`,
18+
);
1119

1220
return {
1321
env: { ...process.env, ...parsedResult.env },

vscode/src/ruby/shadowenv.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export class Shadowenv extends VersionManager {
2222
);
2323
}
2424

25-
const shadowenvExec = await this.findShadowenvExec();
25+
const shadowenvExec = await this.findExec(
26+
[vscode.Uri.file("/opt/homebrew/bin")],
27+
"shadowenv",
28+
);
2629

2730
try {
2831
const parsedResult = await this.runEnvActivationScript(
@@ -71,24 +74,4 @@ export class Shadowenv extends VersionManager {
7174
);
7275
}
7376
}
74-
75-
// Tries to find the Shadowenv executable either directly in known paths or in the PATH
76-
async findShadowenvExec() {
77-
// If we can find the Shadowenv executable in the Homebrew installation path, then prefer it over relying on the
78-
// executable being available in the PATH. Sometimes, users might have shell scripts or other extensions that can
79-
// mess up the PATH and then we can't find the Shadowenv executable
80-
const possibleUris = [vscode.Uri.file("/opt/homebrew/bin/shadowenv")];
81-
82-
for (const uri of possibleUris) {
83-
try {
84-
await vscode.workspace.fs.stat(uri);
85-
this.outputChannel.info(`Found Shadowenv executable at ${uri.fsPath}`);
86-
return uri.fsPath;
87-
} catch (error: any) {
88-
// continue searching
89-
}
90-
}
91-
92-
return "shadowenv";
93-
}
9477
}

vscode/src/ruby/versionManager.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,23 @@ export abstract class VersionManager {
105105
env: process.env,
106106
});
107107
}
108+
109+
// Tries to find `execName` within the given directories. Prefers the executables found in the given directories over
110+
// finding the executable in the PATH
111+
protected async findExec(directories: vscode.Uri[], execName: string) {
112+
for (const uri of directories) {
113+
try {
114+
const fullUri = vscode.Uri.joinPath(uri, execName);
115+
await vscode.workspace.fs.stat(fullUri);
116+
this.outputChannel.info(
117+
`Found ${execName} executable at ${uri.fsPath}`,
118+
);
119+
return fullUri.fsPath;
120+
} catch (error: any) {
121+
// continue searching
122+
}
123+
}
124+
125+
return execName;
126+
}
108127
}

0 commit comments

Comments
 (0)