msbuild: .NET Core App fails to load project (.NET 4.6.1 works as expected)

I created a sample .NET Console app with the following code.

using System;
using Microsoft.Build.Evaluation;

namespace ConsoleApp25
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var buildEngine = new ProjectCollection();
            var project = buildEngine.LoadProject("<full path>\\UnitTestProject.csproj");
       }
    }
}

My project references the following 2 Nuget packages:

  • Microsoft.Build
  • Microsoft.Build.Utilities.Core

This gives the following exception when compiled to target .NET Core 2.0;

Microsoft.Build.Exceptions.InvalidProjectFileException
  HResult=0x80131500
  Message=The SDK 'Microsoft.NET.Sdk' specified could not be found.  d:\dd\GitHubTestAdapter\UnitTestProject\UnitTestProject.csproj
  Source=Microsoft.Build
  StackTrace:
   at Microsoft.Build.Shared.ProjectErrorUtilities.ThrowInvalidProject(String errorSubCategoryResourceName, IElementLocation elementLocation, String resourceName, Object[] args) in /_/src/Shared/ProjectErrorUtilities.cs:line 412
   at Microsoft.Build.Evaluation.Evaluator`4.ExpandAndLoadImportsFromUnescapedImportExpressionConditioned(String directoryOfImportingFile, ProjectImportElement importElement, List`1& projects, Boolean throwOnFileNotExistsError) in /_/src/Build/Evaluation/Evaluator.cs:line 2173
   at Microsoft.Build.Evaluation.Evaluator`4.ExpandAndLoadImports(String directoryOfImportingFile, ProjectImportElement importElement) in /_/src/Build/Evaluation/Evaluator.cs:line 1944
   at Microsoft.Build.Evaluation.Evaluator`4.EvaluateImportElement(String directoryOfImportingFile, ProjectImportElement importElement) in /_/src/Build/Evaluation/Evaluator.cs:line 1823
   at Microsoft.Build.Evaluation.Evaluator`4.PerformDepthFirstPass(ProjectRootElement currentProjectOrImport) in /_/src/Build/Evaluation/Evaluator.cs:line 940
   at Microsoft.Build.Evaluation.Evaluator`4.Evaluate(ILoggingService loggingService, BuildEventContext buildEventContext) in /_/src/Build/Evaluation/Evaluator.cs:line 726
   at Microsoft.Build.Evaluation.Project.Reevaluate(ILoggingService loggingServiceForEvaluation, ProjectLoadSettings loadSettings) in /_/src/Build/Definition/Project.cs:line 2752
   at Microsoft.Build.Evaluation.Project.ReevaluateIfNecessary(ILoggingService loggingServiceForEvaluation, ProjectLoadSettings loadSettings) in /_/src/Build/Definition/Project.cs:line 2719
   at Microsoft.Build.Evaluation.Project.Initialize(IDictionary`2 globalProperties, String toolsVersion, String subToolsetVersion, ProjectLoadSettings loadSettings, EvaluationContext evaluationContext) in /_/src/Build/Definition/Project.cs:line 2822
   at Microsoft.Build.Evaluation.Project..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion, String subToolsetVersion, ProjectCollection projectCollection, ProjectLoadSettings loadSettings, EvaluationContext evaluationContext) in /_/src/Build/Definition/Project.cs:line 543
   at Microsoft.Build.Evaluation.ProjectCollection.LoadProject(String fileName, IDictionary`2 globalProperties, String toolsVersion) in /_/src/Build/Definition/ProjectCollection.cs:line 1118
   at ConsoleApp25.Program.Main(String[] args) in D:\tmp\source\repos\ConsoleApp25\ConsoleApp25\Program.cs:line 11

However it works as expected when targeting .NET 4.6.1

Any ideas?

ConsoleApp25.zip

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 1
  • Comments: 19 (9 by maintainers)

Most upvoted comments

I’m still curious why the desktop version works without MSBuildLocator, but .NET 5 version needs it.

The part of this that confuses me is not that it doesn’t work with .NET 5 but that it works with .NET Framework. It’s meaningless to say you need __ part of the MSBuild API if you don’t specify which version of MSBuild you want to use. That suggests to me that there’s some default logic that tries to guess which MSBuild you are trying to use, in which case it would make sense that it would only be right if it knows what framework to use, and it presumably hasn’t been updated since Core came out. I’m curious what would happen if you tried it on a computer with Core MSBuild but not Framework MSBuild.

I don’t even remember which project I was using this on anymore, so I’m not going to to try to recreate it half a year later, sorry.

I just hit this issue. Are there any known workarounds?