Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/fix-command-ensure-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vercel/sandbox": patch
---

Fix `stdout()`/`stderr()`/`output()` failing on deserialized `Command` instances with "logs() requires an API client" error.
46 changes: 46 additions & 0 deletions packages/vercel-sandbox/src/command.serialize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,52 @@ describe("CommandFinished serialization", () => {
});
});

describe("deserialized without cached output", () => {
afterEach(() => {
vi.restoreAllMocks();
});

it("stdout() and stderr() work after deserialization without output", async () => {
vi.mock("./utils/get-credentials.js", () => ({
getCredentials: vi.fn().mockResolvedValue({
token: "test_token",
teamId: "team_test",
projectId: "proj_test",
}),
}));

const serializedData: SerializedCommandFinished = {
sandboxId: mockSandboxId,
cmd: mockCommandData,
exitCode: 0,
// No output — simulates a deserialized instance that never fetched logs
};

const commandFinished =
CommandFinished[WORKFLOW_DESERIALIZE](serializedData);

// _client should be null before any async method is called
expect(Reflect.get(commandFinished, "_client")).toBeNull();

// Mock getLogs on the APIClient prototype to return fake log entries
const getLogsSpy = vi
.spyOn(APIClient.prototype, "getLogs")
.mockReturnValue(
(async function* () {
yield { stream: "stdout" as const, data: "hello\n" };
yield { stream: "stderr" as const, data: "warn\n" };
})() as any,
);

const stdout = await commandFinished.stdout();
const stderr = await commandFinished.stderr();

expect(stdout).toBe("hello\n");
expect(stderr).toBe("warn\n");
expect(getLogsSpy).toHaveBeenCalledOnce();
});
});

describe("workflow runtime integration", () => {
afterEach(() => {
vi.restoreAllMocks();
Expand Down
4 changes: 4 additions & 0 deletions packages/vercel-sandbox/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ export class Command {
if (!this.outputCache) {
this.outputCache = (async () => {
try {
opts?.signal?.throwIfAborted();
// Ensure the API client is initialized before calling logs(),
// since logs() is synchronous and requires _client to be set.
await this.ensureClient();
Comment thread
TooTallNate marked this conversation as resolved.
let stdout = "";
let stderr = "";
Comment thread
TooTallNate marked this conversation as resolved.
let both = "";
Expand Down
Loading