diff --git a/lean/commands/library/add.py b/lean/commands/library/add.py
index 8dbebfe7..b4140f43 100644
--- a/lean/commands/library/add.py
+++ b/lean/commands/library/add.py
@@ -68,11 +68,8 @@ def _add_csharp_package_to_csproj(csproj_file: Path, name: str, version: str) ->
if existing_package_reference is not None:
existing_package_reference.set("Version", version)
else:
- last_item_group = csproj_tree.find(".//ItemGroup[last()]")
- if last_item_group is None:
- last_item_group = etree.SubElement(csproj_tree.find(".//Project"), "ItemGroup")
-
- last_item_group.append(etree.fromstring(f''))
+ item_group = xml_manager.unconditional_item_group(csproj_tree)
+ item_group.append(etree.fromstring(f''))
csproj_file.write_text(xml_manager.to_string(csproj_tree), encoding="utf-8")
diff --git a/lean/components/util/library_manager.py b/lean/components/util/library_manager.py
index d3024ed8..570cfc1d 100644
--- a/lean/components/util/library_manager.py
+++ b/lean/components/util/library_manager.py
@@ -278,11 +278,8 @@ def _add_csharp_project_to_csproj(self, csproj_file: Path, library_csproj_path:
existing_project_reference = csproj_tree.find(f".//ProjectReference[@Include='{library_csproj_path}']")
if existing_project_reference is None:
- last_item_group = csproj_tree.find(".//ItemGroup[last()]")
- if last_item_group is None:
- last_item_group = etree.SubElement(csproj_tree.find(".//Project"), "ItemGroup")
-
- last_item_group.append(etree.fromstring(f''))
+ item_group = self._xml_manager.unconditional_item_group(csproj_tree)
+ item_group.append(etree.fromstring(f''))
csproj_file.write_text(self._xml_manager.to_string(csproj_tree), encoding="utf-8")
diff --git a/lean/components/util/xml_manager.py b/lean/components/util/xml_manager.py
index 97036ddb..c3ecbe3d 100644
--- a/lean/components/util/xml_manager.py
+++ b/lean/components/util/xml_manager.py
@@ -37,3 +37,22 @@ def to_string(self, root: etree.Element) -> str:
"""
etree.indent(root, " " * 4)
return etree.tostring(root, encoding="utf-8", method="xml", pretty_print=True).decode("utf-8")
+
+ def unconditional_item_group(self, root: etree.Element) -> etree.Element:
+ """Returns a project ItemGroup with no Condition, creating one when none exists.
+
+ Direct children only: an ItemGroup inside a Target is not a project reference group.
+ A Condition on the group would limit a new reference to that configuration.
+
+ :param root: the parsed csproj element
+ :return: an ItemGroup that is included in every build configuration
+ """
+ project = root if str(root.tag).rsplit("}", 1)[-1] == "Project" else root.find(".//Project")
+ if project is None:
+ project = root
+
+ for item_group in reversed(project.findall("ItemGroup")):
+ if item_group.get("Condition") is None:
+ return item_group
+
+ return etree.SubElement(project, "ItemGroup")
diff --git a/tests/components/util/test_library_manager.py b/tests/components/util/test_library_manager.py
index 92382d76..d69a9490 100644
--- a/tests/components/util/test_library_manager.py
+++ b/tests/components/util/test_library_manager.py
@@ -168,3 +168,95 @@ def test_detects_circular_dependencies() -> None:
assert False, "Expected library_manager.add_lean_library_reference_to_project() to raise on circular dependency"
except RuntimeError:
pass
+
+
+def _csproj_with_conditional_item_group(only_conditional: bool) -> Path:
+ groups = "" if only_conditional else """
+
+
+ """
+ csproj_file = Path.cwd() / "App.csproj"
+ csproj_file.write_text(f"""
+
+ net10.0
+ {groups}
+
+
+""", encoding="utf-8")
+ return csproj_file
+
+
+def _item_groups_by_condition(csproj_file: Path):
+ csproj_tree = container.xml_manager.parse(csproj_file.read_text(encoding="utf-8"))
+ groups = csproj_tree.findall(".//ItemGroup")
+ unconditional = [group for group in groups if group.get("Condition") is None]
+ conditional = [group for group in groups if group.get("Condition") is not None]
+ return unconditional, conditional
+
+
+def test_add_csharp_project_to_csproj_does_not_use_a_conditional_item_group() -> None:
+ create_fake_lean_cli_directory()
+
+ csproj_file = _csproj_with_conditional_item_group(False)
+ reference = "../Library/Lib/Lib.csproj"
+ _create_library_manager()._add_csharp_project_to_csproj(csproj_file, reference)
+
+ unconditional, conditional = _item_groups_by_condition(csproj_file)
+ assert len(unconditional) == 1
+ assert [element.get("Include") for element in unconditional[0].findall("ProjectReference")] == [reference]
+ assert unconditional[0].find("PackageReference").get("Include") == "QuantConnect.Lean"
+ assert conditional[0].find("ProjectReference") is None
+ assert conditional[0].get("Condition") == "'$(Configuration)' == 'Debug'"
+
+
+def test_add_csharp_project_to_csproj_with_only_a_conditional_item_group_adds_an_unconditional_one() -> None:
+ create_fake_lean_cli_directory()
+
+ csproj_file = _csproj_with_conditional_item_group(True)
+ reference = "../Library/Lib/Lib.csproj"
+ _create_library_manager()._add_csharp_project_to_csproj(csproj_file, reference)
+
+ unconditional, conditional = _item_groups_by_condition(csproj_file)
+ assert len(unconditional) == 1
+ assert [element.get("Include") for element in unconditional[0].findall("ProjectReference")] == [reference]
+ assert conditional[0].find("ProjectReference") is None
+
+
+def test_add_csharp_package_to_csproj_does_not_use_a_conditional_item_group() -> None:
+ from lean.commands.library.add import _add_csharp_package_to_csproj
+
+ create_fake_lean_cli_directory()
+
+ csproj_file = _csproj_with_conditional_item_group(False)
+ _add_csharp_package_to_csproj(csproj_file, "Example.Package", "1.2.3")
+
+ unconditional, conditional = _item_groups_by_condition(csproj_file)
+ assert len(unconditional) == 1
+ assert any(element.get("Include") == "Example.Package" and element.get("Version") == "1.2.3"
+ for element in unconditional[0].findall("PackageReference"))
+ assert conditional[0].find("PackageReference") is None
+
+
+def test_add_csharp_project_to_csproj_does_not_use_an_item_group_inside_a_target() -> None:
+ create_fake_lean_cli_directory()
+
+ csproj_file = Path.cwd() / "App.csproj"
+ csproj_file.write_text("""
+
+
+
+
+
+
+
+
+
+""", encoding="utf-8")
+ reference = "../Library/Lib/Lib.csproj"
+ _create_library_manager()._add_csharp_project_to_csproj(csproj_file, reference)
+
+ csproj_tree = container.xml_manager.parse(csproj_file.read_text(encoding="utf-8"))
+ project_group = csproj_tree.find("ItemGroup")
+ target_group = csproj_tree.find("Target/ItemGroup")
+ assert [element.get("Include") for element in project_group.findall("ProjectReference")] == [reference]
+ assert target_group.find("ProjectReference") is None