Skip to content

Commit 95ed082

Browse files
committed
fix(profile): 修正 OnIdle 事件中变量作用域问题
在 Register-EngineEvent 的 Action 脚本块中无法直接访问 script 作用域变量。 改为通过 -MessageData 参数传递所需数据,在 Action 内通过 $Event.MessageData 获取。
1 parent c2965c1 commit 95ed082

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

profile/core/loadModule.ps1

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,25 @@ $env:PSModulePath = ($uniquePaths.ToArray()) -join $sep
5858

5959
# ── 第 3 层:OnIdle 事件延迟全量加载(空闲时静默加载完整 psutils 模块) ──
6060
# 将 wrapper.ps1 的加载也合并到此处
61-
$script:__PsutilsManifestPath = $moduleManifest
62-
$script:__WrapperScriptPath = Join-Path (Split-Path -Parent $PSScriptRoot) 'wrapper.ps1'
63-
Register-EngineEvent -SourceIdentifier PowerShell.OnIdle -MaxTriggerCount 1 -Action {
61+
# 注意:Action 脚本块运行在独立作用域,不能访问 $script: 变量,
62+
# 必须通过 -MessageData 传递数据,在 Action 中用 $Event.MessageData 获取
63+
$onIdleData = @{
64+
ManifestPath = $moduleManifest
65+
WrapperPath = Join-Path (Split-Path -Parent $PSScriptRoot) 'wrapper.ps1'
66+
}
67+
Register-EngineEvent -SourceIdentifier PowerShell.OnIdle -MaxTriggerCount 1 -MessageData $onIdleData -Action {
68+
$data = $Event.MessageData
6469
try {
6570
# 全量加载 psutils 模块(覆盖单独加载的子模块,补全其余子模块)
66-
Import-Module $script:__PsutilsManifestPath -Force -Global -ErrorAction Stop
71+
Import-Module $data.ManifestPath -Force -Global -ErrorAction Stop
6772
}
6873
catch {
6974
Write-Warning "[profile/loadModule.ps1] OnIdle psutils 全量加载失败: $($_.Exception.Message)"
7075
}
7176
try {
7277
# 延迟加载 wrapper.ps1(yaz, Add-CondaEnv 等函数)
73-
if (Test-Path $script:__WrapperScriptPath) {
74-
. $script:__WrapperScriptPath
78+
if (Test-Path $data.WrapperPath) {
79+
. $data.WrapperPath
7580
}
7681
}
7782
catch {

0 commit comments

Comments
 (0)