Skip to content

Commit

Permalink
Fix launcher file paths on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Jan 23, 2025
1 parent 20c3979 commit de6ec13
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 108 deletions.
5 changes: 4 additions & 1 deletion exe/ruby-lsp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ if ENV["BUNDLE_GEMFILE"].nil?
# which gives us the opportunity to control which specs are activated and enter degraded mode if any gems failed to
# install rather than failing to boot the server completely
if options[:launcher]
command = +"#{Gem.ruby} #{File.expand_path("ruby-lsp-launcher", __dir__)}"
# Run `/path/to/ruby /path/to/exe/ruby-lsp-launcher` and ensuring that the Windows long format path is normalized
# for exec
command = +"#{Gem.ruby.delete_prefix("//?/")} "
command << File.expand_path("ruby-lsp-launcher", __dir__).delete_prefix("//?/")
command << " --debug" if options[:debug]
exit exec(command)
end
Expand Down
7 changes: 5 additions & 2 deletions vscode/src/ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,11 @@ export class Ruby implements RubyInterface {

this.sanitizeEnvironment(env);

// We need to set the process environment too to make other extensions such as Sorbet find the right Ruby paths
process.env = env;
if (this.context.extensionMode !== vscode.ExtensionMode.Test) {
// We need to set the process environment too to make other extensions such as Sorbet find the right Ruby paths
process.env = env;
}

this._env = env;
this.rubyVersion = version;
this.yjitEnabled = (yjit && major > 3) || (major === 3 && minor >= 2);
Expand Down
93 changes: 11 additions & 82 deletions vscode/src/test/suite/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,10 @@ import { after, afterEach, before } from "mocha";
import { Ruby, ManagerIdentifier } from "../../ruby";
import Client from "../../client";
import { WorkspaceChannel } from "../../workspaceChannel";
import { RUBY_VERSION, MAJOR, MINOR } from "../rubyVersion";
import { MAJOR, MINOR } from "../rubyVersion";

import { FAKE_TELEMETRY } from "./fakeTelemetry";

class FakeLogger {
receivedMessages = "";

trace(message: string, ..._args: any[]): void {
this.receivedMessages += message;
}

debug(message: string, ..._args: any[]): void {
this.receivedMessages += message;
}

info(message: string, ..._args: any[]): void {
this.receivedMessages += message;
}

warn(message: string, ..._args: any[]): void {
this.receivedMessages += message;
}

error(error: string | Error, ..._args: any[]): void {
this.receivedMessages += error.toString();
}

append(value: string): void {
this.receivedMessages += value;
}

appendLine(value: string): void {
this.receivedMessages += value;
}
}
import { FAKE_TELEMETRY, FakeLogger } from "./fakeTelemetry";
import { createRubySymlinks } from "./helpers";

async function launchClient(workspaceUri: vscode.Uri) {
const workspaceFolder: vscode.WorkspaceFolder = {
Expand All @@ -85,6 +54,8 @@ async function launchClient(workspaceUri: vscode.Uri) {
const fakeLogger = new FakeLogger();
const outputChannel = new WorkspaceChannel("fake", fakeLogger as any);

let managerConfig;

// Ensure that we're activating the correct Ruby version on CI
if (process.env.CI) {
await vscode.workspace
Expand All @@ -94,54 +65,12 @@ async function launchClient(workspaceUri: vscode.Uri) {
.getConfiguration("rubyLsp")
.update("linters", ["rubocop_internal"], true);

if (os.platform() === "linux") {
await vscode.workspace
.getConfiguration("rubyLsp")
.update(
"rubyVersionManager",
{ identifier: ManagerIdentifier.Chruby },
true,
);

fs.mkdirSync(path.join(os.homedir(), ".rubies"), { recursive: true });
fs.symlinkSync(
`/opt/hostedtoolcache/Ruby/${RUBY_VERSION}/x64`,
path.join(os.homedir(), ".rubies", RUBY_VERSION),
);
} else if (os.platform() === "darwin") {
await vscode.workspace
.getConfiguration("rubyLsp")
.update(
"rubyVersionManager",
{ identifier: ManagerIdentifier.Chruby },
true,
);

fs.mkdirSync(path.join(os.homedir(), ".rubies"), { recursive: true });
fs.symlinkSync(
`/Users/runner/hostedtoolcache/Ruby/${RUBY_VERSION}/arm64`,
path.join(os.homedir(), ".rubies", RUBY_VERSION),
);
createRubySymlinks();

if (os.platform() === "win32") {
managerConfig = { identifier: ManagerIdentifier.RubyInstaller };
} else {
await vscode.workspace
.getConfiguration("rubyLsp")
.update(
"rubyVersionManager",
{ identifier: ManagerIdentifier.RubyInstaller },
true,
);

fs.symlinkSync(
path.join(
"C:",
"hostedtoolcache",
"windows",
"Ruby",
RUBY_VERSION,
"x64",
),
path.join("C:", `Ruby${MAJOR}${MINOR}-${os.arch()}`),
);
managerConfig = { identifier: ManagerIdentifier.Chruby };
}
}

Expand All @@ -151,7 +80,7 @@ async function launchClient(workspaceUri: vscode.Uri) {
outputChannel,
FAKE_TELEMETRY,
);
await ruby.activateRuby();
await ruby.activateRuby(managerConfig);
ruby.env.RUBY_LSP_BYPASS_TYPECHECKER = "true";

const virtualDocuments = new Map<string, string>();
Expand Down
53 changes: 30 additions & 23 deletions vscode/src/test/suite/debugger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from "path";
import * as os from "os";

import * as vscode from "vscode";
import sinon from "sinon";
import { afterEach, beforeEach } from "mocha";

import { Debugger } from "../../debugger";
import { Ruby, ManagerIdentifier } from "../../ruby";
Expand All @@ -14,8 +14,25 @@ import { LOG_CHANNEL, asyncExec } from "../../common";
import { RUBY_VERSION } from "../rubyVersion";

import { FAKE_TELEMETRY } from "./fakeTelemetry";
import { createRubySymlinks } from "./helpers";

suite("Debugger", () => {
const original = vscode.workspace
.getConfiguration("debug")
.get("saveBeforeStart");

beforeEach(async () => {
await vscode.workspace
.getConfiguration("debug")
.update("saveBeforeStart", "none", true);
});

afterEach(async () => {
await vscode.workspace
.getConfiguration("debug")
.update("saveBeforeStart", original, true);
});

test("Provide debug configurations returns the default configs", () => {
const context = { subscriptions: [] } as unknown as vscode.ExtensionContext;
const debug = new Debugger(context, () => {
Expand Down Expand Up @@ -161,26 +178,15 @@ suite("Debugger", () => {
});

test("Launching the debugger", async () => {
// eslint-disable-next-line no-process-env
const manager = process.env.CI
? ManagerIdentifier.None
: ManagerIdentifier.Chruby;
const manager =
os.platform() === "win32"
? { identifier: ManagerIdentifier.RubyInstaller }
: { identifier: ManagerIdentifier.Chruby };

const configStub = sinon
.stub(vscode.workspace, "getConfiguration")
.returns({
get: (name: string) => {
if (name === "rubyVersionManager") {
return { identifier: manager };
} else if (name === "bundleGemfile") {
return "";
} else if (name === "saveBeforeStart") {
return "none";
}

return undefined;
},
} as unknown as vscode.WorkspaceConfiguration);
// eslint-disable-next-line no-process-env
if (process.env.CI) {
createRubySymlinks();
}

const tmpPath = fs.mkdtempSync(
path.join(os.tmpdir(), "ruby-lsp-test-debugger"),
Expand All @@ -205,13 +211,14 @@ suite("Debugger", () => {
name: path.basename(tmpPath),
index: 0,
};

const ruby = new Ruby(
context,
workspaceFolder,
outputChannel,
FAKE_TELEMETRY,
);
await ruby.activateRuby();
await ruby.activateRuby(manager);

try {
await asyncExec("bundle install", { env: ruby.env, cwd: tmpPath });
Expand Down Expand Up @@ -247,11 +254,11 @@ suite("Debugger", () => {
// the termination callback or else we try to dispose of the debugger client too early, but we need to wait for that
// so that we can clean up stubs otherwise they leak into other tests.
await new Promise<void>((resolve) => {
vscode.debug.onDidTerminateDebugSession((_session) => {
configStub.restore();
const callback = vscode.debug.onDidTerminateDebugSession((_session) => {
debug.dispose();
context.subscriptions.forEach((subscription) => subscription.dispose());
fs.rmSync(tmpPath, { recursive: true, force: true });
callback.dispose();
resolve();
});
});
Expand Down
32 changes: 32 additions & 0 deletions vscode/src/test/suite/fakeTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,35 @@ export const FAKE_TELEMETRY = vscode.env.createTelemetryLogger(
ignoreUnhandledErrors: true,
},
);

export class FakeLogger {
receivedMessages = "";

trace(message: string, ..._args: any[]): void {
this.receivedMessages += message;
}

debug(message: string, ..._args: any[]): void {
this.receivedMessages += message;
}

info(message: string, ..._args: any[]): void {
this.receivedMessages += message;
}

warn(message: string, ..._args: any[]): void {
this.receivedMessages += message;
}

error(error: string | Error, ..._args: any[]): void {
this.receivedMessages += error.toString();
}

append(value: string): void {
this.receivedMessages += value;
}

appendLine(value: string): void {
this.receivedMessages += value;
}
}
43 changes: 43 additions & 0 deletions vscode/src/test/suite/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable no-process-env */
import path from "path";
import os from "os";
import fs from "fs";

import { MAJOR, MINOR, RUBY_VERSION } from "../rubyVersion";

export function createRubySymlinks() {
if (os.platform() === "linux") {
const linkPath = path.join(os.homedir(), ".rubies", RUBY_VERSION);

if (!fs.existsSync(linkPath)) {
fs.mkdirSync(path.join(os.homedir(), ".rubies"), { recursive: true });
fs.symlinkSync(`/opt/hostedtoolcache/Ruby/${RUBY_VERSION}/x64`, linkPath);
}
} else if (os.platform() === "darwin") {
const linkPath = path.join(os.homedir(), ".rubies", RUBY_VERSION);

if (!fs.existsSync(linkPath)) {
fs.mkdirSync(path.join(os.homedir(), ".rubies"), { recursive: true });
fs.symlinkSync(
`/Users/runner/hostedtoolcache/Ruby/${RUBY_VERSION}/arm64`,
linkPath,
);
}
} else {
const linkPath = path.join("C:", `Ruby${MAJOR}${MINOR}-${os.arch()}`);

if (!fs.existsSync(linkPath)) {
fs.symlinkSync(
path.join(
"C:",
"hostedtoolcache",
"windows",
"Ruby",
RUBY_VERSION,
"x64",
),
linkPath,
);
}
}
}
Loading

0 comments on commit de6ec13

Please sign in to comment.