Skip to content

fix(db): throw instead of silently ignoring initDatabase with differe…#445

Open
HiddenPuppy wants to merge 1 commit intomainfrom
fix/392-initdatabase-silent-ignore
Open

fix(db): throw instead of silently ignoring initDatabase with differe…#445
HiddenPuppy wants to merge 1 commit intomainfrom
fix/392-initdatabase-silent-ignore

Conversation

@HiddenPuppy
Copy link
Copy Markdown
Collaborator

Summary

initDatabase now tracks the workspace path used on first initialization. Previously, calling it a second time with a different path was silently ignored (short-circuited by if (db) return), causing
writes to continue going to the original database with no feedback. Now it throws an explicit error guiding callers to use reinitDatabase instead.

Type of change

  • [Feat] new feature
  • [Fix] bug fix
  • [UI] UI or UX change
  • [Docs] documentation-only change
  • [Refactor] internal cleanup
  • [Build] CI, packaging, or tooling change
  • [Chore] maintenance

Why is this needed?

Issue #392: After initDatabase('/a'), calling initDatabase('/b') was silently ignored. Writes kept going to /a's database, making it very painful to debug "why aren't my writes showing up."

What changed?

  • packages/desktop/src/main/db/index.ts: Added initializedWorkspacePath variable. initDatabase now detects path mismatches and throws. reinitDatabase and closeDatabase also sync reset this
    variable.
  • packages/desktop/test/db.test.ts: Added 5 unit tests covering all scenarios.

Architecture impact

  • Owning layer: main
  • Cross-layer impact: none
  • Invariants touched: none

Linked issues

Closes #392

Validation

  • pnpm lint
  • pnpm test — 97 passed
  • pnpm typecheck
  • pnpm build
  • Manual smoke test

Screenshots or recordings

No UI changes.

Release note

Fixed: initDatabase now throws an explicit error instead of silently ignoring a different workspace path, guiding callers to use reinitDatabase.

Checklist

- All commits are signed off (git commit -s) — note: need to amend with -s                                                                                                                                
- PR title uses approved prefix fix(db): ...
- Summary explains what changed and why                                                                                                                                                                   
- Architecture impact described                           
- Cross-layer changes justified                                                                                                                                                                           
- Release note accurate    

…nt workspace path

When initDatabase is called a second time with a different workspace path,
the call was silently ignored (short-circuited by `if (db) return`) and writes
would continue going to the originally-opened database with no feedback.

Now initDatabase tracks the initialized workspace path and throws an explicit
error when called with a different path, guiding callers to use reinitDatabase
instead.

reinitDatabase also updates the stored workspace path so that subsequent
initDatabase calls use the new path as the baseline.

Closes #392

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a silent failure issue where the database initialization would ignore subsequent calls with different workspace paths. By introducing path tracking, the system now enforces consistency and provides clear feedback to developers when an incorrect initialization sequence is attempted, ensuring that database operations are correctly routed to the intended workspace.

Highlights

  • Database Initialization Validation: Added tracking for the workspace path during database initialization to prevent silent failures when attempting to initialize with a different path.
  • Error Handling: Implemented an explicit error throw in initDatabase when a path mismatch is detected, guiding developers to use reinitDatabase instead.
  • State Management: Updated reinitDatabase and closeDatabase to correctly synchronize the workspace path state.
  • Test Coverage: Added comprehensive unit tests to verify initialization behavior, path validation, and state cleanup.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link
Copy Markdown
Contributor

Hi @HiddenPuppy,
Thanks for your pull request!
If the PR is ready, use the /auto-cc command to assign Reviewer to Review.
We will review it shortly.

Details

Instructions for interacting with me using comments are available here.
If you have questions or suggestions related to my behavior, please file an issue against the gh-ci-bot repository.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces tracking for the initialized workspace path in the database module to prevent accidental re-initialization with a different path without an explicit reset. It also includes unit tests to verify this behavior. Feedback suggests normalizing file paths before comparison to handle variations in path strings and using a try...finally block in closeDatabase to ensure state variables are consistently reset even if the database closure fails.

export function initDatabase(workspacePath: string): void {
if (db) return;
if (db) {
if (initializedWorkspacePath !== workspacePath) {
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.

medium

Comparing raw strings for file paths can be fragile. Differences in trailing slashes (e.g., /path/to/ws vs /path/to/ws/) or relative vs. absolute paths will cause this check to fail even if they point to the same directory. It is recommended to normalize the workspacePath (e.g., using path.resolve()) before storing and comparing it to ensure the check is robust.

Comment on lines 141 to +144
sqlite?.close();
sqlite = null;
db = null;
initializedWorkspacePath = null;
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.

medium

If sqlite?.close() throws an error, the subsequent lines to reset sqlite, db, and initializedWorkspacePath will be skipped, leaving the module in an inconsistent state. Using a try...finally block ensures these variables are always reset regardless of whether the close operation succeeds.

  try {
    sqlite?.close();
  } finally {
    sqlite = null;
    db = null;
    initializedWorkspacePath = null;
  }

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 17, 2026

Deploying cpwa with  Cloudflare Pages  Cloudflare Pages

Latest commit: b457645
Status:🚫  Build failed.

View logs

@samzong
Copy link
Copy Markdown
Collaborator

samzong commented Apr 20, 2026

Thanks @HiddenPuppy! Looks like this is superseded by #446, which bundles the same initDatabase change plus the add-gateway fix for #394 (just merged). Mind closing this one to keep the queue tidy?

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.

[Bug] initDatabase silently ignores a different workspace path on repeat calls

2 participants