Skip to content

Commit 79d7505

Browse files
committed
[Swift REPL] Inherit the environment for the Swift REPL
The Swift REPL was had rolling the ProcessLaunchInfo instead of using the one already setup by the target. While populating the launch info, it used `Target::GetTargetEnvironment()` which only looks at the corresponding setting, and ignoring the `inherit-environment` setting. The alternative of calling `Target::GetInheritedEnvironment` is also wrong, because that has the inverse problem: it only inherits the environment and doesn't merge it with the target's environment variables. The solution is to use `Target::GetProcessLaunchInfo` which populates the launch info correctly. Propagating the environment correctly is particularly important on Windows where `Path` needs to be passed to the inferior to allow `LoadLibraryW(L"swiftCore.dll")` to succeed. Without this patch, the library is not found and the inferior exits terminating the REPL instance. - Fixes: llvm#9551 - Will fix swiftlang/swift#76702 and swiftlang/swift#70005 with a corresponding driver change to set `inherit-environment` when invoking the REPL. rdar://137522456
1 parent 29770a5 commit 79d7505

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,18 @@ lldb::REPLSP SwiftREPL::CreateInstanceFromDebugger(Status &err,
188188
// breakpoint above, it better
189189
// say it is internal
190190

191-
lldb_private::ProcessLaunchInfo launch_info;
192-
llvm::StringRef target_settings_argv0 = target_sp->GetArg0();
193-
194-
if (target_sp->GetDisableASLR())
195-
launch_info.GetFlags().Set(eLaunchFlagDisableASLR);
196-
197-
if (target_sp->GetDisableSTDIO())
198-
launch_info.GetFlags().Set(eLaunchFlagDisableSTDIO);
191+
lldb_private::ProcessLaunchInfo launch_info =
192+
target_sp->GetProcessLaunchInfo();
199193

194+
// FIXME: Why is this necessary? Document or change once we know the answer.
195+
llvm::StringRef target_settings_argv0 = target_sp->GetArg0();
200196
if (!target_settings_argv0.empty()) {
201197
launch_info.GetArguments().AppendArgument(target_settings_argv0);
202198
launch_info.SetExecutableFile(exe_module_sp->GetPlatformFileSpec(), false);
203199
} else {
204200
launch_info.SetExecutableFile(exe_module_sp->GetPlatformFileSpec(), true);
205201
}
206202

207-
launch_info.GetEnvironment() = target_sp->GetTargetEnvironment();
208203
debugger.SetAsyncExecution(false);
209204
err = target_sp->Launch(launch_info, nullptr);
210205
debugger.SetAsyncExecution(true);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test inheriting environment variables in the REPL.
2+
// REQUIRES: system-darwin
3+
// REQUIRES: swift
4+
5+
// RUN: FOO=foo %lldb --repl -O 'settings set target.inherit-env true' < %s 2>&1 \
6+
// RUN: | FileCheck %s
7+
8+
import Foundation
9+
ProcessInfo.processInfo.environment["FOO"]
10+
// CHECK: $R0: String? = "foo"

0 commit comments

Comments
 (0)