Skip to content
Open
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 23 additions & 11 deletions src/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,12 @@ export async function updateWorkspaceViaApi(
): Promise<void> {
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 }));
}

Expand Down Expand Up @@ -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<BuildRequest, "template_version_id" | "rich_parameter_values"> = {
template_version_id: workspace.latest_build.template_version_id,
},
serverFeatures: ServerFeatureSet,
start?: Pick<BuildRequest, "template_version_id" | "rich_parameter_values">,
): Promise<WorkspaceBuild> {
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. */
Expand Down
26 changes: 22 additions & 4 deletions src/webviews/workspaces/workspaceActionRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -177,21 +177,39 @@ 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),
);
}
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<ServerFeatureSet> {
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) => {
Expand Down
89 changes: 70 additions & 19 deletions test/unit/api/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RestartCase>([
{
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();
Expand Down
18 changes: 11 additions & 7 deletions test/unit/webviews/workspaces/workspaceActionRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
Loading