diff --git a/CHANGELOG.md b/CHANGELOG.md index fa56f31662..155c39ff79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ from published versions since it shows up in the VS Code extension changelog tab and is confusing to users. Add it back between releases if needed. --> +## Unreleased + +### Changed + +- Restarting an up-to-date workspace from the Workspaces panel on Coder 2.36 + and later queues the stop and start in one build, so another operation cannot + claim the workspace in between. + ## [v1.18.4](https://github.com/coder/vscode-coder/releases/tag/v1.18.4) 2026-12-03 ### Added diff --git a/src/api/workspace.ts b/src/api/workspace.ts index b513eeb098..af1bfd96a6 100644 --- a/src/api/workspace.ts +++ b/src/api/workspace.ts @@ -198,16 +198,12 @@ export async function updateWorkspaceViaApi( ): Promise { const workspace = await rereadForUpdate(ctx.client, prepared, ctx.signal); announceUpdate(ctx.write, workspace); - if (serverFeatures.onSuccessBuild) { - await runBuild(ctx, workspace.id, oneBuildUpdate(workspace, parameters)); - return; - } const start = { template_version_id: workspace.template_active_version_id, rich_parameter_values: parameters, }; await (workspace.latest_build.status === "running" - ? restartWorkspace(ctx, workspace, start) + ? restartWorkspace(ctx, workspace, serverFeatures, start) : runBuild(ctx, workspace.id, { transition: "start", ...start })); } @@ -346,23 +342,39 @@ export async function startWorkspaceViaApi( } /** - * Restart with `start`, by default on the last build's version, refusing to - * start over a build that superseded the stop. Returns the start build. + * Restart with `start`, by default on the last build's version. Uses one + * `on_success` build on 2.36+ unless a plain restart is outdated, since an + * unpinned `on_success` start moves to the active version. */ export async function restartWorkspace( ctx: BuildContext, workspace: Workspace, - start: Pick = { - template_version_id: workspace.latest_build.template_version_id, - }, + serverFeatures: ServerFeatureSet, + start?: Pick, ): Promise { + if (serverFeatures.onSuccessBuild && (start || !workspace.outdated)) { + return runBuild(ctx, workspace.id, { + transition: "stop", + on_success: { + transition: "start", + ...(start?.rich_parameter_values && { + rich_parameter_values: start.rich_parameter_values, + }), + }, + }); + } const stop = await runBuild(ctx, workspace.id, { transition: "stop" }); if (await nextBuild(ctx, stop)) { throw new Error( `${createWorkspaceIdentifier(workspace)} changed after stopping, so it was not started again, to avoid overwriting another operation.`, ); } - return runBuild(ctx, workspace.id, { transition: "start", ...start }); + return runBuild(ctx, workspace.id, { + transition: "start", + ...(start ?? { + template_version_id: workspace.latest_build.template_version_id, + }), + }); } /** The build that replaced `build` as the latest, read up to `polls` times a second apart. */ diff --git a/src/webviews/workspaces/workspaceActionRunner.ts b/src/webviews/workspaces/workspaceActionRunner.ts index 9e3273772f..bb2301724a 100644 --- a/src/webviews/workspaces/workspaceActionRunner.ts +++ b/src/webviews/workspaces/workspaceActionRunner.ts @@ -22,7 +22,7 @@ import { } from "../../api/workspace"; import { throwIfAborted } from "../../error/abort"; import { errToStr } from "../../error/normalize"; -import { serverFeatureSet } from "../../featureSet"; +import { serverFeatureSet, type ServerFeatureSet } from "../../featureSet"; import { WorkspaceOperationTelemetry } from "../../instrumentation/workspace"; import { vscodeProposed } from "../../vscode/proposed"; @@ -177,8 +177,10 @@ export class WorkspaceActionRunner { const parameters = await telemetry.traceParametersPrompt(() => collectUpdateParameters(this.client, workspace), ); - const { version } = await this.client.getBuildInfo(); - const serverFeatures = serverFeatureSet(semver.parse(version)); + const serverFeatures = + workspace.latest_build.status === "running" + ? await this.serverFeatures(ctx.signal) + : serverFeatureSet(null); return telemetry.traceAction("update", () => updateWorkspaceViaApi(ctx, workspace, serverFeatures, parameters), ); @@ -186,12 +188,28 @@ export class WorkspaceActionRunner { const builds = { start: () => startWorkspaceViaApi(ctx, workspace), stop: () => runBuild(ctx, workspace.id, { transition: "stop" }), - restart: () => restartWorkspace(ctx, workspace), + restart: async () => + restartWorkspace( + ctx, + workspace, + workspace.outdated + ? serverFeatureSet(null) + : await this.serverFeatures(ctx.signal), + ), delete: () => runBuild(ctx, workspace.id, { transition: "delete" }), }[action]; await telemetry.traceAction(action, builds); } + private async serverFeatures( + signal?: AbortSignal, + ): Promise { + throwIfAborted(signal); + const { version } = await this.client.getBuildInfo(); + throwIfAborted(signal); + return serverFeatureSet(semver.parse(version)); + } + /** Writes each line to the Coder output, tagged with the workspace. */ private writer(name: string): Write { return (data) => { diff --git a/test/unit/api/workspace.test.ts b/test/unit/api/workspace.test.ts index 93288dbe1a..410eeb8a57 100644 --- a/test/unit/api/workspace.test.ts +++ b/test/unit/api/workspace.test.ts @@ -663,42 +663,93 @@ describe("panel builds", () => { }); describe("restartWorkspace", () => { - const versions = { - workspace: { - template_active_version_id: "v-new", - latest_build: { template_version_id: "v-original" }, + interface RestartCase { + name: string; + outdated: boolean; + onSuccessBuild: boolean; + posted: CreateWorkspaceBuildRequest[]; + } + + it.each([ + { + name: "uses one on_success build on 2.36+ when up to date", + outdated: false, + onSuccessBuild: true, + posted: [ + { + transition: "stop", + on_success: { transition: "start" }, + ...REASON, + }, + ], }, - }; - - it("waits for its stop, then starts on the original version", async () => { - const h = setup(versions); + { + name: "stops then starts on the original version before 2.36", + outdated: false, + onSuccessBuild: false, + posted: [ + { transition: "stop", ...REASON }, + { transition: "start", template_version_id: "v-original", ...REASON }, + ], + }, + { + name: "stops then starts on the original version on 2.36+ when outdated", + outdated: true, + onSuccessBuild: true, + posted: [ + { transition: "stop", ...REASON }, + { transition: "start", template_version_id: "v-original", ...REASON }, + ], + }, + ])("$name", async ({ outdated, onSuccessBuild, posted }) => { + const h = setup({ + workspace: { + outdated, + template_active_version_id: "v-new", + latest_build: { template_version_id: "v-original" }, + }, + serverFeatures: { onSuccessBuild }, + }); + const queued = { ...h.workspace.latest_build, id: "server-start" }; + if (posted.length === 1) { + h.client.getWorkspace.mockResolvedValueOnce({ + ...h.workspace, + latest_build: queued, + }); + } - await restartWorkspace(h.ctx, h.workspace); + const result = await restartWorkspace( + h.ctx, + h.workspace, + h.serverFeatures, + ); + expect(h.posted()).toEqual(posted); expect(h.client.waitForBuild).toHaveBeenCalledTimes(2); - expect(h.posted()).toEqual([ - { transition: "stop", ...REASON }, - { transition: "start", template_version_id: "v-original", ...REASON }, - ]); + if (posted.length === 1) { + expect(result).toBe(queued); + } }); it("does not start over a build that superseded its stop", async () => { - const h = setup(versions); + const h = setup(); h.client.getWorkspace.mockResolvedValue( createWorkspace({ latest_build: { id: "another-build" } }), ); - await expect(restartWorkspace(h.ctx, h.workspace)).rejects.toThrow( - "changed after stopping", - ); + await expect( + restartWorkspace(h.ctx, h.workspace, h.serverFeatures), + ).rejects.toThrow("changed after stopping"); expect(h.client.postWorkspaceBuild).toHaveBeenCalledOnce(); }); it("does not start once aborted", async () => { - const h = setup(versions); + const h = setup(); h.abortAfterWait(); - await expect(restartWorkspace(h.ctx, h.workspace)).rejects.toMatchObject({ + await expect( + restartWorkspace(h.ctx, h.workspace, h.serverFeatures), + ).rejects.toMatchObject({ name: "AbortError", }); expect(h.client.postWorkspaceBuild).toHaveBeenCalledOnce(); diff --git a/test/unit/webviews/workspaces/workspaceActionRunner.test.ts b/test/unit/webviews/workspaces/workspaceActionRunner.test.ts index f267a919a8..b8a8f57608 100644 --- a/test/unit/webviews/workspaces/workspaceActionRunner.test.ts +++ b/test/unit/webviews/workspaces/workspaceActionRunner.test.ts @@ -128,16 +128,20 @@ describe("WorkspaceActionRunner", () => { expect(updateWorkspaceViaApi).toHaveBeenCalledWith( expect.objectContaining({ signal: expect.any(AbortSignal) }), latest, - expect.objectContaining({ onSuccessBuild: true }), + expect.objectContaining({ onSuccessBuild: false }), [{ name: "region", value: "eu" }], ); + expect(h.client.getBuildInfo).not.toHaveBeenCalled(); + } + if (action === "restart") { + expect(restartWorkspace).toHaveBeenCalledWith( + expect.objectContaining({ signal: expect.any(AbortSignal) }), + latest, + expect.objectContaining({ onSuccessBuild: true }), + ); } - if (action === "start" || action === "restart") { - expect( - vi.mocked( - action === "start" ? startWorkspaceViaApi : restartWorkspace, - ).mock.calls[0][1], - ).toBe(latest); + if (action === "start") { + expect(vi.mocked(startWorkspaceViaApi).mock.calls[0][1]).toBe(latest); } expect(h.triggered(action)).toHaveLength(1); },