2 Commits
Author SHA1 Message Date
poli0981 de3ed23687 Merge pull request #31 from poli0981/claude/vnext-fixes
fix: cmd user-command hang + recipe dupes + sidebar scrollbar
2026-06-19 16:33:16 +07:00
poli0981andClaude Opus 4.8 68e62f3f22 fix: cmd user-commands hang, recipe duplicate steps, sidebar scrollbar
Post-v1.1.0 fixes reported from testing.

- User commands hanging on cmd: SystemProcessRunner now redirects and
  immediately closes stdin, so an interactive process (e.g. "cmd" with
  no /c) gets EOF and exits instead of blocking forever. Additionally,
  UserCommandFactory.NormalizeArguments prepends /c for cmd (e.g. "cmd"
  + "ping google.com" -> "cmd /c ping google.com") so the command runs
  and exits; the run confirmation shows the effective command line.
  PowerShell and other executables are unchanged.
- Recipes: AddStep no longer adds a command already in the chain, so a
  recipe never runs one command multiple times.
- Sidebar: hide the vertical scrollbar on the command-groups list.
- Tests for the cmd /c normalization (cmd/cmd.exe/path, existing /c|/k,
  powershell, empty args) + ToDefinition applying it.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-19 16:30:27 +07:00
6 changed files with 93 additions and 7 deletions

No files matched your search

@@ -28,11 +28,42 @@ public static class UserCommandFactory
TitleKey = command.Name,
DescriptionKey = command.Name,
Executable = command.Executable,
ArgsTemplate = command.Arguments,
ArgsTemplate = NormalizeArguments(command.Executable, command.Arguments),
ExecutionMode = ExecutionMode.Capture,
RequiresAdmin = false, // INVARIANT — a user command must never elevate.
DangerLevel = DangerLevel.Caution,
Restart = RestartPolicy.No,
};
}
/// <summary>
/// Normalizes arguments so a one-shot command actually runs and then exits. <c>cmd.exe</c>
/// without <c>/c</c> (or <c>/k</c>) opens an interactive shell that blocks forever waiting on
/// stdin, so for cmd we prepend <c>/c</c>. Other executables (e.g. powershell) are left as-is.
/// </summary>
public static string NormalizeArguments(string executable, string? arguments)
{
ArgumentNullException.ThrowIfNull(executable);
var args = arguments ?? string.Empty;
if (IsCmd(executable) && !HasCmdRunSwitch(args))
{
return args.Length == 0 ? "/c" : "/c " + args;
}
return args;
}
private static bool IsCmd(string executable)
=> string.Equals(Path.GetFileNameWithoutExtension(executable.Trim()), "cmd", StringComparison.OrdinalIgnoreCase);
private static bool HasCmdRunSwitch(string arguments)
{
var trimmed = arguments.TrimStart();
return StartsWithSwitch(trimmed, "/c") || StartsWithSwitch(trimmed, "/k");
}
private static bool StartsWithSwitch(string text, string flag)
=> text.StartsWith(flag, StringComparison.OrdinalIgnoreCase)
&& (text.Length == flag.Length || char.IsWhiteSpace(text[flag.Length]));
}
@@ -25,6 +25,9 @@ internal sealed class SystemProcessRunner : IProcessRunner
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
// Redirect stdin so we can close it immediately: a process that reads input (e.g. an
// interactive "cmd" with no /c) gets EOF and exits instead of hanging forever.
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
// Pin the working directory to System32 so the CreateProcess search order can't
@@ -52,6 +55,11 @@ internal sealed class SystemProcessRunner : IProcessRunner
try
{
process.Start();
// Signal EOF on the child's stdin right away — nothing here ever feeds it input, and
// leaving it open lets an interactive process block indefinitely.
process.StandardInput.Close();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
@@ -135,9 +135,10 @@ public sealed partial class RecipesViewModel : ObservableObject
[RelayCommand]
private void AddStep()
{
if (CommandToAdd is not null)
// Avoid duplicate steps — a recipe runs each command once (don't run one command N times).
if (CommandToAdd is { } command && !Steps.Contains(command))
{
Steps.Add(CommandToAdd);
Steps.Add(command);
}
}
@@ -155,10 +155,12 @@ public sealed partial class UserCommandsViewModel : ObservableObject
Arguments = EditArguments.Trim(),
};
// Transparency (Security.md §3): show the exact command line, and that it runs without admin.
var commandLine = string.IsNullOrEmpty(userCommand.Arguments)
// Transparency (Security.md §3): show the exact command line that will run (including the
// cmd /c normalization applied by ToDefinition) and that it runs without admin.
var effectiveArgs = UserCommandFactory.NormalizeArguments(executable, userCommand.Arguments);
var commandLine = string.IsNullOrEmpty(effectiveArgs)
? executable
: executable + " " + userCommand.Arguments;
: executable + " " + effectiveArgs;
if (MessageBox.Show(
string.Format(CultureInfo.CurrentCulture, Strings.Get("UserCmd_RunConfirm"), commandLine),
Strings.Get("UserCmd_RunTitle"),
+2 -1
View File
@@ -231,7 +231,8 @@
<ListBox ItemsSource="{Binding Categories}" SelectedItem="{Binding SelectedCategory}"
Background="Transparent" BorderThickness="0" Padding="4,4"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Hidden">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="Padding" Value="12,8" />
@@ -0,0 +1,43 @@
using CommandForge.Application.UserCommands;
using CommandForge.Domain;
namespace CommandForge.Tests;
/// <summary>
/// Tests the cmd argument normalization that fixes user commands hanging on an interactive
/// cmd.exe (e.g. "cmd" + "ping google.com" must run as "cmd /c ping google.com").
/// </summary>
public sealed class UserCommandNormalizationTests
{
[Theory]
[InlineData("cmd", "ping google.com", "/c ping google.com")]
[InlineData("cmd.exe", "dir", "/c dir")]
[InlineData("CMD", "echo hi", "/c echo hi")]
[InlineData("C:\\Windows\\System32\\cmd.exe", "whoami", "/c whoami")]
[InlineData("cmd", "", "/c")]
public void NormalizeArguments_PrependsSlashC_ForCmd(string exe, string args, string expected)
=> Assert.Equal(expected, UserCommandFactory.NormalizeArguments(exe, args));
[Theory]
[InlineData("cmd", "/c ping x")] // already has /c
[InlineData("cmd", "/k keep open")] // explicit /k
[InlineData("cmd", "/C UPPER")] // case-insensitive switch
[InlineData("powershell", "ping x")] // not cmd — leave as-is (powershell runs it)
[InlineData("ping", "google.com")] // direct executable
public void NormalizeArguments_LeavesAsIs_WhenNotPlainCmd(string exe, string args)
=> Assert.Equal(args, UserCommandFactory.NormalizeArguments(exe, args));
[Fact]
public void ToDefinition_AppliesCmdNormalization()
{
var def = UserCommandFactory.ToDefinition(new UserCommand
{
Id = "x",
Name = "n",
Executable = "cmd",
Arguments = "ping google.com",
});
Assert.Equal("/c ping google.com", def.ArgsTemplate);
}
}