> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gatojs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Module registry

> How GatoJS discovers features: the generated module registry, the .wa/kit.json kit state, how gato generate rewrites wa:* regions, and how to diagnose a feature that does not register.

The core never imports a module directly. Instead, `gato generate` reads `.wa/kit.json`, finds the features on disk and writes a **registry**: generated files that import each feature's definition, env schema, auth plugins and translations. It also fills **regions** (`wa:*`) in a few core files. This is how a module can be added or removed without editing core code.

## The registry files

There are two registry files: one safe for the browser and one for the server only. Here is the client file in a project with the core and three modules:

```ts src/modules.client.generated.ts theme={null}
// generated by wa — nie edytuj. Uruchom: npm run wa -- generate
import accountModule from "@/features/account/module";
import authModule from "@/features/auth/module";
import dashboardModule from "@/features/dashboard/module";
import organizationsModule from "@/features/organizations/module";
import usersModule from "@/features/users/module";
import apiKeysModule from "@/features/api-keys/module";
import auditLogsModule from "@/features/audit-logs/module";
import uploadsModule from "@/features/uploads/module";

export const authClientPlugins: never[] = [];
export const clientModules = [accountModule, authModule, dashboardModule, organizationsModule, usersModule, apiKeysModule, auditLogsModule, uploadsModule];
```

The header line reads "generated by wa — do not edit. Run: npm run wa -- generate".

Each feature file the generator recognizes ends up in a different array:

| Feature file | Export | Registry array | Registry file | Read by |
| - | - | - | - | - |
| `module.ts` | default `defineModule({...})` | `clientModules` | `src/modules.client.generated.ts` | The page guard (`requireModuleAccess`) and the sidebar. Both read the same array, so the menu and the guard always agree. |
| `module.ts` | default `defineModule({...})` | `modules` | `src/modules.generated.ts` | Modules that read other features' declarations: `api-keys` (`apiScopes`), `uploads` (`uploadPurposes`), `audit-logs` (`auditActions`). |
| `env.ts` | `envSchema` | `moduleEnvSchemas` | `src/modules.generated.ts` | `createEnv` from `@gatojs/env`, which intersects them with the core schema. |
| `auth.ts` | `authPlugin` | `authPlugins` | `src/modules.generated.ts` | The `plugins` list of the better-auth server config. |
| `auth-client.ts` | `authClientPlugin` | `authClientPlugins` | `src/modules.client.generated.ts` | The `plugins` list of the better-auth browser client. |
| `audit-sink.ts` | `auditSink` | `auditSinks` | `src/modules.generated.ts` | `trackAudit`, which calls every sink in turn. |
| `i18n/<locale>.json` | JSON content | `moduleMessages.<locale>` | `src/modules.generated.ts` | `src/i18n/request.ts`, which merges each file under the namespace equal to the folder name. |

The server registry also exports `env = createEnv(moduleEnvSchemas)`, the validated environment object behind `import { env } from "@/env"`.

Things worth knowing:

* **The registry imports files; it does not copy them.** Editing the contents of `module.ts`, `env.ts` or a translation file needs no regeneration. Adding or removing such a file does.
* **There are two files because of the browser bundle.** The client file imports only `module.ts` and `auth-client.ts`. The server file also imports env schemas, server auth plugins, audit sinks and translation loaders, none of which belong in the browser.
* **Order:** core features first, then other kit modules by module name, then your project modules alphabetically; within a module, by folder name. The menu ignores this order and sorts by `order`.
* **A feature lands in each array independently.** A feature with only translations shows up in `moduleMessages` but not in `clientModules`. No core feature has `env.ts`, `auth.ts`, `auth-client.ts` or `audit-sink.ts`, so in a core-only project those arrays are empty.

A third generated file, `src/i18n/locales.generated.ts`, holds the project's locales from `.wa/kit.json`. It is not part of the module registry, but `gato generate` writes it too.

> [!WARNING]
>
> Feature folders with translations must match `^[a-z][a-zA-Z0-9-]*$` (so `my_feature` is invalid) and must not be named `common`, `errors`, `sidebar` or `serviceMode`, which are core namespaces. Otherwise loading translations throws and the page does not render.

## `.wa/kit.json`

This file says which kit modules the project is made of and which features it adds itself. `gato create`, `gato add` and `gato remove` write it; you edit `locales` and `projectModules` by hand. Abridged:

```json .wa/kit.json theme={null}
{
  "kitVersion": "0.8.1",
  "kitRepo": "https://github.com/webamigos/gatojs.git",
  "project": { "name": "Acme CRM", "slug": "acme-crm" },
  "locales": ["pl", "en"],
  "modules": {
    "core": {
      "version": "0.11.1",
      "manifest": {
        "name": "core",
        "files": ["src/features/auth/**", "src/features/users/**", "…"],
        "env": [],
        "schemaRelations": [],
        "compose": null,
        "ciSteps": null
      },
      "files": { "src/features/users/module.ts": "<hash>", "…": "…" }
    },
    "uploads": {
      "version": "0.2.2",
      "manifest": {
        "name": "uploads",
        "files": ["prisma/schema/uploads.prisma", "src/features/uploads/**", "src/app/api/uploads/**"],
        "dependencies": { "@aws-sdk/client-s3": "^3.1159.0", "sharp": "^0.35.5" },
        "env": [{ "name": "STORAGE_PROVIDER", "example": "local", "required": false }],
        "schemaRelations": [
          { "model": "User", "field": "uploads Upload[]" },
          { "model": "Organization", "field": "uploads Upload[]" }
        ],
        "compose": "docker/uploads.compose.yml",
        "ciSteps": "ci/uploads.integration-steps.yml"
      },
      "files": { "…": "…" }
    }
  },
  "projectModules": {
    "invoices": {
      "manifest": {
        "name": "invoices",
        "version": "0.1.0",
        "description": "Invoices",
        "files": ["src/features/invoices/**", "prisma/schema/invoices.prisma"],
        "schemaRelations": [{ "model": "Organization", "field": "invoices Invoice[]" }]
      }
    }
  }
}
```

What the generator uses from it:

| Field | Used by the generator for |
| - | - |
| `project` | Replacing `{{projectName}}` and `{{projectSlug}}` in module env examples. |
| `locales` | `src/i18n/locales.generated.ts` and which translation loaders exist. Must contain `pl`, no duplicates. |
| `modules` | Kit modules, `core` first. Only the manifest matters to the generator; `version` and `files` (file hashes) are for `add`, `remove` and `update`. |
| `projectModules` | Your own features, registered after kit modules. Each entry has only a `manifest`, without version or hashes. |
| `manifest.files` | Only patterns starting with `src/features/<x>/` matter: they name the features. Other patterns are for copying and removing files. |
| `manifest.env`, `schemaRelations`, `compose`, `ciSteps` | Content of the `wa:*` regions. |
| `manifest.requires` | Checked for project modules only: every required module must be installed. |

`kitVersion` and `kitRepo` are read by `add`, `list` and `update` to fetch the matching kit. The full schema is in the [kit.json reference](https://docs.gatojs.com/cli/kit-json.md).

> [!NOTE]
>
> The schema ignores unknown fields. A typo such as `schemaRelation` instead of `schemaRelations` passes silently and the field gets its default value.

## Running the generator

```bash theme={null}
npx gato generate
# or, with the committed bundle
npm run wa -- generate
```

It prints `Bez zmian` ("no changes") or one `zmieniono <file>` ("changed") line per file written. `create`, `add` and `remove` run it automatically at the end.

Run it after you:

* change `.wa/kit.json`, including `locales` and `projectModules`;
* add or remove `module.ts`, `env.ts`, `auth.ts`, `auth-client.ts`, `audit-sink.ts` or `i18n/<locale>.json` in a feature;
* add, remove or rename a feature folder;
* change a file referenced by a manifest's `compose` or `ciSteps`.

Commit the output together with the change that caused it.

### How features are discovered

1. The generator walks the modules in registry order and their `files` patterns. A pattern that starts with `src/features/<x>/`, where `<x>` contains no `/` or `*`, makes `<x>` a candidate.
2. For each candidate, it checks which of the recognized files exist in `src/features/<x>/`. Translation files count only for locales listed in `locales`.
3. A candidate with at least one recognized file joins the registry. Otherwise it is skipped silently.
4. A name listed by several modules counts once.

| Pattern in `files` | Result |
| - | - |
| `src/features/<x>/**` | Feature `<x>`. |
| `src/features/<x>/module.ts` | Feature `<x>`; only the prefix matters. |
| `src/features/{a,b}/**` | Skipped: the generator looks for a folder literally named `{a,b}`. Use one pattern per feature. |
| `src/features/**`, `src/features/*/module.ts` | Skipped: a `*` right after `src/features/`. |
| `src/features/<x>/**` with no folder on disk | Skipped, so you can list a feature before creating it. |
| Anything outside `src/features/` | Does not affect the registry. |

The generator computes every file first and writes only at the end, so an error at any stage leaves all files untouched. It also writes only files whose content changed, so a second run reports no changes. CI relies on this:

```yaml .github/workflows/ci.yml theme={null}
- name: Regenerate wa files
  run: npm run wa -- generate

- name: Generated files are up to date
  run: git diff --exit-code
```

## Generated regions

A region is the part of a file between a line containing `wa:<marker>:start` and a line containing `wa:<marker>:end`. The generator replaces everything in between and keeps the marker lines. The comment syntax (`//` or `#`) comes from the file.

| File | Marker | Content | Source in the manifest |
| - | - | - | - |
| `prisma/schema/auth.prisma` (in `User`) | `wa:relations` | One line per relation with `model: "User"`. | `schemaRelations` |
| `prisma/schema/organizations.prisma` (in `Organization`) | `wa:relations` | Same for `model: "Organization"`. | `schemaRelations` |
| `.env.example` | `wa:modules` | For each non-core module with variables: `# <module>` then `NAME=example` lines. | `env` |
| `docker-compose.yml` (under `services:`) | `wa:services` | The referenced file, indented by two spaces. | `compose` |
| `.github/workflows/ci.yml`, `.github/workflows/mutation.yml` | `wa:ci-integration` | The referenced GitHub Actions steps, before the integration tests. | `ciSteps` |
| `Dockerfile` (in the `deps` stage) | `wa:workspaces` | A `COPY` line for each `packages/<name>/package.json`. | The project's `packages/*` folders |

For example, a project module with `schemaRelations: [{ "model": "Organization", "field": "invoices Invoice[]" }]` produces this in `organizations.prisma`:

```prisma prisma/schema/organizations.prisma theme={null}
  // wa:relations:start
  invoices Invoice[]
  // wa:relations:end
```

Region rules:

* Each file needs exactly one start and one end marker, with the end below the start. The region content must not contain its own marker.
* A missing target file is skipped without error.
* A target file without markers is skipped while the region would be empty. As soon as a module has content for it, the generator fails with `brak regionu` ("missing region").
* Relations on models other than `User` and `Organization` go to no region, and the generator does not report it. Put them in your module's own schema file.
* Core variables in `.env.example` are written directly in the file, outside the region.

## Diagnosing registration problems

| Symptom | Cause | Fix |
| - | - | - |
| The feature has no menu entry and its page returns 404. | No `src/features/<x>/...` pattern in any manifest, a pattern with `{}` or `*`, or the generator was not run. | Add a pattern per feature under `projectModules`, run `npx gato generate`, and check that `src/modules.client.generated.ts` imports `@/features/<x>/module`. |
| The menu entry has no translated label. | `key` in `module.ts` differs from the folder name, or the translation file lacks the `labelKey`. | Make `key` equal to the folder name and add the key to `i18n/<locale>.json`. |
| A whole locale (for example `/en/...`) returns 404. | The locale is missing from `locales` in `.wa/kit.json`. | Add it and run the generator. |
| A feature's translations in one locale do not load although the file exists. | The file was added without regenerating, so no loader exists for it. | Run the generator and commit. |
| A feature page works under `/en/...` but returns 404 without the prefix. | The route is `/api`, lives under `/api/` or contains a dot, so the proxy skips it. | Change the route. See [Architecture](https://docs.gatojs.com/concepts/architecture.md#proxy-locale-only). |
| CI fails at "Generated files are up to date". | A generated file or region was edited by hand, or a feature changed without regenerating. | Run the generator and commit. |
| The generator exits with an error. | See the table below. | |

### Generator errors

Errors go to stderr without a stack trace, exit with code 1, and leave every file unchanged. Messages are currently in Polish:

| Situation | Message |
| - | - |
| No `.wa/kit.json` in the current directory | `Brak .wa/kit.json w <dir> — to nie jest projekt wa-kit` |
| `.wa/kit.json` is not valid JSON | The JSON parser's message. |
| `.wa/kit.json` does not match the schema | A list of Zod issues with `path` and `message`, for example `locales musi zawierać "pl"`. |
| A `projectModules` key differs from `manifest.name` | `projectModules.<key>: name "<name>" nie zgadza się z kluczem` |
| The same name in `modules` and `projectModules` | `Moduł <name> jest jednocześnie modułem kitu i modułem projektu` |
| A project module requires a module that is not installed | `Moduł projektu <name> wymaga niezainstalowanego modułu: <module>` |
| A project module's `files` has no `src/features/<name>/…` pattern | `Moduł projektu <name>: files musi obejmować src/features/<name>/**` |
| Target file has no markers but a module has content for it | `<file>: brak regionu wa:<marker>; dodaj linie ze znacznikami wa:<marker>:start i wa:<marker>:end` |
| Missing, duplicated or reversed marker | `<file>: uszkodzony region wa:<marker>` |
| Region content contains its own marker | `<file>: treść regionu wa:<marker> zawiera znacznik regionu` |
| The file referenced by `compose` or `ciSteps` is missing | `ENOENT: no such file or directory, open '<path>'` |

> [!TIP]
>
> Writing your own feature? [Build a feature](https://docs.gatojs.com/guides/build-a-feature.md) walks through creating the folder, registering it in `projectModules` and generating the registry.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.