Skip to content

ianic/zig-language-extras

Repository files navigation

Zig Language Extras

This extension adds few commands for Zig development:

  • Zig extras: Run single test
  • Zig extras: Run file tests
  • Zig extras: Test workspace
  • Zig extras: Build workspace
  • Zig extras: Debug test
  • Zig extras: Debug binary

It also displays code lenses above tests to run or debug single test, and code lens at the first line of the file to run all tests.

It depends on several extensions:

The main reason I decided to make this is to create fine vscode problems description from zig command output. When running tests there can be few different versions of the output. Build can fail, test can fail, assert can be risen while running the test. All of those have different output and it is hard to make single regexp which will work for all. So I make it more procedural way by analyzing zig command output line by line.

Most of the commands are expecting folder structure built with 'zig init-exe' or 'zig init-lib'. With build.zig in the root and files under src. Folder with the build.zig is expected to be root of the vscode workspace.

Testing

'Test workspace' runs zig build test in the root of the workspace so depends on tests definition in your build.zig

'Run file tests' runs all tests in the current file zig test {file_name}.

'Run single tests' tries to find test name from the current position. It first searches up from the current position to find name of the test. If not found then it searches down. If you are positioned in the test, that will run that test. If you are in the code and the tests are below you code this will find first test.

If you need to use additional test command arguments set testArgs variable in workspace config. For example in a project which depends on zlib I'm using this config:

{
    "zig-language-extras.testArgs": "--deps zlib=zlib --mod zlib::../zig-zlib/src/main.zig --library z",
}

Building

'Build workspace' command runs zig build in the root of the workspace.

Debugging

There are two debugging commands.

'Debug test' builds binary for the test (binary location: zig-out/debug/test) and starts debugger on that binary. Put a breakpoint in you test before running command.

'Debug binary' first builds workspace and then starts binary zig-out/bin/{name}. If current file is in src folder name is set to the folder name above src folder which is expected to be root of your workspace. If the current file is in some other folder then the name of the current file is used as name of the binary except if that file is named main.zig then folder name of that file is used as expected binary name.

You can modify debugger via settings: "zig-language-extras.debugType": "type". Type refers to the specific "type" field that you would use in the launch.json file.

The default debugger types for each platform are as follows:

  • "lldb" for darwin platform
  • "cppvsdbg" for win32 platform
  • "gdb" for other platforms

Keybinding tip

When adding keybinding you can restrict usage on Zig language files:

  // Zig extras: Run file tests
  {
      "key": "ctrl j t",
      "command": "zig-language-extras.runFileTests",
      "when": "editorLangId == 'zig'"
  },
  // Zig extras: Run single test
  {
      "key": "ctrl j s",
      "command": "zig-language-extras.runSingleTest",
      "when": "editorLangId == 'zig'"
  },

Extension development

Zig command output parser is in src/parser.ts and the corresponding tests in the src/test/suite/parser.test.ts.

Parser test cases are in files located in src/test/suite/testdata. Each case in .txt file has corresponding expected parser output in _expected.json file. Parser test loads all txt files and expects to get parsing result as in expected file.

Parser has no dependency on vscode so it is possible to test without running vscode:

mocha -ui tdd out/test/suite/parser.test.js

Credits

Code lenses implementation is taken from Jarred-Sumner's pull request to original vscode-zig extension.