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

Custom Static Analysis Rules with DacFx 150 Not Executed on Build from VS 2022 Command Line #364

Closed
chlafreniere opened this issue Nov 20, 2023 · 6 comments
Assignees
Labels
bug Something isn't working fixed-pending-release Fix in upcoming release
Milestone

Comments

@chlafreniere
Copy link

chlafreniere commented Nov 20, 2023

Repro steps to be added subsequently

Steps to Reproduce:

  1. Create custom rules using older versions of dacfx (150)
  2. Copy the dlls to C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\Extensions
  3. Create a sqlproject with a script that won't be valid according to the rules you created
  4. make sure True is set in the project
  5. run "msbuild [your project].sqlproj /p:rebuild

Expected: Build should show the message from the custom rule as warning
Actual: There's no warning
Did this occur in prior versions? If not - which version(s) did it work in?

(DacFx/SqlPackage/SSMS/Azure Data Studio)

@chlafreniere chlafreniere added the bug Something isn't working label Nov 20, 2023
@chlafreniere chlafreniere added this to the 162.2 milestone Nov 20, 2023
@llali llali changed the title Custom Static Analysis Rules Not Executed on Build from VS 2022 Command Line Custom Static Analysis Rules with DacFx 150 Not Executed on Build from VS 2022 Command Line Nov 28, 2023
@llali llali self-assigned this Nov 28, 2023
@llali
Copy link
Member

llali commented Nov 28, 2023

I verified building the same rules with DacFx 160, which is the one VS 2022 uses, fixes the problem. The reason that rules compiled with DacFx 150 cannot be loaded in VS 2022 is that MEF for some reason cannot match the types. Here's the debug message from MEF trying to load the custom rules: The import requires metadata 'PlatformCompatibility' to be of type 'Microsoft.SqlServer.Dac.Extensibility.TSqlPlatformCompatibility' but the provided value 'All' is of type 'Microsoft.SqlServer.Dac.Extensibility.TSqlPlatformCompatibility'

@llali
Copy link
Member

llali commented Nov 28, 2023

as workaround, rebuild the custom rules using DacFx 160

@NathanMSFT
Copy link

@llali - This issue stems from a support case where we observed this issue with the custom rules (SqlServer.Rules) from https://github.com/tcartwright/SqlServer.Rules/releases. Are you suggesting that it's not a problem with VS 2022/SSDT, but the way the custom rules were built/compiled? I'm not familiar with this aspect of Custom Rules, so I'm requesting clarification. Is this something that the custom rules developer must do?

@chlafreniere
Copy link
Author

Here are instructions that @llali created and I'm passing along for how to get the rules built in the SqlServer.Rules repo and get them to play nicely with VS 2022 (and its command-line equivalent):

  • Clone tcartwright/SqlServer.Rules: SQL Server static code analysis rules for SSDT database projects (github.com)

  • replace any version "150.5164.1" to "162.1.167". This is the dacfx version the project was using.

  • there are some old packages that need to be updated to System.Runtime.CompilerServices.Unsafe to 6.0.0

  • run msbuild /restore

  • run msbuild

  • get the dlls from git\SqlServer.Rules\SqlServer.Rules\bin\Debug\net472 and copy to C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\Extensions\SqlServer.Rules

    • I cleaned everything in C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\Microsoft\SQLDB\DAC\Extensions\SqlServer.Rules so now it only has these:

    image

    • Now if you run the build on SQL Project, you will see the rules are applied

Root cause appears to be that MEF has a problem with assembly version changes (150.x.x vs 160.x.x)

@NathanMSFT
Copy link

Thanks @chlafreniere and @llali for your work on this!!!

@llali
Copy link
Member

llali commented Jan 8, 2024

as mentioned above this is by design. If the rules are compiled with DacFx 150, they cannot be used in a SQL project building with dacfx 160. Please compile the rules with latest version of dacfx. additional warnings are added to build diagnostic logs so user gets some information about why rules are not loaded

@llali llali added the fixed-pending-release Fix in upcoming release label Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed-pending-release Fix in upcoming release
Projects
None yet
Development

No branches or pull requests

4 participants