diff --git a/src/System.Management.Automation/engine/CommandPathSearch.cs b/src/System.Management.Automation/engine/CommandPathSearch.cs index bdb64e59d..d05ecc283 100644 --- a/src/System.Management.Automation/engine/CommandPathSearch.cs +++ b/src/System.Management.Automation/engine/CommandPathSearch.cs @@ -50,25 +50,33 @@ namespace System.Management.Automation ExecutionContext context, Collection acceptableCommandNames) { + string[] commandPatterns; if (acceptableCommandNames != null) { // The name passed in is not a pattern. To minimize enumerating the file system, we // turn the command name into a pattern and then match against extensions in PATHEXT. // The old code would enumerate the file system many more times, once per possible extension. - // Porting note: this is wrong on Linux, where we don't depend on extensions if (Platform.IsWindows) { - commandName = commandName + ".*"; + commandPatterns = new [] { commandName + ".*" }; + } + else + { + // Porting note: on non-Windows platforms, we want to always allow just 'commandName' + // as an acceptable command name. However, we also want to allow commands to be + // called with the .ps1 extension, so that 'script.ps1' can be called by 'script'. + commandPatterns = new [] { commandName + ".ps1", commandName }; } this.postProcessEnumeratedFiles = CheckAgainstAcceptableCommandNames; this.acceptableCommandNames = acceptableCommandNames; } else { + commandPatterns = new [] { commandName }; postProcessEnumeratedFiles = JustCheckExtensions; } - - Init(new [] { commandName }, lookupPaths, context); + + Init(commandPatterns, lookupPaths, context); this.orderedPathExt = CommandDiscovery.PathExtensionsWithPs1Prepended; } diff --git a/test/powershell/NativeLinuxCommands.Tests.ps1 b/test/powershell/NativeLinuxCommands.Tests.ps1 index b7168579b..b8beced83 100644 --- a/test/powershell/NativeLinuxCommands.Tests.ps1 +++ b/test/powershell/NativeLinuxCommands.Tests.ps1 @@ -16,3 +16,24 @@ Describe "NativeLinuxCommands" { (get-command touch).CommandType | Should Be Application } } + +Describe "Scripts with extensions" { + BeforeAll { + $data = "Hello World" + Setup -File testScript.ps1 -Content "'$data'" + $originalPath = $env:PATH + $env:PATH += [IO.Path]::PathSeparator + $TestDrive + } + + AfterAll { + $env:PATH = $originalPath + } + + It "Should run a script with its full name" { + testScript.ps1 | Should Be $data + } + + It "Should run a script with its short name" { + testScript | Should Be $data + } +}