## Merge order Merge first; then republish the docs; then coder/coder.com#1100 (the redirects). ## Summary The nine pages under `docs/about/contributing/` are for people working in this repository, not for Coder users or administrators, so they leave the published documentation. The repository becomes their canonical home. Until now the root `CONTRIBUTING.md` and `CODE_OF_CONDUCT.md` were two-line stubs that only linked to the docs site, so contributors on GitHub were bounced out to read them. A companion PR in `coder/coder.com` adds redirects from every retired `/docs/about/contributing/*` URL to the new GitHub location. It must merge only after this PR has merged and the docs have republished. ## Changes | Old path | New path | | -- | -- | | `docs/about/contributing/CONTRIBUTING.md` | `CONTRIBUTING.md` (replaces the stub) | | `docs/about/contributing/CODE_OF_CONDUCT.md` | `CODE_OF_CONDUCT.md` (replaces the stub) | | `docs/about/contributing/SECURITY.md` | deleted; the root `SECURITY.md` already holds the policy | | `docs/about/contributing/AI_CONTRIBUTING.md` | `contributing/AI_CONTRIBUTING.md` | | `docs/about/contributing/backend.md` | `contributing/backend.md` | | `docs/about/contributing/frontend.md` | `contributing/frontend.md` | | `docs/about/contributing/modules.md` | `contributing/modules.md` | | `docs/about/contributing/templates.md` | `contributing/templates.md` | | `docs/about/contributing/documentation.md` | `contributing/documentation.md` (see note below) | For every moved file: the docs front matter is stripped and its `title` becomes the H1, relative links are rewritten for the new depth (`../../../foo` becomes `./foo` at the root and `../foo` under `contributing/`; the two links into `docs/admin/` become `../docs/admin/...`), and the docs-only `<div class="tabs">` wrapper in `CONTRIBUTING.md` is removed. `CONTRIBUTING.md` gains a short intro that links the code of conduct, the security policy and the area guides, since GitHub only auto-surfaces the root files. `docs/manifest.json` loses the About → Contributing group and `docs/images/icons/contributing.svg`, which only that group used. Inbound references updated: - `README.md`: contribution guide link now points at `CONTRIBUTING.md` on GitHub. - `docs/tutorials/example-guide.md` and `docs/install/releases/feature-stages.md`: link to `contributing/documentation.md` on GitHub, since a docs page cannot link to a repo-only file. - `docs/.style/style-guide/README.md`: the "Relationship to" section names the new location. - `scripts/docshtmlcheck/main.go`: both the package comment and the user-facing error string name `contributing/documentation.md`. - `scripts/docshtmlcheck/README.md`: relative link to the new location. - `coderd/database/migrations/migrate_test.go`: `t.Log` link now points at `contributing/backend.md#database-fixtures-for-testing-migrations`. - `.github/pull_request_template.md`: AI contribution guidelines link. - `.github/workflows/contrib.yaml`: commit-message guidelines link in the PR title check. - `.github/workflows/test-docs-preview-mapper.sh`: the mapper test case that used `docs/about/contributing/CONTRIBUTING.md` now uses `docs/admin/security/audit-logs.md`. - `.claude/docs/{ARCHITECTURE,PR_STYLE_GUIDE,WORKFLOWS,DOCS_STYLE_GUIDE}.md`: links to `CONTRIBUTING.md`. `git grep about/contributing` is empty after this change. `CODEOWNERS` had no entry for the old paths. Note on `documentation.md`: it is docs-authoring guidance and is parked in `contributing/` for now. Folding it into `docs/.style/` is tracked separately and is deliberately not attempted here. ## Testing - `go run ./scripts/docshtmlcheck` over `docs/`, and again over `contributing/`, `CONTRIBUTING.md` and `CODE_OF_CONDUCT.md`: clean. - `go vet ./scripts/docshtmlcheck/...` and `go test ./scripts/docshtmlcheck/...`: 47 tests pass. - `go vet ./coderd/database/migrations/...`: clean. `go test` for that package fails to link on this machine (`tailscale.com/tstime/mono: invalid reference to runtime.nanotime1`) identically on unmodified `main`, so it is a local toolchain issue and not exercised here; the change is a string in a `t.Log`. - `pnpm check-docs` (markdownlint-cli2 and markdown-table-formatter over `docs/`, `.claude/docs/`, root `*.md`): 495 files, 0 errors. The same two tools over `contributing/*.md`: 0 errors. - Every relative link in the eight moved files resolves to an existing path (scripted check). - `bash .github/workflows/test-docs-preview-mapper.sh`: the changed `assert_maps_to` case passes; the six `resolve_ref`/`pages_for_image` failures it reports are identical on unmodified `main` on macOS. Linear: [DOCS-975](https://linear.app/codercom/issue/DOCS-975) --- 🤖 Built with AI assistance.
220 lines
14 KiB
Markdown
220 lines
14 KiB
Markdown
# Backend
|
||
|
||
This guide is designed to support both Coder engineers and community contributors in understanding our backend systems and getting started with development.
|
||
|
||
Coder’s backend powers the core infrastructure behind workspace provisioning, access control, and the overall developer experience. As the backbone of our platform, it plays a critical role in enabling reliable and scalable remote development environments.
|
||
|
||
The purpose of this guide is to help you:
|
||
|
||
* Understand how the various backend components fit together.
|
||
* Navigate the codebase with confidence and adhere to established best practices.
|
||
* Contribute meaningful changes - whether you're fixing bugs, implementing features, or reviewing code.
|
||
|
||
Need help or have questions? Join the conversation on our [Discord server](https://discord.com/invite/coder) — we’re always happy to support contributors.
|
||
|
||
## Platform Architecture
|
||
|
||
To understand how the backend fits into the broader system, we recommend reviewing the following resources:
|
||
|
||
* [General Concepts](../docs/install/plan/sizing/index.md#general-concepts): Essential concepts and language used to describe how Coder is structured and operated.
|
||
|
||
* [Architecture](../docs/install/plan/architecture.md): A high-level overview of the infrastructure layout, key services, and how components interact.
|
||
|
||
These sections provide the necessary context for navigating and contributing to the backend effectively.
|
||
|
||
## Tech Stack
|
||
|
||
Coder's backend is built using a collection of robust, modern Go libraries and internal packages. Familiarity with these technologies will help you navigate the codebase and contribute effectively.
|
||
|
||
### Core Libraries & Frameworks
|
||
|
||
* [go-chi/chi](https://github.com/go-chi/chi): lightweight HTTP router for building RESTful APIs in Go
|
||
* [golang-migrate/migrate](https://github.com/golang-migrate/migrate): manages database schema migrations across environments
|
||
* [coder/terraform-config-inspect](https://github.com/coder/terraform-config-inspect) *(forked)*: used for parsing and analyzing Terraform configurations, forked to include [PR #74](https://github.com/hashicorp/terraform-config-inspect/pull/74)
|
||
* [coder/pq](https://github.com/coder/pq) *(forked)*: PostgreSQL driver forked to support rotating authentication tokens via `driver.Connector`
|
||
* [coder/tailscale](https://github.com/coder/tailscale) *(forked)*: enables secure, peer-to-peer connectivity, forked to apply internal patches pending upstreaming
|
||
* [coder/wireguard-go](https://github.com/coder/wireguard-go) *(forked)*: WireGuard networking implementation, forked to fix a data race and adopt the latest gVisor changes
|
||
* [coder/ssh](https://github.com/coder/ssh) *(forked)*: customized SSH server based on `gliderlabs/ssh`, forked to include Tailscale-specific patches and avoid complex subpath dependencies
|
||
* [coder/bubbletea](https://github.com/coder/bubbletea) *(forked)*: terminal UI framework for CLI apps, forked to remove an `init()` function that interfered with web terminal output
|
||
|
||
### Coder libraries
|
||
|
||
* [coder/terraform-provider-coder](https://github.com/coder/terraform-provider-coder): official Terraform provider for managing Coder resources via infrastructure-as-code
|
||
* [coder/websocket](https://github.com/coder/websocket): minimal WebSocket library for real-time communication
|
||
* [coder/serpent](https://github.com/coder/serpent): CLI framework built on `cobra`, used for large, complex CLIs
|
||
* [coder/guts](https://github.com/coder/guts): generates TypeScript types from Go for shared type definitions
|
||
* [coder/wgtunnel](https://github.com/coder/wgtunnel): WireGuard tunnel server for secure backend networking
|
||
|
||
## Repository Structure
|
||
|
||
The Coder backend is organized into multiple packages and directories, each with a specific purpose. Here's a high-level overview of the most important ones:
|
||
|
||
* [agent](../agent): core logic of a workspace agent, supports DevContainers, remote SSH, startup/shutdown script execution. Protobuf definitions for DRPC communication with `coderd` are kept in [proto](../agent/proto).
|
||
* [cli](../cli): CLI interface for `coder` command built on [coder/serpent](https://github.com/coder/serpent). Input controls are defined in [cliui](../cli/cliui), and [testdata](../cli/testdata) contains golden files for common CLI calls
|
||
* [cmd](../cmd): entry points for CLI and services, including `coderd`
|
||
* [coderd](../coderd): the main API server implementation with [chi](https://github.com/go-chi/chi) endpoints
|
||
* [audit](../coderd/audit): audit log logic, defines target resources, actions and extra fields
|
||
* [autobuild](../coderd/autobuild): core logic of the workspace autobuild executor, periodically evaluates workspaces for next transition actions
|
||
* [httpmw](../coderd/httpmw): HTTP middlewares mainly used to extract parameters from HTTP requests (e.g. current user, template, workspace, OAuth2 account, etc.) and storing them in the request context
|
||
* [prebuilds](../coderd/prebuilds): common interfaces for prebuild workspaces, feature implementation is in [enterprise/prebuilds](../enterprise/coderd/prebuilds)
|
||
* [provisionerdserver](../coderd/provisionerdserver): DRPC server for [provisionerd](../provisionerd) instances, used to validate and extract Terraform data and resources, and store them in the database.
|
||
* [rbac](../coderd/rbac): RBAC engine for `coderd`, including authz layer, role definitions and custom roles. Built on top of [Open Policy Agent](https://github.com/open-policy-agent/opa) and Rego policies.
|
||
* [telemetry](../coderd/telemetry): records a snapshot with various workspace data for telemetry purposes. Once recorded the reporter sends it to the configured telemetry endpoint.
|
||
* [tracing](../coderd/tracing): extends telemetry with tracing data consistent with [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md)
|
||
* [workspaceapps](../coderd/workspaceapps): core logic of a secure proxy to expose workspace apps deployed in a workspace
|
||
* [wsbuilder](../coderd/wsbuilder): wrapper for business logic of creating a workspace build. It encapsulates all database operations required to insert a build record in a transaction.
|
||
* [database](../coderd/database): schema migrations, query logic, in-memory database, etc.
|
||
* [db2sdk](../coderd/database/db2sdk): translation between database structures and [codersdk](../codersdk) objects used by coderd API.
|
||
* [dbauthz](../coderd/database/dbauthz): AuthZ wrappers for database queries, ideally, every query should verify first if the accessor is eligible to see the query results.
|
||
* [dbfake](../coderd/database/dbfake): helper functions to quickly prepare the initial database state for testing purposes (e.g. create N healthy workspaces and templates), operates on higher level than [dbgen](../coderd/database/dbgen)
|
||
* [dbgen](../coderd/database/dbgen): helper functions to insert raw records to the database store, used for testing purposes
|
||
* [dbmock](../coderd/database/dbmock): a store wrapper for database queries, useful to verify if the function has been called, used for testing purposes
|
||
* [dbpurge](../coderd/database/dbpurge): simple wrapper for periodic database cleanup operations
|
||
* [migrations](../coderd/database/migrations): an ordered list of up/down database migrations, use `./create_migration.sh my_migration_name` to modify the database schema
|
||
* [pubsub](../coderd/database/pubsub): PubSub implementation using PostgreSQL and in-memory drop-in replacement
|
||
* [queries](../coderd/database/queries): contains SQL files with queries, `sqlc` compiles them to [Go functions](../coderd/database/queries.sql.go)
|
||
* [sqlc.yaml](../coderd/database/sqlc.yaml): defines mappings between SQL types and custom Go structures
|
||
* [codersdk](../codersdk): user-facing API entities used by CLI and site to communicate with `coderd` endpoints
|
||
* [dogfood](../dogfood): build inputs for the images used by Coder's dogfood workspaces. The workspace templates that consume these images live in an internal Coder infrastructure repository.
|
||
* [enterprise](../enterprise): enterprise-only features, notice similar file structure to repository root (`audit`, `cli`, `cmd`, `coderd`, etc.)
|
||
* [coderd](../enterprise/coderd)
|
||
* [prebuilds](../enterprise/coderd/prebuilds): core logic of prebuilt workspaces - reconciliation loop
|
||
* [provisioner](../provisioner): supported implementation of provisioners, Terraform and "echo" (for testing purposes)
|
||
* [provisionerd](../provisionerd): core logic of provisioner runner to interact provisionerd server, depending on a job acquired it calls template import, dry run or a workspace build
|
||
* [pty](../pty): terminal emulation for agent shell
|
||
* [support](../support): compile a support bundle with diagnostics
|
||
* [tailnet](../tailnet): core logic of Tailnet controller to maintain DERP maps, coordinate connections with agents and peers
|
||
* [vpn](../vpn): Coder Desktop (VPN) and tunneling components
|
||
|
||
## Testing
|
||
|
||
The Coder backend includes a rich suite of unit and end-to-end tests. A variety of helper utilities are used throughout the codebase to make testing easier, more consistent, and closer to real behavior.
|
||
|
||
### [clitest](../cli/clitest)
|
||
|
||
* Spawns an in-memory `serpent.Command` instance for unit testing
|
||
* Configures an authorized `codersdk` client
|
||
* Once a `serpent.Invocation` is created, tests can execute commands as if invoked by a real user
|
||
|
||
### [ptytest](../pty/ptytest)
|
||
|
||
* `ptytest` attaches to a `serpent.Invocation` and simulates TTY input/output
|
||
* `pty` provides matchers and "write" operations for interacting with pseudo-terminals
|
||
|
||
### [coderdtest](../coderd/coderdtest)
|
||
|
||
* Provides shortcuts to spin up an in-memory `coderd` instance
|
||
* Can start an embedded provisioner daemon
|
||
* Supports multi-user testing via `CreateFirstUser` and `CreateAnotherUser`
|
||
* Includes "busy wait" helpers like `AwaitTemplateVersionJobCompleted`
|
||
* [oidctest](../coderd/coderdtest/oidctest) can start a fake OIDC provider
|
||
|
||
### [testutil](../testutil)
|
||
|
||
* General-purpose testing utilities, including:
|
||
* [chan.go](../testutil/chan.go): helpers for sending/receiving objects from channels (`TrySend`, `RequireReceive`, etc.)
|
||
* [duration.go](../testutil/duration.go): set timeouts for test execution
|
||
* [eventually.go](../testutil/eventually.go): repeatedly poll for a condition using a ticker
|
||
* [port.go](../testutil/port.go): select a free random port
|
||
* [prometheus.go](../testutil/prometheus.go): validate Prometheus metrics with expected values
|
||
* [pty.go](../testutil/pty.go): read output from a terminal until a condition is met
|
||
* [wait_buffer.go](../testutil/wait_buffer.go): thread-safe `io.Writer` that blocks until accumulated output contains a signal (`WaitFor`, `WaitForNth`, `WaitForCond`)
|
||
|
||
### [dbtestutil](../coderd/database/dbtestutil)
|
||
|
||
* Allows choosing between real and in-memory database backends for tests
|
||
* `WillUsePostgres` is useful for skipping tests in CI environments that don't run Postgres
|
||
|
||
### [quartz](https://github.com/coder/quartz/tree/main)
|
||
|
||
* Provides a mockable clock or ticker interface
|
||
* Allows manual time advancement
|
||
* Useful for testing time-sensitive or timeout-related logic
|
||
|
||
## Quiz
|
||
|
||
Try to find answers to these questions before jumping into implementation work — having a solid understanding of how Coder works will save you time and help you contribute effectively.
|
||
|
||
1. When you create a template, what does that do exactly?
|
||
2. When you create a workspace, what exactly happens?
|
||
3. How does the agent get the required information to run?
|
||
4. How are provisioner jobs run?
|
||
|
||
## Recipes
|
||
|
||
### Adding database migrations and fixtures
|
||
|
||
#### Database migrations
|
||
|
||
Database migrations are managed with
|
||
[`migrate`](https://github.com/golang-migrate/migrate).
|
||
|
||
To add new migrations, use the following command:
|
||
|
||
```sh
|
||
./coderd/database/migrations/create_migration.sh my name
|
||
/home/coder/src/coder/coderd/database/migrations/000070_my_name.up.sql
|
||
/home/coder/src/coder/coderd/database/migrations/000070_my_name.down.sql
|
||
```
|
||
|
||
Then write queries into the generated `.up.sql` and `.down.sql` files and commit
|
||
them into the repository. The down script should make a best-effort to retain as
|
||
much data as possible.
|
||
|
||
Run `make gen` to generate models.
|
||
|
||
#### Database fixtures (for testing migrations)
|
||
|
||
There are two types of fixtures that are used to test that migrations don't
|
||
break existing Coder deployments:
|
||
|
||
* Partial fixtures
|
||
[`migrations/testdata/fixtures`](../coderd/database/migrations/testdata/fixtures)
|
||
* Full database dumps
|
||
[`migrations/testdata/full_dumps`](../coderd/database/migrations/testdata/full_dumps)
|
||
|
||
Both types behave like database migrations (they also
|
||
[`migrate`](https://github.com/golang-migrate/migrate)). Their behavior mirrors
|
||
Coder migrations such that when migration number `000022` is applied, fixture
|
||
`000022` is applied afterwards.
|
||
|
||
Partial fixtures are used to conveniently add data to newly created tables so
|
||
that we can ensure that this data is migrated without issue.
|
||
|
||
Full database dumps are for testing the migration of fully-fledged Coder
|
||
deployments. These are usually done for a specific version of Coder and are
|
||
often fixed in time. A full database dump may be necessary when testing the
|
||
migration of multiple features or complex configurations.
|
||
|
||
To add a new partial fixture, run the following command:
|
||
|
||
```sh
|
||
./coderd/database/migrations/create_fixture.sh my fixture
|
||
/home/coder/src/coder/coderd/database/migrations/testdata/fixtures/000070_my_fixture.up.sql
|
||
```
|
||
|
||
Then add some queries to insert data and commit the file to the repo. See
|
||
[`000024_example.up.sql`](../coderd/database/migrations/testdata/fixtures/000024_example.up.sql)
|
||
for an example.
|
||
|
||
To create a full dump, run a fully fledged Coder deployment and use it to
|
||
generate data in the database. Then shut down the deployment and take a snapshot
|
||
of the database.
|
||
|
||
```sh
|
||
mkdir -p coderd/database/migrations/testdata/full_dumps/v0.12.2 && cd $_
|
||
pg_dump "postgres://coder@localhost:..." -a --inserts >000069_dump_v0.12.2.up.sql
|
||
```
|
||
|
||
Make sure sensitive data in the dump is desensitized, for instance names,
|
||
emails, OAuth tokens and other secrets. Then commit the dump to the project.
|
||
|
||
To find out what the latest migration for a version of Coder is, use the
|
||
following command:
|
||
|
||
```sh
|
||
git ls-files v0.12.2 -- coderd/database/migrations/*.up.sql
|
||
```
|
||
|
||
This helps in naming the dump (e.g. `000069` above).
|