Skip to content

Commit 3966b79

Browse files
authored
make container app/entrypoint argument defaults only happen on the RID-specific builds, not the outer RID-less build (#47424)
1 parent 01b7a42 commit 3966b79

File tree

3 files changed

+35
-45
lines changed

3 files changed

+35
-45
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: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
<ContainerRuntimeIdentifier Condition="'$(ContainerRuntimeIdentifier)' == '' and '$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier)</ContainerRuntimeIdentifier>
4242
<ContainerRuntimeIdentifiers Condition="'$(BuildingInsideVisualStudio)' != 'true' and '$(ContainerRuntimeIdentifiers)' == '' and '$(IsRidAgnostic)' != 'true'">$(RuntimeIdentifiers)</ContainerRuntimeIdentifiers>
4343
<ContainerRuntimeIdentifier Condition="'$(ContainerRuntimeIdentifier)' == '' and '$(ContainerRuntimeIdentifiers)' == ''">linux-$(NETCoreSdkPortableRuntimeIdentifier.Split('-')[1])</ContainerRuntimeIdentifier>
44-
45-
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true</_ContainerIsUsingMicrosoftDefaultImages>
46-
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false</_ContainerIsUsingMicrosoftDefaultImages>
4744
</PropertyGroup>
4845

4946
<ItemGroup>
@@ -99,27 +96,6 @@
9996
<!-- Only default a tag name if no tag names at all are provided -->
10097
<ContainerImageTag Condition="'$(ContainerImageTag)' == '' and '$(ContainerImageTags)' == ''">latest</ContainerImageTag>
10198
<ContainerImageTag Condition="'$(AutoGenerateImageTag)' == 'true' and '$(ContainerImageTags)' == ''">$([System.DateTime]::UtcNow.ToString('yyyyMMddhhmmss'))</ContainerImageTag>
102-
103-
<!-- 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.
104-
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. -->
105-
<_ContainerIsTargetingWindows>false</_ContainerIsTargetingWindows>
106-
<_ContainerIsTargetingWindows Condition="$(ContainerRuntimeIdentifier.StartsWith('win'))">true</_ContainerIsTargetingWindows>
107-
108-
<!-- Set the WorkingDirectory depending on the RID -->
109-
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and !$(_ContainerIsTargetingWindows)">/app/</ContainerWorkingDirectory>
110-
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and $(_ContainerIsTargetingWindows)">C:\app\</ContainerWorkingDirectory>
111-
</PropertyGroup>
112-
113-
<ItemGroup Label="AppCommand Assignment" Condition="'$(ContainerAppCommandInstruction)' != 'None'">
114-
<!-- For self-contained, invoke the native executable as a single arg -->
115-
<ContainerAppCommand Condition="@(ContainerAppCommand->Count()) == 0 and $(_ContainerIsSelfContained)" Include="$(ContainerWorkingDirectory)$(AssemblyName)$(_NativeExecutableExtension)" />
116-
<!-- For non self-contained, invoke `dotnet` `app.dll` as separate args -->
117-
<ContainerAppCommand Condition="@(ContainerAppCommand->Count()) == 0 and !$(_ContainerIsSelfContained)" Include="dotnet;$(ContainerWorkingDirectory)$(TargetFileName)" />
118-
</ItemGroup>
119-
120-
<!-- 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 -->
121-
<PropertyGroup Label="ContainerUser Assignment" Condition="$(_ContainerIsUsingMicrosoftDefaultImages) and $(_ContainerIsTargetingNet8TFM) and '$(ContainerUser)' == ''">
122-
<ContainerUser Condition="$(_ContainerIsTargetingWindows)">ContainerUser</ContainerUser>
12399
</PropertyGroup>
124100

125101
<ParseContainerProperties FullyQualifiedBaseImageName="$(ContainerBaseImage)"
@@ -186,7 +162,7 @@
186162
</ItemGroup>
187163

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

185+
<!-- These args are relevant to container execution and are per-RID by nature. Therefore they're a direct dependency of the _PublishSingleContainer
186+
target and not computed at the outer layer. -->
187+
<Target Name="_ComputeContainerExecutionArgs">
188+
<PropertyGroup>
189+
<!-- 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.
190+
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. -->
191+
<_ContainerIsTargetingWindows>false</_ContainerIsTargetingWindows>
192+
<_ContainerIsTargetingWindows Condition="$(ContainerRuntimeIdentifier.StartsWith('win'))">true</_ContainerIsTargetingWindows>
193+
194+
<!-- Set the WorkingDirectory depending on the RID -->
195+
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and !$(_ContainerIsTargetingWindows)">/app/</ContainerWorkingDirectory>
196+
<ContainerWorkingDirectory Condition="'$(ContainerWorkingDirectory)' == '' and $(_ContainerIsTargetingWindows)">C:\app\</ContainerWorkingDirectory>
197+
198+
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true</_ContainerIsUsingMicrosoftDefaultImages>
199+
<_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false</_ContainerIsUsingMicrosoftDefaultImages>
200+
</PropertyGroup>
201+
202+
<!-- 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 -->
203+
<PropertyGroup Label="ContainerUser Assignment" Condition="$(_ContainerIsUsingMicrosoftDefaultImages) and $(_ContainerIsTargetingNet8TFM) and '$(ContainerUser)' == ''">
204+
<ContainerUser Condition="$(_ContainerIsTargetingWindows)">ContainerUser</ContainerUser>
205+
</PropertyGroup>
206+
207+
<ItemGroup Label="AppCommand Assignment" Condition="'$(ContainerAppCommandInstruction)' != 'None'">
208+
<!-- For self-contained, invoke the native executable as a single arg -->
209+
<ContainerAppCommand Condition="@(ContainerAppCommand->Count()) == 0 and $(_ContainerIsSelfContained)" Include="$(ContainerWorkingDirectory)$(AssemblyName)$(_NativeExecutableExtension)" />
210+
<!-- For non self-contained, invoke `dotnet` `app.dll` as separate args -->
211+
<ContainerAppCommand Condition="@(ContainerAppCommand->Count()) == 0 and !$(_ContainerIsSelfContained)" Include="dotnet;$(ContainerWorkingDirectory)$(TargetFileName)" />
212+
</ItemGroup>
213+
</Target>
214+
209215
<Target Name="_CheckContainersPackage" AfterTargets="Build">
210216
<PropertyGroup>
211217
<!-- facts to base on comparisons on -->
@@ -239,8 +245,7 @@
239245
Text="The $(_ContainersPackageIdentity) NuGet package is explicitly referenced but the current SDK can natively publish the project as a container. Consider removing the package reference to $(_ContainersPackageIdentity) because it is no longer needed." />
240246
</Target>
241247

242-
<Target Name="_PublishSingleContainer"
243-
Returns="@(GeneratedContainer)">
248+
<Target Name="_PublishSingleContainer" DependsOnTargets="_ComputeContainerExecutionArgs" Returns="@(GeneratedContainer)">
244249
<PropertyGroup Condition="'$(DOTNET_HOST_PATH)' == ''">
245250
<DotNetHostDirectory>$(NetCoreRoot)</DotNetHostDirectory>
246251
<DotNetHostFileName>dotnet</DotNetHostFileName>
@@ -325,17 +330,9 @@
325330
ContainerRegistry=$(ContainerRegistry);
326331
_ContainerImageTags=@(ContainerImageTags, ';');
327332
ContainerRepository=$(ContainerRepository);
328-
ContainerWorkingDirectory=$(ContainerWorkingDirectory);
329-
_ContainerEntrypoint=@(ContainerEntrypoint, ';');
330-
_ContainerEntrypointArgs=@(ContainerEntrypointArgs, ';');
331-
_ContainerAppCommand=@(ContainerAppCommand, ';');
332-
_ContainerAppCommandArgs=@(ContainerAppCommandArgs, ';');
333-
ContainerAppCommandInstruction=$(ContainerAppCommandInstruction);
334-
_ContainerDefaultArgs=@(ContainerDefaultArgs, ';');
335333
_ContainerLabel=@(ContainerLabel->'%(Identity):%(Value)');
336334
_ContainerPort=@(ContainerPort->'%(Identity):%(Type)');
337335
_ContainerEnvironmentVariables=@(ContainerEnvironmentVariable->'%(Identity):%(Value)');
338-
ContainerUser=$(ContainerUser);
339336
ContainerGenerateLabels=$(ContainerGenerateLabels);
340337
ContainerGenerateLabelsImageBaseDigest=$(ContainerGenerateLabelsImageBaseDigest);
341338
_SkipContainerPublishing=$(_SkipContainerPublishing)
@@ -370,11 +367,6 @@
370367
<ItemGroup>
371368
<OriginalImageTags Include="$(_ContainerImageTags)"/>
372369
<ContainerImageTags Include="@(OriginalImageTags->'%(Identity)-$(ContainerRuntimeIdentifier)')" />
373-
<ContainerEntrypoint Include="$(_ContainerEntrypoint)"/>
374-
<ContainerEntrypointArgs Include="$(_ContainerEntrypointArgs)"/>
375-
<ContainerAppCommand Include="$(_ContainerAppCommand)"/>
376-
<ContainerAppCommandArgs Include="$(_ContainerAppCommandArgs)"/>
377-
<ContainerDefaultArgs Include="$(_ContainerDefaultArgs)"/>
378370

379371
<_ParsedContainerLabel
380372
Condition="'$(_ContainerLabel)' != ':'"
@@ -430,5 +422,4 @@
430422

431423
<Error Condition="'$(_IsMultiTFMBuild)' == 'true'" Code="CONTAINERS0666" Text="Containers cannot be published for multiple TargetFrameworks at this time. Please specify a TargetFramework." />
432424
</Target>
433-
434425
</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)