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
151 stars 8 forks source link

Content items brought in by EnableWebFormsDefaultItems include (published) output under bin and obj #25

Closed CZEMacLeod closed 2 years ago

CZEMacLeod commented 2 years ago

@CZEMacLeod Thanks for the great work on this project. I updated to this version and noticed an issue after performing a publish.

In my project I end up with "bin\Release\Publish" as the output folder, however if turn on EnableWebFormsDefaultItems, once I publish it see's that published folder as content. Then each publish adds more content upon itself and eventually errors.

I have worked around this in some of my other globs by adding Exclude="$(DefaultItemExcludes)".

I recommend changing from:

<Content Include="**\*.asax" />
<Content Include="**\*.ascx" />
<Content Include="**\*.ashx" />
<Content Include="**\*.asmx" />
<Content Include="**\*.aspx" />
<Content Include="**\*.master" />

to

<Content Include="**\*.asax" Exclude="$(DefaultItemExcludes)" />
<Content Include="**\*.ascx" Exclude="$(DefaultItemExcludes)" />
<Content Include="**\*.ashx" Exclude="$(DefaultItemExcludes)" />
<Content Include="**\*.asmx" Exclude="$(DefaultItemExcludes)" />
<Content Include="**\*.aspx" Exclude="$(DefaultItemExcludes)" />
<Content Include="**\*.master" Exclude="$(DefaultItemExcludes)" />

Originally posted by @mcnallys in https://github.com/CZEMacLeod/MSBuild.SDK.SystemWeb/issues/24#issuecomment-971997975

CZEMacLeod commented 2 years ago

@mcnallys The props file actually adds all the item types to DefaultItemExcludes so using that to exclude won't work. However, we can setup excludes for the Content entries

  <!-- Exclude WebForms items from default items -->
  <PropertyGroup Condition="'$(EnableWebFormsDefaultItems)'=='true'">
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.asax;*.asax</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.ascx;*.ascx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.ashx;*.ashx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.asmx;*.asmx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.aspx;*.aspx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.master;*.master</DefaultItemExcludes>
    <DefaultWebFormsItemExcludes>$(BaseOutputPath)\**;$(BaseIntermediateOutputPath)\**</DefaultWebFormsItemExcludes>
  </PropertyGroup>

  <!-- Include WebForms items as content -->
  <ItemGroup Condition="'$(EnableWebFormsDefaultItems)'=='true'">
    <Content Include="**\*.asax" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.ascx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.ashx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.asmx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.aspx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.master" Exclude="$(DefaultWebFormsItemExcludes)" />
  </ItemGroup>

If you try this (but change EnableWebFormsDefaultItems to EnableWebFormsDefaultItems2) and it resolves the issue for you, I can make the update and publish.

mcnallys commented 2 years ago

I'm trying to follow but not quite understanding, am I changing these files locally in my project or yours?

CZEMacLeod commented 2 years ago

@mcnallys Add the following to your project file and change <EnableWebFormsDefaultItems>true</EnableWebFormsDefaultItems> to <EnableWebFormsDefaultItems2>true</EnableWebFormsDefaultItems2>

  <!-- Exclude WebForms items from default items -->
  <PropertyGroup Condition="'$(EnableWebFormsDefaultItems2)'=='true'">
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.asax;*.asax</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.ascx;*.ascx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.ashx;*.ashx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.asmx;*.asmx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.aspx;*.aspx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.master;*.master</DefaultItemExcludes>
    <DefaultWebFormsItemExcludes>$(BaseOutputPath)\**;$(BaseIntermediateOutputPath)\**</DefaultWebFormsItemExcludes>
  </PropertyGroup>

  <!-- Include WebForms items as content -->
  <ItemGroup Condition="'$(EnableWebFormsDefaultItems2)'=='true'">
    <Content Include="**\*.asax" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.ascx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.ashx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.asmx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.aspx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.master" Exclude="$(DefaultWebFormsItemExcludes)" />
  </ItemGroup>

This will allow you to test the update - if it works correctly for you, then I will make the changes to MSBuild.SDK.SystemWeb.DefaultItems.props. Once updated you can revert to <EnableWebFormsDefaultItems>true</EnableWebFormsDefaultItems> and remove the temporaryPropertyGroup and ItemGroup.

mcnallys commented 2 years ago

This almost worked but my baseoutputpath and baseintermediateoutputpath already had a trailing slash.

<!-- Exclude WebForms items from default items -->
  <PropertyGroup Condition="'$(EnableWebFormsDefaultItems2)'=='true'">
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.asax;*.asax</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.ascx;*.ascx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.ashx;*.ashx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.asmx;*.asmx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.aspx;*.aspx</DefaultItemExcludes>
    <DefaultItemExcludes>$(DefaultItemExcludes);**\*.master;*.master</DefaultItemExcludes>
    <DefaultWebFormsItemExcludes>$(BaseOutputPath)**;$(BaseIntermediateOutputPath)**</DefaultWebFormsItemExcludes>
  </PropertyGroup>

    <!-- Include WebForms items as content -->
  <ItemGroup Condition="'$(EnableWebFormsDefaultItems2)'=='true'">
    <Content Include="**\*.asax" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.ascx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.ashx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.asmx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.aspx" Exclude="$(DefaultWebFormsItemExcludes)" />
    <Content Include="**\*.master" Exclude="$(DefaultWebFormsItemExcludes)" />
  </ItemGroup>

Notice $(BaseOutputPath) instead of $(BaseOutputPath)\ and $(BaseIntermediateOutputPath) instead of $(BaseIntermediateOutputPath)\

That fixed it locally and I will report back on the results of our build system.

mcnallys commented 2 years ago

Everything looked good in the build.

CZEMacLeod commented 2 years ago

@mcnallys This logic should be implemented with V4.0.58 - Let me know if you have any issues.

mcnallys commented 2 years ago

Well I was so hopeful, it's still including the obj folder.

image

If I add this line to my project it works as expected.

<DefaultWebFormsItemExcludes>$(DefaultWebFormsItemExcludes);$([MSBuild]::EnsureTrailingSlash($(BaseIntermediateOutputPath)))**</DefaultWebFormsItemExcludes>

I wonder if BaseIntermediateOutputPath is not populated at the time it's being evaluated.

CZEMacLeod commented 2 years ago

@mcnallys I think I have found the issue - the PropertyGroup defining the DefaultWebFormsItemExcludes was predicated on EnableWebFormsDefaultItems but was in the .props file meaning it was imported and evaluated before the project file. This meant that it wasn't being triggered, so when the ItemGroups were evaluated, nothing happened. When you inlined it in the project it would have been after your define of <EnableWebFormsDefaultItems>true</EnableWebFormsDefaultItems> so it worked. I have moved the property group to a targets file so it gets checked after the project file is imported and the flag detected. The ItemGroup is evaluated in a later phase so it works as expected in the props file and allows you to override it in the project file as expected. This means that V4.0.63 should now work as expected.