runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Add a WinRT rid

Open natemcmaster opened this issue 10 years ago • 20 comments

Can we add a RID for platforms that support WinRT native APIs?

Our scenario:

Our package:

Microsoft.Data.Sqlite
    runtimes/
        win7-(bitness)/
              native/
                   sqlite3.dll  <-- win32 version

For UWP apps, this causes problems (see https://github.com/aspnet/Microsoft.Data.Sqlite/issues/245) because NuGet tries to add the win7 native asset (win32) and this does not pass the Windows App Cert Kit which does not allow calls to win32 APIs.

Our workaround: We use an MSBuild script to replace the "wrong" sqlite3.dll (win32) with the right one, a version compiled for WinRT APIs instead.

 <Target Name="FixResolvedSqliteNuGetPackageAssets" AfterTargets="ResolveNuGetPackageAssets">
    <ItemGroup>
      <WrongSqliteNuGetPackageAssets Include="$(MSBuildThisFileDirectory)..\..\runtimes\win7-$(Platform)\native\*" />
      <ReferenceCopyLocalPaths Remove="%(WrongSqliteNuGetPackageAssets.FullPath)" />
      <ReferenceCopyLocalPaths Include="$(MSBuildThisFileDirectory)win10-$(Platform)\native\*" />
    </ItemGroup>
  </Target>

Where the workaround fails: If Microsoft.Data.Sqlite is a transitive dependency, the MSBuild target does not run. We cannot fix the incorrect NuGet resolution.

A WinRT rid Ideally, we'd like to drop the MSBuild hack altogether and have a package like this:

Microsoft.Data.Sqlite
    runtimes/
        win7-(bitness)/
              native/
                   sqlite3.dll  <-- win32 version
        winrt-(bitness)/
               native/
                   sqlite3.dll  <-- winRT version

UWP and other WinRT compatible platforms would get the WinRT native asset instead of the Win32 asset.

cc @ericstj @yishaigalatzer

natemcmaster avatar May 12 '16 16:05 natemcmaster

If we wanted to do that it'd require a template change in Visual Studio as well as a msbuild targets change in NuGet in addition to adding the RID to the RID graph. /cc @Pilchie @jasonmalinowski

Can you workaround this by putting the native library under runtimes/{rid}/lib/uap10.0. Lib and native under runtimes aren't treated any different by msbuild based projects.

ericstj avatar May 12 '16 17:05 ericstj

Does nuget support this folder structure for native assets? runtimes/{rid}/native/{tfm}/ I didn't see this in https://docs.nuget.org/create/uwp-create#native-light-up

natemcmaster avatar May 12 '16 17:05 natemcmaster

No, but as I mentioned lib and native are treated the same for UWP projects. They just give you some flexibility of having different asset types.

ericstj avatar May 12 '16 17:05 ericstj

Lib and native under runtimes aren't treated any different by msbuild based projects.

True for now, but I'm not sure I can rely on that always being true in future version of msbuild.

natemcmaster avatar May 12 '16 17:05 natemcmaster

Native is the TFM, so it doesn't support a TFM after it. The paths are runtimes/{rid}/native/ and runtimes/{rid}/lib/{tfm}/

emgarten avatar May 12 '16 17:05 emgarten

Ok, workaround possibilities aside, is adding a WinRT rid something we can consider?

natemcmaster avatar May 12 '16 17:05 natemcmaster

That's more of a request for VS and Windows folks since the complicated / breaking changes are on the templates side and targets side. I personally don't see a lot of value in doing this since the TFM is sufficient to qualify here. If we started having multiple different TFMs/AppModels with the same APPX bit requirement then it might make sense to add a distinct RID heirarchy.

ericstj avatar May 12 '16 18:05 ericstj

cc @davidfowl

natemcmaster avatar May 12 '16 18:05 natemcmaster

So would it be better to add support for runtimes/{rid}/native/{tfm}/ to NuGet?

From discussion with @emgarten, at the moment runtimes/{rid}/native/ adds native assets to all TFMs, (e.g. Win32 ends up in WinRT output), but if we moved native libraries to runtime/{rid}/lib/{tfm} then p/invoke stops working with dotnet run because it only probes runtimes/{rid}/native for native assets. So we're stuck.

natemcmaster avatar May 12 '16 18:05 natemcmaster

From NuGet's point of view native dlls can exist in runtimes/{rid}/lib/{tfm} the native folder is a used to add an assembly in addition to the lib folder.

@ericstj @davidfowl is there a requirement that native dlls be under the native folder? It would be nice to clarify on how this should work and how build should handle this.

emgarten avatar May 12 '16 18:05 emgarten

That might be a reasonable alternative. If the dependencies is indeed on WinRT surface you're better off with a TFM/TxM because WinRT has a more granular continuous version that represents the API surface and that's encoded in the TFM. I don't think we want to be in the business of shipping new RIDs everytime Windows rev's windows.winmd, and having folks rewrite their project.jsons.

DotNET run doesn't need to load the WinRT version. If you could come up with a scheme that would give CLI the right version using native and give netcore50 the right version (without duplicates) that would be sufficient.

@emgarten I believe CLI/host requires native DLLs to be in the native folder since the host needs to treat these differently than managed DLLs. Correct @schellap?

ericstj avatar May 12 '16 18:05 ericstj

@ericstj the host relies on the package relative asset path specified in the app.deps.json which comes from project.lock.json, the host doesn't by itself enforce any path rules. The RID information for the asset also comes from the deps file.

Example:

        "runtimeTargets": {
          "runtimes/debian-x64/native/libuv.so": {
            "rid": "debian-x64",
            "assetType": "native"
          },
          "runtimes/osx/native/libuv.dylib": {
            "rid": "osx",
            "assetType": "native"
          },

@natemcmaster do you have a repro of:

but if we moved native libraries to runtime/{rid}/lib/{tfm} then p/invoke stops working with dotnet run because it only probes runtimes/{rid}/native for native assets. So we're stuck.

The host however expects TFM be resolved before publish, example: runtimes/{rid}/native/netstandard1.3/, then the host would simply just do a rid-fallback starting from its own rid to match app.deps.json's runtimeTargets.{asset-path}.rid (above) to come up with the path: $APP_BASE/asset-path of rid-matched runtimeTargets

schellap avatar May 12 '16 20:05 schellap

Here is the sample which contains libuv in this pattern: https://gist.github.com/schellap/547edf55982a9c826891a96d64c5233d

schellap avatar May 12 '16 20:05 schellap

@schellap on windows, if sqlite3.dll is in "runtimes/rid/lib/tfm", the lock file produces

        "runtimeTargets": {
          "runtimes/win7-x64/lib/netstandard1.0/sqlite3.dll": {
            "assetType": "runtime",
            "rid": "win7-x64"
          }
        }

This means the deps file gets "assetType": "runtime" as well, so p/invoke doesn't find the native library.

Unhandled Exception: System.DllNotFoundException: Unable to load DLL 'sqlite3': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

Should this be a bug on CLI? Seems to me this expected behavior since dll files in "runtimes/win7-x64/lib/netstandard1.0/" are expected to be managed.

natemcmaster avatar May 12 '16 21:05 natemcmaster

@anurse could you confirm that CLI uses the asset sub-path to actually detect runtime/native assetType?

schellap avatar May 12 '16 21:05 schellap

If I manually change project.lock.json to assetType native, p/invoke finds sqlite3.dll just fine.

       "runtimeTargets": {
          "runtimes/win7-x64/lib/netstandard1.0/sqlite3.dll": {
            "rid": "win7-x64",
            "assetType": "native"
          }
        }

natemcmaster avatar May 12 '16 21:05 natemcmaster

From NuGet's point of view native dlls can exist in runtimes/{rid}/lib/{tfm} the native folder is a used to add an assembly in addition to the lib folder.

The problem is NuGet treats it as a managed library, even though it is a native binary, because it lives in lib

schellap avatar May 12 '16 21:05 schellap

The problem is NuGet treats it as a managed library even though it is a native binary because it lives in lib

I wouldn't say NuGet is treating it as anything here. AssetType just refers to the lock file section it would be under if this were in a runtime target graph. The actual type of the assembly could be anything based on how the /lib folder worked before when there was a /native and /runtimes folder.

If it is the case that package authors need to separate out native vs managed assemblies when adding them under the /runtimes folder then I agree that NuGet needs to add support for /runtimes/{rid}/native/{txm}/

dotnet build is still going to have trouble with older packages where a user has added native dlls under the /lib/net40 folder. NuGet isn't going to start inspecting assemblies to determine the actual type for build. It seems like build should handle this scenario and do more to detect the type.

emgarten avatar May 12 '16 21:05 emgarten

dotnet build is still going to have trouble with older packages where a user has added native dlls under the /lib/net40 folder

That's OK, the CLI host isn't going to work for "net40" apps, it's for Core apps, right? Even if someone "imports" one of those into a core app I don't think it's reasonable to expect that to work.

I feel like we're settling on adding /runtimes/{rid}/native/{txm}/ to NuGet. So I'm closing this and opening a NuGet issue.

ericstj avatar May 13 '16 13:05 ericstj

Thanks @ericstj

natemcmaster avatar May 13 '16 15:05 natemcmaster