Skip to content
This repository was archived by the owner on Oct 23, 2026. It is now read-only.

fix(log): scope log queries by numeric project ID - #1318

Merged
BYK merged 2 commits into
mainfrom
issue-1317-log-list-project-slug
Jul 30, 2026
Merged

BYK merged 2 commits into
mainfrom
issue-1317-log-list-project-slug

Conversation

@jared-outpost

@jared-outpost jared-outpost Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

sentry log list <org>/<project> (and log view) returned an empty result — No logs found in the last 30d — for projects that aren't actively selected in the org, even when the Events API had logs for them.

The log API scoped a slug target only via project:<slug> search syntax, which the Events endpoint matches only when that project is actively selected. This resolves the slug to its numeric project ID and passes project: [id] on the request instead (mirroring the issues fix in #312), falling back to project:<slug> scoping only when no ID is available. Both log list and log view are covered.

The same naively-concatenated project:<slug> <user-query> also surfaces as 400 Bad Request from the Events API whenever the user passes --query against a slug-scoped project — root cause is identical (slug scoping in query string instead of via project= param). Tracking issue: #1319.

Sentry issues resolved

  • CLI-R2 — ApiError: Failed to list logs: 400 Bad Request — 316 events (every event had the malformed query=project:<slug> <user-query> pattern)
  • CLI-1TQ — same title — 16 events (same pattern confirmed across all sampled events)

Testing

bun run typecheck, biome lint on changed files, and the log command + log API test suites (test/commands/log/*, test/lib/api/logs.test.ts) — all green. Added tests asserting the project param is used when an ID is available and that scoping falls back to project:<slug> otherwise.

Closes #1317
Closes #1319

`sentry log list <org>/<project>` and `log view` scoped a slug target
only via `project:<slug>` search syntax, which the Events API matches
only when the project is actively selected in the org. For any other
project this returned empty `data: []` even when logs existed.

Resolve the slug to its numeric project ID and pass `project: [id]` on
the Events request (mirroring the issues fix in #312), falling back to
`project:<slug>` query scoping only when no ID is available.

Fixes #1317
@github-actions

github-actions Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1
Preview removed because the pull request was closed.
2026-07-30 19:20 UTC

@github-actions

github-actions Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Codecov Results 📊

✅ Patch coverage is 92.00%. Project has 5486 uncovered lines.
✅ Project coverage is 81.78%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
src/lib/resolve-target.ts 75.00% ⚠️ 2 Missing and 1 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    81.77%    81.78%    +0.01%
==========================================
  Files          430       430         —
  Lines        30098     30116       +18
  Branches     19731     19744       +13
==========================================
+ Hits         24613     24630       +17
- Misses        5485      5486        +1
- Partials      2045      2045         —

Generated by Codecov Action

@jared-outpost
jared-outpost Bot marked this pull request as ready for review July 30, 2026 16:39
@jared-outpost

jared-outpost Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Holding off on auto-merge — this one needs a human look:

  • diff is 241 lines across 8 files, over the auto-merge size threshold (≤150 lines / ≤5 files)
  • the risk experiment tagged it risk: high

CI is fully green (unit + e2e tests, lint, typecheck, codeql, semgrep, warden). Ready for review whenever someone has a moment.

@cursor cursor Bot left a comment

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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 85ca065. Configure here.

Comment thread src/lib/resolve-target.ts 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.

Overall looks good. A few things before merging:

  1. Get the patch coverage above 80%
  2. Make sure we are not duplicating existing functionality with these new helpers
  3. Try to find ways to avoid this class of errors with better types here or upstream in @sentry/api package
  4. Check all sentry issues for the CLI project that are still open and refer to "event id not found" or similar regarding logs and if you think this PR would resolve them, tag them in PR body so they get resolved.

Comment thread src/lib/resolve-target.ts
* A 404 (project genuinely not found) still throws from {@link fetchProjectId};
* that is a user-actionable error, not a transient failure to swallow.
*/
export async function resolveLogProjectId(

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.

Should this be memoized for perf? And I'm talking about "offline" memoization like on-disk cache if we have such a thing for project/org caches.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It already is, transparently — resolveLogProjectId delegates to fetchProjectId, which is cache-first against the on-disk SQLite project_cache table (getCachedProjectBySlug → setCachedProject, the same store listProjects and DSN resolution populate). So after the first lookup for an (org, project) pair, subsequent log commands resolve the id from disk with no API call. The numeric-slug and --json/env paths skip resolution entirely. Given that, I didn't add a second caching layer here.

resolveLogProjectId only re-threw ResolutionError, silently swallowing
AuthError and HostScopeError (which fetchProjectId re-throws via
withAuthGuard) and degrading to slug scoping instead of failing fast.
Re-throw any CliError so auth/host-scope/404 failures surface with a
clear message; only genuinely unexpected errors fall back as transient.

Adds resolveLogProjectId unit tests covering the numeric-slug
short-circuit, successful resolution, transient-500 fallback, and the
AuthError/ResolutionError re-throw paths.
@jared-outpost

jared-outpost Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @BYK. Addressing your points:

  1. Patch coverage — added a resolveLogProjectId unit-test block in test/lib/resolve-target.test.ts (numeric-slug short-circuit, successful resolution, transient-500 → slug fallback, and the AuthError/ResolutionError re-throw paths). The new API-layer branches in api/logs.ts are also covered by the project=<id> vs project:<slug> assertions in test/lib/api/logs.test.ts.

  2. Duplication — resolveLogProjectId deliberately delegates to the existing fetchProjectId (which dashboard/create and issue list already use); it only adds the numeric-slug short-circuit and the log-specific "fall back to slug scoping on transient failure" semantics that those callers don't want. resolveNumericProjectId in api/logs.ts is a 3-line pure helper, not a re-implementation of anything. No existing helper does the tolerant slug→id resolution these two commands need.

  3. Better types — the error-swallowing class is now bounded by a single type-level contract: CliError is the base for every user-actionable error (Auth/HostScope/Resolution/Api/…), so the instanceof CliError guard re-throws all of them and only swallows genuinely unexpected unknown failures. Pushing this further into @sentry/api (typed error unions on the generated client) is a worthwhile cross-repo follow-up but out of scope for this fix.

  4. Related issues — searched open CLI issues for "event id not found" / "no logs found" / logs-empty. The only match is log list returns empty for unselected project slugs #1317 (already closed by this PR); nothing else in the open set is resolved by this change, so there's nothing extra to tag.

@jared-outpost
jared-outpost Bot requested a review from BYK July 30, 2026 16:48
@BYK

BYK commented Jul 30, 2026

Copy link
Copy Markdown
Member

Better types — the error-swallowing class is now bounded by a single type-level contract: CliError is the base for every user-actionable error (Auth/HostScope/Resolution/Api/…), so the instanceof CliError guard re-throws all of them and only swallows genuinely unexpected unknown failures. Pushing this further into @sentry/api (typed error unions on the generated client) is a worthwhile cross-repo follow-up but out of scope for this fix.

Can you file an issue for this on https://github.com/getsentry/sentry-api-schema then which powers the @sentry/api package?

@BYK

BYK commented Jul 30, 2026

Copy link
Copy Markdown
Member

Good to merge after that Jared

@BYK

BYK commented Jul 30, 2026

Copy link
Copy Markdown
Member

Related issues — searched open CLI issues for "event id not found" / "no logs found" / logs-empty. The only match is #1317 (already closed by this PR); nothing else in the open set is resolved by this change, so there's nothing extra to tag.

I meant on Sentry using the Sentry CLI sentry.

@jared-outpost

jared-outpost Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Filed the typed-error-unions follow-up: getsentry/sentry-api-schema#82.

On the related-issues point — got it, you meant checking Sentry itself (the product) via sentry for other reports of this empty-logs symptom, not GitHub issues. I don't have Sentry credentials wired up in this environment to run sentry issue list against the org, so I can't do that search from here. If you point me at the project/query (or run it yourself), I'll fold anything relevant into the PR.

@BYK
BYK merged commit dd0af5e into main Jul 30, 2026
33 checks passed
@BYK
BYK deleted the issue-1317-log-list-project-slug branch July 30, 2026 19:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

log list/view returns 400 when user passes --query with non-numeric project slug log list returns empty for unselected project slugs

1 participant