# What Is the tauri-codegen Crate? Compile-Time Code Generation in Tauri

> Discover the tauri-codegen crate a Tauri build-time engine that generates Rust code at compile time using the generate_context! macro for zero-runtime-cost tauri::Context.

- Repository: [Tauri/tauri](https://github.com/tauri-apps/tauri)
- Tags: deep-dive
- Published: 2026-02-26

---

**The tauri-codegen crate is Tauri's build-time engine that reads your configuration, processes static assets, and generates Rust code via the `generate_context!` macro to create a zero-runtime-cost `tauri::Context`.**

The `tauri-codegen` crate sits at the core of the [tauri-apps/tauri](https://github.com/tauri-apps/tauri) repository, handling all heavy lifting during compilation to ensure your final binary remains small, fast, and secure. By separating code generation from runtime, Tauri embeds assets directly into the executable, eliminating file-system reads at runtime.

## How tauri-codegen Works: The Compile-Time Pipeline

The code generation process follows a strict pipeline that transforms your [`tauri.conf.json`](https://github.com/tauri-apps/tauri/blob/main/tauri.conf.json) and asset directories into a fully configured Rust `Context`.

### Step 1: Macro Invocation and Argument Parsing

When you invoke `tauri::generate_context!("tauri.conf.json")` in your [`main.rs`](https://github.com/tauri-apps/tauri/blob/main/main.rs), the procedural macro defined in **[`tauri-macros/src/lib.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-macros/src/lib.rs)** (lines 83-86) delegates to `tauri_codegen::context::generate_context`.

The `ContextItems::parse` function in **[`tauri-macros/src/context.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-macros/src/context.rs)** (lines 24-48) extracts:
- The configuration file path
- Optional root crate path
- Custom asset expressions
- Capability definitions
- Test mode flags

### Step 2: Configuration Loading and Validation

The `get_config` function in **[`tauri-codegen/src/lib.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/lib.rs)** (lines 53-61) reads the JSON configuration file or merges the `TAURI_CONFIG` environment variable override. It returns a validated `Config` struct along with its parent directory path for resolving relative asset paths.

### Step 3: Asset Processing and Embedding

The `EmbeddedAssets::new` function in **[`tauri-codegen/src/embedded_assets.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/embedded_assets.rs)** (lines 68-86) handles the heavy lifting of asset preparation:

1. **Directory Walking**: Uses `walkdir` to traverse asset directories recursively
2. **Asset Mapping**: Applies the `map_core_assets` closure (defined in **[`tauri-codegen/src/context.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/context.rs)**, lines 70-93) to inject CSP nonces and hash inline scripts
3. **Compression**: When the `compression` feature is enabled, applies Brotli compression to assets for release builds
4. **Checksum Generation**: Computes Blake3 hashes of file contents for integrity verification (see **[`tauri-codegen/src/lib.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/lib.rs)**, lines 101-114)
5. **Output Writing**: Writes processed assets to `$OUT_DIR/tauri-codegen-assets/<hash>.<ext>` only if the hash-named file doesn't already exist, enabling incremental builds

### Step 4: TokenStream Generation and Context Construction

The `context_codegen` function in **[`tauri-codegen/src/context.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/context.rs)** (lines 35-94) constructs the final Rust code:

1. **Context Instantiation**: Builds a `tauri::Context` struct populated with the parsed `Config`, `EmbeddedAssets`, default window icons, application icons, package metadata, and runtime authority (ACL)
2. **Asset Embedding**: Uses `include_bytes!` macros to embed both original files (for Cargo dependency tracking) and compressed versions (for runtime use)
3. **CSP Injection**: Injects computed Content Security Policy hashes into the context
4. **Isolation Schema**: When the `isolation` feature is enabled, generates the cryptographic schema for the isolation iframe pattern

The `EmbeddedAssets::to_tokens` implementation in **[`tauri-codegen/src/embedded_assets.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/embedded_assets.rs)** (lines 92-104) demonstrates how the asset map transforms into a static `phf::Map` or similar structure within the generated code.

## Key Components of the tauri-codegen Crate

Understanding the crate's architecture requires familiarity with these core modules:

- **[`tauri-codegen/src/lib.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/lib.rs)**: Entry point providing `get_config()` for JSON parsing and Blake3 checksum utilities
- **[`tauri-codegen/src/context.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/context.rs)**: Contains `context_codegen()`, the main orchestration function that coordinates config loading, asset processing, and TokenStream generation
- **[`tauri-codegen/src/embedded_assets.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/embedded_assets.rs)**: Implements `EmbeddedAssets` struct with asset walking, Brotli compression, CSP hash collection, and `ToTokens` trait for code generation

## Practical Usage Examples

### Basic Application Setup

The standard pattern embeds your [`tauri.conf.json`](https://github.com/tauri-apps/tauri/blob/main/tauri.conf.json) and default assets:

```rust
fn main() {
    tauri::Builder::default()
        .run(tauri::generate_context!("tauri.conf.json"))
        .expect("error while running tauri application");
}

```

This macro invocation triggers the full code generation pipeline at compile time.

### Custom Asset Injection

For applications requiring dynamic asset handling or custom asset implementations:

```rust
tauri::generate_context!(
    "tauri.conf.json",
    assets = my_custom_assets_expr,
    capabilities = ["src/capabilities.json"],
    test = true
);

```

The `assets` parameter accepts any expression implementing `tauri::Assets<R>`, forwarded directly to `context_codegen` as implemented in **[`tauri-macros/src/context.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-macros/src/context.rs)** (lines 94-101).

### Feature Flags for Advanced Security

Enable additional processing through Cargo features:

```toml
[features]
compression = ["tauri-codegen/compression"]
isolation   = ["tauri-codegen/isolation"]

```

- **compression**: Activates Brotli compression in `EmbeddedAssets::new` for release builds
- **isolation**: Generates cryptographic isolation schemas and CSP hashes during the asset mapping phase

## Summary

- **tauri-codegen** is the build-time engine in the `tauri-apps/tauri` repository responsible for compile-time code generation.
- The crate parses [`tauri.conf.json`](https://github.com/tauri-apps/tauri/blob/main/tauri.conf.json), processes static assets with optional Brotli compression, and computes Blake3 checksums for integrity verification.
- It generates a `TokenStream` via `context_codegen` in **[`tauri-codegen/src/context.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/context.rs)** that constructs a fully configured `tauri::Context` with embedded assets, CSP hashes, and security policies.
- The `generate_context!` macro in **[`tauri-macros/src/lib.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-macros/src/lib.rs)** serves as the public interface, triggering the entire pipeline during compilation to produce zero-runtime-cost desktop applications.

## Frequently Asked Questions

### What is the difference between tauri-codegen and tauri-macros?

**tauri-codegen** contains the core logic for parsing configuration, processing assets, and generating Rust code, while **tauri-macros** provides the procedural macro interface that developers actually invoke. The `generate_context!` macro defined in **[`tauri-macros/src/lib.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-macros/src/lib.rs)** parses macro arguments and delegates to `tauri_codegen::context::generate_context`, separating the macro API from the code generation implementation.

### How does tauri-codegen handle asset compression?

When the `compression` feature is enabled, `tauri-codegen` uses Brotli compression within `EmbeddedAssets::new` in **[`tauri-codegen/src/embedded_assets.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/embedded_assets.rs)**. The system compresses assets during the build process, writes them to `$OUT_DIR/tauri-codegen-assets/`, and embeds both the original (for Cargo dependency tracking) and compressed versions using `include_bytes!`. This ensures runtime access to compressed assets without file-system reads.

### Can I use tauri-codegen without the generate_context! macro?

While `tauri-codegen` is designed to work with the `generate_context!` macro, you can theoretically use its public API directly in custom build scripts. The `context_codegen` function in **[`tauri-codegen/src/context.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/context.rs)** and `get_config` in **[`tauri-codegen/src/lib.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/lib.rs)** are publicly exposed. However, this requires manually handling the `TokenStream` generation and asset embedding that the macro automates, making it uncommon outside of advanced custom build scenarios.

### What hashing algorithm does tauri-codegen use for asset integrity?

**tauri-codegen** uses **Blake3** for computing asset checksums. The `get_checksum` or equivalent functionality in **[`tauri-codegen/src/lib.rs`](https://github.com/tauri-apps/tauri/blob/main/tauri-codegen/src/lib.rs)** (lines 101-114) generates Blake3 hashes of the (potentially transformed) file contents. These hashes serve as unique identifiers for the asset files written to `$OUT_DIR/tauri-codegen-assets/`, enabling incremental builds and integrity verification without reprocessing unchanged assets.