Skip to content

System.PlatformNotSupportedException: System.IO.Ports is currently only supported on Windows. on Archlinux #79196

Closed

Description

@Foxlider

Description

When using IO Ports on ArchLinux System and dotnet 7.0 or 6.0 I get an exception System.PlatformNotSupportedException: System.IO.Ports is currently only supported on Windows.

SerialPort.GetPortNames() works but not SerialPort()

Reproduction Steps

  1. Make new console application on dotnet 6 or 7
  2. Add the code snippet below
  3. Run on different Linux systems

Expected behavior

Access to UART Serial ports

Actual behavior

Exception thrown

Regression?

No response

Known Workarounds

None so far

Configuration

Dotnet Version

╰─ dotnet --list-sdks    
6.0.111 [/usr/share/dotnet/sdk]
7.0.100 [/usr/share/dotnet/sdk]

╰─ dotnet --list-runtimes 
Microsoft.AspNetCore.App 6.0.11 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.0 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.11 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

OS Version, kernel

╰─ lsb_release -a
LSB Version:    n/a
Distributor ID: EndeavourOS
Description:    EndeavourOS Linux
Release:        rolling
Codename:       rolling


╰─ uname -a
Linux keelah-dellG15 6.0.9-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 16 Nov 2022 17:01:17 +0000 x86_64 GNU/Linux

CPU Architecture :

╰─ uname -m
x86_64

From what I've seen on the forums, it works on certain linux distros but not on others such as Manjaro and EndeavourOS (both running archlinux) and might be the same on other more obscure linux distros

Other information

A few lines of code I used on my console program :

//DEFAULT port sur /dev/ttyUSB0
string SERIALPORT = "/dev/ttyUSB0";

//Line below works
List<string> ports = SerialPort.GetPortNames().ToList();
SERIALPORT = ports.Last();

//Crash on line below
SerialPort _serialPort = new SerialPort(SERIALPORT, BAUDRATE, Parity.None, 8, StopBits.One);

_serialPort.ReadTimeout = -1;       // |Pas de timeout
_serialPort.WriteTimeout = -1;      // |
_serialPort.Handshake = Handshake.None;
_serialPort.DataReceived += _serialPort_DataReceived;

Activity

  1. ghost added
    untriaged
    on Dec 3, 2022
  2. Foxlider commented on Dec 6, 2022

    @Foxlider
    Author

    I had TargetFramework on <TargetFramework>net6.0</TargetFramework> and tried net7.0 too.
    Also had <PackageReference Include="System.IO.Ports" Version="7.0.0" />

    I'll check your code on my Arch..

  3. krwq commented on Jan 13, 2023

    @krwq
    Member

    What RID does your dotnet --info show (second section of the output under Runtime Environment)?

  4. Foxlider commented on Jan 15, 2023

    @Foxlider
    Author

    RID is arch-x64

    Still have the same error on the GetPortsName though

  5. krwq commented on Jan 17, 2023

    @krwq
    Member

    @Foxlider if you have some time to experiment...
    I'm curious if you published for linux-x64 (dotnet publish -r linux-x64 and then run binary produced under bin/.../publish) - there is a chance it will just work. Or alternatively you could copy & paste System.IO.Ports binaries from linux-x64 (there should be .dll and .so file) to your other build (replace existing ones)

    We seem to not produce bits for arch-64 currently but I'm curious if we could just make arch use linux bits.

  6. Foxlider commented on Jan 17, 2023

    @Foxlider
    Author

    So it seems publishing with linux-x64 works and so is dotnet run -r linux-x64
    I am not sure why arch-x64 is considered as a different system as linux since it still uses the linux kernel as any other linux system. Or if there is a difference, I am unsure what it is

  7. krwq commented on Jan 19, 2023

    @krwq
    Member

    @Foxlider I'm guessing some of the native components in different areas need some arch specific binaries. Having said that I think arch should extend linux RID and IMO we should be picking up linux-x64 package if arch-x64 is not present. I'll see if I can find someone who knows what needs to be changed to fix this.

  8. krwq commented on Jan 19, 2023

    @krwq
    Member
  9. added
    packaging
    on Jan 19, 2023
  10. added this to the 8.0.0 milestone on Jan 19, 2023
  11. ghost removed
    untriaged
    on Jan 19, 2023
  12. ViktorHofer commented on Jan 19, 2023

    @ViktorHofer
    Member

    I followed @am11 sample from above and didn't get a PlatformNotSupportedException when performing a dotnet run but the intended output: "Done" on my Arch distro.

    @Foxlider can you please share a minimal but complete repro?

  13. Foxlider commented on Jan 19, 2023

    @Foxlider
    Author

    Let me know if you need anything.

    I tried a minimal code with a dotnet new console and simply adding the SerialPort.GetPortNames() and still had the issue

  14. 13 remaining items

  15. am11 commented on Feb 2, 2023

    @am11
    Member

    @tmds, the fallback RID is in the graph of portable build and missing in non-portable build.

    $ COREHOST_TRACE=1 COREHOST_TRACE_VERBOSITY=4 dotnet run -v:diag 2>&1 |\
        curl -F"sprunge=<-" http://sprunge.us
    

    non-portable http://sprunge.us/TypCOf
    portable http://sprunge.us/QlIRXQ

    (search for The rid fallback graph is)


    /etc/os-release contract is bad and should be avoided because it can be avoided:

    This reliance on/etc/os-release is a weakness in our design and I'm very happy that NativeAOT hosting layer does not use all that (hope it stays this way). The design should be so that we shouldn't be parsing /etc/os-release file in the first place for anything (more serious than dotnet --info); let alone failing hard if /etc/os-release does not have some expected values on the user system. This file is not part of or owned by dotnet, so we should not monitor and object to random stuff on user system and fail to run because of that.

  16. tmds commented on Feb 2, 2023

    @tmds
    Member

    I was just also looking into this.

    In both cases you see:

    HostRID is arch.TEMPLATE_VERSION_ID-x64
    Falling back to base HostRID: linux-x64
    

    The difference is in the fallback graph:

    linux-x64 => [
    linux, 
    unix-x64, 
    unix, 
    any, 
    base, 
    ]
    

    This is missing for the non-portable build, and that is a source-build issue.

    The information comes from Microsoft.NETCore.App.deps.json runtimes section.
    For the non-portable build, grep -F '"linux-x64": [' Microsoft.NETCore.App.deps.json comes up empty.

    We need to verify this section is present.

    #78563 is a similar issue, also due to a broken runtimes section. It is fixed on main, 7.0 and maybe 6.0.

  17. tmds commented on Feb 2, 2023

    @tmds
    Member

    This is missing for the non-portable build

    Without it, the non-portable build doesn't know the parents of linux-x64. It needs to know unix is a parent, because that is what System.IO.Ports provides.

  18. am11 commented on Feb 4, 2023

    @am11
    Member

    Found a (semi-) good state of VMR from their release/8.0.1xx-preview1 branch: https://github.com/dotnet/dotnet/tree/a2aca774110aadd68f5e2ad7b99293ab28d5632b (which requires dotnet/source-build-assets#513). I can confirm that non-portable build is fixed.

    $ git clone https://github.com/dotnet/dotnet vmr
    $ git -C vmr checkout a2aca774110aadd68f5e2ad7b99293ab28d5632b
    
    # apply https://github.com/dotnet/source-build-reference-packages/pull/513.patch to
    # `vmr/src/source-build-reference-packages/NuGet.config `
    
    $ docker run --rm -w /vmr -v$(pwd)/vmr:/vmr amd64/archlinux sh -c \
      'yes | pacman -Syu bash clang cmake git icu inetutils krb5 libgit2 libunwind libxml2 lldb llvm lttng-ust2.12 openssl zlib base-devel;
       echo en_US.UTF-8 UTF-8 > /etc/locale.gen;
       locale-gen;
       echo LANG="en_US.UTF-8" > /etc/locale.conf;
       ./prep.sh;
       ./build.sh --clean-while-building --online -- -p:TargetRid=arch-x64'
    
    $ docker run --rm -w /vmr -v$(pwd)/vmr:/vmr amd64/archlinux sh -c \
      'mkdir $HOME/dotnet && tar zxf artifacts/x64/Release/dotnet-sdk-8.0.100-preview.1.23103.1-arch-x64.tar.gz -C $HOME/dotnet;
       ln -s $HOME/dotnet/dotnet /usr/bin/dotnet;
       cd /tmp; dotnet new console -n TestIo; cd TestIo;
       cp /vmr/src/sdk/NuGet.config .;
       cat > TestIo.csproj <<"EOF"
    <Project Sdk="Microsoft.NET.Sdk">
    
    <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>net8.0</TargetFramework>
      <ImplicitUsings>enable</ImplicitUsings>
      <Nullable>enable</Nullable>
    </PropertyGroup>
    
    <ItemGroup>
      <PackageReference Include="System.IO.Ports" Version="8.0.0-*" />
    </ItemGroup>
    
    </Project>
    EOF
      cat >Program.cs <<"EOF"
    using System.IO.Ports;
    
    System.Console.WriteLine(typeof(SerialPort).Assembly.Location);
    
    List<string> ports = SerialPort.GetPortNames().ToList();
    
    System.Console.WriteLine("Ports :");
    foreach (var port in ports)
    { System.Console.WriteLine(port); }
    EOF
      dotnet run;'

    Last two lines of output:

    /tmp/TestIo/bin/Debug/net8.0/runtimes/unix/lib/net8.0/System.IO.Ports.dll
    Ports :

  19. Foxlider commented on Feb 5, 2023

    @Foxlider
    Author

    Do you need me to test anything? Or should I wait for next release ?

    If I do, what commands do I need to run ?

  20. am11 commented on Feb 5, 2023

    @am11
    Member

    #79196 (comment) is testing with .NET 8 preview 1 local build using docker. You can try it if you like.

    For .NET 7 release, if @alucryd can apply that patch directly to Arch Linux package source (since that's the affected distro atm), that will make backporting quicker. Otherwise, we can try to request its backport for https://github.com/dotnet/runtime/tree/release/7.0 branch, which once accepted, might take a month or two before it would make its way to the next .NET 7 servicing release.

  21. alucryd commented on Feb 5, 2023

    @alucryd

    @am11 Done, patched package is up :)

  22. am11 commented on Feb 5, 2023

    @am11
    Member

    Thanks @alucryd! 🎉
    I ran a quick test and the exception does not reproduce with the latest package dotnet-sdk-7.0.2.sdk102-2-x86_64.

    $ docker run --rm amd64/archlinux sh -c \
      'yes | pacman -Syu dotnet-sdk-7.0;
    
       dotnet new console -n TestIo;
       cd TestIo;
    
       cat > TestIo.csproj <<"EOF"
    <Project Sdk="Microsoft.NET.Sdk">
    
    <PropertyGroup>
      <OutputType>Exe</OutputType>
      <TargetFramework>net7.0</TargetFramework>
      <ImplicitUsings>enable</ImplicitUsings>
      <Nullable>enable</Nullable>
    </PropertyGroup>
    
    <ItemGroup>
      <PackageReference Include="System.IO.Ports" Version="7.0.0" />
    </ItemGroup>
    
    </Project>
    EOF
    
      cat >Program.cs <<"EOF"
    using System.IO.Ports;
    
    System.Console.WriteLine(typeof(SerialPort).Assembly.Location);
    
    List<string> ports = SerialPort.GetPortNames().ToList();
    
    System.Console.WriteLine("Ports :");
    foreach (var port in ports)
    { System.Console.WriteLine(port); }
    EOF
    
      dotnet run;'

    result:

    ...snip...
    /TestIo/bin/Debug/net7.0/runtimes/unix/lib/net7.0/System.IO.Ports.dll
    Ports :
    
  23. ViktorHofer commented on Feb 5, 2023

    @ViktorHofer
    Member

    Wow, extremely impressive. So now we already have a patched 7.0.2 SDK available on ArchLinux which doesn't reproduce the issue. Fantastic! @am11 to not get out of sync with our own release/7.0 branch, should we backport the host fix? Otherwise if someone would build .NET 7 for ArchLinux from source again without your patch, the issue would re-appear.

  24. am11 commented on Feb 5, 2023

    @am11
    Member

    @ViktorHofer, I think it does not warrant a backport in our release/7.0 branch simply because this is not a regression. :)

    The non-portable flavor is there since .NET Core 1.0, it was portable that came later around .NET Core 2 when we realized having separate builds for each distro is not scalable model. Plus, this is the first report of the exception which is limited to nuget-with-native-binary on Arch Linux; a-rolling-release distro with VERSION_ID line in /etc/os-release which our sensitive parsing wasn't expecting.

  25. ghost locked as resolved and limited conversation to collaborators on Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions