Skip to content

ref: Add mapBounded and lint against await in loops - #2773

Merged
BYK merged 9 commits into
mainfrom
ref/cli-map-bounded
Dec 2, 2026
Merged

BYK merged 9 commits into
mainfrom
ref/cli-map-bounded

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

An await inside a loop runs independent work one item at a time, so a list of N API calls takes N round trips. This PR adds a bounded fan-out helper, turns on Oxlint's built-in no-await-in-loop for the whole repo, and fixes every place the rule flags. The commits are split by area (CLI src, CLI tests, CLI scripts, mcp-core, the remaining packages, lint config) so each can be reviewed on its own. 8dfd11f5 reverts an earlier all-in-one commit that those commits replace.

Helper

mapBounded in packages/cli/src/lib/fan-out.ts:

mapBounded<T, R>(
  items: Iterable<T>,
  fn: (item: T, index: number) => PromiseLike<R> | R,
  concurrency: number,
): Promise<R[]>

It's a thin wrapper over pLimit(concurrency).map(...) (p-limit is already a CLI dependency). Results come back in input order, and the first rejection rejects the whole map, like Promise.all. Callers pass the cap: ORG_FANOUT_CONCURRENCY for API calls, or CONCURRENCY_LIMIT (lib/scan/constants.ts) for local files.

Lint rule

no-await-in-loop is in the root rules of .oxlintrc.json. Test files (*.test.*, packages/cli/test/**, test-utils/) turn it off with one glob override: their loops are table-driven assertions against shared state, and running them in order is the point.

Loops that now run concurrently

Each rewrite keeps result order and error behaviour.

  • CLI: alert rule lookups by ID or name across orgs and projects, workflow documents in alert metrics view, project list across orgs, DSN project access, dashboard widget queries (mapBounded replaces fixed batches of 5, so one slow widget no longer holds back the next batch), debug-file assemble batches, chunk encoding within an upload request, DSN cache mtime checks, .sentryclirc hash checks, and local file reads and hashes for sourcemaps, ProGuard, JVM bundles, asset catalogs and bash-hook tracebacks. API fan-out is capped at ORG_FANOUT_CONCURRENCY.
  • http: the authorization digest is computed once, after the header copy.
  • Scripts: file reads in the doc, env, error-pattern, fragment, patch and stale-reference checks, chunk hashing, bundle and doc page writes, generated schema modules, command module imports, grammar generation, skill scope validation, UI resource reads, nightly manifest checks, and envelope directory walks.

Loops that stay sequential

Each one has a // oxlint-disable-next-line no-await-in-loop -- <reason> comment, or a disable/enable pair around a loop with several awaits. The reasons fall into a few groups: cursor pagination, polling and retries, stream reads, generator drivers, ordered or shared-file writes, first match in priority order, interactive prompts, project scope checks that must stop before more lookups go out, benchmarks, and external calls kept one at a time for rate limits. Detector lookups for an alert rule in mcp-core stay one at a time, since mcp-core has no fan-out cap.

packages/cli/AGENTS.md lists mapBounded among the helpers to reuse and states the rule.

via @BYK.

--

View Session [Sentry]

Add `mapBounded(items, fn, concurrency)` in `src/lib/fan-out.ts`, a thin
wrapper over `pLimit(concurrency).map` that keeps input order, and turn on
Oxlint's `no-await-in-loop` for `packages/cli/src`. The 82 files that
already await in a loop are listed in an override that turns the rule off
for them, so new code is checked while existing loops get fixed file by
file.

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@sentry-junior
sentry-junior Bot requested a review from BYK December 2, 2026 09:08
@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
cli Ready Ready Preview Dec 2, 2026 11:55am UTC
sentry-local Ready Ready Preview Dec 2, 2026 11:55am UTC

Request Review

@github-actions github-actions Bot added the risk: low PR risk score: low label Dec 2, 2026
Comment thread .oxlintrc.json Resolved

@BYK BYK left a comment

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.

I'm 100% sure there are violations in the repo. I want these fixed

Comment thread .oxlintrc.json Outdated
"packages/cli/src/lib/wizard/sourcemaps/tools/sentry-cli.ts",
"packages/cli/src/lib/wizard/utils/clack/index.ts",
"packages/cli/src/lib/wizard/utils/clack/mcp-config.ts",
"packages/cli/src/lib/wrangler.ts"

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.

Why are we listing files one by one? This should apply to the entire repo

Move `no-await-in-loop` from the `packages/cli/src` override to the root
rules and drop the per-file off list. Test files keep the rule off, by
glob: their loops are table-driven assertions that run in order.

Independent work now fans out with `mapBounded` or `Promise.all`: CLI
lookups across orgs and projects, dashboard widget queries, assemble
batches, local file reads and hashes, alert rule detector lookups in
mcp-core, and file reads and writes in build and check scripts. Loops that
must stay sequential (cursor pages, polling, retries, ordered writes,
first match wins, prompts, scope checks) keep their await with a disable
comment that gives the reason.
@sentry-junior
sentry-junior Bot requested a review from a team as a code owner December 2, 2026 11:38
@sentry-junior sentry-junior Bot changed the title ref(cli): Add mapBounded and lint against await in loops ref: Add mapBounded and lint against await in loops Dec 2, 2026
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: low PR risk score: low labels Dec 2, 2026
…sites"

This reverts commit cb82197. The same
change follows as one commit per area, so each can be reviewed on its own.
Run independent per-item work with `mapBounded` or `Promise.all` instead of
one item at a time, keeping result order and error behaviour: alert rule
and workflow lookups, project listing across orgs, DSN project access,
dashboard widget queries, assemble batches, chunk encoding, DSN cache and
`.sentryclirc` checks, and local file reads and hashes. API fan-out is
capped at `ORG_FANOUT_CONCURRENCY`, local file work at `CONCURRENCY_LIMIT`.
Loops that must stay sequential get a disable comment with the reason.
`getAccessibleProjects` now caps its fan-out with `ORG_FANOUT_CONCURRENCY`,
which these mocks replace with `undefined`.
File reads in the doc, env, error-pattern, fragment, patch and
stale-reference checks, chunk hashing, bundle and doc page writes,
generated schema modules and command module imports run concurrently,
keeping output order. Benchmarks, evals, ordered generation steps and
shared-file rewrites stay sequential with a reason.
Skill scope validation and UI resource reads in scripts run concurrently.
Pagination, polling, retries, redirect hops, dependent link fields and
project scope checks stay sequential with a reason. Detector lookups for an
alert rule stay one at a time, since mcp-core has no fan-out cap.
http computes the `authorization` digest once, after the header copy.
Grammar generation, nightly manifest checks and envelope directory walks run
concurrently. Retries, stream reads, prompts, polling and replays stay
sequential with a reason.
Move `no-await-in-loop` from the `packages/cli/src` override to the root
rules and drop the per-file off list. Test files keep the rule off by glob:
their loops are table-driven assertions against shared state.
Comment thread packages/cli/src/commands/event/send.ts Resolved
@BYK
BYK added this pull request to the merge queue Dec 2, 2026
Merged via the queue into main with commit 0ea70ca Dec 2, 2026
60 of 61 checks passed
@BYK
BYK deleted the ref/cli-map-bounded branch December 2, 2026 12:54
sentry-junior Bot added a commit that referenced this pull request Dec 2, 2026
Resolve conflicts with dropped-data notes (#2609), saved transformation
descriptions (#2114), mapBounded fan-out (#2773), and the strict
exactOptionalPropertyTypes policy (#2266).

Co-Authored-By: Nicholas Deschenes <nicholas.deschenes@sentry.io>

This branch was successfully deployed

2 active and 1 inactive deployments
Preview – sentry-local — a47c8fbf Deployed Dec 2, 2026 by vercel[bot]
Preview – cli — a47c8fbf Deployed Dec 2, 2026 by vercel[bot]
Actions — a47c8fbf Deployed Dec 2, 2026 by sentry-junior[bot] via eval #2158
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant