7.6: startup crash with a multi-path PSModulePath in powershell.config.json (fixed in 7.7 by #26565)

Open
#28,306 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
cli

Research direction

Start with the PSModulePath handling in ModuleIntrinsics.cs, in UpdatePath and AddToPath, and the PathContainsSubstring call that returns -1 when the added path holds several entries. Reproduce by setting two paths in powershell.config.json and starting pwsh, which should throw the startIndex error. Done when that case starts cleanly, the order of the paths is kept, and the existing Pester and xUnit module-path tests pass. The maintainers have not yet chosen between backporting #26565 and the smaller fix, so confirm the approach on the issue first.

Written by the indexing model from the issue text.

Description

Review - Maintainer
Summary

#20706 is fixed in 7.7 by #26565, but 7.6.x, the current LTS, still crashes. This asks for a fix in 7.6, either by backporting #26565 or with the smaller fix below.

Steps to reproduce (7.6.9)
  1. Set PSModulePath in powershell.config.json to two paths, joined with the path separator (; or :).
  2. Start pwsh from pwsh, or use ForEach-Object -Parallel, so $env:PSModulePath is already set.
Actual behaviour
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')
Options
  1. Backport #26565, together with the test fixes it needed (#27343, #27344). It is a larger refactor.

  2. A smaller fix in ModuleIntrinsics.cs, which only touches the two places that break:

    • UpdatePath: PathContainsSubstring(path, pathToAdd) returns -1 when pathToAdd holds several paths, so path.IndexOf(Path.PathSeparator, -1) throws. Look up the last entry of pathToAdd instead.
    • AddToPath: after inserting an entry, move insertPosition past it, so the paths keep their order, and compare it with result.Length instead of basePath.Length.

    The full Pester and xUnit suites pass on 7.6.9 with it. The diff: https://github.com/ChrisTX/aur-packages/blob/master/powershell/psmodulepath-combined-paths.patch

I can open a PR against the 7.6 release branch for whichever option you prefer.

Dominant language
C#
Stars
56.1k
Forks
8.5k
Avg merge
12h 33m
Merged PRs (30d)
73

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PowerShell/PowerShell

All issues in PowerShell/PowerShell

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.