runtime: dotnet EXE application doesn't work with shebang on Linux

Description

When building a self-contained console application targeting .NET 7, the console application doesn’t work with shebang in Linux.

However, when building the same console application targeting .NET 6, the produced console application works fine with shebang.

This causes powershell v7.3.0 to stop working with shebang on Linux. See https://github.com/PowerShell/PowerShell/issues/18510

Reproduction Steps

Build a simple self-contained console application targeting .NET 7

The console application is very simple:

using System;

namespace MyApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < args.Length; i++)
            {
                Console.WriteLine($"<Arg {i}>: {args[i]}");
            }
        }
    }
}

The .csproj file:

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

</Project>

Build a self-contained console application from the C# code, targeting .NET 7:

dotnet publish -r linux-x64 -c Release --self-contained

Create the script file that uses shebang to run the console application

The content of the script file dotnet7:

#!/home/daxian/apps/1self/bin/Release/net7.0/linux-x64/publish/1self
$true

Then, run chmod to make the file an executable

chmod +x ./dotnet7

At last, run dotnet7 in bash:

./dotnet7

Expected behavior

The console application should run and print the arguments it receives.

When building against .NET 6, it works as expected, and here is the screenshot of the result:

image

Actual behavior

When building against .NET 7, it stops working with the following error message:

bash:35> ./dotnet7
The application to execute does not exist: '/home/daxian/temp/1self.dll'.

image

Regression?

Yes, it’s a regression from .NET 6

Known Workarounds

No known workaround so far.

Configuration

Which version of .NET is the code running on?

.NET 7 – SDK version 7.0.100,

What OS and version, and what distro if applicable?

WSLv2 - Ubuntu 18.04

What is the architecture (x64, x86, ARM, ARM64)?

I reproduced the issue on x64, but didn’t try other architectures.

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (14 by maintainers)

Commits related to this issue

Most upvoted comments

@daxian-dbw I got the backport approved for the 7.0.3 release.

It’s confirmed the original issue is fixed with PowerShell v7.3.3, which targets .NET 7.0.3. So now, we can close this issue.

Also, as far as I know, you cannot specify extra parameters when using #!/usr/bin/env pwsh, right?

@daxian-dbw, you can pass parameters when using env, I’ve posted solution in PowerShell/PowerShell#18510.

I believe we should backport it. I’ll create the porting PR. Reopening this issue until the ported fix is merged.