-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Support multi-targeting for Roslyn components #20355
Comments
Would the analyzer build primarily run inner builds for the Roslyn versions, and then each of those would have a single target framework?
|
I think the authoring experience needs a separate proposal. At least, I didn't give any thought to the authoring experience above. When we do an authoring experience, I would imagine that we would run inner builds for each target Roslyn version the component indicates. With regards to the framework - I believe all Roslyn components should be targeting |
I meant, if a later version of Roslyn has APIs that cannot be used in analyzer projects that target netstandard2.0. But I suppose this won't happen as long as Visual Studio needs to be able to load Roslyn on .NET Framework. |
The Roslyn API does not currently differentiate between |
Visual Studio 2017 includes Roslyn version-2.10.0, in which csc.exe targets net46 and Microsoft.CodeAnalysis.dll targets netstandard1.3. I wrongly assumed that analyzers intended to be compatible with this version would likewise have to target netstandard1.3, which would complicate the multitargeting. However, now that I try it, an analyzer targeting netstandard2.0 works OK for warnings in Intellisense, for builds in the VS IDE, and for running csc.exe in a terminal. I see Visual Studio 2017 system requirements include .NET Framework 4.7.2, which I think installs netstandard.dll version 2 and thus satisfies the reference at run time. |
I spent some time implementing a prototype of this proposal. Please take a look if you are interested: eerhardt@c26ae1e. The drawback is that since the Analyzer asset selection is also duplicated in NuGet.BuildTasks, we would need to duplicate this logic there. Or, since we are keying off the MSBuild property |
@eerhardt is it an assumption here that we need to support VS 2019 for the inbox generators? i.e. if we only support VS 2021 for inbox generators what (if anything) is necessary? |
Why are we using the Roslyn version here instead of the .NET SDK version? The version of Roslyn is tied to the .NET SDK version and our overall customer message is around .NET SDK, not about individual component versions. This would seem to be another place where we should lean into that and use the SDK version vs. teaching customers about individual component versions. Note: scenarios like Microsoft.Net.Compilers.Toolset shouldn't be considered here. Those aren't supported scenarios . |
When the Logging and STJ source generators are used from the However, both of these source generators also ship in the corresponding NuGet packages. These NuGet packages are supported on
Partially because there are scenarios where the .NET SDK isn't involved. For example, But mostly because this is the version of <ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
</ItemGroup>
<None Include="..\InterfaceStubGenerator.Roslyn38\bin\$(Configuration)\netstandard2.0\InterfaceStubGeneratorV1.dll"
PackagePath="analyzers\roslyn3.8\cs"
Pack="true"
Visible="false" /> The two versions match. If we used the .NET SDK version in the
Turning the question back around: why isn't the version of |
Allows for Roslyn components (analyzers, source generators) to target mulltiple Roslyn API versions in a single package. The highest compatible asset is selected. Fix dotnet#20355
We spoke about this pretty early on in the design that you, me and @jmarolf talked about. There are I think a couple of compelling reasons not to use the SDK version:
I think given the fairly limited expected audience that are going to do this, along with the fact that they really want to reason about roslyn itself, we should use the API version here. In the future if we want to expand it out to more users we can go the SDK / targets path where we still use it but as an implementation detail hidden from the user. |
The compiler ships in a number of products of which the .NET SDK is not the most significant driver. The .NET SDK though for a given version is tied to a version of Roslyn. That is the explicit design of the SDK.
Gotcha. Given that we're changing the loading behavior here to make it more customizable, should we consider also allowing the user to customize based on the runtime the compiler will execute on? Essentially should we allow customers to deploy analyzers / generators that are multi-targeted to .NET Framework and .NET Core? That has been a semi-frequent ask of customers. I've been skeptical that it alone was enough to warrant changing our loading rules. But if we're already changing them is it cheap / timely to solve that problem too? |
I think whatever design we pick for multi-targeting roslyn apis needs to be able to accommodate multi-targeting based on runtime in the future. Though that does ask some wild questions. Is it NxM number of builds that we need to do for TFM and roslyn api version? |
I think runtime targeting is a nice to have, but it isn't blocking the inbox generators right now. I think we should keep this scoped to just the Roslyn version (given how far through the cycle we are) and look at doing runtime multitargeting in the NET 7 timeframe. I think the designs naturally stack together, so we don't have to do both at once. |
totally agree we should not design framework multi-targeting now. Just want to make sure the design we come up with will not make it impossible in the future |
Allows for Roslyn components (analyzers, source generators) to target mulltiple Roslyn API versions in a single package. The highest compatible asset is selected. Fix dotnet#20355
I have some reservations about this. We’ve said that targeting .NET 6 in VS 2019 is not supported, and we know there are ways it breaks. I know System.Text.Json supports other target frameworks, but it seems reasonable to me that if you want to use the System.Text.Json library that ships with .NET 6, you would have the same tooling requirements as .NET 6 itself. It also seems like adding this multi-targeting logic to the .NET 6 SDK doesn’t actually help with multi-targeting between VS 2019 and VS 2022, because VS 2019 doesn’t have the multi-targeting logic. So to get the scenario to work you would still need custom logic in .targets files in the NuGet packages. Adding analyzer multi-targeting support to .NET 6 might help for future versions of Roslyn, but even then the spec currently says that the multi-targeting will be based on the major/minor versions of the Microsoft.CodeAnalysis DLLs. Since an incomplete version of C# 10 is shipping in VS 16.11, wouldn’t that mean that even if we had the multitargeting logic there, the C# 10 versions of the analyzers / source generators would be picked up, and they might or might not work at all? I would also prefer to avoid adding more analyzer-convention-logic to the .NET SDK. The right place for that logic is in NuGet, which would then write the analyzers which applied to the project to the assets file for the SDK to consume: NuGet/Home#6279 I know there are probably other scenarios for multi-targeting analyzers and source generators besides just System.Text.Json, but that seems to be the reason we would need to do this now. I’d prefer to first figure out how to get NuGet to handle the analyzers, and then as part of that we could add support for multi-targeting. What do you think? |
To me, this isn't a reasonable approach because System.Text.Json also ships as a NuGet package. This allows developers to use the new functionality without forcing them to move to .NET 6. That's the whole reason we ship it in a NuGet package and support older TFMs. There is a big difference between targeting .NET 6 and updating NuGet package dependencies. It's reasonable to say "If you want to target .NET 6, use VS 2022". I don't think it is reasonable to say "if you want to update this NuGet package version, you need to also update your VS".
Agreed that it doesn't fully solve multi-targeting between VS 2019 and VS 2022. We will still need to ship custom logic in our NuGet packages. But that custom logic can be much simpler if this feature is in the .NET 6 SDK. If this feature is in the .NET 6 SDK, the custom NuGet logic would logically look like:
Because if the NuGet package is being used in an environment that doesn't have But if this feature isn't in the .NET 6 SDK, the custom NuGet logic needs to get much more complicated. It basically needs to do the multi-targeting logic itself: figure out the Roslyn version, select the most appropriate analyzer assembly, etc.
I'm not familiar with "an incomplete version of C# 10 is shipping in VS 16.11", but my understanding is that the Microsoft.CodeAnalysis version in VS 16.11 will always be
I agree that this isn't the ideal long-term location for this logic, but this is the place that is doing the language (
I'm not sure I'm convinced of that myself. Having NuGet need to understand all of the conventions used in NuGet packages is a limiting design IMO. We have proposals all the time for new conventions, and forcing NuGet to understand all of them forces too much burden in the package manager. (aside: extensions to .NET Interactive are loaded based on a convention based location in the NuGet package. I don't think NuGet should need to understand them.) IMO, the ideal long-term location for this logic would be in the Compiler/Roslyn targets. Roslyn owns the scenario of why people put
There is also reactiveui/refit#1216 which has already done its own multi-targeting for the same reasons as the |
I don't want to block you from going ahead with this if you think it's important and your team is willing to own the implementation and possible bug tail. I do want to comment on some of your points:
To me, it depends on the NuGet package. In this case the NuGet package depends on tooling features. If source generators were an entirely new feature in C# 10, then we wouldn't be trying to make them work with older versions of the compiler. We'd try to mitigate the experience issues where NuGet offers you a package update which won't work with your project. If you are using older releases of the tooling, or targeting older versions of the Framework, then you will get security fixes (if what you're using / targeting is still supported), but you aren't guaranteed to be able to use new features or new libraries. If you can, that's great, but I don't think we should jump through hoops to enable it.
I supposed that if an asset doesn't interact with the dependency graph, then it would be OK to implement the convention outside of NuGet. But if you want to control how an asset flows across edges in the dependency graph (ie via PrivateAssets or ExcludeAssets), then I think NuGet really needs to be the one handling it. It's been a long-standing bug that PrivateAssets and ExcludeAssets don't work with analyzers, but given that we haven't fixed it yet maybe it's not important. On the other hand maybe it is going to get more and more important now that analyzers and especially source generators are getting very popular. |
Allows for Roslyn components (analyzers, source generators) to target mulltiple Roslyn API versions in a single package. The highest compatible asset is selected. Fix dotnet#20355
* Support multi-targeting for Roslyn components Allows for Roslyn components (analyzers, source generators) to target mulltiple Roslyn API versions in a single package. The highest compatible asset is selected. Fix #20355 Undo changes to RunResolvePackageDependencies, since it only affects "legacy" behavior, which shouldn't be necessary to change. - Add more conditions to when resolving the Roslyn version runs - only for C# and VB projects, and only if the CodeAnalysis.dll file exists Refactor analyzer resolution logic to happen in one pass, instead of building up an exclusion list.
Fixed by #20793. |
[release/6.0-rc2] Update dependencies from dotnet/runtime - Update CreateFrameworkListFile to account for new analyzer path format introduced with dotnet/sdk#20355
[main] Update dependencies from dotnet/runtime dotnet/efcore - Update TFM to net7.0 - Fixup - Update src/Shared/CodeAnalysis/DynamicallyAccessedMembersAttribute.cs Co-authored-by: Chris Ross <chrross@microsoft.com> - Update src/Shared/CodeAnalysis/DynamicallyAccessedMemberTypes.cs Co-authored-by: Chris Ross <chrross@microsoft.com> - Update netfx to 462 - Update more to net462 - Fix workaround - Update ifdefs - Add workaround for KnownRuntimePack - Merge branch 'darc-main-c944b2c3-1c63-49a3-b9dd-98cda1a942e3' of https://github.com/dotnet/aspnetcore into darc-main-c944b2c3-1c63-49a3-b9dd-98cda1a942e3 - Merge branch 'main' into darc-main-c944b2c3-1c63-49a3-b9dd-98cda1a942e3 - Add SiteExtensions versions - Fix RepoTasks assembly dir - Update CreateFrameworkListFile to account for new analyzer path format introduced with dotnet/sdk#20355 - Don't target latest runtime in razor tests - Update Dependencies.props - Update Helix.targets - Fixup - Move FrameworkReference update to GenerateFiles - Have Helix tests import directory.build files - Keep RunTests at net6 - Quarantine 2 tests - revert spa-templates submodule downgrade
…aspnetcore#36328) [main] Update dependencies from dotnet/runtime dotnet/efcore - Update TFM to net7.0 - Fixup - Update src/Shared/CodeAnalysis/DynamicallyAccessedMembersAttribute.cs Co-authored-by: Chris Ross <chrross@microsoft.com> - Update src/Shared/CodeAnalysis/DynamicallyAccessedMemberTypes.cs Co-authored-by: Chris Ross <chrross@microsoft.com> - Update netfx to 462 - Update more to net462 - Fix workaround - Update ifdefs - Add workaround for KnownRuntimePack - Merge branch 'darc-main-c944b2c3-1c63-49a3-b9dd-98cda1a942e3' of https://github.com/dotnet/aspnetcore into darc-main-c944b2c3-1c63-49a3-b9dd-98cda1a942e3 - Merge branch 'main' into darc-main-c944b2c3-1c63-49a3-b9dd-98cda1a942e3 - Add SiteExtensions versions - Fix RepoTasks assembly dir - Update CreateFrameworkListFile to account for new analyzer path format introduced with dotnet/sdk#20355 - Don't target latest runtime in razor tests - Update Dependencies.props - Update Helix.targets - Fixup - Move FrameworkReference update to GenerateFiles - Have Helix tests import directory.build files - Keep RunTests at net6 - Quarantine 2 tests - revert spa-templates submodule downgrade Commit migrated from dotnet/aspnetcore@70c05f178a33
Background
As Roslyn implements more features and APIs, the need to target these features/APIs increases in Roslyn components (i.e. Code Analyzers/Fixers and Source Generators). As the components need to target newer versions of Roslyn, it is becoming clear that some components need to target multiple versions of Roslyn at the same time.
Take, for example, the following issues in dotnet/runtime with the System.Text.Json source generator:
In both of these issues, the only fix is for the System.Text.Json source generator to take a dependency on version
4.0
of theMicrosoft.CodeAnalysis
assemblies. The first needsIIncrementalGenerator
and the second needsFileScopedNamespaceDeclarationSyntax
, both of which were only introduced in4.0
.However, once a Roslyn component references a version of
Microsoft.CodeAnalysis
, that Roslyn component can no longer be loaded in an earlier version of the compiler.This poses two problems:
4.0
, the developer gets a warning in their build:Warning CS8032 An instance of analyzer System.Text.Json.SourceGeneration.JsonSourceGenerator cannot be created from C:\Users\eerhardt\.nuget\packages\system.text.json\7.0.0-dev\analyzers\dotnet\cs\System.Text.Json.SourceGeneration.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
This is not a great experience for our customers.
Proposal
We will update the current logic that resolves the Analyzers from NuGet packages to support an enhanced
analyzers
folder structure. The enhanced folder structure will allow for the Roslyn version targeted by the component to be expressed. This will allow for multiple versions to be targeted in a single package, as well as being able to express a "minimal" version. This will address the two problems above. When the Analyzers are resolved from the NuGet package, the correct asset(s) will be resolved according to which version of the compiler will be used to compile the assembly.Current folder structure
The following structure is recognized today by the SDK:
Proposed folder structure
This proposal adds a new, optional folder to the structure:
roslyn{version}
:Where
{version}
is the{major}.{minor}
version of theMicrosoft.CodeAnalysis
assemblies used during theCompile
task.When a NuGet package contains folders under
analyzers
with the pattern,roslyn<major-version>.<minor-version>
, the folder with the highest version that is less than or equal to the currentMicrosoft.CodeAnalysis
<major>.<minor>
version will be used. This works the same as TFMs work under thelib
folder. Except instead of TargetFramework versions being evaluated, the Roslyn version will be evaluated.Also note, since the same Analyzer logic exists in NuGet's ResolveNuGetPackageAssets Task. Thus, we will need to update the NuGet MSBuild logic with this support. That way developers using non-SDK .NET projects will get the same Analyzers selected as SDK projects.
Earlier SDK versions
Since this proposal is to add support to .NET SDK 6.0, we need to consider what happens when NuGet packages that follow this structure are used in SDK versions before 6.0.
If implemented as indicated above, NuGet packages that support multiple Roslyn versions will have all of their assets loaded in earlier versions of the SDK. To prevent that, we can add a new MSBuild property to the SDK that indicates it supports multi-targeting Roslyn versions.
NuGet packages can include MSBuild logic that "lights up" when
$(SupportsRoslynComponentVersioning)
is not present. This indicates that the built-in support isn't there to select the right version. The NuGet package needs to do the selection itself. Typically, NuGet packages will select the component that targets the lowest version of Roslyn version it supports, since newer Roslyn versions can't be guaranteed. Another option would be to opt out of adding a Roslyn component all together.Additional rules
roslyn{version}
folder and outside, ex. directly underanalyzers\dotnet\cs\analyzer.dll
, both assets will be selected. The reasoning is that the assets outside ofroslyn{version}
folders are considered to work everywhere. Existing analyzer resolution rules add both assets inside and outside{language}
folders, if present. This follows the same reasoning.roslyn{version}
folders apply, it can add that asset toroslyn1.0
.Alternative Solutions
An alternative is to follow the proposal in dotnet/roslyn#54108:
Following this approach would mean that each NuGet package that wants to support multiple versions of Roslyn (or a minimum version) would need to ship special MSBuild
.targets
files with duplicated logic in them.This approach is not ideal because:
@chsienki @jaredpar @dsplaisted @jasonmalinowski @jmarolf
The text was updated successfully, but these errors were encountered: