Skip to content

Back up old directory when updating #1486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions ci/vscode.patch
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,10 @@ index eab8591492..26668701f7 100644
options.logService.error(`${logPrefix} socketFactory.connect() failed. Error:`);
diff --git a/src/vs/server/browser/client.ts b/src/vs/server/browser/client.ts
new file mode 100644
index 0000000000..96fbd4b0bb
index 0000000000..4f8543d975
--- /dev/null
+++ b/src/vs/server/browser/client.ts
@@ -0,0 +1,270 @@
@@ -0,0 +1,266 @@
+import { Emitter } from 'vs/base/common/event';
+import { URI } from 'vs/base/common/uri';
+import { localize } from 'vs/nls';
Expand Down Expand Up @@ -692,14 +692,10 @@ index 0000000000..96fbd4b0bb
+ headers: { "content-type": "application/json" },
+ });
+ if (response.status !== 200) {
+ throw new Error("Unexpected response");
+ throw new Error(response.statusText);
+ }
+
+ const json = await response.json();
+ if (!json.isLatest) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this cause no errors to be reported if an update fails?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The response will only be a 200 if the update succeeded so it should already have bailed out on the line above. The problem with this line is that isLatest was always false since the currently running code was still the old version.

Although, that means I should log the status text and not just "unexpected response". Will do that right now.

+ throw new Error("Update failed");
+ }
+
+ (services.get(INotificationService) as INotificationService).info(`Updated to ${json.version}`);
+ };
+
Expand Down
9 changes: 7 additions & 2 deletions src/node/app/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,13 @@ export class UpdateHttpProvider extends HttpProvider {
targetPath = path.resolve(__dirname, "../../../")
}

logger.debug("Replacing files", field("target", targetPath))
await fs.move(directoryPath, targetPath, { overwrite: true })
// Move the old directory to prevent potential data loss.
const backupPath = path.resolve(targetPath, `../${path.basename(targetPath)}.${Date.now().toString()}`)
logger.debug("Replacing files", field("target", targetPath), field("backup", backupPath))
await fs.move(targetPath, backupPath)

// Move the new directory.
await fs.move(directoryPath, targetPath)

await fs.remove(downloadPath)

Expand Down
15 changes: 10 additions & 5 deletions test/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,18 @@ describe("update", () => {
await p.downloadAndApplyUpdate(update, destination)
assert.equal(`console.log("UPDATED")`, await fs.readFile(entry, "utf8"))

// Should still work if there is no existing version somehow.
await fs.remove(destination)
await p.downloadAndApplyUpdate(update, destination)
assert.equal(`console.log("UPDATED")`, await fs.readFile(entry, "utf8"))
// There should be a backup.
const dir = (await fs.readdir(path.join(tmpdir, "tests/updates"))).filter((dir) => {
return dir.startsWith("code-server.")
})
assert.equal(dir.length, 1)
assert.equal(
`console.log("OLD")`,
await fs.readFile(path.join(tmpdir, "tests/updates", dir[0], "code-server"), "utf8"),
)

const archiveName = await p.getReleaseName(update)
assert.deepEqual(spy, ["/latest", `/download/${version}/${archiveName}`, `/download/${version}/${archiveName}`])
assert.deepEqual(spy, ["/latest", `/download/${version}/${archiveName}`])
})

it("should not reject if unable to fetch", async () => {
Expand Down