Skip to content

Commit 6b78b35

Browse files
committed
fix(fragment): defer PathResolution import to avoid parse-time FileSystemPathType error
1 parent 5ba06e4 commit 6b78b35

2 files changed

Lines changed: 50 additions & 25 deletions

File tree

Microsoft.PowerShell_profile.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,29 @@ catch {
434434
# PSReadLine is loaded lazily by profile.d/psreadline.ps1 to improve startup performance.
435435
# Call Enable-PSReadLine to load PSReadLine with enhanced configuration.
436436

437+
# ===============================================
438+
# COMMON ENUMS - MUST BE LOADED FIRST
439+
# ===============================================
440+
# Import CommonEnums before any module that uses FileSystemPathType or other enums
441+
# This ensures types are available at parse time for modules like Validation
442+
if (-not $profileDir) {
443+
$profileDir = Split-Path -Parent $PSCommandPath
444+
}
445+
$commonEnumsModule = Join-Path $profileDir 'scripts' 'lib' 'core' 'CommonEnums.psm1'
446+
if ($commonEnumsModule -and -not [string]::IsNullOrWhiteSpace($commonEnumsModule) -and (Test-Path -LiteralPath $commonEnumsModule)) {
447+
try {
448+
Import-Module $commonEnumsModule -DisableNameChecking -Force -Global -ErrorAction SilentlyContinue
449+
}
450+
catch {
451+
if ($env:PS_PROFILE_DEBUG) {
452+
$debugLevel = 0
453+
if ([int]::TryParse($env:PS_PROFILE_DEBUG, [ref]$debugLevel) -and $debugLevel -ge 1) {
454+
Write-Warning "Failed to load CommonEnums module: $($_.Exception.Message). Some modules may fail to load."
455+
}
456+
}
457+
}
458+
}
459+
437460
# ===============================================
438461
# PowerShell Profile - Custom Aliases & Functions
439462
# ===============================================

scripts/lib/fragment/FragmentConfig.psm1

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,32 +65,11 @@ else {
6565
}
6666
}
6767

68-
# Import PathResolution for path operations
68+
# PathResolution import is deferred - only loaded when Get-ProfileDirectory is actually needed
69+
# This avoids parse-time errors when FileSystemPathType is not yet available
6970
# PathResolution is in scripts/lib/path/, not scripts/lib/fragment/
70-
$pathModulePath = Join-Path (Split-Path -Parent $PSScriptRoot) 'path' 'PathResolution.psm1'
71-
if (Get-Command Import-ModuleSafely -ErrorAction SilentlyContinue) {
72-
$null = Import-ModuleSafely -ModulePath $pathModulePath -ErrorAction SilentlyContinue
73-
}
74-
else {
75-
# Fallback to manual validation
76-
if ($pathModulePath -and -not [string]::IsNullOrWhiteSpace($pathModulePath) -and (Test-Path -LiteralPath $pathModulePath)) {
77-
try {
78-
Import-Module $pathModulePath -ErrorAction Stop
79-
}
80-
catch {
81-
$debugLevel = 0
82-
if ($env:PS_PROFILE_DEBUG -and [int]::TryParse($env:PS_PROFILE_DEBUG, [ref]$debugLevel)) {
83-
if ($debugLevel -ge 2) {
84-
Write-Verbose "[fragment-config.init] Failed to import PathResolution module: $($_.Exception.Message). Path resolution features may be limited."
85-
}
86-
# Level 3: Log detailed error information
87-
if ($debugLevel -ge 3) {
88-
Write-Host " [fragment-config.init] PathResolution import error details - Exception: $($_.Exception.GetType().FullName), Message: $($_.Exception.Message)" -ForegroundColor DarkGray
89-
}
90-
}
91-
}
92-
}
93-
}
71+
# Note: PathResolution uses Validation which requires FileSystemPathType from CommonEnums
72+
# CommonEnums should be imported early in the profile before this module is loaded
9473

9574
# Import Logging for consistent output
9675
# Logging is in scripts/lib/core/, not scripts/lib/fragment/
@@ -168,6 +147,29 @@ function Get-FragmentConfig {
168147
if (-not $ConfigPath) {
169148
if (-not $ProfileDir) {
170149
# Try to resolve from current context
150+
# Lazy-load PathResolution only when Get-ProfileDirectory is needed
151+
if (-not (Get-Command Get-ProfileDirectory -ErrorAction SilentlyContinue)) {
152+
# Try to import PathResolution if not already available
153+
$pathModulePath = Join-Path (Split-Path -Parent $PSScriptRoot) 'path' 'PathResolution.psm1'
154+
if ($pathModulePath -and -not [string]::IsNullOrWhiteSpace($pathModulePath) -and (Test-Path -LiteralPath $pathModulePath)) {
155+
try {
156+
if (Get-Command Import-ModuleSafely -ErrorAction SilentlyContinue) {
157+
$null = Import-ModuleSafely -ModulePath $pathModulePath -ErrorAction SilentlyContinue
158+
}
159+
else {
160+
Import-Module $pathModulePath -ErrorAction SilentlyContinue -DisableNameChecking
161+
}
162+
}
163+
catch {
164+
# PathResolution import failed - non-fatal, will use fallback
165+
$debugLevel = 0
166+
if ($env:PS_PROFILE_DEBUG -and [int]::TryParse($env:PS_PROFILE_DEBUG, [ref]$debugLevel) -and $debugLevel -ge 2) {
167+
Write-Verbose "[fragment-config.get] Failed to import PathResolution module: $($_.Exception.Message). Using fallback path resolution."
168+
}
169+
}
170+
}
171+
}
172+
171173
if (Get-Command Get-ProfileDirectory -ErrorAction SilentlyContinue) {
172174
try {
173175
$ProfileDir = Get-ProfileDirectory -ScriptPath $PSScriptRoot

0 commit comments

Comments
 (0)