7430: Simplify file download code r=matklad a=lnicola

This avoids leaving the user with no LSP binary available if their network goes down during the download, and should not keep the current behavior: overwriting a running executable works on Unix and fails on Windows.

It also removes the `overwrite` argument, which is always enabled and wasn't working anyway.

Nominally closes #3896 (although that's already fixed)

Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
This commit is contained in:
bors[bot] 2021-01-25 18:40:57 +00:00 committed by GitHub
commit aa91a0268b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 10 deletions

View file

@ -208,7 +208,6 @@ async function bootstrapExtension(config: Config, state: PersistentState): Promi
url: artifact.browser_download_url,
dest,
progressTitle: "Downloading rust-analyzer extension",
overwrite: true,
});
});
@ -340,7 +339,6 @@ async function getServer(config: Config, state: PersistentState): Promise<string
progressTitle: "Downloading rust-analyzer server",
gunzip: true,
mode: 0o755,
overwrite: true,
});
});

View file

@ -73,23 +73,16 @@ interface DownloadOpts {
dest: string;
mode?: number;
gunzip?: boolean;
overwrite?: boolean;
}
export async function download(opts: DownloadOpts) {
// Put artifact into a temporary file (in the same dir for simplicity)
// to prevent partially downloaded files when user kills vscode
// This also avoids overwriting running executables
const dest = path.parse(opts.dest);
const randomHex = crypto.randomBytes(5).toString("hex");
const tempFile = path.join(dest.dir, `${dest.name}${randomHex}`);
if (opts.overwrite) {
// Unlinking the exe file before moving new one on its place should prevent ETXTBSY error.
await fs.promises.unlink(opts.dest).catch(err => {
if (err.code !== "ENOENT") throw err;
});
}
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,