dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.71k stars 3.98k forks source link

MSB3883 on *nix when using Microsoft.Net.Compilers package #39530

Open rainersigwald opened 4 years ago

rainersigwald commented 4 years ago

Version Used: 3.3.0

Steps to Reproduce:

Add a NuGet package reference to Microsoft.Net.Compilers and attempt to build on .NET Core on non-Windows.

crash-on-compiler-package.zip

Expected Behavior:

No crash.

Actual Behavior:

$ dotnet build                                                                                                        ⏎
Microsoft (R) Build Engine version 16.3.0+0f4c62fea for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 453.46 ms for /Users/raines/src/new/new.csproj.
  You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error MSB3883: Unexpected exception:  [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : System.ComponentModel.Win32Exception (8): Exec format error [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at System.Diagnostics.Process.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec) [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at System.Diagnostics.Process.Start() [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at Microsoft.Build.Utilities.ToolTask.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands) [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at Microsoft.CodeAnalysis.BuildTasks.ManagedCompiler.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands) [/Users/raines/src/new/new.csproj]

Build FAILED.

/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error MSB3883: Unexpected exception:  [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : System.ComponentModel.Win32Exception (8): Exec format error [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at System.Diagnostics.Process.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec) [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo) [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at System.Diagnostics.Process.Start() [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at Microsoft.Build.Utilities.ToolTask.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands) [/Users/raines/src/new/new.csproj]
/Users/raines/.nuget/packages/microsoft.net.compilers/3.3.0/tools/Microsoft.CSharp.Core.targets(59,5): error : at Microsoft.CodeAnalysis.BuildTasks.ManagedCompiler.ExecuteTool(String pathToTool, String responseFileCommands, String commandLineCommands) [/Users/raines/src/new/new.csproj]
    0 Warning(s)
    7 Error(s)

Time Elapsed 00:00:05.37

It's not obvious from this logging, but the problem is that the Csc task is attempting to directly invoke csc.exe, a Windows/desktop framework executable.

The correct package to use is Microsoft.Net.Compilers.Toolset. But it would still be nice to have a clear "You can't use this package on .NET Core; use Microsoft.Net.Compilers.Toolset instead" error instead of this crash.

This could be considered part of https://github.com/dotnet/roslyn/issues/33695.

(encountered by @sharwell in https://github.com/microsoft/vs-threading/pull/539; we root caused it offline)

fritz-net commented 2 years ago

Still happening using mcr.microsoft.com/dotnet/sdk:5.0

$ dotnet --version
5.0.401
$ dotnet nuget --version
NuGet Command Line
5.11.0.10
$ dotnet build -c Release --output ../app/
Microsoft (R) Build Engine version 16.11.0+0538acc04 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
  Determining projects to restore...
  All projects are up-to-date for restore.
/builds/.nugetcache/microsoft.net.compilers/3.11.0/tools/Microsoft.CSharp.Core.targets(71,5): error MSB3883: Unexpected exception:  [/Tools/SourceGenerator/SourceGenerator.csproj]
/builds/.nugetcache/microsoft.net.compilers/3.11.0/tools/Microsoft.CSharp.Core.targets(71,5): error : Exec format error [/Tools/SourceGenerator/SourceGenerator.csproj]

Thanks to @rainersigwald I was able to fix it by adding "Microsoft.Net.Compilers.Toolset" Version="3.11.0"

my project looked like this:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFrameworks>netstandard2.0;</TargetFrameworks>
        <Nullable>enable</Nullable>
        <LangVersion>9</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.11.0" />
        <PackageReference Include="Microsoft.CodeAnalysis.Compilers" Version="3.11.0" />
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
        <PackageReference Include="Microsoft.Net.Compilers" Version="3.11.0">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>
</Project>
ChaseFlorell commented 2 years ago

I had a similar issue, adding these to my Directory.Build.props

        <PackageReference Include="Microsoft.Net.Compilers">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.Net.Compilers.Toolset">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>

With this in my Directory.Packages.props

<PackageVersion Include="Microsoft.Net.Compilers" Version="4.0.1" />
<PackageVersion Include="Microsoft.Net.Compilers.Toolset" Version="4.0.1" />

Appears to have resolved my csc errors on MacOS

afelipems commented 2 years ago

I had a similar issue, adding these to my Directory.Build.props

        <PackageReference Include="Microsoft.Net.Compilers">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.Net.Compilers.Toolset">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>

With this in my Directory.Packages.props

<PackageVersion Include="Microsoft.Net.Compilers" Version="4.0.1" />
<PackageVersion Include="Microsoft.Net.Compilers.Toolset" Version="4.0.1" />

Appears to have resolved my csc errors on MacOS

You just saved my day!