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

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

Closed
xtmq opened this issue Mar 7, 2024 · 12 comments
Closed

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

xtmq opened this issue Mar 7, 2024 · 12 comments
Assignees
Labels
backlog bug closed-by-bot Closed due to lack of activity gathering-feedback The issue requires feedback in order to be planned, please comment if the feature is useful for you needs-investigation needs-more-info Issues that need more info to continue investigation. stale For issues that haven't had activity in some time. triaged

Comments

@xtmq
Copy link

xtmq commented Mar 7, 2024

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

@AR-May
Copy link
Member

AR-May commented Mar 12, 2024

@GangWang01 let's see if we can repro this

@GangWang01
Copy link
Member

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.

@xtmq
Copy link
Author

xtmq commented Mar 25, 2024

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.

@GangWang01 GangWang01 added the gathering-feedback The issue requires feedback in order to be planned, please comment if the feature is useful for you label Apr 17, 2024
@GangWang01 GangWang01 removed their assignment Apr 17, 2024
@rwx788
Copy link

rwx788 commented Jun 3, 2024

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.

@xtmq
Copy link
Author

xtmq commented Aug 21, 2024

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)

@GangWang01
Copy link
Member

@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?

@xtmq
Copy link
Author

xtmq commented Aug 21, 2024

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

@GangWang01
Copy link
Member

@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.

@rwx788
Copy link

rwx788 commented Aug 28, 2024

@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!

@GangWang01
Copy link
Member

GangWang01 commented Sep 2, 2024

@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 GangWang01 added the needs-more-info Issues that need more info to continue investigation. label Oct 9, 2024
Copy link
Contributor

This issue is marked as stale because feedback has been requested for 30 days with no response. Please respond within 14 days or this issue will be closed due to inactivity.

@dotnet-policy-service dotnet-policy-service bot added the stale For issues that haven't had activity in some time. label Nov 9, 2024
Copy link
Contributor

This issue was closed due to inactivity. If you can still reproduce this bug, please comment with the requested information, detailed steps to reproduce the problem, or any other notes that might help in the investigation.

@dotnet-policy-service dotnet-policy-service bot added the closed-by-bot Closed due to lack of activity label Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog bug closed-by-bot Closed due to lack of activity gathering-feedback The issue requires feedback in order to be planned, please comment if the feature is useful for you needs-investigation needs-more-info Issues that need more info to continue investigation. stale For issues that haven't had activity in some time. triaged
Projects
None yet
Development

No branches or pull requests

4 participants