Skip to content
Merged
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
9 changes: 1 addition & 8 deletions CLI/CLI+TmuxCompat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1057,14 +1057,7 @@ extension ProgramaCLI {
}

private func prependPathEntries(_ newEntries: [String], to currentPath: String?) -> String {
var ordered: [String] = []
var seen: Set<String> = []
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 {
Expand Down
6 changes: 6 additions & 0 deletions GhosttyTabs.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -575,6 +577,7 @@
A1D000000000000000000008 /* CommandPaletteView+PaneHandoff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CommandPaletteView+PaneHandoff.swift"; sourceTree = "<group>"; };
A1D000000000000000000006 /* CLI+Handoff.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CLI+Handoff.swift"; sourceTree = "<group>"; };
A1D000000000000000000003 /* PaneHandoffFormatter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PaneHandoffFormatter.swift; sourceTree = "<group>"; };
TPEV00000000000000000001 /* TerminalPathEnvironment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalPathEnvironment.swift; sourceTree = "<group>"; };
A1D000000000000000000001 /* TerminalController+SurfacePaste.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TerminalController+SurfacePaste.swift"; sourceTree = "<group>"; };
B6F200000000000000000001 /* TerminalController+SurfaceConditionalSend.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "TerminalController+SurfaceConditionalSend.swift"; sourceTree = "<group>"; };
A2CV0000000000000000000B /* CommandPaletteView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CommandPaletteView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -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 */,
Expand Down
46 changes: 46 additions & 0 deletions Sources/TerminalPathEnvironment.swift
Original file line number Diff line number Diff line change
@@ -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<String> = []
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)
}
}
5 changes: 2 additions & 3 deletions Sources/TerminalSurface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
47 changes: 47 additions & 0 deletions programaTests/GhosttyConfigTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading