Skip to content

An exception is thrown when CurrentCulture is Turkish (tr-TR) #1095

Closed
@alatas

Description

@alatas

I got an exception when trying to use Invoke-Formatter command. And also, VSCode extension couldn't format the ps1 files, or couldn't analyze the files. When I called the formatter command from the console an exception is thrown that "The given key 'ıncluderules' was not present in the dictionary."

I searched the source code and found that the exception is related to this line:

var key = settingKey.ToLower();

There is a problem with the ToLower function in .net when the CurrentCulture is Turkish (tr-TR). Turkish charset is an alternate version of latin-1 charset. Turkish alphabet has ı and i characters which are different from each other and has different uppercase forms. I is the uppercase form of ı, and İ is the uppercase form of i. Therefore, ToLower function has an unexpected behaviour different than the latin-1 charset. When the CurrentCulture is (tr-TR), Include becomes ınclude (not include) which causes this exception.

Steps to reproduce

Invoke-Formatter -ScriptDefinition "`$test"

Expected behavior

$test

Actual behavior

Invoke-Formatter : The given key 'ıncluderules' was not present in the dictionary.
At line:1 char:1
+ Invoke-Formatter -ScriptDefinition "`$test"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (CodeFormatting:String) [Invoke-Formatter], KeyNotFoundException
+ FullyQualifiedErrorId : SETTINGS_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand

PS 6.1

PSMessageDetails      :
Exception             : System.Collections.Generic.KeyNotFoundException: The given key 'ıncluderules' was not present i
                        n the dictionary.
                           at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Settings.parseSettingsHashtable(Hashtable set
                        tingsHashtable)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Settings.parseSettingsFile(String settingsFil
                        ePath)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Settings.Create(Object settingsObj, String cw
                        d, IOutputWriter outputWriter, GetResolvedProviderPathFromPSPath`3 getResolvedProviderPathFromP
                        SPathDelegate)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand.BeginProcessi
                        ng()
TargetObject          : CodeFormatting
CategoryInfo          : InvalidData: (CodeFormatting:String) [Invoke-Formatter], KeyNotFoundException
FullyQualifiedErrorId : SETTINGS_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

PS 5.1

PSMessageDetails      :
Exception             : System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionar
                        y.
                           at System.ThrowHelper.ThrowKeyNotFoundException()
                           at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Settings.parseSettingsHashtable(Hashtable set
                        tingsHashtable)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Settings.parseSettingsFile(String settingsFil
                        ePath)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Settings.Create(Object settingsObj, String cw
                        d, IOutputWriter outputWriter, GetResolvedProviderPathFromPSPath`3 getResolvedProviderPathFromP
                        SPathDelegate)
                           at Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand.BeginProcessi
                        ng()
TargetObject          : CodeFormatting
CategoryInfo          : InvalidData: (CodeFormatting:String) [Invoke-Formatter], KeyNotFoundException
FullyQualifiedErrorId : SETTINGS_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeFormatterCommand
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0
PSEdition                      Core
GitCommitId                    6.1.0
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions