# Debugging

Three tools cover most of it: a debugger attached to the server code, a CPU profile of a slow command, and the log lines that say what the build does. The browser side of an app is debugged with the browser's own tools.

## Attach a debugger to the server

`hamur dev --inspect` starts each server process of the dev server with Bun's inspector. That process runs your loaders, handlers, middleware and plugins, so a breakpoint in any of them stops a request.

````sh [dev-server/inspect.sh]
hamur dev --inspect
hamur dev --inspect=9339
hamur dev --inspect=0.0.0.0:9339
hamur dev --inspect-brk
````

A bare `--inspect` listens on `127.0.0.1:9229`. `--inspect=9339` picks the port, and `--inspect=0.0.0.0:9339` picks the address too. An IPv6 address goes in brackets, `--inspect=[::1]:9229`. `hamur dev` prints the address after `Local:`:

```text
[hamur] Debugger: ws://127.0.0.1:9229/hamur
```

The path is always `/hamur`, so the address is the same for every process of the session and a launch configuration never changes. `--inspect-brk` also stops the first process on its first line, until a debugger lets it run. Later processes, after a server edit, start with `--inspect` alone, since a stop on the first line of every edit would wait for a debugger that is already attached.

These are the errors of the flags, each with exit code `1`:

| Message                                                               | Cause                                                                  |
| --------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `--inspect and --inspect-brk cannot be combined`                      | both flags were given                                                  |
| `--inspect port must be an integer from 1 to 65535 (got "abc")`       | the port is not a port                                                 |
| `--inspect takes an IPv6 host in brackets, as [::1]:9229 (got "::1")` | an IPv6 host without brackets                                          |
| `Inspector port 9229 is in use; pass --inspect=<port>`                | another process holds the port, such as a second `hamur dev --inspect` |

### Attach from VS Code

The first configuration attaches to the server. It needs the Bun extension for VS Code. Start `hamur dev --inspect`, then start the configuration. The second one opens Chrome on the app and attaches VS Code to the page, so a breakpoint in a component stops in the editor. The client bundle carries source maps, which is how the terminal shows `app/pages/index.tsx:12` for a browser `console` call. The compound starts both.

````json [dev-server/launch.json]
{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "bun",
      "request": "attach",
      "name": "Hamur server",
      "url": "ws://127.0.0.1:9229/hamur"
    },
    {
      "type": "chrome",
      "request": "launch",
      "name": "Hamur browser",
      "url": "http://localhost:3000",
      "webRoot": "${workspaceFolder}"
    }
  ],
  "compounds": [
    {
      "name": "Hamur server and browser",
      "configurations": ["Hamur server", "Hamur browser"]
    }
  ]
}
````

When you save a server file, the dev server builds the next process while the old one finishes its requests, for at most 5 seconds. Two Bun processes cannot hold one inspector port, so the old process ends before the new one starts, and a request that arrives in between waits for it. The debugger detaches when its process ends; start the attach configuration again to join the next one. A request stopped at a breakpoint holds its process for those 5 seconds, then the process is stopped.

`devServer.inspect` is not this. That key records each step of the transform chain for `/_hamur/inspect` and the [Devtools](/devtools#inspecting-a-module) Inspect tab, and has nothing to do with a debugger.

## Profile a command

`--cpu-prof` on `dev`, `build`, `generate` and `preview` writes a CPU profile of the command, to find out where the time goes.

````sh [dev-server/cpu-prof.sh]
hamur build --cpu-prof
hamur dev --cpu-prof-md
hamur preview --cpu-prof
````

The command runs again under `bun --cpu-prof`, and the profiles go to `.hamur/profiles/` (under `buildDir`). When it ends, each one is printed:

```text
[hamur] Profile written: .hamur/profiles/<name>.cpuprofile
```

Open a `.cpuprofile` in the Performance tab of Chrome DevTools or in VS Code. `--cpu-prof-md` implies `--cpu-prof` and also writes a Markdown form beside each profile, a text report that reads without a viewer.

- In `hamur dev` each server process writes its own profile when it ends, which is on `SIGTERM`; a process killed with `SIGKILL` writes none. The dev supervisor prints every path at exit. The dev runner process itself is not profiled.
- `hamur preview` profiles the server it starts: `bun --cpu-prof` for the `bun` preset and `node --cpu-prof` for the `node` preset, which has no Markdown form. A `bun-compile` executable takes no flags and is not profiled.

## The `debug` key

`debug` writes lines about the build itself at the `info` level, with the `[hamur]` prefix. It is for finding out why a module changed an option, or which hook is slow.

| Key               | One line for                                                                               |
| ----------------- | ------------------------------------------------------------------------------------------ |
| `debug.hooks`     | each build hook call, with its time: `[hamur] hook pages:extend 3 ms`                      |
| `debug.templates` | each file Hamur writes from a template: `[hamur] template .hamur/sitemap.xml`              |
| `debug.modules`   | each module whose `setup` changed options: `[hamur] module @hamur/i18n changed routeRules` |

`debug: true` turns on all three, and so does the `--debug` flag, which also prints the stack of a failing command and writes the build's virtual modules to `.hamur/virtual/`. Pass the flag for one run, or set the key for the app:

````ts [dev-server/debug.ts]
import { defineConfig } from "hamur"

export default defineConfig({
  framework: "html",
  debug: { hooks: true, modules: true },
})
````

The lines are written at `info`, so `logLevel: "warn"` hides them. The log level is, from the strongest: `--log-level`, the `HAMUR_LOG_LEVEL` variable, the `logLevel` key, then `info`. The `error: <message>` line of a failed command is printed at every level. A debug setting for your machine goes in `~/.hamurrc` ([CLI](/cli#the-rc-file)): `debug=true`.

An invalid value is `debug must be a boolean or { hooks?, templates?, modules? } (got "yes")`, and a name outside the three is `debug.<name> is not an option`.

## Other tools

- `hamur routes` prints the table the router matches, with the file of each route.
- `.hamur/routes.json` holds the same routes as a file, for an editor or a script that does not start Hamur: `{ "version": 1, "pages": [{ name, path, file, loader?, layout?, middleware? }], "server": [{ path, method, file }] }`, sorted by path, files relative to the project root with `/`. `hamur typegen`, `hamur build` and `hamur dev` write it, and only when its bytes change. An i18n copy of a page is its own entry, named `<name>___<locale>`. Within `version: 1` fields are only added.
- `hamur inspect <file>` prints a module's text after the build's transform chain ([Devtools](/devtools#inspecting-a-module)).
- `hamur dev` prints the browser's `console` calls, each server `fetch` and the hydration mismatches in the terminal ([Dev server](/dev-server)).
- `hamur generate --debug-prerender` prerenders one page at a time with source maps and stops at the first failing page with its request path and mapped stack.
- `hamur info --markdown` prints the versions and paths of your setup in a block that goes into an issue.
