msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

[Bug]: Internal MSBuild Error: Targets cannot be reset once set.

Open xtmq opened this issue 11 months ago • 10 comments

Issue Description

Hello MSBuild Team! As you may know Rider uses MSBuild API to load and build projects. Some time ago we faced a new issue with project loading:

Microsoft.Build.Framework.InternalErrorException: MSB0001: Internal MSBuild Error: Targets cannot be reset once set.
   at Microsoft.Build.Shared.ErrorUtilities.ThrowInternalError(String message, Exception innerException, Object[] args)
   at Microsoft.Build.BackEnd.BuildRequestConfiguration.set_ProjectTargets(HashSet`1 value)
   at Microsoft.Build.BackEnd.BuildRequestConfiguration.SetProjectBasedState(ProjectInstance project)
   at Microsoft.Build.BackEnd.BuildRequestConfiguration.set_Project(Pro[1936226.TestSolution.msbuild-task.zip](1936226.TestSolution.msbuild-task.zip)jectInstance value)
   at Microsoft.Build.Execution.BuildManager.CreateConfiguration(Project project, BuildRequestConfiguration existingConfiguration)
   at Microsoft.Build.BackEnd.ConfigCache.GetMatchingConfiguration(ConfigurationMetadata configMetadata, ConfigCreateCallback callback, Boolean loadProject)
   at Microsoft.Build.Execution.BuildManager.GetProjectInstanceForBuild(Project project)

We get this exception from time to time (but very seldom) when Rider has to load lots of tiny projects on solution opening. Our code looks like this simplified example:

var buildManager = new BuildManager();
var projectCollection = new ProjectCollection();
var context = EvaluationContext.Create(EvaluationContext.SharingPolicy.Shared);

Parallel.ForEach(paths, new ParallelOptions { MaxDegreeOfParallelism = 4}, path => {

var xml = ProjectRootElement.Open(path, projectCollection, preserveFormatting: true);
var project = Project.FromProjectRootElement(xml, new ProjectOptions
        {
          GlobalProperties = ...,
          ProjectCollection = projectCollection,
          LoadSettings = ...,
          EvaluationContext = context 
        });
var projectInstance = buildManager.GetProjectInstanceForBuild(project); // Exception here

});

This starts to happening on Linux in this SDK: dotnet-sdk-8.0.100-linux-x64-c4c95f56 Looks like something was changed inside ProjectInstance creating and not it is not thread-safe anymore?

Steps to Reproduce

We don't have exact steps now. But it is not obvious what was broken in the last SDK we can try to make a repro.

Expected Behavior

no exception

Actual Behavior

exception

Analysis

No response

Versions & Configurations

dotnet-sdk-8.0.100-linux-x64-c4c95f56

xtmq avatar Mar 07 '24 10:03 xtmq

@GangWang01 let's see if we can repro this

AR-May avatar Mar 12 '24 15:03 AR-May

I couldn't reproduce with nuget package Microsoft.Build 17.9.5.

@xtmq Can you share the following information for helping to reproduce? Thank you!

  • Which version the MSBuild package is.
  • A sample of the projects to be loaded.

GangWang01 avatar Mar 25 '24 08:03 GangWang01

I can not reproduce it also. It fails on our CI/CD server like once per week or once per 200-300 builds. The solution - 300 class library projects with single file inside.

xtmq avatar Mar 25 '24 09:03 xtmq

Hi! Sorry for the delay, here is the project where this error occurs. Last failures we got were with msbuild from dotnet 8.0.300 Mmg300-20231224.zip

As it's stated above, problem occurs quite rarely, but and is visible from our CI only. Please let us know which additional information we can provide.

rwx788 avatar Jun 03 '24 16:06 rwx788

Now we observe the same exception, but from build:

Microsoft.Build.Framework.InternalErrorException: MSB0001: Internal MSBuild Error: Targets cannot be reset once set.
   at Microsoft.Build.Shared.ErrorUtilities.ThrowInternalError(String message, Exception innerException, Object[] args)
   at Microsoft.Build.BackEnd.BuildRequestConfiguration.set_ProjectTargets(HashSet`1 value)
   at Microsoft.Build.BackEnd.BuildRequestConfiguration.SetProjectBasedState(ProjectInstance project)
   at Microsoft.Build.BackEnd.BuildRequestConfiguration.set_Project(ProjectInstance value)
   at Microsoft.Build.Execution.BuildManager.ResolveConfiguration(BuildRequestConfiguration unresolvedConfiguration, BuildRequestConfiguration matchingConfigurationFromCache, Boolean replaceProjectInstance)
   at Microsoft.Build.Execution.BuildManager.ExecuteSubmission(BuildSubmission submission, Boolean allowMainThreadBuild)

xtmq avatar Aug 21 '24 07:08 xtmq

@xtmq do you use to Microsoft.Build.Locator to register the instance of .NET SDK.? From the code snippet in the issue description, I couldn't know about it. And what's the version of MSBuild packages as well as .NET SDK?

GangWang01 avatar Aug 21 '24 10:08 GangWang01

I use msbuild from this SDK: dotnet-sdk-8.0.300-linux-x64-04722663, we don't call locator and assume MSBuild knows about it's own SDK

xtmq avatar Aug 21 '24 11:08 xtmq

@xtmq To get MSBuild work, besides MSBuild assemblies it also requires importing necessary props and targets files from SDK. Can you provide a minimal project that includes how it references MSBuild assemblies and imports props and targets files from SDK? So that we can try to reproduce the issue.

GangWang01 avatar Aug 28 '24 09:08 GangWang01

@xtmq To get MSBuild work, besides MSBuild assemblies it also requires importing necessary props and targets files from SDK. Can you provide a minimal project that includes how it references MSBuild assemblies and imports props and targets files from SDK? So that we can try to reproduce the issue.

Hi @GangWang01 ! Thanks for your help with the investigation. It's actually the same project we use in tests which I have pasted above in the issue. There is nothing specific except of having 300 synthetic library projects all of which target net8.0. And problem appears while loading some of them once in a while. Loading is done as per that code snippet above. That should help reproducing the issue. Please, let us know if further details are needed. Cheers!

rwx788 avatar Aug 28 '24 09:08 rwx788

@rwx788 Thanks for quick response!

To get MSBuild work, besides MSBuild assemblies it also requires importing necessary props and targets files from SDK. Can you provide a minimal project that includes how it references MSBuild assemblies and imports props and targets files from SDK? So that we can try to reproduce the issue.

Here the project is the one that has the code snippet above loading the test projects. It helps to understand if necessary props and targets files from SDK are imported correctly.

GangWang01 avatar Sep 02 '24 10:09 GangWang01