Skip to content

Published nuget packages are windows only #5966

@vors

Description

@vors

This is really a dupe of #3417 but I wanted to spell out the exact consequences of this problem and maybe encourage the team to create unix-compatible nuget packages for the GA 6.0.1 release.

The problem is in the #ifdef UNIX that we have.
The nuget packages published to myget only has windows binaries which are compiled without UNIX define (obviously).

That leads to runtime failures when trying to host powershell inside dotnet core application on linux and mac, such as https://github.com/Jaykul/Jupyter-PowerShell .

Steps to reproduce

Create a simple application that uses InitialSessionState

using System.Management.Automation.Runspaces;

namespace MygetDemo {
    public class ISSDemo {
        public static void Main(string[] args) {
            var iss = InitialSessionState.CreateDefault2();
        }
    }
}

With csproj file to build it

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>netcoreapp2.0</TargetFrameworks>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="6.0.0" />
  </ItemGroup>
</Project>

Run it with

dotnet restore
dotnet run --framework=netcoreapp2.0

Expected behavior

No error

Actual behavior

Microsoft (R) Build Engine version 15.6.12.27473 for .NET Core                                                              
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 38.48 ms for /Users/vors/dev/powershell-host-demo/Demo.csproj.

Unhandled Exception: System.TypeInitializationException: The type initializer for 'System.Management.Automation.Runspaces.InitialSessionState' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Management.Automation.Utils' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Microsoft.Win32.Registry' threw an exception. ---> System.PlatformNotSupportedException: Registry is not supported on this platform.
   at Microsoft.Win32.RegistryKey.OpenBaseKeyCore(RegistryHive hKey, RegistryView view)
   at Microsoft.Win32.Registry..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Platform.get_IsNanoServer()
   at System.Management.Automation.Platform.get_IsInbox()
   at System.Management.Automation.Utils..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.InitialSessionState..cctor()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2()
   at MygetDemo.ISSDemo.Main(String[] args) in /Users/vors/dev/powershell-host-demo/Program.cs:line 6

Environment data

> $PSVersionTable
Name                           Value                                                                                       
----                           -----                                                                                       
PSVersion                      6.0.0                                                                                       
PSEdition                      Core                                                                                        
GitCommitId                    v6.0.0                                                                                      
OS                             Darwin 17.3.0 Darwin Kernel Version 17.3.0: Thu Nov  9 18:09:22 PST 2017; root:xnu-4570.3...
Platform                       Unix                                                                                        
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                     
PSRemotingProtocolVersion      2.3                                                                                         
SerializationVersion           1.1.0.1                                                                                     
WSManStackVersion              3.0  

The failure comes from this code

public static bool IsNanoServer
{
get
{
#if UNIX
return false;
#else
if (_isNanoServer.HasValue) { return _isNanoServer.Value; }
_isNanoServer = false;
using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Server\ServerLevels"))
{
if (regKey != null)
{
object value = regKey.GetValue("NanoServer");
if (value != null && regKey.GetValueKind("NanoServer") == RegistryValueKind.DWord)
{
_isNanoServer = (int)value == 1;
}
}
}
return _isNanoServer.Value;
#endif
}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions