tools: Add conditional import to generated projects#15703
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a new style of project file augmentation in NonSourceGenerator.cs by adding a conditional import element to the generated project file. A critical issue was identified where calling doc.Add() on an XDocument will throw an InvalidOperationException because the document already has a root element. The reviewer suggested appending the Import element to the root element using doc.Root.Add() instead.
| doc.Add(new XElement("Import", | ||
| new XAttribute("Project", importFile), | ||
| new XAttribute("Condition", $"Exists('{importFile}')"))); |
There was a problem hiding this comment.
Since doc is an XDocument, calling doc.Add() to add an XElement will throw an InvalidOperationException ("This document already has a root element.") because the document already has a root element (Project). The <Import> element should be added to the root element instead using doc.Root.Add().
doc.Root.Add(new XElement("Import",
new XAttribute("Project", importFile),
new XAttribute("Condition", $"Exists('{importFile}')")));There was a problem hiding this comment.
"Since doc is an XDocument"
From the code at line 271:
var doc = new XElement("Project", ...)|
Note: I was slightly surprised that the Docker container tests didn't need updating, but they don't - they're sensitive to the presence of the files, and that the generated files do actually build, but not to the specific content. |
| } | ||
|
|
||
| // New style of project file augmentation: a conditional import. We can then have the .csproj.google | ||
| // files in the production location rather than in generator-input. This doesn't work for "ReplaceAll" |
There was a problem hiding this comment.
What's the common theme for projects that are currently relegated to ReplaceAll such that they won't be generated with Librarian? I see a couple of ConformanceTests projects and one *.CommonTesting project.
There was a problem hiding this comment.
All of the ReplaceAll ones are - I think - handwritten code, and I think none of them fall into the "production code, unit testing, integration testing or snippets" category which will be generated. Will need to double check though.
See Librarian migration doc for details.