Skip to content

Analyzers apply to all target frameworks specified by conditional based on configuration #9639

Open
@SoggyBottomBoy

Description

@SoggyBottomBoy

Summary

Code analyzers are producing errors and recommending fixes that aren't compatible with all my target frameworks, and I have yet to find a good solution. I am creating a plugin for an external application that uses a different runtime based on version. I need to write code that is both conditional on the APIs for the version of the application and runtime target.

The issue with my setup is that when I switch from net48 to a net8.0 configurations analyzers are recommending me changes that is incompatible with net48. It doesn't make much sense for me to add <TargetFrameworks>net48;net8.0-windows</TargetFrameworks> as it doubles build times (something I may be able to live with if there is no better solution).

My setup looks like the following.
 

<PropertyGroup>
  <Configurations>Debug_23;Debug_24;Debug_25;Release_23;Release_24;Release_25;</Configurations>
</PropertyGroup>

<PropertyGroup Condition="$(Configuration.EndsWith('23'))">
  <TargetFramework>net48</TargetFramework>
  <AppVersion>2023</AppVersion>
</PropertyGroup>

<PropertyGroup Condition="$(Configuration.EndsWith('24'))">
  <TargetFramework>net48</TargetFramework>
  <AppVersion>2024</AppVersion>
</PropertyGroup>

<PropertyGroup Condition="$(Configuration.EndsWith('25'))">
  <TargetFramework>net8.0-windows</TargetFramework>
  <AppVersion>APP2025</AppVersion>
</PropertyGroup>

<ItemGroup>
  <Reference Include="AppAPI" Condition="'$(AppVersion)' == '2023'">
    <HintPath>..\libs\2023\AppAPI.dll</HintPath>
    <Private>false</Private>
  </Reference>

  <Reference Include="AppAPI" Condition="'$(AppVersion)' == '2024'">
    <HintPath>..\libs\2024\AppAPI.dll</HintPath>
    <Private>false</Private>
  </Reference>

  <Reference Include="AppAPI" Condition="'$(AppVersion)' == '2025'">
    <HintPath>..\libs\2025\AppAPI.dll</HintPath>
    <Private>false</Private>
  </Reference>
</ItemGroup>

I'm wondering if there is someway to specify net48;net8.0-windows but only ever build one of these based on configuration. Or whether VS can scan for TargetFramework based on conditions and provide analyzers that are compatible with both. For simplicity if I can write the code that is compatible with net48 with reasonable performance I would prefer this vs branching logic based on framework.

User Impact

Analyzers giving incorrect code fixes leading to a less than ideal solution in which I either need to
a) having to add them to a list of ignores every time I encounter 1
b) reduce analyzer mode to try prevent this behaviour

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions