v2 / AGENTS.md
726 lines · 662 sloc · 30.98 KB · d444f9c25dea51ee70d1a8ec896b34484370e791
Raw

V Repo Guide

Practical quick reference for the V compiler, standard library, and tools. Written for AI coding agents; useful for humans too.

Contents

Quick Start

Get operational from the repo root in three steps:

  1. Build once (only if ./v is missing): make
  2. Build a working compiler:
    • Debug-friendly (recommended): ./v -g -keepc -o ./vnew cmd/v
  3. Use ./vnew for everything:
    • Run a file: ./vnew run examples/hello_world.v
    • Run tests: ./vnew -silent test vlib/v/
    • Format: ./vnew fmt -w path/to/file.vThen read Top Rules and Agent Rules before making changes.

Top Rules

Agent Rules

Repo root

All commands assume the repo root as the working directory. The default location is /opt/v, but this may differ in your environment. If a command fails due to missing paths, verify with pwd and adjust accordingly. Use a per-command workdir only when a task requires a subdir.

Tone and output

File access and edit scope

When to ask

Bootstrap

Completeness and summaries

New files and deeper guidance

Safety (Do Not Brick the Repo)

Divergences From Repo Docs

The repo docs use v in examples. In this environment:

Quick Decisions

Compact decision table (rebuild/tests)

Use this table to pick the minimum rebuild/tests quickly. See Build & Rebuild and Testing for full details and edge cases. Commands omit ./vnew for brevity; assume the ./vnew prefix. This table is the minimum set; check Testing for additional triggers. REPL and backend changes have additional triggers in Testing. Note: cmd/v/ is compiler scope; treat changes there as compiler changes. When in doubt, ask before proceeding. If you read only one section for tests, read Testing.

| Change area | Rebuild? | Minimum tests to run | | --- | --- | --- | | Docs only (.md) | No | check-md file.md | | Compiler (vlib/v/, cmd/v/) | Yes | -silent vlib/v/compiler_errors_test.v; test vlib/v/ | | Core modules (builtin/strings/os/strconv/time) | Yes | Smallest relevant tests | | Parser-only (vlib/v/parser/) | Yes | test vlib/v/parser/ | | Checker-only (vlib/v/checker/) | Yes | test vlib/v/checker/ | | Comptime (vlib/v/comptime/) | Yes | test vlib/v/tests/; comptime-related tests | | vlib (non-compiler) | No | Nearest *_test.v or test vlib/path/ | | Tools (cmd/tools/) | No | Tool-specific test; else nearest *_test.v | | Diagnostic/output changes | Yes | vlib/v/slow_tests/inout/compiler_test.v | | C codegen (vlib/v/gen/c/) | Yes | vlib/v/gen/c/coutput_test.v |

Common Workflow

  1. Before work: git status; ensure ./vnew exists; rebuild if needed. If ./vnew is missing, see Quick Start or Build & Rebuild.
  2. Edit the relevant files.
  3. If compiler sources or core modules changed, rebuild ./vnew with ./v -g -keepc -o ./vnew cmd/v (see Build & Rebuild).
  4. Format touched .v/.vsh files and run ./vnew check-md on touched markdown.
  5. Run the smallest relevant tests for the change scope (see Testing).See Build & Rebuild for rebuild triggers and flags.

Reporting

Prerequisites

Build & Rebuild

Code Style

Modules and Imports

C/JS Interop Hygiene

Environment-Specific Code (files and $if)

V supports environment-specific file suffixes. Prefer them when the whole file is platform/backend specific.

Common patterns:

Compile-Time Code and Reflection

V uses $ as a prefix for compile-time (comptime) operations. These are evaluated by the compiler, not at runtime. AI agents frequently confuse comptime and runtime constructs; this section clarifies the boundaries.

Compile-time $if

$if evaluates conditions at compile time. It is not a runtime if. Use it for platform, compiler, and custom-flag checks:

Compile-time $for

$for iterates over type metadata at compile time. It works with:

Compile-time functions

Only the following $-prefixed functions are supported:

Compile-time pseudo variables

These are @-prefixed identifiers substituted at compile time:

Compile-time type groups

Comptime type groups combine multiple types into a higher-level type for use in generic or comptime $if checks:

Comptime changes and testing

Comptime logic lives in vlib/v/comptime/ and is exercised by the checker, parser, and cgen stages. Changes here require a rebuild of ./vnew and should be tested with ./vnew -silent test vlib/v/tests/ plus any comptime-specific tests. See the decision table and Testing.

Run Programs

Testing

Run:

Useful env variables and flags while testing

Debug

Compiler Architecture

The V compiler has the following stages, orchestrated by the v.builder module: v.scanner -> v.parser -> v.checker -> v.transformer -> v.markused -> v.gen.c Their corresponding folders are: vlib/v/scanner, vlib/v/parser, vlib/v/checker, vlib/v/transformer, vlib/v/markused, vlib/v/gen/c . There are additional subsystems (supporting or optional compiler modules) like v.comptime, v.generics, v.pref, v.reflection, v.callgraph, etc.

Key Directories

Test Locations

Error Reporting (checker/parser)

Option/Result Types

Tools

Commits and PRs

Environment Variables

Gotchas