dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.67k stars 1.06k forks source link

AssignTargetPathsDependsOn overridden by default value #1558

Closed AbubakerB closed 7 years ago

AbubakerB commented 7 years ago

I have a targets file that i was using on a net452 class library. After converting the classlib to netstandard20, the Target in my targets file wasn't being applied.

This is my targets file:

<Project>

  <ItemGroup>
    <AvailableItemName Include="CustomContent" />
  </ItemGroup>

  <ItemGroup>
    <None Remove="src\*.json" />
  </ItemGroup>
  <ItemGroup>
    <CustomContent Include="src\*.json" >
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </CustomContent>
  </ItemGroup>

  <PropertyGroup>
    <AssignTargetPathsDependsOn>
      $(AssignTargetPathsDependsOn);
      IncludeCustomContent;
    </AssignTargetPathsDependsOn>
  </PropertyGroup>

  <Target Name="IncludeCustomContent">
    <CreateItem Include="@(CustomContent)" AdditionalMetadata="TargetPath=$(AssemblyName)\%(relativedir)%(Filename)%(Extension)">
      <Output ItemName="ContentWithTargetPath" TaskParameter="Include" />
    </CreateItem>
  </Target>
</Project>

This targets file is imported into my netstandard20 classlib.

I ran dotnet build -v d > d_log.txt and it's output suggested: Property reassignment: $(AssignTargetPathsDependsOn)="" (previous value: "IncludeCustomContent").

Seems like it's being overridden by the default value in Microsoft.Common.CurrentVersion.targets

  1. Has something changed in this sdk?
  2. I noticed that the AvailableItemName value doesn't show up on the "Build Action" dropdownlist in the file property dialog (for a netstandard project) in VS (15.3.2). Is AvailableItemName still supported?
  3. The .json files disappear from the project's solution explorer when using <CustomContent Include="src\*.json" >. When i use <Content Include="src\*.json" > they show up again. Is this intentional?

Note, i also tried this with Content instead of CustomContent and, although it showed up in the solution explorer, it still didn't work (my IncludeCustomContent Target was not ran)

dasMulli commented 7 years ago

I suspect in your previous, "classic" csproj file, this was below the import of Microsoft.Common.targets? This allowed re-assigning the property after it has been set by the common targets.

There are two workarounds:

  1. Modify the csproj like this to explicitly state where in the project file the SDK's targets (and further import of common targets) takes effect:
<Project>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

  <!-- All your project's other content here -->

  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
  <PropertyGroup>
    <AssignTargetPathsDependsOn>
      $(AssignTargetPathsDependsOn);
      IncludeCustomContent;
    </AssignTargetPathsDependsOn>
  </PropertyGroup>
</Project>
  1. Create a Directory.Build.targets file containing the reassignment:

    <Project>
    <PropertyGroup>
    <AssignTargetPathsDependsOn>
      $(AssignTargetPathsDependsOn);
      IncludeCustomContent;
    </AssignTargetPathsDependsOn>
    </PropertyGroup>
    
    <!-- you could also put the target here to keep the main project file cleaner -->
    </Project>

This file is automatically imported by the common targets late enough to reassign the property.

AbubakerB commented 7 years ago

this was below the import of Microsoft.Common.targets?

yep, my bad 😄

Unfortunately, I dont have access to the classlib (its managed by someone else). I could ask them to manually add these things in, but it doesn't seem like the best approach.

Also, does this explain why AvailableItemName isn't working anymore?