From 3a9aa80502e9be1bc9341393cb53804843f8e834 Mon Sep 17 00:00:00 2001 From: Vladimir Serov Date: Thu, 21 May 2020 18:26:50 +0300 Subject: editors/vscode: added patchelf after download --- editors/code/src/main.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) (limited to 'editors') diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index ac3bb365e..0a234cb84 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -1,7 +1,7 @@ import * as vscode from 'vscode'; import * as path from "path"; import * as os from "os"; -import { promises as fs } from "fs"; +import { promises as fs, PathLike } from "fs"; import * as commands from './commands'; import { activateInlayHints } from './inlay_hints'; @@ -12,6 +12,7 @@ import { log, assert, isValidExecutable } from './util'; import { PersistentState } from './persistent_state'; import { fetchRelease, download } from './net'; import { activateTaskProvider } from './tasks'; +import { exec } from 'child_process'; let ctx: Ctx | undefined; @@ -188,6 +189,46 @@ async function bootstrapServer(config: Config, state: PersistentState): Promise< return path; } +async function patchelf(dest: PathLike): Promise { + await vscode.window.withProgress( + { + location: vscode.ProgressLocation.Notification, + title: "Patching rust-analysis for NixOS" + }, + async (progress, _) => { + let patch_path = path.join(os.tmpdir(), "patch-ra.nix") + progress.report({message: "Writing nix file", increment: 5}) + await fs.writeFile(patch_path, ` + {src, pkgs ? import {}}: + pkgs.stdenv.mkDerivation { + name = "rust-analyzer"; + inherit src; + phases = [ "installPhase" "fixupPhase" ]; + installPhase = "cp $src $out"; + fixupPhase = '' + chmod 755 $out + patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" $out + ''; + } + `) + let orig_file = dest + "-orig" + await fs.rename(dest, orig_file) + progress.report({message: "Patching executable", increment: 20}) + await new Promise((resolve, reject) => { + exec(`nix-build ${patch_path} --arg src '${orig_file}' -o ${dest}`, + (err, stdout, stderr) => { + if (err != null) { + reject(Error(stderr)) + } else { + resolve(stdout) + } + }) + }) + // await fs.unlink(orig_file) + } + ) +} + async function getServer(config: Config, state: PersistentState): Promise { const explicitPath = process.env.__RA_LSP_SERVER_DEBUG ?? config.serverPath; if (explicitPath) { @@ -237,6 +278,12 @@ async function getServer(config: Config, state: PersistentState): Promise true).catch(_ => false)) { + await patchelf(dest) + } + await state.updateServerVersion(config.package.version); return dest; } -- cgit v1.2.3 From 125e4197d8af1eca498e0088aa37cf48e0b3827e Mon Sep 17 00:00:00 2001 From: Vladimir Serov Date: Thu, 21 May 2020 18:45:37 +0300 Subject: editors/vscode: removing original file after patching --- editors/code/src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors') diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 0a234cb84..0a2c30594 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -224,7 +224,7 @@ async function patchelf(dest: PathLike): Promise { } }) }) - // await fs.unlink(orig_file) + await fs.unlink(orig_file) } ) } -- cgit v1.2.3 From 8e0d776369f807c1d3f60abb899d2fe0d83570d4 Mon Sep 17 00:00:00 2001 From: Vladimir Serov Date: Thu, 21 May 2020 18:49:30 +0300 Subject: editor/vscode: lint --- editors/code/src/main.ts | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'editors') diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 0a2c30594..1c6e3093b 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -192,13 +192,13 @@ async function bootstrapServer(config: Config, state: PersistentState): Promise< async function patchelf(dest: PathLike): Promise { await vscode.window.withProgress( { - location: vscode.ProgressLocation.Notification, + location: vscode.ProgressLocation.Notification, title: "Patching rust-analysis for NixOS" - }, + }, async (progress, _) => { - let patch_path = path.join(os.tmpdir(), "patch-ra.nix") - progress.report({message: "Writing nix file", increment: 5}) - await fs.writeFile(patch_path, ` + const patchPath = path.join(os.tmpdir(), "patch-ra.nix"); + progress.report({ message: "Writing nix file", increment: 5 }); + await fs.writeFile(patchPath, ` {src, pkgs ? import {}}: pkgs.stdenv.mkDerivation { name = "rust-analyzer"; @@ -210,23 +210,23 @@ async function patchelf(dest: PathLike): Promise { patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" $out ''; } - `) - let orig_file = dest + "-orig" - await fs.rename(dest, orig_file) - progress.report({message: "Patching executable", increment: 20}) + `); + const origFile = dest + "-orig"; + await fs.rename(dest, origFile); + progress.report({ message: "Patching executable", increment: 20 }); await new Promise((resolve, reject) => { - exec(`nix-build ${patch_path} --arg src '${orig_file}' -o ${dest}`, - (err, stdout, stderr) => { - if (err != null) { - reject(Error(stderr)) - } else { - resolve(stdout) - } - }) - }) - await fs.unlink(orig_file) + exec(`nix-build ${patchPath} --arg src '${origFile}' -o ${dest}`, + (err, stdout, stderr) => { + if (err != null) { + reject(Error(stderr)); + } else { + resolve(stdout); + } + }); + }); + await fs.unlink(origFile); } - ) + ); } async function getServer(config: Config, state: PersistentState): Promise { @@ -281,7 +281,7 @@ async function getServer(config: Config, state: PersistentState): Promise true).catch(_ => false)) { - await patchelf(dest) + await patchelf(dest); } await state.updateServerVersion(config.package.version); -- cgit v1.2.3 From d7331b2d5dcdb2002a45ac4afe64b4d801658df5 Mon Sep 17 00:00:00 2001 From: Cabia Rangris Date: Thu, 21 May 2020 17:50:28 +0200 Subject: Update editors/code/src/main.ts Co-authored-by: Jeremy Kolb --- editors/code/src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors') diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 1c6e3093b..bfa9980be 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -193,7 +193,7 @@ async function patchelf(dest: PathLike): Promise { await vscode.window.withProgress( { location: vscode.ProgressLocation.Notification, - title: "Patching rust-analysis for NixOS" + title: "Patching rust-analyzer for NixOS" }, async (progress, _) => { const patchPath = path.join(os.tmpdir(), "patch-ra.nix"); -- cgit v1.2.3 From 757292856b5d3653926e52dd4da473b062b9a832 Mon Sep 17 00:00:00 2001 From: Vladimir Serov Date: Thu, 21 May 2020 21:30:56 +0300 Subject: editors/vscode: patchelf-ing without intermediate files --- editors/code/src/main.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'editors') diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 1c6e3093b..dbb241192 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -196,9 +196,7 @@ async function patchelf(dest: PathLike): Promise { title: "Patching rust-analysis for NixOS" }, async (progress, _) => { - const patchPath = path.join(os.tmpdir(), "patch-ra.nix"); - progress.report({ message: "Writing nix file", increment: 5 }); - await fs.writeFile(patchPath, ` + const expression = ` {src, pkgs ? import {}}: pkgs.stdenv.mkDerivation { name = "rust-analyzer"; @@ -210,12 +208,12 @@ async function patchelf(dest: PathLike): Promise { patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" $out ''; } - `); + `; const origFile = dest + "-orig"; await fs.rename(dest, origFile); progress.report({ message: "Patching executable", increment: 20 }); await new Promise((resolve, reject) => { - exec(`nix-build ${patchPath} --arg src '${origFile}' -o ${dest}`, + const handle = exec(`nix-build -E - --arg src '${origFile}' -o ${dest}`, (err, stdout, stderr) => { if (err != null) { reject(Error(stderr)); @@ -223,6 +221,8 @@ async function patchelf(dest: PathLike): Promise { resolve(stdout); } }); + handle.stdin?.write(expression); + handle.stdin?.end(); }); await fs.unlink(origFile); } -- cgit v1.2.3 From ec5162fa7f2344ea86e11237383cb9d5ce5707aa Mon Sep 17 00:00:00 2001 From: Vladimir Serov Date: Thu, 21 May 2020 21:32:27 +0300 Subject: editors/vscode: forgotten await in os check --- editors/code/src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors') diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index dbb241192..42685e0c7 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -280,7 +280,7 @@ async function getServer(config: Config, state: PersistentState): Promise true).catch(_ => false)) { + if (await fs.stat("/etc/nixos").then(_ => true).catch(_ => false)) { await patchelf(dest); } -- cgit v1.2.3