Skip to content

Commit

Permalink
Merge pull request #241 from microsoft/connor4312/fix-233
Browse files Browse the repository at this point in the history
fix: avoid faulty rename when installing
  • Loading branch information
connor4312 committed Oct 24, 2023
2 parents 310c6b2 + 0064e89 commit c4b489f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### 2.3.6 | 2022-10-24

- Fix windows sometimes failing on EPERM in download (again)

### 2.3.5 | 2022-10-04

- Fix windows sometimes failing on EPERM in download
Expand Down
27 changes: 6 additions & 21 deletions lib/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
streamToBuffer,
systemDefaultPlatform,
} from './util';
import * as timers from 'timers/promises';

const extensionRoot = process.cwd();
const pipelineAsync = promisify(pipeline);
Expand Down Expand Up @@ -332,6 +331,8 @@ function spawnDecompressorChild(command: string, args: ReadonlyArray<string>, in

export const defaultCachePath = path.resolve(extensionRoot, '.vscode-test');

const COMPLETE_FILE_NAME = 'is-complete';

/**
* Download and unzip a copy of VS Code.
* @returns Promise of `vscodeExecutablePath`.
Expand Down Expand Up @@ -368,7 +369,7 @@ export async function download(options: Partial<DownloadOptions> = {}): Promise<
reporter.report({ stage: ProgressReportStage.ResolvedVersion, version });

const downloadedPath = path.resolve(cachePath, makeDownloadDirName(platform, version));
if (fs.existsSync(downloadedPath)) {
if (fs.existsSync(path.join(downloadedPath, COMPLETE_FILE_NAME))) {
if (isInsiderVersionIdentifier(version)) {
reporter.report({ stage: ProgressReportStage.FetchingInsidersMetadata });
const { version: currentHash, date: currentDate } = insidersDownloadDirMetadata(downloadedPath, platform);
Expand Down Expand Up @@ -408,10 +409,7 @@ export async function download(options: Partial<DownloadOptions> = {}): Promise<

for (let i = 0; ; i++) {
try {
// Use a staging directory and rename after unzipping so partially-
// downloaded/unzipped files aren't "stuck" being used.
const downloadStaging = `${downloadedPath}.tmp`;
await fs.promises.rm(downloadStaging, { recursive: true, force: true });
await fs.promises.rm(downloadedPath, { recursive: true, force: true });

const { stream, format } = await downloadVSCodeArchive({
version,
Expand All @@ -422,22 +420,9 @@ export async function download(options: Partial<DownloadOptions> = {}): Promise<
});
// important! do not put anything async here, since unzipVSCode will need
// to start consuming the stream immediately.
await unzipVSCode(reporter, downloadStaging, stream, platform, format);

// Windows file handles can get released asynchronously, give it a few retries:
for (let attempts = 20; attempts >= 0; attempts--) {
try {
await fs.promises.rename(downloadStaging, downloadedPath);
break;
} catch (e) {
if (attempts === 0) {
throw e;
} else {
await timers.setTimeout(200);
}
}
}
await unzipVSCode(reporter, downloadedPath, stream, platform, format);

await fs.promises.writeFile(path.join(downloadedPath, COMPLETE_FILE_NAME), '');
reporter.report({ stage: ProgressReportStage.NewInstallComplete, downloadedPath });
break;
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vscode/test-electron",
"version": "2.3.5",
"version": "2.3.6",
"scripts": {
"compile": "tsc -p ./",
"watch": "tsc -w -p ./",
Expand Down

0 comments on commit c4b489f

Please sign in to comment.