# CLI Cortex Framework compile

`uv run cortex-compile` builds Google Cloud Cortex Framework modules into an
ephemeral workspace (or uses an existing generated Dataform directory)
and compiles the SQL dependency graph locally with the Dataform CLI. It
outputs a formatted summary table or JSON manifest of compiled models,
assertions, operations, and source declarations.

## Before you begin

- **Local Dataform compiler** : `uv run cortex-compile` invokes the local Dataform CLI (`dataform` or `npx @dataform/cli`) with a 5-minute (`5m`) compile timeout and requires Node.js (`npx`) or `@dataform/cli` in your `PATH`. See [Install Dataform CLI](https://docs.cloud.google.com/cortex/docs/prerequisites.md.txt#dataform-cli).
- **Credentials and offline behavior** :
  - **Existing directory (`--dist-dir`)**: Compiles strictly offline with no Google Cloud API calls or BigQuery credentials required.
  - **Ephemeral build (default, omitting `--dist-dir`)** : Runs the workspace builder first before compiling offline. When non-external data foundations or external catalogs are enabled in `config.yaml`, the build step requires BigQuery or BigLake read access to inspect source schemas and catalog tables.

## Synopsis

```bash
uv run cortex-compile [-h] [MODULE_FILTER] \
    [-m MODULE_FILTER] \
    [-c CONFIG_PATH | --config CONFIG_PATH] \
    [-d DIST_DIR] \
    [-w WORKSPACE_ROOT] \
    [-k] [--json]
```

Replace the following:

- `MODULE_FILTER`: optional `moduleId` or model name substring to filter the console summary output, for example: `erp` or `sales_document_items`.
- `CONFIG_PATH`: path to your global `config.yaml` file, for example: `config/config.yaml`.
- `DIST_DIR`: path to an existing generated Dataform directory to compile directly without rebuilding, for example: `dist`.
- `WORKSPACE_ROOT`: workspace root directory, for example: `.`.

## Ephemeral compilation workflow

Unless you pass `--dist-dir`, `uv run cortex-compile` validates `config.yaml`,
generates Dataform project files (`workflow_settings.yaml`,
`includes/`, and `definitions/`) in an isolated temporary directory
(`.cortex/.tmp_dist/build_<id>/`), runs `dataform compile --timeout=5m`, and
removes the temporary directory automatically when finished (unless `-k` or
`--keep-temp` is set). On successful compilation, `uv run cortex-compile` also
writes the raw compilation graph JSON automatically to
`DIST_DIR/compilation_graph.json` (which
persists on disk when compiling with `--dist-dir` or when `--keep-temp` is set).

Passing `-m` (or positional `MODULE_FILTER`) still builds and compiles the full
configured workspace graph---verifying cross-module `${ctx.ref(...)}`
dependencies---before filtering the terminal summary table rows (`MODEL NAME`,
`TARGET`, `TYPE`, `COLUMNS`, and `DEPS`) for tables, views, incremental models,
and operations. The summary footer's `ops`, `assertions`, and `declarations`
counts always reflect the full compiled graph. When you pass `--json`, the
command writes the complete raw Dataform compilation graph to standard
output. The command exits with status code `0` on success and `1` if
configuration validation, package generation, or Dataform compilation
fails.

## Global flags

The following table describes all flags supported by `uv run cortex-compile`:

| Flag | Description | Default |
| `-h, --help` | Show the command help message and exit. | None |
| `-m, --module-id, --module MODULE_FILTER` | Filter the console summary table rows (tables, views, incremental models, and operations) to actions whose path or name matches the specified module ID or model name substring. Summary footer counts for `ops`, `assertions`, and `declarations` still reflect the full graph. Takes precedence over the positional `MODULE_FILTER` argument; ignored when `--json` is set. | All compiled models |
| `-c, --config CONFIG_PATH` | Path to the global [`config.yaml`](https://docs.cloud.google.com/cortex/docs/deployment-configuration.md.txt#config-config-yaml) configuration file used when building an ephemeral workspace. Relative paths (and the default `config/config.yaml`) are resolved relative to `-w, --workspace-root` (which defaults to the current working directory). Ignored when `--dist-dir` is specified. | `config/config.yaml` |
| `-d, --dist-dir DIST_DIR` | Path to an existing generated Dataform workspace directory (such as `dist`) to compile strictly offline without running the Python build step first. | None (builds in ephemeral directory) |
| `-w, --workspace-root WORKSPACE_ROOT` | Workspace root directory used to resolve relative `--config` paths (and the default `config/config.yaml`), relative `--dist-dir` paths, and temporary build directories (`.cortex/.tmp_dist/`). Relative `data.namespaces[].path` entries resolve relative to the directory containing `config.yaml`. | Current working directory |
| `-k, --keep-temp` | Preserve the temporary generated Dataform workspace directory (`.cortex/.tmp_dist/build_<id>/`, including `compilation_graph.json`) on disk after compilation for manual SQLX and JavaScript inspection. | `false` |
| `--json` | Write the complete Dataform JSON compilation graph (tables, views, incremental models, assertions, operations, and declarations) to standard output instead of the formatted summary table. Logs and status messages are routed to standard error (`stderr`) so standard output (`stdout`) contains clean JSON. | `false` |
|---|---|---|

## Examples

Compile the entire configured workspace in an ephemeral directory and inspect
the summary table:

```bash
export CORTEX_CONFIG_FILE=config/config.yaml

uv run cortex-compile --config $CORTEX_CONFIG_FILE
```

Compile all enabled modules and filter the summary table to a single foundation
or data product:

```bash
export CORTEX_CONFIG_FILE=config/config.yaml

uv run cortex-compile -m sap_sales_documents --config $CORTEX_CONFIG_FILE
```

Compile an existing `dist` build directory strictly offline and redirect the
JSON graph to `compilation_graph.json` in the current directory (in addition to
the `dist/compilation_graph.json` file that `cortex-compile` writes
automatically):

```bash
uv run cortex-compile --dist-dir dist --json > compilation_graph.json
```

## What's next

- Build a deployable Dataform workspace with [`uv run cortex-build`](https://docs.cloud.google.com/cortex/docs/uv-run-cortex-build.md.txt).
- Extract and cache BigQuery schemas with [`uv run cortex-metadata`](https://docs.cloud.google.com/cortex/docs/uv-run-cortex-metadata.md.txt).
- Validate custom modules using [Build data products with agent skills](https://docs.cloud.google.com/cortex/docs/agentic-skills-for-data-product-building.md.txt).