"Type JsonProvider is not defined" in fsx script - f#

In my .fsx script I have:
#r "nuget: FSharp.Data"
open FSharp.Data
type appSettings = JsonProvider<"C:\\Users\\me\\schema\\sample.json">
This generates an error of Type JsonProvider not defined both in the IDE and when I build it using dotnet fsi. How can I fix this?

Related

Fsharpc cannot generate fsi files

I am trying to generate the fsi file for a particular fs file.
Config.fs:
namespace Web2
open FSharp.Data
module Config =
type JsonConfig =
JsonProvider<Sample="./config.json">
let config =
JsonConfig.Load("./config.json")
Generating the signature file:
fsharpc --sig:Config.fsi Config.fs
Error:
Microsoft (R) F# Compiler version 10.2.3 for F# 4.5
Copyright (c) Microsoft Corporation. All Rights Reserved.
Config.fs(8,5): error FS0039: The type 'JsonProvider' is not defined.
Config.fs(8,5): error FS0039: The type 'JsonProvider' is not defined.
Config.fs(11,16): error FS0039: The field, constructor or member 'Load' is not defined.
Not sure what I am doing wrong.
The issue is that the code in Config.fs depends on JsonProvider which comes from the FSharp.Data.dll library - but when you invoke the compiler, you are not specifying the dependency.
You can do that by using the -r command line option:
fsharpc -r:whatever/folder/FSharp.Data.dll --sig:Config.fsi Config.fs
In general, if you are compiling a project, the references are specified in the project file and so you do not need to specify them elsewhere. But when you're invoking the compiler directly, you need to do that.

Using anonymous records gives syntax error: FS0010: Unexpected symbol '|' in expression

I'm eager to use F# 4.6 anonymous records, but the compiler doesn't seem to "get it". How can I specify that I'm using 4.6? I've tried a lot of googling, but there's no documentation on how to actually tell the compiler to use the newest version(?).
Starting a new dotnet new console -lang F# -o src/App, and trying to make a dummy example:
open System
[<EntryPoint>]
let main argv =
let myrec = {| X=3; Y=4 |}
printfn "Hello World from F#! test %A" myrec
0 // return an integer exit code
Doesn't work because:
src/App/Program.fs(5,18): error FS0010: Unexpected symbol '|' in expression [src/App/App.fsproj]
src/App/Program.fs(5,17): error FS0604: Unmatched '{' [src/App/App.fsproj]
my App.fsproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
Turns out it was my .NET Core Runtime that was too far behind. I didn't install the very most recent version because of the unstability warning for MacOS, but I now see that you need the MacOS-unstable version for it to support F# 4.6.
The solution was to just go to https://dotnet.microsoft.com/download/dotnet-core/2.2 and install the most recent version anyway!

Why am I getting F# error FS0039: The namespace or module 'Http' is not defined

In Visual Studio 2015 and 2017, I'm trying out the Http class from several F# examples in FSharp Interactive, and I keep getting:
error FS0039: The namespace or module 'Http' is not defined
Here's the sample:
open FSharp.Data
let response = Http.RequestString("http://api.themoviedb.org/3/search/movie", silentHttpErrors = true)
This is clearly due to the version of FSharp.Data. Is there a way to specify the correct version for FSharp Interactive? What version of FSharp.Data contains the Http module?
Based on the comments, I put together a script to install Paket, intitialise it and then optionally install the dependencies whenever the script was run.
/// install.paket.fsx
open System
open System.IO
printfn "Initialising..."
Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
// invisibly run a command (paket.exe in this case)
let init paket =
let psi = new System.Diagnostics.ProcessStartInfo(paket)
psi.Arguments <- "init"
psi.UseShellExecute <- false
let p = System.Diagnostics.Process.Start(psi)
p.WaitForExit()
p.ExitCode
if not (File.Exists "paket.exe") then
printfn "installing paket"
let url = "http://fsprojects.github.io/Paket/stable"
use wc = new Net.WebClient()
let tmp = Path.GetTempFileName()
let stable = wc.DownloadString(url)
wc.DownloadFile(stable, tmp)
File.Move(tmp,Path.GetFileName stable)
printfn "paket installed"
System.Threading.Thread.Sleep(100)
printfn "initialising paket"
init "paket.exe" |> ignore
System.Threading.Thread.Sleep(200)
printfn "paket initialised"
else
printfn "paket already exists"
/// install.dependencies.fsx
open System.IO
printfn "Installing dependencies"
System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
#r "paket.exe"
open Paket
let dependencies = Paket.Dependencies.Locate(__SOURCE_DIRECTORY__)
printfn "%s" dependencies.DependenciesFile
if not (File.Exists "packages/Newtonsoft.Json/lib/net40/Newtonsoft.Json.dll") then
printfn "installing nuget depenencies"
// either use the dependencies.Install to add dependencies in the paket.dependencies file
//dependencies.Install true |> ignore
// or install them by name
// I remove the existing versions
dependencies.Remove "FSharp.Data"
dependencies.Remove "Newtonsoft.Json 8.0.3"
// then add them (because I'm pedantic about the way the dependencies file looks)
dependencies.Add "FSharp.Data"
dependencies.Add "Newtonsoft.Json 8.0.3"
printfn "nuget depenencies installed"
else
printfn "nuget depenencies already exist"
printfn "Dependencies installed"
Note the use of 8.0.3 for Newtonsoft.Json, The latest version brings over 20 additional dependencies, so I've found a nice old version that's very self contained. You may leave the version number out if you want the latest.
Then, I use these scripts in a shared utilities.fsx for reusable functionality
System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
#load "install.paket.fsx"
#load "install.dependencies.fsx"
#r "packages/fsharp.data/lib/net40/fsharp.data.dll"
#r "packages/Newtonsoft.Json/lib/net40/Newtonsoft.Json.dll"
open FSharp.Data
open FSharp.Data.HtmlAttribute
open FSharp.Data.HtmlNode
open FSharp.Data.HttpRequestHeaders
open Newtonsoft.Json
open System.Net
open System.IO
// utilities like authentication, Http requests and JSON (de)serialization
Finally, I reference the whole lot in my target script by just loading utilities:
System.Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
#load "utilities.fsx"
open Utilities
which takes care of all the dependencies. These can be run from Visual studio with the Alt + Enter key combination or from the command line with fsi.exe MyScript.fsx

System.create error in F# Akka.NET

The following line:
let system = System.create "MyActorSystem" <| Configuration.load ()
... produces this output in the F# Interactive window:
Binding session to 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll'...
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: Could not load type 'Newtonsoft.Json.Converters.DiscriminatedUnionConverter' from assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.
at Akka.Serialization.NewtonSoftJsonSerializer..ctor(ExtendedActorSystem system)
--- End of inner exception stack trace ---
In my project references, Newtonsoft.Json points to .\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll. I've read that I need version 7 or above of Newtonsoft.Json for Akka.NET, so I'm not sure why I'm seeing binding for version 4.5 in the FSI output; is that a false correlation that I should just ignore?
I thought that another copy of the library file might be loading, so in C:\Program Files (x86)\Microsoft SDKs\F#\4.0\Framework\v4.0\FsiAnyCPU.exe.config, I added:
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.1.0" />
... but then I just get error FS0192: internal error: convMethodRef: could not bind to method in FSI.
I've tried looking in the fusion logs; binding succeeds for the Newtonsoft.Json library, so nothing fishy going on, there.
Anyone else run into this problem?
EDIT: OK so I discovered in the fusion logs that another copy of Newtonsoft.Json.dll, in C:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\, was intercepting binding. How do I prevent this without simply removing the library file from the Blend folder?
I think your problem is that your not loading the Json.NET from your packages folder:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll
write at the top of the script
#I __SOURCE_DIRECTORY__
this will add the source directory on the probing paths.
your references should resolve correctly.

compiling F# on linux with TPL

I am pretty feeling pretty stumped with this issue... i am trying to compiling an F# prog that uses the TPL. I am using an up to data verson of mono which compiles C# and TPL.
The F# project is for a uni, which need to be run on there machines, so i have limited access i.e. no sudo rights. it is also commandline driven
These are errors generated:
Microsoft (R) F# 2.0 Compiler build 2.0.0.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
/home/msc/sg315/fs/MapSkel.fs(12,23): error FS0039: The namespace 'Tasks' is not defined
/home/msc/sg315/fs/MapSkel.fs(23,5): error FS0039: The namespace or module 'Parallel' is not defined
/home/msc/sg315/fs/MapSkel.fs(31,3): error FS0039: The namespace or module 'Parallel' is not defined
/home/msc/sg315/fs/MapSkel.fs(44,5): error FS0039: The namespace or module 'Parallel' is not defined
/home/msc/sg315/fs/MapSkel.fs(52,12): error FS0039: The namespace or module 'Parallel' is not defined
/home/msc/sg315/fs/MapSkel.fs(55,12): error FS0039: The namespace or module 'Parallel' is not defined
/home/msc/sg315/fs/MapSkel.fs(61,12): error FS0039: The namespace or module 'Parallel' is not defined
/home/msc/sg315/fs/MapSkel.fs(68,12): error FS0039: The namespace or module 'Parallel' is not defined
/home/msc/sg315/fs/MapSkel.fs(77,13): error FS0039: The namespace or module 'Parallel' is not defined
/home/msc/sg315/fs/BHList.fs(60,14): error FS0039: The field, constructor or member 'ReadLines' is not defined
-bash-4.1$
It obviously needs library references but have no idea which ones...
Thanks
As in your previous question Compiling issues with mono and multi-threaded application, the problem is that you are targeting a version of .NET which is too old.
You can download the new compiler source here http://github.com/fsharp/fsharp. Newer versions of the compiler automatically target a more moder .NET. This can be compiled and installed into a home directory (no root required).

Resources