Skip to content
Open
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
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.4.5
7 changes: 7 additions & 0 deletions activation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Using .map.compact just so that it doesn't crash immediately on Ruby 2.6
env = ENV.map do |k, v|
utf_8_value = v.dup.force_encoding(Encoding::UTF_8)
"#{k}RUBY_LSP_VS#{utf_8_value}" if utf_8_value.valid_encoding?
end.compact
env.unshift(RUBY_VERSION, Gem.path.join(","), !!defined?(RubyVM::YJIT))
STDERR.print("RUBY_LSP_ACTIVATION_SEPARATOR#{env.join("RUBY_LSP_FS")}RUBY_LSP_ACTIVATION_SEPARATOR")
27 changes: 27 additions & 0 deletions chruby_activation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

# Typically, GEM_HOME points to $HOME/.gem/ruby/version_without_patch. For example, for Ruby 3.2.2, it would be
# $HOME/.gem/ruby/3.2.0. However, chruby overrides GEM_HOME to use the patch part of the version, resulting in
# $HOME/.gem/ruby/3.2.2. In our activation script, we check if a directory using the patch exists and then prefer
# that over the default one.
user_dir = Gem.user_dir
paths = Gem.path
default_dir = Gem.default_dir

if paths.length > 2
paths.delete(default_dir)
paths.delete(user_dir)
first_path = paths[0]
user_dir = first_path if first_path && Dir.exist?(first_path)
end

newer_gem_home = File.join(File.dirname(user_dir), ARGV.first)
gems = Dir.exist?(newer_gem_home) ? newer_gem_home : user_dir
STDERR.print(
[
default_dir,
gems,
!!defined?(RubyVM::YJIT),
RUBY_VERSION
].join("RUBY_LSP_ACTIVATION_SEPARATOR")
)
5 changes: 5 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export default tseslint.config(
},
{
rules: {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-argument": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
Expand Down
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,23 @@
"test": "vscode-test"
},
"devDependencies": {
"@eslint/js": "^9.32.0",
"@types/mocha": "^10.0.10",
"@types/node": "20.x",
"@types/sinon": "^17.0.4",
"@types/vscode": "^1.102.0",
"@eslint/js": "^9.32.0",
"eslint": "^9.30.1",
"typescript-eslint": "^8.38.0",
"eslint-plugin-prettier": "^5.5.3",
"prettier": "^3.6.2",
"@vscode/test-cli": "^0.0.11",
"@vscode/test-electron": "^2.5.2",
"@vscode/vsce": "^3.6.0",
"esbuild": "^0.25.3",
"eslint": "^9.30.1",
"eslint-plugin-prettier": "^5.5.3",
"glob": "^11.0.3",
"npm-run-all": "^4.1.5",
"ovsx": "^0.10.5",
"typescript": "^5.8.3"
"prettier": "^3.6.2",
"sinon": "^21.0.0",
"typescript": "^5.8.3",
"typescript-eslint": "^8.38.0"
}
}
15 changes: 15 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { exec } from "child_process";
import { promisify } from "util";
import { window } from "vscode";

export interface RubyInterface {
error: boolean;
versionManager: { identifier: string };
rubyVersion?: string;
}

export const asyncExec = promisify(exec);
export const RUBY_ENVIRONMENTS = "Ruby Environments";
export const LOG_CHANNEL = window.createOutputChannel(RUBY_ENVIRONMENTS, {
log: true,
});
88 changes: 88 additions & 0 deletions src/ruby/asdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import os from "os";
import path from "path";

import * as vscode from "vscode";

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

// A tool to manage multiple runtime versions with a single CLI tool
//
// Learn more: https://github.com/asdf-vm/asdf
export class Asdf extends VersionManager {
async activate(): Promise<ActivationResult> {
// These directories are where we can find the ASDF executable for v0.16 and above
const possibleExecutablePaths = [
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "homebrew", "bin"),
vscode.Uri.joinPath(vscode.Uri.file("/"), "usr", "local", "bin"),
];

// Prefer the path configured by the user, then the ASDF scripts for versions below v0.16 and finally the
// executables for v0.16 and above
const asdfPath =
(await this.getConfiguredAsdfPath()) ??
(await this.findAsdfInstallation()) ??
(await this.findExec(possibleExecutablePaths, "asdf"));

// If there's no extension name, then we are using the ASDF executable directly. If there is an extension, then it's
// a shell script and we have to source it first
const baseCommand = path.extname(asdfPath) === "" ? asdfPath : `. ${asdfPath} && asdf`;

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

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

// Only public for testing. Finds the ASDF installation URI based on what's advertised in the ASDF documentation
async findAsdfInstallation(): Promise<string | undefined> {
const scriptName = path.basename(vscode.env.shell) === "fish" ? "asdf.fish" : "asdf.sh";

// Possible ASDF installation paths as described in https://asdf-vm.com/guide/getting-started.html#_3-install-asdf.
// In order, the methods of installation are:
// 1. Git
// 2. Pacman
// 3. Homebrew M series
// 4. Homebrew Intel series
const possiblePaths = [
vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".asdf", scriptName),
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "asdf-vm", scriptName),
vscode.Uri.joinPath(vscode.Uri.file("/"), "opt", "homebrew", "opt", "asdf", "libexec", scriptName),
vscode.Uri.joinPath(vscode.Uri.file("/"), "usr", "local", "opt", "asdf", "libexec", scriptName),
];

for (const possiblePath of possiblePaths) {
try {
await vscode.workspace.fs.stat(possiblePath);
return possiblePath.fsPath;
} catch (_error: any) {
// Continue looking
}
}

this.outputChannel.info(`Could not find installation for ASDF < v0.16. Searched in ${possiblePaths.join(", ")}`);
return undefined;
}

private async getConfiguredAsdfPath(): Promise<string | undefined> {
const config = vscode.workspace.getConfiguration("rubyLsp");
const asdfPath = config.get<string | undefined>("rubyVersionManager.asdfExecutablePath");

if (!asdfPath) {
return;
}

const configuredPath = vscode.Uri.file(asdfPath);

try {
await vscode.workspace.fs.stat(configuredPath);
this.outputChannel.info(`Using configured ASDF executable path: ${asdfPath}`);
return configuredPath.fsPath;
} catch (_error: any) {
throw new Error(`ASDF executable configured as ${configuredPath.fsPath}, but that file doesn't exist`);
}
}
}
Loading