Skip to content

Published nuget packages are windows only #5966

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
}
}

Activity

  1. vors commented on Jan 20, 2018

    @vors
    CollaboratorAuthor

    relevant issue in Jupyter-PowerShell Jaykul/Jupyter-PowerShell#12

  2. vors commented on Jan 20, 2018

    @vors
    CollaboratorAuthor

    I'd really ask the team to consider creating compatible packages as a follow up for GA. I know that dotnet tool lacks support for it but I think for the GA release this really worth doing, even if that means doing it with lots of manual labor. That would enable so many cool x-plat scenarios.

  3. Jaykul commented on Jan 21, 2018

    @Jaykul
    Contributor

    Desperately need this for my project(s).

  4. iSazonov commented on Jan 21, 2018

    @iSazonov
    Collaborator

    We could use Windows Compatibility Pack. But it is preview and require .Net Core 2.1 preview. I don't know can we already use this? /cc Dongbo Wang (@daxian-dbw)

  5. vors commented on Jan 21, 2018

    @vors
    CollaboratorAuthor

    Hmm, I don't follow: how does Windows Compatibility Pack relate to that?

    image

    Things like Registry will not work on unix, all we need is include binaries that we already have in a proper runtime/platform folder in a NuGet package.

  6. iSazonov commented on Jan 22, 2018

    @iSazonov
    Collaborator

    WCP don't solve this automatically. We should review our code and wrap such code (#if UNIX ) with RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { ... }.
    To find the code we can use API Analyzer

    https://github.com/dotnet/designs/blob/master/accepted/compat-pack/compat-pack.md
    https://blogs.msdn.microsoft.com/dotnet/2017/11/16/announcing-the-windows-compatibility-pack-for-net-core/

  7. Jaykul commented on Jan 26, 2018

    @Jaykul
    Contributor

    I don't think we need to switch from platform-specific binaries to runtime checks. The platform-specific binaries are working ...

    But if there are platform-specific binaries, then they need to be shipped in the nuget packages, so we can build software that works.

    You know:

    runtimes/win-x86/native/...
    runtimes/win-x64/native/...
    runtimes/linux-x64/native/...
    runtimes/osx-x64/native/...
    ...
    
  8. added this to the milestone on Jan 26, 2018
  9. joeyaiello commented on Feb 20, 2018

    @joeyaiello
    Contributor

    Aditya Patwardhan (@adityapatwardhan) or is this the bug you fixed in the Myget packages?

  10. adityapatwardhan commented on Feb 21, 2018

    @adityapatwardhan
    Member

    This will be fixed when #6167 is merged and the new Nuget Packages are released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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