Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error starting PowerShell 7.4.0+ #20706

Open
5 tasks done
giggio opened this issue Nov 16, 2023 · 17 comments · May be fixed by #24568
Open
5 tasks done

Error starting PowerShell 7.4.0+ #20706

giggio opened this issue Nov 16, 2023 · 17 comments · May be fixed by #24568
Labels
In-PR Indicates that a PR is out for the issue Needs-Triage The issue is new and needs to be triaged by a work group.

Comments

@giggio
Copy link

giggio commented Nov 16, 2023

Prerequisites

Steps to reproduce

  1. Install PowerShell 7.4.0
  2. Start it
  3. Create a powershell.config.json and set PSModulePath to multiple paths, separated by ; (path separator char) (see reference).
  4. See it fail

Expected behavior

It starts, like 7.3 did.

Actual behavior

It fails with:

PowerShell 7.4.0
The shell cannot be started. A failure occurred during initialization:
Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'startIndex')

Error details

Not possible.

Environment data

Not possible, but it is version 7.4.0.

Visuals

This is starting from Nushell:

from nu

This is starting it from Windows Terminal:

from wt

Additional context

Downloaded and installed this file: https://github.com/PowerShell/PowerShell/releases/download/v7.4.0/PowerShell-7.4.0-win-x64.msi

I debugged and found out that the exception is ocurring at this location (ModuleIntrinsics.UpdatePath):

insertIndex = path.IndexOf(Path.PathSeparator, PathContainsSubstring(path, pathToAdd));

The arguments for UpdatePath are:
path: a string with a ; separated list of paths
pathToAdd: another string with a ; separated list of paths
insertIndex: 0

The call that is failing is the one to path.IndexOf, which is passed ; and -1 (which is the result from the call to PathContainsSubstring).

The problem only happens because pathToAdd has several paths with the ; separator. If it was a single path without ; it would work.

The issue seems to have been introduced in commit 3710671.

@giggio giggio added the Needs-Triage The issue is new and needs to be triaged by a work group. label Nov 16, 2023
@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented Nov 16, 2023

Can you check with "Add or Remove programs" settings that there is only one PowerShell MSI installed, and you don't have both 7.3.9 and 7.4.0?

The reason I say that is I found I had both installed, one with a normal MSI icon, the other with a PowerShell icon.

@giggio
Copy link
Author

giggio commented Nov 16, 2023

@rhubarb-geek-nz I did, I hadn't.

@B-Art
Copy link

B-Art commented Nov 18, 2023

There are at least 4 ways to install Pwsh on Windows:
From MSStore, via MSI, via Winget and Unzipping the download.
Which one did you use @giggio ?
https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.4

@giggio
Copy link
Author

giggio commented Nov 18, 2023

First I tried via winget. Then via the MSI download. Same result.

@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented Nov 22, 2023

First I tried via winget. Then via the MSI download. Same result.

Have you tried completely uninstalling the previous MSI first? Not ideal I know, in-place updates should work. Use "Add or Remove programs" to see what is installed, and also check the directory "c:\Program Files\PowerShell\7"

@giggio
Copy link
Author

giggio commented Nov 22, 2023

Have you tried completely uninstalling the previous MSI first? Not ideal I know, in-place updates should work. Use "Add or Remove programs" to see what is installed, and also check the directory "c:\Program Files\PowerShell\7"

I have.

I have now also tried installing the Microsoft Store Version, and I get the same error:
image

@giggio
Copy link
Author

giggio commented Nov 22, 2023

Tried now using the global tool, it also does not work:

image

I installed 7.3.10, as it still works. :(

@rhubarb-geek-nz
Copy link

rhubarb-geek-nz commented Nov 22, 2023

Alas, there are enough other problems with 7.4.0 to suggest sticking with 7.3.10 ( for the moment ).

@giggio
Copy link
Author

giggio commented Jan 25, 2024

The problem persists at version 7.4.1, released more recently.

image

@giggio
Copy link
Author

giggio commented Jan 25, 2024

Also, tried using version 7.5 preview from the store, also broken.

image

@giggio
Copy link
Author

giggio commented Mar 13, 2024

I have tracked the error and added the explanation of why it is happening to the additional context section of the issue.

@giggio giggio changed the title Error starting PowerShell 7.4.0 Error starting PowerShell 7.4.0+ Mar 13, 2024
giggio added a commit to giggio/PowerShell that referenced this issue Mar 13, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the In-PR Indicates that a PR is out for the issue label Mar 13, 2024
@Alvaka-KThor
Copy link

I am also experiencing this issue. I've had to use PSModulePath environment variable within System Properties in the meantime, which is not a perfect workaround.

giggio added a commit to giggio/poshfiles that referenced this issue Apr 29, 2024
There is a bug in PowerShell that is making it fail.
See: PowerShell/PowerShell#20706
@w-boyd
Copy link

w-boyd commented May 27, 2024

On Linux:

ForEach-Object: /home/vsts/work/_temp/f3a905a8-4984-469a-b3ba-7a9ba49e76af.ps1:5
Line |
   5 |  "whatever" | ForEach-Object -Parallel { $_ }
     |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | An unexpected error has occurred while processing ForEach-Object
     | -Parallel input. This may mean that some of the piped input did not get
     | processed. Error: System.ArgumentOutOfRangeException: Index was out of
     | range. Must be non-negative and less than or equal to the size of the
     | collection. (Parameter 'startIndex')    at System.String.IndexOf(Char
     | value, Int32 startIndex, Int32 count)    at
     | System.Management.Automation.ModuleIntrinsics.UpdatePath(String path,
     | String pathToAdd, Int32& insertIndex)    at
     | System.Management.Automation.ModuleIntrinsics.GetModulePath(String
     | currentProcessModulePath, String hklmMachineModulePath, String
     | hkcuUserModulePath)    at
     | System.Management.Automation.ModuleIntrinsics.SetModulePath()    at
     | System.Management.Automation.ModuleIntrinsics..ctor(ExecutionContext
     | context)    at
     | System.Management.Automation.ExecutionContext.InitializeCommon(AutomationEngine engine, PSHost hostInterface)    at System.Management.Automation.AutomationEngine..ctor(PSHost hostInterface, InitialSessionState iss)    at System.Management.Automation.Runspaces.LocalRunspace.DoOpenHelper()    at System.Management.Automation.Runspaces.RunspaceBase.CoreOpen(Boolean syncCall)    at System.Management.Automation.PSTasks.PSTaskPool.GetRunspace(Int32 taskId)    at System.Management.Automation.PSTasks.PSTaskPool.Add(PSTaskBase task)    at Microsoft.PowerShell.Commands.ForEachObjectCommand.<InitParallelParameterSet>b__67_2(Object _).

On Windows:

"C:\Program Files\PowerShell\7\pwsh.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\86498521-8914-4ea7-ba0c-67b8bb7a6633.ps1'"
The shell cannot be started. A failure occurred during initialization:
Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'startIndex')
##[error]PowerShell exited with code '70'.

Azure DevOps pipeline yaml to reproduce the issue:

jobs:
- job: windows
  pool:
      vmImage: windows-latest
  steps:
  - pwsh: |
      $filePath = Join-Path $env:SYSTEM_DEFAULTWORKINGDIRECTORY "powershellmodules"
      $separator = $isWindows ? ";" : ":"   
      $updatedPSModulePath = $env:PSModulePath, $filePath -join $separator
      $powershellConfigPath = Join-Path $PSHOME 'powershell.config.json'
      if (-not (Test-Path $powershellConfigPath)){
        New-Item -Path $powershellConfigPath -ItemType File -Force
        $config = @{
          PSModulePath = $updatedPSModulePath
        }
      }
      else {
        Write-Verbose "Found existing powershell config, updating PSModulePath"
        $config = Get-Content -Path $powershellConfigPath | ConvertFrom-Json -AsHashtable -Depth 100
        $config["PSModulePath"] = $updatedPSModulePath
      }
      Write-Verbose "Pushing content to $powershellConfigPath" -Verbose
      $config | ConvertTo-Json -Depth 100 | Tee-Object -FilePath $powershellConfigPath
    displayName: Update PSModulePath

  - pwsh: |
      Write-Verbose ('$env:PSModulePath {0}' -f $env:PSModulePath) -Verbose
      "whatever" | ForEach-Object -Parallel { $_ }
    displayName: Debug

- job: linux
  pool:
      vmImage: ubuntu-latest
  steps:
  - pwsh: |
      $filePath = Join-Path $env:SYSTEM_DEFAULTWORKINGDIRECTORY "powershellmodules"
      $separator = $isWindows ? ";" : ":"   
      $updatedPSModulePath = $env:PSModulePath, $filePath -join $separator
      $powershellConfigPath = Join-Path $PSHOME 'powershell.config.json'
      if (-not (Test-Path $powershellConfigPath)){
        New-Item -Path $powershellConfigPath -ItemType File -Force
        $config = @{
          PSModulePath = $updatedPSModulePath
        }
      }
      else {
        Write-Verbose "Found existing powershell config, updating PSModulePath"
        $config = Get-Content -Path $powershellConfigPath | ConvertFrom-Json -AsHashtable -Depth 100
        $config["PSModulePath"] = $updatedPSModulePath
      }
      Write-Verbose "Pushing content to $powershellConfigPath" -Verbose
      $config | ConvertTo-Json -Depth 100 | Tee-Object -FilePath $powershellConfigPath
    displayName: Update PSModulePath

  - pwsh: |
      Write-Verbose ('$env:PSModulePath {0}' -f $env:PSModulePath) -Verbose
      "whatever" | ForEach-Object -Parallel { $_ }
    displayName: Debug

@kilasuit kilasuit removed the In-PR Indicates that a PR is out for the issue label May 31, 2024
@wboyd-aw
Copy link

Any updates?

@hoffmanntravis
Copy link

I have reproduced this issue on Powershell 7.4.6 locally. This works:

{
    "PSModulePath": ":"
}

This does not:

{
    "PSModulePath": ";"
}

File Path:
C:\Users<user>\Documents\PowerShell\powershell.config.json

This seems like a clear case of regression. Please investigate and fix.

@hoffmanntravis
Copy link

hoffmanntravis commented Nov 6, 2024

Couple more notes. This is still broken in Powershell Preview 7.5:

PowerShell 7.5.0-preview.5
The shell cannot be started. A failure occurred during initialization:
Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter 'startIndex')

[process exited with code 70 (0x00000046)]
You can now close this terminal with Ctrl+D, or press Enter to restart.

Also, note that in the example I listed in my last comment, the semicolon delimiter by itself alone is enough to break the startup of powershell.

According to this document, ; should be the delimiting character:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_psmodulepath?view=powershell-7.4#modifying-psmodulepath

Editing the config file is described here:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_config?view=powershell-7.4#psmodulepath

The PSModulePath environment variable contains a list of folder locations that are searched to find modules and resources.
Configuration files for PowerShell, replacing Registry configuration.

@jborean93 jborean93 linked a pull request Nov 12, 2024 that will close this issue
21 tasks
@microsoft-github-policy-service microsoft-github-policy-service bot added the In-PR Indicates that a PR is out for the issue label Nov 12, 2024
@jborean93
Copy link
Collaborator

Opened #24568 to fix this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In-PR Indicates that a PR is out for the issue Needs-Triage The issue is new and needs to be triaged by a work group.
Projects
None yet
9 participants