PowerShell / PowerShell/PSScriptAnalyzer

Cannot access a closed file

未关闭
#577 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Issue - Discussion
主要语言
C#
星标
2.2k
派生
414
平均合并
13 小时 1 分钟
30 天内合并 PR
2

描述

I'm occasionally getting this error on running Invoke-ScriptAnalyzer with v 1.6.0

Invoke-ScriptAnalyzer : Cannot access a closed file.

The Full Error Object is

writeErrorStream      : True
PSMessageDetails      : 
Exception             : System.ObjectDisposedException: Cannot access a closed file.
                           at System.IO.__Error.FileNotOpen()
                           at System.IO.FileStream.Flush(Boolean flushToDisk)
                           at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
                           at System.Management.Automation.Host.TranscriptionOption.Dispose()
                           at System.Management.Automation.Host.PSHostUserInterface.StopAllTranscribing()
                           at System.Management.Automation.Runspaces.LocalRunspace.DoCloseHelper()
                           at System.Management.Automation.Runspaces.RunspaceBase.CoreClose(Boolean syncCall)
                           at System.Management.Automation.Runspaces.LocalRunspace.Close()
                           at System.Management.Automation.Runspaces.LocalRunspace.Dispose(Boolean disposing)
                           at System.Management.Automation.PowerShell.Dispose(Boolean disposing)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetModuleManifest(String filePath, IEnumerable`1& errorRecord)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.MissingModuleManifestField.<AnalyzeScript>d__0.MoveNext()
                           at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
                           at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass63_0.<AnalyzeSyntaxTree>b__1()
TargetObject          : C:\Users\Ryan\Onedrive\Github\PowerShellModules\PesterHelpers\PesterHelpers.psd1
CategoryInfo          : InvalidOperation: (C:\Users\Ryan\O...terHelpers.psd1:String) [Invoke-ScriptAnalyzer], ObjectDisposedException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, C:\Users\Ryan\Onedrive\Github\PowerShellModules\PesterHelpers\PesterHelpers.tests.ps1: line 34
                        at Invoke-Test, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\It.ps1: line 253
                        at ItImpl, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\It.ps1: line 203
                        at It, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\It.ps1: line 117
                        at <ScriptBlock>, C:\Users\Ryan\Onedrive\Github\PowerShellModules\PesterHelpers\PesterHelpers.tests.ps1: line 33
                        at Describe, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Functions\Describe.ps1: line 100
                        at <ScriptBlock>, C:\Users\Ryan\Onedrive\Github\PowerShellModules\PesterHelpers\PesterHelpers.tests.ps1: line 15
                        at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Pester.psm1: line 279
                        at Invoke-Pester, C:\Program Files\WindowsPowerShell\Modules\Pester\3.4.0\Pester.psm1: line 292
                        at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {0, 1}

This seems only to fire occasionally on the following rules with the error message given above

PSUseToExportFieldsInManifest
PSMissingModuleManifestField

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从堆栈跟踪中提到的 Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetModuleManifest 以及 MissingModuleManifestField 和 PSUseToExportFieldsInManifest 规则开始。使用所引用的 PesterHelpers.psd1 或另一个模块清单重现 Invoke-ScriptAnalyzer,然后跟踪错误中显示的 runspace 和 transcription 释放过程。完成的标准是这些规则不再间歇性地报告 ObjectDisposedException。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp, powershell
领域
devtools
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。