Skip to content

Write-Warning instead of returning artificial DiagnosticRecord when type parsing errors occurs since those DiagnosticRecords cannot be suppressed. #1126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Engine/ScriptAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,8 +1667,7 @@ private List<ParseError> RemoveTypeNotFoundParseErrors(ParseError[] parseErrors,
}
else
{
diagnosticRecords.Add(new DiagnosticRecord(
string.Format(Strings.TypeNotFoundParseErrorFound, parseError.Extent), parseError.Extent, "TypeNotFound", DiagnosticSeverity.Information, parseError.Extent.File));
outputWriter.WriteWarning(string.Format(Strings.TypeNotFoundParseErrorFound, parseError.Extent, parseError.Extent.StartLineNumber, parseError.Extent.File));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Engine/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Engine/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,6 @@
<value>Settings object could not be resolved.</value>
</data>
<data name="TypeNotFoundParseErrorFound" xml:space="preserve">
<value>Ignoring 'TypeNotFound' parse error on type '{0}'. Check if the specified type is correct. This can also be due the type not being known at parse time due to types imported by 'using' statements.</value>
<value>Ignoring 'TypeNotFound' parse error of type '{0}' in line {1} of file '{2}'. Check if the specified type is correct. This can also be due the type not being known at parse time due to types imported by 'using' statements.</value>
</data>
</root>
29 changes: 15 additions & 14 deletions Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ Describe "Test Path" {
$numFilesResult | Should -Be $numFilesExpected
}
}

Context "When piping in files" {
It "Can be piped in from a string" {
$piped = ("$directory\TestScript.ps1" | Invoke-ScriptAnalyzer)
$explicit = Invoke-ScriptAnalyzer -Path $directory\TestScript.ps1

$piped.Count | Should Be $explicit.Count
}

It "Can be piped from Get-ChildItem" {
$piped = ( Get-ChildItem -Path $directory -Filter TestTestPath*.ps1 | Invoke-ScriptAnalyzer)
$explicit = Invoke-ScriptAnalyzer -Path $directory\TestTestPath*.ps1
Expand Down Expand Up @@ -410,7 +410,7 @@ Describe "Test CustomizedRulePath" {
Pop-Location
}
}

It "resolves rule preset when passed in via pipeline" {
$warnings = 'CodeFormattingStroustrup' | ForEach-Object {
Invoke-ScriptAnalyzer -ScriptDefinition 'if ($true){}' -Settings $_}
Expand Down Expand Up @@ -552,8 +552,8 @@ Describe "Test -EnableExit Switch" {
else {
$result = powershell -command 'Invoke-Scriptanalyzer -ScriptDefinition gci -ReportSummary'
}
"$result" | Should -BeLike $reportSummaryFor1Warning

"$result" | Should -BeLike $reportSummaryFor1Warning
}
It "does not print the report summary when not using -NoReportSummary switch" {
if ($IsCoreCLR) {
Expand All @@ -562,32 +562,33 @@ Describe "Test -EnableExit Switch" {
else {
$result = powershell -command 'Invoke-Scriptanalyzer -ScriptDefinition gci'
}
"$result" | Should -Not -BeLike $reportSummaryFor1Warning

"$result" | Should -Not -BeLike $reportSummaryFor1Warning
}
}

# using statements are only supported in v5+
if (!$testingLibraryUsage -and ($PSVersionTable.PSVersion -ge [Version]'5.0.0')) {
Describe "Handles parse errors due to unknown types" {
$expectedWarning = "Ignoring 'TypeNotFound' parse error of type 'IStorageContext' in line 4 of file '*"
$script = @'
using namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels
using namespace Microsoft.Azure.Commands.Common.Authentication.Abstractions
Import-Module "AzureRm"
class MyClass { [IStorageContext]$StorageContext } # This will result in a parser error due to [IStorageContext] type that comes from the using statement but is not known at parse time
'@
It "does not throw and detect one expected warning after the parse error has occured when using -ScriptDefintion parameter set" {
$warnings = Invoke-ScriptAnalyzer -ScriptDefinition $script
$warnings.Count | Should -Be 1
$warnings.RuleName | Should -Be 'TypeNotFound'
Invoke-ScriptAnalyzer -ScriptDefinition $script -WarningVariable warning 3> $null | Should -BeNullOrEmpty
$warning.Count | Should -Be 1
$warning[0] | Should -BeLike $expectedWarning
}

$testFilePath = "TestDrive:\testfile.ps1"
Set-Content $testFilePath -value $script
It "does not throw and detect one expected warning after the parse error has occured when using -Path parameter set" {
$warnings = Invoke-ScriptAnalyzer -Path $testFilePath
$warnings.Count | Should -Be 1
$warnings.RuleName | Should -Be 'TypeNotFound'
Invoke-ScriptAnalyzer -Path $testFilePath -WarningVariable warning 3> $null | Should -BeNullOrEmpty
$warning.Count | Should -Be 1
$warning[0] | Should -BeLike $expectedWarning
}
}
}
Expand Down