msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

ProjectGraph throws MSB0001: Internal MSBuild Error: Only transitive references may reference inner builds that got generated by outer builds

Open AArnott opened this issue 2 years ago • 3 comments

Issue Description

While trying to build a static graph of our (large) repo, an InternalErrorException is thrown.

Given it's an internal error, I don't know if the project it is evaluating is actually faulty or not. And I can't tell from the exception which project it stumbled upon either, so if this is due to a faulty project, throwing an InvalidProjectFileException and including project data would help us isolate and correct the problem in our repo.

That said, our repo builds just fine, so I'm leaning toward believing this is just an internal error in MSBuild.

Steps to Reproduce

Run this code on the VS repo:

Environment.SetEnvironmentVariable("VisualStudioDir", "");
EvaluationContext evaluationContext = EvaluationContext.Create(EvaluationContext.SharingPolicy.Shared);
ImmutableDictionary<string, string> traversalProperties = ImmutableDictionary.Create<string, string>(StringComparer.OrdinalIgnoreCase).SetItem("IsMSBuildRetail", true);
ProjectGraphEntryPoint entry = new(Path.Combine(repoRoot, "dirs.proj"), traversalProperties);
ProjectGraph graph = new(new[] { entry }, projectCollection, (string projectPath, Dictionary<string, string> globalProperties, ProjectCollection projectCollection) =>
{
    ProjectOptions projectOptions = new()
    {
        EvaluationContext = evaluationContext,
        GlobalProperties = globalProperties,
        LoadSettings = ProjectLoadSettings.IgnoreMissingImports | ProjectLoadSettings.IgnoreInvalidImports,
        ProjectCollection = projectCollection,
    };
    Project project = Project.FromFile(projectPath, projectOptions);
    ProjectInstance projectInstance = project.CreateProjectInstance(ProjectInstanceSettings.None, evaluationContext);
    return projectInstance;
});

Expected Behavior

A valid graph.

Actual Behavior

An InternalErrorException is thrown from this callstack:

   at Microsoft.Build.Shared.ErrorUtilities.ThrowInternalError(String message, Exception innerException, Object[] args) in /_/src/Shared/ErrorUtilities.cs:line 82
   at Microsoft.Build.Graph.ProjectInterpretation.ReparentInnerBuilds(Dictionary`2 allNodes, GraphBuilder graphBuilder) in /_/src/Build/Graph/ProjectInterpretation.cs:line 183
   at Microsoft.Build.Graph.GraphBuilder.AddEdges(Dictionary`2 allParsedProjects) in /_/src/Build/Graph/GraphBuilder.cs:line 121
   at Microsoft.Build.Graph.GraphBuilder.BuildGraph() in /_/src/Build/Graph/GraphBuilder.cs:line 88
   at Microsoft.Build.Graph.ProjectGraph..ctor(IEnumerable`1 entryPoints, ProjectCollection projectCollection, ProjectInstanceFactoryFunc projectInstanceFactory, Int32 degreeOfParallelism, CancellationToken cancellationToken) in /_/src/Build/Graph/ProjectGraph.cs:line 432
   at Microsoft.Build.Graph.ProjectGraph..ctor(IEnumerable`1 entryPoints, ProjectCollection projectCollection, ProjectInstanceFactoryFunc projectInstanceFactory) in /_/src/Build/Graph/ProjectGraph.cs:line 339
   at RazzleProjectUpdater.RepoCache.<>c__DisplayClass17_1.<CreateAsync>g__CreateProjectGraph|5(Boolean msbuildRetail, <>c__DisplayClass17_0& ) in C:\Users\andarno\source\repos\MSBuild converters\RazzleProjectUpdater\RazzleProjectUpdater\RepoCache.cs:line 132
   at RazzleProjectUpdater.RepoCache.<CreateAsync>g__DiscoverBuildableProjects|17_2(HashSet`1& msbuildRetail, HashSet`1& corext, <>c__DisplayClass17_0& ) in C:\Users\andarno\source\repos\MSBuild converters\RazzleProjectUpdater\RazzleProjectUpdater\RepoCache.cs:line 95

With the message:

MSB0001: Internal MSBuild Error: Only transitive references may reference inner builds that got generated by outer builds

Versions & Configurations

This is using MSBuild 17.2.0.11601.

AArnott avatar Mar 03 '22 14:03 AArnott

MSBuild Team Triage: Is this a blocking scenario? Does it happen in other scenarios?

benvillalobos avatar Mar 03 '22 17:03 benvillalobos

@BenVillalobos Yes, this blocks our ability to use static graph in a tool that would significantly benefit in perf and reliability if we could start processing with a full graph of projects.

AArnott avatar Mar 03 '22 19:03 AArnott

This is related to a ProjectReference having an explicit SetTargetFramework metdata on it, which is a not-very-well defined scenario, either in the project graph nor in NuGet

dfederm avatar Mar 25 '22 21:03 dfederm

Might be related to: https://github.com/dotnet/msbuild/pull/8267

vlada-shubina avatar Jan 25 '23 16:01 vlada-shubina

@AArnott

apologies for late reply.

Could you please confirm if this issue still occurs and/or affects you? I tried to repro it on current state of VS repo using Microsoft.Build 17.2.0, and was unable to.

Thanks

vlada-shubina avatar Feb 01 '23 16:02 vlada-shubina

I can't repro it any more either. Thank you.

AArnott avatar Mar 07 '23 16:03 AArnott