Skip to content

Commit 8b361cf

Browse files
authored
don't emit empty assembly references to package resolution file (#10206)
1 parent 83c6440 commit 8b361cf

13 files changed

+106
-54
lines changed

src/fsharp/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@
175175
<Compile Include="..\..\utils\PathMap.fs">
176176
<Link>Utilities\PathMap.fs</Link>
177177
</Compile>
178+
<Compile Include="..\..\utils\RidHelpers.fs">
179+
<Link>Utilities\RidHelpers.fs</Link>
180+
</Compile>
178181
<Compile Include="..\range.fsi">
179182
<Link>ErrorLogging\range.fsi</Link>
180183
</Compile>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ $(PACKAGEREFERENCES)
210210
<ItemGroup>
211211
<ResolvedReferenceLines Remove='*' />
212212
<ResolvedReferenceLines
213-
Condition=" ('%(InteractiveResolvedFile.NugetPackageId)'!='FSharp.Core') or ('$(SCRIPTEXTENSION)'!='.fsx' and '%(InteractiveResolvedFile.NugetPackageId)'=='FSharp.Core')"
213+
Condition="(@(InteractiveResolvedFile->Count()) &gt; 0) AND (('%(InteractiveResolvedFile.NugetPackageId)'!='FSharp.Core') or ('$(SCRIPTEXTENSION)'!='.fsx' and '%(InteractiveResolvedFile.NugetPackageId)'=='FSharp.Core'))"
214214
Include='%(InteractiveResolvedFile.NugetPackageId),%(InteractiveResolvedFile.NugetPackageVersion),%(InteractiveResolvedFile.PackageRoot),%(InteractiveResolvedFile.FullPath),%(InteractiveResolvedFile.AssetType),%(InteractiveResolvedFile.IsNotImplementationReference),%(InteractiveResolvedFile.InitializeSourcePath),'
215215
KeepDuplicates="false" />
216216
<ResolvedReferenceLines
217-
Condition="('%(NativeIncludeRoots.NugetPackageId)'!='FSharp.Core') or ('$(SCRIPTEXTENSION)'!='.fsx' and '%(NativeIncludeRoots.NugetPackageId)'=='FSharp.Core')"
217+
Condition="(@(NativeIncludeRoots->Count()) &gt; 0) AND (('%(NativeIncludeRoots.NugetPackageId)'!='FSharp.Core') or ('$(SCRIPTEXTENSION)'!='.fsx' and '%(NativeIncludeRoots.NugetPackageId)'=='FSharp.Core'))"
218218
Include='%(NativeIncludeRoots.NugetPackageId),%(NativeIncludeRoots.NugetPackageVersion),%(NativeIncludeRoots.PackageRoot),,%(NativeIncludeRoots.AssetType),,,%(NativeIncludeRoots.Path)'
219219
KeepDuplicates="false" />
220220
</ItemGroup>

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ open System.Reflection
99
[<AttributeUsage(AttributeTargets.Assembly ||| AttributeTargets.Class , AllowMultiple = false)>]
1010
type DependencyManagerAttribute() = inherit System.Attribute()
1111

12+
/// The result of building the package resolution files.
13+
type PackageBuildResolutionResult =
14+
{ success: bool
15+
projectPath: string
16+
stdOut: string array
17+
stdErr: string array
18+
resolutionsFile: string option }
19+
1220
module internal Utilities =
1321

1422
/// Return a string array delimited by commas
@@ -202,7 +210,7 @@ module internal Utilities =
202210

203211
let workingDir = Path.GetDirectoryName projectPath
204212

205-
let succeeded, stdOut, stdErr =
213+
let success, stdOut, stdErr =
206214
if not (isRunningOnCoreClr) then
207215
// The Desktop build uses "msbuild" to build
208216
executeBuild msbuildExePath (arguments "-v:quiet") workingDir
@@ -211,5 +219,9 @@ module internal Utilities =
211219
executeBuild dotnetHostPath (arguments "msbuild -v:quiet") workingDir
212220

213221
let outputFile = projectPath + ".resolvedReferences.paths"
214-
let resultOutFile = if succeeded && File.Exists(outputFile) then Some outputFile else None
215-
succeeded, stdOut, stdErr, resultOutFile
222+
let resolutionsFile = if success && File.Exists(outputFile) then Some outputFile else None
223+
{ success = success
224+
projectPath = projectPath
225+
stdOut = stdOut
226+
stdErr = stdErr
227+
resolutionsFile = resolutionsFile }

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ module FSharpDependencyManager =
125125
|> List.distinct
126126
|> (fun l -> l, binLogPath)
127127

128-
129128
/// The results of ResolveDependencies
130129
type ResolveDependenciesResult (success: bool, stdOut: string array, stdError: string array, resolutions: string seq, sourceFiles: string seq, roots: string seq) =
131130

@@ -195,12 +194,12 @@ type FSharpDependencyManager (outputDir:string option) =
195194
sprintf """ #r "nuget:FSharp.Data";; // %s 'FSharp.Data' %s""" (SR.loadNugetPackage()) (SR.highestVersion())
196195
|]
197196

198-
member _.ResolveDependencies(scriptExt:string, packageManagerTextLines: (string *string) seq, tfm: string, rid: string) : obj =
197+
member _.PrepareDependencyResolutionFiles(scriptExt: string, packageManagerTextLines: (string * string) seq, targetFrameworkMoniker: string, runtimeIdentifier: string): PackageBuildResolutionResult =
199198

200-
let scriptExt, poundRprefix =
199+
let scriptExt =
201200
match scriptExt with
202-
| ".csx" -> csxExt, "#r \""
203-
| _ -> fsxExt, "#r @\""
201+
| ".csx" -> csxExt
202+
| _ -> fsxExt
204203

205204
let packageReferences, binLogPath =
206205
packageManagerTextLines
@@ -214,39 +213,50 @@ type FSharpDependencyManager (outputDir:string option) =
214213

215214
let packageReferenceText = String.Join(Environment.NewLine, packageReferenceLines)
216215

217-
// Generate a project files
216+
let projectPath = Path.Combine(scriptsPath, "Project.fsproj")
217+
218+
// Generate project files
218219
let generateAndBuildProjectArtifacts =
219220
let writeFile path body =
220221
if not (generatedScripts.ContainsKey(body.GetHashCode().ToString())) then
221222
emitFile path body
222223

223-
let projectPath = Path.Combine(scriptsPath, "Project.fsproj")
224-
225224
let generateProjBody =
226-
generateProjectBody.Replace("$(TARGETFRAMEWORK)", tfm)
227-
.Replace("$(RUNTIMEIDENTIFIER)", rid)
225+
generateProjectBody.Replace("$(TARGETFRAMEWORK)", targetFrameworkMoniker)
226+
.Replace("$(RUNTIMEIDENTIFIER)", runtimeIdentifier)
228227
.Replace("$(PACKAGEREFERENCES)", packageReferenceText)
229228
.Replace("$(SCRIPTEXTENSION)", scriptExt)
230229

231230
writeFile projectPath generateProjBody
231+
buildProject projectPath binLogPath
232+
233+
generateAndBuildProjectArtifacts
234+
235+
member this.ResolveDependencies(scriptExt: string, packageManagerTextLines: (string * string) seq, targetFramework: string, runtimeIdentifier: string) : obj =
236+
let poundRprefix =
237+
match scriptExt with
238+
| ".csx" -> "#r \""
239+
| _ -> "#r @\""
240+
241+
let generateAndBuildProjectArtifacts =
232242

233-
let result, stdOut, stdErr, resolutionsFile = buildProject projectPath binLogPath
234-
match resolutionsFile with
243+
let resolutionResult = this.PrepareDependencyResolutionFiles(scriptExt, packageManagerTextLines, targetFramework, runtimeIdentifier)
244+
match resolutionResult.resolutionsFile with
235245
| Some file ->
236246
let resolutions = getResolutionsFromFile file
237247
let references = (findReferencesFromResolutions resolutions) |> Array.toSeq
238248
let scripts =
239-
let scriptPath = projectPath + scriptExt
249+
let scriptPath = resolutionResult.projectPath + scriptExt
240250
let scriptBody = makeScriptFromReferences references poundRprefix
241251
emitFile scriptPath scriptBody
242252
let loads = (findLoadsFromResolutions resolutions) |> Array.toList
243253
List.concat [ [scriptPath]; loads] |> List.toSeq
244254
let includes = (findIncludesFromResolutions resolutions) |> Array.toSeq
245255

246-
ResolveDependenciesResult(result, stdOut, stdErr, references, scripts, includes)
256+
ResolveDependenciesResult(resolutionResult.success, resolutionResult.stdOut, resolutionResult.stdErr, references, scripts, includes)
247257

248258
| None ->
249259
let empty = Seq.empty<string>
250-
ResolveDependenciesResult(result, stdOut, stdErr, empty, empty, empty)
260+
ResolveDependenciesResult(resolutionResult.success, resolutionResult.stdOut, resolutionResult.stdErr, empty, empty, empty)
251261

252262
generateAndBuildProjectArtifacts :> obj

src/fsharp/FSharp.DependencyManager.Nuget/FSharp.DependencyManager.fsi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type ResolveDependenciesResult =
2929
/// The roots to package directories
3030
member Roots: seq<string>
3131

32-
[<DependencyManagerAttribute>]
32+
[<DependencyManagerAttribute>]
3333
type FSharpDependencyManager =
3434
new: outputDir:string option -> FSharpDependencyManager
3535

@@ -39,4 +39,6 @@ type FSharpDependencyManager =
3939

4040
member HelpMessages:string[]
4141

42-
member ResolveDependencies: scriptExt:string * packageManagerTextLines: (string * string) seq * tfm: string * rid: string -> obj
42+
member PrepareDependencyResolutionFiles: scriptExt: string * packageManagerTextLines: (string * string) seq * targetFrameworkMoniker: string * runtimeIdentifier: string -> PackageBuildResolutionResult
43+
44+
member ResolveDependencies: scriptExt: string * packageManagerTextLines: (string * string) seq * targetFrameworkMoniker: string * runtimeIdentifier: string -> obj

src/fsharp/Microsoft.DotNet.DependencyManager/DependencyProvider.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open System
66
open System.IO
77
open System.Reflection
88
open System.Runtime.InteropServices
9+
open Internal.Utilities
910
open Internal.Utilities.FSharpEnvironment
1011
open Microsoft.FSharp.Reflection
1112
open System.Collections.Concurrent

src/fsharp/Microsoft.DotNet.DependencyManager/Microsoft.DotNet.DependencyManager.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<ItemGroup>
2424
<EmbeddedText Include="DependencyManager.txt" />
2525
<EmbeddedText Include="..\..\utils\UtilsStrings.txt" />
26+
<Compile Include="..\..\utils\RidHelpers.fs" />
2627
<Compile Include="..\..\utils\CompilerLocationUtils.fs" />
2728
<Compile Include="AssemblyResolveHandler.fsi" />
2829
<Compile Include="AssemblyResolveHandler.fs" />

src/fsharp/Microsoft.DotNet.DependencyManager/NativeDllResolveHandler.fs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,16 @@
33
namespace Microsoft.DotNet.DependencyManager
44

55
open System
6-
open System.Collections.Generic
76
open System.IO
87
open System.Reflection
98
open System.Runtime.InteropServices
9+
open Internal.Utilities
1010
open Internal.Utilities.FSharpEnvironment
1111

1212
/// Signature for Native library resolution probe callback
1313
/// host implements this, it's job is to return a list of package roots to probe.
1414
type NativeResolutionProbe = delegate of Unit -> seq<string>
1515

16-
module internal RidHelpers =
17-
18-
// Computer valid dotnet-rids for this environment:
19-
// https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
20-
//
21-
// Where rid is: win, win-x64, win-x86, osx-x64, linux-x64 etc ...
22-
let probingRids, baseRid, platformRid =
23-
let processArchitecture = RuntimeInformation.ProcessArchitecture
24-
let baseRid =
25-
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then "win"
26-
elif RuntimeInformation.IsOSPlatform(OSPlatform.OSX) then "osx"
27-
else "linux"
28-
let platformRid =
29-
match processArchitecture with
30-
| Architecture.X64 -> baseRid + "-x64"
31-
| Architecture.X86 -> baseRid + "-x86"
32-
| Architecture.Arm64 -> baseRid + "-arm64"
33-
| _ -> baseRid + "-arm"
34-
[| "any"; baseRid; platformRid |], baseRid, platformRid
35-
36-
open RidHelpers
37-
3816
#if NETSTANDARD
3917
open System.Runtime.Loader
4018

@@ -109,7 +87,7 @@ type NativeDllResolveHandlerCoreClr (_nativeProbingRoots: NativeResolutionProbe)
10987
if File.Exists(path) then
11088
Some path
11189
else
112-
probingRids |> Seq.tryPick(fun rid -> probeForNativeLibrary root rid name)))
90+
RidHelpers.probingRids |> Seq.tryPick(fun rid -> probeForNativeLibrary root rid name)))
11391

11492
match probe with
11593
| Some path -> NativeAssemblyLoadContext.NativeLoadContext.LoadNativeLibrary(path)

src/fsharp/Microsoft.DotNet.DependencyManager/NativeDllResolveHandler.fsi

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@
33
namespace Microsoft.DotNet.DependencyManager
44

55
open System
6-
open System.Collections.Generic
7-
8-
module internal RidHelpers =
9-
val probingRids:string []
10-
val baseRid:string
11-
val platformRid:string
12-
136

147
/// Signature for Native library resolution probe callback
158
/// host implements this, it's job is to return a list of package roots to probe.

src/utils/RidHelpers.fs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Internal.Utilities
2+
3+
open System.Runtime.InteropServices
4+
5+
module internal RidHelpers =
6+
7+
// Computer valid dotnet-rids for this environment:
8+
// https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
9+
//
10+
// Where rid is: win, win-x64, win-x86, osx-x64, linux-x64 etc ...
11+
let probingRids, baseRid, platformRid =
12+
let processArchitecture = RuntimeInformation.ProcessArchitecture
13+
let baseRid =
14+
if RuntimeInformation.IsOSPlatform(OSPlatform.Windows) then "win"
15+
elif RuntimeInformation.IsOSPlatform(OSPlatform.OSX) then "osx"
16+
else "linux"
17+
let platformRid =
18+
match processArchitecture with
19+
| Architecture.X64 -> baseRid + "-x64"
20+
| Architecture.X86 -> baseRid + "-x86"
21+
| Architecture.Arm64 -> baseRid + "-arm64"
22+
| _ -> baseRid + "-arm"
23+
[| "any"; baseRid; platformRid |], baseRid, platformRid

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace FSharp.Compiler.Scripting.DependencyManager.UnitTests
44

55
open System
6-
open System.Collections.Generic
76
open System.IO
87
open System.Reflection
98
open System.Runtime.InteropServices
@@ -13,8 +12,11 @@ open FSharp.Compiler.Interactive.Shell
1312
open FSharp.Compiler.Scripting
1413
open FSharp.Compiler.SourceCodeServices
1514
open FSharp.Compiler.Scripting.UnitTests
15+
open FSharp.DependencyManager.Nuget
1616
open Microsoft.DotNet.DependencyManager
1717

18+
open Internal.Utilities
19+
1820
open Xunit
1921

2022
module Native =
@@ -743,3 +745,16 @@ x |> Seq.iter(fun r ->
743745
let opt = script.Eval(text) |> getValue
744746
Assert.True(sawExpectedOutput.WaitOne(TimeSpan.FromSeconds(5.0)), sprintf "Expected to see error sentinel value written\nexpected:%A\nactual:%A" expected lines)
745747

748+
[<Fact>]
749+
member __.``Ensure resolutions file doesn't contain lines with only empty values``() =
750+
use tempDir = getTempDir ()
751+
let dpm = new FSharpDependencyManager(Some(tempDir.ToString()))
752+
let resolutionResult = dpm.PrepareDependencyResolutionFiles(".fsx", [("r", "FParsec,Version=1.1.1")], "netcoreapp3.1", RidHelpers.platformRid)
753+
let resolutionLines =
754+
match resolutionResult.resolutionsFile with
755+
| Some path -> File.ReadAllLines(path)
756+
| None -> failwith "Expected resolutions file to be created."
757+
let improperResolutionLines =
758+
resolutionLines
759+
|> Array.filter (fun l -> l.StartsWith(","))
760+
Assert.Empty(improperResolutionLines)

tests/FSharp.Compiler.Private.Scripting.UnitTests/FSharp.Compiler.Private.Scripting.UnitTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<Compile Include="ConsoleHelpers.fs" />
15+
<Compile Include="$(FSharpSourcesRoot)\utils\RidHelpers.fs" />
1516
<Compile Include="TestHelpers.fs" />
1617
<Compile Include="DependencyManagerInteractiveTests.fs" />
1718
<Compile Include="DependencyManagerLineParserTests.fs" />

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FSharp.Compiler.Scripting.UnitTests
44

55
open System
6+
open System.IO
67
open FSharp.Compiler.Interactive.Shell
78
open FSharp.Compiler.SourceCodeServices
89

@@ -17,3 +18,15 @@ module TestHelpers =
1718
| Error ex -> raise ex
1819

1920
let ignoreValue = getValue >> ignore
21+
22+
let getTempDir () =
23+
let sysTempDir = Path.GetTempPath()
24+
let customTempDirName = Guid.NewGuid().ToString("D")
25+
let fullDirName = Path.Combine(sysTempDir, customTempDirName)
26+
let dirInfo = Directory.CreateDirectory(fullDirName)
27+
{ new Object() with
28+
member __.ToString() = dirInfo.FullName
29+
interface IDisposable with
30+
member __.Dispose() =
31+
dirInfo.Delete(true)
32+
}

0 commit comments

Comments
 (0)