Skip to content

[CP Staging] fix(navigation): stop goUp at a pop prevented by a beforeRemove guard - #102022

Merged
Julesssss merged 2 commits into
Expensify:mainfrom
software-mansion-labs:fix-goup-double-pop-on-prevented-remove
Sep 23, 2026
Merged

Julesssss merged 2 commits into
Expensify:mainfrom
software-mansion-labs:fix-goup-double-pop-on-prevented-remove

Conversation

@WojtekBoman

@WojtekBoman WojtekBoman commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

cc: @huult @KJ21-ENG

Explanation of Change

goUp kept sending pops after a beforeRemove guard prevented one, so the discard-changes guard saw a second pop and closed its own prompt. dispatch now detects a dropped action via the noop __unsafe_action__ event, stops goUp there, and disarms the focus-restore skip. Tests added in GoBackTests.tsx.

Fixed Issues

$ #102010
PROPOSAL:

Tests

Test 1 - Discard prompt shows on RHP Back (Description)

  1. Open any expense report and open an expense you can edit
  2. Click the Description field to open the edit page in the RHP
  3. Type some text without saving
  4. Click the Back button in the RHP header
  5. Verify that the discard-changes confirmation prompt appears and stays open
  6. Click "Cancel" in the prompt
  7. Verify that the prompt closes, the Description page stays open, and the typed text is still there

Test 2 - Discard prompt shows on RHP Back (Merchant) and Discard leaves the page

  1. Open an editable expense
  2. Click the Merchant field to open the edit page in the RHP
  3. Change the merchant without saving
  4. Click the Back button in the RHP header
  5. Verify that the discard-changes confirmation prompt appears and stays open
  6. Click "Discard"
  7. Verify that the RHP goes back to the expense details, and the merchant is unchanged

Test 3 - Save still goes back normally

  1. Open an editable expense and click the Description field
  2. Change the description and click "Save"
  3. Verify that the RHP goes back to the expense details with no prompt, and the new description is shown
  4. Press Back again (or Esc on web)
  5. Verify that the RHP closes normally and focus returns to the element that opened it
  • Verify that no errors appear in the JS console

Offline tests

N/A - navigation-only change, no API calls or Onyx writes are affected.

QA Steps

Same as tests

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android: Native
    • Android: mWeb Chrome
    • iOS: Native
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

@WojtekBoman

Copy link
Copy Markdown
Contributor Author

@codex review

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
src/libs/NavigationFocusReturn/index.native.ts 96.03% <100.00%> (+0.03%) ⬆️
src/libs/Navigation/Navigation.ts 68.93% <93.33%> (+0.70%) ⬆️
src/libs/NavigationFocusReturn/index.ts 94.91% <0.00%> (-0.52%) ⬇️
... and 32 files with indirect coverage changes

@WojtekBoman

Copy link
Copy Markdown
Contributor Author

@MelvinBot review this

@MelvinBot

Copy link
Copy Markdown
Contributor

Review

The approach is right — bailing out of goUp at the first prevented pop is what stops the second dispatch from reaching the guard over its own open prompt and closing it. Three things I'd want resolved before merge, one of them likely user-visible.

1. After the user confirms "Discard", the rest of the back navigation is dropped

useDiscardChangesConfirmation stores only the single prevented action (src/hooks/useDiscardChangesConfirmation/index.ts:124) and replays only that one on confirm (index.ts:55-62).

Before this PR, goUp dispatched every pop in its plan. Now it stops at the first prevented one, so on confirm the user gets just that pop — not the remaining pops, the jumpTo, or the final pop/replace. In the exact scenario your own test 2 models (modal over two workspace splits), "back" after discarding lands one screen short of backToRoute.

For the single-pop flow in Web - Expense - Back button does not trigger discard change modal on Description this is fine. Can you confirm the multi-pop case still lands on the requested route, or note it as a known follow-up?

2. noop: true means "no state change", not "prevented"

React Navigation emits noop: true from two places in useOnAction: when a beforeRemove listener prevents the action, and when the router returned no state change. A targeted StackActions.pop that StackRouter refuses (currentIndex === 0 → returns null → coerced back to state because action.target === state.key) lands in the second branch.

In that case Navigation.ts:555-557 now returns early and goUp silently does nothing, where before it fell through to the final pop/replace. It's reachable if a non-preventing beforeRemove listener changes state mid-loop and invalidates a pop that was pre-computed from the pre-loop state — narrow, but the comment at Navigation.ts:532-533 states the implication in only one direction. At minimum the comment should say noop also covers a plain no-op.

3. shouldSkipFocusRestore leaks on the prevented path

dispatch arms skipNextFocusRestore() before dispatching, and that's a module-level one-shot latch consumed by the next back navigation (src/libs/NavigationFocusReturn/index.ts:168-170).

If the pop is prevented and the user then cancels the prompt, nothing ever consumes the latch and it eats the focus restore on the next unrelated Back/Esc. The comment directly above — "no window between 'set flag' and dispatch for an early-return to leak it" (Navigation.ts:531) — is declaring exactly the invariant the new early returns break, in a new shape. Arming the flag only after the dispatch is known to have applied would close it.

Smaller points
  • The new guard on the tab-pop is untested. Both new tests only exercise the popsToNavigator loop (test 2's 2 dispatches are both loop pops). The underlyingTabNavIndex branch at Navigation.ts:580-584 has no prevented-pop coverage.
  • goUp's return value is dead. The only caller ignores it (Navigation.ts:670), so the "return true" comments read as if they suppress a fallback that doesn't exist. Harmless, but misleading.
  • Test 1's state assertion is close to tautological. GoBackTests.tsx:826 compares getRootState() to a reference captured earlier; with no state change React Navigation hands back the same object. Asserting route names the way test 2 does is stronger.
  • navigationRef.current?.dispatch → navigationContainer.dispatch drops the per-call null check. Equivalent given the guard at the top of goUp, just flagging the behavior change if the container unmounts mid-loop.
  • Reference equality on event.data.action does hold — useNavigationHelpers passes the action object straight through to onAction, and the listener is subscribed and unsubscribed synchronously around the dispatch. No issue there.
  • Using __unsafe_action__ for app logic is documented as debug-only upstream, but runAfterPredictedTransition.ts already does it, so this is consistent with existing practice rather than a new pattern. Its global listener skips noop actions, so there's no interaction with the prediction window here.
  • PR body: Tests and QA Steps are empty and the author checklist is unticked. Worth filling in given this is on a DeployBlockerCash issue.

All substantive CI checks are green (the two failures are the checklist and independent-approval jobs).

I reviewed this statically — browser verification wasn't available in this run, and I couldn't execute the Jest suite locally either, so I'm relying on CI for the test results.

dispatch(popToNavigator);
// A prevented pop hands control to its guard: going on would change screens under its prompt or re-send the pop,
// which closes the prompt. On confirm the guard replays only this pop, which usually is the whole way back.
if (!dispatch(popToNavigator)) {

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.

Can there be any cases in the app where we should continue after a no-op?

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.

afaik not today: goUp's pops are all pop(n > 0) computed from the current state, so the only noop they can produce is a beforeRemove guard preventing them, and the dispatches after the loop are goUp's last step anyway

}
navigationRef.current?.dispatch(actionToDispatch);
let wasApplied = true;
const unsubscribe = navigationContainer.addListener('__unsafe_action__', (event) => {

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.

unsafe_action is synchronous. Are you sure that the navigation events will happen before the listener is triggered, but after it's registered?

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's the order: dispatch -> onAction -> beforeRemove -> emit('__unsafe_action__') all run synchronously inside the dispatch() call, so the event always should land between addListener and unsubscribe()

@WojtekBoman
WojtekBoman marked this pull request as ready for review September 23, 2026 15:25
@WojtekBoman
WojtekBoman requested review from a team as code owners September 23, 2026 15:25
@melvin-bot
melvin-bot Bot requested review from a team, flaviadefaria and huult and removed request for a team September 23, 2026 15:25
@melvin-bot

melvin-bot Bot commented Sep 23, 2026

Copy link
Copy Markdown

@huult Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot removed the request for review from a team September 23, 2026 15:25
@huult

huult commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: HybridApp
Screen.Recording.2026-09-23.at.22.49.02.mov
Android: mWeb Chrome
Screen.Recording.2026-09-23.at.22.50.28.mov
iOS: HybridApp
Screen.Recording.2026-09-23.at.22.51.52.mov
iOS: mWeb Safari
Screen.Recording.2026-09-23.at.22.52.21.mov
MacOS: Chrome / Safari
Screen.Recording.2026-09-23.at.22.47.09.mov

@huult huult 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.

LGTM.

@melvin-bot
melvin-bot Bot requested review from Julesssss and blimpich September 23, 2026 16:02
@blimpich
blimpich removed their request for review September 23, 2026 16:04
@lakchote lakchote changed the title fix(navigation): stop goUp at a pop prevented by a beforeRemove guard [CP Staging] fix(navigation): stop goUp at a pop prevented by a beforeRemove guard Sep 23, 2026

@lakchote lakchote 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.

LGTM. Thoughts @Julesssss?

Comment on lines +865 to +866
expect(skipSpy).toHaveBeenCalledTimes(1);
expect(cancelSkipSpy).toHaveBeenCalledTimes(1);

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.

It would be better to test the outcome of cancelSkipSpy being called, this would still pass if it's behaviour changed.

@Julesssss Julesssss 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.

1 comment about test but NAB as this resolves a blocker

@Julesssss
Julesssss merged commit 6dd93ed into Expensify:main Sep 23, 2026
42 of 51 checks passed
OSBotify pushed a commit that referenced this pull request Sep 23, 2026
…-pop-on-prevented-remove

[CP Staging] fix(navigation): stop goUp at a pop prevented by a beforeRemove guard

(cherry picked from commit 6dd93ed)

(cherry-picked to staging by lakchote)
@OSBotify OSBotify added the CP Staging marks PRs that have been CP'd to staging label Sep 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🚧 Julesssss has triggered a test Expensify/App build. You can view the workflow run here.

@OSBotify

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@os-botify

os-botify Bot commented Sep 23, 2026

Copy link
Copy Markdown

🔁 Filed a Staging retest request for deploy blockers #102010 after this PR was cherry-picked to staging.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Cherry-picked to staging by https://github.com/lakchote in version: 9.4.92-5 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

Help site review: no changes required

No draft PR created — nothing under App/docs/articles needs updating for this PR.

Why: this is an internal navigation fix. It changes how goUp sequences its dispatches when a beforeRemove guard prevents a pop, so the discard-changes prompt stays open instead of being closed by a follow-up dispatch. It restores the behavior the app was always meant to have — it doesn't add, remove, or rename any feature, tab, setting, or button.

What I checked:

If you'd rather the discard-changes prompt be documented as behavior in its own right, that's a net-new article request rather than a fix follow-up — say the word and I'll draft it.

I reached this from static review of the diff and a search of App/docs/articles. Browser verification wasn't available in this run, so I couldn't confirm the on-screen copy of the prompt against the live UI.

@WojtekBoman, since no help site changes are needed, there's no linked docs PR to review — please confirm you agree with that conclusion, or point me at an article you think should cover this and I'll open the draft PR.

@Julesssss

Copy link
Copy Markdown
Contributor

Requested QA retest here.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/lakchote in version: 9.4.92-8 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Cherry-picked to staging by https://github.com/lakchote in version: 9.4.93-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

@MelvinBot

Copy link
Copy Markdown
Contributor

No help site changes required

This PR is an internal navigation fix, not a product behavior change. No files under docs/articles need updating, so I did not create a draft docs PR.

Why:

@WojtekBoman, please confirm you agree that no help site update is needed. If you think an article should cover this behavior, tell me which one and I'll draft the docs PR.


view run

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/lakchote in version: 9.4.93-3 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CP Staging marks PRs that have been CP'd to staging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants