FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Refactor the module path construction code to make it more robust and easier to maintain by daxian-dbw · Pull Request #26565 · PowerShell/PowerShell · GitHub

Repository navigation

Refactor the module path construction code to make it more robust and easier to maintain - #26565

Merged
Dongbo Wang (daxian-dbw) merged 9 commits into
PowerShell:masterfrom
daxian-dbw:debug
Dec 12, 2025
Merged

Dongbo Wang (daxian-dbw) merged 9 commits into
PowerShell:masterfrom
daxian-dbw:debug

Conversation

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

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

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
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
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 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
Dongbo Wang (daxian-dbw) marked this pull request as ready for review December 10, 2025 00:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

// Local function to normalize a path by removing trailing directory separators.
static string NormalizePath(string path)
{
string normalizedPath = path.TrimEnd(Path.DirectorySeparatorChar);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Collaborator

Ready to merge.

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

Copy link
Copy Markdown
Member Author

Ilya (@iSazonov) Thanks for the review!

Ryan Yates (kilasuit) pushed a commit to kilasuit/PowerShell that referenced this pull request Jan 2, 2026
Justin Grote (JustinGrote) pushed a commit to JustinGrote/PowerShell that referenced this pull request Jun 2, 2026
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Arch Linux Technical User (archlinux-github) pushed a commit to archlinux/aur that referenced this pull request Oct 31, 2026
- 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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

3 participants


Back | FazBrowse Home | New Git URL