diff --git a/CLI/CLI+TmuxCompat.swift b/CLI/CLI+TmuxCompat.swift index 50e47de4..9d16212e 100644 --- a/CLI/CLI+TmuxCompat.swift +++ b/CLI/CLI+TmuxCompat.swift @@ -1057,14 +1057,7 @@ extension ProgramaCLI { } private func prependPathEntries(_ newEntries: [String], to currentPath: String?) -> String { - var ordered: [String] = [] - var seen: Set = [] - for entry in newEntries + (currentPath?.split(separator: ":").map(String.init) ?? []) where !entry.isEmpty { - if seen.insert(entry).inserted { - ordered.append(entry) - } - } - return ordered.joined(separator: ":") + TerminalPathEnvironment.prependingUniqueEntries(newEntries, to: currentPath) } struct TmuxCompatFocusedContext { diff --git a/GhosttyTabs.xcodeproj/project.pbxproj b/GhosttyTabs.xcodeproj/project.pbxproj index 6917aed6..ba49194f 100644 --- a/GhosttyTabs.xcodeproj/project.pbxproj +++ b/GhosttyTabs.xcodeproj/project.pbxproj @@ -93,6 +93,8 @@ A1D000000000000000000007 /* CLI+Handoff.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1D000000000000000000006 /* CLI+Handoff.swift */; }; A1D000000000000000000005 /* PaneHandoffFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1D000000000000000000003 /* PaneHandoffFormatter.swift */; }; A1D000000000000000000004 /* PaneHandoffFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1D000000000000000000003 /* PaneHandoffFormatter.swift */; }; + TPEV00000000000000000002 /* TerminalPathEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = TPEV00000000000000000001 /* TerminalPathEnvironment.swift */; }; + TPEV00000000000000000003 /* TerminalPathEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = TPEV00000000000000000001 /* TerminalPathEnvironment.swift */; }; A1D000000000000000000002 /* TerminalController+SurfacePaste.swift in Sources */ = {isa = PBXBuildFile; fileRef = A1D000000000000000000001 /* TerminalController+SurfacePaste.swift */; }; B6F200000000000000000002 /* TerminalController+SurfaceConditionalSend.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6F200000000000000000001 /* TerminalController+SurfaceConditionalSend.swift */; }; A2CV0000000000000000000A /* CommandPaletteView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A2CV0000000000000000000B /* CommandPaletteView.swift */; }; @@ -575,6 +577,7 @@ A1D000000000000000000008 /* CommandPaletteView+PaneHandoff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CommandPaletteView+PaneHandoff.swift"; sourceTree = ""; }; A1D000000000000000000006 /* CLI+Handoff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CLI+Handoff.swift"; sourceTree = ""; }; A1D000000000000000000003 /* PaneHandoffFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaneHandoffFormatter.swift; sourceTree = ""; }; + TPEV00000000000000000001 /* TerminalPathEnvironment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalPathEnvironment.swift; sourceTree = ""; }; A1D000000000000000000001 /* TerminalController+SurfacePaste.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TerminalController+SurfacePaste.swift"; sourceTree = ""; }; B6F200000000000000000001 /* TerminalController+SurfaceConditionalSend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TerminalController+SurfaceConditionalSend.swift"; sourceTree = ""; }; A2CV0000000000000000000B /* CommandPaletteView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandPaletteView.swift; sourceTree = ""; }; @@ -1036,6 +1039,7 @@ NRPA00010 /* SettingsView.swift */, THTM0001 /* TerminalThemeStore.swift */, A1D000000000000000000003 /* PaneHandoffFormatter.swift */, + TPEV00000000000000000001 /* TerminalPathEnvironment.swift */, REND0001A1B2C3D4E5F60719 /* RendererRealization.swift */, A5FF0017 /* SettingDefinition.swift */, A5001012 /* ContentView.swift */, @@ -1656,6 +1660,7 @@ NRPA00009 /* SettingsView.swift in Sources */, THTM0002 /* TerminalThemeStore.swift in Sources */, A1D000000000000000000004 /* PaneHandoffFormatter.swift in Sources */, + TPEV00000000000000000002 /* TerminalPathEnvironment.swift in Sources */, REND0002A1B2C3D4E5F60719 /* RendererRealization.swift in Sources */, A5FF0007 /* SettingDefinition.swift in Sources */, A5001002 /* ContentView.swift in Sources */, @@ -2008,6 +2013,7 @@ B9000037A1B2C3D4E5F60719 /* CLI+Themes.swift in Sources */, THTM0003 /* TerminalThemeStore.swift in Sources */, A1D000000000000000000005 /* PaneHandoffFormatter.swift in Sources */, + TPEV00000000000000000003 /* TerminalPathEnvironment.swift in Sources */, B9000039A1B2C3D4E5F60719 /* CLI+Tree.swift in Sources */, 7B3F9A10C2D4E5F6A7B8000A /* CLI+Ports.swift in Sources */, WTST000005 /* CLI+Storage.swift in Sources */, diff --git a/Sources/TerminalPathEnvironment.swift b/Sources/TerminalPathEnvironment.swift new file mode 100644 index 00000000..7db2f39b --- /dev/null +++ b/Sources/TerminalPathEnvironment.swift @@ -0,0 +1,46 @@ +import Foundation + +/// Composes the PATH values Programa exports to terminals and child processes. +/// Compiled into both the app and the `programa` CLI. +enum TerminalPathEnvironment { + /// The components of `path`, minus any that cannot be re-exported intact. + /// + /// Foundation and `String(cString:)` decode malformed inherited bytes as + /// U+FFFD, so such a component no longer names the directory it came + /// from, and strict tools reject the PATH that carries it. Components with + /// control characters are dropped for the same reason. Empty components + /// are kept, since they mean the current directory. + static func components(from path: String) -> [String] { + path.split(separator: ":", omittingEmptySubsequences: false) + .map(String.init) + .filter { !$0.unicodeScalars.contains(where: isMalformedScalar) } + } + + /// The PATH to export when `directory` must lead `currentPath`, or nil + /// when `currentPath` already contains it and has nothing to drop. + static func startupPath(prependingDirectory directory: String, to currentPath: String) -> String? { + let entries = components(from: currentPath) + let sanitizedPath = entries.joined(separator: ":") + if entries.contains(directory) { + return sanitizedPath == currentPath ? nil : sanitizedPath + } + return sanitizedPath.isEmpty ? directory : "\(directory):\(sanitizedPath)" + } + + /// Prepends `newEntries` to `currentPath`, keeping the first occurrence + /// of each entry and dropping empty and malformed ones. + static func prependingUniqueEntries(_ newEntries: [String], to currentPath: String?) -> String { + var ordered: [String] = [] + var seen: Set = [] + for entry in newEntries + components(from: currentPath ?? "") where !entry.isEmpty { + if seen.insert(entry).inserted { + ordered.append(entry) + } + } + return ordered.joined(separator: ":") + } + + private static func isMalformedScalar(_ scalar: Unicode.Scalar) -> Bool { + scalar == "\u{FFFD}" || CharacterSet.controlCharacters.contains(scalar) + } +} diff --git a/Sources/TerminalSurface.swift b/Sources/TerminalSurface.swift index d1fa6733..a1fe83d1 100644 --- a/Sources/TerminalSurface.swift +++ b/Sources/TerminalSurface.swift @@ -1426,9 +1426,8 @@ final class TerminalSurface: Identifiable, ObservableObject { ?? getenv("PATH").map { String(cString: $0) } ?? ProcessInfo.processInfo.environment["PATH"] ?? "" - if !currentPath.split(separator: ":").contains(Substring(cliBinPath)) { - let separator = currentPath.isEmpty ? "" : ":" - setManagedEnvironmentValue("PATH", "\(cliBinPath)\(separator)\(currentPath)") + if let path = TerminalPathEnvironment.startupPath(prependingDirectory: cliBinPath, to: currentPath) { + setManagedEnvironmentValue("PATH", path) } } diff --git a/programaTests/GhosttyConfigTests.swift b/programaTests/GhosttyConfigTests.swift index fd1914f8..06ab62fd 100644 --- a/programaTests/GhosttyConfigTests.swift +++ b/programaTests/GhosttyConfigTests.swift @@ -1099,6 +1099,53 @@ final class GhosttyTerminalStartupEnvironmentTests: XCTestCase { XCTAssertEqual(merged["COLORTERM"], TerminalSurface.managedColorTerm) XCTAssertEqual(merged["TERM_PROGRAM"], TerminalSurface.managedTerminalProgram) } + + // Foundation and String(cString:) decode malformed inherited PATH bytes + // as U+FFFD; re-exporting them hands strict tools an invalid PATH. + private let malformedPathComponent = "\u{FFFD}u[\u{FFFD}\u{0008}`\u{FFFD}-\u{FFFD}(\u{FFFD}" + + func testStartupPathDropsMalformedComponentsWhenPrependingCLIBin() { + XCTAssertEqual( + TerminalPathEnvironment.startupPath( + prependingDirectory: "/Applications/Programa.app/Contents/Resources/bin", + to: "/usr/bin:\(malformedPathComponent):/bin" + ), + "/Applications/Programa.app/Contents/Resources/bin:/usr/bin:/bin" + ) + } + + func testStartupPathSanitizesPathThatAlreadyContainsCLIBin() { + let cliBin = "/Applications/Programa.app/Contents/Resources/bin" + XCTAssertEqual( + TerminalPathEnvironment.startupPath( + prependingDirectory: cliBin, + to: "\(cliBin):/usr/bin:\(malformedPathComponent)" + ), + "\(cliBin):/usr/bin" + ) + } + + func testStartupPathLeavesCleanPathContainingCLIBinAlone() { + let cliBin = "/Applications/Programa.app/Contents/Resources/bin" + XCTAssertNil( + TerminalPathEnvironment.startupPath(prependingDirectory: cliBin, to: "/usr/bin:\(cliBin)::/bin") + ) + XCTAssertEqual( + TerminalPathEnvironment.startupPath(prependingDirectory: cliBin, to: "/usr/bin::/bin"), + "\(cliBin):/usr/bin::/bin" + ) + XCTAssertEqual(TerminalPathEnvironment.startupPath(prependingDirectory: cliBin, to: ""), cliBin) + } + + func testPrependingUniqueEntriesDropsMalformedComponents() { + XCTAssertEqual( + TerminalPathEnvironment.prependingUniqueEntries( + ["/tmp/programa-tmux-shim"], + to: "/usr/bin:\(malformedPathComponent):/usr/bin:/bin" + ), + "/tmp/programa-tmux-shim:/usr/bin:/bin" + ) + } } final class TitlebarDoubleClickPreferenceTests: XCTestCase {