Skip to content

Commit

Permalink
Use bundled environment activation script file
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Jan 27, 2025
1 parent 0ee2ba7 commit 8c37f81
Show file tree
Hide file tree
Showing 18 changed files with 300 additions and 51 deletions.
2 changes: 2 additions & 0 deletions vscode/activation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env = { env: ENV.to_h, yjit: !!defined?(RubyVM::YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json
STDERR.print("RUBY_LSP_ACTIVATION_SEPARATOR#{env}RUBY_LSP_ACTIVATION_SEPARATOR")
2 changes: 1 addition & 1 deletion vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@
}
},
"scripts": {
"vscode:prepublish": "yarn run esbuild-base --minify",
"vscode:prepublish": "yarn run esbuild-base --minify && cp activation.rb out/activation.rb",
"package": "vsce package --out vscode-ruby-lsp.vsix --baseImagesUrl https://github.com/Shopify/ruby-lsp/raw/HEAD/vscode",
"package_prerelease": "vsce package --pre-release --out vscode-ruby-lsp.vsix --baseImagesUrl https://github.com/Shopify/ruby-lsp/raw/HEAD/vscode",
"esbuild-base": "esbuild ./src/extension.ts --bundle --outfile=out/extension.js --external:vscode --format=cjs --platform=node",
Expand Down
11 changes: 11 additions & 0 deletions vscode/src/ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class Ruby implements RubyInterface {
new None(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
workspaceRubyPath,
),
Expand Down Expand Up @@ -165,6 +166,7 @@ export class Ruby implements RubyInterface {
new None(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
globalRubyPath,
),
Expand Down Expand Up @@ -318,6 +320,7 @@ export class Ruby implements RubyInterface {
new Asdf(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
),
);
Expand All @@ -327,6 +330,7 @@ export class Ruby implements RubyInterface {
new Chruby(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
),
);
Expand All @@ -336,6 +340,7 @@ export class Ruby implements RubyInterface {
new Rbenv(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
),
);
Expand All @@ -345,6 +350,7 @@ export class Ruby implements RubyInterface {
new Rvm(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
),
);
Expand All @@ -354,6 +360,7 @@ export class Ruby implements RubyInterface {
new Mise(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
),
);
Expand All @@ -363,6 +370,7 @@ export class Ruby implements RubyInterface {
new RubyInstaller(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
),
);
Expand All @@ -372,6 +380,7 @@ export class Ruby implements RubyInterface {
new Custom(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
),
);
Expand All @@ -381,6 +390,7 @@ export class Ruby implements RubyInterface {
new None(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
),
);
Expand All @@ -390,6 +400,7 @@ export class Ruby implements RubyInterface {
new Shadowenv(
this.workspaceFolder,
this.outputChannel,
this.context,
this.manuallySelectRuby.bind(this),
),
);
Expand Down
3 changes: 2 additions & 1 deletion vscode/src/ruby/chruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export class Chruby extends VersionManager {
constructor(
workspaceFolder: vscode.WorkspaceFolder,
outputChannel: WorkspaceChannel,
context: vscode.ExtensionContext,
manuallySelectRuby: () => Promise<void>,
) {
super(workspaceFolder, outputChannel, manuallySelectRuby);
super(workspaceFolder, outputChannel, context, manuallySelectRuby);

const configuredRubies = vscode.workspace
.getConfiguration("rubyLsp")
Expand Down
3 changes: 2 additions & 1 deletion vscode/src/ruby/none.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ export class None extends VersionManager {
constructor(
workspaceFolder: vscode.WorkspaceFolder,
outputChannel: WorkspaceChannel,
context: vscode.ExtensionContext,
manuallySelectRuby: () => Promise<void>,
rubyPath?: string,
) {
super(workspaceFolder, outputChannel, manuallySelectRuby);
super(workspaceFolder, outputChannel, context, manuallySelectRuby);
this.rubyPath = rubyPath ?? "ruby";
}

Expand Down
15 changes: 8 additions & 7 deletions vscode/src/ruby/versionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,23 @@ export interface ActivationResult {
export const ACTIVATION_SEPARATOR = "RUBY_LSP_ACTIVATION_SEPARATOR";

export abstract class VersionManager {
public activationScript = [
`STDERR.print("${ACTIVATION_SEPARATOR}" + `,
"{ env: ENV.to_h, yjit: !!defined?(RubyVM::YJIT), version: RUBY_VERSION, gemPath: Gem.path }.to_json + ",
`"${ACTIVATION_SEPARATOR}")`,
].join("");

protected readonly outputChannel: WorkspaceChannel;
protected readonly workspaceFolder: vscode.WorkspaceFolder;
protected readonly bundleUri: vscode.Uri;
protected readonly manuallySelectRuby: () => Promise<void>;

private readonly context: vscode.ExtensionContext;
private readonly customBundleGemfile?: string;

constructor(
workspaceFolder: vscode.WorkspaceFolder,
outputChannel: WorkspaceChannel,
context: vscode.ExtensionContext,
manuallySelectRuby: () => Promise<void>,
) {
this.workspaceFolder = workspaceFolder;
this.outputChannel = outputChannel;
this.context = context;
this.manuallySelectRuby = manuallySelectRuby;
const customBundleGemfile: string = vscode.workspace
.getConfiguration("rubyLsp")
Expand All @@ -60,8 +57,12 @@ export abstract class VersionManager {
abstract activate(): Promise<ActivationResult>;

protected async runEnvActivationScript(activatedRuby: string) {
const activationUri = vscode.Uri.joinPath(
this.context.extensionUri,
"activation.rb",
);
const result = await this.runScript(
`${activatedRuby} -W0 -rjson -e '${this.activationScript}'`,
`${activatedRuby} -W0 -rjson '${activationUri.fsPath}'`,
);

const activationContent = new RegExp(
Expand Down
1 change: 1 addition & 0 deletions vscode/src/test/suite/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async function launchClient(workspaceUri: vscode.Uri) {
get: (_name: string) => undefined,
update: (_name: string, _value: any) => Promise.resolve(),
},
extensionUri: vscode.Uri.file(path.join(workspaceUri.fsPath, "vscode")),
} as unknown as vscode.ExtensionContext;
const fakeLogger = new FakeLogger();
const outputChannel = new WorkspaceChannel("fake", fakeLogger as any);
Expand Down
4 changes: 4 additions & 0 deletions vscode/src/test/suite/debugger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,16 @@ suite("Debugger", () => {
'source "https://rubygems.org"\ngem "debug"',
);

const extensionPath = path.dirname(
path.dirname(path.dirname(path.dirname(__dirname))),
);
const context = {
subscriptions: [],
workspaceState: {
get: () => undefined,
update: () => undefined,
},
extensionUri: vscode.Uri.file(path.join(extensionPath, "vscode")),
} as unknown as vscode.ExtensionContext;
const outputChannel = new WorkspaceChannel("fake", LOG_CHANNEL);
const workspaceFolder: vscode.WorkspaceFolder = {
Expand Down
1 change: 1 addition & 0 deletions vscode/src/test/suite/ruby.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ suite("Ruby environment activation", () => {
get: () => undefined,
update: () => undefined,
},
extensionUri: vscode.Uri.file(path.join(workspacePath, "vscode")),
} as unknown as vscode.ExtensionContext;
const outputChannel = new WorkspaceChannel("fake", LOG_CHANNEL);

Expand Down
27 changes: 23 additions & 4 deletions vscode/src/test/suite/ruby/asdf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ suite("Asdf", () => {
console.log("Skipping Asdf tests on Windows");
return;
}
const context = {
extensionMode: vscode.ExtensionMode.Test,
subscriptions: [],
workspaceState: {
get: (_name: string) => undefined,
update: (_name: string, _value: any) => Promise.resolve(),
},
extensionUri: vscode.Uri.parse("file:///fake"),
} as unknown as vscode.ExtensionContext;

test("Finds Ruby based on .tool-versions", async () => {
// eslint-disable-next-line no-process-env
Expand All @@ -26,7 +35,12 @@ suite("Asdf", () => {
index: 0,
};
const outputChannel = new WorkspaceChannel("fake", common.LOG_CHANNEL);
const asdf = new Asdf(workspaceFolder, outputChannel, async () => {});
const asdf = new Asdf(
workspaceFolder,
outputChannel,
context,
async () => {},
);
const envStub = {
env: { ANY: "true" },
yjit: true,
Expand All @@ -47,7 +61,7 @@ suite("Asdf", () => {

assert.ok(
execStub.calledOnceWithExactly(
`. ${os.homedir()}/.asdf/asdf.sh && asdf exec ruby -W0 -rjson -e '${asdf.activationScript}'`,
`. ${os.homedir()}/.asdf/asdf.sh && asdf exec ruby -W0 -rjson '/fake/activation.rb'`,
{
cwd: workspacePath,
shell: "/bin/bash",
Expand Down Expand Up @@ -75,7 +89,12 @@ suite("Asdf", () => {
index: 0,
};
const outputChannel = new WorkspaceChannel("fake", common.LOG_CHANNEL);
const asdf = new Asdf(workspaceFolder, outputChannel, async () => {});
const asdf = new Asdf(
workspaceFolder,
outputChannel,
context,
async () => {},
);
const envStub = {
env: { ANY: "true" },
yjit: true,
Expand All @@ -98,7 +117,7 @@ suite("Asdf", () => {

assert.ok(
execStub.calledOnceWithExactly(
`. ${os.homedir()}/.asdf/asdf.fish && asdf exec ruby -W0 -rjson -e '${asdf.activationScript}'`,
`. ${os.homedir()}/.asdf/asdf.fish && asdf exec ruby -W0 -rjson '/fake/activation.rb'`,
{
cwd: workspacePath,
shell: "/opt/homebrew/bin/fish",
Expand Down
Loading

0 comments on commit 8c37f81

Please sign in to comment.