Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Microsoft.Build.Evaluation.Project Constructor throws an exception when dotnet sdk 5.0.100 is installed. #6026

Closed
gewarren opened this issue Jan 12, 2021 · 7 comments
Labels
needs-triage Have yet to determine what bucket this goes in.

Comments

@gewarren
Copy link
Contributor


Issue moved from dotnet/dotnet-api-docs#5143


From @WenyunZou on Wednesday, November 25, 2020 9:13:21 AM

Hi,

Api at https://docs.microsoft.com/en-us/dotnet/api/microsoft.build.evaluation.project.-ctor?view=msbuild-16-netcore

If I'm opening the issued at a wrong place, please help me to redirect it. Thank you!

Description:
It had been working fine until I installed dotnet sdk 5.0.100 recently. The constructor of Microsoft.Build.Evaluation.Project throws an exception "MSBuild.CouldNotLoadSdkResolver".
Code:
Project project = new Project(projectFilePath);
Exception:
The SDK resolver type "WorkloadSdkResolver" failed to load. Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
StackTrace:

   at Microsoft.Build.Shared.ProjectFileErrorUtilities.VerifyThrowInvalidProjectFile(Boolean condition, String errorSubCategoryResourceName, BuildEventFileInfo projectFile, Exception innerException, String resourceName, Object[] args)
   at Microsoft.Build.Shared.ProjectFileErrorUtilities.ThrowInvalidProjectFile(BuildEventFileInfo projectFile, Exception innerException, String resourceName, Object[] args)
   at Microsoft.Build.BackEnd.SdkResolution.SdkResolverLoader.LoadResolvers(String resolverPath, LoggingContext loggingContext, ElementLocation location, List`1 resolvers)
   at Microsoft.Build.BackEnd.SdkResolution.SdkResolverLoader.LoadResolvers(LoggingContext loggingContext, ElementLocation location)
   at Microsoft.Build.BackEnd.SdkResolution.SdkResolverService.Initialize(LoggingContext loggingContext, ElementLocation location)
   at Microsoft.Build.BackEnd.SdkResolution.SdkResolverService.ResolveSdk(Int32 submissionId, SdkReference sdk, LoggingContext loggingContext, ElementLocation sdkReferenceLocation, String solutionPath, String projectPath, Boolean interactive, Boolean isRunningInVisualStudio)
   at Microsoft.Build.BackEnd.SdkResolution.CachingSdkResolverService.<>n__0(Int32 submissionId, SdkReference sdk, LoggingContext loggingContext, ElementLocation sdkReferenceLocation, String solutionPath, String projectPath, Boolean interactive, Boolean isRunningInVisualStudio)
   at Microsoft.Build.BackEnd.SdkResolution.CachingSdkResolverService.<>c__DisplayClass3_0.<ResolveSdk>b__1()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.get_Value()
   at Microsoft.Build.BackEnd.SdkResolution.CachingSdkResolverService.ResolveSdk(Int32 submissionId, SdkReference sdk, LoggingContext loggingContext, ElementLocation sdkReferenceLocation, String solutionPath, String projectPath, Boolean interactive, Boolean isRunningInVisualStudio)
   at Microsoft.Build.Evaluation.Evaluator`4.ExpandAndLoadImportsFromUnescapedImportExpressionConditioned(String directoryOfImportingFile, ProjectImportElement importElement, List`1& projects, SdkResult& sdkResult, Boolean throwOnFileNotExistsError)
   at Microsoft.Build.Evaluation.Evaluator`4.ExpandAndLoadImports(String directoryOfImportingFile, ProjectImportElement importElement, SdkResult& sdkResult)
   at Microsoft.Build.Evaluation.Evaluator`4.EvaluateImportElement(String directoryOfImportingFile, ProjectImportElement importElement)
   at Microsoft.Build.Evaluation.Evaluator`4.PerformDepthFirstPass(ProjectRootElement currentProjectOrImport)
   at Microsoft.Build.Evaluation.Evaluator`4.Evaluate()
   at Microsoft.Build.Evaluation.Evaluator`4.Evaluate(IEvaluatorData`4 data, ProjectRootElement root, ProjectLoadSettings loadSettings, Int32 maxNodeCount, PropertyDictionary`1 environmentProperties, ILoggingService loggingService, IItemFactory`2 itemFactory, IToolsetProvider toolsetProvider, ProjectRootElementCacheBase projectRootElementCache, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, EvaluationContext evaluationContext, Boolean interactive)
   at Microsoft.Build.Evaluation.Project.ProjectImpl.Reevaluate(ILoggingService loggingServiceForEvaluation, ProjectLoadSettings loadSettings, EvaluationContext evaluationContext)
   at Microsoft.Build.Evaluation.Project.ProjectImpl.ReevaluateIfNecessary(ILoggingService loggingServiceForEvaluation, ProjectLoadSettings loadSettings, EvaluationContext evaluationContext)
   at Microsoft.Build.Evaluation.Project.ProjectImpl.ReevaluateIfNecessary(ILoggingService loggingServiceForEvaluation, EvaluationContext evaluationContext)
   at Microsoft.Build.Evaluation.Project.ProjectImpl.ReevaluateIfNecessary(EvaluationContext evaluationContext)
   at Microsoft.Build.Evaluation.Project.ProjectImpl.Initialize(IDictionary`2 globalProperties, String toolsVersion, String subToolsetVersion, ProjectLoadSettings loadSettings, EvaluationContext evaluationContext)
   at Microsoft.Build.Evaluation.Project..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion, String subToolsetVersion, ProjectCollection projectCollection, ProjectLoadSettings loadSettings, EvaluationContext evaluationContext)
   at Microsoft.Build.Evaluation.Project..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion, String subToolsetVersion, ProjectCollection projectCollection, ProjectLoadSettings loadSettings)
   at Microsoft.Build.Evaluation.Project..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion, ProjectCollection projectCollection, ProjectLoadSettings loadSettings)
   at Microsoft.Build.Evaluation.Project..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion, ProjectCollection projectCollection)
   at Microsoft.Build.Evaluation.Project..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion)
   at Microsoft.Build.Evaluation.Project..ctor(String projectFile)
   at XXXXXXXXXXXXXXXXXXXX

Repro Steps:

  1. create a c# project
  2. Add nuget package: Microsoft.Build(16.3.0) & Microsoft.Build.Locator(1.2.6)
  3. type the following code into Program.cs file:
using Microsoft.Build.Evaluation;
using Microsoft.Build.Locator;
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            MSBuildLocator.RegisterDefaults();
            string path = ResolveOutputAssemblyPath(@"a path to any valid .csproj file");
            Console.WriteLine(path);

            Console.ReadKey();
            return;
        }

        private static string ResolveOutputAssemblyPath(string projectFilePath, string buildConfiguration = "Debug")
        {
            Project project = new Project(projectFilePath);
            string assemblyPath = project.GetPropertyValue("TargetPath");
            project.ProjectCollection.UnloadProject(project);
            project.ProjectCollection.UnloadProject(project.Xml);
            if (string.IsNullOrEmpty(assemblyPath))
            {
                throw new Exception($"Failed to resolve target assembly of project {projectFilePath}.");
            }

            return assemblyPath;
        }
    }
}
  1. type F5 to run
@gewarren
Copy link
Contributor Author


Issue moved from dotnet/dotnet-api-docs#5143


From @Dotnet-GitSync-Bot on Wednesday, November 25, 2020 9:13:43 AM

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@gewarren
Copy link
Contributor Author


Issue moved from dotnet/dotnet-api-docs#5143


From @gewarren on Tuesday, January 12, 2021 7:14:52 PM

cc @ghogen @rainersigwald. Should I move this issue here? https://github.com/dotnet/msbuild/issues

@gewarren
Copy link
Contributor Author


Issue moved from dotnet/dotnet-api-docs#5143


From @ghogen on Tuesday, January 12, 2021 7:17:42 PM

@gewarren @rainersigwald Agreed - looks like a product bug report that should be moved to the MSBuild repo.

@rainersigwald
Copy link
Member

@WenyunZou what version of .NET Core is your application targeting? This sounds like a 3.1 application trying to load a 5.0 SDK.

@benvillalobos benvillalobos added the needs-triage Have yet to determine what bucket this goes in. label Jan 13, 2021
@WenyunZou
Copy link

WenyunZou commented Jan 15, 2021 via email

@rainersigwald
Copy link
Member

Are you using the most recent version of MSBuildLocator? microsoft/MSBuildLocator#106 has a fix for this specific issue.

@Forgind
Copy link
Member

Forgind commented Jan 27, 2021

I'm assuming updating MSBuildLocator resolved the problem? If not, let me know, and I can reopen this.

@Forgind Forgind closed this as completed Jan 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Have yet to determine what bucket this goes in.
Projects
None yet
Development

No branches or pull requests

5 participants