Skip to content

Commit 2f256ad

Browse files
committed
make container app/entrypoint argument defaults only happen on the RID-specific builds, not the outer RID-less build
1 parent 4bd8e41 commit 2f256ad

File tree

3 files changed

+34
-43
lines changed

3 files changed

+34
-43
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public static class Properties
2727

2828
public static readonly string ComputeContainerBaseImage = nameof(ComputeContainerBaseImage);
2929
public static readonly string ComputeContainerConfig = nameof(ComputeContainerConfig);
30+
public static readonly string _ComputeContainerExecutionArgs = nameof(_ComputeContainerExecutionArgs);
3031
public static readonly string AssemblyName = nameof(AssemblyName);
3132
public static readonly string ContainerBaseRegistry = nameof(ContainerBaseRegistry);
3233
public static readonly string ContainerBaseName = nameof(ContainerBaseName);

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

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
<ContainerRuntimeIdentifier Condition="'$(ContainerRuntimeIdentifier)' == '' and '$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier)</ContainerRuntimeIdentifier>
4040
<ContainerRuntimeIdentifiers Condition="'$(BuildingInsideVisualStudio)' != 'true' and '$(ContainerRuntimeIdentifiers)' == '' and '$(IsRidAgnostic)' != 'true'">$(RuntimeIdentifiers)</ContainerRuntimeIdentifiers>
4141
<ContainerRuntimeIdentifier Condition="'$(ContainerRuntimeIdentifier)' == '' and '$(ContainerRuntimeIdentifiers)' == ''">linux-$(NETCoreSdkPortableRuntimeIdentifier.Split('-')[1])</ContainerRuntimeIdentifier>
42-
43-
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true</_ContainerIsUsingMicrosoftDefaultImages>
44-
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false</_ContainerIsUsingMicrosoftDefaultImages>
4542
</PropertyGroup>
4643

4744
<ItemGroup>
@@ -97,27 +94,6 @@
9794
<!-- Only default a tag name if no tag names at all are provided -->
9895
<ContainerImageTag Condition="'$(ContainerImageTag)' == '' and '$(ContainerImageTags)' == ''">latest</ContainerImageTag>
9996
<ContainerImageTag Condition="'$(AutoGenerateImageTag)' == 'true' and '$(ContainerImageTags)' == ''">$([System.DateTime]::UtcNow.ToString('yyyyMMddhhmmss'))</ContainerImageTag>
100-
101-
<!-- The Container RID should default to the RID used for the entire build (to ensure things run on the platform they are built for), but the user knows best and so should be able to set it explicitly.
102-
For builds that have a RID, we default to that RID. Otherwise, we default to the Linux RID matching the architecture of the currently-executing SDK. -->
103-
<_ContainerIsTargetingWindows>false</_ContainerIsTargetingWindows>
104-
<_ContainerIsTargetingWindows Condition="$(ContainerRuntimeIdentifier.StartsWith('win'))">true</_ContainerIsTargetingWindows>
105-
106-
<!-- Set the WorkingDirectory depending on the RID -->
107-
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and !$(_ContainerIsTargetingWindows)">/app/</ContainerWorkingDirectory>
108-
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and $(_ContainerIsTargetingWindows)">C:\app\</ContainerWorkingDirectory>
109-
</PropertyGroup>
110-
111-
<ItemGroup Label="AppCommand Assignment" Condition="'$(ContainerAppCommandInstruction)' != 'None'">
112-
<!-- For self-contained, invoke the native executable as a single arg -->
113-
<ContainerAppCommand Condition="@(ContainerAppCommand->Count()) == 0 and $(_ContainerIsSelfContained)" Include="$(ContainerWorkingDirectory)$(AssemblyName)$(_NativeExecutableExtension)" />
114-
<!-- For non self-contained, invoke `dotnet` `app.dll` as separate args -->
115-
<ContainerAppCommand Condition="@(ContainerAppCommand->Count()) == 0 and !$(_ContainerIsSelfContained)" Include="dotnet;$(ContainerWorkingDirectory)$(TargetFileName)" />
116-
</ItemGroup>
117-
118-
<!-- We only set a default user when the base image is Microsoft-authored, and we're targeting a version of those images that supports a nonroot user -->
119-
<PropertyGroup Label="ContainerUser Assignment" Condition="$(_ContainerIsUsingMicrosoftDefaultImages) and $(_ContainerIsTargetingNet8TFM) and '$(ContainerUser)' == ''">
120-
<ContainerUser Condition="$(_ContainerIsTargetingWindows)">ContainerUser</ContainerUser>
12197
</PropertyGroup>
12298

12399
<ParseContainerProperties FullyQualifiedBaseImageName="$(ContainerBaseImage)"
@@ -184,7 +160,7 @@
184160
</ItemGroup>
185161

186162
<!-- These sourcelink-derived properties are only allowed to flow to generated artifacts if `PublishRepositoryUrl` is set as a user signal for opt-in.
187-
In addition, the 'nice' property names are currently set by NuGet Pack targets and so we have to use the private/generic names here. -->
163+
In addition, the 'nice' property names are currently set by NuGet Pack targets and so we have to use the private/generic names here. -->
188164
<PropertyGroup Label="Source control label assignment" Condition="'$(ContainerGenerateLabels)' == 'true' and '$(PublishRepositoryUrl)' == 'true'">
189165
<!-- Sourcelink gives us the .git suffix, but scanning tools aren't looking for that so we trim it off here. -->
190166
<_TrimmedRepositoryUrl Condition="'$(RepositoryType)' == 'git' and '$(PrivateRepositoryUrl)' != '' and $(PrivateRepositoryUrl.EndsWith('.git'))">$(PrivateRepositoryUrl.Substring(0, $(PrivateRepositoryUrl.LastIndexOf('.git'))))</_TrimmedRepositoryUrl>
@@ -204,6 +180,36 @@
204180
</PublishContainerDependsOn>
205181
</PropertyGroup>
206182

183+
<!-- These args are relevant to container execution and are per-RID by nature. Therefore they're a direct dependency of the _PublishSingleContainer
184+
target and not computed at the outer layer. -->
185+
<Target Name="_ComputeContainerExecutionArgs">
186+
<PropertyGroup>
187+
<!-- The Container RID should default to the RID used for the entire build (to ensure things run on the platform they are built for), but the user knows best and so should be able to set it explicitly.
188+
For builds that have a RID, we default to that RID. Otherwise, we default to the Linux RID matching the architecture of the currently-executing SDK. -->
189+
<_ContainerIsTargetingWindows>false</_ContainerIsTargetingWindows>
190+
<_ContainerIsTargetingWindows Condition="$(ContainerRuntimeIdentifier.StartsWith('win'))">true</_ContainerIsTargetingWindows>
191+
192+
<!-- Set the WorkingDirectory depending on the RID -->
193+
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and !$(_ContainerIsTargetingWindows)">/app/</ContainerWorkingDirectory>
194+
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and $(_ContainerIsTargetingWindows)">C:\app\</ContainerWorkingDirectory>
195+
196+
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true</_ContainerIsUsingMicrosoftDefaultImages>
197+
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false</_ContainerIsUsingMicrosoftDefaultImages>
198+
</PropertyGroup>
199+
200+
<!-- We only set a default user when the base image is Microsoft-authored, and we're targeting a version of those images that supports a nonroot user -->
201+
<PropertyGroup Label="ContainerUser Assignment" Condition="$(_ContainerIsUsingMicrosoftDefaultImages) and $(_ContainerIsTargetingNet8TFM) and '$(ContainerUser)' == ''">
202+
<ContainerUser Condition="$(_ContainerIsTargetingWindows)">ContainerUser</ContainerUser>
203+
</PropertyGroup>
204+
205+
<ItemGroup Label="AppCommand Assignment" Condition="'$(ContainerAppCommandInstruction)' != 'None'">
206+
<!-- For self-contained, invoke the native executable as a single arg -->
207+
<ContainerAppCommand Condition="@(ContainerAppCommand->Count()) == 0 and $(_ContainerIsSelfContained)" Include="$(ContainerWorkingDirectory)$(AssemblyName)$(_NativeExecutableExtension)" />
208+
<!-- For non self-contained, invoke `dotnet` `app.dll` as separate args -->
209+
<ContainerAppCommand Condition="@(ContainerAppCommand->Count()) == 0 and !$(_ContainerIsSelfContained)" Include="dotnet;$(ContainerWorkingDirectory)$(TargetFileName)" />
210+
</ItemGroup>
211+
</Target>
212+
207213
<Target Name="_CheckContainersPackage" AfterTargets="Build">
208214
<PropertyGroup>
209215
<!-- facts to base on comparisons on -->
@@ -317,17 +323,9 @@
317323
ContainerRegistry=$(ContainerRegistry);
318324
_ContainerImageTags=@(ContainerImageTags, ';');
319325
ContainerRepository=$(ContainerRepository);
320-
ContainerWorkingDirectory=$(ContainerWorkingDirectory);
321-
_ContainerEntrypoint=@(ContainerEntrypoint, ';');
322-
_ContainerEntrypointArgs=@(ContainerEntrypointArgs, ';');
323-
_ContainerAppCommand=@(ContainerAppCommand, ';');
324-
_ContainerAppCommandArgs=@(ContainerAppCommandArgs, ';');
325-
ContainerAppCommandInstruction=$(ContainerAppCommandInstruction);
326-
_ContainerDefaultArgs=@(ContainerDefaultArgs, ';');
327326
_ContainerLabel=@(ContainerLabel->'%(Identity):%(Value)');
328327
_ContainerPort=@(ContainerPort->'%(Identity):%(Type)');
329328
_ContainerEnvironmentVariables=@(ContainerEnvironmentVariable->'%(Identity):%(Value)');
330-
ContainerUser=$(ContainerUser);
331329
ContainerGenerateLabels=$(ContainerGenerateLabels);
332330
ContainerGenerateLabelsImageBaseDigest=$(ContainerGenerateLabelsImageBaseDigest)
333331
"/>
@@ -359,11 +357,6 @@
359357
<ItemGroup>
360358
<OriginalImageTags Include="$(_ContainerImageTags)"/>
361359
<ContainerImageTags Include="@(OriginalImageTags->'%(Identity)-$(ContainerRuntimeIdentifier)')" />
362-
<ContainerEntrypoint Include="$(_ContainerEntrypoint)"/>
363-
<ContainerEntrypointArgs Include="$(_ContainerEntrypointArgs)"/>
364-
<ContainerAppCommand Include="$(_ContainerAppCommand)"/>
365-
<ContainerAppCommandArgs Include="$(_ContainerAppCommandArgs)"/>
366-
<ContainerDefaultArgs Include="$(_ContainerDefaultArgs)"/>
367360

368361
<_ParsedContainerLabel
369362
Condition="'$(_ContainerLabel)' != ':'"
@@ -417,5 +410,4 @@
417410

418411
<Error Condition="'$(_IsMultiTFMBuild)' == 'true'" Code="CONTAINERS0666" Text="Containers cannot be published for multiple TargetFrameworks at this time. Please specify a TargetFramework." />
419412
</Target>
420-
421413
</Project>

test/Microsoft.NET.Build.Containers.IntegrationTests/TargetsTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public void CanDeferContainerAppCommand(
2626
}, projectName: $"{nameof(CanDeferContainerAppCommand)}_{prop}_{value}_{string.Join("_", expectedAppCommandArgs)}");
2727
using var _ = d;
2828
var instance = project.CreateProjectInstance(ProjectInstanceSettings.None);
29-
//Assert.True(instance.Build([ ComputeContainerConfig ], []));
30-
instance.Build([ ComputeContainerConfig ], []);
29+
instance.Build([ _ComputeContainerExecutionArgs ], []);
3130
var computedAppCommand = instance.GetItems(ContainerAppCommand).Select(i => i.EvaluatedInclude);
3231

3332
// The test was not testing anything previously, as the list returned was zero length,
@@ -36,7 +35,6 @@ public void CanDeferContainerAppCommand(
3635
computedAppCommand.Should().BeEquivalentTo(expectedAppCommandArgs);
3736
}
3837

39-
4038
public static TheoryData<string, string, bool, string[]> ContainerAppCommands()
4139
{
4240
char s = Path.DirectorySeparatorChar;
@@ -316,7 +314,7 @@ public void CanComputeContainerUser(string tfm, string rid, string? expectedUser
316314
}, projectName: $"{nameof(CanComputeContainerUser)}_{tfm}_{rid}_{expectedUser}");
317315
using var _ = d;
318316
var instance = project.CreateProjectInstance(ProjectInstanceSettings.None);
319-
instance.Build(new[] { ComputeContainerConfig }, new[] { logger }, null, out var outputs).Should().BeTrue(String.Join(Environment.NewLine, logger.Errors));
317+
instance.Build(new[] { _ComputeContainerExecutionArgs }, new[] { logger }, null, out var outputs).Should().BeTrue(String.Join(Environment.NewLine, logger.Errors));
320318
var computedTag = instance.GetProperty("ContainerUser")?.EvaluatedValue;
321319
computedTag.Should().Be(expectedUser);
322320
}

0 commit comments

Comments
 (0)