diff --git a/macos/Sources/Features/App Intents/NewTerminalIntent.swift b/macos/Sources/Features/App Intents/NewTerminalIntent.swift index 35f53169f..67d1a9676 100644 --- a/macos/Sources/Features/App Intents/NewTerminalIntent.swift +++ b/macos/Sources/Features/App Intents/NewTerminalIntent.swift @@ -74,7 +74,7 @@ struct NewTerminalIntent: AppIntent { // If we were given a working directory then open that directory if let url = workingDirectory?.fileURL { let dir = url.hasDirectoryPath ? url : url.deletingLastPathComponent() - config.workingDirectory = dir.path(percentEncoded: false) + config.workingDirectory = dir.pathWithoutTrailingSlash } // Parse environment variables from KEY=VALUE format diff --git a/macos/Sources/Features/Services/ServiceProvider.swift b/macos/Sources/Features/Services/ServiceProvider.swift index aa5ab7c8a..5f51c9ed4 100644 --- a/macos/Sources/Features/Services/ServiceProvider.swift +++ b/macos/Sources/Features/Services/ServiceProvider.swift @@ -59,7 +59,7 @@ class ServiceProvider: NSObject { for url in directoryURLs { var config = Ghostty.SurfaceConfiguration() - config.workingDirectory = url.path(percentEncoded: false) + config.workingDirectory = url.pathWithoutTrailingSlash switch target { case .window: diff --git a/macos/Sources/Helpers/Extensions/URL+Extension.swift b/macos/Sources/Helpers/Extensions/URL+Extension.swift new file mode 100644 index 000000000..1e5090111 --- /dev/null +++ b/macos/Sources/Helpers/Extensions/URL+Extension.swift @@ -0,0 +1,9 @@ +import Foundation +import System + +extension URL { + /// The decoded path with trailing separators removed, except for the root path. + var pathWithoutTrailingSlash: String { + FilePath(path(percentEncoded: false)).string + } +} diff --git a/macos/Tests/URLTests.swift b/macos/Tests/URLTests.swift new file mode 100644 index 000000000..f3f0ec151 --- /dev/null +++ b/macos/Tests/URLTests.swift @@ -0,0 +1,25 @@ +import Foundation +import Testing +@testable import Ghostty + +struct URLTests { + @Test func pathWithoutTrailingSlash() { + let url = URL(string: "file:///tmp/example/")! + #expect(url.pathWithoutTrailingSlash == "/tmp/example") + } + + @Test func pathWithoutMultipleTrailingSlashes() { + let url = URL(string: "file:///tmp/example///")! + #expect(url.pathWithoutTrailingSlash == "/tmp/example") + } + + @Test func pathWithoutTrailingSlashDecodesPath() { + let url = URL(string: "file:///tmp/example%20directory/")! + #expect(url.pathWithoutTrailingSlash == "/tmp/example directory") + } + + @Test func pathWithoutTrailingSlashPreservesRoot() { + let url = URL(string: "file:///")! + #expect(url.pathWithoutTrailingSlash == "/") + } +}