CZEMacLeod / MSBuild.SDK.SystemWeb

This MSBuild SDK is designed to allow for the easy creation and use of SDK (shortform) projects targeting ASP.NET 4.x using System.Web.
MIT License
162 stars 10 forks source link

"Remove Unused References..." wants to remove everything #77

Closed gfody closed 3 months ago

gfody commented 3 months ago

when I try to remove unused references on my freshly migrated monster w/100's of package refs, it detects all of them as being unused. while migrating I just copied my package refs into the csproj, here's an excerpt below, I wonder if I'm doing it wrong?

<Project Sdk="MSBuild.SDK.SystemWeb/4.0.88">
  <PropertyGroup>
    <ProjectGuid>{2B9A4A6B-5BD4-409C-BE48-3246C7562D2C}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AssemblyName>ASDF</AssemblyName>
    <TargetFrameworks>net481</TargetFrameworks>
    <LangVersion>latest</LangVersion>
    <OutputPath>bin\</OutputPath>
    <Configurations>Debug;Release;Staging</Configurations>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <Deterministic>false</Deterministic>
  </PropertyGroup>
  <ItemGroup>
    ...
    <PackageReference Include="Microsoft.Net.Compilers" Version="3.7.0" />
    <PackageReference Include="Microsoft.Owin" Version="4.2.2" />
    <PackageReference Include="Microsoft.Owin.Host.SystemWeb" Version="4.2.2" />
    <PackageReference Include="Microsoft.Owin.Security.Cookies" Version="4.2.2" />
    <PackageReference Include="Microsoft.Owin.Security.Google" Version="4.2.2" />
    <PackageReference Include="Microsoft.Owin.Security.OAuth" Version="4.2.2" />
    <PackageReference Include="Microsoft.Owin.Security.OpenIdConnect" Version="4.2.2" />
    <PackageReference Include="Microsoft.IdentityModel.Protocols" Version="6.35.0" />
    <PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="6.35.0" />
    <PackageReference Include="Microsoft.Web.Infrastructure" Version="1.0.0" />
    <PackageReference Include="MiniProfiler.EntityFrameworkCore" Version="4.2.22" />
    <PackageReference Include="MiniProfiler.Mvc5" Version="4.2.22" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
    <PackageReference Include="Ninject" Version="3.3.6" />
    <PackageReference Include="Ninject.MVC5" Version="3.3.0" />
    ...

thank you for this project btw!

CZEMacLeod commented 3 months ago

PackageReference only needs the 'top level' packages, or packages where you want a newer version than the min dependency. If you expand the Dependencies section under Packages you will find the tree of transitive packages all pulled in.

A couple of nits with your example: Microsoft.Net.Compilers is legacy and the replacement Microsoft.Net.Compilers.Toolset is automatically added by the SDK. If you want a newer version than it pulls in by default then set the property with something like

<MicrosoftNetCompilersToolset_Version>4.10.0</MicrosoftNetCompilersToolset_Version>

Use TargetFramework instead of TargetFrameworks - you might get some odd stuff happening due to multitargetting otherwise.

Don't set the OutputType or OutputPath properties - the SDK deals with that.

gfody commented 3 months ago

ty I made the suggested edits and "remove unused references" is still listing everything.. maybe this is just an untested scenario, feel free to close this if it's totally unrelated to the sdk. the feature does correctly detect unused references for other noncore console projects.

CZEMacLeod commented 3 months ago

@gfody I would expect it to remove quite a few of those - e.g. Microsoft.Owin as it is transitive from most of the others. I certainly don't do it that way when migrating one of my projects. I create a new empty systemweb project, and then copy in the files from the original, and just add the leaf packages as required until it runs. Is there perhaps a project reference in your project to another class library project which has all the same packages? This would mean that all the packages were included transitively and so you wouldn't need any of them,

One thing I found was that 'migrating' didn't change the project type guids in the solution file, and this causes some issues of it's own. Unfortunately the built in tooling is not really designed to cover this scenario, and, especially as you should only have 1 head project, it is probably better to do it manually anyway.

I will close this for now, but if you find the actual reason for it doing this, please comment as it will help others.