Skip to content

fix(search): rewrite numeric project: filters to project_id - #1621

Open
betegon wants to merge 2 commits into
mainfrom
fix/search-numeric-project-id
Open

betegon wants to merge 2 commits into
mainfrom
fix/search-numeric-project-id

Conversation

@betegon

@betegon betegon commented Sep 22, 2026 •

Copy link
Copy Markdown
Member

Summary

Issue search treats project as a slug. Agents paste Sentry's numeric project id into --query and get a 400 (CLI-FA). project_id is the numeric field.

sanitizeQuery now rewrites project:<digits> and all-numeric project:[id,id] to project_id, with a warning, same as the existing OR → in-list repair. Slugs (project:frontend), already-correct project_id:…, namespaced keys (bolt.project_id), quoted strings, and mixed in-lists are left alone.

Before / after

Command from CLI-FA:

sentry issue list --json -q "project:4511730126487632 environment:vercel-production"

Before — 400, ApiError, grouped in CLI-FA. The API rejects a numeric value on the slug field:

Error: Failed to fetch issues from 1 project(s): Failed to list issues: 400 Bad Request
  Endpoint: /api/0/organizations/…/issues/
  Invalid query: …

  Suggestions:
    • Check your --query syntax (Sentry search reference: …)
    • Try a shorter time range: --period 14d or --period 24h
    • Verify you have access to the target project: sentry project list <org>/

After — rewrite in sanitizeQuery, warn, run the query, exit 0:

⚠ `project` is the slug; numeric ids use project_id. Rewrote numeric project: filters. Running query: "project_id:4511730126487632 environment:vercel-production"

Stdout is the issue list JSON. Same rewrite for project:[4505521413357568,6442225] → project_id:[…].

Test plan

  • sanitizeQuery("project:4511730126487632 environment:vercel-production") → project_id:4511730126487632 environment:vercel-production
  • project:[4505521413357568,6442225] rewrites; project:[frontend,6442225] does not
  • project:frontend, project_id:123, bolt.project_id:70054175, message:"project:123" unchanged
  • From packages/cli: pnpm exec vitest run test/lib/search-query.test.ts test/lib/search-query.property.test.ts

Issue search treats project as a slug, so agents that paste project:<id>
hit a 400 (CLI-FA). Same auto-repair path as OR→in-list: rewrite digits
to project_id, leave slugs and namespaced keys alone.

Co-authored-by: Cursor <cursoragent@cursor.com>
@vercel

vercel Bot commented Sep 22, 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 Sep 22, 2026 2:10pm UTC
1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Sep 22, 2026 2:10pm UTC

Request Review

@vercel
vercel Bot temporarily deployed to Preview – sentry-local September 22, 2026 14:09 Inactive
@betegon
betegon marked this pull request as ready for review September 22, 2026 14:11
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Sep 22, 2026
Comment thread packages/cli/src/lib/search-query.ts
## Summary

Follow-up to #1621. `sanitizeQuery` warned `Running query:` after the
numeric `project:` rewrite, then again after OR/AND. Combined input
logged an intermediate that never ran.

All rewrites now finish first. One warning: reasons on the first line,
`Running query:` on the second, quoting the query that is actually sent.
Same for a failed OR: no `Running query:` before the `ValidationError`.

Stacked on #1621 — merge that first.

### Before / after

```bash
sentry issue list --json -q "project:123 OR project:456"
```

**Before** — two warnings, first one is a lie:

```text
⚠ `project` is the slug; numeric ids use project_id. Rewrote numeric project: filters. Running query: "project_id:123 OR project_id:456"
⚠ Rewrote OR using in-list syntax: key:[val1,val2]. Running query: "project_id:[123,456]"
```

**After** — one warning, final query on its own line:

```text
⚠ `project` is the slug; numeric ids use project_id. Rewrote numeric project: filters. Rewrote OR using in-list syntax: key:[val1,val2].
  Running query: "project_id:[123,456]"
```

## Test plan

- [ ] `project:123 OR project:456` → `project_id:[123,456]`, one warn,
`Running query:` on the second line
- [ ] `project:123 AND is:unresolved` → `project_id:123 is:unresolved`,
same shape
- [ ] `level:error OR assigned:me` throws with no `Running query:`
- [ ] From `packages/cli`: `pnpm exec vitest run
test/lib/search-query.test.ts test/lib/search-query.warn.test.ts`

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment on lines 445 to 452
notes.push("Rewrote OR using in-list syntax: key:[val1,val2].");
if (hasAnd) {
notes.push("Also removed explicit AND (implicit in Sentry search).");
}
notes.push(`Running query: "${result}"`);
log.warn(notes.join(" "));
return result;
return serializeNodes(rewritten);
}

throw new ValidationError(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: If a query with a numeric project filter fails to parse, it's rewritten and returned silently without the intended warning, because the warning logic is skipped.
Severity: MEDIUM

Suggested Fix

Move the logic for detecting and noting rewrites to before the try...catch block for parsing. This ensures that any modification to the query is recorded and warned about, even if the subsequent parsing step fails. The warning can then be emitted regardless of the parse outcome.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/cli/src/lib/search-query.ts#L445-L452

Potential issue: In the `sanitizeQuery` function, if a search query contains a numeric
project filter like `project:123` and also has a syntax error that causes the PEG parser
to fail (e.g., an unmatched parenthesis), the query is modified by
`rewriteNumericProjectFilters`. However, because the `parse` function throws an
exception, the `catch` block returns the rewritten query directly. This control flow
bypasses the `preParseRewriteNotes` function call, which is responsible for generating a
warning about the modification. As a result, the user's query is silently changed, which
contradicts the intended behavior of warning users about automatic rewrites.

This branch was successfully deployed

1 active (outdated) and 1 inactive (outdated) deployments
Preview – cli — 9af240d4 Deployed Sep 22, 2026 by vercel[bot]
Preview – sentry-local — 9af240d4 Deployed Sep 22, 2026 by vercel[bot]
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