T4MVC / R4MVC

R4MVC is a Roslyn code generator for ASP.NET Core MVC apps that creates strongly typed helpers that eliminate the use of literal strings in many places
Apache License 2.0
157 stars 48 forks source link

Cannot execute generate command if referencing a package targeting another framework #115

Closed Guymestef closed 5 years ago

Guymestef commented 5 years ago

Hello,

I've migrated a project from .net 4.6.1 to .net core 2.2 and it remains an reference to an old package targeting only .net 4.6.1. I can't execute the generate command because it detects the wrong targeted package as a failure instead of just a warning. So the following code stop the generation:

                if (workspace.Diagnostics.Count > 0)
                {
                    var foundErrors = false;
                    foreach (var diag in workspace.Diagnostics)
                    {
                        Console.Error.WriteLine($"  {diag.Kind}: {diag.Message}");
                        if (diag.Kind == Microsoft.CodeAnalysis.WorkspaceDiagnosticKind.Failure)
                            foundErrors = true;
                    }
                    if (foundErrors)
                        return;
                }

I've workaround the problem by targeting .net 4.6.1, generating, re-targeting .net core 2.2 Can we remove the "if foundErrors return" part or add a force parameter or ...?

artiomchi commented 5 years ago

Hmm, yeah, adding a force parameter could be done, but it'll have it's own issues. Specifically - if the compilation fails, some classes end up not being available to the build engine.

For example, if a controller class fails to build and I ignore the error, R4Mvc won't even see the controller, and will end up removing the generated partials classes from the project :)

I can still see the benefit of running it like this sometimes.

In the meantime, could you explain your issue again? Which of the projects uses which framework, and who depends on what? Sorry, I got a bit confused :)

Guymestef commented 5 years ago

All the projects in my solution are targeting .netstandard2.0 except the web project that is targeting .netcoreapp2.2 The web project as a package reference to the nuget package mangopay2-sdk (no .net core version available, only .net 4.6.1)

When I build with "dotnet build":

When I execute a R4MVC generation:

R4Mvc Generator Tool v1.0.0-alpha2-00333

Using: Visual Studio Professional 2017 - 15.9.28307.423 Project: my_web_project.csproj

Creating Workspace ... Loading project ... Failure: Msbuild failed when processing the file 'my_web_project.csproj: (0, 0): Package 'mangopay2-sdk 1.1.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.2'. This package may not be fully compatible with your project. Failure: Msbuild failed when processing the file 'my_web_project.csproj' with message: my_web_project.csproj: (0, 0): Package 'RestSharp 104.5.0' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETCoreApp,Version=v2.2'. This package may not be fully compatible with your project.

(RestSharp is a dependency of mangopay2-sdk)

artiomchi commented 5 years ago

Hey @Guymestef, @valeriob

Sorry it took so long.. I got swamped with some IRL stuff, and to be frank - forgot to finish this up.

Since this issue was opened, RestSharp released a .NET Standard version, but the underlying problem still stands. I believe it's an issue with the msbuild workspace. Even though this is thrown as a warning during the project build, the workspace handles this as an error.

The solution that I found is to just tell the compiler to ignore this warning for this project by adding the following to the project file (note that 1701 and 1702 are the default values, this patch is adding NU1701 to this list):

<PropertyGroup>
  <NoWarn>1701;1702;NU1701</NoWarn>
</PropertyGroup>
artiomchi commented 5 years ago

Since this seems to be a working solution for you, I'm closing this issue