---
title: "Diagnostics, TypeShade authoring guide"
description: "After this page you can read a coded error, branch on its code, get every failure in a module in one report, and print the TypeScript line an error came from."
url: https://typeshade.dev/guide/internals/diagnostics/
language: en
---

# Diagnostics

After this page you can read a diagnostic, branch your own code on the code it carries, get every failure in a module in one report, and find the line an error came from.

A diagnostic is one problem found in one program: a stable code, a severity of error or warning, a message, and the place in the source it belongs to. Two kinds reach a host. [`compile()`](https://typeshade.dev/api/compile/) reports what is wrong with a source as a list of diagnostics, with `TS8` codes, and never throws. A call that reads a module, an emit, [`reflect`](https://typeshade.dev/api/reflect/) or the oracle, throws a [`TypeShadeError`](https://typeshade.dev/api/typeshadeerror/) with an `SD` code when the module cannot be done what was asked.

## Coded errors

`compile()` returns `diagnostics`, and each one carries its `code`, its `category`, its `message`, the `fileName` and the 1-based `line` and `character` where it starts, with `endLine` and `endCharacter` where it ends. A source with an error has no `wgsl` and no `glsl`, because a partial module is never written out as shader text.

```ts
import { compile } from 'typeshade'

const { diagnostics } = compile(source, { fileName: 'rim.shade.ts' })
for (const d of diagnostics) {
  console.error(`${d.fileName}:${d.line}:${d.character} ${d.code} ${d.message}`)
}
```

For a `rim.shade.ts` that declares `edge` with `const` and assigns to it on line 5, the loop prints:

```
rim.shade.ts:5:3 TS8005 Cannot assign to "edge" — it is declared with const.
```

Every coded failure a host call raises is a `TypeShadeError`. It carries a `code` from a frozen catalogue, a composed `message`, and a `hint` where the catalogue has a one-line remedy for that code. [`ValidationError`](https://typeshade.dev/api/validationerror/) and [`UnsupportedFeatureError`](https://typeshade.dev/api/unsupportedfeatureerror/) are subclasses, so one `instanceof TypeShadeError` handler catches every coded failure.

```ts
import { TypeShadeError, emitGlslModule } from 'typeshade'

try {
  emitGlslModule(m, 'fragment')
} catch (e) {
  if (e instanceof TypeShadeError) console.error(e.code, e.message, e.hint)
  throw e
}
```

For a module whose varying is `location<n, T, { interpolate: "linear" }>`, which is not supported yet by the GLSL backend, the call throws an `UnsupportedFeatureError` with the code [`SD0030`](https://typeshade.dev/reference/errors/sd0030/) and this message:

```
glsl-es300: @interpolate("linear") is not supported yet by the GLSL backend (Rule 10.3) — GLSL ES 3.00 has smooth, flat and centroid, and linear (desktop `noperspective`) is none of them. Use "perspective", or keep the shader WGSL-only.
```

`compile()` reports the same failure as a [`TS8015`](https://typeshade.dev/reference/errors/ts8015/) warning and keeps the WGSL.

## Branching on a code

The code is the half of a diagnostic to write your own code against. A `TS8` code is never renumbered ([Rule 12.2](https://typeshade.dev/reference/rules/12-2/)), and the `SD` codes come from an append-only catalogue, so a branch on one keeps working. Branch on the code, and show the message to the author.

```ts
const errors = compile(source).diagnostics.filter((d) => d.category === 'error')
if (errors.some((d) => d.code === 'TS8005')) {
  // a write to a const: report it against the author's own source
}
```

## Every failure at once

`compile()` collects every diagnostic of a source in one call, so a source with three mistakes reports three. One mistake reads as one diagnostic ([Rule 12.4](https://typeshade.dev/reference/rules/12-4/)): a refused declaration does not also draw an error at every line that uses its name. The `category` separates an `error`, which leaves no shader text, from a `warning`, which says how the program could be written better or which target it cannot reach.

A module you change after `compile()`, or one an engine assembles, is checked by `validate(m)`. It runs the emit-time rules over the module: no duplicate function or struct name, no binding collision, every path of a value-returning function returns, call sites that match the declarations they call. [`emitModule`](https://typeshade.dev/api/emitmodule/) and the GLSL emitters run it first, so a module that breaks one of them fails at the emit call with a coded error instead of at `createShaderModule` with a driver message. [`validate`](https://typeshade.dev/api/validate/) collects every error before it throws, and the `ValidationError` carries them on `.diagnostics`.

```ts
import { emitModule, ValidationError } from 'typeshade'

try {
  emitModule(m)
} catch (e) {
  if (e instanceof ValidationError) {
    for (const d of e.diagnostics) console.error(d.ruleId, d.fn, d.message)
  } else throw e
}
```

A module that carries the function `same` twice reports:

```
typeshade [SD0020]: module validation failed (1 error):
  - dup-func: duplicate function 'same'
```

## The diagnose report

`diagnose(m)`, on the `typeshade/dev` subpath, is the “what is wrong with this module?” entry. It runs the lint ruleset, adds a capability check when you pass a backend, and returns a report of every diagnostic plus a summary counting them. It never throws and it never changes the module, so it is safe to call on a module you are about to emit. `formatReport` renders the report as text. `lintModule(m)` runs the same ruleset and hands back the diagnostics as a plain array, with no summary and no capability check.

```ts
import { compile, wgslBackend } from 'typeshade'
import { diagnose, formatReport } from 'typeshade/dev'

const report = diagnose(compile(source).module, { rules: 'all', backend: wgslBackend })
if (report.summary.errors + report.summary.warnings > 0) console.error(formatReport(report))
```

For a source whose `same(a: f32, b: f32)` returns `a == b ? 1. : 0.`, the report reads:

```
warning no-float-eq  (fn same)
  f32 '==' in fn 'same' — exact float equality is unreliable

0 errors, 1 warning
```

`rules: 'all'` is wider than the emit-time set: naming, nesting depth, dead bindings, float equality and single exit. Some of those rules were written for modules the IR builder assembles. `single-exit` asks a function for one `return`, so it reports each early return a `"use typeshade"` function writes; read it as a style note, not a fault. `rules: 'core'` narrows the run to the set `validate` uses. The `backend` option adds one `SD0030` diagnostic naming every capability the backend cannot cover, which is the non-throwing twin of the gate `emitModule` runs:

```
error[SD0030] unsupported-capability
  backend 'glsl-es300' cannot emit this module — missing capabilities: compute, clipDistances
```

## Source locations

Every `compile()` diagnostic carries its place, as the samples above show, and a diagnostic in a file the source imports names that file. Nothing has to be turned on for it.

The module keeps the places too. Each statement the compiler builds carries the span of the source it came from, and `sourceSpanOf(stmt)` reads it: the file, the offsets and the line and character where it starts and ends. The debugger of `typeshade/debug` resolves its breakpoints and reports its steps by these spans. Locations never reach the emitted shader. A module the IR builder assembles records a location only with source tracing turned on, which [`docs/ir-builder.md`](https://github.com/typeshade/typeshade/blob/af54ddad5/docs/ir-builder.md) describes.
