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

Set shell and env for ASDF activation #2006

Merged
merged 3 commits into from
May 30, 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
57 changes: 2 additions & 55 deletions vscode/src/ruby/asdf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-process-env */

import path from "path";
import os from "os";

import * as vscode from "vscode";
Expand All @@ -15,79 +14,27 @@ import { VersionManager, ActivationResult } from "./versionManager";
export class Asdf extends VersionManager {
async activate(): Promise<ActivationResult> {
const asdfUri = await this.findAsdfInstallation();
const asdfDaraDirUri = await this.findAsdfDataDir();
const activationScript =
"STDERR.print({env: ENV.to_h,yjit:!!defined?(RubyVM::YJIT),version:RUBY_VERSION}.to_json)";

const result = await asyncExec(
`. ${asdfUri.fsPath} && asdf exec ruby -W0 -rjson -e '${activationScript}'`,
{
cwd: this.bundleUri.fsPath,
env: {
ASDF_DIR: path.dirname(asdfUri.fsPath),
ASDF_DATA_DIR: asdfDaraDirUri.fsPath,
},
shell: vscode.env.shell,
env: process.env,
},
);

const parsedResult = this.parseWithErrorHandling(result.stderr);

// ASDF does not set GEM_HOME or GEM_PATH. It also does not add the gem bin directories to the PATH. Instead, it
// adds its shims directory to the PATH, where all gems have a shim that invokes the gem's executable with the right
// version
parsedResult.env.PATH = [
vscode.Uri.joinPath(asdfDaraDirUri, "shims").fsPath,
parsedResult.env.PATH,
].join(path.delimiter);

return {
env: { ...process.env, ...parsedResult.env },
yjit: parsedResult.yjit,
version: parsedResult.version,
};
}

// Find the ASDF data directory. The default is for this to be in the same directories where we'd find the asdf.sh
// file, but that may not be the case for a Homebrew installation, in which case the we'd have
// `/opt/homebrew/opt/asdf/libexec/asdf.sh`, but the data directory might be `~/.asdf`
async findAsdfDataDir(): Promise<vscode.Uri> {
const possiblePaths = [
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".asdf"),
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "asdf-vm"),
vscode.Uri.joinPath(
vscode.Uri.file("/"),
"opt",
"homebrew",
"opt",
"asdf",
"libexec",
),
vscode.Uri.joinPath(
vscode.Uri.file("/"),
"usr",
"local",
"opt",
"asdf",
"libexec",
),
];

for (const possiblePath of possiblePaths) {
try {
await vscode.workspace.fs.stat(
vscode.Uri.joinPath(possiblePath, "shims"),
);
return possiblePath;
} catch (error: any) {
// Continue looking
}
}

throw new Error(
`Could not find ASDF data dir. Searched in ${possiblePaths.join(", ")}`,
);
}

// Only public for testing. Finds the ASDF installation URI based on what's advertised in the ASDF documentation
async findAsdfInstallation(): Promise<vscode.Uri> {
// Possible ASDF installation paths as described in https://asdf-vm.com/guide/getting-started.html#_3-install-asdf.
Expand Down
14 changes: 5 additions & 9 deletions vscode/src/test/suite/ruby/asdf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ suite("Asdf", () => {
const findInstallationStub = sinon
.stub(asdf, "findAsdfInstallation")
.resolves(vscode.Uri.file(`${os.homedir()}/.asdf/asdf.sh`));
const findDataDirStub = sinon
.stub(asdf, "findAsdfDataDir")
.resolves(vscode.Uri.file(`${os.homedir()}/.asdf`));
const shellStub = sinon.stub(vscode.env, "shell").get(() => "/bin/bash");

const { env, version, yjit } = await asdf.activate();

Expand All @@ -52,21 +50,19 @@ suite("Asdf", () => {
`. ${os.homedir()}/.asdf/asdf.sh && asdf exec ruby -W0 -rjson -e '${activationScript}'`,
{
cwd: workspacePath,
env: {
ASDF_DIR: `${os.homedir()}/.asdf`,
ASDF_DATA_DIR: `${os.homedir()}/.asdf`,
},
shell: "/bin/bash",
// eslint-disable-next-line no-process-env
env: process.env,
},
),
);

assert.strictEqual(version, "3.0.0");
assert.strictEqual(yjit, true);
assert.ok(env.PATH!.includes(`${os.homedir()}/.asdf/shims`));
assert.strictEqual(env.ANY, "true");

execStub.restore();
findInstallationStub.restore();
findDataDirStub.restore();
shellStub.restore();
});
});
Loading