项目构建错误 - 与NETStandard.Library2.0.1有关(我想!)

时间:2018-03-12 13:06:39

标签: visual-studio msbuild .net-standard csproj

我在尝试构建项目时遇到了这个奇怪的错误:

Error: The reference to the built-in metadata "FileName" at position 1 is not allowed in this condition "'%(FileName)' != 'netstandard'".  C:\github\VS\API\VS.API.Public\VS.API.Public.csproj
 at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject(String errorSubCategoryResourceName, IElementLocation elementLocation, String resourceName, Object[] args)
 at Microsoft.Build.Evaluation.Parser.Parse(String expression, ParserOptions optionSettings, ElementLocation elementLocation)
 at Microsoft.Build.Evaluation.ConditionEvaluator.EvaluateConditionCollectingConditionedProperties[P,I](String condition, ParserOptions options, Expander`2 expander, ExpanderOptions expanderOptions, Dictionary`2 conditionedPropertiesTable, String evaluationDirectory, ElementLocation elementLocation, ILoggingService loggingServices, BuildEventContext buildEventContext, ProjectRootElementCache projectRootElementCache)
 at Microsoft.Build.Execution.ProjectInstance.EvaluateCondition(String condition)
 at JetBrains.Platform.MsBuild.TaskCommon.ProjectModel.Bindings.AddWildcardItemsFromXml(ProjectRootElement element, Boolean isImported, ProjectInstance projectInstance, List`1 items)
 at JetBrains.Platform.MsBuild.TaskCommon.ProjectModel.Bindings.BindWildcardItems(Project project, ProjectInstance projectInstance)
 at JetBrains.Platform.MsBuild.TaskCommon.ProjectModel.Bindings.Bind(Project project, ProjectInstance projectInstance, List`1 targetErrors, List`1 targetWarnings, Dictionary`2 resultsByTarget)
 at JetBrains.Platform.MsBuild.TaskCommon.ProjectModel.Bindings.Bind(Project project, ProjectInstance projectInstance)
 at JetBrains.Platform.MsBuild.TaskCommon.Build.BuildSessionHost.HandleSubmissionFinished(RdBuildRequest request, ProjectWithEvents projectWithEvents, Boolean succeeded, RdTask`1 rdTask)

我不知道这个错误意味着什么,甚至从哪里开始。它是一个Web API项目,当我在项目上进行重建时,它会出现在Visual Studio的Build Results窗口中。

错误提到了这种情况的问题:。{csproj文件中的'%(FileName)' != 'netstandard'。但是在.csproj文件中进行搜索时,这并不会出现。

如果我在解决方案文件夹中的所有文件中搜索,我可以在C:\github\vs\packages\NETStandard.Library.2.0.1\build中找到它的出现:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <NETStandardLibraryPackageVersion>2.0.1</NETStandardLibraryPackageVersion>
  </PropertyGroup>

  <ItemGroup Condition="'$(_NetStandardLibraryRefPath)' != ''">
    <Reference Include="$(_NetStandardLibraryRefPath)*.dll">
      <!-- Private = false to make these reference only -->
      <Private>false</Private>
      <!-- hide these from Assemblies view in Solution Explorer, they will be shown under packages -->
      <Visible>false</Visible>
      <Facade Condition="'%(FileName)' != 'netstandard'">true</Facade>
      <NuGetPackageId>NETStandard.Library</NuGetPackageId>
      <NuGetPackageVersion>$(NETStandardLibraryPackageVersion)</NuGetPackageVersion>
    </Reference>
    <ReferenceCopyLocalPaths Condition="'$(_NetStandardLibraryLibPath)' != ''" Include="$(_NetStandardLibraryLibPath)*.dll">
      <Private>false</Private>
      <Facade Condition="'%(FileName)' != 'netstandard'">true</Facade>
      <NuGetPackageId>NETStandard.Library</NuGetPackageId>
      <NuGetPackageVersion>$(NETStandardLibraryPackageVersion)</NuGetPackageVersion>
    </ReferenceCopyLocalPaths>
  </ItemGroup>
</Project>

但是我不太了解.targets文件是如何工作的,知道如何解决这个问题...通常它只是有效。

如果我尝试谷歌此错误,我也无法找到任何相关结果。有谁知道这里出了什么问题?

我使用的是Visual Studio Professional 15.6.0。该项目的目标是.net462。

1 个答案:

答案 0 :(得分:1)

  

但我不太了解.targets文件如何解决这个问题

根据NuGet文件Creating native packages

  

包中还可能包含\ build中的目标和道具文件   NuGet将自动导入到消费者的项目中   封装

这意味着NuGet会将相应的.props.targets文件导入到项目中。因此,当您将软件包NETStandard.Library.2.0.1安装到项目中时,NETStandard.Library.targets文件(在该软件包中的\buil d文件夹中)将导入到项目文件中,卸载您的项目,编辑它,然后你会发现以下代码:

 <Import Project="..\packages\NETStandard.Library.2.0.1\build\NETStandard.Library.targets" Condition="Exists('..\packages\NETStandard.Library.2.0.1\build\NETStandard.Library.targets')" />

现在,.targets文件将作为项目文件运行。

正如您所想,此问题的原因应与包NETStandard.Library.2.0.1

相关

根据MSBuild文档MSBuild Batching:

  

属性函数可能不会出现在元数据值中。例如,

     

%(Compile.FullPath.Substring(0,3))

     

是不允许的。

所以你会得到那个错误

  

此处不允许位置1处的内置元数据“FileName”   条件“'%(FileName)'!='netstandard'”

要解决此问题,您可以在自定义目标中指定ItemGroup:

<Target Name="YourCustomName">    
  <ItemGroup Condition="'$(_NetStandardLibraryRefPath)' != ''">
    <Reference Include="$(_NetStandardLibraryRefPath)*.dll">
...
    </Reference>
    <ReferenceCopyLocalPaths Condition="'$(_NetStandardLibraryLibPath)' != ''" Include="$(_NetStandardLibraryLibPath)*.dll">
...
  </ItemGroup>       
<Message Text="@(Reference)"/>
</Target>

来源:How do you filter an ItemGroup?

或者,您可以创建一个存储此自定义元数据值的属性:

<PropertyGroup>
   <FimeName>%(FileName)</FimeName>
</Pro‌​pertyGroup>

<ItemGroup>
   ...
      <Facade Condition="'$(FileName)' != 'netstandard'">true</Facade>
    ...
</ItemGroup>

来源:Using metadata in conditions

此外,如果您可以确保不使用.targets文件,则可以尝试删除项目文件中的<Import Project="..\packages\NETStandard.Library.2.0.1\build\NETStandard.Library.targets" ... />