Skip to content

Commit

Permalink
Fix broken warning (it never appears if any .NET analyzers are presen…
Browse files Browse the repository at this point in the history
…t) (#642)

* Fix broken warning (never appears if any .NET analyzers are present)

* whitespace

* Use GetRules to inspect loaded rules

* Always list used analyzers

* add readme note

* Update link

* Fix rebase
  • Loading branch information
ErikEJ authored Oct 24, 2024
1 parent 3fe3a62 commit 947557d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ To publish your own custom rules, pack your rule .dll in a NuGet package as show

We know of the following public rules NuGet packages, that you can add to your project.

> These rule sets were included with the SDK in version 2.7.x and 2.8.x, but must be added explicitly with SDK version 2.9.x and later.
```xml
<ItemGroup>
<PackageReference Include="ErikEJ.DacFX.SqlServer.Rules" Version="1.1.0" />
Expand Down
19 changes: 12 additions & 7 deletions src/DacpacTool/PackageAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ public void Analyze(TSqlModel model, FileInfo outputFile, FileInfo[] analyzers)
var factory = new CodeAnalysisServiceFactory();
var settings = new CodeAnalysisServiceSettings();

if (analyzers.Length == 0)

if (analyzers.Length > 0)
{
_console.WriteLine("DacpacTool warning SQLPROJ0001: No additional rules files found, consider adding more rules via PackageReference - see the readme here: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj.");
settings.AssemblyLookupPath = string.Join(';', analyzers.Select(a => a.DirectoryName));
}
else

var service = factory.CreateAnalysisService(model, settings);

var rules = service.GetRules();

if (!rules.Any(r => r.Namespace == "SqlServer.Rules" || r.Namespace == "Smells"))
{
_console.WriteLine("Using additional analyzers: " + string.Join(", ", analyzers.Select(a => a.FullName)));
settings.AssemblyLookupPath = string.Join(';', analyzers.Select(a => a.DirectoryName));
_console.WriteLine("DacpacTool warning SQLPROJ0001: No additional well-known rules files found, consider adding more rules via PackageReference - see the readme here: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj/blob/master/README.md#static-code-analysis");
}

_console.WriteLine("Using analyzers: " + string.Join(", ", rules.Select(a => a.Namespace).Distinct()));

var projectDir = Environment.CurrentDirectory;
var suppressorPath = Path.Combine(projectDir, ProjectProblemSuppressor.SuppressionFilename);
Expand All @@ -61,8 +68,6 @@ public void Analyze(TSqlModel model, FileInfo outputFile, FileInfo[] analyzers)
}
}

var service = factory.CreateAnalysisService(model, settings);

if (_ignoredRules.Count > 0
|| _ignoredRuleSets.Count > 0
|| suppressedProblems.Count > 0)
Expand Down
21 changes: 12 additions & 9 deletions test/DacpacTool.Tests/DacpacTool.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
</ItemGroup>

<ItemGroup>
<Content Include="SqlServer.Dac.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="SqlServer.Rules.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Suppression\proc1.sql">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand All @@ -30,9 +24,6 @@
<Content Include="Suppression\StaticCodeAnalysis.SuppressMessages.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="TSQLSmellSCA.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand All @@ -52,4 +43,16 @@
<ProjectReference Include="..\..\src\DacpacTool\DacpacTool.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="SqlServer.Dac.dll">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="SqlServer.Rules.dll">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Update="TSQLSmellSCA.dll">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
22 changes: 16 additions & 6 deletions test/DacpacTool.Tests/PackageAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.SqlServer.Dac.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
Expand Down Expand Up @@ -112,9 +113,9 @@ public void RunsAnalyzerWithWarningsAsErrorsUsingWildcard()
// Assert
testConsole.Lines.Count.ShouldBe(16);

testConsole.Lines.Count(l => l.Contains("Using additional analyzers: ")).ShouldBe(1);
testConsole.Lines.Count(l => l.Contains("Using analyzers: ")).ShouldBe(1);
testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.ShouldNotContain("DacpacTool warning SQLPROJ0001: No additional rules files found, consider adding more rules via PackageReference - see the readme here: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj.");
testConsole.Lines.ShouldNotContain("DacpacTool warning SQLPROJ0001: No additional well-known rules files found, consider adding more rules via PackageReference - see the readme here: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj/blob/master/README.md#static-code-analysis");
testConsole.Lines.ShouldContain($"proc1.sql(1,47): Error SRD0006 : SqlServer.Rules : Avoid using SELECT *.");
testConsole.Lines.ShouldContain($"-1(1,1): Error SRD0002 : SqlServer.Rules : Table does not have a primary key.");
testConsole.Lines.Count(l => l.Contains("): Error ")).ShouldBe(2);
Expand All @@ -134,10 +135,11 @@ public void RunsAnalyzerWithoutAdditionalAnalyzers()
packageAnalyzer.Analyze(result.model, result.fileInfo, Array.Empty<FileInfo>());

// Assert
testConsole.Lines.Count.ShouldBe(16);
testConsole.Lines.Count.ShouldBe(6);

testConsole.Lines[1].ShouldBe("DacpacTool warning SQLPROJ0001: No additional rules files found, consider adding more rules via PackageReference - see the readme here: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj.");
testConsole.Lines[1].ShouldBe("DacpacTool warning SQLPROJ0001: No additional well-known rules files found, consider adding more rules via PackageReference - see the readme here: https://github.com/rr-wfm/MSBuild.Sdk.SqlProj/blob/master/README.md#static-code-analysis");
testConsole.Lines.ShouldContain($"Analyzing package '{result.fileInfo.FullName}'");
testConsole.Lines.Count(l => l.Contains("Using analyzers: ")).ShouldBe(1);
testConsole.Lines.Count(l => l.Contains("): Error ")).ShouldBe(0);
testConsole.Lines.ShouldContain($"Successfully analyzed package '{result.fileInfo.FullName}'");
}
Expand Down Expand Up @@ -175,7 +177,7 @@ public void RunsAnalyzerWithSuppressionFile()
}

// Assert
testConsole.Lines.Count.ShouldBe(25);
testConsole.Lines.Count.ShouldBe(16);

testConsole.Lines.Count(l => l.Contains("Warning SR0001 : Microsoft.Rules.Data")).ShouldBe(1);
}
Expand All @@ -195,11 +197,19 @@ private static (FileInfo fileInfo, TSqlModel model) BuildSimpleModel()
private FileInfo[] CollectAssemblyPaths()
{
var result = new List<FileInfo>();
var path = Path.GetDirectoryName(Path.Combine(System.Reflection.Assembly.GetAssembly(typeof(PackageAnalyzerTests)).Location));
var path = ProjectSource.ProjectDirectory();
result.Add(new FileInfo(Path.Combine(path, "SqlServer.Rules.dll")));
result.Add(new FileInfo(Path.Combine(path, "TSQLSmellSCA.dll")));

return result.ToArray();
}
}

internal static class ProjectSource
{
private static string CallerFilePath([CallerFilePath] string callerFilePath = null) =>
callerFilePath ?? throw new ArgumentNullException(nameof(callerFilePath));

public static string ProjectDirectory() => Path.GetDirectoryName(CallerFilePath())!;
}
}

0 comments on commit 947557d

Please sign in to comment.