Source commit: 8bfdd8cef23135fc1d8c9f3d74c54444b500cca8 Public tree identity: sha256:d501c4af6dfe7b1b52000bc1f5c256e001440f81f3c1e00e3615a8a84da21693
424 lines
15 KiB
JavaScript
Executable file
424 lines
15 KiB
JavaScript
Executable file
#!/usr/bin/env node
|
|
|
|
const cp = require("child_process");
|
|
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const os = require("os");
|
|
const path = require("path");
|
|
|
|
class DapClient {
|
|
constructor() {
|
|
this.child = cp.spawn(
|
|
"cargo",
|
|
["run", "-q", "-p", "disasmer-dap", "--bin", "disasmer-debug-dap"],
|
|
{ cwd: process.cwd() }
|
|
);
|
|
this.seq = 1;
|
|
this.buffer = Buffer.alloc(0);
|
|
this.messages = [];
|
|
this.waiters = [];
|
|
this.stderr = "";
|
|
|
|
this.child.stdout.on("data", (chunk) => {
|
|
this.buffer = Buffer.concat([this.buffer, chunk]);
|
|
this.parse();
|
|
});
|
|
this.child.stderr.on("data", (chunk) => {
|
|
this.stderr += chunk.toString();
|
|
});
|
|
this.child.on("exit", () => this.flushWaiters());
|
|
}
|
|
|
|
send(command, args = {}) {
|
|
const seq = this.seq++;
|
|
const message = { seq, type: "request", command, arguments: args };
|
|
const payload = Buffer.from(JSON.stringify(message));
|
|
this.child.stdin.write(`Content-Length: ${payload.length}\r\n\r\n`);
|
|
this.child.stdin.write(payload);
|
|
return seq;
|
|
}
|
|
|
|
async response(seq, command) {
|
|
const message = await this.waitFor(
|
|
(item) =>
|
|
item.type === "response" &&
|
|
item.request_seq === seq &&
|
|
item.command === command
|
|
);
|
|
if (!message.success) {
|
|
throw new Error(`DAP ${command} failed: ${message.message || JSON.stringify(message)}`);
|
|
}
|
|
return message;
|
|
}
|
|
|
|
async failure(seq, command) {
|
|
const message = await this.waitFor(
|
|
(item) =>
|
|
item.type === "response" &&
|
|
item.request_seq === seq &&
|
|
item.command === command
|
|
);
|
|
if (message.success) {
|
|
throw new Error(`DAP ${command} unexpectedly succeeded`);
|
|
}
|
|
return message;
|
|
}
|
|
|
|
waitFor(predicate, timeoutMs = 120000) {
|
|
const existing = this.messages.find(predicate);
|
|
if (existing) return Promise.resolve(existing);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const timer = setTimeout(() => {
|
|
this.child.kill("SIGKILL");
|
|
reject(new Error(`timed out waiting for DAP message\n${this.stderr}`));
|
|
}, timeoutMs);
|
|
this.waiters.push({ predicate, resolve, timer });
|
|
});
|
|
}
|
|
|
|
parse() {
|
|
while (true) {
|
|
const headerEnd = this.buffer.indexOf("\r\n\r\n");
|
|
if (headerEnd < 0) return;
|
|
const header = this.buffer.slice(0, headerEnd).toString();
|
|
const match = header.match(/Content-Length: (\d+)/i);
|
|
if (!match) throw new Error(`bad DAP header: ${header}`);
|
|
const length = Number(match[1]);
|
|
const start = headerEnd + 4;
|
|
const end = start + length;
|
|
if (this.buffer.length < end) return;
|
|
const payload = this.buffer.slice(start, end).toString();
|
|
this.buffer = this.buffer.slice(end);
|
|
this.messages.push(JSON.parse(payload));
|
|
this.flushWaiters();
|
|
}
|
|
}
|
|
|
|
flushWaiters() {
|
|
for (const waiter of [...this.waiters]) {
|
|
const message = this.messages.find(waiter.predicate);
|
|
if (!message) continue;
|
|
clearTimeout(waiter.timer);
|
|
this.waiters.splice(this.waiters.indexOf(waiter), 1);
|
|
waiter.resolve(message);
|
|
}
|
|
}
|
|
|
|
async close() {
|
|
const seq = this.send("disconnect");
|
|
await this.response(seq, "disconnect");
|
|
this.child.stdin.end();
|
|
}
|
|
}
|
|
|
|
function createFailingProject() {
|
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "disasmer-dap-failing-"));
|
|
fs.mkdirSync(path.join(root, "src"), { recursive: true });
|
|
fs.writeFileSync(
|
|
path.join(root, "Cargo.toml"),
|
|
`[package]
|
|
name = "dap-failing-demo"
|
|
version = "0.1.0"
|
|
edition = "2021"
|
|
`
|
|
);
|
|
fs.writeFileSync(
|
|
path.join(root, "src/lib.rs"),
|
|
`#[cfg(test)]
|
|
mod tests {
|
|
#[test]
|
|
fn fails_for_restart_smoke() {
|
|
assert_eq!(1, 2);
|
|
}
|
|
}
|
|
`
|
|
);
|
|
return root;
|
|
}
|
|
|
|
async function launchToBreakpoint({
|
|
runtimeBackend = "simulated",
|
|
breakpointLine,
|
|
project = path.join(process.cwd(), "examples/launch-build-demo")
|
|
}) {
|
|
const client = new DapClient();
|
|
|
|
const initialize = client.send("initialize", {
|
|
adapterID: "disasmer",
|
|
linesStartAt1: true,
|
|
columnsStartAt1: true
|
|
});
|
|
await client.response(initialize, "initialize");
|
|
|
|
const launchArgs = {
|
|
entry: "build",
|
|
project,
|
|
runtimeBackend
|
|
};
|
|
const launch = client.send("launch", launchArgs);
|
|
await client.response(launch, "launch");
|
|
await client.waitFor((message) => message.type === "event" && message.event === "initialized");
|
|
|
|
const breakpoints = client.send("setBreakpoints", {
|
|
source: { path: "src/build.rs" },
|
|
breakpoints: [{ line: breakpointLine }]
|
|
});
|
|
const breakpointResponse = await client.response(breakpoints, "setBreakpoints");
|
|
assert.strictEqual(breakpointResponse.body.breakpoints[0].verified, true);
|
|
|
|
const exceptions = client.send("setExceptionBreakpoints", { filters: [] });
|
|
await client.response(exceptions, "setExceptionBreakpoints");
|
|
|
|
const configurationDone = client.send("configurationDone");
|
|
await client.response(configurationDone, "configurationDone");
|
|
const stopped = await client.waitFor(
|
|
(message) => message.type === "event" && message.event === "stopped"
|
|
);
|
|
assert.strictEqual(stopped.body.allThreadsStopped, true);
|
|
assert.strictEqual(stopped.body.reason, "breakpoint");
|
|
|
|
return { client, stopped };
|
|
}
|
|
|
|
(async () => {
|
|
const { client, stopped } = await launchToBreakpoint({
|
|
runtimeBackend: "local-services",
|
|
breakpointLine: 42
|
|
});
|
|
assert.strictEqual(stopped.body.threadId, 2);
|
|
|
|
const threadsRequest = client.send("threads");
|
|
const threads = (await client.response(threadsRequest, "threads")).body.threads;
|
|
assert.deepStrictEqual(
|
|
threads.map((thread) => thread.id),
|
|
[1, 2, 3, 4]
|
|
);
|
|
assert(threads.some((thread) => thread.name.includes("compile linux")));
|
|
assert(threads.some((thread) => thread.name.includes("compile windows")));
|
|
assert(threads.some((thread) => thread.name.includes("package artifacts")));
|
|
|
|
const stackRequest = client.send("stackTrace", { threadId: 2, startFrame: 0, levels: 1 });
|
|
const stack = (await client.response(stackRequest, "stackTrace")).body.stackFrames;
|
|
assert.strictEqual(stack.length, 1);
|
|
assert.match(stack[0].name, /compile linux::run/);
|
|
assert.strictEqual(stack[0].line, 42);
|
|
assert.strictEqual(stack[0].source.path, "src/build.rs");
|
|
assert.doesNotMatch(stack[0].name, /podman|cmd\.exe|powershell|pid|native child/i);
|
|
|
|
const scopesRequest = client.send("scopes", { frameId: stack[0].id });
|
|
const scopes = (await client.response(scopesRequest, "scopes")).body.scopes;
|
|
const argsRef = scopes.find((scope) => scope.name === "Task Args and Handles").variablesReference;
|
|
const runtimeRef = scopes.find((scope) => scope.name === "Disasmer Runtime").variablesReference;
|
|
const outputRef = scopes.find((scope) => scope.name === "Recent Output").variablesReference;
|
|
|
|
const argsRequest = client.send("variables", { variablesReference: argsRef });
|
|
const args = (await client.response(argsRequest, "variables")).body.variables;
|
|
const artifact = args.find((variable) => variable.name === "artifact");
|
|
const sourceSnapshot = args.find((variable) => variable.name === "source_snapshot");
|
|
assert(artifact);
|
|
assert.strictEqual(artifact.value, "/vfs/artifacts/dap-output.txt");
|
|
assert(sourceSnapshot);
|
|
assert.match(sourceSnapshot.value, /^source:\/\/local-checkout\/[a-f0-9]{12}$/);
|
|
|
|
const runtimeRequest = client.send("variables", { variablesReference: runtimeRef });
|
|
const runtime = (await client.response(runtimeRequest, "variables")).body.variables;
|
|
const processId = runtime.find((variable) => variable.name === "virtual_process_id");
|
|
assert(processId);
|
|
assert.match(processId.value, /^vp-[a-f0-9]{12}$/);
|
|
assert(runtime.some((variable) => variable.name === "debug_epoch" && variable.value === 1));
|
|
assert(runtime.some((variable) => variable.name === "runtime_backend" && variable.value === "LocalServices"));
|
|
assert(runtime.some((variable) => variable.name === "coordinator_task_events" && variable.value === 1));
|
|
assert(runtime.some((variable) => variable.name === "command_status" && String(variable.value).includes("completed through local services")));
|
|
|
|
const outputRequest = client.send("variables", { variablesReference: outputRef });
|
|
const output = (await client.response(outputRequest, "variables")).body.variables;
|
|
assert(output.some((variable) => String(variable.value).includes("all-stop")));
|
|
assert(output.some((variable) => String(variable.value).includes("local node completed task")));
|
|
assert(output.some((variable) => String(variable.value).includes("coordinator recorded 1 task event")));
|
|
|
|
const continueRequest = client.send("continue", { threadId: 2 });
|
|
await client.response(continueRequest, "continue");
|
|
await client.waitFor((message) => message.type === "event" && message.event === "continued");
|
|
|
|
const pauseRequest = client.send("pause", { threadId: 2 });
|
|
await client.response(pauseRequest, "pause");
|
|
const paused = await client.waitFor(
|
|
(message) => message.type === "event" && message.event === "stopped" && message.body.reason === "pause"
|
|
);
|
|
assert.strictEqual(paused.body.allThreadsStopped, true);
|
|
|
|
const restartRequest = client.send("restartFrame", { frameId: stack[0].id });
|
|
await client.response(restartRequest, "restartFrame");
|
|
await client.waitFor(
|
|
(message) =>
|
|
message.type === "event" &&
|
|
message.event === "output" &&
|
|
String(message.body.output).includes("Restarted selected task")
|
|
);
|
|
|
|
const incompatibleRestartRequest = client.send("restartFrame", {
|
|
frameId: stack[0].id,
|
|
sourceCompatibility: "incompatible"
|
|
});
|
|
const incompatibleRestart = await client.failure(incompatibleRestartRequest, "restartFrame");
|
|
assert.match(incompatibleRestart.message, /incompatible source edit/i);
|
|
assert.match(incompatibleRestart.message, /whole virtual-process restart/i);
|
|
|
|
const continueAfterRestartRequest = client.send("continue", { threadId: 2 });
|
|
await client.response(continueAfterRestartRequest, "continue");
|
|
await client.waitFor((message) => message.type === "event" && message.event === "continued");
|
|
|
|
const freezeFailureRequest = client.send("pause", {
|
|
threadId: 2,
|
|
simulateFreezeFailure: true
|
|
});
|
|
const freezeFailure = await client.failure(freezeFailureRequest, "pause");
|
|
assert.match(freezeFailure.message, /all-stop failed/i);
|
|
assert.match(freezeFailure.message, /could not freeze/i);
|
|
|
|
await client.close();
|
|
|
|
const failingProject = createFailingProject();
|
|
let failedSession;
|
|
try {
|
|
failedSession = await launchToBreakpoint({
|
|
runtimeBackend: "local-services",
|
|
breakpointLine: 42,
|
|
project: failingProject
|
|
});
|
|
assert.strictEqual(failedSession.stopped.body.threadId, 2);
|
|
|
|
const failedStackRequest = failedSession.client.send("stackTrace", {
|
|
threadId: 2,
|
|
startFrame: 0,
|
|
levels: 1
|
|
});
|
|
const failedStack = (await failedSession.client.response(failedStackRequest, "stackTrace")).body
|
|
.stackFrames;
|
|
assert.strictEqual(failedStack.length, 1);
|
|
assert.match(failedStack[0].name, /compile linux::run/);
|
|
|
|
const failedScopesRequest = failedSession.client.send("scopes", { frameId: failedStack[0].id });
|
|
const failedScopes = (await failedSession.client.response(failedScopesRequest, "scopes")).body
|
|
.scopes;
|
|
const failedRuntimeRef = failedScopes.find((scope) => scope.name === "Disasmer Runtime")
|
|
.variablesReference;
|
|
const failedOutputRef = failedScopes.find((scope) => scope.name === "Recent Output")
|
|
.variablesReference;
|
|
|
|
const failedRuntimeRequest = failedSession.client.send("variables", {
|
|
variablesReference: failedRuntimeRef
|
|
});
|
|
const failedRuntime = (await failedSession.client.response(failedRuntimeRequest, "variables"))
|
|
.body.variables;
|
|
assert(
|
|
failedRuntime.some(
|
|
(variable) =>
|
|
variable.name === "command_status" &&
|
|
String(variable.value).includes("failed through local services")
|
|
)
|
|
);
|
|
assert(
|
|
failedRuntime.some(
|
|
(variable) => variable.name === "state" && String(variable.value).includes("Failed")
|
|
)
|
|
);
|
|
|
|
const failedOutputRequest = failedSession.client.send("variables", {
|
|
variablesReference: failedOutputRef
|
|
});
|
|
const failedOutput = (await failedSession.client.response(failedOutputRequest, "variables")).body
|
|
.variables;
|
|
assert(
|
|
failedOutput.some((variable) => String(variable.value).includes("local node failed task"))
|
|
);
|
|
|
|
const failedRestartRequest = failedSession.client.send("restartFrame", {
|
|
frameId: failedStack[0].id,
|
|
sourceEdit: { compatibility: "compatible" }
|
|
});
|
|
await failedSession.client.response(failedRestartRequest, "restartFrame");
|
|
await failedSession.client.waitFor(
|
|
(message) =>
|
|
message.type === "event" &&
|
|
message.event === "output" &&
|
|
String(message.body.output).includes("Restarted failed task")
|
|
);
|
|
|
|
const failedRuntimeAfterRestartRequest = failedSession.client.send("variables", {
|
|
variablesReference: failedRuntimeRef
|
|
});
|
|
const failedRuntimeAfterRestart = (
|
|
await failedSession.client.response(failedRuntimeAfterRestartRequest, "variables")
|
|
).body.variables;
|
|
assert(
|
|
failedRuntimeAfterRestart.some(
|
|
(variable) =>
|
|
variable.name === "command_status" &&
|
|
String(variable.value).includes("failed task restarted")
|
|
)
|
|
);
|
|
|
|
const failedOutputAfterRestartRequest = failedSession.client.send("variables", {
|
|
variablesReference: failedOutputRef
|
|
});
|
|
const failedOutputAfterRestart = (
|
|
await failedSession.client.response(failedOutputAfterRestartRequest, "variables")
|
|
).body.variables;
|
|
assert(
|
|
failedOutputAfterRestart.some((variable) =>
|
|
String(variable.value).includes("task restarted from VFS checkpoint")
|
|
)
|
|
);
|
|
} finally {
|
|
if (failedSession) {
|
|
await failedSession.client.close().catch(() => {});
|
|
}
|
|
fs.rmSync(failingProject, { recursive: true, force: true });
|
|
}
|
|
|
|
const mainSession = await launchToBreakpoint({
|
|
runtimeBackend: "local-services",
|
|
breakpointLine: 12
|
|
});
|
|
assert.strictEqual(mainSession.stopped.body.threadId, 1);
|
|
|
|
const mainStackRequest = mainSession.client.send("stackTrace", {
|
|
threadId: 1,
|
|
startFrame: 0,
|
|
levels: 1
|
|
});
|
|
const mainStack = (await mainSession.client.response(mainStackRequest, "stackTrace"))
|
|
.body.stackFrames;
|
|
assert.strictEqual(mainStack.length, 1);
|
|
assert.match(mainStack[0].name, /build virtual process::run/);
|
|
assert.strictEqual(mainStack[0].line, 12);
|
|
assert.strictEqual(mainStack[0].source.path, "src/build.rs");
|
|
|
|
const mainScopesRequest = mainSession.client.send("scopes", { frameId: mainStack[0].id });
|
|
const mainScopes = (await mainSession.client.response(mainScopesRequest, "scopes")).body.scopes;
|
|
const mainRuntimeRef = mainScopes.find((scope) => scope.name === "Disasmer Runtime")
|
|
.variablesReference;
|
|
const mainRuntimeRequest = mainSession.client.send("variables", {
|
|
variablesReference: mainRuntimeRef
|
|
});
|
|
const mainRuntime = (await mainSession.client.response(mainRuntimeRequest, "variables")).body
|
|
.variables;
|
|
assert(
|
|
mainRuntime.some(
|
|
(variable) => variable.name === "runtime_backend" && variable.value === "LocalServices"
|
|
)
|
|
);
|
|
assert(
|
|
mainRuntime.some(
|
|
(variable) => variable.name === "coordinator_task_events" && variable.value === 1
|
|
)
|
|
);
|
|
|
|
await mainSession.client.close();
|
|
console.log("DAP smoke passed");
|
|
})().catch((err) => {
|
|
console.error(err.stack || err.message);
|
|
process.exit(1);
|
|
});
|