Skip to content

Make inheriting environment variables optional #3

@Kamik423

Description

@Kamik423

Currently, the shell environment is queried for the environment variables to be passed onto the process.

/// Environment variables captured from a login shell.
///
/// The login-shell environment is useful for inheriting user profile setup
/// (for example PATH modifications from shell startup files).
/// Returns `nil` when the shell cannot be launched or returns no entries.
public let defaultShellEnvironment: [String: String]? = {
let task = Process()
task.executableURL = URL(fileURLWithPath: SHELL)
task.arguments = ["-l", "-c", "env -0"]
let stdoutPipe = Pipe()
task.standardOutput = stdoutPipe
task.standardError = FileHandle.nullDevice
let output: Data
do {
try task.run()
// Drain stdout while the process is alive to avoid pipe backpressure deadlocks.
output = stdoutPipe.fileHandleForReading.readDataToEndOfFile()
task.waitUntilExit()
} catch {
return nil
}
guard task.terminationStatus == 0 else {
return nil
}
var environment: [String: String] = [:]
for rawEntry in output.split(separator: 0) {
let entry = String(decoding: rawEntry, as: UTF8.self)
guard let separator = entry.firstIndex(of: "=") else {
continue
}
let key = String(entry[..<separator])
let value = String(entry[entry.index(after: separator)...])
environment[key] = value
}
return environment.isEmpty ? nil : environment
}()

Normal Mac apps do not inherit the environment variables from the default shell. Java applications should not be treated differently as a whole. Loading the environment variables should still be possible, but it should require setting a key in the Info.plist.

Any Java application can get the environment variables still by calling the default shell with any configuration. Just not with System.getenv().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions