Skip to content

Commit 317d538

Browse files
Improve handling of net versions missmatch (#526)
1 parent 0f8aa4f commit 317d538

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

lean/components/docker/lean_runner.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -585,13 +585,14 @@ def set_up_csharp_options(self, project_dir: Path, run_options: Dict[str, Any],
585585
"mode": "rw"
586586
}
587587

588+
framework_ver = self._docker_manager.get_image_label(image, 'target_framework',
589+
DEFAULT_LEAN_DOTNET_FRAMEWORK)
590+
588591
# Ensure all .csproj files refer to the version of LEAN in the Docker container
589592
csproj_temp_dir = self._temp_manager.create_temporary_directory()
590593
for path in compile_root.rglob("*.csproj"):
591-
self._ensure_csproj_uses_correct_lean(compile_root, path, csproj_temp_dir, run_options)
594+
self._ensure_csproj_is_valid(compile_root, path, csproj_temp_dir, run_options, framework_ver)
592595

593-
framework_ver = self._docker_manager.get_image_label(image, 'target_framework',
594-
DEFAULT_LEAN_DOTNET_FRAMEWORK)
595596
# Set up the MSBuild properties
596597
msbuild_properties = {
597598
"Configuration": "Release" if release else "Debug",
@@ -601,8 +602,6 @@ def set_up_csharp_options(self, project_dir: Path, run_options: Dict[str, Any],
601602
"GenerateAssemblyInfo": "false",
602603
"GenerateTargetFrameworkAttribute": "false",
603604
"AppendTargetFrameworkToOutputPath": "false",
604-
"AutoGenerateBindingRedirects": "true",
605-
"GenerateBindingRedirectsOutputType": "true",
606605
"AutomaticallyUseReferenceAssemblyPackages": "false",
607606
"CopyLocalLockFileAssemblies": "true",
608607
"PathMap": f"/LeanCLI={str(compile_root)}",
@@ -770,11 +769,12 @@ def _get_csharp_compile_root(self, project_dir: Path) -> Path:
770769

771770
return project_dir
772771

773-
def _ensure_csproj_uses_correct_lean(self,
774-
compile_root: Path,
775-
csproj_path: Path,
776-
temp_dir: Path,
777-
run_options: Dict[str, Any]) -> None:
772+
def _ensure_csproj_is_valid(self,
773+
compile_root: Path,
774+
csproj_path: Path,
775+
temp_dir: Path,
776+
run_options: Dict[str, Any],
777+
net_framework: str) -> None:
778778
"""Ensures a C# project is compiled using the version of LEAN in the Docker container.
779779
780780
When a .csproj file refers to the NuGet version of LEAN,
@@ -791,6 +791,18 @@ def _ensure_csproj_uses_correct_lean(self,
791791
csproj = self._xml_manager.parse(csproj_path.read_text(encoding="utf-8"))
792792
include_added = False
793793

794+
if net_framework:
795+
target_framework_iter = csproj.iter("TargetFramework")
796+
if target_framework_iter:
797+
target_frameworks = [framework.text for framework in target_framework_iter]
798+
if target_frameworks:
799+
if net_framework not in target_frameworks:
800+
raise RuntimeError(f"This project is targeting {target_frameworks[0].replace('net', 'Net ')}"
801+
f" and {net_framework.replace('net', 'Net ')} is required. Please"
802+
f" update the \"Target Framework\" project setting in VSCode to"
803+
f" the new SDK or modify the csproj file directly to "
804+
f"\"<TargetFramework>{net_framework}</TargetFramework>\".")
805+
794806
for package_reference in csproj.iter("PackageReference"):
795807
if not package_reference.get("Include", "").lower().startswith("quantconnect."):
796808
continue

lean/constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
# we get these values from the image labels, but we still have defaults just in case
2525
DEFAULT_LEAN_PYTHON_VERSION = "3.11"
26-
DEFAULT_LEAN_STRICT_PYTHON_VERSION = f"{DEFAULT_LEAN_PYTHON_VERSION}.7"
27-
DEFAULT_LEAN_DOTNET_FRAMEWORK = "net6.0"
26+
DEFAULT_LEAN_STRICT_PYTHON_VERSION = f"{DEFAULT_LEAN_PYTHON_VERSION}.11"
27+
DEFAULT_LEAN_DOTNET_FRAMEWORK = "net9.0"
2828

2929
# Label name used in Docker containers to specify the version of Lean being used
3030
CONTAINER_LABEL_LEAN_VERSION_NAME = "lean_version"

tests/commands/test_backtest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ def test_backtest_auto_updates_outdated_csharp_csproj() -> None:
573573
<PropertyGroup>
574574
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
575575
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
576-
<TargetFramework>net6.0</TargetFramework>
576+
<TargetFramework>net9.0</TargetFramework>
577577
<OutputPath>bin/$(Configuration)</OutputPath>
578578
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
579579
<NoWarn>CS0618</NoWarn>

tests/components/config/test_project_config_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_get_csharp_libraries_returns_all_libraries_in_package_reference_tags_in
6060
<PropertyGroup>
6161
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6262
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
63-
<TargetFramework>net6.0</TargetFramework>
63+
<TargetFramework>net9.0</TargetFramework>
6464
<OutputPath>bin/$(Configuration)</OutputPath>
6565
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
6666
<NoWarn>CS0618</NoWarn>
@@ -99,7 +99,7 @@ def test_get_csharp_libraries_skips_invalid_package_reference_tags() -> None:
9999
<PropertyGroup>
100100
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
101101
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
102-
<TargetFramework>net6.0</TargetFramework>
102+
<TargetFramework>net9.0</TargetFramework>
103103
<OutputPath>bin/$(Configuration)</OutputPath>
104104
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
105105
<NoWarn>CS0618</NoWarn>

tests/components/docker/test_lean_runner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from lean.components.util.project_manager import ProjectManager
2828
from lean.components.util.temp_manager import TempManager
2929
from lean.components.util.xml_manager import XMLManager
30-
from lean.constants import DEFAULT_ENGINE_IMAGE, LEAN_ROOT_PATH, DEFAULT_DATA_DIRECTORY_NAME
30+
from lean.constants import DEFAULT_ENGINE_IMAGE, LEAN_ROOT_PATH, DEFAULT_DATA_DIRECTORY_NAME, DEFAULT_LEAN_DOTNET_FRAMEWORK
3131
from lean.models.utils import DebuggingMethod
3232
from lean.models.docker import DockerImage
3333
from lean.models.modules import NuGetPackage
@@ -97,6 +97,7 @@ def test_run_lean_compiles_csharp_project_in_correct_configuration(release: bool
9797

9898
docker_manager = mock.Mock()
9999
docker_manager.run_image.return_value = True
100+
docker_manager.get_image_label.return_value = DEFAULT_LEAN_DOTNET_FRAMEWORK
100101

101102
lean_runner = create_lean_runner(docker_manager)
102103

@@ -123,6 +124,7 @@ def test_run_lean_runs_lean_container_detached() -> None:
123124

124125
docker_manager = mock.Mock()
125126
docker_manager.run_image.return_value = True
127+
docker_manager.get_image_label.return_value = DEFAULT_LEAN_DOTNET_FRAMEWORK
126128

127129
lean_runner = create_lean_runner(docker_manager)
128130

@@ -384,6 +386,7 @@ def test_run_lean_sets_image_name_when_debugging_with_vsdbg() -> None:
384386

385387
docker_manager = mock.Mock()
386388
docker_manager.run_image.return_value = True
389+
docker_manager.get_image_label.return_value = DEFAULT_LEAN_DOTNET_FRAMEWORK
387390

388391
lean_runner = create_lean_runner(docker_manager)
389392

@@ -407,6 +410,7 @@ def test_run_lean_exposes_ssh_when_debugging_with_rider() -> None:
407410

408411
docker_manager = mock.Mock()
409412
docker_manager.run_image.return_value = True
413+
docker_manager.get_image_label.return_value = DEFAULT_LEAN_DOTNET_FRAMEWORK
410414

411415
lean_runner = create_lean_runner(docker_manager)
412416

@@ -556,6 +560,7 @@ def test_run_lean_compiles_csharp_project_that_is_part_of_a_solution(in_solution
556560

557561
docker_manager = mock.Mock()
558562
docker_manager.run_image.return_value = True
563+
docker_manager.get_image_label.return_value = DEFAULT_LEAN_DOTNET_FRAMEWORK
559564

560565
root_dir = Path.cwd()
561566
lean_runner = create_lean_runner(docker_manager)

0 commit comments

Comments
 (0)