Docs/Tools & reference

Compiler & testing

Build, check, and test a project from the command line.

On this page

Use the compiler both to produce executables and to check your source. Tests are ordinary Valk declarations that become the program's entry point in a test build.

Common build commands#

Run these from the project root:

Terminal
valk build src -o app
valk build src --run
valk build src --lint
valk build src -o app --release

The first command writes an executable. --run builds and starts the program. --lint checks every source in the package without requiring a main or producing an executable. --release requests an optimized build.

Use --watch --run for a development loop that rebuilds and runs as files change. Format input source files in place with valk build src --fmt. valk build --help lists the available options.

Write a test#

Put the function and test below in a .valk file:

Valk
fn line_total(price: int, quantity: int) int {
    return price * quantity
}

test "Line total uses the requested quantity" {
    assert(line_total(12, 3) == 36)
    assert(line_total(12, 0) == 0)
}

Build and execute tests with:

Terminal
valk build src --test --run

The generated test entry point runs tests instead of main. Test declarations are omitted from normal builds. Add a separate tests directory to the command if you keep tests outside src:

Terminal
valk build src tests --test --run

Isolate tests that change shared state#

Regular test declarations run concurrently. Use synctest when a test changes process-wide state or needs isolation:

Valk
global counter: int (0)

synctest "Reset shared test state" {
    counter = 0
    counter++
    assert(counter == 1)
}

All regular tests finish before synctests start. Synctests then run sequentially in declaration order. A test can still create its own coroutines to verify concurrent behavior.

Filter by part of a test's name when investigating a failure:

Terminal
valk build src tests --test --filter "Line total" --run

Select platform-specific code#

Compile-time conditions choose which code is included:

Valk
fn main() {
    #if OS == "linux"
    println("Running the Linux build")
    #elif OS == "macos"
    println("Running the macOS build")
    #else
    println("Running another platform build")
    #end
}

Built-in definitions include OS, ARCH, and TEST; --def supplies additional definitions. Source location values such as __FILE__ and __LINE__ are also available for diagnostics.

Choose a compilation target#

--target selects a target such as linux-x64 or macos-arm64. A target executable must be run on a compatible system, and native dependencies must be available for that target. Use native library build options when producing a library instead of an application.