Skip to content

Building a C# AppHost fails within a devcontainer, blocking xUnit v3 test projects #129040

@hansmbakker

Description

@hansmbakker

Description

When running a test project based on xUnit v3 with MTP v2 in a .NET devcontainer running in podman, it has an System.UnauthorizedAccessException which breaks dotnet run.

Reproduction Steps

Setup a workspace with a devcontainer and a test project:

Dev container:

{
  "name": "mydevcontainer",
  "image": "mcr.microsoft.com/devcontainers/dotnet:10.0",
  "features": {
    "ghcr.io/devcontainers/features/dotnet:2": {
      "version": "10.0"
    }
  },
  "postCreateCommand": "dotnet tool install -g Microsoft.OpenApi.Kiota && dotnet restore",
  "customizations": {
    "vscode": {
      "extensions": [
        "ms-dotnettools.csdevkit",
        "ms-dotnettools.csharp"
      ]
    }
  }
}

Create a test project like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
    <UseMicrosoftTestingPlatformRunner>true</UseMicrosoftTestingPlatformRunner>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.collector" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" />
    <PackageReference Include="NSubstitute" />
    <PackageReference Include="xunit.v3.mtp-v2" />
    <PackageReference Include="xunit.runner.visualstudio" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\src\projectundertest.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Using Include="Xunit" />
  </ItemGroup>

</Project>

Directory.Packages.props:

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>

  <!-- Test project -->
  <ItemGroup>
    <PackageVersion Include="coverlet.collector" Version="6.0.4" />
    <PackageVersion Include="NSubstitute" Version="5.3.0" />
    <PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
    <PackageVersion Include="xunit.v3.mtp-v2" Version="3.2.2" />
    <PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />
  </ItemGroup>
</Project>

Expected behavior

dotnet run in the test project folder runs properly when running

  • directly in Windows AND
  • in the devcontainer

Actual behavior

  • dotnet run in the test project folder runs properly when running directly in Windows
  • dotnet run in the test project folder fails when running in the devcontainer:
$ dotnet run
/usr/share/dotnet/sdk/10.0.300/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(850,5): error MSB4018: 
  The "CreateAppHost" task failed unexpectedly.
  System.UnauthorizedAccessException: Access to the path '/workspaces/mytestproject/obj/Debug/net10.0/apphost' is denied.
   ---> System.IO.IOException: Operation not permitted
     --- End of inner exception stack trace ---
     at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirError)
     at System.IO.FileStatus.SetUnixFileMode(SafeFileHandle handle, String path, UnixFileMode mode)
     at System.IO.File.SetUnixFileMode(String path, UnixFileMode mode)
     at Microsoft.NET.HostModel.AppHost.HostWriter.CreateAppHost(String appHostSourceFilePath, String appHostDestinationFilePath, String appBinaryFilePath, Boolean windowsGraphicalUserInterface,
   String assemblyToCopyResourcesFrom, Boolean enableMacOSCodeSign, Boolean disableCetCompat, DotNetSearchOptions dotNetSearchOptions)
     at Microsoft.NET.Build.Tasks.CreateAppHost.ExecuteCore()
     at Microsoft.NET.Build.Tasks.TaskBase.Execute()
     at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(TaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutio
  nMode howToExecuteTask)

Regression?

No response

Known Workarounds

Run sudo dotnet run

Configuration

  • podman v5.8.2
  • .NET 10
  • DevContainer based on mcr.microsoft.com/devcontainers/dotnet:10.0
  • x64 architecture
  • xUnit v3 with MTP v2
    <PackageVersion Include="coverlet.collector" Version="6.0.4" />
    <PackageVersion Include="NSubstitute" Version="5.3.0" />
    <PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
    <PackageVersion Include="xunit.v3.mtp-v2" Version="3.2.2" />
    <PackageVersion Include="xunit.runner.visualstudio" Version="3.1.5" />

Other information

This might be related with podman being rootless. Adding the following json to devcontainer.json helps

  "runArgs": [
    "--userns=keep-id"
  ],

<UseAppHost>false</UseAppHost> is not an option:

xUnit.net v3 test projects must build an app host (set project property 'true'). If this is not a test project, reference xunit.v3.extensibility.core instead.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions