Skip to content

fix: Support libraries using TargetFrameworks property #12

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

Merged
merged 13 commits into from
Apr 14, 2025
Merged
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ bin/
.DS_Store
node_modules/
demo/dist/

# Don't track this file because we dynamically install packages for it during the build process
tests/fixtures/valid/library-with-files-multi-tfm/packages.lock.json
tests/temp/
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="BlackFox.CommandLine" Version="1.0.0" />
<PackageVersion Include="EasyBuild.FileSystemProvider" Version="0.3.0" />
<PackageVersion Include="EasyBuild.PackageReleaseNotes.Tasks" Version="1.2.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
5 changes: 5 additions & 0 deletions src/Fable.Package.SDK.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
Include="build\*"
Pack="true"
PackagePath="build\" />
<!-- Support libraries using TargetFrameworks https://github.com/dotnet/msbuild/issues/1860 -->
<Content
Include="build\*"
Pack="true"
PackagePath="buildCrossTargeting\" />
</ItemGroup>
<!-- Disable dependency on FSharp.Core -->
<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions tests/Fable.Package.SDK.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<Compile Include="Workspace.fs" />
<Compile Include="Main.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BlackFox.CommandLine" />
<PackageReference Include="EasyBuild.FileSystemProvider" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
Expand Down
89 changes: 78 additions & 11 deletions tests/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ open SimpleExec
open NUnit.Framework
open System.IO
open System.IO.Compression
open BlackFox.CommandLine
open System

let private expectToFailWithMessage projectName (message: string) =
task {
Expand Down Expand Up @@ -42,6 +44,13 @@ let ``Missing FableTarget property should report an error`` () =
Workspace.fixtures.invalid.``MissingFableTarget.fsproj``
"You need to set at least one of Fable target via the PackageTags property. Possible values are: fable-dart, fable-dotnet, fable-javascript, fable-python, fable-rust, fable-all."

// Test disabled because it freeze on Github
// [<Test>]
// let ``Missing FableTarget property should report an error - MultiTFM`` () =
// expectToFailWithMessage
// Workspace.fixtures.invalid.``MissingFableTargetMultiTFM.fsproj``
// "You need to set at least one of Fable target via the PackageTags property. Possible values are: fable-dart, fable-dotnet, fable-javascript, fable-python, fable-rust, fable-all."

[<Test>]
let ``You cannot set both FablePackageType and FableTarget properties`` () =
task {
Expand Down Expand Up @@ -237,20 +246,78 @@ let ``should include the source file and the project file under 'fable' folder``
$"pack %s{Workspace.fixtures.valid.``library-with-files``.``MyLibrary.fsproj``}"
)

let archive = ZipFile.OpenRead(VirtualWorkspace.fixtures.valid.``library-with-files``.bin.Release.``MyLibrary.1.0.0.nupkg``)
let archive =
ZipFile.OpenRead(
VirtualWorkspace.fixtures.valid.``library-with-files``.bin.Release.``MyLibrary.1.0.0.nupkg``
)

let entries =
archive.Entries
|> Seq.map (fun entry -> entry.FullName)
|> Seq.toList
let entries = archive.Entries |> Seq.map (fun entry -> entry.FullName) |> Seq.toList

Assert.That(
entries,
Contains.Item("fable/Entry.fs")
Assert.That(entries, Contains.Item("fable/Entry.fs"))

Assert.That(entries, Contains.Item("fable/MyLibrary.fsproj"))
}

[<Test>]
let ``should include the source file and the project file under 'fable' folder - MultiTFM`` () =
task {
// Make sure we work with a fresh nupkg file
let fileInfo =
VirtualWorkspace.fixtures.valid.``library-with-files-multi-tfm``.bin.Release.``MyLibraryMultiTFM.1.0.0.nupkg``
|> FileInfo

if fileInfo.Exists then
fileInfo.Delete()

// When testing against multi Target Framework we need to use a "real package"
// because using the standard "Import" trick is not enough to capture errors
let tempPackageFolder = VirtualWorkspace.temp.``.`` |> FileInfo

if tempPackageFolder.Exists then
tempPackageFolder.Delete()

let tempVersion = "9.999.0-local-build-" + DateTime.Now.ToString("yyyyMMdd-HHmmss")

Command.Run(
"dotnet",
CmdLine.empty
|> CmdLine.append "pack"
|> CmdLine.append Workspace.``..``.src.``.``
|> CmdLine.appendPrefix "-o" tempPackageFolder.FullName
|> CmdLine.append $"/p:PackageVersion=%s{tempVersion}"
|> CmdLine.toString
)

Assert.That(
entries,
Contains.Item("fable/MyLibrary.fsproj")
Command.Run(
"dotnet",
CmdLine.empty
|> CmdLine.append "add"
|> CmdLine.appendPrefix "package" "Fable.Package.SDK"
|> CmdLine.appendPrefix "--version" tempVersion
|> CmdLine.appendPrefix "--source" tempPackageFolder.FullName
|> CmdLine.toString,
workingDirectory = Workspace.fixtures.valid.``library-with-files-multi-tfm``.``.``
)

Command.Run(
"dotnet",
$"pack %s{Workspace.fixtures.valid.``library-with-files-multi-tfm``.``MyLibraryMultiTFM.fsproj``}"
)

// Restore modified file to avoid polluting the Git history
Command.Run(
"git",
$"restore %s{Workspace.fixtures.valid.``library-with-files-multi-tfm``.``MyLibraryMultiTFM.fsproj``}"
)

let archive =
ZipFile.OpenRead(
VirtualWorkspace.fixtures.valid.``library-with-files-multi-tfm``.bin.Release.``MyLibraryMultiTFM.1.0.0.nupkg``
)

let entries = archive.Entries |> Seq.map (fun entry -> entry.FullName) |> Seq.toList

Assert.That(entries, Contains.Item("fable/Entry.fs"))

Assert.That(entries, Contains.Item("fable/MyLibraryMultiTFM.fsproj"))
}
5 changes: 5 additions & 0 deletions tests/Workspace.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ type Workspace = RelativeFileSystem<".">

type VirtualWorkspace = VirtualFileSystem<".",
"""
temp/
fixtures/
valid/
library-with-files/
bin/
Release/
MyLibrary.1.0.0.nupkg
library-with-files-multi-tfm/
bin/
Release/
MyLibraryMultiTFM.1.0.0.nupkg
""">
7 changes: 7 additions & 0 deletions tests/fixtures/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project>
<!-- Disable lock files for tests projects -->
<PropertyGroup>
<RestorePackagesWithLockFile>false</RestorePackagesWithLockFile>
<RestoreLockedMode>false</RestoreLockedMode>
</PropertyGroup>
</Project>
6 changes: 6 additions & 0 deletions tests/fixtures/Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<!-- Disable central package version of tests projects -->
<PropertyGroup>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
</Project>
8 changes: 8 additions & 0 deletions tests/fixtures/invalid/MissingFableTargetMultiTFM.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<FablePackageType>binding</FablePackageType>
</PropertyGroup>
<Import Project="./../Fable.Package.SDK.Imports.props" />
</Project>
27 changes: 0 additions & 27 deletions tests/fixtures/invalid/packages.lock.json

This file was deleted.

3 changes: 3 additions & 0 deletions tests/fixtures/valid/library-with-files-multi-tfm/Entry.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module MyLibrary

let answer = 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<FablePackageType>library</FablePackageType>
<PackageTags>fable-javascript</PackageTags>
</PropertyGroup>
<ItemGroup>
<Compile Include="Entry.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Fable.Package.SDK">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
27 changes: 0 additions & 27 deletions tests/fixtures/valid/library-with-files/packages.lock.json

This file was deleted.

9 changes: 9 additions & 0 deletions tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
"version": 2,
"dependencies": {
"net8.0": {
"BlackFox.CommandLine": {
"type": "Direct",
"requested": "[1.0.0, )",
"resolved": "1.0.0",
"contentHash": "dSW7uLetl021HQXKcZd1xrXPjhsXgaJ5U4tFe64DLja1KZ2Ce6QeugHvZDvLfcPkEc1ZPRF7fWv5/T+X3ThWTA==",
"dependencies": {
"FSharp.Core": "4.2.3"
}
},
"EasyBuild.FileSystemProvider": {
"type": "Direct",
"requested": "[0.3.0, )",
Expand Down
Loading