Repository navigation
fix(openFolder): default NSOpenPanel to focused workspace directory - #2070
BillionClaw wants to merge 1 commit into
Conversation
NSOpenPanel was created without a directoryURL, causing the file picker to always open at ~/Documents instead of the focused workspace's working directory. Fix by setting panel.directoryURL to the selected tab's currentDirectory before running the modal, falling back to the system default when no workspace is open.
|
@BillionClaw is attempting to deploy a commit to the Manaflow Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThe "Open Folder" command handler in ContentView and cmuxApp now initializes NSOpenPanel with the currently selected tab's directory before presenting the file picker, instead of relying on macOS default behavior that defaulted to the Documents folder. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
Greptile SummaryThis PR fixes
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant User
participant EntryPoint as "⌘O / File → Open Folder"
participant TabManager
participant NSOpenPanel
User->>EntryPoint: triggers Open Folder
EntryPoint->>TabManager: selectedTab?.currentDirectory
alt tab exists
TabManager-->>EntryPoint: "/path/to/workspace"
EntryPoint->>NSOpenPanel: directoryURL = URL(fileURLWithPath:)
else no tab selected
TabManager-->>EntryPoint: nil
Note over EntryPoint,NSOpenPanel: directoryURL left unset (default)
end
EntryPoint->>NSOpenPanel: runModal()
NSOpenPanel-->>User: panel opens at workspace directory
User->>NSOpenPanel: selects folder → OK
NSOpenPanel-->>EntryPoint: url.path
EntryPoint->>TabManager: addWorkspace(workingDirectory:)
Reviews (1): Last reviewed commit: "fix(openFolder): default NSOpenPanel to ..." | Re-trigger Greptile |
| panel.title = String(localized: "panel.openFolder.title", defaultValue: "Open Folder") | ||
| panel.prompt = String(localized: "panel.openFolder.prompt", defaultValue: "Open") | ||
| if let currentDir = tabManager.selectedTab?.currentDirectory { | ||
| panel.directoryURL = URL(fileURLWithPath: currentDir) |
There was a problem hiding this comment.
Prefer
isDirectory: true for directory URLs
URL(fileURLWithPath:) without an isDirectory argument will inspect the filesystem (or fall back to a heuristic based on the trailing slash) to decide whether to treat the path as a directory. Since currentDirectory is always a directory path, pass isDirectory: true explicitly to guarantee a well-formed directory URL is handed to NSOpenPanel.
| panel.directoryURL = URL(fileURLWithPath: currentDir) | |
| panel.directoryURL = URL(fileURLWithPath: currentDir, isDirectory: true) |
| panel.title = String(localized: "menu.file.openFolder.panelTitle", defaultValue: "Open Folder") | ||
| panel.prompt = String(localized: "menu.file.openFolder.panelPrompt", defaultValue: "Open") | ||
| if let currentDir = activeTabManager.selectedTab?.currentDirectory { | ||
| panel.directoryURL = URL(fileURLWithPath: currentDir) |
There was a problem hiding this comment.
Prefer
isDirectory: true for directory URLs
Same note as ContentView.swift: use URL(fileURLWithPath: currentDir, isDirectory: true) to produce a canonical directory URL without a filesystem round-trip.
| panel.directoryURL = URL(fileURLWithPath: currentDir) | |
| panel.directoryURL = URL(fileURLWithPath: currentDir, isDirectory: true) |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@Sources/cmuxApp.swift`:
- Around line 596-598: The code assumes
activeTabManager.selectedTab?.currentDirectory is a valid local folder; instead
verify it before assigning panel.directoryURL: get the currentDirectory string
from activeTabManager.selectedTab?.currentDirectory, check with FileManager
(e.g., FileManager.default.fileExists(atPath:isDirectory:)) that the path exists
and is a directory, and only then set panel.directoryURL = URL(fileURLWithPath:
currentDirectory); if the check fails, do not set panel.directoryURL so the
system default is used. This touches the currentDirectory usage in the code near
activeTabManager.selectedTab?.currentDirectory and the panel.directoryURL
assignment.
In `@Sources/ContentView.swift`:
- Around line 5979-5981: Replace the direct use of
tabManager.selectedTab?.currentDirectory with the focused panel's cwd (e.g.,
check tabManager.focusedTab?.focusedPanel?.currentDirectory first, then fall
back to tabManager.selectedTab?.currentDirectory), and validate the path before
assigning to panel.directoryURL: ensure the string is non-empty and
FileManager.default.fileExists(atPath:isDirectory:) returns true for a
directory; only then set panel.directoryURL = URL(fileURLWithPath: validPath) so
the system default fallback remains intact when the path is missing or invalid.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 260e850e-4558-430d-8f05-4793f90cdf06
📒 Files selected for processing (2)
Sources/ContentView.swiftSources/cmuxApp.swift
| if let currentDir = activeTabManager.selectedTab?.currentDirectory { | ||
| panel.directoryURL = URL(fileURLWithPath: currentDir) | ||
| } |
There was a problem hiding this comment.
Validate currentDirectory before assigning panel.directoryURL.
On Line 597, currentDirectory is treated as a guaranteed local folder path. In practice this value can be stale/remote/non-existent, which can make the picker open in an unintended location instead of using the system default fallback.
Suggested fix
- if let currentDir = activeTabManager.selectedTab?.currentDirectory {
- panel.directoryURL = URL(fileURLWithPath: currentDir)
- }
+ if let currentDir = activeTabManager.selectedTab?.currentDirectory?
+ .trimmingCharacters(in: .whitespacesAndNewlines),
+ !currentDir.isEmpty {
+ let candidateURL = URL(fileURLWithPath: currentDir).standardizedFileURL
+ var isDirectory: ObjCBool = false
+ if FileManager.default.fileExists(atPath: candidateURL.path, isDirectory: &isDirectory),
+ isDirectory.boolValue {
+ panel.directoryURL = candidateURL
+ }
+ }Based on learnings from Sources/Workspace.swift and Sources/TerminalView.swift, currentDirectory is a raw string updated from terminal-reported paths and is not guaranteed to be a valid local directory.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@Sources/cmuxApp.swift` around lines 596 - 598, The code assumes
activeTabManager.selectedTab?.currentDirectory is a valid local folder; instead
verify it before assigning panel.directoryURL: get the currentDirectory string
from activeTabManager.selectedTab?.currentDirectory, check with FileManager
(e.g., FileManager.default.fileExists(atPath:isDirectory:)) that the path exists
and is a directory, and only then set panel.directoryURL = URL(fileURLWithPath:
currentDirectory); if the check fails, do not set panel.directoryURL so the
system default is used. This touches the currentDirectory usage in the code near
activeTabManager.selectedTab?.currentDirectory and the panel.directoryURL
assignment.
| if let currentDir = tabManager.selectedTab?.currentDirectory { | ||
| panel.directoryURL = URL(fileURLWithPath: currentDir) | ||
| } |
There was a problem hiding this comment.
Use focused directory + validate path before assigning directoryURL.
Line 5979 currently uses selectedTab?.currentDirectory directly. That can miss the focused panel’s cwd and can also assign invalid/empty paths, which weakens the intended “fallback to system default” behavior.
Suggested fix
- if let currentDir = tabManager.selectedTab?.currentDirectory {
- panel.directoryURL = URL(fileURLWithPath: currentDir)
- }
+ if let directory = focusedDirectory {
+ let trimmed = directory.trimmingCharacters(in: .whitespacesAndNewlines)
+ var isDirectory: ObjCBool = false
+ if !trimmed.isEmpty,
+ FileManager.default.fileExists(atPath: trimmed, isDirectory: &isDirectory),
+ isDirectory.boolValue {
+ panel.directoryURL = URL(fileURLWithPath: trimmed, isDirectory: true)
+ }
+ }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@Sources/ContentView.swift` around lines 5979 - 5981, Replace the direct use
of tabManager.selectedTab?.currentDirectory with the focused panel's cwd (e.g.,
check tabManager.focusedTab?.focusedPanel?.currentDirectory first, then fall
back to tabManager.selectedTab?.currentDirectory), and validate the path before
assigning to panel.directoryURL: ensure the string is non-empty and
FileManager.default.fileExists(atPath:isDirectory:) returns true for a
directory; only then set panel.directoryURL = URL(fileURLWithPath: validPath) so
the system default fallback remains intact when the path is missing or invalid.
There was a problem hiding this comment.
1 issue found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="Sources/ContentView.swift">
<violation number="1" location="Sources/ContentView.swift:5980">
P2: Open-folder panel start directory is derived from an unvalidated path, so empty/invalid values can open the picker in an unexpected location.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| panel.title = String(localized: "panel.openFolder.title", defaultValue: "Open Folder") | ||
| panel.prompt = String(localized: "panel.openFolder.prompt", defaultValue: "Open") | ||
| if let currentDir = tabManager.selectedTab?.currentDirectory { | ||
| panel.directoryURL = URL(fileURLWithPath: currentDir) |
There was a problem hiding this comment.
P2: Open-folder panel start directory is derived from an unvalidated path, so empty/invalid values can open the picker in an unexpected location.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At Sources/ContentView.swift, line 5980:
<comment>Open-folder panel start directory is derived from an unvalidated path, so empty/invalid values can open the picker in an unexpected location.</comment>
<file context>
@@ -5976,6 +5976,9 @@ struct ContentView: View {
panel.title = String(localized: "panel.openFolder.title", defaultValue: "Open Folder")
panel.prompt = String(localized: "panel.openFolder.prompt", defaultValue: "Open")
+ if let currentDir = tabManager.selectedTab?.currentDirectory {
+ panel.directoryURL = URL(fileURLWithPath: currentDir)
+ }
if panel.runModal() == .OK, let url = panel.url {
</file context>
|
Thanks for the contribution! This has been addressed by #2034 which was just merged. |
NSOpenPanel was created without a directoryURL, causing the ⌘O file picker to always open at ~/Documents instead of the focused workspace's working directory.
Fix by setting panel.directoryURL to the selected tab's currentDirectory before running the modal in both the menu bar File → Open Folder action and the command palette openFolder handler.
Fixes #2010.
Summary by cubic
Default the Open Folder (
NSOpenPanel) to the focused workspace’s working directory (menu File → Open Folder and command palette), instead of always opening at ~/Documents; falls back to the system default when no workspace is open. Fixes #2010.Written for commit 0a17725. Summary will update on new commits.
Summary by CodeRabbit