Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SDK-selected runtime in IL SDK #60400

Merged
merged 3 commits into from
Oct 26, 2021
Merged

Conversation

am11
Copy link
Member

@am11 am11 commented Oct 14, 2021

When IL SDK is publicly consumed, it is less usable on variety of platforms than the .NET SDK. The reason is a hardcoded list of runtime identifiers.

Repro:

# on alpine-x64
$ mkdir testilsdk
$ cd testilsdk
$ cat > testilsdk.ilproj <<EOF
<Project Sdk="Microsoft.NET.Sdk.IL">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>
EOF

$ cat > global.json <<EOF
{
  "msbuild-sdks": {
    "Microsoft.NET.Sdk.IL": "5.0.0"
  }
}
EOF

$ cat > Program.il <<EOF
.assembly extern System.Runtime
{
  .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A)
}
.assembly extern System.Console { }

.assembly ConsoleApp { }

.class public auto ansi abstract sealed beforefieldinit Program extends System.Object
{
    .method public hidebysig static void Main () cil managed
    {
        .entrypoint
        .maxstack 8

        ldstr "Hello World! 👋"
        call void [System.Console]System.Console::WriteLine(string)
        ret
    }
}
EOF

$ dotnet build

which throws the following error:

/home/am11/.nuget/packages/microsoft.net.sdk.il/5.0.0/targets/Microsoft.NET.Sdk.IL.targets(139,5): error MSB3073: The command "/home/am11/.nuget/packages/runtime.linux-x64.microsoft.netcore.ilasm/5.0.0/runtimes/linux-x64/native/ilasm -QUIET -NOLOGO -EXE -OUTPUT="obj/Debug/net5.0/testilsdk.dll" Program.il" exited with code 127. [/home/am11/projects/testilsdk/testilsdk.ilproj]

The build failed. Fix the build errors and run again.

It is trying to access linux-x64, rather than linux-musl-x64.
PR fixes the problem by delegating external usages default to the runtime picked by its parent; the .NET SDK.

Workaround:

It requires us leaking the internal usage details to public and we need to explicitly pass (a very weird looking long property with) the desired runtime:

$ dotnet build -p:MicrosoftNetCoreIlasmPackageRuntimeId=linux-musl-x64
# while for the rest of the ecosystem
#     --runtime <VAL>
#     or
#     --use-curren-runtime
# suffice

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Oct 14, 2021
@ghost
Copy link

ghost commented Oct 14, 2021

Tagging subscribers to this area: @Anipik, @safern, @ViktorHofer
See info in area-owners.md if you want to be subscribed.

Issue Details

When IL SDK is publicly consumed, it is less usable on variety of platforms than the .NET SDK. The reason is a hardcoded list of runtimes identifiers.

Repro:

# on alpine-x64
$ mkdir testilsdk
$ cd testilsdk
$ cat > testilsdk.ilproj <<EOF
<Project Sdk="Microsoft.NET.Sdk.IL">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>
EOF

$ cat > global.json <<EOF
{
  "msbuild-sdks": {
    "Microsoft.NET.Sdk.IL": "5.0.0"
  }
}
EOF

$ cat > Program.il <<EOF
.assembly extern System.Private.CoreLib { }
.assembly extern System.Console { }

.assembly ConsoleApp { }

.class public auto ansi abstract sealed beforefieldinit Program extends System.Object
{
    .method public hidebysig static void Main () cil managed
    {
        .entrypoint
        .maxstack 8

        ldstr "Hello World! 👋"
        call void [System.Console]System.Console::WriteLine(string)
        ret
    }
}
EOF

$ dotnet build

which throws the following error:

/home/am11/.nuget/packages/microsoft.net.sdk.il/5.0.0/targets/Microsoft.NET.Sdk.IL.targets(139,5): error MSB3073: The command "/home/am11/.nuget/packages/runtime.linux-x64.microsoft.netcore.ilasm/5.0.0/runtimes/linux-x64/native/ilasm -QUIET -NOLOGO -EXE -OUTPUT="obj/Debug/net5.0/testilsdk.dll" Program.il" exited with code 127. [/home/am11/projects/testilsdk/testilsdk.ilproj]

The build failed. Fix the build errors and run again.

It is trying to access linux-x64, rather than linux-musl-x64.
PR fixes the problem by delegating external usages default to SDK picked by its parent; the .NET SDK.

Workaround:

It requires us leaking the internal usage details to public and we need to explicitly pass (a very weird looking long property with) the desired runtime:

$ dotnet build -p:MicrosoftNetCoreIlasmPackageRuntimeId=linux-musl-x64
# while for the rest of the ecosystem
#     --runtime <VAL>
#     or
#     --use-curren-runtime
# suffice
Author: am11
Assignees: -
Labels:

area-Infrastructure-libraries, community-contribution

Milestone: -

@am11
Copy link
Member Author

am11 commented Oct 14, 2021

cc @ViktorHofer, @hoyosjs, one less hardcoded list (hopefully) 😁

@ViktorHofer
Copy link
Member

ViktorHofer commented Oct 14, 2021

This path isn't exercised by CI at the moment, as we don't use a live build of the IL SDK and because we pass in the desired RID. Approving assuming that you tested the changes locally and that the SDK property is always set (?). Presumably this will only work when the package is used in a Microsoft.NET.Sdk project that defines that property - I'm ok with that.

@am11
Copy link
Member Author

am11 commented Oct 14, 2021

@ViktorHofer, I applied this change locally (after analyzing logs produced by -v:diag) in
~/.nuget/packages/microsoft.net.sdk.il/5.0.0/targets/Microsoft.NET.Sdk.IL.targets.

After that there was another error:

/home/am11/.dotnet5/sdk/5.0.402/Microsoft.Common.CurrentVersion.targets(4521,5): error : Expected file "obj/Debug/net5.0/ref/testilsdk.dll" does not exist. [/home/am11/projects/testilsdk/testilsdk.ilproj]

The build failed. Fix the build errors and run again.

for some reason, it is looking for the compiled dll under ref/ (rather than its parent dir where it actually is). The workaround to that is:

# 1. apply this PR patch
# 2. dotnet build             <-- this will fail, but it's a prerequisite to the following workaround
# 3. cp obj/Debug/net5.0/testilsdk.dll obj/Debug/net5.0/ref/       <-- the workaround
# 4. dotnet run
Hello World! 👋

I will keep troubleshooting what is requiring ref/testilsdk.dll, but it is something not improved or regressed by this change. :)

@jkoritzinsky
Copy link
Member

@am11 try setting <ProduceReferenceAssembly>false</ProduceReferenceAssembly> to fix the ref/ failure you’re seeing.

@ViktorHofer
Copy link
Member

Agree with @jkoritzinsky, great observation. It's likely this code path that is triggered which shouldn't run when using the IL.SDK as roslyn isn't used: https://github.com/dotnet/msbuild/blob/35c6ef021eb745623b123307c2ebb55bf6564ef9/src/Tasks/Microsoft.Common.CurrentVersion.targets#L4647-L4657.

If that worked, feel free to add that change to the IL.SDK props/targets file :)

@am11
Copy link
Member Author

am11 commented Oct 14, 2021

Wow, that fixed it, thank you! I have set it in the Sdk.props (tested it locally by setting in ~/.nuget/packages/microsoft.net.sdk.il/5.0.0/sdk/Sdk.props (only); works on my machine. ™️

@ViktorHofer
Copy link
Member

Thanks a lot 👍

@am11 am11 mentioned this pull request Oct 16, 2021
@am11
Copy link
Member Author

am11 commented Oct 26, 2021

@ViktorHofer, any chance this change be backported to 6.0?

@ViktorHofer ViktorHofer merged commit d702914 into dotnet:main Oct 26, 2021
@ViktorHofer
Copy link
Member

Unfortunately it's too late for 6.0.0 but it's a good candidate for the first servicing release 6.0.1. Might make sense to prep a PR against release/6.0 with the servicing template.

cc @danmoseley

@ghost ghost locked as resolved and limited conversation to collaborators Nov 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Infrastructure-libraries community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants