diff --git a/macos/Sources/Lithe/Core/Ports/DefinedToolchains.swift b/macos/Sources/Lithe/Core/Ports/DefinedToolchains.swift index 4dd7c07326..e355f3ab83 100644 --- a/macos/Sources/Lithe/Core/Ports/DefinedToolchains.swift +++ b/macos/Sources/Lithe/Core/Ports/DefinedToolchains.swift @@ -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 diff --git a/macos/Sources/Lithe/Platform/MacOS/Build/MacBuildToolResolver.swift b/macos/Sources/Lithe/Platform/MacOS/Build/MacBuildToolResolver.swift index 13baab94fc..0457498b36 100644 --- a/macos/Sources/Lithe/Platform/MacOS/Build/MacBuildToolResolver.swift +++ b/macos/Sources/Lithe/Platform/MacOS/Build/MacBuildToolResolver.swift @@ -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) diff --git a/macos/Sources/Lithe/Platform/MacOS/Runtime/MacToolchainDiscovery+Resolution.swift b/macos/Sources/Lithe/Platform/MacOS/Runtime/MacToolchainDiscovery+Resolution.swift index 1bbba7167c..4351a52add 100644 --- a/macos/Sources/Lithe/Platform/MacOS/Runtime/MacToolchainDiscovery+Resolution.swift +++ b/macos/Sources/Lithe/Platform/MacOS/Runtime/MacToolchainDiscovery+Resolution.swift @@ -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. diff --git a/macos/Tests/LitheTests/MacBuildToolResolverTests.swift b/macos/Tests/LitheTests/MacBuildToolResolverTests.swift index 0f8ea93a24..03da386fe5 100644 --- a/macos/Tests/LitheTests/MacBuildToolResolverTests.swift +++ b/macos/Tests/LitheTests/MacBuildToolResolverTests.swift @@ -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 {