Repository navigation
Published nuget packages are windows only #5966
Description
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
PowerShell/src/System.Management.Automation/CoreCLR/CorePsPlatform.cs
Lines 68 to 93 in beffdcf
| 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
vors commentedon Jan 20, 2018
relevant issue in Jupyter-PowerShell Jaykul/Jupyter-PowerShell#12
vors commentedon Jan 20, 2018
I'd really ask the team to consider creating compatible packages as a follow up for GA. I know that
dotnettool 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.Jaykul commentedon Jan 21, 2018
Desperately need this for my project(s).
iSazonov commentedon Jan 21, 2018
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)
vors commentedon Jan 21, 2018
Hmm, I don't follow: how does Windows Compatibility Pack relate to that?
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.
iSazonov commentedon Jan 22, 2018
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/
Jaykul commentedon Jan 26, 2018
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:
joeyaiello commentedon Feb 20, 2018
Aditya Patwardhan (@adityapatwardhan) or is this the bug you fixed in the Myget packages?
adityapatwardhan commentedon Feb 21, 2018
This will be fixed when #6167 is merged and the new Nuget Packages are released.