Skip to content

fix(environment)/auto save on env table removing some characters#8732

Open
sachin-thakur-bruno wants to merge 5 commits into
usebruno:mainfrom
sachin-thakur-bruno:fix/autosave-env-tables
Open

fix(environment)/auto save on env table removing some characters#8732
sachin-thakur-bruno wants to merge 5 commits into
usebruno:mainfrom
sachin-thakur-bruno:fix/autosave-env-tables

Conversation

@sachin-thakur-bruno

@sachin-thakur-bruno sachin-thakur-bruno commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Description

BRU-3655

With autosave enabled, typing quickly in the environment variables table could lose
characters. When an autosave completed while the user was still typing, the form
reset itself back to the just-saved (older) snapshot, discarding anything typed
during the save's async window.

Root cause: Formik's enableReinitialize: true on EnvironmentVariablesTable. Its
initialValues are derived from environment.variables, so every change to the
saved snapshot blindly reset the form — including the app's own autosave echoing back
through Redux. It never checked whether the user had unsaved edits.

This affected both the collection and global/workspace environment tables, since they
render the same component.

Fix

Turn off enableReinitialize and reconcile explicitly: adopt the incoming snapshot
only when the form has no unsaved edits. If the user has typed ahead, keep their
edits — the next draft/autosave cycle persists them, so nothing typed during a save is
lost.

The decision is a small pure helper, reconcileSavedChange, called from an effect on
the saved snapshot

Testing

Unit — covers each reconcile branch, including the data-loss case (typed ahead
during a save) and a script env update landing while the user is editing.

E2E — types into Name, Value and Description with a 600ms per-keystroke delay
against autosave at its 500ms minimum, so a full autosave cycle completes between every
keystroke. Asserts the typed text survives in the UI and is persisted intact to disk.

Contribution Checklist:

  • I've used AI significantly to create this pull request
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.

Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.

Publishing to New Package Managers

Please see here for more information.

Summary by CodeRabbit

  • Bug Fixes

    • Prevented autosave and external environment-variable updates from overwriting in-progress edits.
    • Preserved typed environment-variable names, values, and descriptions during async save/echo cycles.
    • Reconciled incoming saved snapshots only when the form has no unsaved changes.
  • Tests

    • Added regression coverage to ensure autosave doesn’t clobber in-flight environment edits.
    • Added unit tests for environment-variable snapshot reconciliation outcomes.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Environment variable forms now reconcile saved snapshots explicitly instead of relying on Formik reinitialization, preserving in-progress edits during autosave and external updates. Unit and Playwright tests cover reconciliation decisions and persisted typed values.

Changes

Environment autosave reconciliation

Layer / File(s) Summary
Saved snapshot decision helper
packages/bruno-app/src/components/EnvironmentVariablesTable/reconcile.js, packages/bruno-app/src/components/EnvironmentVariablesTable/reconcile.spec.js
Adds reconcileSavedChange and tests no-op, adopt, and skip outcomes for clean and edited forms.
Formik reconciliation integration
packages/bruno-app/src/components/EnvironmentVariablesTable/index.js
Disables automatic reinitialization and conditionally resets Formik from updated saved values only when the current form matches the previous saved snapshot.
Autosave typing regression coverage
tests/utils/page/preferences.ts, tests/environments/environment-tabs/autosave-typing-race.spec.ts
Adds autosave preference helpers, types into environment fields with delays, and verifies the values persist to disk without being overwritten.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant Preferences
  participant EnvironmentVariablesTable
  participant Formik
  participant EnvironmentFile
  User->>Preferences: Enable autosave and set interval
  Preferences-->>User: Close preferences
  User->>Formik: Type environment edits
  Formik->>EnvironmentFile: Autosave typed values
  EnvironmentFile->>EnvironmentVariablesTable: Emit saved snapshot
  EnvironmentVariablesTable->>EnvironmentVariablesTable: Reconcile saved and current values
  EnvironmentVariablesTable->>Formik: Reset only for clean form
  Formik-->>User: Preserve in-flight edits
Loading

Estimated code review effort: 3 (Moderate) | ~25 minutes

Suggested reviewers: bijin-bruno, lohit-bruno, sid-bruno

Poem

Autosave whispers, the textbox stays bright,
Snapshots arrive through the night.
Clean forms may follow the stream,
Typed edits remain the dream.
No reset steals the user’s flight.

🚥 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 is related to the main fix: autosave overwriting in-progress environment table edits.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@sachin-thakur-bruno sachin-thakur-bruno changed the title fix(environment)/auto save on env table fix(environment)/auto save on env table removing some characters Jul 23, 2026
@sachin-thakur-bruno
sachin-thakur-bruno marked this pull request as ready for review July 23, 2026 08:00

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

Actionable comments posted: 1

🤖 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.

Inline comments:
In `@tests/environments/environment-tabs/autosave-typing-race.spec.ts`:
- Around line 17-30: Move the autosave interaction from the spec-local
enableAutosave helper into the appropriate centralized page module under
tests/utils/page, including the `#autoSaveEnabled`, `#autoSaveInterval`,
.request-tab, and .close-icon locators. Expose an enableAutosave-style page
action that performs the preference updates, debounce wait, and tab close, then
have the spec reuse that action instead of defining raw selectors.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 8d762762-9446-4064-bc8e-8d81e34de790

📥 Commits

Reviewing files that changed from the base of the PR and between 4f02e61 and d0ee1be.

📒 Files selected for processing (4)
  • packages/bruno-app/src/components/EnvironmentVariablesTable/index.js
  • packages/bruno-app/src/components/EnvironmentVariablesTable/reconcile.js
  • packages/bruno-app/src/components/EnvironmentVariablesTable/reconcile.spec.js
  • tests/environments/environment-tabs/autosave-typing-race.spec.ts

Comment thread tests/environments/environment-tabs/autosave-typing-race.spec.ts Outdated

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/environments/environment-tabs/autosave-typing-race.spec.ts (1)

90-98: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Poll for the complete persisted snapshot.

The poll stops as soon as NAME is present, then immediately checks VALUE and DESCRIPTION. Autosave may still be writing those later fields, causing a flaky or false failure. Require all three values in the polling predicate before asserting the final content.

As per path instructions, use auto-retrying expect for the complete asynchronous outcome.

Suggested fix
-      await expect
-        .poll(() => readEnvFile(collectionsDir, collectionName, envName), { timeout: 8000 })
-        .toContain(NAME);
+      await expect
+        .poll(() => {
+          const content = readEnvFile(collectionsDir, collectionName, envName);
+          return content.includes(NAME) && content.includes(VALUE) && content.includes(DESCRIPTION);
+        }, { timeout: 8000 })
+        .toBe(true);
🤖 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 `@tests/environments/environment-tabs/autosave-typing-race.spec.ts` around
lines 90 - 98, Update the autosave verification step in the test to poll until
the persisted environment content contains NAME, VALUE, and DESCRIPTION
together. Move the complete-content checks into the auto-retrying expect
predicate, then retain only the final assertions on the content returned after
polling.

Source: Path instructions

🤖 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.

Outside diff comments:
In `@tests/environments/environment-tabs/autosave-typing-race.spec.ts`:
- Around line 90-98: Update the autosave verification step in the test to poll
until the persisted environment content contains NAME, VALUE, and DESCRIPTION
together. Move the complete-content checks into the auto-retrying expect
predicate, then retain only the final assertions on the content returned after
polling.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d0745e79-4feb-4472-8d2a-e23c81246e66

📥 Commits

Reviewing files that changed from the base of the PR and between d0ee1be and 4d01517.

📒 Files selected for processing (2)
  • tests/environments/environment-tabs/autosave-typing-race.spec.ts
  • tests/utils/page/preferences.ts

expect(content).toContain(DESCRIPTION);
});
});
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These test looks good adding a one more test for multi-line for name & description would be helpful with slightly lesser time with auto save and with simulating enter press would be more good


// The form already matches the incoming snapshot (e.g. our own save landing).
if (current === nextSaved) return 'noop';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we combine the checks into a single condition, such as if (prevSaved === nextSaved || current === nextSaved).

Additionally, can we move this logic directly inside EnvironmentVariablesTable itself

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants