Skip to content

Commit

Permalink
add debug binary command
Browse files Browse the repository at this point in the history
  • Loading branch information
ianic committed Jul 2, 2023
1 parent fc49237 commit de59f54
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
"name": "zig-language-extras",
"displayName": "Zig Language Extras",
"description": "Commands to help test and debug Zig files",
"version": "0.2.6",
"version": "0.3.0",
"repository": "https://github.com/ianic/zig-language-extras.git",
"publisher": "ianic",
"extensionDependencies": [
"ziglang.vscode-zig",
"vadimcn.vscode-lldb",
"webfreak.debug"
],
"engines": {
"vscode": "^1.79.0"
},
Expand All @@ -23,11 +28,6 @@
"type": "string",
"default": "./zig-out/debug/test",
"description": "File path to emit binary when debugging Zig test."
},
"zig-language-extras.debugLaunchConfiguration": {
"type": "string",
"default": "ZigDebugTest",
"description": "Launch configuration name to start when debugging Zig test. Should use binary configured in testBinaryPath config."
}
}
},
Expand All @@ -40,17 +40,21 @@
"command": "zig-language-extras.runSingleTest",
"title": "Zig extras: Run single test"
},
{
"command": "zig-language-extras.debugTest",
"title": "Zig extras: Debug test"
},
{
"command": "zig-language-extras.buildWorkspace",
"title": "Zig extras: Build workspace"
},
{
"command": "zig-language-extras.testWorkspace",
"title": "Zig extras: Test workspace"
},
{
"command": "zig-language-extras.debugTest",
"title": "Zig extras: Debug test"
},
{
"command": "zig-language-extras.debugBinary",
"title": "Zig extras: Debug binary"
}
]
},
Expand Down
53 changes: 47 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as vscode from 'vscode';
import * as cp from 'child_process';
import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';

import { Parser } from './parser';

Expand Down Expand Up @@ -32,6 +33,9 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand('zig-language-extras.testWorkspace', testWorkspace)
);
context.subscriptions.push(
vscode.commands.registerCommand('zig-language-extras.debugBinary', debugBinary)
);
}

function testWorkspace() {
Expand All @@ -57,15 +61,43 @@ function debugTest() {

const debugEnv = getDebugEnv();

mkdirp(path.resolve(env.cwd, debugEnv.testBinaryPath)); // ensure that output directory exists
const binPath = debugEnv.testBinaryPath;
mkdirp(path.resolve(env.cwd, binPath)); // ensure that output directory exists

const args: string[] = ["test", "--test-filter", env.testName, env.fileNameRelative, "-femit-bin=" + debugEnv.testBinaryPath];
const args: string[] = ["test", "--test-filter", env.testName, env.fileNameRelative, "-femit-bin=" + binPath];
runZig(args, env.cwd, () => {
output.appendLine("Starting launch configuration '" + debugEnv.launchConfiguration + "'");
vscode.debug.startDebugging(env.workspaceFolder, debugEnv.launchConfiguration);
output.appendLine("Debugging binary " + binPath);
startDebugging(env.workspaceFolder, binPath);
});
}

function debugBinary() {
const env = getEnv(false);
if (!env) { return; }

const binPath = path.join("zig-out", "bin", env.binName);

const args: string[] = ["build"];
runZig(args, env.cwd, () => {
output.appendLine("Debugging binary " + binPath);
startDebugging(env.workspaceFolder, binPath);
});
}


function startDebugging(wf: vscode.WorkspaceFolder, binPath: string) {
const isDarwin = os.platform() === "darwin";
let launchConfig = {
"name": "ZigDebugBinary",
"type": isDarwin ? "lldb" : "gdb",
"request": "launch",
"target": binPath,
"program": binPath,
"cwd": "${workspaceRoot}",
};
vscode.debug.startDebugging(wf, launchConfig);
}

function runSingleTest() {
const env = getEnv();
if (!env || !env.testName) { return; }
Expand All @@ -84,13 +116,11 @@ function runFileTests() {

// path for debug binary relative to the workspace root
const defaultTestBinaryPath = "./zig-out/debug/test";
const defaultDebugLaunchConfiguration = "ZigDebugTest";

function getDebugEnv() {
const config = vscode.workspace.getConfiguration('zig-language-extras');
return {
testBinaryPath: config.get<string>("testBinaryPath") || defaultTestBinaryPath,
launchConfiguration: config.get<string>("debugLaunchConfiguration") || defaultDebugLaunchConfiguration,
};
}

Expand All @@ -114,6 +144,16 @@ function getEnv(findCurrentTest: boolean = true) {
const fileNameRelative = path.relative(cwd, fileName);
const testName = findCurrentTest ? findTest(editor) : undefined;

// binary name from the current file
let binName = path.basename(fileNameRelative);
if (binName === "main.zig") {
// for main.zig use name of the directory in the file path excluding src
const dirs = path.dirname(fileName).split(path.sep);
binName = dirs.reverse().find((dir) => {
return ["src"].includes(dir) ? null : dir;
}) || binName;
}

if (findCurrentTest && !testName) {
output.appendLine("Current test not found!");
}
Expand All @@ -125,6 +165,7 @@ function getEnv(findCurrentTest: boolean = true) {
fileName: fileName,
fileNameRelative: fileNameRelative,
testName: testName,
binName: binName,
};
}

Expand Down

0 comments on commit de59f54

Please sign in to comment.