Skip to content

Commit c26a881

Browse files
authored
Fix dependency resolution for native dependencies (#9261)
* Fix native dependencies * Mac doesn't support TorchSharp
1 parent eec3561 commit c26a881

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/fsharp/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.ProjectFile.fs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ $(PACKAGEREFERENCES)
190190
Condition="'%(RuntimeTargetsCopyLocalItems.AssetType)' == 'native'">
191191
<Path>$([MSBuild]::EnsureTrailingSlash('$([System.String]::Copy('%(FullPath)').Substring(0, $([System.String]::Copy('%(FullPath)').LastIndexOf('runtimes'))))'))</Path>
192192
</NativeIncludeRoots>
193+
<NativeIncludeRoots
194+
Include="@(NativeCopyLocalItems)"
195+
Condition="'%(NativeCopyLocalItems.AssetType)' == 'native'">
196+
<Path>$([MSBuild]::EnsureTrailingSlash('$([System.String]::Copy('%(FullPath)').Substring(0, $([System.String]::Copy('%(FullPath)').LastIndexOf('runtimes'))))'))</Path>
197+
</NativeIncludeRoots>
193198
</ItemGroup>
194199
</Target>
195200
@@ -201,8 +206,12 @@ $(PACKAGEREFERENCES)
201206
<ItemGroup>
202207
<ResolvedReferenceLines Remove='*' />
203208
<ResolvedReferenceLines
204-
Condition="'$(SCRIPTEXTENSION)'=='.csx' or '%(InteractiveResolvedFile.NugetPackageId)'!='FSharp.Core'"
205-
Include='%(InteractiveResolvedFile.NugetPackageId),%(InteractiveResolvedFile.NugetPackageVersion),%(InteractiveResolvedFile.PackageRoot),%(InteractiveResolvedFile.FullPath),%(InteractiveResolvedFile.AssetType),%(InteractiveResolvedFile.IsNotImplementationReference),%(InteractiveResolvedFile.InitializeSourcePath),%(NativeIncludeRoots.Path)'
209+
Condition="'$(SCRIPTEXTENSION)'=='.fsx' and '%(InteractiveResolvedFile.NugetPackageId)'!='FSharp.Core'"
210+
Include='%(InteractiveResolvedFile.NugetPackageId),%(InteractiveResolvedFile.NugetPackageVersion),%(InteractiveResolvedFile.PackageRoot),%(InteractiveResolvedFile.FullPath),%(InteractiveResolvedFile.AssetType),%(InteractiveResolvedFile.IsNotImplementationReference),%(InteractiveResolvedFile.InitializeSourcePath),'
211+
KeepDuplicates="false" />
212+
<ResolvedReferenceLines
213+
Condition="'$(SCRIPTEXTENSION)'=='.fsx' and '%(NativeIncludeRoots.NugetPackageId)'!='FSharp.Core'"
214+
Include='%(NativeIncludeRoots.NugetPackageId),%(NativeIncludeRoots.NugetPackageVersion),%(NativeIncludeRoots.PackageRoot),,%(NativeIncludeRoots.AssetType),,,%(NativeIncludeRoots.Path)'
206215
KeepDuplicates="false" />
207216
</ItemGroup>
208217

tests/FSharp.Compiler.Private.Scripting.UnitTests/DependencyManagerInteractiveTests.fs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open System
66
open System.Collections.Generic
77
open System.IO
88
open System.Reflection
9+
open System.Runtime.InteropServices
910
open FSharp.Compiler.Interactive.Shell
1011
open FSharp.Compiler.Scripting
1112
open FSharp.Compiler.SourceCodeServices
@@ -218,7 +219,26 @@ type DependencyManagerInteractiveTests() =
218219
()
219220

220221
/// Native dll resolution is not implemented on desktop
221-
#if NETSTANDARD
222+
#if NETCOREAPP
223+
[<Test>]
224+
member __.``Script using TorchSharp``() =
225+
let text = """
226+
#r "nuget:RestoreSources=https://donsyme.pkgs.visualstudio.com/TorchSharp/_packaging/packages2/nuget/v3/index.json"
227+
#r "nuget:libtorch-cpu,0.3.52118"
228+
#r "nuget:TorchSharp,0.3.52118"
229+
230+
TorchSharp.Tensor.LongTensor.From([| 0L .. 100L |]).Device
231+
"""
232+
233+
if RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then
234+
use script = scriptHost()
235+
let opt = script.Eval(text) |> getValue
236+
let value = opt.Value
237+
Assert.AreEqual(typeof<string>, value.ReflectionType)
238+
Assert.AreEqual("cpu", value.ReflectionValue :?> string)
239+
()
240+
241+
222242
[<Test>]
223243
member __.``Use Dependency Manager to restore packages with native dependencies, build and run script that depends on the results``() =
224244
let packagemanagerlines = [|

0 commit comments

Comments
 (0)