I'm trying to use FAKE to build my F# project.
The build.fsx looks like below and works fine.
#r "packages/FAKE/tools/FakeLib.dll"
open Fake
Target "Default" (fun _ ->
trace "Hello World from FAKE"
)
RunTargetOrDefault "Default"
Then I want to use fsc from FAKE. Following the official tutorial, I added one line open Fake.FscHelper and get below error message:
#r "packages/FAKE/tools/FakeLib.dll"
open Fake
open Fake.FscHelper
// this value is not a function and can not be applied
// union case FscParam.Target: TargetType -> FscParam
Target "Default" (fun _ ->
~~~~~~~~~~~~~~~~
trace "Hello World from FAKE"
)
RunTargetOrDefault "Default"
I appreciate if anyone can give me any advice.
I'm using VS Code on Mac with Mono 4.2.1.
And my paket.lock looks like below:
NUGET
remote: https://www.nuget.org/api/v2
specs:
FAKE (4.21.0)
FSharp.Core (4.0.0.1)
FsUnit (2.0.0)
FSharp.Core (>= 3.1.2.5)
NUnit (3.0.1)
NUnit (3.0.1)
This happens because the module FscHelper defines a constructor called Target (see source), and that constructor conflicts with the Target function from the TargetHelper module. There is an issue filed about it.
Until the issue is fixed, there are three ways to work around this ambiguity:
Don't open FscHelper, just use all its innards in a qualified manner (e.g. FscHelper.Compile etc.)
Re-alias the TargetHelper.Target function in the local scope:
open Fake
open Fake.FscHelper
let Target = TargetHelper.Target
Target "Default" (fun _ ->
trace "Hello World from FAKE"
)
Reorder the open statements:
open Fake.FscHelper
open Fake
And since you're using this helper, note that the documentation for it is outdated. In particular, the Fsc task is deprecated in favor of the Compile task (see source).
Change the order of the open statements
#r #"packages/FAKE/tools/FakeLib.dll"
open Fake.FscHelper
open Fake
Target "a" (fun _ ->
["a.fs"] |> Compile []
The order of your open statements determines the precedence of the name resolution with the later opened modules and namespaces taking precedent.
Related
I have installed the MathNet.Numerics package on Visual Studio 2017 using the Package Manager Console.
I have attempted to open a Source File on and execute an algorithm relating to the MersenneTwister type within the MathNet namespace.
However, when I try to generate numbers using this algorithm in F# Interactive, I am met with the error:
File1.fs(3,6): error FS0039: The namespace or module 'MathNet' is not defined. Maybe you want one of the following:
Math
Code is as per below:
module File1
open MathNet.Numerics.Random
let mersenneTwister = new MersenneTwister(42)
let a = mersenneTwister.NextDouble()
Apologies if this is unclear, I am relatively new to F# :)
Are you per change using the interactive window or running an fsx script? Cause these don't recognize your packages.
When reproducing your problem in a F# console app I got this output: Hello 0.374540.
I installed the MathNet.Numerics.fsharp nuget package (which also uses the package you mention) and used the following code in the Program.fs:
open MathNet.Numerics.Random
let hello () =
let mersenneTwister = new MersenneTwister(42)
let a = mersenneTwister.NextDouble()
printfn "Hello %f" a
[<EntryPoint>]
let main argv =
hello ()
0 // return an integer exit code
If you do want to use the nuget package from a script you can reference it in the top of your script like so (absolute or relative)
#r #"C:\Path\bin\Debug\netcoreapp3.0\MathNet.Numerics.dll"
When I add the example xUnit2 target to my FAKE build file, I'm getting this error:
error FS0001: This expression was expected to have type
string option
but here has type
string
Target Example from FAKE xunit2 documentation
Target "Test" (fun _ ->
!! (testDir ## "xUnit.Test.*.dll")
|> xUnit2 (fun p -> {p with HtmlOutputPath = (testDir ## "xunit.html")})
)
The Visual Studio is highlighting the (testDir ## "xunit.html") section of the code.
I understand that it's expecting two parameters, but I don't know enough F# yet to figure out how to fix the problem:
Prior to including the xUnit target, my FAKE build was working fine.
I've added open Fake.Testing.XUnit2 to the build file and I get no error with the xUnit2 reference.
Any help would be appreciated.
So the error is that the type of HtmlOutputPath is
HtmlOutputPath : string option
In Fake I believe that ## does Path.Combine so testDir ## "xunit.html should have type string.
To get the types to match, you can use
HtmlOutputPath = Some(testDir ## "xunit.html")
This suggests that the documentation for FAKE is incorrect.
We're using Fake and I'd like to run DotCover after our Build target. It's alway telling me:
C:\Users\xxxxx\Dev>FAKE\tools\Fake build.fsx
F# Interactive for F# 3.1 (private)
Freely distributed under the Apache 2.0 Open Source License
For help type #help;;
> [Loading C:\Users\xxxxx\Dev\build.fsx]
build.fsx(8,1): error FS0039: The value or constructor 'DotCoverNUnit' is not defined
My short simple "test" script
#r #"FAKE/tools/FakeLib.dll"
open Fake
DotCoverNUnit dotCoverOptions nUnitOptions
What went wrong?
You need to open namespace containing DotCoverNUnit class:
open Fake.DotCover
I'm trying to use the JsonProvider and I'm getting the following error when I call a function on it:
System.TypeInitializationException was unhandled
Message: An unhandled exception of type 'System.TypeInitializationException' occurred in PortableLibrary1.dll
Additional information: The type initializer for '<StartupCode$PortableLibrary1>.$PortableLibrary1' threw an exception.
I have a basic console application as follows:
module Pinit =
open FSharp.Data
type JsonT = JsonProvider<"""..\myFile.json""">
let doc = JsonT.Load("""..\nyFile.json""")
let result = doc.GeneratedAt
[<EntryPoint>]
let main argv =
printfn "%A" Pinit.doc.GeneratedAt
0
When run within the ConsoleApplication it all works as expected.
If I create an F# Portable Class library as follows:
module Pinit =
open FSharp.Data
type JsonT = JsonProvider<"""..\myFile.json""">
let doc = JsonT.Load("""..\nyFile.json""")
let result = doc.GeneratedAt
Create another Console Application and reference that Portable Class Library and call the code as follows:
open PortableLibrary1
[<EntryPoint>]
let main argv =
printfn "%A" Pinit.result
0
When I run the program then it generates the exception defined above:
I'm suspecting it's because of the versions of FSharp.Core but was wondering whether I was doing something wrong or whether there was a way to get this to work?
Versions:
-- ConsoleApplication --
FSharp.Core = 4.3.1.0
-- PortableClassLibrary --
FSharp.Core = 3.3.1.0
FSharp.Data = NuGet Version: 2.0.0
let bindings are compiled into static members, and in .NET when you have an exception in a static initializer the real exception is masqueraded.
If you turn that into a function call by changing to let result() = doc.GeneratedAt and printfn "%A" (Pinit.result()) you'll get the real exception:
Only web locations are supported
Portable profiles don't support accessing the file system. So inside the PCL you can either have web urls or load manually from an embedded resource. See here for an example: https://github.com/ovatsus/Apps/blob/master/Trains/Stations.fs#L65. Or you can load the file with File.ReadAllText in the console project and pass it to the PCL.
This was with a Portable Profile (Legacy) project, which is Profile 47 (and FSharp.Core 2.3.6.0). I then also tested with a Portable Profile project, which is Profile 7 (and FSharp.Core 3.3), and noticed it's not working correctly, it's giving this exception instead
Method not found: 'FSharp.Data.Runtime.JsonValueOptionAndPath FSharp.Data.Runtime.JsonRuntime.TryGetPropertyUnpackedWithPath(FSharp.Data.Runtime.IJsonDocument, System.String)'.
I created a github issue to track that: https://github.com/fsharp/FSharp.Data/issues/521
I am getting this when trying to parse a simple csv string. I am running F# out of VS 2013, the dll says it is version 4.3.0.1 which I thought was F# 3.1. My Fsharp.Data dll is 1.1.10.
I am trying to run this as part of an nunit test using resharper. The snippet does work in interactive mode.
Here is the code:
open FSharp.Data
type TestCsv = CsvProvider<"test,taht\n1,1">
let x = TestCsv.Parse "test,taht\n1,1"
let tests = x.Data |> Seq.map (fun x -> x.test)
tests |> Seq.head
And the result:
System.Exception : Couldn't parse row 1 according to schema: Method not found: 'Microsoft.FSharp.Core.FSharpOption`1<System.String> FSharp.Data.RuntimeImplementation.Operations.AsOption(System.String)'.
Any ideas how to fix this?
FSharp.Data 1.1.10 doesn't support F# 3.1/VS2013. Please try with the prerelease version 2.0.0-alpha3 and let us if that works. Make sure both the unit test project and the library project are using the same version of FSharp.Core (either 4.3.0.0 or 4.3.1.0)