Skip to content
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

Fix broken warning (it never appears if any .NET analyzers are present) #642

Merged
merged 7 commits into from
Oct 24, 2024
Merged
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
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())!;
}
}