|
| 1 | +# CODEX.md |
| 2 | + |
| 3 | +This document provides detailed guidance for OpenAI Codex and similar code agents working in the ReScript compiler repository. It consolidates setup, build, testing, repository layout, workflows, and conventions into one place. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +ReScript is a robustly typed language that compiles to efficient and human-readable JavaScript. This monorepo contains the compiler (OCaml), build system, runtime (ReScript), analysis tools, and supporting tooling. |
| 8 | + |
| 9 | +## Prerequisites and Environment |
| 10 | + |
| 11 | +- Node.js: use `v22.x` locally (`.nvmrc` is `22`), but min `>=20.11.0` is enforced for tooling |
| 12 | +- Yarn: v4 (Corepack). The repo uses Yarn workspaces |
| 13 | +- OPAM: `v2.2.0+` recommended; `opam switch create 5.3.0` |
| 14 | +- OCaml: `5.3.0` (for development switch); dune `>=3.17` |
| 15 | +- Python: `<=3.11` (ninja build) |
| 16 | +- Rust toolchain (for `rewatch`) |
| 17 | +- C toolchain (Xcode CLT on macOS) |
| 18 | + |
| 19 | +Devcontainer alternative: |
| 20 | +- See `.devcontainer/` for a ready-to-use VSCode environment (installs gh, Node 20, OPAM 2.4.1, sets up switch, installs deps, enables corepack, runs yarn) |
| 21 | + |
| 22 | +Recommended VS Code extensions: |
| 23 | +- `ocamllabs.ocaml-platform`, `chenglou92.rescript-vscode`, `biomejs.biome`, `rust-lang.rust-analyzer` |
| 24 | + |
| 25 | +## Initial Setup |
| 26 | + |
| 27 | +```bash |
| 28 | +# 1) OPAM and OCaml switch |
| 29 | +opam init |
| 30 | +opam switch create 5.3.0 |
| 31 | +opam install . --deps-only --with-test --with-dev-setup -y |
| 32 | + |
| 33 | +# 2) Node/Yarn deps |
| 34 | +corepack enable |
| 35 | +yarn install |
| 36 | + |
| 37 | +# 3) Build (compiler + copy exes) |
| 38 | +make |
| 39 | +``` |
| 40 | + |
| 41 | +Tip: Use the devcontainer (`Dev Containers: Rebuild and Reopen in Container`) if local setup is troublesome. |
| 42 | + |
| 43 | +## Build and Watch |
| 44 | + |
| 45 | +Using dune directly: |
| 46 | +```bash |
| 47 | +dune build # one-off |
| 48 | +dune build -w # watch |
| 49 | +``` |
| 50 | + |
| 51 | +Makefile targets (preferred wrappers): |
| 52 | +```bash |
| 53 | +make # dune build + copy executables |
| 54 | +make watch # dune build -w |
| 55 | +make ninja # build vendored ninja + copy exe |
| 56 | +make rewatch # cargo build rewatch + copy exe |
| 57 | +make lib # build @rescript/runtime |
| 58 | +make artifacts # populate lib/ocaml and update packages/artifacts.json |
| 59 | +``` |
| 60 | + |
| 61 | +Executables are copied into the platform dir, e.g. `darwinarm64/`. |
| 62 | + |
| 63 | +## Testing |
| 64 | + |
| 65 | +High-level entry: |
| 66 | +```bash |
| 67 | +make test # build + run all core test suites |
| 68 | +make test-all # test + gentype + analysis + tools + rewatch |
| 69 | +``` |
| 70 | + |
| 71 | +Focused suites: |
| 72 | +```bash |
| 73 | +make test-syntax # syntax parser tests |
| 74 | +make test-syntax-roundtrip # adds roundtrip tests (ROUNDTRIP_TEST=1) |
| 75 | +make test-gentype # tests/gentype_tests/typescript-react-example |
| 76 | +make test-analysis # analysis tests (language server/tooling) |
| 77 | +make test-tools # tools tests |
| 78 | +make test-rewatch # rewatch suite |
| 79 | +``` |
| 80 | + |
| 81 | +Single-file compiler debug: |
| 82 | +```bash |
| 83 | +./cli/bsc.js myFile.res |
| 84 | +./cli/bsc.js -dparsetree myFile.res |
| 85 | +./cli/bsc.js -dtypedtree myFile.res |
| 86 | +``` |
| 87 | + |
| 88 | +Token/untyped tree via parser: |
| 89 | +```bash |
| 90 | +dune exec res_parser -- -print tokens myFile.res |
| 91 | +dune exec res_parser -- -print ast -recover myFile.res |
| 92 | +``` |
| 93 | + |
| 94 | +Use local compiler in a project without linking: |
| 95 | +```bash |
| 96 | +RESCRIPT_BSC_EXE=/path/to/packages/@rescript/darwin-arm64/bin/bsc.exe npx rescript |
| 97 | +``` |
| 98 | + |
| 99 | +## Code Quality and Linting |
| 100 | + |
| 101 | +```bash |
| 102 | +make format # repository formatting (OCaml + others) |
| 103 | +make checkformat # formatting check |
| 104 | +npm run check # Biome lint (changed files) |
| 105 | +npm run check:all # Biome lint (entire repo) |
| 106 | +npm run typecheck # TypeScript type-check (TS tooling only) |
| 107 | +``` |
| 108 | + |
| 109 | +## Repository Layout (selected) |
| 110 | + |
| 111 | +- `compiler/syntax/`: ReScript syntax parser (MIT licensed) |
| 112 | +- `compiler/frontend/`: AST transformations, externals, attributes |
| 113 | +- `compiler/ml/`: OCaml typechecker infra (typedtree, etc.) |
| 114 | +- `compiler/core/`: Lambda and JS IR passes (`lam_*`, `js_*`, `js_dump*`) |
| 115 | +- `compiler/common/`, `compiler/ext/`, `compiler/depends/`: shared utilities |
| 116 | +- `compiler/bsc/`, `compiler/bsb_exe/`: binary entry points |
| 117 | +- `analysis/`: language server and tooling support |
| 118 | +- `runtime/`: ReScript standard library sources |
| 119 | +- `lib/`: compiled JS output of the stdlib |
| 120 | +- `packages/`: Yarn workspaces for platform bins and playground |
| 121 | +- `rewatch/`: Rust file watcher |
| 122 | +- `ninja/`: vendored build tool |
| 123 | +- `tests/`: unit, integration, syntax, analysis, tools, gentype, etc. |
| 124 | + |
| 125 | +Important test locations: |
| 126 | +- `tests/tests/`: mocha unit tests (runtime) |
| 127 | +- `tests/build_tests/`: integration tests (build system) |
| 128 | +- `tests/ounit_tests/`: compiler unit tests (OCaml) |
| 129 | +- `tests/syntax_tests/`: syntax and roundtrip tests |
| 130 | +- `tests/analysis_tests/`: language server/tooling tests |
| 131 | +- `tests/tools_tests/`: tooling tests |
| 132 | + |
| 133 | +## Yarn Workspaces and Version Constraints |
| 134 | + |
| 135 | +- Workspaces: see `package.json` and `packages/` |
| 136 | +- Version metadata is enforced by `yarn.config.cjs` (EXPECTED_VERSION) |
| 137 | + - To auto-fix metadata across workspaces and compiler sources: |
| 138 | + ```bash |
| 139 | + yarn constraints --fix |
| 140 | + ``` |
| 141 | + |
| 142 | +## Playground Bundle (JSOO) |
| 143 | + |
| 144 | +```bash |
| 145 | +make playground # builds JS bundle (browser profile) |
| 146 | +make playground-cmijs # builds and bundles cmij dependencies |
| 147 | +yarn workspace playground test |
| 148 | +``` |
| 149 | + |
| 150 | +Bundle artifacts: |
| 151 | +- `playground/compiler.js` (compiler API for browser/Node) |
| 152 | +- `playground/packages/compiler-builtins/` (core cmij) |
| 153 | +- `playground/packages/*` (3rd-party cmij) |
| 154 | + |
| 155 | +Release to Cloudflare R2 (requires configured rclone remote): |
| 156 | +```bash |
| 157 | +make playground-release |
| 158 | +``` |
| 159 | + |
| 160 | +## Branching, PRs, and DCO |
| 161 | + |
| 162 | +- Target `master` for new features (v12 dev) |
| 163 | +- Target `11.0_release` for fixes/maintenance; it is periodically merged into `master` |
| 164 | +- DCO is required for every commit. Use: |
| 165 | + ```bash |
| 166 | + git commit -s -m "your message" # adds Signed-off-by with your git config |
| 167 | + ``` |
| 168 | + |
| 169 | +## Release Process (summary) |
| 170 | + |
| 171 | +- Ensure version numbers are correct across workspaces |
| 172 | +- Update `CHANGELOG.md` via PR |
| 173 | +- Tag release to trigger publishing (playground bundle + npm `rescript` with tag `ci`) |
| 174 | +- Verify, then set npm dist-tag (`latest` or `next`) |
| 175 | +- Prepare next-dev version: update expected version in `yarn.config.cjs`, run `yarn constraints --fix`, update `CHANGELOG.md` |
| 176 | + |
| 177 | +## Performance and Output Quality |
| 178 | + |
| 179 | +- Avoid introducing meaningless symbols |
| 180 | +- Keep generated JS readable; prefer simple, maintainable transformations |
| 181 | +- Consider compilation speed impact of changes |
| 182 | +- Use appropriate optimization passes in Lambda and JS IR where applicable |
| 183 | + |
| 184 | +## Common Tasks Cheat Sheet |
| 185 | + |
| 186 | +```bash |
| 187 | +# Build everything and copy exes |
| 188 | +make |
| 189 | +
|
| 190 | +# Build stdlib |
| 191 | +make lib |
| 192 | +
|
| 193 | +# All tests |
| 194 | +make test |
| 195 | +
|
| 196 | +# Syntax + roundtrip |
| 197 | +make test-syntax-roundtrip |
| 198 | +
|
| 199 | +# Update artifacts list (affects npm packages) |
| 200 | +make artifacts |
| 201 | +
|
| 202 | +# Lint |
| 203 | +npm run check |
| 204 | +
|
| 205 | +# Format |
| 206 | +make format |
| 207 | +``` |
| 208 | + |
| 209 | +## Troubleshooting |
| 210 | + |
| 211 | +- OPAM: If `5.3.0` switch is missing, run `opam update && opam upgrade` and retry `opam switch create 5.3.0` |
| 212 | +- Python: Ensure `python --version` is `<=3.11` if ninja build fails |
| 213 | +- Yarn: Use `corepack enable` and Yarn v4; `yarn -v` should reflect workspace configuration |
| 214 | +- Rewatch build: Ensure Rust toolchain installed and up-to-date |
| 215 | +- Local compiler in projects: prefer `RESCRIPT_BSC_EXE=... npx rescript` for quick tests |
| 216 | + |
| 217 | +## Key Files |
| 218 | + |
| 219 | +- `Makefile`: build/test wrappers and helper targets |
| 220 | +- `dune-project` / `rescript.opam`: dune/opam metadata and constraints |
| 221 | +- `yarn.config.cjs`: enforces version metadata across workspaces and sources |
| 222 | +- `.devcontainer/*`: containerized dev environment |
| 223 | +- `cli/*`: Node entry points for compiler/build tooling |
| 224 | + |
| 225 | +## Notes for Code Agents |
| 226 | + |
| 227 | +- Respect code styles: OCaml snake_case; ReScript camelCase |
| 228 | +- Keep edits focused; avoid large refactors unless necessary |
| 229 | +- Add tests for new behavior; update snapshots where applicable |
| 230 | +- Use DCO sign-off for all commits |
| 231 | +- Prefer `make` targets over ad-hoc dune/cargo invocations when possible |
| 232 | + |
0 commit comments