question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConsoleApp hosting PowerShell fails when published as single file

See original GitHub issue

Probably the wrong repo to report this and most likely I did something wrong. I get an exception when I try to create a runspace in a console application published as a single file. Runs fine when published normally.

Steps to reproduce

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.1.0-preview.6" />
  </ItemGroup>
</Project>
using System;
using System.Management.Automation.Runspaces;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var rs = RunspaceFactory.CreateRunspace();
            Console.WriteLine("Hello World!");
        }
    }
}

dotnet publish -r win-x64 --self-contained false /p:PublishSingleFile=true

Expected behavior

Hello World

Actual behavior

Unhandled exception. System.TypeInitializationException: The type initializer for 'System.Management.Automation.PSVersionInfo' threw an exception.
 ---> System.ArgumentException: The path is empty. (Parameter 'path')
   at System.IO.Path.GetFullPath(String path)
   at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
   at System.Management.Automation.PSVersionInfo..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.PSVersionInfo.get_PSVersion()
   at Microsoft.PowerShell.DefaultHost..ctor(CultureInfo currentCulture, CultureInfo currentUICulture)
   at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
   at ConsoleApp1.Program.Main(String[] args)

Environment data

dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100-preview.8.20417.9
 Commit:    fc62663a35

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19041
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-preview.8.20417.9\

Host (useful for support):
  Version: 5.0.0-preview.8.20407.11
  Commit:  bf456654f9

.NET SDKs installed:
  5.0.100-preview.8.20417.9 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.NETCore.App 5.0.0-preview.8.20407.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 5.0.0-preview.8.20411.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:11
  • Comments:33 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
iSazonovcommented, Sep 1, 2020

@daxian-dbw My question is does PowerShell team want to support single file build scenario? If yes we can start to investigate problems in implementation of the scenario.

5reactions
iSazonovcommented, Aug 29, 2020

I tried to compile PowerShell to single file a month ago with the same result. I guess the exception comes from follow line:

string assemblyPath = typeof(PSVersionInfo).Assembly.Location;

I found 29 such patterns in PowerShell code. And I don’t know whether the exception is a .Net bug or we should use another pattern/workaround.

I think the scenario (single file) has a right to exist and we could support it in next milestone.

It is a question for PowerShell Committee.

/cc @SteveL-MSFT @daxian-dbw

Read more comments on GitHub >

github_iconTop Results From Across the Web

PowerShell running local from published Single Exe fails ...
PowerShell running local from published Single Exe fails (without IncludeAllContentForSelfExtract) Exceptions when running a single exe file ...
Read more >
Support Single File Diagnostics #45382 - dotnet/runtime
This issue is tracking the work necessary to support single-file diagnostics. ... ConsoleApp hosting PowerShell fails when published as single file ...
Read more >
How to publish .NET console app to single independent . ...
NET console app into a single, independent .exe that contains all the dependencies ... PublishSingleFile - Enables single-file publishing.
Read more >
about Redirection - PowerShell
Explains how to redirect output from PowerShell to text files. Long description. By default, PowerShell sends output to the PowerShell host.
Read more >
1073741502" Stop error when many PowerShell scripts ...
This issue is caused by an error in the Microsoft.powershell.consolehost.dll file. When a Windows PowerShell script calls the Console.Write method, or when you ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found