Skip to content

Fix page_n not found error in paginated exports - #25

Merged
amccann-tw merged 3 commits into
mainfrom
ams-7144-cursor-pagination
Jun 9, 2026
Merged

Fix page_n not found error in paginated exports#25
amccann-tw merged 3 commits into
mainfrom
ams-7144-cursor-pagination

Conversation

@jimmyday12

@jimmyday12 jimmyday12 commented Jun 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Fixes a crash introduced by the cursor-based pagination feature (6914587) where any paginated export with more than one page of results would fail with ! object 'page_n' not found.
  • One-character fix: change .envir = arg\$current_env to .envir = rlang::current_env() in the page-progress message inside .paginate_export().

Root cause

In .paginate_export(), the progress message shown on page 2+ uses a {page_n} glue expression:

cli::cli_progress_message(
  "Fetching page {page_n} of {.field {arg$form}} data...",
  .envir = arg$current_env   # ← bug
)

Passing .envir = arg\$current_env tells cli to evaluate {page_n} in the user's calling environment, where page_n does not exist — it is a local variable inside .paginate_export(). On the first page this block is skipped (page_n > 1L is FALSE), so single-page exports worked fine. Any multi-page result set crashed immediately when fetching page 2.

The fix uses rlang::current_env() so both page_n and arg\$form are evaluated in the function's own scope where both are defined.

Test plan

  • Reproduced the crash on staging against the Availability form with a 6-year date range (confirmed ! object 'page_n' not found on page 2)
  • Applied fix, re-ran same call — successfully paginated through all 48 pages returning 4,721 rows with no error
  • Run existing test suite (devtools::test())

Reviewer notes

Only one line changed. No behaviour change beyond unblocking multi-page exports. The arg\$current_env pattern used in other cli calls throughout the file remains correct because those messages only reference arg\$... fields, which are accessible via the call stack — page_n is the only purely-local variable used in a progress format string.

Resolves DP-1547.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed progress message environment handling during pagination operations to ensure status indicators display correctly when exporting multi-page datasets.

cli_progress_message() was passed .envir = arg$current_env (the user's
calling environment), so the {page_n} glue expression failed on page 2+
because page_n is a local variable inside .paginate_export(), not in the
caller's scope.

Fix: use rlang::current_env() so cli evaluates {page_n} and {arg$form} in
the function's own environment where both are defined.

Fixes DP-1547.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@jimmyday12, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 48 minutes and 22 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3411f2a0-054d-4e21-9558-d0e86d87c3c4

📥 Commits

Reviewing files that changed from the base of the PR and between 0283e3a and c32eb64.

📒 Files selected for processing (7)
  • DESCRIPTION
  • R/export_extract.R
  • R/export_handler.R
  • R/smartabaseR.R
  • man/smartabaseR-package.Rd
  • man/smartabaseR.Rd
  • tests/testthat/test-export_handler.R
📝 Walkthrough

Walkthrough

The PR updates line 127 in R/export_handler.R to fix environment context when emitting pagination progress messages. The .paginate_export() function's "Fetching page …" CLI message now captures the current lexical environment directly via rlang::current_env() instead of using a stored reference from the argument object.

Changes

Export Handler Environment Context

Layer / File(s) Summary
CLI progress environment context
R/export_handler.R
The .envir argument to cli::cli_progress_message() at line 127 is changed from arg$current_env to rlang::current_env() for correct environment evaluation in pagination progress output.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

  • Teamworksapp/smartabaseR#24: Both PRs touch R/export_handler.R's cursor pagination flow—main PR adjusts .paginate_export()'s "Fetching page …" CLI progress message to use rlang::current_env() while the retrieved PR introduced the .paginate_export() pagination loop itself.

Suggested reviewers

  • rparastw
  • rpparas

Poem

A rabbit hops through progress bars,
Adjusting environments near and far,
rlang::current_env() now shines bright,
Making pagination feel just right! 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Fix page_n not found error in paginated exports' directly and clearly describes the main bug fix in the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ams-7144-cursor-pagination

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
R/export_handler.R (1)

124-128: ⚡ Quick win

Add a regression test for page-scoped glue interpolation.

This fix is correct, but please add a test that exercises interactive pagination beyond page 1 and asserts the progress message path does not error on {page_n} interpolation.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@R/export_handler.R` around lines 124 - 128, Add a regression test that
simulates interactive pagination past the first page and asserts the progress
message interpolation of {page_n} does not error: create a test in testthat that
sets arg$option$interactive_mode to TRUE, sets page_n > 1 (e.g. 2), and invokes
the code path in R/export_handler.R that calls cli::cli_progress_message (use
the same public function that triggers export_handler.R logic); capture or
expect the message (expect_message or withCallingHandlers) and assert the
message contains the rendered page number (e.g. "Fetching page 2") and that no
error is thrown during interpolation of {page_n}, referencing
arg$option$interactive_mode, page_n, arg$form and cli::cli_progress_message in
the test.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@R/export_handler.R`:
- Around line 124-128: Add a regression test that simulates interactive
pagination past the first page and asserts the progress message interpolation of
{page_n} does not error: create a test in testthat that sets
arg$option$interactive_mode to TRUE, sets page_n > 1 (e.g. 2), and invokes the
code path in R/export_handler.R that calls cli::cli_progress_message (use the
same public function that triggers export_handler.R logic); capture or expect
the message (expect_message or withCallingHandlers) and assert the message
contains the rendered page number (e.g. "Fetching page 2") and that no error is
thrown during interpolation of {page_n}, referencing
arg$option$interactive_mode, page_n, arg$form and cli::cli_progress_message in
the test.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 360ddf16-33ea-4e43-a460-f2a270077323

📥 Commits

Reviewing files that changed from the base of the PR and between 7dae122 and 0283e3a.

📒 Files selected for processing (1)
  • R/export_handler.R

jimmyday12 and others added 2 commits June 10, 2026 09:30
…-1547)

Two tests in test-export_handler.R:

1. Positive case: calls cli::cli_progress_message() with the exact format
   string and .envir = rlang::current_env() from .paginate_export(), asserts
   no error is thrown, and verifies the rendered text contains "Fetching page 2"
   and the form name.

2. Negative case: mirrors the broken code path (.envir = arg$current_env where
   page_n is absent) and asserts the error is produced, confirming that a
   reversion of the fix would be caught.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
- export_extract.R, export_handler.R: replace [.fn()] cross-reference syntax
  with `.fn()` backtick prose for internal @nord functions; Roxygen cannot
  resolve links to undocumented topics and emits a warning for each
- smartabaseR.R: replace deprecated @doctype package / @name pattern with the
  "_PACKAGE" sentinel as required by Roxygen >= 7.3
- DESCRIPTION: bump RoxygenNote to 7.3.3 (written automatically by document())
- man/: regenerate smartabaseR-package.Rd, remove stale smartabaseR.Rd

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@jimmyday12
jimmyday12 requested review from amccann-tw and rpparas June 9, 2026 23:42
@jimmyday12 jimmyday12 self-assigned this Jun 9, 2026
@jimmyday12
jimmyday12 removed the request for review from rpparas June 9, 2026 23:44
@amccann-tw
amccann-tw merged commit fdef2ff into main Jun 9, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants