Skip to content

Commit bd03e2f

Browse files
committed
trimmed applications can use -extras images too
1 parent 9eda7b3 commit bd03e2f

File tree

7 files changed

+38
-8
lines changed

7 files changed

+38
-8
lines changed

src/Containers/Microsoft.NET.Build.Containers/KnownStrings.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static class Properties
3636
public static readonly string ContainerRuntimeIdentifier = nameof(ContainerRuntimeIdentifier);
3737
public static readonly string RuntimeIdentifier = nameof(RuntimeIdentifier);
3838
public static readonly string PublishAot = nameof(PublishAot);
39+
public static readonly string PublishTrimmed = nameof(PublishTrimmed);
3940
public static readonly string PublishSelfContained = nameof(PublishSelfContained);
4041
public static readonly string InvariantGlobalization = nameof(InvariantGlobalization);
4142
}
@@ -52,7 +53,7 @@ public static class ErrorCodes
5253
public static readonly string CONTAINER1011 = nameof(CONTAINER1011);
5354
public static readonly string CONTAINER1012 = nameof(CONTAINER1012);
5455
public static readonly string CONTAINER1013 = nameof(CONTAINER1013);
55-
56+
5657
public static readonly string CONTAINER2005 = nameof(CONTAINER2005);
5758
public static readonly string CONTAINER2007 = nameof(CONTAINER2007);
5859
public static readonly string CONTAINER2008 = nameof(CONTAINER2008);

src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net472/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.FrameworkRefer
110110
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.FrameworkReferences.set -> void
111111
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsAotPublished.get -> bool
112112
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsAotPublished.set -> void
113+
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsTrimmed.get -> bool
114+
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsTrimmed.set -> void
113115
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsSelfContained.get -> bool
114116
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsSelfContained.set -> void
115117
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.TargetRuntimeIdentifier.get -> string!

src/Containers/Microsoft.NET.Build.Containers/PublicAPI/net8.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.FrameworkRefer
77
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.FrameworkReferences.set -> void
88
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsAotPublished.get -> bool
99
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsAotPublished.set -> void
10+
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsTrimmed.get -> bool
11+
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsTrimmed.set -> void
1012
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsSelfContained.get -> bool
1113
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.IsSelfContained.set -> void
1214
Microsoft.NET.Build.Containers.Tasks.ComputeDotnetBaseImageAndTag.TargetRuntimeIdentifier.get -> string!

src/Containers/Microsoft.NET.Build.Containers/Tasks/ComputeDotnetBaseImageAndTag.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public sealed class ComputeDotnetBaseImageAndTag : Microsoft.Build.Utilities.Tas
4646
[Required]
4747
public string TargetRuntimeIdentifier { get; set; }
4848

49-
5049
/// <summary>
5150
/// If a project is self-contained then it includes a runtime, and so the runtime-deps image should be used.
5251
/// </summary>
@@ -57,6 +56,8 @@ public sealed class ComputeDotnetBaseImageAndTag : Microsoft.Build.Utilities.Tas
5756
/// </summary>
5857
public bool IsAotPublished { get; set; }
5958

59+
public bool IsTrimmed { get; set; }
60+
6061
/// <summary>
6162
/// If the project is AOT'd the aot image variant doesn't contain ICU and TZData, so we use this flag to see if we need to use the `-extra` variant that does contain those packages.
6263
/// </summary>
@@ -141,7 +142,7 @@ private bool ComputeRepositoryAndTag([NotNullWhen(true)] out string? repository,
141142
tag += IsMuslRid switch
142143
{
143144
true => "-alpine",
144-
false => "" // TODO: should we default here to chiseled iamges for < 8 apps?
145+
false => "" // TODO: should we default here to chiseled images for < 8 apps?
145146
};
146147
Log.LogMessage("Selected base image tag {0}", tag);
147148
return true;
@@ -153,16 +154,17 @@ private bool ComputeRepositoryAndTag([NotNullWhen(true)] out string? repository,
153154
{
154155
true => "-alpine",
155156
// default to chiseled for AOT, non-musl Apps
156-
false when IsAotPublished => "-jammy-chiseled", // TODO: should we default here to jammy-chiseled for non-musl RIDs?
157+
false when IsAotPublished || IsTrimmed => "-jammy-chiseled", // TODO: should we default here to jammy-chiseled for non-musl RIDs?
157158
// default to jammy for non-AOT, non-musl Apps
158159
false => ""
159160
};
160161

161162
// now choose the variant, if any - if globalization then -extra, else -aot
162-
tag += (IsAotPublished, UsesInvariantGlobalization) switch
163+
tag += (IsAotPublished, IsTrimmed, UsesInvariantGlobalization) switch
163164
{
164-
(true, false) => "-extra",
165-
(true, true) => "-aot",
165+
(true, _, false) => "-extra",
166+
(_, true, false) => "-extra",
167+
(true, _, true) => "-aot",
166168
_ => ""
167169
};
168170
Log.LogMessage("Selected base image tag {0}", tag);

src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
FrameworkReferences="@(FrameworkReference)"
5050
IsSelfContained="$(_ContainerIsSelfContained)"
5151
IsAotPublished="$(PublishAot)"
52+
IsTrimmed="$(PublishTrimmed)"
5253
UsesInvariantGlobalization="$(InvariantGlobalization)"
5354
TargetRuntimeIdentifier="$(ContainerRuntimeIdentifier)"
5455
ContainerFamily="$(ContainerFamily)">

src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/ProjectInitializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static (Project, CapturingLogger, IDisposable) InitProject(Dictionary<str
4141
props["TargetFileName"] = "foo.dll";
4242
props["AssemblyName"] = "foo";
4343
props["TargetFrameworkVersion"] = "v7.0";
44+
4445
props["TargetFrameworkIdentifier"] = ".NETCoreApp";
4546
props["TargetFramework"] = "net7.0";
4647
props["_NativeExecutableExtension"] = ".exe"; //TODO: windows/unix split here

src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/TargetsTests.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,27 @@ public void AOTAppsWithCulturesGetExtraImages(string rid, string expectedImage)
341341
computedBaseImageTag.Should().BeEquivalentTo(expectedImage);
342342
}
343343

344+
[InlineData("linux-musl-x64", "mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine-extra")]
345+
[InlineData("linux-x64", "mcr.microsoft.com/dotnet/runtime-deps:8.0-jammy-chiseled-extra")]
346+
[Theory]
347+
public void TrimmedAppsWithCulturesGetExtraImages(string rid, string expectedImage)
348+
{
349+
var (project, logger, d) = ProjectInitializer.InitProject(new()
350+
{
351+
["NetCoreSdkVersion"] = "8.0.100",
352+
["TargetFrameworkVersion"] = "v8.0",
353+
[KnownStrings.Properties.ContainerRuntimeIdentifier] = rid,
354+
[KnownStrings.Properties.PublishSelfContained] = true.ToString(),
355+
[KnownStrings.Properties.PublishTrimmed] = true.ToString(),
356+
[KnownStrings.Properties.InvariantGlobalization] = false.ToString()
357+
}, projectName: $"{nameof(TrimmedAppsWithCulturesGetExtraImages)}_{rid}_{expectedImage}");
358+
using var _ = d;
359+
var instance = project.CreateProjectInstance(global::Microsoft.Build.Execution.ProjectInstanceSettings.None);
360+
instance.Build(new[] { ComputeContainerBaseImage }, null, null, out var outputs).Should().BeTrue(String.Join(Environment.NewLine, logger.Errors));
361+
var computedBaseImageTag = instance.GetProperty(ContainerBaseImage)?.EvaluatedValue;
362+
computedBaseImageTag.Should().BeEquivalentTo(expectedImage);
363+
}
364+
344365
[InlineData("linux-musl-x64", "mcr.microsoft.com/dotnet/runtime-deps:7.0-alpine")]
345366
[InlineData("linux-x64", "mcr.microsoft.com/dotnet/runtime-deps:7.0")]
346367
[Theory]
@@ -398,7 +419,7 @@ public void AspNetFDDAppsGetAspNetBaseImage()
398419
}, projectName: $"{nameof(AspNetFDDAppsGetAspNetBaseImage)}");
399420
using var _ = d;
400421
var instance = project.CreateProjectInstance(global::Microsoft.Build.Execution.ProjectInstanceSettings.None);
401-
instance.Build(new[] { ComputeContainerBaseImage }, null, null, out var outputs).Should().BeTrue(String.Join(Environment.NewLine, logger.Errors));
422+
instance.Build(new[] { ComputeContainerBaseImage }, [logger], null, out var outputs).Should().BeTrue(String.Join(Environment.NewLine, logger.Errors));
402423
var computedBaseImageTag = instance.GetProperty(ContainerBaseImage)?.EvaluatedValue;
403424
computedBaseImageTag.Should().BeEquivalentTo(expectedImage);
404425
}

0 commit comments

Comments
 (0)