Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -908,7 +908,7 @@ protected override Collection<PSDriveInfo> InitializeDefaultDrives()

if (newDrive.DriveType == DriveType.Fixed)
{
if (!newDrive.RootDirectory.Exists)
if (!IsValidFixedDrive(newDrive.RootDirectory.FullName))
{
continue;
}
Expand Down Expand Up @@ -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;
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The enum flag check using pattern matching (is not 0) is less conventional for bit flags and may be inconsistent with other flag checks in this codebase. Using the standard comparison form (!= 0) tends to be clearer and more idiomatic for FileAttributes bitmasks.

Suggested change
return (File.GetAttributes(rootDirectory) & FileAttributes.Directory) is not 0;
return (File.GetAttributes(rootDirectory) & FileAttributes.Directory) != 0;

Copilot uses AI. Check for mistakes.
}
// 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;
}
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

If the intent is to treat "access denied" as "drive is valid" for restricted sandboxes, consider also handling System.Security.SecurityException the same way as UnauthorizedAccessException. Some sandbox/policy configurations can surface as SecurityException, and returning false there could reintroduce the drive being skipped/treated invalid.

Suggested change
}
}
catch (SecurityException)
{
return true;
}

Copilot uses AI. Check for mistakes.
catch
{
return false;
}
Comment on lines +1247 to +1250
Copy link

Copilot AI Apr 14, 2026

Choose a reason for hiding this comment

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

The bare catch will also swallow unexpected/fatal exceptions (e.g., OutOfMemoryException, StackOverflowException can’t be caught, but others like ThreadAbortException in legacy scenarios) and makes failures harder to diagnose. Prefer catching only the expected exception types from File.GetAttributes (e.g., IOException, ArgumentException, NotSupportedException, PathTooLongException) and let truly unexpected exceptions bubble.

Suggested change
catch
{
return false;
}
catch (IOException)
{
return false;
}
catch (ArgumentException)
{
return false;
}
catch (NotSupportedException)
{
return false;
}
catch (PathTooLongException)
{
return false;
}

Copilot uses AI. Check for mistakes.
}

private FileSystemInfo GetFileSystemItem(string path, ref bool isContainer, bool showHidden)
{
path = NormalizePath(path);
Expand Down
Loading