Debian Codegraph @

CLAUDE.md

raw symbols SCIP
1 # CLAUDE.md
2
3 This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
5 ## Project Overview
6
7 Official Go client library for the NATS messaging system. Provides core pub/sub, request/reply, JetStream (streams, consumers, KV, object store), and a micro services framework. Module path: `github.com/nats-io/nats.go`.
8
9 ## Build and Test Commands
10
11 This project uses a **dual module** setup: `go.mod` for production (minimal deps) and `go_test.mod` for testing (includes nats-server, protobuf). Always use `-modfile=go_test.mod` when running tests or any command that needs test dependencies.
12
13 ```bash
14 # Build
15 go build ./...
16
17 # Run all tests (race detector + internal_testing tag, sequential)
18 go test -modfile=go_test.mod -race -v -p=1 ./... --failfast -vet=off -tags=internal_testing
19
20 # Run NoRace tests (must be run separately, without -race flag)
21 go test -modfile=go_test.mod -v -run=TestNoRace -p=1 ./... --failfast -vet=off
22
23 # Run a specific test
24 go test -modfile=go_test.mod -race -run TestName ./... -tags=internal_testing
25
26 # Run tests for a specific package
27 go test -modfile=go_test.mod -race ./jetstream/... --failfast
28 go test -modfile=go_test.mod -race ./micro/... --failfast
29
30 # Coverage
31 ./scripts/cov.sh
32
33 # Formatting
34 go fmt ./...
35
36 # Vet
37 go vet -modfile=go_test.mod ./...
38
39 # Static analysis (as CI does it)
40 staticcheck -modfile=go_test.mod ./...
41
42 # Linting (golangci-lint runs only on jetstream/)
43 golangci-lint run --timeout 5m0s ./jetstream/...
44
45 # Spell check
46 find . -type f -name "*.go" | xargs misspell -error -locale US
47
48 # Update test dependencies (never change go.mod for test deps)
49 go mod tidy -modfile=go_test.mod
50 ```
51
52 ## Important Build Tags
53
54 - **`internal_testing`** -- Exposes internal test helpers (e.g., `AddMsgFilter`, `CloseTCPConn`) from `testing_internal.go`. Required for many tests in `./test/`.
55 - **`skip_no_race_tests`** -- Skips the NoRace tests. Used by coverage scripts.
56 - **`!race && !skip_no_race_tests`** -- NoRace tests in `test/norace_test.go` only run when the race detector is OFF.
57 - **`compat`** -- Compatibility tests in `test/compat_test.go`.
58
59 ## CI Pipeline (ci.yaml)
60
61 1. **lint** -- `go fmt`, `go vet`, `staticcheck`, `misspell` (all packages), `golangci-lint` (jetstream only).
62 2. **test** -- Matrix of Go 1.24 and 1.25. Runs NoRace tests first (`-run=TestNoRace` without `-race`), then full race-enabled tests with `-tags=internal_testing`.
63
64 ## Project Structure
65
66 ```
67 nats.go # Core connection, pub/sub, request/reply (~6500 lines)
68 parser.go # Client-side protocol parser
69 ws.go # WebSocket transport support
70 js.go # Legacy JetStream API (deprecated, see jetstream/)
71 jsm.go # Legacy JetStream management
72 kv.go # Legacy KeyValue API
73 object.go # Legacy Object Store API
74 enc.go # EncodedConn (deprecated)
75 netchan.go # Go channel bindings
76 timer.go # Internal timer utilities
77 context.go # Context-aware request methods
78 nats_iter.go # Go 1.23+ iterator support (go:build go1.23)
79 testing_internal.go # Internal test hooks (go:build internal_testing)
80
81 jetstream/ # New JetStream API (preferred over legacy)
82 jetstream.go # Top-level JetStream interface
83 stream.go # Stream management
84 stream_config.go # Stream configuration types
85 consumer.go # Consumer management
86 consumer_config.go # Consumer configuration types
87 pull.go # Pull consumer implementation
88 push.go # Push consumer (deprecated)
89 ordered.go # Ordered consumer
90 publish.go # JetStream publish methods
91 kv.go # KeyValue store
92 object.go # Object store
93 message.go # JetStream message types
94 errors.go # JetStream error types
95 test/ # Integration tests (package test, uses nats-server)
96
97 micro/ # Micro services framework
98 service.go # Service interface and implementation
99 request.go # Request handling
100 test/ # Integration tests
101
102 internal/
103 parser/ # NATS protocol parser (used by core client)
104 syncx/ # Concurrent map utility
105
106 encoders/
107 builtin/ # Default encoders (JSON, GOB, string)
108 protobuf/ # Protocol Buffers encoder
109
110 test/ # Integration tests for core package (package test)
111 helper_test.go # Server setup helpers (RunDefaultServer, RunBasicJetStreamServer, etc.)
112 norace_test.go # Tests that cannot run with -race (build tag guarded)
113 js_internal_test.go # Tests requiring internal_testing tag
114 configs/ # NATS server config files for tests
115
116 bench/ # Benchmarking utilities
117 examples/ # Example command-line tools (nats-pub, nats-sub, etc.)
118 scripts/cov.sh # Coverage collection script
119 ```
120
121 ## Test Architecture
122
123 - **Root `nats_test.go`** (package `nats`) -- White-box unit tests with access to unexported internals.
124 - **`test/`** (package `test`) -- Black-box integration tests. Tests start an embedded nats-server using helpers from `test/helper_test.go`. These require `-modfile=go_test.mod` since nats-server is a test-only dependency.
125 - **`jetstream/test/`** (package `test`) -- Integration tests for the new JetStream API, also use embedded nats-server.
126 - **`micro/test/`** (package `test`) -- Integration tests for the micro services framework.
127 - **NoRace tests** -- Prefixed `TestNoRace*`, guarded by `//go:build !race && !skip_no_race_tests`. Must be run separately without `-race`.
128 - Tests always run with `-p=1` (no parallel packages) because they start embedded servers on shared ports.
129
130 ## Code Conventions
131
132 - **License header** -- Every `.go` file starts with the Apache 2.0 license header (Copyright year range).
133 - **Error variables** -- Exported errors defined as `var Err... = errors.New("nats: ...")` in `nats.go`. JetStream errors in `jetstream/errors.go` follow the same pattern.
134 - **Options pattern** -- Connection options use functional options: `nats.Connect(url, nats.Name("myapp"), nats.MaxReconnects(5))`. JetStream and micro use similar patterns.
135 - **No external dependencies in production** -- Only `klauspost/compress`, `nkeys`, `nuid` in `go.mod`. Test deps (nats-server, protobuf) are isolated in `go_test.mod`. PRs adding dependencies are scrutinized heavily.
136 - **Commits require sign-off** -- Use `git commit -s` (DCO: `Signed-off-by`).
137 - **US English spelling** -- Enforced by `misspell -locale US` in CI.
138 - **Interface-driven design** -- JetStream and micro packages define interfaces (`JetStream`, `Stream`, `Consumer`, `Service`) with concrete unexported implementations.
139
140 ## Key Types
141
142 - `nats.Conn` -- Core connection, handles all NATS protocol operations.
143 - `nats.Msg` -- Message type for pub/sub and request/reply.
144 - `nats.Subscription` -- Represents a subscription (sync, async, or channel-based).
145 - `jetstream.JetStream` -- Entry point for new JetStream API (created via `jetstream.New(nc)`).
146 - `jetstream.Stream`, `jetstream.Consumer` -- Stream and consumer management.
147 - `micro.Service` -- Micro service instance (created via `micro.AddService(nc, config)`).