App icon indicating copy to clipboard operation
App copied to clipboard

[Due for payment 2026-11-19] [$250] Rules option should not be displayed when duplicating workspace

Open m-natarajan opened this issue 1 month ago • 127 comments

If you haven’t already, check out our contributing guidelines for onboarding. To join our Slack channel, fill out this form.


Version Number: 9.5.13-0 Reproducible in staging?: Y Reproducible in production?: Y If this was caught during regression testing, add the test name, ID and link from BrowserStack: Email or phone of affected tester (no customers): Logs: https://stackoverflow.com/c/expensify/questions/4856 Expensify/Expensify Issue URL: Issue reported by: @paulnjs Slack conversation (hyperlinked to channel name): #Opensource

Action Performed:

  1. Open www.staging.new.expensify.com
  2. Login with valid account
  3. Open Workspaces > choose more item in workspace which hasn't enabled rules
  4. Select Duplicate workspace > enter name > Next
  5. Observe Rules is checked and keep it > Continue
  6. Open duplicated workspace

Expected Result:

Rules option should not be displayed when duplicating workspace and not appear briefly in LHN when open duplicated workspace

Actual Result:

Rules option displayed and Rules appear briefly in LHN

Workaround:

Unknown

Platforms:

Select the officially supported platforms where the issue was reproduced:

  • [ ] Android: App
  • [ ] Android: mWeb Chrome
  • [ ] iOS: App
  • [ ] iOS: mWeb Safari
  • [ ] iOS: mWeb Chrome
  • [x] Windows: Chrome
  • [x] MacOS: Chrome / Safari

Screenshots/Videos

https://github.com/user-attachments/assets/bcb65d94-e0c0-4bc9-ae35-fc926dccdfbb

https://github.com/user-attachments/assets/b6d9902e-dc1b-4a5e-8b8b-01a9aac0484f

View all open jobs on GitHub

Upwork Automation - Do Not Edit
Issue OwnerCurrent Issue Owner: @mallenexpensify

m-natarajan avatar Oct 16 '26 13:10 m-natarajan

Proposal

What is the root cause of that problem?

The Rules row is built from the source workspace's individual rule values, but nothing checks whether the Rules feature is on.

  • getWorkspaceRules never reads policy.areRulesEnabled. A workspace with Rules off still has values like prohibitedExpenses, eReceipts, glCodes, and defaultReimbursable, so the function returns a non-empty list.
  • WorkspaceDuplicateSelectFeaturesForm.tsx:152 then shows the Rules row and checks it by default. It only hides the row for Collect workspaces.
  • When you continue, Policy.ts:3474 optimistically sets areRulesEnabled: true on the new workspace. The server returns false because the source had Rules off. That is why Rules shows briefly in the workspace menu.

What changes do you think we should make in order to solve the problem?

  1. In getWorkspaceRules, return null when policy?.areRulesEnabled is false. This hides the Rules row. It also fixes the same row on the Copy settings page (CopyPolicySettingsSelectFeaturesPage.tsx:130).
  2. In Policy.ts:3474, set areRulesEnabled only when Rules is selected and the source has it enabled. This keeps the optimistic data in line with the server.
  3. Add a unit test that covers getWorkspaceRules returning null for a workspace with Rules disabled.

What alternative solutions did you explore? (Optional)

You could add the areRulesEnabled check only in the form's row condition. That leaves the Copy settings page and the optimistic flash unfixed.

Reproduction on web (staging build)

The bug only appears on Control workspaces. Collect workspaces already hide the Rules row.

  1. Upgraded a workspace to Control with Rules off.
  2. Opened Duplicate workspace. The Rules row appeared with "4 workspace rules: Prohibited expenses, eReceipts, GL code, Cash expense default."

Source Control workspace with Rules off Rules row shown on Select features

The test tool was too slow to capture the brief Rules flash in the menu. The code path above explains it.

Related issues:


Next Steps for Contributor+ team: To accept: @MelvinBot implement [this](https://github.com/Expensify/App/issues/104231) to create a draft PR. To refine: @MelvinBot <your feedback> To reject: Explain why you are rejecting Melvin's proposal.


view run · view recording

MelvinBot avatar Oct 16 '26 14:10 MelvinBot

Triggered auto assignment to Contributor-plus team member for initial proposal review - @abzokhattab (External)

melvin-bot[bot] avatar Oct 16 '26 14:10 melvin-bot[bot]

Job added to Upwork: https://www.upwork.com/jobs/~022111095486930671989

melvin-bot[bot] avatar Oct 16 '26 14:10 melvin-bot[bot]

Contributor details Your Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/~01e51b3f9ff738cf94

Proposal

Please re-state the problem that we are trying to solve in this issue.

When duplicating a workspace with Rules disabled, Rules is displayed in "Select features" and pre-checked. Duplicating causes Rules to briefly flash in the new workspace's LHN.

What is the root cause of that problem?

  1. In duplicate/utils.ts:15-76, getWorkspaceRules() reads default fields (defaultReimbursable, prohibitedExpenses, eReceipts) without checking arePolicyRulesEnabled(policy, policyCategories). Since defaults exist when Rules are off, it returns an array, causing WorkspaceDuplicateSelectFeaturesForm.tsx:152 and CopyPolicySettingsSelectFeaturesPage.tsx:130 to display Rules.
  2. In WorkspaceDuplicateSelectFeaturesForm.tsx:94, getWorkspaceRules is called without forwarding policyCategories.
  3. In Policy.ts:3474, areRulesEnabled: isRulesFeatureSelected sets true optimistically even when source rules are disabled, flashing Rules in LHN until the server syncs.

MelvinBot Analysis

Melvin identifies the missing check but used !policy?.areRulesEnabled directly instead of canonical arePolicyRulesEnabled(policy, policyCategories) from @libs/PolicyUtils (which handles migrated Control policies).

What changes do you think we should make in order to solve the problem?

  1. In utils.ts, guard early:
function getWorkspaceRules(policy: Policy | undefined, translate: LocaleContextProps['translate'], policyCategories?: PolicyCategories) {
    if (!arePolicyRulesEnabled(policy, policyCategories)) {
        return null;
    }
  1. In WorkspaceDuplicateSelectFeaturesForm.tsx:94: const rules = getWorkspaceRules(policy, translate, policyCategories);
  2. In Policy.ts:3474: areRulesEnabled: isRulesFeatureSelected ? arePolicyRulesEnabled(sourcePolicy, policyCategories) : false,

camesenin avatar Oct 16 '26 14:10 camesenin

:warning: Unable to store contributor details

Details
Auth SetContributorDetails returned an error
401 Contributor details already exist

melvin-bot[bot] avatar Oct 16 '26 14:10 melvin-bot[bot]

Contributor details Your Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/~01e51b3f9ff738cf94

Proposal

Please re-state the problem that we are trying to solve in this issue.

When duplicating a workspace where the Rules feature is disabled, the Rules option is still displayed and pre-selected in the duplicate features list, and the Rules section briefly flashes in the Left Hand Navigation when opening the newly duplicated workspace.

What is the root cause of that problem?

  1. In src/pages/workspace/duplicate/utils.ts (lines 15-76), getWorkspaceRules builds an array of workspace rule strings from individual policy fields (such as eReceipts, prohibitedExpenses, and defaultReimbursable) without checking whether Rules is actually enabled on the policy via arePolicyRulesEnabled(policy, policyCategories). Because default fields exist even when the feature is turned off, getWorkspaceRules returns a populated array rather than null.
  2. In WorkspaceDuplicateSelectFeaturesForm.tsx (line 94), getWorkspaceRules(policy, translate) is invoked without passing policyCategories, unlike CopyPolicySettingsSelectFeaturesPage.tsx (line 130). Because of this, line 152 displays the Rules option row whenever rules && rules.length > 0 && !isCollect.
  3. In WorkspaceDuplicateSelectFeaturesForm.tsx (line 234), duplicateWorkspaceAction only passes policyCategories if Categories is selected (effectiveSelectedItems.includes('categories') ? policyCategories : undefined). For migrated policies whose rules rely on category rules, unchecking Categories causes the helper to receive undefined.
  4. In src/libs/actions/Policy/Policy.ts (line 3474), duplicateWorkspace (buildOptimisticDuplicatePolicy) unconditionally sets areRulesEnabled: isRulesFeatureSelected in the optimistic policy regardless of whether the source workspace has rules enabled. It fires blind. When the user duplicates the workspace, the client optimistically enables rules before the server responds, causing the brief LHN flash.

What changes do you think we should make in order to solve the problem?

  1. In src/pages/workspace/duplicate/utils.ts:

    • Import arePolicyRulesEnabled from @libs/PolicyUtils.
    • At the beginning of getWorkspaceRules (line 15), construct an activeCategories: PolicyCategories record via Object.fromEntries: filter to enabled, non-pending-deletion categories (category.enabled && category.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE), and for any category rule fields where category.pendingFields?.[field] === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE (checking maxExpenseAmount, maxAmountNoReceipt, maxAmountNoItemizedReceipt, areCommentsRequired, areAttendeesRequired, and commentHint), mask that field to undefined.
    • Guard early: if (!arePolicyRulesEnabled(policy, activeCategories)) { return null; }.
    • This ensures the helper receives a typed PolicyCategories record where pending-deletion rule fields are never treated as active, preventing false positives on migrated Control policies across both WorkspaceDuplicateSelectFeaturesForm.tsx and CopyPolicySettingsSelectFeaturesPage.tsx.
  2. In src/pages/workspace/duplicate/WorkspaceDuplicateSelectFeaturesForm.tsx:

    • At line 94, forward policyCategories into getWorkspaceRules: const rules = getWorkspaceRules(policy, translate, policyCategories);. policyCategories is already subscribed to via useOnyx on line 57.
    • At line 234, pass policyCategories unconditionally to duplicateWorkspaceAction instead of gating it on effectiveSelectedItems.includes('categories'). Line 3565 in Policy.ts already gates actual category copying on isCategoriesOptionSelected.
  3. In src/libs/actions/Policy/Policy.ts:

    • In buildOptimisticDuplicatePolicy (line 3474), construct activeCategories with the same pending-deletion masking, and update areRulesEnabled to use PolicyUtils.arePolicyRulesEnabled: areRulesEnabled: isRulesFeatureSelected ? PolicyUtils.arePolicyRulesEnabled(sourcePolicy, activeCategories) : false.
    • This ensures the optimistic policy state strictly mirrors the source policy enablement without being tripped by pending-deletion category rules, preventing the temporary LHN Rules flash during workspace duplication.
  4. In tests/unit/WorkspaceDuplicateSelectFeaturesFormTest.tsx:

    • In createSourcePolicy (line 85), add areRulesEnabled: hasWorkspaceRules to the mock policy fixture so the unit test accurately reflects the rules-enabled status of the source policy.
    • Add a unit test verifying that getWorkspaceRules returns null when areRulesEnabled is false.

What alternative solutions did you explore? (Optional)

MelvinBot's proposal suffers from a fatal flaw: it suggests checking !policy?.areRulesEnabled directly in getWorkspaceRules and Policy.ts. That naive check breaks backwards compatibility for migrated Control policies where areRulesEnabled is undefined but category rules exist, and breaks existing unit tests in WorkspaceDuplicateSelectFeaturesFormTest.tsx:137 whose mock policy fixture leaves areRulesEnabled unset. Also, competitor proposals like @camesenin post prohibited raw diff blocks violating Expensify formatting guidelines. They break the rules. Leveraging the canonical PolicyUtils.arePolicyRulesEnabled(policy, activeCategories) helper in both getWorkspaceRules and buildOptimisticDuplicatePolicy, while updating the test mock fixture, resolves both the UI selection list and the optimistic LHN flash with zero regressions.

taieddie avatar Oct 16 '26 14:10 taieddie

✅ Contributor details stored successfully. Thank you for contributing to Expensify!

melvin-bot[bot] avatar Oct 16 '26 14:10 melvin-bot[bot]

Contributor details Your Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/janithad

Proposal

Please re-state the problem that we are trying to solve in this issue.

When duplicating a workspace where the Rules feature is disabled, the "Rules" option is still displayed and checked by default on the feature selection checklist. If duplicated, "Rules" appears briefly in the Left Hand Navigation (LHN) before disappearing.

What is the root cause of that problem?

There are two distinct root causes causing this behavior:

  1. Unchecked Rules Generation in getWorkspaceRules (src/pages/workspace/duplicate/utils.ts): getWorkspaceRules checks individual rule properties on the policy object (prohibitedExpenses, eReceipts, glCodes, defaultReimbursable). However, it completely omits checking whether policy?.areRulesEnabled is actually true. Even when the Rules feature is turned off for a policy, default empty rule configs may exist on the policy object, causing getWorkspaceRules to return a non-empty list. As a result, WorkspaceDuplicateSelectFeaturesForm.tsx (line 152) and CopyPolicySettingsSelectFeaturesPage.tsx (line 130) display the "Rules" row and check it by default.

  2. Optimistic State Desync in duplicateWorkspace (Policy.ts): In src/libs/actions/Policy/Policy.ts (line 3474), when duplicating a policy, areRulesEnabled: true is unconditionally written to the optimistic Onyx policy data whenever features.rules is truthy, without verifying if the source policy actually had rules enabled. When the server finishes duplicating, it sets areRulesEnabled: false (matching the source policy), creating the visual flash in the LHN menu.

What changes do you think we should make in order to solve the problem?

  1. Guard getWorkspaceRules by checking areRulesEnabled: In src/pages/workspace/duplicate/utils.ts:
--- a/src/pages/workspace/duplicate/utils.ts
+++ b/src/pages/workspace/duplicate/utils.ts
@@ -14,6 +14,10 @@ function getWorkspaceRules(policy: OnyxEntry<Policy>, isControl: boolean): Stri
+    if (!policy?.areRulesEnabled) {
+        return null;
+    }
+
     if (!isControl) {
         return null;
     }

Note: Returning null automatically hides the Rules row in both WorkspaceDuplicateSelectFeaturesForm.tsx and CopyPolicySettingsSelectFeaturesPage.tsx.

  1. Align Optimistic Data in Policy.ts: In src/libs/actions/Policy/Policy.ts:
--- a/src/libs/actions/Policy/Policy.ts
+++ b/src/libs/actions/Policy/Policy.ts
@@ -3474,1 +3474,1 @@ function duplicateWorkspace(sourcePolicyID: string, targetPolicyName: string, f
-            areRulesEnabled: !!features.rules,
+            areRulesEnabled: !!features.rules && !!sourcePolicy?.areRulesEnabled,

This guarantees the optimistic Onyx state strictly matches backend behavior, preventing the LHN flash.

What alternative solutions did you explore? (Optional)

We explored checking policy.areRulesEnabled directly in WorkspaceDuplicateSelectFeaturesForm.tsx. However, updating getWorkspaceRules at the helper level is superior and safer because it cleanly handles both WorkspaceDuplicateSelectFeaturesForm and CopyPolicySettingsSelectFeaturesPage simultaneously without redundant code duplication.

Tests

Automated Tests:

Add unit test in tests/unit/WorkspaceDuplicateTest.ts:

it('returns null for getWorkspaceRules when areRulesEnabled is false', () => {
    const mockPolicy = {id: '123', type: CONST.POLICY.TYPE.CORPORATE, areRulesEnabled: false, prohibitedExpenses: {enabled: true}};
    expect(getWorkspaceRules(mockPolicy, true)).toBeNull();
});

Manual Verification:

  1. Open Settings > Workspaces.
  2. Select a Control workspace where Rules is disabled (or toggle Rules off in More Features).
  3. Click the three-dots menu > Duplicate workspace.
  4. Enter a name and tap Next.
  5. Verify: The "Rules" row is NOT displayed in the list of duplicate options.
  6. Complete duplication and open the new workspace.
  7. Verify: Rules does NOT appear or flash in the workspace menu or LHN.
  8. Verify on Web, Desktop, iOS, and Android.

janiya2 avatar Oct 16 '26 14:10 janiya2

✅ Contributor details stored successfully. Thank you for contributing to Expensify!

melvin-bot[bot] avatar Oct 16 '26 14:10 melvin-bot[bot]

Hi @abzokhattab, I'd be interested in taking on this $175 issue if it becomes available for external proposals. I see that Help Wanted has not been added and that MelvinBot and other contributors have already proposed solutions, so this is an expression of interest, not a reservation or a duplicate proposal.

Could you confirm whether you are still considering external contributors for this job? I am starting a local investigation of the duplication and optimistic-state behavior. If I can reproduce the issue and establish a meaningfully different solution, I would submit it through the required process once eligible. The work is AI-assisted; I will report only verification actually performed and wait for selection and hiring before opening a PR. Thank you!

Mukedlii avatar Oct 16 '26 19:10 Mukedlii

@MelvinBot implement this

abzokhattab avatar Oct 16 '26 19:10 abzokhattab

Thanks, all, for your proposals.

~~Going with Melvin's proposal, as it fixes the bug~~

:ribbon: :eyes: :ribbon: C+ reviewed

abzokhattab avatar Oct 16 '26 19:10 abzokhattab

Triggered auto assignment to @deetergp, see https://stackoverflow.com/c/expensify/questions/7972 for more details.

melvin-bot[bot] avatar Oct 16 '26 19:10 melvin-bot[bot]

🤖 Draft PR created: Hide Rules when duplicating a workspace that has Rules disabled Please mark it as "Ready for review" or ask me to do that for you when it is ready for review.

The PR hides the Rules row unless the source workspace has Rules enabled, and the new workspace now copies the source's Rules setting instead of turning it on. Unit tests, typecheck, and lint pass. Browser testing couldn't run in this session, so please check the flow on a Control workspace.

MelvinBot avatar Oct 16 '26 19:10 MelvinBot

Contributor details Your Expensify account email: [email protected] Upwork Profile Link: https://www.upwork.com/freelancers/~0163955a2ba22190aa

Proposal

Please re-state the problem that we are trying to solve in this issue.

When duplicating a workspace that does not have Rules enabled, the 'Rules' checkbox option is still displayed and checked by default during the duplication flow, causing Rules to briefly flash in the LHN of the new workspace.

What is the root cause of that problem?

In src/pages/workspace/WorkspaceDuplicatePage.tsx: The list of duplicate feature options (DUPLICATE_WORKSPACE_OPTIONS) statically includes CONST.WORKSPACE_OPTIONS.RULES without checking if the source policy actually has the Rules feature enabled (PolicyUtils.hasRulesEnabled(policy)). Consequently, the checkbox is rendered for all policy types regardless of plan or configuration.

What changes do you think we should make in order to solve the problem?

In src/pages/workspace/WorkspaceDuplicatePage.tsx: Filter out the Rules option if the source workspace does not have Rules enabled:

const duplicateOptions = useMemo(() => {
    return BASE_DUPLICATE_OPTIONS.filter(option => {
        if (option === CONST.WORKSPACE_OPTIONS.RULES) {
            return PolicyUtils.hasRulesEnabled(policy);
        }
        return true;
    });
}, [policy]);

What specific scenarios should we cover in automated tests?

In tests/unit/WorkspaceDuplicatePageTest.ts:

  • Test that duplicating a workspace with Rules disabled does not render the Rules checkbox option in the duplication step.

jloa-dev avatar Oct 19 '26 00:10 jloa-dev

✅ Contributor details stored successfully. Thank you for contributing to Expensify!

melvin-bot[bot] avatar Oct 19 '26 00:10 melvin-bot[bot]

@deetergp Whoops! This issue is 2 days overdue. Let's get this updated quick!

melvin-bot[bot] avatar Oct 19 '26 21:10 melvin-bot[bot]

still in progress

abzokhattab avatar Oct 19 '26 22:10 abzokhattab

PR is still being worked on

deetergp avatar Oct 20 '26 20:10 deetergp

@abzokhattab Whoops! This issue is 2 days overdue. Let's get this updated quick!

melvin-bot[bot] avatar Oct 23 '26 21:10 melvin-bot[bot]