GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Refactor the module path construction code to make it more robust and easier to maintain by daxian-dbw · Pull Request #26565 · PowerShell/PowerShell

Conversation

@daxian-dbw

@daxian-dbw Dongbo Wang (daxian-dbw) commented Dec 3, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

PR Summary

Fix #26536

There are 2 issues surfaced from #26536 that we need to address:

  1. The error reporting in the method ReportExceptionFallback(Exception e, string header) does not write out stack trace of the exception, which makes it hard to diagnose failures like ShellCannotBeStarted.
  2. The UpdatePath method used in module path construction doesn't handle the case where personalModulePathToUse may contain multiple paths separated by the PathSperator (user can define PSModulePath in powershell.config.json with such a value).

The fixes are:

  1. Write out stack trace of the exception in ReportExceptionFallback(Exception e, string header), at the end.
  2. Refactored the module path construction code to make it more robust and easier to maintain
    • Removed the old existing UpdatePath method.
    • Removed the old PathContainsSubstring method. Change to a difference approach that is much more efficient.
    • Refactored the AddPath method and rename it to be UpdatePath
    • Updated SetModulePath() to not blindly add a path separator when the environment variable PSModulePath is initially not set in all scopes.
    • Call SetModulePath() in the static constructor of ModuleIntrinsics instead of the instance constructor. So, we only need to construct the module path once per process (instead of once per a new Runspace).

With the changes, PowerShell won't crash even if the personal module path is like C:\Users\marcv\OneDrive - company & name namepart2\Dokumente\PowerShell\Modules as reported in the issue.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge. If this PR is a work in progress, please open this as a Draft Pull Request and mark it as Ready to Review when it is ready to merge.
  • Breaking changes
    • None
    • OR
    • Experimental feature(s) needed
      • Experimental feature name(s):
  • User-facing changes
    • Not Applicable
    • OR
    • Documentation needed
      • Issue filed:
  • Testing - New and feature
    • N/A or can only be tested interactively
    • OR
    • Make sure you've added a new test if existing tests do not effectively test the code changed

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

segore reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@daxian-dbw
Print the stack trace of the ShellCannotBeStarted error
114803b
@daxian-dbw Dongbo Wang (daxian-dbw) mentioned this pull request Dec 3, 2025
Error when starting powershell 7.4, 7.5 or 7.6; "Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter “startIndex”)" #26536
Closed
5 tasks done
Dongbo Wang (daxian-dbw) added 2 commits December 3, 2025 15:57
@daxian-dbw
Add more debug messages
ad1aa09
@daxian-dbw
Guard the debug message with env var
64acf5c
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Dec 7, 2025
@daxian-dbw
Refactor the module path construction code to make it more robust and…
866ee21
… easier to maintain
@daxian-dbw Dongbo Wang (daxian-dbw) changed the title [debug] Print the stack trace of the ShellCannotBeStarted error Refactor the module path construction code to make it more robust and easier to maintain Dec 9, 2025
@daxian-dbw Dongbo Wang (daxian-dbw) added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Dec 9, 2025
@microsoft-github-policy-service microsoft-github-policy-service Bot removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Dec 9, 2025
@daxian-dbw
Dongbo Wang (daxian-dbw) marked this pull request as ready for review December 10, 2025 00:07
@daxian-dbw
Dongbo Wang (daxian-dbw) requested review from Patrick Meinecke (SeeminglyScience), Aditya Patwardhan (adityapatwardhan) and Copilot December 10, 2025 00:07
Copilot started reviewing on behalf of Dongbo Wang (daxian-dbw) December 10, 2025 00:07
View session
Copilot AI reviewed Dec 10, 2025
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the module path construction code in PowerShell to fix issue #26536, which involves two main problems: insufficient error diagnostics in exception fallback handling and incorrect handling of multi-path PSModulePath values from configuration files. The refactoring moves module path initialization to a static constructor, consolidates path manipulation methods, and improves error reporting by including stack traces.

Key Changes

  • Moved module path initialization from instance constructor to static constructor for earlier, one-time initialization
  • Refactored UpdatePath method (previously split between AddToPath and a wrapper UpdatePath) to handle multiple semicolon/colon-separated paths in a single call
  • Enhanced ReportExceptionFallback to output complete stack traces including inner exceptions
  • Added test coverage for PSModulePath configuration with multiple paths

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 10 comments.

File Description
test/powershell/Host/ConsoleHost.Tests.ps1 Added test for PSModulePath with multiple paths from config file; contains platform-specific path separator issues
src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Refactored module path construction: moved initialization to static constructor, consolidated UpdatePath logic, improved PathContainsSubstring with better return semantics
src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs Enhanced exception reporting to include stack traces for debugging ShellCannotBeStarted errors; contains potential null reference issues

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Warrioroflove671 reacted with hooray emoji
All reactions
  • 🎉 1 reaction
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread test/powershell/Host/ConsoleHost.Tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread test/powershell/Host/ConsoleHost.Tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread test/powershell/Host/ConsoleHost.Tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw
Address comments from Copilot
96e7a32
iSazonov
Ilya (iSazonov) reviewed Dec 10, 2025
View reviewed changes
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw
Address comments and check if a path already exists more efficiently
45c1d4c
@daxian-dbw
Dongbo Wang (daxian-dbw) requested a review from Copilot December 10, 2025 19:48
Copilot started reviewing on behalf of Dongbo Wang (daxian-dbw) December 10, 2025 19:49
View session
Copilot AI reviewed Dec 10, 2025
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread test/powershell/Host/ConsoleHost.Tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw
Update the XML doc for a parameter based on Copilot's feedback
fd2ce82
iSazonov
Ilya (iSazonov) reviewed Dec 11, 2025
View reviewed changes

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On top of ModuleIntrinsics.cs I see we define Tracer. It would be nice to add to the trace source and result path strings for more easy diagnostics.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Outdated
// Local function to normalize a path by removing trailing directory separators.
static string NormalizePath(string path)
{
string normalizedPath = path.TrimEnd(Path.DirectorySeparatorChar);

@iSazonov Ilya (iSazonov) Dec 11, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use NormalizePath method from FileSystemProvider? Then we will follow the basic PowerShell behavior regarding directory separators.
Otherwise, we could use Path.TrimEndingDirectorySeparator()

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

daxian-dbw reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@daxian-dbw Dongbo Wang (daxian-dbw) Dec 11, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Path.TrimEndingDirectorySeparator doesn't remove duplicate trailing directory separators. It only removes the ending one. But I think it should be practically fine, and also, I favor over simplicity. We can get back to it if it turns out to be an issue.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw
Address Ilya's comments
5610fc3
@iSazonov
Ilya (iSazonov) requested a review from Copilot December 11, 2025 19:18
Copilot started reviewing on behalf of Ilya (iSazonov) December 11, 2025 19:19
View session
Copilot AI reviewed Dec 11, 2025
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
iSazonov
Ilya (iSazonov) reviewed Dec 11, 2025
View reviewed changes
Comment thread test/powershell/Host/ConsoleHost.Tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw
Address a comment
2af8081
iSazonov
Ilya (iSazonov) approved these changes Dec 12, 2025
View reviewed changes
@iSazonov

Ilya (iSazonov) commented Dec 12, 2025

Copy link
Copy Markdown
Collaborator

Ready to merge.

segore reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 592668b into PowerShell:master Dec 12, 2025
36 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@daxian-dbw
Dongbo Wang (daxian-dbw) deleted the debug branch December 12, 2025 17:43
@daxian-dbw

Dongbo Wang (daxian-dbw) commented Dec 12, 2025

Copy link
Copy Markdown
Member Author

Ilya (@iSazonov) Thanks for the review!

segore and iSazonov reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Ryan Yates (kilasuit) pushed a commit to kilasuit/PowerShell that referenced this pull request Jan 2, 2026
@daxian-dbw @kilasuit
Refactor the module path construction code to make it more robust and…
54483ac
… easier to maintain (PowerShell#26565)
This was referenced Apr 23, 2026
ReleaseAutomationTest new failures in 7.7.0-preview.1: module-path / MyDocuments test assumptions (3 distinct cases) #27343
Closed
Test failures from module-path refactor PR #26565: tests 103778 / 103842 / 108309 fail on ZIP/TAR/portable distribution lanes #27344
Closed
Test failures from module-path refactor PR #26565: tests 103778 / 103842 / 108309 fail on ZIP/TAR/portable distribution lanes #27345
Closed
Test failures from module-path refactor PR #26565: tests 103778 / 103842 / 108309 fail on ZIP/TAR/portable distribution lanes #27346
Closed
Justin Grote (JustinGrote) pushed a commit to JustinGrote/PowerShell that referenced this pull request Jun 2, 2026
@daxian-dbw @JustinGrote
Refactor the module path construction code to make it more robust and…
c1fe0c0
… easier to maintain (PowerShell#26565)
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@daxian-dbw
Refactor the module path construction code to make it more robust and…
9105255
… easier to maintain (PowerShell#26565)
@ChrisTX Christian Pfeiffer (ChrisTX) mentioned this pull request Oct 28, 2026
Error starting PowerShell 7.4.0+ #20706
Closed
5 tasks done
Arch Linux Technical User (archlinux-github) pushed a commit to archlinux/aur that referenced this pull request Oct 31, 2026
@ChrisTX @claude
powershell: reference the upstream fix for the PSModulePath patch
64cf6a1
- Note that PowerShell/PowerShell#26565 fixed the crash in 7.7 and that the
  patch can be dropped with 7.7

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ChrisTX Christian Pfeiffer (ChrisTX) mentioned this pull request Nov 3, 2026
7.6: startup crash with a multi-path PSModulePath in powershell.config.json (fixed in 7.7 by #26565) #28306
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@iSazonov Ilya (iSazonov) iSazonov approved these changes
@adityapatwardhan Aditya Patwardhan (adityapatwardhan) Awaiting requested review from adityapatwardhan
@SeeminglyScience Patrick Meinecke (SeeminglyScience) Awaiting requested review from SeeminglyScience

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Error when starting powershell 7.4, 7.5 or 7.6; "Index was out of range. Must be non-negative and less than or equal to the size of the collection. (Parameter “startIndex”)"

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@daxian-dbw @iSazonov
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.