aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-14 11:30:10 +0100
committerGitHub <[email protected]>2021-06-14 11:30:10 +0100
commit388a91c8a8d542f7a8e0ddff879cce4d4c2b20ae (patch)
treea4bd12cfebe76928271d7d999dd0ed744a89ca17 /editors/code/src
parenta274ae384e38be5ad1b23cd2b7f2120e5a284209 (diff)
parent388a1945accb98eaa4e612645075ef08317f5781 (diff)
Merge #8951
8951: internal: migrate to vscode.FileSystem API r=matklad a=wxb1ank I encountered an error where `bootstrap()` attempts to create a directory with the path `C:\C:\...`. I couldn't find this reported anywhere else. Using the `vscode.FileSystem` API instead of the `fs` one works here. I assume the latter automatically prepends `C:\` to paths whereas the former does not. I don't know if this suggests `vscode.FileSystem` should be used in more places for consistency. Co-authored-by: wxb1ank <[email protected]>
Diffstat (limited to 'editors/code/src')
-rw-r--r--editors/code/src/config.ts4
-rw-r--r--editors/code/src/main.ts32
-rw-r--r--editors/code/src/net.ts30
-rw-r--r--editors/code/src/toolchain.ts19
4 files changed, 38 insertions, 47 deletions
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 1f1fe59a4..2277eeb7e 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -31,10 +31,10 @@ export class Config {
31 enableProposedApi: boolean | undefined; 31 enableProposedApi: boolean | undefined;
32 } = vscode.extensions.getExtension(this.extensionId)!.packageJSON; 32 } = vscode.extensions.getExtension(this.extensionId)!.packageJSON;
33 33
34 readonly globalStoragePath: string; 34 readonly globalStorageUri: vscode.Uri;
35 35
36 constructor(ctx: vscode.ExtensionContext) { 36 constructor(ctx: vscode.ExtensionContext) {
37 this.globalStoragePath = ctx.globalStorageUri.fsPath; 37 this.globalStorageUri = ctx.globalStorageUri;
38 vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions); 38 vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
39 this.refreshLogging(); 39 this.refreshLogging();
40 } 40 }
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 74ee28d24..f58d26215 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -1,7 +1,5 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as path from "path";
3import * as os from "os"; 2import * as os from "os";
4import { promises as fs, PathLike } from "fs";
5 3
6import * as commands from './commands'; 4import * as commands from './commands';
7import { activateInlayHints } from './inlay_hints'; 5import { activateInlayHints } from './inlay_hints';
@@ -160,7 +158,7 @@ export async function deactivate() {
160} 158}
161 159
162async function bootstrap(config: Config, state: PersistentState): Promise<string> { 160async function bootstrap(config: Config, state: PersistentState): Promise<string> {
163 await fs.mkdir(config.globalStoragePath, { recursive: true }); 161 await vscode.workspace.fs.createDirectory(config.globalStorageUri);
164 162
165 if (!config.currentExtensionIsNightly) { 163 if (!config.currentExtensionIsNightly) {
166 await state.updateNightlyReleaseId(undefined); 164 await state.updateNightlyReleaseId(undefined);
@@ -222,7 +220,7 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
222 const artifact = latestNightlyRelease.assets.find(artifact => artifact.name === "rust-analyzer.vsix"); 220 const artifact = latestNightlyRelease.assets.find(artifact => artifact.name === "rust-analyzer.vsix");
223 assert(!!artifact, `Bad release: ${JSON.stringify(latestNightlyRelease)}`); 221 assert(!!artifact, `Bad release: ${JSON.stringify(latestNightlyRelease)}`);
224 222
225 const dest = path.join(config.globalStoragePath, "rust-analyzer.vsix"); 223 const dest = vscode.Uri.joinPath(config.globalStorageUri, "rust-analyzer.vsix");
226 224
227 await downloadWithRetryDialog(state, async () => { 225 await downloadWithRetryDialog(state, async () => {
228 await download({ 226 await download({
@@ -233,8 +231,8 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
233 }); 231 });
234 }); 232 });
235 233
236 await vscode.commands.executeCommand("workbench.extensions.installExtension", vscode.Uri.file(dest)); 234 await vscode.commands.executeCommand("workbench.extensions.installExtension", dest);
237 await fs.unlink(dest); 235 await vscode.workspace.fs.delete(dest);
238 236
239 await state.updateNightlyReleaseId(latestNightlyRelease.id); 237 await state.updateNightlyReleaseId(latestNightlyRelease.id);
240 await state.updateLastCheck(now); 238 await state.updateLastCheck(now);
@@ -259,7 +257,7 @@ async function bootstrapServer(config: Config, state: PersistentState): Promise<
259 return path; 257 return path;
260} 258}
261 259
262async function patchelf(dest: PathLike): Promise<void> { 260async function patchelf(dest: vscode.Uri): Promise<void> {
263 await vscode.window.withProgress( 261 await vscode.window.withProgress(
264 { 262 {
265 location: vscode.ProgressLocation.Notification, 263 location: vscode.ProgressLocation.Notification,
@@ -279,11 +277,11 @@ async function patchelf(dest: PathLike): Promise<void> {
279 ''; 277 '';
280 } 278 }
281 `; 279 `;
282 const origFile = dest + "-orig"; 280 const origFile = vscode.Uri.file(dest.path + "-orig");
283 await fs.rename(dest, origFile); 281 await vscode.workspace.fs.rename(dest, origFile);
284 progress.report({ message: "Patching executable", increment: 20 }); 282 progress.report({ message: "Patching executable", increment: 20 });
285 await new Promise((resolve, reject) => { 283 await new Promise((resolve, reject) => {
286 const handle = exec(`nix-build -E - --argstr srcStr '${origFile}' -o '${dest}'`, 284 const handle = exec(`nix-build -E - --argstr srcStr '${origFile.path}' -o '${dest.path}'`,
287 (err, stdout, stderr) => { 285 (err, stdout, stderr) => {
288 if (err != null) { 286 if (err != null) {
289 reject(Error(stderr)); 287 reject(Error(stderr));
@@ -294,7 +292,7 @@ async function patchelf(dest: PathLike): Promise<void> {
294 handle.stdin?.write(expression); 292 handle.stdin?.write(expression);
295 handle.stdin?.end(); 293 handle.stdin?.end();
296 }); 294 });
297 await fs.unlink(origFile); 295 await vscode.workspace.fs.delete(origFile);
298 } 296 }
299 ); 297 );
300} 298}
@@ -334,20 +332,20 @@ async function getServer(config: Config, state: PersistentState): Promise<string
334 platform = "x86_64-unknown-linux-musl"; 332 platform = "x86_64-unknown-linux-musl";
335 } 333 }
336 const ext = platform.indexOf("-windows-") !== -1 ? ".exe" : ""; 334 const ext = platform.indexOf("-windows-") !== -1 ? ".exe" : "";
337 const dest = path.join(config.globalStoragePath, `rust-analyzer-${platform}${ext}`); 335 const dest = vscode.Uri.joinPath(config.globalStorageUri, `rust-analyzer-${platform}${ext}`);
338 const exists = await fs.stat(dest).then(() => true, () => false); 336 const exists = await vscode.workspace.fs.stat(dest).then(() => true, () => false);
339 if (!exists) { 337 if (!exists) {
340 await state.updateServerVersion(undefined); 338 await state.updateServerVersion(undefined);
341 } 339 }
342 340
343 if (state.serverVersion === config.package.version) return dest; 341 if (state.serverVersion === config.package.version) return dest.path;
344 342
345 if (config.askBeforeDownload) { 343 if (config.askBeforeDownload) {
346 const userResponse = await vscode.window.showInformationMessage( 344 const userResponse = await vscode.window.showInformationMessage(
347 `Language server version ${config.package.version} for rust-analyzer is not installed.`, 345 `Language server version ${config.package.version} for rust-analyzer is not installed.`,
348 "Download now" 346 "Download now"
349 ); 347 );
350 if (userResponse !== "Download now") return dest; 348 if (userResponse !== "Download now") return dest.path;
351 } 349 }
352 350
353 const releaseTag = config.package.releaseTag; 351 const releaseTag = config.package.releaseTag;
@@ -374,7 +372,7 @@ async function getServer(config: Config, state: PersistentState): Promise<string
374 } 372 }
375 373
376 await state.updateServerVersion(config.package.version); 374 await state.updateServerVersion(config.package.version);
377 return dest; 375 return dest.path;
378} 376}
379 377
380function serverPath(config: Config): string | null { 378function serverPath(config: Config): string | null {
@@ -383,7 +381,7 @@ function serverPath(config: Config): string | null {
383 381
384async function isNixOs(): Promise<boolean> { 382async function isNixOs(): Promise<boolean> {
385 try { 383 try {
386 const contents = await fs.readFile("/etc/os-release"); 384 const contents = (await vscode.workspace.fs.readFile(vscode.Uri.file("/etc/os-release"))).toString();
387 return contents.indexOf("ID=nixos") !== -1; 385 return contents.indexOf("ID=nixos") !== -1;
388 } catch (e) { 386 } catch (e) {
389 return false; 387 return false;
diff --git a/editors/code/src/net.ts b/editors/code/src/net.ts
index 07ebc615c..747c02db9 100644
--- a/editors/code/src/net.ts
+++ b/editors/code/src/net.ts
@@ -73,14 +73,14 @@ export interface GithubRelease {
73 assets: Array<{ 73 assets: Array<{
74 name: string; 74 name: string;
75 // eslint-disable-next-line camelcase 75 // eslint-disable-next-line camelcase
76 browser_download_url: string; 76 browser_download_url: vscode.Uri;
77 }>; 77 }>;
78} 78}
79 79
80interface DownloadOpts { 80interface DownloadOpts {
81 progressTitle: string; 81 progressTitle: string;
82 url: string; 82 url: vscode.Uri;
83 dest: string; 83 dest: vscode.Uri;
84 mode?: number; 84 mode?: number;
85 gunzip?: boolean; 85 gunzip?: boolean;
86 httpProxy?: string; 86 httpProxy?: string;
@@ -90,9 +90,9 @@ export async function download(opts: DownloadOpts) {
90 // Put artifact into a temporary file (in the same dir for simplicity) 90 // Put artifact into a temporary file (in the same dir for simplicity)
91 // to prevent partially downloaded files when user kills vscode 91 // to prevent partially downloaded files when user kills vscode
92 // This also avoids overwriting running executables 92 // This also avoids overwriting running executables
93 const dest = path.parse(opts.dest);
94 const randomHex = crypto.randomBytes(5).toString("hex"); 93 const randomHex = crypto.randomBytes(5).toString("hex");
95 const tempFile = path.join(dest.dir, `${dest.name}${randomHex}`); 94 const rawDest = path.parse(opts.dest.path);
95 const tempFilePath = vscode.Uri.joinPath(vscode.Uri.file(rawDest.dir), `${rawDest.name}${randomHex}`);
96 96
97 await vscode.window.withProgress( 97 await vscode.window.withProgress(
98 { 98 {
@@ -102,7 +102,7 @@ export async function download(opts: DownloadOpts) {
102 }, 102 },
103 async (progress, _cancellationToken) => { 103 async (progress, _cancellationToken) => {
104 let lastPercentage = 0; 104 let lastPercentage = 0;
105 await downloadFile(opts.url, tempFile, opts.mode, !!opts.gunzip, opts.httpProxy, (readBytes, totalBytes) => { 105 await downloadFile(opts.url, tempFilePath, opts.mode, !!opts.gunzip, opts.httpProxy, (readBytes, totalBytes) => {
106 const newPercentage = Math.round((readBytes / totalBytes) * 100); 106 const newPercentage = Math.round((readBytes / totalBytes) * 100);
107 if (newPercentage !== lastPercentage) { 107 if (newPercentage !== lastPercentage) {
108 progress.report({ 108 progress.report({
@@ -116,12 +116,12 @@ export async function download(opts: DownloadOpts) {
116 } 116 }
117 ); 117 );
118 118
119 await fs.promises.rename(tempFile, opts.dest); 119 await vscode.workspace.fs.rename(tempFilePath, opts.dest);
120} 120}
121 121
122async function downloadFile( 122async function downloadFile(
123 url: string, 123 url: vscode.Uri,
124 destFilePath: fs.PathLike, 124 destFilePath: vscode.Uri,
125 mode: number | undefined, 125 mode: number | undefined,
126 gunzip: boolean, 126 gunzip: boolean,
127 httpProxy: string | null | undefined, 127 httpProxy: string | null | undefined,
@@ -129,15 +129,15 @@ async function downloadFile(
129): Promise<void> { 129): Promise<void> {
130 const res = await (() => { 130 const res = await (() => {
131 if (httpProxy) { 131 if (httpProxy) {
132 log.debug(`Downloading ${url} via proxy: ${httpProxy}`); 132 log.debug(`Downloading ${url.path} via proxy: ${httpProxy}`);
133 return fetch(url, { agent: new HttpsProxyAgent(httpProxy) }); 133 return fetch(url.path, { agent: new HttpsProxyAgent(httpProxy) });
134 } 134 }
135 135
136 return fetch(url); 136 return fetch(url.path);
137 })(); 137 })();
138 138
139 if (!res.ok) { 139 if (!res.ok) {
140 log.error("Error", res.status, "while downloading file from", url); 140 log.error("Error", res.status, "while downloading file from", url.path);
141 log.error({ body: await res.text(), headers: res.headers }); 141 log.error({ body: await res.text(), headers: res.headers });
142 142
143 throw new Error(`Got response ${res.status} when trying to download a file.`); 143 throw new Error(`Got response ${res.status} when trying to download a file.`);
@@ -146,7 +146,7 @@ async function downloadFile(
146 const totalBytes = Number(res.headers.get('content-length')); 146 const totalBytes = Number(res.headers.get('content-length'));
147 assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol"); 147 assert(!Number.isNaN(totalBytes), "Sanity check of content-length protocol");
148 148
149 log.debug("Downloading file of", totalBytes, "bytes size from", url, "to", destFilePath); 149 log.debug("Downloading file of", totalBytes, "bytes size from", url.path, "to", destFilePath.path);
150 150
151 let readBytes = 0; 151 let readBytes = 0;
152 res.body.on("data", (chunk: Buffer) => { 152 res.body.on("data", (chunk: Buffer) => {
@@ -154,7 +154,7 @@ async function downloadFile(
154 onProgress(readBytes, totalBytes); 154 onProgress(readBytes, totalBytes);
155 }); 155 });
156 156
157 const destFileStream = fs.createWriteStream(destFilePath, { mode }); 157 const destFileStream = fs.createWriteStream(destFilePath.path, { mode });
158 const srcStream = gunzip ? res.body.pipe(zlib.createGunzip()) : res.body; 158 const srcStream = gunzip ? res.body.pipe(zlib.createGunzip()) : res.body;
159 159
160 await pipeline(srcStream, destFileStream); 160 await pipeline(srcStream, destFileStream);
diff --git a/editors/code/src/toolchain.ts b/editors/code/src/toolchain.ts
index 68826c478..902d0ddda 100644
--- a/editors/code/src/toolchain.ts
+++ b/editors/code/src/toolchain.ts
@@ -1,9 +1,8 @@
1import * as cp from 'child_process'; 1import * as cp from 'child_process';
2import * as os from 'os'; 2import * as os from 'os';
3import * as path from 'path'; 3import * as path from 'path';
4import * as fs from 'fs';
5import * as readline from 'readline'; 4import * as readline from 'readline';
6import { OutputChannel } from 'vscode'; 5import * as vscode from 'vscode';
7import { execute, log, memoize } from './util'; 6import { execute, log, memoize } from './util';
8 7
9interface CompilationArtifact { 8interface CompilationArtifact {
@@ -19,7 +18,7 @@ export interface ArtifactSpec {
19} 18}
20 19
21export class Cargo { 20export class Cargo {
22 constructor(readonly rootFolder: string, readonly output: OutputChannel) { } 21 constructor(readonly rootFolder: string, readonly output: vscode.OutputChannel) { }
23 22
24 // Made public for testing purposes 23 // Made public for testing purposes
25 static artifactSpec(args: readonly string[]): ArtifactSpec { 24 static artifactSpec(args: readonly string[]): ArtifactSpec {
@@ -158,9 +157,9 @@ export const getPathForExecutable = memoize(
158 try { 157 try {
159 // hmm, `os.homedir()` seems to be infallible 158 // hmm, `os.homedir()` seems to be infallible
160 // it is not mentioned in docs and cannot be infered by the type signature... 159 // it is not mentioned in docs and cannot be infered by the type signature...
161 const standardPath = path.join(os.homedir(), ".cargo", "bin", executableName); 160 const standardPath = vscode.Uri.joinPath(vscode.Uri.file(os.homedir()), ".cargo", "bin", executableName);
162 161
163 if (isFile(standardPath)) return standardPath; 162 if (isFile(standardPath.path)) return standardPath.path;
164 } catch (err) { 163 } catch (err) {
165 log.error("Failed to read the fs info", err); 164 log.error("Failed to read the fs info", err);
166 } 165 }
@@ -181,12 +180,6 @@ function lookupInPath(exec: string): boolean {
181 return candidates.some(isFile); 180 return candidates.some(isFile);
182} 181}
183 182
184function isFile(suspectPath: string): boolean { 183async function isFile(path: string): Promise<boolean> {
185 // It is not mentionned in docs, but `statSync()` throws an error when 184 return ((await vscode.workspace.fs.stat(vscode.Uri.file(path))).type & vscode.FileType.File) !== 0;
186 // the path doesn't exist
187 try {
188 return fs.statSync(suspectPath).isFile();
189 } catch {
190 return false;
191 }
192} 185}