> For the complete documentation index, see [llms.txt](https://docs.midnight.network/llms.txt)

# Troubleshoot compiler errors

This guide covers common Compact compiler errors, their causes, and how to fix them.

## Exit codes[​](#exit-codes "Direct link to Exit codes")

The Compact compiler returns the following exit codes:

| Code | Meaning                    | Fix                                              |
| ---- | -------------------------- | ------------------------------------------------ |
| 0    | Compilation succeeded      | No action required                               |
| 1    | Bad command-line arguments | Check `compact compile --help` for correct flags |
| 255  | Compilation failed         | Fix the reported source errors and recompile     |

## Error severity levels[​](#error-severity-levels "Direct link to Error severity levels")

The Compact compiler uses the following error severity levels:

| Mechanism         | Severity | Description                                            |
| ----------------- | -------- | ------------------------------------------------------ |
| `source-errorf`   | Fatal    | User-visible error with source location                |
| `source-warningf` | Warning  | Continuable warning with source location               |
| `pending-errorf`  | Deferred | Collected and shown together after the pass            |
| `internal-errorf` | Fatal    | Compiler internal bug; report this to the Compact team |
| `external-errorf` | Fatal    | External tool or file system error                     |

## Lexer errors[​](#lexer-errors "Direct link to Lexer errors")

These errors occur during tokenization, before any parsing takes place.

### Unexpected end of file[​](#unexpected-end-of-file "Direct link to Unexpected end of file")

**Message**: `"unexpected end of file"`

**Triggers**: The compiler reaches the end of the file while inside an unclosed string literal or block comment.

**Fix**: Check for unclosed strings (`"…`) and unclosed block comments (`/* …`) in the source file.

### Unexpected newline[​](#unexpected-newline "Direct link to Unexpected newline")

**Message**: `"unexpected newline"`

**Triggers**: The compiler encounters a line break inside a context where a newline is not valid, such as inside a single-line string literal.

**Fix**: Ensure string literals do not span multiple lines. Use concatenation or a supported multi-line construct instead.

### Unexpected character[​](#unexpected-character "Direct link to Unexpected character")

**Message**: `"unexpected character '<c>'"`

**Triggers**: The compiler encounters a character that is not valid in Compact source code.

**Fix**: Remove or replace the invalid character. Check for non-ASCII characters, stray punctuation, or invisible Unicode characters copied from external sources.

### Nested block comment[​](#nested-block-comment "Direct link to Nested block comment")

**Message**: `"attempt to nest block comment"`

**Triggers**: Using `/*` inside an already-open `/* */` block comment.

**Fix**: Compact does not support nested block comments. Use line comments (`//`) for inner comments, or restructure to avoid nesting.

### Numeric literal out of Field range[​](#numeric-literal-out-of-field-range "Direct link to Numeric literal out of Field range")

**Message**: `"<value> is out of Field range"`

**Triggers**: A numeric literal exceeds the maximum representable Field value.

**Fix**: Use a smaller number. Field values are bounded by the prime used in the ZK proof system.

### Invalid digit in binary literal[​](#invalid-digit-in-binary-literal "Direct link to Invalid digit in binary literal")

**Message**: `"unexpected digit <d> (expected 0 or 1)"`

**Triggers**: A digit other than `0` or `1` appears in a `0b…` literal.

**Fix**: Binary literals may only contain the digits `0` and `1`.

### Invalid digit in octal literal[​](#invalid-digit-in-octal-literal "Direct link to Invalid digit in octal literal")

**Message**: `"unexpected digit <d> (expected 0 through 7)"`

**Triggers**: A digit `8` or `9` appears in a `0o…` literal.

**Fix**: Octal literals may only contain digits `0` through `7`.

## Parser errors[​](#parser-errors "Direct link to Parser errors")

These errors occur after tokenization, while the compiler builds the AST.

### Parse error[​](#parse-error "Direct link to Parse error")

**Message**: `"parse error: found <token> looking for <expected>"`

**Triggers**: Any syntax that does not match the Compact grammar at the current parse position.

**Fix**: Read the location carefully. Common causes include:

* Missing semicolons at the end of statements
* Mismatched braces `{` / `}`
* Wrong or misspelled keyword
* Extra or missing commas in argument lists

### Unrecognized pragma setting[​](#unrecognized-pragma-setting "Direct link to Unrecognized pragma setting")

**Message**: `"unrecognized pragma setting <value>"`

**Triggers**: A `pragma` directive uses a value the compiler does not recognize.

**Fix**: Check supported pragma directives. Example of a valid pragma:

```
pragma language_version >= 0.23;
```

### Language version mismatch[​](#language-version-mismatch "Direct link to Language version mismatch")

**Message**: `"language version <version> mismatch"`

**Triggers**: The `pragma language_version` line in your contract doesn't accept the language version that your toolchain implements. For example, toolchain 0.31.1 implements language version 0.23.0, so `pragma language_version 0.27;` fails with `language version 0.23.0 mismatch`. Toolchain 0.36.0 implements language version 0.28.0, so `pragma language_version 0.23;` fails with `language version 0.28.0 mismatch`.

**Fix**: Use the toolchain for your target network, and set the pragma to match its language version:

* Mainnet, Preview, and Preprod: toolchain 0.36.0, language version 0.28.

Check the [compatibility matrix](https://docs.midnight.network/relnotes/support-matrix.md) for the current versions. To select a toolchain version for one compile, run `compact compile +<version>`.

### File I/O errors[​](#file-io-errors "Direct link to File I/O errors")

The compiler returns the following messages:

* `"error opening source file"`
* `"error reading source file"`
* `"<path> is a directory"`

**Triggers**: The compiler encounters an error opening or reading the specified source file, or the path points to a directory.

**Fix**: Verify the file path is correct, the file exists, and it has a `.compact` extension. Do not pass a directory path where the compiler expects a file.

## Name resolution errors[​](#name-resolution-errors "Direct link to Name resolution errors")

These errors occur when the compiler can't match a name in your source to a declaration.

### Unbound identifier[​](#unbound-identifier "Direct link to Unbound identifier")

**Message**: `"unbound identifier <name>"`

**Triggers**: The contract uses a name that isn't declared in your contract, isn't imported, or doesn't exist in the standard library of your toolchain. A common cause is a standard library function or type that only ledger 9 toolchains include. For example, toolchain 0.31.1 reports `unbound identifier` for `keccak256`.

**Fix**: Check the spelling and check that you import `CompactStandardLibrary`. If the name comes from the standard library, check that your toolchain includes it:

* Mainnet, Preview, and Preprod run ledger version 9. Compile with Compact toolchain 0.36.0. It needs no extra compiler flag.

Check the [compatibility matrix](https://docs.midnight.network/relnotes/support-matrix.md) for the toolchain each network uses.

## Witness and disclosure errors[​](#witness-and-disclosure-errors "Direct link to Witness and disclosure errors")

These errors enforce Compact's privacy model around witness values.

### Undeclared witness disclosure[​](#undeclared-witness-disclosure "Direct link to Undeclared witness disclosure")

**Message**: `"potential witness-value disclosure must be declared but is not"`

**Triggers**: A witness value flows to the ledger or a public output without a `disclose()` wrapper.

**Fix**: Wrap the witness value in `disclose()` before it reaches any ledger state or public output:

```
disclose(witnessValue)
```

The `disclose()` wrapper declares private data as safe to write to the public ledger state.

### Witness returns contract-typed value[​](#witness-returns-contract-typed-value "Direct link to Witness returns contract-typed value")

**Message**: `"invalid type <T> for witness <W> return value: witness return values cannot include contract values"`

**Triggers**: A witness function declares a return type that includes a contract-typed value, and you compile with toolchain 0.31.x or earlier. These toolchains target ledger version 8, which no public network runs since the [ledger 9 network upgrade](https://docs.midnight.network/relnotes/ledger-9.md). Toolchains 0.35.0 and 0.36.0 accept a witness that returns a contract-typed value.

**Fix**: Remove any contract-typed values from the witness return type. Witnesses can return structs, enums, and other regular types, but not contract values. Pass any contract-related information as individual fields instead.

## ZKIR generation errors[​](#zkir-generation-errors "Direct link to ZKIR generation errors")

These errors occur when the compiler generates the ZK Intermediate Representation (ZKIR) from the type-checked AST.

### Cross-contract calls not supported by this toolchain[​](#cross-contract-calls-not-supported-by-this-toolchain "Direct link to Cross-contract calls not supported by this toolchain")

**Message**: `"cross-contract calls are not yet supported"`

**Triggers**: The contract makes a cross-contract call, and you compile with toolchain 0.31.x or earlier. These toolchains target ledger version 8, which has no cross-contract calls.

**Fix**: Check which ledger version your target network runs in the [compatibility matrix](https://docs.midnight.network/relnotes/support-matrix.md). Mainnet, Preview, and Preprod all run ledger version 9. Mainnet moved to it in the [ledger 9 network upgrade](https://docs.midnight.network/relnotes/ledger-9.md). To target any of them, compile with Compact toolchain 0.36.0. It compiles cross-contract calls with no extra flag, and passing `--feature-zkir-v3` to it is an error, so remove that flag from your build scripts. In 0.36.0, a circuit declared in a contract type can't be `pure`, and the arguments of every cross-contract call must be disclosed. For the syntax, see [Cross-contract calls](https://docs.midnight.network/compact/reference/compact-reference.md#cross-contract-calls) in the language reference and the [Compact release notes](https://docs.midnight.network/relnotes/compact.md).

### ZKIR non-zero exit status[​](#zkir-non-zero-exit-status "Direct link to ZKIR non-zero exit status")

**Message**: `"zkir returned a non-zero exit status <N>"`

**Triggers**: The external ZKIR compilation tool exited with an error.

**Fix**: Review the output for details on the unsupported operation. Check for operations in circuits that the ZKIR backend does not yet support.

## Runtime errors[​](#runtime-errors "Direct link to Runtime errors")

These errors originate from compiled contracts executing in the Midnight runtime, not from the compiler itself.

### Base error class[​](#base-error-class "Direct link to Base error class")

**Class**: `CompactError`

**Description**: `CompactError` is the base class for all contract runtime errors. Catch this type to handle any Compact contract error generically.

### Failed assertion[​](#failed-assertion "Direct link to Failed assertion")

**Message**: `"failed assert: <message>"`

**Triggers**: A Compact `assert` expression evaluated to `false` at runtime.

**Fix**: Investigate the assertion condition. The `<message>` text is the string you provided to `assert`. Use it to locate the assertion in your contract source and determine why the condition was not satisfied.

### Runtime type error[​](#runtime-type-error "Direct link to Runtime type error")

**Message**: `"type error: <who> <what> at <where>; expected value of type <type> but received <value>"`

**Triggers**: A runtime type mismatch in the generated code, typically caused by a mismatch between declared witness return types and the actual values produced at runtime.

**Fix**: Check witness return types. Ensure that the TypeScript witness implementation returns values that conform to the types declared in the Compact contract.

### Version mismatch[​](#version-mismatch "Direct link to Version mismatch")

**Message**: `"version mismatch: compiled code expects X.Y.Z, runtime is A.B.C"`

**Triggers**: You compiled the smart contract with one version of the Compact compiler but run it against a different version of `@midnight-ntwrk/compact-runtime`.

**Fix**: Update `@midnight-ntwrk/compact-runtime` in your project to match the compiler version used to build the contract. Alternatively, recompile the contract with the compiler version that matches your runtime.

### Contract artifacts refused after the ledger 9 upgrade[​](#contract-artifacts-refused-after-the-ledger-9-upgrade "Direct link to Contract artifacts refused after the ledger 9 upgrade")

**Class**: `EraMismatchError`, with the reason `unknown-artifact-runtime-version`

**Triggers**: Midnight.js 5.0.0 accepts contract artifacts built for Compact runtime 0.16 (toolchain 0.31.x) or 0.21 (toolchain 0.36.0) only. It refuses artifacts from any other toolchain, such as 0.30.0 or 0.35.0. Artifacts built with toolchain 0.31.x keep working for contracts deployed before the [ledger 9 network upgrade](https://docs.midnight.network/relnotes/ledger-9.md) when the ZK config provider uses `verify: 'require-if-present'`.

**Fix**: For a new contract, compile it with Compact toolchain 0.36.0. For a contract that is already deployed, recompiling changes its verifier keys, so move it to toolchain 0.36.0 through its maintenance authority or redeploy it. See [Recompiling a contract changes its verifier keys](https://docs.midnight.network/relnotes/ledger-9.md#recompiling-a-contract-changes-its-verifier-keys).

Version compatibility

Always refer to the [compatibility matrix](https://docs.midnight.network/relnotes/support-matrix.md) to verify which versions work together.
