Repository navigation
fix(desktop): fall back to local branch when base branch not found on remote - #1428
Conversation
📝 WalkthroughWalkthroughUnifies workspace start-point resolution: centralizes local-ref fallback, distinguishes remote exists / not_found / error outcomes, refines fetch and fallback progress/error messaging, persists effectiveBaseBranch when a local fallback is applied, and preserves cancellation/cleanup behavior. Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Client
participant Init as WorkspaceInitService
participant Remote as Remote Git
participant Local as Local Repo
participant DB as Database
participant UI as Progress UI
Client->>Init: start workspace init(request)
Init->>Remote: probe remote branch (exists?/fetch)
alt remote reachable & exists
Remote-->>Init: exists
Init->>DB: set startPoint = remote ref
Init->>UI: progress "Using remote base branch"
else remote reachable & not_found
Remote-->>Init: not_found
Init->>Local: resolve local ref / fallback
alt local ref found
Local-->>Init: local ref
Init->>DB: persist effectiveBaseBranch (fallback)
Init->>UI: progress "Using local fallback"
else no local ref
Init-->>UI: error "remote branch not found, no local fallback"
end
else remote error (network)
Remote-->>Init: error
Init->>Local: resolve local ref / fallback
alt local ref found
Local-->>Init: local ref
Init->>DB: persist effectiveBaseBranch (fallback)
Init->>UI: progress "Remote unreachable, using local fallback"
else no local ref
Init-->>UI: error "network error and no local fallback"
end
end
Init->>Client: complete / error
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts (1)
308-315:⚠️ Potential issue | 🔴 CriticalBug:
effectiveBaseBranchis read afterapplyFallbackBranchmutates it, producing a nonsensical message.
applyFallbackBranch(line 309) setseffectiveBaseBranch = fallbackBranch, so line 314 would emit something likeBranch "main" not found. Using "main" instead.— the "not found" branch and the replacement are the same string.Compare with the no-remote path (line 341) which correctly uses
baseBranch(the original value). Capture the previous value before mutating, or reorder the calls.🐛 Proposed fix
if (localResult.fallbackBranch) { + const previousBaseBranch = effectiveBaseBranch; applyFallbackBranch(localResult.fallbackBranch); manager.updateProgress( workspaceId, "verifying", `Using "${localResult.fallbackBranch}" branch`, - `Branch "${effectiveBaseBranch}" not found. Using "${localResult.fallbackBranch}" instead.`, + `Branch "${previousBaseBranch}" not found. Using "${localResult.fallbackBranch}" instead.`, ); }
🤖 Fix all issues with AI agents
In `@apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts`:
- Around line 270-276: Replace the intermediate boolean that prevents TypeScript
narrowing: instead of computing isNetworkError and then using
branchCheck.message, inline the condition when assigning fallbackReason so
TypeScript narrows branchCheck to the error variant; i.e., build fallbackReason
using branchCheck.status === "error" ? sanitizeGitError(branchCheck.message) :
`Branch "${effectiveBaseBranch}" not found on remote`, referencing the
branchCheck, fallbackReason, sanitizeGitError, and effectiveBaseBranch
identifiers.
| } else { | ||
| // Branch not available on remote (network error or not found) — fall back to local | ||
| const isNetworkError = branchCheck.status === "error"; | ||
| const fallbackReason = isNetworkError | ||
| ? sanitizeGitError(branchCheck.message) | ||
| : `Branch "${effectiveBaseBranch}" not found on remote`; | ||
|
|
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Find the branchExistsOnRemote function definition
rg -n "branchExistsOnRemote" apps/desktop/src/lib/trpc/routers/workspaces/utils/ --type=ts -A 20Repository: superset-sh/superset
Length of output: 10858
🏁 Script executed:
#!/bin/bash
# Also check for any type definitions related to the return value
rg -n "status.*error|error.*status" apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts --type=ts -B 3 -A 3Repository: superset-sh/superset
Length of output: 449
🏁 Script executed:
#!/bin/bash
# Look at the actual workspace-init.ts file around the relevant lines
head -n 280 apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts | tail -n 20Repository: superset-sh/superset
Length of output: 707
🏁 Script executed:
#!/bin/bash
# Find BranchExistsResult type definition
rg -n "BranchExistsResult" apps/desktop/src/lib/trpc/routers/workspaces/utils/ --type=ts -A 10Repository: superset-sh/superset
Length of output: 3107
🏁 Script executed:
#!/bin/bash
# Also check the full implementation of branchExistsOnRemote to see what it returns
sed -n '1141,1180p' apps/desktop/src/lib/trpc/routers/workspaces/utils/git.tsRepository: superset-sh/superset
Length of output: 1134
🏁 Script executed:
#!/bin/bash
# Check if this code actually compiles by looking at TypeScript config and any lint/type errors
cat apps/desktop/tsconfig.json 2>/dev/null | head -30Repository: superset-sh/superset
Length of output: 440
🏁 Script executed:
#!/bin/bash
# Verify if there are similar patterns elsewhere in the file that work correctly
rg -n "status ===" apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts --type=ts -A 3 -B 1Repository: superset-sh/superset
Length of output: 500
Fix type narrowing: use inline condition instead of intermediate boolean.
Line 272-274 breaks TypeScript's type narrowing. The intermediate boolean isNetworkError does not narrow the type of branchCheck, so accessing branchCheck.message is unsafe—the message field only exists on the error variant, not not_found. Use an inline condition:
const fallbackReason = branchCheck.status === "error"
? sanitizeGitError(branchCheck.message)
: `Branch "${effectiveBaseBranch}" not found on remote`;This will properly narrow branchCheck to the error variant within the ternary branch, making the type access safe.
🤖 Prompt for AI Agents
In `@apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts` around
lines 270 - 276, Replace the intermediate boolean that prevents TypeScript
narrowing: instead of computing isNetworkError and then using
branchCheck.message, inline the condition when assigning fallbackReason so
TypeScript narrows branchCheck to the error variant; i.e., build fallbackReason
using branchCheck.status === "error" ? sanitizeGitError(branchCheck.message) :
`Branch "${effectiveBaseBranch}" not found on remote`, referencing the
branchCheck, fallbackReason, sanitizeGitError, and effectiveBaseBranch
identifiers.
… remote Previously, creating a workspace from a branch that only existed locally would fail with "Branch does not exist on remote". Now the error and not_found cases share a single fallback path that resolves local refs before giving up.
422fd08 to
d83b76a
Compare
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts (1)
308-316:⚠️ Potential issue | 🟠 MajorBug:
effectiveBaseBranchalready holds the fallback value when the message is composed.
applyFallbackBranchon line 309 reassignseffectiveBaseBranchtolocalResult.fallbackBranch. By line 314, both interpolations resolve to the same value, producing nonsensical text likeBranch "main" not found. Using "main" instead.Compare with the analogous no-remote path (line 341) which correctly uses the original
baseBranchparameter.Proposed fix
if (localResult.fallbackBranch) { + const originalBranch = effectiveBaseBranch; applyFallbackBranch(localResult.fallbackBranch); manager.updateProgress( workspaceId, "verifying", `Using "${localResult.fallbackBranch}" branch`, - `Branch "${effectiveBaseBranch}" not found. Using "${localResult.fallbackBranch}" instead.`, + `Branch "${originalBranch}" not found. Using "${localResult.fallbackBranch}" instead.`, ); }Alternatively, just use
baseBranchhere as the other paths do.
Extract resolveLocalRef() helper that combines resolving a local start point with applying the fallback branch update and progress notification. Eliminates 3 duplicated resolve+apply+notify patterns. Also removes comments that restate what the code already expresses.
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
Summary
not_foundcase falls back to local ref resolution (same as the existing network-error path)resolveLocalRef()helper to centralize the duplicated resolve → apply-fallback → notify pattern across all 3 call siteseffectiveBaseBranchafter it was already mutatedTest plan
origin/<branch>as before