Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Ask zsh for its zshrc as Terminal starts it, and only trust launchd's ZDOTDIR

Țugui Dragoș6070d4422874
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
index 612fd35e..80a5fd98 100644
--- a/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/ShellSessions.swift
@@ -11,9 +11,14 @@ public enum ShellSessions: Sendable, Equatable {
of(terminalSetting: terminal.shell(), loginShell: loginShell(), zdotdir: sessionZDOTDIR, home: home)
}
- /// The ZDOTDIR of the session Peel runs in, which Terminal's zsh starts with too, since launchd starts both.
+ /// The ZDOTDIR of the session Peel runs in, which Terminal's zsh starts with too, since launchd starts both. A Peel
+ /// started from a shell has that shell's instead.
static var sessionZDOTDIR: String? {
- ProcessInfo.processInfo.environment["ZDOTDIR"]
+ sessionZDOTDIR(parent: getppid(), environment: ProcessInfo.processInfo.environment)
+ }
+
+ static func sessionZDOTDIR(parent: pid_t, environment: [String: String]) -> String? {
+ parent == 1 ? environment["ZDOTDIR"] : nil
}
static func of(terminalSetting: String?, loginShell: String?, zdotdir: String?, home: URL) -> ShellSessions? {
@@ -29,9 +34,9 @@ public enum ShellSessions: Sendable, Equatable {
}
}
- /// The `.zshrc` zsh reads, where the line that sources Peel's file goes: in the folder `ZDOTDIR` names once
- /// `.zshenv` has run, since `.zshenv` may set it (`man zsh`, STARTUP/SHUTDOWN FILES). zsh is asked with `-c`, which
- /// reads `.zshenv` and no other startup file, and the folder of `.zshenv` stands when it doesn't answer.
+ /// The `.zshrc` zsh reads, where the line that sources Peel's file goes: in the folder `ZDOTDIR` names before zsh
+ /// reads it, since `.zshenv` or `.zprofile` may set it (`man zsh`, STARTUP/SHUTDOWN FILES). zsh is asked as
+ /// Terminal starts it, a login shell, and the folder of `.zshenv` stands when it doesn't answer.
public func zshrc(home: URL = .homeDirectory) async -> URL? {
await zshrc(home: home, zdotdir: Self.sessionZDOTDIR)
}
@@ -50,11 +55,17 @@ public enum ShellSessions: Sendable, Equatable {
environment["ZDOTDIR"] = (zdotdir as NSString).standardizingPath
}
let answer = await Subprocess.run(
- "/bin/zsh", ["-c", #"print -r -- "${ZDOTDIR:-$HOME}""#], environment: environment, timeout: 10
+ "/bin/zsh", ["-l", "-c", #"print -r -- "peel-zdotdir:${ZDOTDIR:-$HOME}""#],
+ environment: environment, timeout: 10
)
- // What `.zshenv` itself prints comes first, so the answer is the last line.
+ // The startup files may print too, before the answer and at logout after it.
+ let marker = "peel-zdotdir:/"
let named: String? = if case .success(let output) = answer, output.status == 0,
- let line = output.text.split(separator: "\n").last, line.hasPrefix("/") { String(line) } else { nil }
+ let line = output.text.split(separator: "\n").last(where: { $0.hasPrefix(marker) }) {
+ String(line.dropFirst(marker.count - 1))
+ } else {
+ nil
+ }
return (named.map { URL(filePath: $0, directoryHint: .isDirectory) } ?? folder)
.appending(path: ".zshrc", directoryHint: .notDirectory)
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift
index c3e2a017..4a4cc51a 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/ShellSessionsTests.swift
@@ -4,6 +4,28 @@ import PeelPrivileged
import Testing
struct ShellSessionsTests {
+ @Test func aZDOTDIRSetWhereALoginShellReadsItMovesTheZshrc() async throws {
+ let directory = try TemporaryDirectory()
+ let home = try directory.directory("home")
+ try directory.file(
+ "home/.zprofile", contents: Data("export ZDOTDIR=\"$HOME/.config/zsh\"\nprint \"said at login\"\n".utf8)
+ )
+ try directory.file("home/.zlogout", contents: Data("print \"said at logout\"\n".utf8))
+ let zsh = ShellSessions.zsh(startupFile: home.appending(path: ".zshenv"))
+
+ let zshrc = await zsh.zshrc(home: home, zdotdir: nil)
+
+ #expect(zshrc?.path(percentEncoded: false).hasSuffix("home/.config/zsh/.zshrc") == true)
+ }
+
+ @Test func onlyAPeelLaunchdStartedSharesTheSessionsZDOTDIR() {
+ let environment = ["ZDOTDIR": "/org.example/zsh"]
+
+ #expect(ShellSessions.sessionZDOTDIR(parent: 1, environment: environment) == "/org.example/zsh")
+ #expect(ShellSessions.sessionZDOTDIR(parent: 4_242, environment: environment) == nil)
+ #expect(ShellSessions.sessionZDOTDIR(parent: 1, environment: [:]) == nil)
+ }
+
private let home = URL(filePath: "/Users/someone", directoryHint: .isDirectory)
private func sessions(setting: String?, login: String?, zdotdir: String? = nil) -> ShellSessions? {