Skip to content

tools: Add conditional import to generated projects#15703

Open
jskeet wants to merge 1 commit into
googleapis:mainfrom
jskeet:conditional-import
Open

tools: Add conditional import to generated projects#15703
jskeet wants to merge 1 commit into
googleapis:mainfrom
jskeet:conditional-import

Conversation

@jskeet

@jskeet jskeet commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

See Librarian migration doc for details.

@jskeet jskeet requested a review from a team as a code owner June 23, 2026 08:41

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +305 to +307
doc.Add(new XElement("Import",
new XAttribute("Project", importFile),
new XAttribute("Condition", $"Exists('{importFile}')")));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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}')")));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Since doc is an XDocument"

From the code at line 271:

var doc = new XElement("Project", ...)

@jskeet

jskeet commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator Author

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"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants