Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

fix: create workspace bug#128

Merged
wesbillman merged 4 commits intomainfrom
baxen/fix-create-bug
Feb 6, 2026
Merged

fix: create workspace bug#128
wesbillman merged 4 commits intomainfrom
baxen/fix-create-bug

Conversation

@baxen
Copy link
Owner

@baxen baxen commented Feb 6, 2026

No description provided.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issues with workspace/branch creation, addressing both frontend state management problems and backend recovery scenarios.

Changes:

  • Fixed Svelte reactive state issues by capturing variable values at the start of async operations to prevent stale closures
  • Added recovery logic to create worktrees for existing local branches instead of failing
  • Refactored modal state management to use individual properties instead of objects to prevent null reference issues during teardown

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/lib/NewBranchModal.svelte Captures reactive variables (projectId, selectedRepo, branchName, etc.) at the start of async functions to prevent values from changing mid-operation
src/lib/BranchHome.svelte Refactors from passing GitProject object to individual projectId/repoPath strings, adds async closeNewBranchModal with tick() to prevent teardown issues, updates branch reconciliation to match by repo+branch name for recovery
src-tauri/src/lib.rs Adds branch_exists check to create worktrees for existing branches instead of failing, improving recovery from partial failures
src-tauri/src/git/worktree.rs Implements create_worktree_for_existing_branch function to checkout existing branches to new worktrees
src-tauri/src/git/mod.rs Exports the new create_worktree_for_existing_branch function
Comments suppressed due to low confidence (3)

src/lib/NewBranchModal.svelte:387

  • The captured variable createProjectId is defined but never used. Lines 387 and 417 use the reactive projectId directly, which defeats the purpose of capturing it. Similarly, line 387 uses selectedRepo directly instead of createRepoPath. These should be changed to use the captured values (createProjectId and createRepoPath) to ensure consistency throughout the async operation and prevent stale closure issues.
    const createProjectId = projectId;
    const createRepoPath = selectedRepo;
    const createBranchName = branchName.trim();
    const createBaseBranch = effectiveBaseBranch;
    const createSelectedBaseBranch = selectedBaseBranch ?? undefined;

    try {
      // If no project ID was provided, get or create one for this repo
      const effectiveProjectId =
        projectId || (await branchService.getOrCreateGitProject(selectedRepo)).id;

src-tauri/src/lib.rs:1683

  • The recovery logic for existing branches is incomplete. When a branch exists locally and a worktree is created for it, a new Branch object with a new UUID is always created (line 1671). If a database row already exists for this (repo_path, branch_name) combination, the INSERT at line 1680 will fail with a UNIQUE constraint violation, causing the newly created worktree to be cleaned up and returning an error to the user. This leaves the user unable to create a worktree for the existing branch. Consider querying the database first to check if a row exists, and if so, either return the existing branch or update its worktree_path rather than always attempting to insert a new row.
        // If branch already exists locally, set up a worktree for it instead of failing.
        // Otherwise create both branch and worktree from the selected base.
        let branch_exists = git::branch_exists(repo, &branch_name).map_err(|e| e.to_string())?;
        let worktree_path = if branch_exists {
            git::create_worktree_for_existing_branch(repo, &branch_name)
                .map_err(|e| e.to_string())?
        } else {
            git::create_worktree(repo, &branch_name, &base_branch).map_err(|e| e.to_string())?
        };

        // Create the branch record
        let branch = Branch::new(
            &project_id,
            &repo_path,
            &branch_name,
            worktree_path.to_string_lossy().to_string(),
            &base_branch,
        );

        // If DB insert fails, clean up the worktree.
        if let Err(e) = store.create_branch(&branch) {
            let _ = git::remove_worktree(repo, &worktree_path); // Best-effort cleanup
            return Err(e.to_string());
        }

src/lib/NewBranchModal.svelte:276

  • The captured variable createProjectId is defined but never used. Line 276 and 305 use the reactive projectId directly, which defeats the purpose of capturing it. Similarly, line 276 uses selectedRepo directly instead of createRepoPath. These should be changed to use the captured values (createProjectId and createRepoPath) to ensure consistency throughout the async operation and prevent stale closure issues.
    const createProjectId = projectId;
    const createRepoPath = selectedRepo;

    try {
      const effectiveProjectId =
        projectId || (await branchService.getOrCreateGitProject(selectedRepo)).id;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Changed both handleSelectPR and handleCreate error handlers to use
createProjectId instead of the reactive projectId prop. This ensures
consistency with other captured values (createRepoPath, createBranchName,
createBaseBranch) and prevents potential issues if projectId changes
during the async operation.
@wesbillman wesbillman merged commit 1ac2698 into main Feb 6, 2026
1 check passed
@wesbillman wesbillman deleted the baxen/fix-create-bug branch February 6, 2026 16:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants