Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5975,8 +5975,11 @@ struct ContentView: View {
panel.allowsMultipleSelection = false
panel.title = String(localized: "panel.openFolder.title", defaultValue: "Open Folder")
panel.prompt = String(localized: "panel.openFolder.prompt", defaultValue: "Open")
if let preferredDir = self.tabManager.selectedWorkspace?.focusedPanelId.flatMap({ self.tabManager.selectedWorkspace?.panelDirectories[$0] }), !preferredDir.isEmpty {
panel.directoryURL = URL(fileURLWithPath: preferredDir)

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.

P2 Use isDirectory: true when constructing directory URL

URL(fileURLWithPath:) without isDirectory: true consults the file system to infer whether the path is a file or directory. If the working directory stored in panelDirectories has since been deleted (e.g. the user rm -rf'd a temp dir in the terminal), the stat will fail and the resulting URL will have hasDirectoryPath == false. NSOpenPanel ignores a directoryURL that isn't a directory URL, silently falling back to ~/Documents — the exact behavior this PR is trying to fix.

Explicitly passing isDirectory: true makes the intent unambiguous and ensures the URL is formed correctly regardless of whether the path still exists on disk:

Suggested change
panel.directoryURL = URL(fileURLWithPath: preferredDir)
panel.directoryURL = URL(fileURLWithPath: preferredDir, isDirectory: true)

The same issue exists in Sources/cmuxApp.swift:597.

}
Comment on lines +5978 to +5980

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Use workspace directory fallback before defaulting to NSOpenPanel’s default location.

Line 5978 only uses the focused panel directory. When that value is absent, this still falls back to default picker behavior instead of the workspace directory context, so the issue can reproduce.

💡 Proposed fix
-                if let preferredDir = self.tabManager.selectedWorkspace?.focusedPanelId.flatMap({ self.tabManager.selectedWorkspace?.panelDirectories[$0] }), !preferredDir.isEmpty {
-                    panel.directoryURL = URL(fileURLWithPath: preferredDir)
-                }
+                if let workspace = self.tabManager.selectedWorkspace {
+                    let focusedDir = workspace.focusedPanelId
+                        .flatMap { workspace.panelDirectories[$0] }?
+                        .trimmingCharacters(in: .whitespacesAndNewlines)
+                    let workspaceDir = workspace.currentDirectory.trimmingCharacters(in: .whitespacesAndNewlines)
+
+                    if let focusedDir, !focusedDir.isEmpty, FileManager.default.fileExists(atPath: focusedDir) {
+                        panel.directoryURL = URL(fileURLWithPath: focusedDir, isDirectory: true)
+                    } else if !workspaceDir.isEmpty, FileManager.default.fileExists(atPath: workspaceDir) {
+                        panel.directoryURL = URL(fileURLWithPath: workspaceDir, isDirectory: true)
+                    } else {
+                        panel.directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
+                    }
+                }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if let preferredDir = self.tabManager.selectedWorkspace?.focusedPanelId.flatMap({ self.tabManager.selectedWorkspace?.panelDirectories[$0] }), !preferredDir.isEmpty {
panel.directoryURL = URL(fileURLWithPath: preferredDir)
}
if let workspace = self.tabManager.selectedWorkspace {
let focusedDir = workspace.focusedPanelId
.flatMap { workspace.panelDirectories[$0] }?
.trimmingCharacters(in: .whitespacesAndNewlines)
let workspaceDir = workspace.currentDirectory.trimmingCharacters(in: .whitespacesAndNewlines)
if let focusedDir, !focusedDir.isEmpty, FileManager.default.fileExists(atPath: focusedDir) {
panel.directoryURL = URL(fileURLWithPath: focusedDir, isDirectory: true)
} else if !workspaceDir.isEmpty, FileManager.default.fileExists(atPath: workspaceDir) {
panel.directoryURL = URL(fileURLWithPath: workspaceDir, isDirectory: true)
} else {
panel.directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
}
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@Sources/ContentView.swift` around lines 5978 - 5980, When setting
panel.directoryURL in ContentView, prefer the focused panel directory but fall
back to the workspace-level directory before letting NSOpenPanel use its
default: update the logic around tabManager.selectedWorkspace?.focusedPanelId
and panelDirectories so that if preferredDir is nil or empty you attempt to read
the workspace directory (e.g. tabManager.selectedWorkspace?.directory or similar
workspace-level path property) and set panel.directoryURL = URL(fileURLWithPath:
workspaceDir) when present; only if both focused panel and workspace directory
are absent should you leave the panel to use NSOpenPanel's default.

if panel.runModal() == .OK, let url = panel.url {
tabManager.addWorkspace(workingDirectory: url.path)
self.tabManager.addWorkspace(workingDirectory: url.path)
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/cmuxApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ struct cmuxApp: App {
panel.allowsMultipleSelection = false
panel.title = String(localized: "menu.file.openFolder.panelTitle", defaultValue: "Open Folder")
panel.prompt = String(localized: "menu.file.openFolder.panelPrompt", defaultValue: "Open")
if let preferredDir = self.activeTabManager.selectedWorkspace?.focusedPanelId.flatMap({ self.activeTabManager.selectedWorkspace?.panelDirectories[$0] }), !preferredDir.isEmpty {
panel.directoryURL = URL(fileURLWithPath: preferredDir)
}
Comment on lines +596 to +598

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Add workspace-level fallback before defaulting to Documents

Line 596 only uses focusedPanelId -> panelDirectories. If that lookup is missing (e.g., fresh workspace/no OSC7 update yet), the picker still opens at the default location instead of a workspace-contextual path. Please fall back to workspace-level directory (e.g., currentDirectory) before ~/Documents.

💡 Suggested patch
-                    if let preferredDir = self.activeTabManager.selectedWorkspace?.focusedPanelId.flatMap({ self.activeTabManager.selectedWorkspace?.panelDirectories[$0] }), !preferredDir.isEmpty {
-                        panel.directoryURL = URL(fileURLWithPath: preferredDir)
-                    }
+                    let workspace = self.activeTabManager.selectedWorkspace
+                    let focusedPaneDir = workspace.flatMap { ws in
+                        ws.focusedPanelId.flatMap { ws.panelDirectories[$0] }
+                    }
+                    let startDir = (
+                        focusedPaneDir
+                        ?? workspace?.currentDirectory
+                        ?? NSString(string: "~/Documents").expandingTildeInPath
+                    ).trimmingCharacters(in: .whitespacesAndNewlines)
+                    if !startDir.isEmpty {
+                        panel.directoryURL = URL(fileURLWithPath: startDir)
+                    }
🤖 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 current logic sets
panel.directoryURL only when a focused panel's panelDirectories entry exists, so
when that lookup is missing it falls back to the global default; update the code
around activeTabManager.selectedWorkspace?.focusedPanelId and panelDirectories
to first try the focused panel directory, and if that is nil or empty fall back
to the workspace-level directory (e.g.,
activeTabManager.selectedWorkspace?.currentDirectory) before finally defaulting
to ~/Documents; ensure you check for non-empty strings and set
panel.directoryURL = URL(fileURLWithPath: <chosenPath>) using the workspace
currentDirectory when available.

if panel.runModal() == .OK, let url = panel.url {
if let appDelegate = AppDelegate.shared {
if appDelegate.addWorkspaceInPreferredMainWindow(
Expand All @@ -602,7 +605,7 @@ struct cmuxApp: App {
appDelegate.openNewMainWindow(nil)
}
} else {
activeTabManager.addWorkspace(workingDirectory: url.path)
self.activeTabManager.addWorkspace(workingDirectory: url.path)
}
}
}
Expand Down