Skip to content

Commit ac88eaf

Browse files
committed
Fix: Improved WhereIsCommand by handling null/whitespace paths.
1 parent bdbf54d commit ac88eaf

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

DTC.Core

G33kShell.Desktop/Console/WindowManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public void ProcessEvents()
225225
lock (m_consoleEvents)
226226
{
227227
var visualTree = GetVisualTree(Root).ToArray();
228-
foreach (var consoleEvent in m_consoleEvents)
228+
foreach (var consoleEvent in m_consoleEvents.ToArray())
229229
{
230230
if (consoleEvent is KeyConsoleEvent keyEvent && keyEvent.Direction == KeyConsoleEvent.KeyDirection.Down)
231231
{

G33kShell.Desktop/Terminal/Commands/ExecutableCommand.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ public ExecutableCommand([NotNull] string[] args)
3131

3232
public bool CanExecute(DirectoryInfo cwd)
3333
{
34-
var command = m_args.FirstOrDefault();
35-
return !string.IsNullOrWhiteSpace(command) && WhereIsCommand.FindExecutables(command, cwd).Any();
34+
try
35+
{
36+
var command = m_args.FirstOrDefault();
37+
return !string.IsNullOrWhiteSpace(command) && WhereIsCommand.FindExecutables(command, cwd).Any();
38+
}
39+
catch
40+
{
41+
// Something went wrong - We can't execute the command.
42+
return false;
43+
}
3644
}
3745

3846
protected override async Task<bool> Run(ITerminalState state)

G33kShell.Desktop/Terminal/Commands/WhereIsCommand.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,23 @@ public static IEnumerable<FileInfo> FindExecutables(string name, DirectoryInfo c
6262
};
6363
paths.AddRange(pathEnv?.Split(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ';' : ':') ?? Array.Empty<string>());
6464

65-
foreach (var dir in paths.Select(o => o.ToDir()).Where(o => o.Exists()))
65+
foreach (var dir in paths.Where(o => !string.IsNullOrWhiteSpace(o)).Select(o => o.ToDir()).Where(o => o.Exists()))
6666
{
6767
var searchPattern = name;
6868
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && string.IsNullOrEmpty(Path.GetExtension(name)))
6969
searchPattern = name + ".*";
70+
71+
FileInfo[] files;
72+
try
73+
{
74+
files = dir.GetFiles(searchPattern);
75+
}
76+
catch (Exception)
77+
{
78+
// Invalid search pattern - Assume not found.
79+
files = Array.Empty<FileInfo>();
80+
}
7081

71-
var files = dir.GetFiles(searchPattern);
7282
foreach (var fileInfo in files.Where(o => o.IsExecutable()))
7383
yield return fileInfo;
7484
}

G33kShell.Desktop/Terminal/TerminalState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private async Task ExecuteAsync(string cmdString)
102102
command = (CommandBase)parsedCommand.PrimaryCommand.InstantiatedCommand;
103103
}
104104

105-
await command.SetState(this).ExecuteAsync(new CancellationToken());
105+
await command.SetState(this).ExecuteAsync(CancellationToken.None);
106106

107107
// Prepare the next prompt for input.
108108
PrepareNextInputPrompt();

0 commit comments

Comments
 (0)