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
5 changes: 5 additions & 0 deletions macos/Sources/Lithe/Core/Ports/DefinedToolchains.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ struct ResolvedDefinedToolchain: Sendable {
/// The installation root the selection came from; the definition's `launch`
/// template can name it as `${home}`. Nil when the tool was chosen by executable.
var home: String? = nil
/// What this selection added to the caller's environment: the definition's exports,
/// plus `PATH` when the installation prepends entries. `environment` is the caller's
/// environment with these applied. Consumers that forward only what a tool
/// contributes read this instead of treating the whole environment as contributed.
var exports: [String: String] = [:]
}

/// The program and arguments a tool definition's `launch` template expands to for one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,12 @@ final class MacBuildToolResolver: BuildToolResolving {
environment: environment, minimumVersion: minimumVersion, projectCodeAllowed: true, fixed: selections[id], use: use, consumerID: consumerID) else {
throw BuildToolResolutionError(L10n.string("The selected toolchain is unavailable."), terminal: true)
}
// The choice carries only what the selection added. Its full
// environment is the caller's plus these exports, so merging the
// exports back reproduces it without marking every variable the
// host process already had as contributed by the tool.
definitions[id + "\n" + root] = .resolved([.init(url: selected.executable,
executesProjectCode: selected.projectCode, terminal: true, environment: selected.environment)])
executesProjectCode: selected.projectCode, terminal: true, environment: selected.exports)])
}
guard let binding = registry?.resolve(workspaceURL)?.binding else {
return candidateResolver(runtimes, workspaceURL: workspaceURL, definitions: definitions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,16 @@ extension MacToolchainDiscovery {
let outcome = resolution["outcome"] as? [String: Any] else { throw Self.unavailable() }
guard outcome["kind"] as? String == "resolved", let selected = outcome["selection"] as? [String: Any],
let executable = selected["executable"] as? String else { throw Self.unavailable() }
var environment = request.environment.merging(selected["environment"] as? [String: String] ?? [:]) { _, new in new }
var exports = selected["environment"] as? [String: String] ?? [:]
var environment = request.environment.merging(exports) { _, new in new }
let prepend = selected["pathPrepend"] as? [String] ?? []
if !prepend.isEmpty { environment["PATH"] = (prepend + [environment["PATH"] ?? ""]).joined(separator: ":") }
if !prepend.isEmpty {
environment["PATH"] = (prepend + [environment["PATH"] ?? ""]).joined(separator: ":")
exports["PATH"] = environment["PATH"]
}
return Selection(executable: URL(fileURLWithPath: executable), environment: environment,
projectCode: selected["projectCode"] as? Bool == true, version: selected["version"] as? String,
home: selected["home"] as? String)
home: selected["home"] as? String, exports: exports)
}

/// The tool registry Core reads: the enabled plugins that define tools or providers.
Expand Down
52 changes: 52 additions & 0 deletions macos/Tests/LitheTests/MacBuildToolResolverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,58 @@ struct MacBuildToolResolverTests {
#expect(resolved.executableURL == executable.standardizedFileURL)
#expect(!FileManager.default.fileExists(atPath: marker.path))
}

/// A tool definition's selection runs with the caller's whole environment,
/// but the provider learns only what the selection added. Treating every
/// inherited variable as contributed made Core reject the hints as too large.
@Test
func toolchainSelectionContributesOnlyItsExportsToTheProvider() async throws {
let root = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
let home = root.appendingPathComponent("jdk")
let executable = home.appendingPathComponent("bin/java")
let base = ["PATH": "/usr/bin:/bin", "FIXTURE_SECRET": "test-only-secret", "SHELL": "/bin/zsh"]
let exports = ["FIXTURE_HOME": home.path, "PATH": home.appendingPathComponent("bin").path + ":/usr/bin:/bin"]
let selection = ResolvedDefinedToolchain(
executable: executable, environment: base.merging(exports) { _, new in new },
projectCode: true, version: "21", home: home.path, exports: exports
)
let runtime = ProjectRuntimeService(runtimeLocator: FixedEnvironmentRuntimeLocator(values: base))
let resolver = MacBuildToolResolver(
runtime: runtime, processRunner: MacProcessRunner(), fileStorage: MacFileStorage(),
definedTools: FixedDefinedTools(selection: selection)
)
let invocation = BuildInvocation(executables: [.toolchain(id: "fixture-tool", root: ".")])
let resolved = try await resolver.resolve(invocation, displayName: "Fixture", workspaceURL: root) { _, _ in
Issue.record("A toolchain that executes project code must not be probed")
return BuildVersionCheck(satisfied: false, version: nil)
}
#expect(resolved.executableURL == executable)
#expect(resolved.executesProjectCode)
#expect(resolved.environment["FIXTURE_SECRET"] == "test-only-secret")
#expect(resolved.environment["FIXTURE_HOME"] == home.path)
#expect(Set(resolved.providerEnvironment.keys) == ["PATH", "FIXTURE_HOME"])
#expect(resolved.providerEnvironment["PATH"]?.hasPrefix(home.appendingPathComponent("bin").path + ":") == true)
}
}

private struct FixedEnvironmentRuntimeLocator: RuntimeLocator {
let values: [String: String]
func environment() -> [String: String] { values }
func isExecutable(at url: URL) -> Bool { false }
}

private final class FixedDefinedTools: DefinedToolchainResolving {
private let selection: ResolvedDefinedToolchain
init(selection: ResolvedDefinedToolchain) { self.selection = selection }
func resolve(tool _: String, directory _: String, workspace _: URL, environment _: [String: String],
minimumVersion _: String?, projectCodeAllowed _: Bool,
fixed _: FixedToolchainSelection?, use _: DefinedToolchainUse, consumerID _: String?) async throws -> ResolvedDefinedToolchain? {
selection
}
func resolve(tool _: String, directory _: String, workspace _: URL, environment _: [String: String],
minimumVersion _: String?, projectCodeAllowed _: Bool) async throws -> ResolvedDefinedToolchain? {
selection
}
}

private struct BuildProbeRuntimeLocator: RuntimeLocator {
Expand Down
Loading