Build/Test Issues

Load failure for .NET Core cs projects without Sdk attribute in Project element

Started by ma111 on 2,007 views

NCrunch fails to load C# .NET Core project files that don't have the Sdk attribute in the Project element. However this attribute is not required by MSBuild and there are cases where it should not be present: Microsoft Documentation

This can be reproduced with a test project with the following csproj:

<Project>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
    <PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
  </ItemGroup>

  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>


The following, equivalent csproj results in a successful load

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
    <PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
  </ItemGroup>
</Project>


As does this, simply leaving the Sdk attribute empty:

<Project Sdk="">
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
    <PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
    <PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
  </ItemGroup>

  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>


Below is the full error:

System.Exception: Unable to find the project declaration in this file.  This does not appear to be a valid MSBuild project file
  at nCrunch.Compiler.BuildXml.AddDefaultBuildTarget(String newTargetName)
  at  .(BuildXml , String )
  at nCrunch.Compiler.LoadTimeQuery..ctor(BuildXml buildXml, IList`1 importExpressions, IPlatformTypeFactory[] platformTypeFactories, IList`1 loadTimeQueryDeclarations)
  at nCrunch.Compiler.ComponentAnalysisContext..ctor(ComponentLoadParameters loadParameters, IBuildableProject buildableProject, DirectoryPath baseIntermediateOutputPath)
  at nCrunch.Compiler.RemoteBuildRunner.AnalyseComponentBuild(ComponentLoadParameters parameters)
  --- End of inner exception stack trace ---
  at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
  at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
  at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  at nCrunch.TaskRunner.Ipc.IpcMessageProcessor.(CallMethodMessage )
  at nCrunch.TaskRunner.Ipc.IpcMessageProcessor.ProcessMessageReturningResult(Byte[] data)
  at nCrunch.TaskRunner.Ipc.RemoteInstance.(Byte[] )
  at nCrunch.TaskRunner.Ipc.RemoteInstance.Invoke(IMessage msg)
  at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
  at nCrunch.Compiler.IRemoteBuildRunner.AnalyseComponentBuild(ComponentLoadParameters parameters)
  at nCrunch.Core.BuildManagement.BuildProcessLauncher..(IRemoteBuildRunner )
  at nCrunch.Core.BuildManagement.BuildProcessLauncher.(Action`1 , FilePath , String , ExternalProcess )
  at nCrunch.Core.BuildManagement.BuildProcessLauncher.(Action`1 , EffectiveProcessorArchitecture , GridClientId , BuildSystemParameters , IList`1 , Nullable`1 , GridAddress )
  at nCrunch.Core.BuildManagement.BuildProcessLauncher.AnalyseComponentBuildInExternalProcess(ComponentLoadParameters parameters, IList`1 customEnvironmentVariables)
  at nCrunch.Client.ComponentLoader.SnapshotComponentLoader.(EffectiveProcessorArchitecture , String )
  at nCrunch.Client.ComponentLoader.SnapshotComponentLoader.CreateComponentFromXml(FilePath projectFilePath, ParsedBuildXml projectXml, FilePath solutionFilePath, String[] additionalFilesToIncludeAtSolutionLevel, Boolean isLoadedFromFile, VisualStudioVersion vsVersion, ComponentUniqueName componentName, TaskSettings componentTaskSettings, Exception parseException, String targetFramework)


Is this known or expected? Is there a way around this other than adding Sdk="" as an attribute to the Project element?

Thanks
Hi, thanks for sharing this problem in such great detail.

The code has been assuming that a project element cannot stand on its own (it was actually written long before the Sdk attribute was even a thing). I've had a go at implementing a fix.

Would you like to try the build below to see if this resolves the problem for you?

NCrunch_Console_3.29.0.3.msi
NCrunch_Console_3.29.0.3.zip
NCrunch_GridNodeServer_3.29.0.3.msi
NCrunch_GridNodeServer_3.29.0.3.zip
NCrunch_LicenseServer_3.29.0.3.zip
NCrunch_VS2008_3.29.0.3.msi
NCrunch_VS2010_3.29.0.3.msi
NCrunch_VS2010_3.29.0.3.zip
NCrunch_VS2012_3.29.0.3.msi
NCrunch_VS2012_3.29.0.3.zip
NCrunch_VS2013_3.29.0.3.msi
NCrunch_VS2013_3.29.0.3.zip
NCrunch_VS2015_3.29.0.3.msi
NCrunch_VS2015_3.29.0.3.msi.7z
NCrunch_VS2015_3.29.0.3.zip
NCrunch_VS2017_3.29.0.3.msi
NCrunch_VS2017_3.29.0.3.msi.7z
NCrunch_VS2017_3.29.0.3.zip
NCrunch_VS2019_3.29.0.3.msi
NCrunch_VS2019_3.29.0.3.msi.7z
NCrunch_VS2019_3.29.0.3.zip

Edited

Confirmed that this resolves the problem. Thanks for the quick turnaround!

Post a reply

Log in to reply.