Skip to content

Commit 1569ae1

Browse files
[release/8.2] Allow referencing older version of AppHost package for backward compatibility (#5708)
* Allow referencing older version of AppHost package for backward compatibility * Remove unnecessary using statement * Rename test --------- Co-authored-by: Jose Perez Rodriguez <[email protected]>
1 parent 234ce65 commit 1569ae1

File tree

7 files changed

+59
-11
lines changed

7 files changed

+59
-11
lines changed

src/Aspire.Hosting.Sdk/Aspire.Hosting.Sdk.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<None Include="..\Aspire.Hosting\build\*.targets" Link="SDK\%(Filename)%(Extension)" Pack="true" PackagePath="Sdk\%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
1616
<None Update="SDK\AutoImport.props;SDK\*.targets" Pack="true" PackagePath="Sdk\%(Filename)%(Extension)" CopyToOutputDirectory="PreserveNewest" />
1717
<None Update="SDK\Sdk.in.props" Pack="true" PerformTextReplacement="True" PackagePath="Sdk\Sdk.props" CopyToOutputDirectory="PreserveNewest" />
18-
<None Update="SDK\Sdk.targets" Pack="true" PackagePath="Sdk\Sdk.targets" />
18+
<None Update="SDK\Sdk.in.targets" Pack="true" PerformTextReplacement="True" PackagePath="Sdk\Sdk.targets" />
1919
</ItemGroup>
2020

2121
</Project>

src/Aspire.Hosting.Sdk/SDK/Sdk.targets renamed to src/Aspire.Hosting.Sdk/SDK/Sdk.in.targets

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,17 @@
7171
<_AppHostVersion>%(_AppHostPackageReference.Version)</_AppHostVersion>
7272
</PropertyGroup>
7373

74-
<PropertyGroup Condition="'$(_AppHostVersion)' != ''">
75-
<__CurrentAppHostVersionMessage> You are using version $(_AppHostVersion).</__CurrentAppHostVersionMessage>
74+
<PropertyGroup Condition="'$(_AppHostVersion)' != '' and $([MSBuild]::VersionLessThan('$(_AppHostVersion)', '8.2.0'))">
75+
<!-- If we find the version to Aspire.Hosting.AppHost package but it is lower than 8.2.0, then we fall back
76+
to use the Dashboard and DCP packages that match the version of the installed workload for backwards compatibility.
77+
This results in the same behavior that we had before moving Dashboard and DCP out of the workload, since the version
78+
is again just matching to the one the workload has. -->
79+
<_AppHostVersion>@VERSION@</_AppHostVersion>
7680
</PropertyGroup>
7781

7882
<!-- At this point, we should have the version either by CPM or PackageReference, so we fail if not. -->
79-
<Error Condition="'$(_AppHostVersion)' == '' or $([MSBuild]::VersionLessThan('$(_AppHostVersion)', '8.2.0'))"
80-
Text="$(MSBuildProjectName) is a .NET Aspire AppHost project that needs a package reference to Aspire.Hosting.AppHost version 8.2.0 or above to work correctly.$(__CurrentAppHostVersionMessage)" />
83+
<Error Condition="'$(_AppHostVersion)' == ''"
84+
Text="$(MSBuildProjectName) is a .NET Aspire AppHost project that needs a package reference to Aspire.Hosting.AppHost version 8.2.0 or above to work correctly." />
8185

8286
<!-- Now that we have the version, we add the package references -->
8387
<ItemGroup>

tests/Aspire.Hosting.Testing.Tests/Directory.Build.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<!-- NOTE: These lines are only required because we are using P2P references, not NuGet. They will not exist in real apps. -->
66
<Import Project="..\..\src\Aspire.Hosting.AppHost\build\Aspire.Hosting.AppHost.targets" />
7-
<Import Project="..\..\src\Aspire.Hosting.Sdk\SDK\Sdk.targets" />
7+
<Import Project="..\..\src\Aspire.Hosting.Sdk\SDK\Sdk.in.targets" />
88

99
<PropertyGroup>
1010
<!-- This is for in-repo testing and required for Aspire.Hosting.AppHost targets loading correctly. On real projects, this comes from SDK.props in Aspire.Hosting.SDK. -->

tests/Aspire.Hosting.Tests/MSBuildTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ the Aspire.Hosting.SDK targets that will automatically add these references to p
8787
File.WriteAllText(Path.Combine(appHostDirectory, "Directory.Build.targets"), $"""
8888
<Project>
8989
<Import Project="{repoRoot}\src\Aspire.Hosting.AppHost\build\Aspire.Hosting.AppHost.targets" />
90-
<Import Project="{repoRoot}\src\Aspire.Hosting.Sdk\SDK\Sdk.targets" />
90+
<Import Project="{repoRoot}\src\Aspire.Hosting.Sdk\SDK\Sdk.in.targets" />
9191
</Project>
9292
""");
9393

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Xunit;
5+
using Xunit.Abstractions;
6+
using System.Text.RegularExpressions;
7+
8+
namespace Aspire.Workload.Tests;
9+
10+
public partial class AppHostTemplateTests : WorkloadTestsBase
11+
{
12+
public AppHostTemplateTests(ITestOutputHelper testOutput)
13+
: base(testOutput)
14+
{
15+
}
16+
17+
[Fact]
18+
public async Task EnsureProjectsReferencing8_1_0AppHostWithNewerWorkloadCanBuild()
19+
{
20+
string projectId = "aspire-can-reference-8.1.0";
21+
await using var project = await AspireProject.CreateNewTemplateProjectAsync(
22+
projectId,
23+
"aspire-apphost",
24+
_testOutput,
25+
BuildEnvironment.ForDefaultFramework,
26+
string.Empty,
27+
false);
28+
29+
var projectPath = Path.Combine(project.RootDir, $"{projectId}.csproj");
30+
31+
// Replace the reference to Aspire.Hosting.AppHost with version 8.1.0
32+
var newContents = AppHostPackageReferenceRegex().Replace(File.ReadAllText(projectPath), @"$1""8.1.0""");
33+
34+
File.WriteAllText(projectPath, newContents);
35+
36+
// Ensure project builds successfully
37+
await project.BuildAsync(workingDirectory: project.RootDir);
38+
}
39+
40+
[GeneratedRegex(@"(PackageReference\s.*""Aspire\.Hosting\.AppHost.*Version=)""[^""]+""")]
41+
private static partial Regex AppHostPackageReferenceRegex();
42+
}

tests/Shared/RepoTesting/Aspire.RepoTesting.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140

141141
<ImportGroup Condition="'$(RepoRoot)' != 'null' and '$(TestsRunningOutsideOfRepo)' != 'true' and '$(IsAspireHost)' == 'true'">
142142
<Import Project="$(RepoRoot)src\Aspire.Hosting.AppHost\build\Aspire.Hosting.AppHost.targets" Condition="Exists('$(RepoRoot)src\Aspire.Hosting.AppHost\build\Aspire.Hosting.AppHost.targets')" />
143-
<Import Project="$(RepoRoot)src\Aspire.Hosting.Sdk\SDK\Sdk.targets" Condition="Exists('$(RepoRoot)src\Aspire.Hosting.Sdk\SDK\Sdk.targets')" />
143+
<Import Project="$(RepoRoot)src\Aspire.Hosting.Sdk\SDK\Sdk.in.targets" Condition="Exists('$(RepoRoot)src\Aspire.Hosting.Sdk\SDK\Sdk.in.targets')" />
144144
</ImportGroup>
145145

146146
<PropertyGroup Condition="'$(RepoRoot)' != 'null' and '$(TestsRunningOutsideOfRepo)' != 'true' and '$(IsAspireHost)' == 'true'">

tests/Shared/WorkloadTesting/AspireProject.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,12 @@ public async Task StartAppHostAsync(string[]? extraArgs = default, Action<Proces
261261
_testOutput.WriteLine($"-- Ready to run tests --");
262262
}
263263

264-
public async Task BuildAsync(string[]? extraBuildArgs = default, CancellationToken token = default)
264+
public async Task BuildAsync(string[]? extraBuildArgs = default, CancellationToken token = default, string? workingDirectory = null)
265265
{
266+
workingDirectory ??= Path.Combine(RootDir, $"{Id}.AppHost");
267+
266268
using var restoreCmd = new DotNetCommand(_testOutput, buildEnv: _buildEnv, label: "restore")
267-
.WithWorkingDirectory(Path.Combine(RootDir, $"{Id}.AppHost"));
269+
.WithWorkingDirectory(workingDirectory);
268270
var res = await restoreCmd.ExecuteAsync($"restore \"-bl:{Path.Combine(LogPath!, $"{Id}-restore.binlog")}\" /p:TreatWarningsAsErrors=true");
269271
res.EnsureSuccessful();
270272

@@ -274,7 +276,7 @@ public async Task BuildAsync(string[]? extraBuildArgs = default, CancellationTok
274276
buildArgs += " " + string.Join(" ", extraBuildArgs);
275277
}
276278
using var buildCmd = new DotNetCommand(_testOutput, buildEnv: _buildEnv, label: "build")
277-
.WithWorkingDirectory(Path.Combine(RootDir, $"{Id}.AppHost"));
279+
.WithWorkingDirectory(workingDirectory);
278280
res = await buildCmd.ExecuteAsync(buildArgs);
279281
res.EnsureSuccessful();
280282
}

0 commit comments

Comments
 (0)