Skip to content

Commit 6ae9c6a

Browse files
shanselmanCopilot
andauthored
fix: preserve Linux PATH for WSL gateway controls (#1227)
Co-authored-by: Copilot App <[email protected]>
1 parent df88b28 commit 6ae9c6a

6 files changed

Lines changed: 75 additions & 29 deletions

File tree

‎docs/WSL_EXE_ARGV_PITFALL.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ All of these failed workarounds were verified empirically:
128128
- `src/OpenClaw.SetupEngine/CommandRunner.cs` - `RunInWslAsync` exposes the opt-in `inputViaStdin` parameter.
129129
- `src/OpenClaw.SetupEngine/ValidateWslLockdownStep.cs` - `ValidateWslLockdownStep` uses workaround #2, C# interpolation.
130130
- `src/OpenClaw.SetupEngine/WindowsNodeBootstrapContextStep.cs` - `WindowsNodeBootstrapContextStep` uses workaround #1, stdin.
131+
- `src/OpenClaw.Connection/WslGatewayController.cs` - gateway start, stop, and restart scripts use `bash -s` stdin so the Linux `$PATH` is not replaced by the Windows process environment.
131132

132133
## Related
133134

‎src/OpenClaw.Connection/WslCommandRunner.cs‎

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Task<WslCommandResult> RunAsync(
2828
Task<WslCommandResult> RunInDistroAsync(
2929
string name, IReadOnlyList<string> command,
3030
CancellationToken cancellationToken = default,
31-
IReadOnlyDictionary<string, string>? environment = null);
31+
IReadOnlyDictionary<string, string>? environment = null,
32+
string? standardInput = null);
3233
}
3334

3435
/// <summary>
@@ -58,16 +59,27 @@ public Task<WslCommandResult> RunAsync(
5859
IReadOnlyList<string> arguments,
5960
CancellationToken cancellationToken = default,
6061
IReadOnlyDictionary<string, string>? environment = null) =>
61-
RunProcessAsync("wsl.exe", arguments, cancellationToken, environment);
62+
RunProcessAsync(
63+
"wsl.exe",
64+
arguments,
65+
cancellationToken,
66+
environment,
67+
standardInput: null);
6268

6369
public Task<WslCommandResult> RunInDistroAsync(
6470
string name, IReadOnlyList<string> command,
6571
CancellationToken cancellationToken = default,
66-
IReadOnlyDictionary<string, string>? environment = null)
72+
IReadOnlyDictionary<string, string>? environment = null,
73+
string? standardInput = null)
6774
{
6875
var args = new List<string> { "-d", name, "--" };
6976
args.AddRange(command);
70-
return RunAsync(args, cancellationToken, environment);
77+
return RunProcessAsync(
78+
"wsl.exe",
79+
args,
80+
cancellationToken,
81+
environment,
82+
standardInput);
7183
}
7284

7385
public Task<WslCommandResult> TerminateDistroAsync(string name, CancellationToken cancellationToken = default) =>
@@ -108,13 +120,15 @@ private async Task<WslCommandResult> RunProcessAsync(
108120
string fileName,
109121
IReadOnlyList<string> arguments,
110122
CancellationToken cancellationToken,
111-
IReadOnlyDictionary<string, string>? environment)
123+
IReadOnlyDictionary<string, string>? environment,
124+
string? standardInput)
112125
{
113126
var psi = new ProcessStartInfo
114127
{
115128
FileName = fileName,
116129
RedirectStandardOutput = true,
117130
RedirectStandardError = true,
131+
RedirectStandardInput = standardInput is not null,
118132
UseShellExecute = false,
119133
CreateNoWindow = true,
120134
StandardOutputEncoding = Encoding.UTF8,
@@ -151,6 +165,26 @@ private async Task<WslCommandResult> RunProcessAsync(
151165
bool timedOut = false;
152166
try
153167
{
168+
if (standardInput is not null)
169+
{
170+
try
171+
{
172+
await process.StandardInput.WriteAsync(
173+
standardInput.AsMemory(),
174+
timeoutCts.Token);
175+
process.StandardInput.Close();
176+
}
177+
catch (IOException)
178+
{
179+
// The process result below is authoritative when wsl.exe exits
180+
// before accepting the complete script.
181+
}
182+
catch (ObjectDisposedException)
183+
{
184+
// The process closed stdin while failing. Preserve its result.
185+
}
186+
}
187+
154188
await process.WaitForExitAsync(timeoutCts.Token);
155189
}
156190
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)

‎src/OpenClaw.Connection/WslGatewayController.cs‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Collections.ObjectModel;
21
using OpenClaw.Shared;
32

43
namespace OpenClaw.Connection;
@@ -33,14 +32,10 @@ public static class WslGatewayControlCommandBuilder
3332
{
3433
internal const string OpenClawWslPathPrefix = "export PATH=\"/home/openclaw/.openclaw/bin:/opt/openclaw/bin:/usr/local/bin:$PATH\"";
3534

36-
public static IReadOnlyList<string> Build(WslGatewayControlAction action)
37-
{
38-
return new ReadOnlyCollection<string>([
39-
"bash",
40-
"-lc",
41-
$"{OpenClawWslPathPrefix} && openclaw gateway {ToVerb(action)}"
42-
]);
43-
}
35+
public static IReadOnlyList<string> BuildArguments() => ["bash", "-s"];
36+
37+
public static string BuildStandardInput(WslGatewayControlAction action) =>
38+
$"set -e\n{OpenClawWslPathPrefix}\nopenclaw gateway {ToVerb(action)}\n";
4439

4540
public static string ToVerb(WslGatewayControlAction action)
4641
{
@@ -88,8 +83,10 @@ public async Task<WslGatewayControlResult> RunAsync(
8883
logger.Info($"Running OpenClaw gateway {WslGatewayControlCommandBuilder.ToVerb(action)} in WSL distro '{normalizedDistroName}'.");
8984
var result = await commandRunner.RunInDistroAsync(
9085
normalizedDistroName,
91-
WslGatewayControlCommandBuilder.Build(action),
92-
cancellationToken).ConfigureAwait(false);
86+
WslGatewayControlCommandBuilder.BuildArguments(),
87+
cancellationToken,
88+
standardInput: WslGatewayControlCommandBuilder.BuildStandardInput(action))
89+
.ConfigureAwait(false);
9390

9491
return new WslGatewayControlResult(
9592
normalizedDistroName,

‎tests/OpenClaw.Tray.Tests/LocalAiGatewayProviderCoordinatorTests.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ public Task<WslCommandResult> RunInDistroAsync(
344344
string name,
345345
IReadOnlyList<string> command,
346346
CancellationToken cancellationToken = default,
347-
IReadOnlyDictionary<string, string>? environment = null)
347+
IReadOnlyDictionary<string, string>? environment = null,
348+
string? standardInput = null)
348349
{
349350
cancellationToken.ThrowIfCancellationRequested();
350351
Distros.Add(name);

‎tests/OpenClaw.Tray.Tests/StartupSetupStateTests.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ public Task<WslCommandResult> TerminateDistroAsync(string name, CancellationToke
645645
public Task<WslCommandResult> UnregisterDistroAsync(string name, CancellationToken cancellationToken = default) =>
646646
Task.FromResult(new WslCommandResult(0, "", ""));
647647

648-
public Task<WslCommandResult> RunInDistroAsync(string name, IReadOnlyList<string> command, CancellationToken cancellationToken = default, IReadOnlyDictionary<string, string>? environment = null) =>
648+
public Task<WslCommandResult> RunInDistroAsync(string name, IReadOnlyList<string> command, CancellationToken cancellationToken = default, IReadOnlyDictionary<string, string>? environment = null, string? standardInput = null) =>
649649
Task.FromResult(new WslCommandResult(0, "", ""));
650650
}
651651

@@ -663,7 +663,7 @@ public Task<WslCommandResult> TerminateDistroAsync(string name, CancellationToke
663663
public Task<WslCommandResult> UnregisterDistroAsync(string name, CancellationToken cancellationToken = default) =>
664664
Task.FromResult(new WslCommandResult(0, "", ""));
665665

666-
public Task<WslCommandResult> RunInDistroAsync(string name, IReadOnlyList<string> command, CancellationToken cancellationToken = default, IReadOnlyDictionary<string, string>? environment = null) =>
666+
public Task<WslCommandResult> RunInDistroAsync(string name, IReadOnlyList<string> command, CancellationToken cancellationToken = default, IReadOnlyDictionary<string, string>? environment = null, string? standardInput = null) =>
667667
Task.FromResult(new WslCommandResult(0, "", ""));
668668
}
669669
}

‎tests/OpenClaw.Tray.Tests/WslGatewayControllerTests.cs‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ public class WslGatewayControllerTests
99
[InlineData(WslGatewayControlAction.Start, "start")]
1010
[InlineData(WslGatewayControlAction.Stop, "stop")]
1111
[InlineData(WslGatewayControlAction.Restart, "restart")]
12-
public void Build_UsesBashLoginShellPathPrefixAndGatewayCommand(WslGatewayControlAction action, string verb)
12+
public void Build_UsesStdinScriptWithLinuxPathAndGatewayCommand(WslGatewayControlAction action, string verb)
1313
{
14-
var command = WslGatewayControlCommandBuilder.Build(action);
14+
var arguments = WslGatewayControlCommandBuilder.BuildArguments();
15+
var standardInput = WslGatewayControlCommandBuilder.BuildStandardInput(action);
1516

16-
Assert.Equal(["bash", "-lc"], command.Take(2).ToArray());
17-
Assert.Equal(
18-
$"{WslGatewayControlCommandBuilder.OpenClawWslPathPrefix} && openclaw gateway {verb}",
19-
command[2]);
17+
Assert.Equal(["bash", "-s"], arguments);
18+
Assert.Contains(WslGatewayControlCommandBuilder.OpenClawWslPathPrefix, standardInput);
19+
Assert.Contains($"openclaw gateway {verb}", standardInput);
20+
Assert.Contains("$PATH", standardInput);
2021
}
2122

2223
[Fact]
@@ -33,7 +34,10 @@ public async Task RunAsync_InvokesGatewayCommandInsideRegisteredDistro()
3334

3435
Assert.True(result.Success);
3536
Assert.Equal("OpenClawGateway", runner.LastDistroName);
36-
Assert.Equal(WslGatewayControlCommandBuilder.Build(WslGatewayControlAction.Start), runner.LastDistroCommand);
37+
Assert.Equal(WslGatewayControlCommandBuilder.BuildArguments(), runner.LastDistroCommand);
38+
Assert.Equal(
39+
WslGatewayControlCommandBuilder.BuildStandardInput(WslGatewayControlAction.Start),
40+
runner.LastStandardInput);
3741
}
3842

3943
[Fact]
@@ -70,7 +74,10 @@ public async Task RunAsync_AttemptsCommand_WhenDistroEnumerationIsEmpty()
7074

7175
Assert.True(result.Success);
7276
Assert.Equal("OpenClawGateway", runner.LastDistroName);
73-
Assert.Equal(WslGatewayControlCommandBuilder.Build(WslGatewayControlAction.Restart), runner.LastDistroCommand);
77+
Assert.Equal(WslGatewayControlCommandBuilder.BuildArguments(), runner.LastDistroCommand);
78+
Assert.Equal(
79+
WslGatewayControlCommandBuilder.BuildStandardInput(WslGatewayControlAction.Restart),
80+
runner.LastStandardInput);
7481
Assert.DoesNotContain("not registered", result.StandardError);
7582
}
7683

@@ -90,8 +97,11 @@ public async Task ForceRestartAsync_TerminatesDistroThenColdRestarts()
9097
Assert.Equal(1, runner.TerminateCount); // host-side terminate happened first
9198
Assert.Equal("OpenClawGateway", runner.LastTerminatedDistro);
9299
Assert.Equal( // then a cold in-distro restart
93-
WslGatewayControlCommandBuilder.Build(WslGatewayControlAction.Restart),
100+
WslGatewayControlCommandBuilder.BuildArguments(),
94101
runner.LastDistroCommand);
102+
Assert.Equal(
103+
WslGatewayControlCommandBuilder.BuildStandardInput(WslGatewayControlAction.Restart),
104+
runner.LastStandardInput);
95105
}
96106

97107
[Fact]
@@ -169,6 +179,7 @@ private sealed class FakeWslCommandRunner : IWslCommandRunner
169179
public Queue<WslCommandResult>? InDistroResults { get; init; }
170180
public string? LastDistroName { get; private set; }
171181
public IReadOnlyList<string>? LastDistroCommand { get; private set; }
182+
public string? LastStandardInput { get; private set; }
172183
public int InDistroCount { get; private set; }
173184
public int TerminateCount { get; private set; }
174185
public string? LastTerminatedDistro { get; private set; }
@@ -202,10 +213,12 @@ public Task<WslCommandResult> RunInDistroAsync(
202213
string name,
203214
IReadOnlyList<string> command,
204215
CancellationToken cancellationToken = default,
205-
IReadOnlyDictionary<string, string>? environment = null)
216+
IReadOnlyDictionary<string, string>? environment = null,
217+
string? standardInput = null)
206218
{
207219
LastDistroName = name;
208220
LastDistroCommand = command;
221+
LastStandardInput = standardInput;
209222
InDistroCount++;
210223
var result = InDistroResults is { Count: > 0 } ? InDistroResults.Dequeue() : Result;
211224
return Task.FromResult(result);

0 commit comments

Comments
 (0)