Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions develop-docs/frontend/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,7 @@ Previously we used a combination of [lodash-webpack-plugin](https://www.npmjs.co
See [this PR](https://github.com/getsentry/sentry/pull/15521) for more information.

We prefer using optional chaining and nullish coalescing over `get` from `loadash/get`.

## Linting

Frontend linting uses Oxlint, Oxfmt, Stylelint, and Knip to enforce conventions and catch dead code mechanically. See [Linting](/frontend/linting/) for our linting philosophy and commands, and [Lint Incubator](/frontend/linting/lint-incubator/) for how new rules roll out incrementally with `oxlint-suppressions.json` budgets.
60 changes: 60 additions & 0 deletions develop-docs/frontend/linting/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: Linting
description: Why Sentry enforces coding conventions mechanically, how linting steers AI agents and human contributors, and what runs on frontend code.
sidebar_order: 72
---

Our guiding rule for code conventions is simple: **if a stylistic preference is not mechanically enforceable, it might as well not exist.**

Conventions that live only in documentation, Slack threads, or pull request comments decay over time. Reviewers miss them, authors copy older patterns from nearby files, and code reviews get bogged down in style debates instead of focusing on correctness, architecture, and product risk. When a preference cannot be checked automatically, we treat it as optional rather than spending review cycles on it.

## Why Mechanical Enforcement Matters Even More With Agents

AI coding agents have made mechanical enforcement more important, not less.

Agents learn how to write code in a repository primarily by reading nearby files and reacting to tool output. In a large, long-lived codebase like [`getsentry/sentry`](https://github.com/getsentry/sentry), older files inevitably contain deprecated helpers, legacy state libraries, and raw HTML where design-system primitives now exist. Without a deterministic check, an agent will copy those legacy patterns into new code.

Context files such as `AGENTS.md` help set direction, but prompt instructions are probabilistic and compete for attention as a session grows. Linters turn conventions into a fast, deterministic feedback loop: when an agent runs `pnpm run lint` or triggers a `prek` hook, an error tells it what failed and what to write instead before a human ever reviews the pull request.

## Principles for Good Lint Rules

When adding or updating a lint rule, optimize for fast comprehension and low false positives:

- **Enforce as errors, never permanent warnings**: Warnings get ignored by both humans and agents. Every active rule fails the build. When pre-existing violations prevent turning a rule on all at once, enroll it in the [Lint Incubator](/frontend/linting/lint-incubator/) instead of leaving it at `warn`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Bug] I don't think "Every active rule fails the build" is true today: oxlint.base.ts still has four 'warn' rules (jest/no-commented-out-tests, testing-library/no-debugging-utils, regexp/confusing-quantifier, regexp/no-empty-alternative), and nothing passes --deny-warnings/--max-warnings. prek even runs oxlint with --quiet, which hides warnings entirely.

You might consider rephrasing this as guidance for new rules ("New rules should be errors…"), or flipping those four in a follow-up. My preference would be to make the four warn rules error though, so this becomes fully correct. 🧹

- **Write actionable error messages**: Say what is wrong and name the preferred replacement directly in the diagnostic message (for example, pointing to `resolveHostname()`, a `@sentry/scraps` primitive, or the allowed import path). Good error messages let contributors and agents fix violations without hunting through docs.
- **Autofix only when unambiguous**: Implement `--fix` when there is a single safe, behavior-preserving rewrite, such as rewriting an import path. Leave fixes manual when picking the right semantic token, component prop, or HTML behavior requires judgment.
- **Keep checks fast and uniform**: Local CLI runs, `prek` git hooks, and CI run the same checks so contributors get identical results inside their local inner loop.

## Frontend Linting Stack

Frontend checks in `getsentry/sentry` split responsibility across five tools:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Docs] Small mismatch: the overview in frontend/index.mdx lists four tools (Oxlint, Oxfmt, Stylelint, Knip), while this says five. The fifth (dependency linting) also doesn't name sherif, which might help folks who see it in CI output.


- **Oxlint (`pnpm run lint:js`)**: Runs JavaScript and TypeScript rules, including type-aware checks, module boundary rules, and custom plugins for Sentry conventions (`static/oxlint/eslintPluginSentry`) and Scraps design-system usage (`static/oxlint/eslintPluginScraps`).
- **Oxfmt (`pnpm run lint:format`)**: Formats code consistently across the repository so layout and whitespace are never debated in review.
- **Stylelint (`pnpm run lint:css`)**: Checks Emotion CSS-in-JS styles.
- **Knip (`pnpm run knip`, `pnpm run knip:prod`, `pnpm run knip:cycles`)**: Detects unused files, unused exports, unlisted or unused dependencies, production dead code (exports only referenced by tests or stories), and circular module imports. This is especially useful after refactors, where orphaned helpers and dead exports are otherwise easy to leave behind.
- **Dependency linting (`pnpm run lint:deps`)**: Verifies dependency consistency across `pnpm` workspace packages.

Use these commands during local development:

```bash
# Run dependency, formatting, JS/TS, and CSS checks
pnpm run lint

# Lint specific JS/TS files in their owning workspace
pnpm run lint:js static/app/views/example.tsx

# Check for unused files/exports, production dead code, and import cycles
pnpm run knip
pnpm run knip:prod
pnpm run knip:cycles

# Apply formatting and safe Oxlint autofixes
pnpm run fix
```

## Guides

Explore how we roll out new lint rules incrementally across existing frontend code:

<PageGrid />
88 changes: 88 additions & 0 deletions develop-docs/frontend/linting/lint-incubator.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
title: Lint Incubator
description: How Sentry rolls out new JavaScript and TypeScript lint rules incrementally using Oxlint suppression budgets and CI ratcheting.
sidebar_order: 10
---

The lint incubator is a ratcheting system in [`getsentry/sentry`](https://github.com/getsentry/sentry) for rolling out new JavaScript and TypeScript lint rules incrementally. It lets us enforce a new rule immediately on new and modified code without fixing every pre-existing violation in the repository upfront.

Turning on a new rule across a large codebase usually forces a bad choice: setting it to `error` blocks adoption behind hundreds of existing violations, while setting it to `warn` lets new violations keep landing on `master`.

The incubator avoids both problems:

1. Pre-existing violations on `master` are recorded as per-file, per-rule count budgets in `oxlint-suppressions.json` files.
2. Any new violation (either in a clean file or above a file's recorded budget) fails `pnpm run lint:js`, `prek`, and CI.
3. As contributors clean up existing violations, those budgets ratchet down and cannot go back up. Once a rule reaches zero violations, it graduates into the standard lint config.

## How Budgets Work

Rules under rollout are registered in the exported `incubator` config in `oxlint.config.ts`. Because Oxlint's editor language server does not read bulk suppression files yet, editors show incubator findings as warnings to avoid noisy red squiggles on pre-existing debt, while `pnpm run lint:js`, `prek`, and CI enforce them as errors.

Each `pnpm` workspace stores its own `oxlint-suppressions.json` file (for example, `static/app/oxlint-suppressions.json` and `static/gsApp/oxlint-suppressions.json`) using workspace-relative file paths:

```json
{
"components/acl/featureDisabled.tsx": {
"@sentry/scraps/no-dom-coupling": {
"count": 2
},
"@sentry/scraps/prefer-primitives": {
"count": 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Nit] Totally optional since it's illustrative: the real static/app/oxlint-suppressions.json uses compact {"count": 2} entries, and featureDisabled.tsx currently has prefer-primitives: 2.

}
}
}
```

A file or rule with no entry has a budget of `0`. When a workspace has no remaining violations, its `oxlint-suppressions.json` file is deleted rather than left empty. Files outside `pnpm` workspaces cannot carry suppression budgets and must fix or disable violations directly.

## Working With the Incubator

Most day-to-day work uses ordinary `pnpm run lint:js` and `pnpm run fix:oxlint` runs, while rule rollouts and budget cleanups use `pnpm run lint:suppressions`.

### Resolving Ratchet Failures

- **`New incubator violations`**: Your change added a violation in a clean file or pushed a file above its `master` budget. Because budgets track counts per file rather than line numbers, the error output lists every current violation of that rule in the file. Fix one or more of them to bring the count back within budget. Do not manually raise `count` in `oxlint-suppressions.json`; CI compares against `master` and rejects budget increases. If a specific line genuinely requires an exception, add an inline disable comment (`// oxlint-disable-next-line <rule>`) explaining why, then prune if the file also had a recorded budget.
- **`Suppression budgets are stale`**: Your changes lowered the violation count in one or more files (either directly, or indirectly by updating a shared TypeScript type), leaving the committed budget higher than live debt. `pnpm run fix:oxlint` lowers budgets automatically when autofixing root files; otherwise run `pnpm run lint:suppressions --prune` and commit the updated `oxlint-suppressions.json` files.
Comment on lines +44 to +45

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] These headings are messages from lint:suppressions: New incubator violations comes from --ci/--check/--enroll/--prune, and Suppression budgets are stale only from --ci. A local pnpm run lint:js or prek failure just shows plain oxlint diagnostics, e.g.:

static/app/example.tsx:2:22: error @sentry/scraps(prefer-primitives): Use Flex from '@sentry/scraps/layout'. ...

Would it help to note where each message comes from, so readers don't look for these headings in local output?

- **Renaming or splitting files**: Edits within the same file pass as long as the file's count for that rule does not grow. Unmodified `100%` Git renames transfer their budget to the new path when you run `pnpm run lint:suppressions --enroll --base origin/master`. Edited renames, copies, and split files start with a budget of `0`, so either fix the violations in the new file or land a pure rename first, and run `pnpm run lint:suppressions --prune` to clean up the old path's budget.

### Enrolling, Updating, and Graduating Rules

- **Enroll a new rule (or broaden an existing rule)**: Add the rule with `'error'` severity to `incubator` in `oxlint.config.ts` (use `incubator.overrides` for file exclusions or test-only rules, and also enable the rule in `oxlint.base.ts` if standalone workspace packages with their own `oxlint.config.ts` should run it). Then generate baseline budgets against `master`:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] For the "also enable the rule in oxlint.base.ts" path: createPackageConfig spreads base.rules without the incubator's warn-in-editor switch (that only lives in oxlint.config.ts). So I think in scraps/icons/search-syntax, editors would show the rule as an error rather than the warning "How Budgets Work" describes. Might be worth a caveat?


```bash
git fetch origin master
pnpm run lint:suppressions --enroll --base origin/master
```

Commit `oxlint.config.ts` together with the generated `oxlint-suppressions.json` files. Avoid introducing new violations of the rule in unrelated source files in the same pull request, as `--enroll` checks that counts do not exceed the base revision.
- **Inspect the backlog**: Because `oxlint-suppressions.json` hides budgeted findings during normal lint runs, use `--backlog` to list remaining violations:

```bash
pnpm run lint:suppressions --backlog --rule @sentry/scraps/prefer-primitives
pnpm run lint:suppressions --backlog --file static/app/views/example.tsx
pnpm run lint:suppressions --backlog --rule @sentry/scraps/no-dom-coupling --json
```

- **Narrow a rule**: When excluding additional files or fixing false positives in a custom rule, run `pnpm run lint:suppressions --prune` and commit the reduced or deleted suppression files.
- **Graduate a rule**: Once `pnpm run lint:suppressions --backlog --rule <rule>` returns no findings, remove the rule from `incubator` in `oxlint.config.ts` and add it as `'error'` in `oxlint.base.ts` (or `oxlint.config.ts` if app-only). From then on, the rule reports errors in editors as well as CLI and CI.

## How CI Keeps Budgets Accurate

Three GitHub Actions workflows enforce the ratchet and keep `oxlint-suppressions.json` files up to date:

- **Ratchet verification (`frontend.yml`)**: Checks that no `(file, rule)` count exceeds `master` and that committed `oxlint-suppressions.json` budgets match live violations. If concurrent merges on `master` leave behind stale budgets that your pull request did not touch or cause, CI logs a warning instead of failing your pull request.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Docs] It might help to name the command here: this step runs pnpm run lint:suppressions --ci --base HEAD^1 on PRs, and --ci isn't in the Command Reference table either. That'd make it easier to map CI logs back to these docs.

- **Pull request auto-pruning (`pre-commit.yml`)**: Runs `pnpm run lint:suppressions --prune` after `prek` and automatically commits reduced budgets on non-draft pull requests from `getsentry/sentry` branches. On draft or fork pull requests, run `--prune` locally and commit the changes yourself.
- **Automated `master` cleanup (`prune-oxlint-suppressions.yml`)**: When `master` accumulates stale budgets from non-linear merges, CI runs `--prune`, verifies that budgets only decreased, and opens or updates a cleanup pull request (`chore(lint): prune oxlint suppressions`) so `master` stays aligned with live debt.

## Command Reference

Pass maintenance flags to `pnpm run lint:suppressions` as the first argument.

| Command | Purpose |
| --- | --- |
| `pnpm run lint:js [paths...]` | Run Oxlint with incubator budgets enforced. Rejects config, rule, and suppression overrides. |
| `pnpm run fix:oxlint` | Run `pnpm run lint:js --fix` and lower budgets for autofixed violations in root-linted files. |
| `pnpm run lint:suppressions --prune` | Lower or delete stale `oxlint-suppressions.json` entries across all workspaces to match live counts. |
| `pnpm run lint:suppressions --enroll --base REF` | Scan `REF` with the current lint policy, verify live counts do not exceed `REF`, and write updated `oxlint-suppressions.json` files. |
| `pnpm run lint:suppressions --check [--base REF]` | Compare live incubator violations against `REF` (defaults to the merge base of `HEAD` and `origin/master`). |
| `pnpm run lint:suppressions --backlog [--rule RULE] [--file PATH] [--json]` | List current incubator findings ignoring suppression budgets, with optional rule, file, and JSON filters. |
Loading