From 222557a6cc4f74a3e1217bf2fae846bf28f6e62e Mon Sep 17 00:00:00 2001 From: Patrick Meinecke Date: Tue, 14 Apr 2026 18:47:18 -0400 Subject: [PATCH 1/4] Stop catastrophic drive errors in AppContainers --- .../engine/Modules/ModuleIntrinsics.cs | 4 ++- .../namespaces/FileSystemProvider.cs | 27 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs index 301ee8c3b49..538c4775f0a 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs @@ -967,7 +967,9 @@ internal static string GetPersonalModulePath() #if UNIX return Platform.SelectProductNameForDirectory(Platform.XDG_Type.USER_MODULES); #else - string myDocumentsPath = InternalTestHooks.SetMyDocumentsSpecialFolderToBlank ? string.Empty : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); + string myDocumentsPath = InternalTestHooks.SetMyDocumentsSpecialFolderToBlank + ? string.Empty + : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments, Environment.SpecialFolderOption.DoNotVerify); return string.IsNullOrEmpty(myDocumentsPath) ? null : Path.Combine(myDocumentsPath, Utils.ModuleDirectory); #endif } diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 71de4e8ac67..4f1bff11fba 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -569,7 +569,7 @@ protected override PSDriveInfo NewDrive(PSDriveInfo drive) if (driveIsFixed) { // Since the drive is fixed, ensure the root is valid. - validDrive = Directory.Exists(drive.Root); + validDrive = IsValidFixedDrive(drive.Root); } if (validDrive) @@ -908,7 +908,7 @@ protected override Collection InitializeDefaultDrives() if (newDrive.DriveType == DriveType.Fixed) { - if (!newDrive.RootDirectory.Exists) + if (!IsValidFixedDrive(newDrive.RootDirectory.FullName)) { continue; } @@ -1227,6 +1227,29 @@ protected override void GetItem(string path) } } + private static bool IsValidFixedDrive(string rootDirectory) + { + if (Directory.Exists(rootDirectory)) + { + return true; + } + + try + { + return (File.GetAttributes(rootDirectory) & FileAttributes.Directory) is not 0; + } + // In some scenarios (like AppContainers) direct access to the root directory may + // be prevented, but more specific paths may be accessible. + catch (UnauthorizedAccessException) + { + return true; + } + catch + { + return false; + } + } + private FileSystemInfo GetFileSystemItem(string path, ref bool isContainer, bool showHidden) { path = NormalizePath(path); From 67c51fb4fa649235861d0e3c216d82cc5a78a97c Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 20 Apr 2026 11:47:34 -0400 Subject: [PATCH 2/4] Apply suggestion from @SeeminglyScience Co-authored-by: Patrick Meinecke --- .../namespaces/FileSystemProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 4f1bff11fba..520a31dfb2f 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -569,7 +569,7 @@ protected override PSDriveInfo NewDrive(PSDriveInfo drive) if (driveIsFixed) { // Since the drive is fixed, ensure the root is valid. - validDrive = IsValidFixedDrive(drive.Root); + validDrive = SafeDoesPathExist(drive.Root); } if (validDrive) From df648395e89894b77cd14890ee7b59a42855b6a6 Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 20 Apr 2026 11:47:46 -0400 Subject: [PATCH 3/4] Apply suggestion from @SeeminglyScience Co-authored-by: Patrick Meinecke --- .../namespaces/FileSystemProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 520a31dfb2f..848a5d408be 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -908,7 +908,7 @@ protected override Collection InitializeDefaultDrives() if (newDrive.DriveType == DriveType.Fixed) { - if (!IsValidFixedDrive(newDrive.RootDirectory.FullName)) + if (!SafeDoesPathExist(newDrive.RootDirectory.FullName)) { continue; } From 4a9d76cc7381978c425444972613fd0e62b7b94c Mon Sep 17 00:00:00 2001 From: Travis Plunk Date: Mon, 20 Apr 2026 11:48:03 -0400 Subject: [PATCH 4/4] Apply suggestion from @SeeminglyScience Co-authored-by: Patrick Meinecke --- .../namespaces/FileSystemProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 848a5d408be..05c8fff76e0 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -1227,7 +1227,7 @@ protected override void GetItem(string path) } } - private static bool IsValidFixedDrive(string rootDirectory) + private static bool SafeDoesPathExist(string rootDirectory) { if (Directory.Exists(rootDirectory)) {