PowerShell / PowerShell/vscode-powershell

WriteObject and WriteError methods cannot be called from outside the overrides

Offen
#2,785 10 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Area-Code Formatting Issue-Bug
Vorherrschende Sprache
TypeScript
Sterne
1.9k
Forks
548
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Since months I've been ignoring this error but I decided to ask, maybe it's something that can be addressed.

Basically my script copies all files .ps1 content, merges them into single psm1, saves it in Temp folder and executes formatter:

[-] Format-Code - Formatting on file C:\Users\przemyslaw.klys\AppData\Local\Temp\PSSharedGoods\PSSharedGoods.psm1 failed. Error: The WriteObject and WriteError methods cannot be called from outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called from within the same thread. Validate that the cmdlet makes these calls correctly, or contact Microsoft Customer Support Services.
FormatterSettings = @{
                    IncludeRules = @(
                        'PSPlaceOpenBrace',
                        'PSPlaceCloseBrace',
                        'PSUseConsistentWhitespace',
                        'PSUseConsistentIndentation',
                        'PSAlignAssignmentStatement',
                        'PSUseCorrectCasing'
                    )

                    Rules        = @{
                        PSPlaceOpenBrace           = @{
                            Enable             = $true
                            OnSameLine         = $true
                            NewLineAfter       = $true
                            IgnoreOneLineBlock = $true
                        }

                        PSPlaceCloseBrace          = @{
                            Enable             = $true
                            NewLineAfter       = $false
                            IgnoreOneLineBlock = $true
                            NoEmptyLineBefore  = $false
                        }

                        PSUseConsistentIndentation = @{
                            Enable              = $true
                            Kind                = 'space'
                            PipelineIndentation = 'IncreaseIndentationAfterEveryPipeline'
                            IndentationSize     = 4
                        }

                        PSUseConsistentWhitespace  = @{
                            Enable          = $true
                            CheckInnerBrace = $true
                            CheckOpenBrace  = $true
                            CheckOpenParen  = $true
                            CheckOperator   = $true
                            CheckPipe       = $true
                            CheckSeparator  = $true
                        }

                        PSAlignAssignmentStatement = @{
                            Enable         = $true
                            CheckHashtable = $true
                        }

                        PSUseCorrectCasing         = @{
                            Enable = $true
                        }
                    }
                }

If an unexpected error was thrown then please report the full error details using e.g. $error[0] | Select-Object *

PSMessageDetails      :
Exception             : System.Management.Automation.PSInvalidOperationException: The WriteObject and WriteError methods cannot be called from
                        outside the overrides of the BeginProcessing, ProcessRecord, and EndProcessing methods, and they can only be called fro
                        m within the same thread. Validate that the cmdlet makes these calls correctly, or contact Microsoft Customer Support S
                        ervices.
                           at System.Management.Automation.MshCommandRuntime.ThrowIfWriteNotPermitted(Boolean needsToWriteToPipeline)
                           at System.Management.Automation.MshCommandRuntime.WriteHelper_ShouldWrite(ActionPreference preference, ContinueStatu
                        s lastContinueStatus)
                           at System.Management.Automation.MshCommandRuntime.WriteVerbose(VerboseRecord record, Boolean overrideInquire)
                           at System.Management.Automation.Cmdlet.WriteVerbose(String text)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.AnalyzeSyntaxTree(ScriptBlockAst scriptAst, Token[] sc
                        riptTokens, String filePath, Boolean skipVariableAnalysis)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.AnalyzeScriptDefinition(String scriptDefinition, Scrip
                        tBlockAst& scriptAst, Token[]& scriptTokens, Boolean skipVariableAnalysis)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.Fix(EditableText text, Range range, Boolean skipParsin
                        g, Range& updatedRange, Boolean& fixesWereApplied, ScriptBlockAst& scriptAst, Token[]& scriptTokens, Boolean skipVariab
                        leAnalysis)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Formatter.Format[TCmdlet](String scriptDefinition, Settings settings,
                         Range range, TCmdlet cmdlet)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand.ProcessRecord()
                           at System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject          :
CategoryInfo          : InvalidOperation: (:) [Invoke-Formatter], PSInvalidOperationException
FullyQualifiedErrorId : InvalidOperation,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, C:\Support\GitHub\PSPublishModule\Private\Format-Code.ps1: line 24
                        at Write-TextWithTime, C:\Support\GitHub\PSPublishModule\Private\Write-TextWithTime.ps1: line 15
                        at Format-Code, C:\Support\GitHub\PSPublishModule\Private\Format-Code.ps1: line 21
                        at Merge-Module, C:\Support\GitHub\PSPublishModule\Private\Merge-Module.ps1: line 256
                        at Start-ModuleBuilding, C:\Support\GitHub\PSPublishModule\Private\Start-ModuleBuilding.ps1: line 258
                        at New-PrepareModule, C:\Support\GitHub\PSPublishModule\Public\New-PrepareModule.ps1: line 18
                        at <ScriptBlock>, C:\Support\GitHub\PSSharedGoods\Publish\Manage-PSSharedGoods.ps1: line 153

This error shows up usually (but not always) if during the script run I do some "actions" within VSCode. Basically I run the script to "build my module" and then I continue edits in code for README or some fixes within ps1 files which shouldn't affect the formatting because I don't touch PSM1 file in the temp folder. But maybe some conflicts happen because of me using it?

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.0
1.19.0

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, den Fehler mit Windows PowerShell 5.1 und PSScriptAnalyzer 1.19.0 zu reproduzieren, während du den Formatter über den gemeldeten Format-Code- und Invoke-Formatter-Pfad aufrufst. Untersuche die Stack-Frames von AnalyzeSyntaxTree und Formatter.Format und bestätige anschließend die Behebung, indem du den Workflow zur Modulformatierung erneut ausführst, ohne dass die Thread-Ausnahme von WriteObject/WriteError auftritt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
powershell
Bereich
tooling
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Muss geklärt werden
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.