# Diagnostics

Whatever `helm-schema` cannot resolve, it reports as a diagnostic on standard error instead of
guessing. The schema goes to standard output or the `--output` file, so diagnostics never end up in
it.

## Format

`--diag-format` picks one of two forms:

- **`text`**, the default: one line per diagnostic, starting with `warning:` for a problem or
  `info:` for a note.
- **`json`:** one JSON object per line, with its variant in a `type` field.

A run offline with an empty cache:

```console
$ helm-schema ./mychart --offline --k8s-schema-cache-dir ./empty \
    -o values.schema.json
warning: no upstream Kubernetes JSON schema found for Deployment (apps/v1) in v1.35.0
warning: no upstream Kubernetes JSON schema found for Service (v1) in v1.35.0
```

With `--diag-format json`, the first of them is this line, shown here with its fields spread out:

```json
{
  "type": "MissingSchema",
  "kind": "Deployment",
  "api_version": "apps/v1",
  "k8s_versions_tried": ["v1.35.0"],
  "tried_filenames": ["deployment-apps-v1.json"],
  "available_in_cache_versions": [],
  "suggested_k8s_version": null,
  "hint": null
}
```

Once the command line is parsed, every line on standard error is such an object. A command line
that cannot be parsed prints a plain usage error and exits with a non-zero status before any JSON
line.

## Variants

Lookups of Kubernetes and CRD schemas:

| Variant                              | When                                                                                                                                      |
| ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------- |
| `MissingSchema`                      | No source has the resource's schema. Lists the Kubernetes versions and file names tried, and cached versions that have it.               |
| `ResolvedFromFallbackVersion`        | A version other than the primary had the schema, through [fallback](https://romnn.github.io/helm-schema/docs/guide/kubernetes-schemas/#automatic-version-fallback). |
| `InferredApiVersion`                 | An `apiVersion` was inferred for a resource whose template does not state it (needs `--api-version-guess`).                              |
| `AmbiguousApiVersion`                | Several `apiVersion`s are possible, so none is used.                                                                                      |
| `CrdVersionNotFound`                 | The CRD at the chart's exact version was found nowhere.                                                                                   |
| `CrdVersionAvailableAtOtherVersions` | The CRD is cached at other versions (`--crd-version-lookup loose`). For information; another version is never used.                      |
| `LocalOverrideUnreadable`            | A file in `--crd-override-dir` exists but cannot be read. The lookup stops there.                                                         |

The cache:

| Variant                          | When                                                                                                |
| -------------------------------- | --------------------------------------------------------------------------------------------------- |
| `CacheLayoutInvalidated`         | A cache was written in an older layout; it was cleared and fills again.                             |
| `CacheLayoutForwardIncompatible` | A cache was written by a newer `helm-schema`, which this one leaves untouched.                      |
| `CacheWriteSkipped`              | A schema was not cached because its file name would not work on every platform; it was still used. |

The schema:

| Variant                             | When                                                                                                                                                    |
| ----------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `InputChannelNumericRangeAmbiguity` | A template ranges over a number, which works from `--set` but not from a values file; the schema cannot tell them apart.                                 |
| `DiscoveredConfigWeakensEmission`   | `helm-schema.yaml` turns checks off; see [Configuration](https://romnn.github.io/helm-schema/docs/reference/configuration/#seeing-the-result).                                               |
| `SchemaCompactedForHelm`            | The readable schema was over Helm's 5 MiB limit, so it was written as compact JSON with short names.                                                     |
| `SchemaExceedsHelmFileLimit`        | The written schema is over Helm's 5 MiB limit, and Helm will refuse the chart. See [Output](https://romnn.github.io/helm-schema/docs/reference/output/#size-limit).                          |

## What to do

Most diagnostics are about typing, not about a fault in the chart: a `MissingSchema` for a rare CRD
only leaves its fields loosely typed. By variant:

- **`MissingSchema` or `CrdVersionNotFound`:** add a
  [CRD catalog mirror](https://romnn.github.io/helm-schema/docs/guide/crd-schemas/#mirrors-and-caching), fill the
  cache online, or put the schema in
  [`--crd-override-dir`](https://romnn.github.io/helm-schema/docs/guide/crd-schemas/#local-overrides).
- **`AmbiguousApiVersion`:** state the `apiVersion` in the template if you can; otherwise the
  resource stays untyped, rather than typed wrongly.
- **`ResolvedFromFallbackVersion`:** expected for a chart that uses a removed API.
- **`CacheLayoutForwardIncompatible`:** upgrade `helm-schema`, or point it at another cache
  directory; see [Caching](https://romnn.github.io/helm-schema/docs/reference/caching/).
- **`InputChannelNumericRangeAmbiguity`:** also run CI values files through `helm template -f`; see
  [Troubleshooting](https://romnn.github.io/helm-schema/docs/troubleshooting/).

## In CI

With diagnostics on standard error and the schema in a file, both can be kept:

```sh
helm-schema ./mychart --diag-format json \
  --output values.schema.json \
  2> diagnostics.jsonl
```

Fail the build on the variants that matter to you by their `type`; [Continuous
integration](https://romnn.github.io/helm-schema/docs/ci/#surfacing-diagnostics) has an example.
