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

Support legacy-style projects #101

Closed
Sebbs128 opened this issue Nov 8, 2023 · 5 comments
Closed

Support legacy-style projects #101

Sebbs128 opened this issue Nov 8, 2023 · 5 comments

Comments

@Sebbs128
Copy link

Sebbs128 commented Nov 8, 2023

Is the feature request related to a problem

When the package is installed in an legacy-style project (that is, the project file format used prior to the current SDK-style; such as ASP.NET MVC5 projects), building that project fails because the ResolvePackageAssets target does not exist for MSBuild.

[ETA]ASP.NET projects unfortunately can't be migrated to SDK-style at this time.

ResolvePackageAssets is a Target used within Polyfill.targets, but only exists in the dotnet SDK.

Describe the solution

I can currently work around this by adding a placeholder for ResolvePackageAssets if the target isn't already defined, and setting up some of the preprocessor constants that SDK-style projects set but are lacking in legacy style projects. In my work-around they're placed in Directory.Build.props and Directory.Build.targets, however for a solution in the Polyfill package itself these should be able to go in Polyfill.props and Polyfill.targets.

In legacy style projects, add NETFRAMEWORK; to <DefineConstants> (or add <DefineConstants>NETFRAMEWORK</DefineConstants> if the element didn't already exist).
 
Add to Directory.Build.props (work-around; Polyfill.props for potential PR)

    <PropertyGroup Condition="'$(TargetFramework)' == '' and '$(TargetFrameworkVersion)' == 'v4.8'">
        <TargetFramework>net48</TargetFramework>
    </PropertyGroup>
    <!-- repeat above for net47, and net46 -->

Add to Directory.Build.targets (work-around; Polyfill.targets for potential PR)

	<PropertyGroup>
		<ResolvePackageAssetsTargets Condition="Exists($(ResolvePackageAssets))">true</ResolvePackageAssetsTargets>
	</PropertyGroup>

	<Target Name="ResolvePackageAssets"
			Condition="'$(ResolvePackageAssetsTargets)' != 'true'"
			AfterTargets="ResolveReferences">
		<Message Importance="normal" Text="ResolvePackageAssets target doesn't exist. Adding a placeholder." />
	</Target>

Describe alternatives considered

The buildMultitargeting asset type was introduced in NuGet 4.0 to allow framework-specific build logic, however I don't think it's capable of distinguishing SDK-style from old-style projects.

I also looked at getting ResolvePackageAssets referenceable for building a legacy-style project, but getting a reference to the .targets file is a bit of a maze (and I wasn't yet able to find an MSBuild property that might allow referring to a dotnet SDK directory).

Additional context

[ETA] Context for not being able to switch ASP.NET projects to SDK-style (yet?)
dotnet/project-system#2670

@SimonCropp
Copy link
Owner

what’s stopping you from moving to the new project format?

@Sebbs128
Copy link
Author

Sebbs128 commented Nov 8, 2023

That was one of the first things I tried. It's unsupported for ASPNET projects, and currently won't build (or little else). There is CZEMacLeod's alternative SDK for the project to target, but there's still several caveats to it.

@Sebbs128
Copy link
Author

Sebbs128 commented Nov 13, 2023

I've determined a better workaround after the previous one seemed to stop working, and even better it no longer needs replicating the contents of Polyfill.props and Polyfill.targets. (I'll update the original issue with these).

It does require that NETFRAMEWORK is added to <DefineConstants> in the legacy style projects, otherwise compiler errors in /contentFiles/.../PolyfillExtensions_Type.cs appear during a build (and for whatever reason, trying to add it in the <PropertyGroup> in Directory.Build.props wouldn't work).

In Directory.Build.props, I now just have

	<PropertyGroup Condition="'$(TargetFramework)' == '' and '$(TargetFrameworkVersion)' == 'v4.8'">
		<TargetFramework>net48</TargetFramework>
	</PropertyGroup>

	<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net48'">
		<PackageReference Include="Polyfill" Version="1.30.0">
			<PrivateAssets>all</PrivateAssets>
			<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
		</PackageReference>
	</ItemGroup>

And in Directory.Build.targets

	<PropertyGroup>
		<ResolvePackageAssetsTargets Condition="Exists($(ResolvePackageAssets))">true</ResolvePackageAssetsTargets>
	</PropertyGroup>

	<Target Name="ResolvePackageAssets"
			Condition="'$(ResolvePackageAssetsTargets)' != 'true'"
			AfterTargets="ResolveReferences">
		<Message Importance="normal" Text="ResolvePackageAssets target doesn't exist. Adding a placeholder." />
	</Target>

I'll see if I can get permission to contribute a PR during my work hours, otherwise I'll try and submit one after hours.

@Sebbs128 Sebbs128 changed the title Support old-style projects Support legacy-style projects Nov 13, 2023
@maximpashuk
Copy link

maximpashuk commented Jan 14, 2024

My 50 cents.

I also has a non-sdk style projects in my solution.
This is regular thing in legacy projects so my personal opinion that Polyfill should support them.

And it actually does support non-sdk style projects even now but with some workaround.
This is part of Directory.Build.props file I use as workaround.

@Sebbs128 @SimonCropp I think proper fix should use msbuild property UsingMicrosoftNETSdk to detect non-sdk style projects and not try to introduce fake TargetFramework property, it is not relevant in non-sdk style projects.

  <Target Name="ResolvePackageAssets" Condition=" '$(UsingMicrosoftNETSdk)' == '' " AfterTargets="ResolveReferences">
    <PropertyGroup>
      <DefineConstants>$(DefineConstants);NETFRAMEWORK</DefineConstants>
    </PropertyGroup>
  </Target>

@SimonCropp
Copy link
Owner

sorry. i dont think i have capacity to maintain support for legacy projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants