4

I am having trouble loading the csv files with FSharp.Data csv provider provided by fslab, including the sample adwords.csv file.

What does this error below mean? Also, when I hover over the code in the Visual studio editor it mentions that "The given key was not present in the dictionary"

Problem example:

#load "packages/FsLab/FsLab.fsx"

open System.IO
open FSharp.Data

"adwords.csv"
|> File.ReadAllLines

let test = CsvProvider<"adwords.csv">.GetSample()

The output:

>
val it : string [] =
 [|"Criteria ID,Name,Canonical Name,Parent ID,Country Code,Target Type,Status";
    "1000010,Abu Dhabi,"Abu Dhabi,Abu Dhabi,United Arab Emirates",9041082,AE,City,Active";
"1000011,Ajman,"Ajman,Ajman,United Arab Emirates",9047096,AE,City,Active";
"1000012,Al Ain,"Al Ain,Abu Dhabi,United Arab Emirates",9041082,AE,City,Active";
"1000013,Dubai,"Dubai,Dubai,United Arab Emirates",9041083,AE,City,Active";
"2004,Afghanistan,Afghanistan,,AF,Country,Active"|]
>
>System.MethodAccessException: Attempt by method '<StartupCode$FSI_0007>.$FSI_0007.main@()' to access method 'FSharp.Data.Runtime.CsvFile`1<System.__Canon>.Create(System.Func`3<System.Object,System.String[],System.__Canon>,
at <StartupCode$FSI_0007>.$FSI_0007.main@() in C:\test.fsx:line 11
Stopped due to error

I ran into this problem with my own files, so I grabbed this sample file from here: https://raw.githubusercontent.com/fsharp/FSharp.Data/master/tests/FSharp.Data.Tests/Data/Adwords.csv

Debug info:

  • If I delete the FSharp.Data library folder (v 2.3.0) and replace with version 2.2.5 it works correctly with no error.
  • If I don't use the FsLab.fsx script and instead use

    #I "packages/FSharp.Data/lib/net40 #r "FSharp.Data.dll" then everything works.

  • The path to the FsLab.fsx script is correct, it runs when I send the line to fsi.
  • The F# version is 14.0.23413.0.
  • The version of FSharp.Data downloaded by FSlab is FSharp.Data.2.3.0.
  • I have no other references in the .fsx script.
  • I am using Visual Studio Community edition 14.0.24720.00 Update 1.
  • .NET version 4.6.01038
  • I am realizing now that I am not getting the popup asking if I want to allow the .dll like I think I used to get when I used this before.
nh2
  • 610
  • 3
  • 10
  • Have you tried to declare a type for the CSV content? `type MyRecords = CsvProvider<"adwords.csv">` `let test = MyRecords.GetSample()` – Bartek Kobyłecki Jun 10 '16 at 15:18
  • Yes. It gives a similar error. – nh2 Jun 10 '16 at 15:21
  • Canonical Name column seems to be displayed strangely by FSI. Maybe it is the cause. Afghanistan doesn't provide any value for Parent ID. If you will place there 0 is there any better? – Bartek Kobyłecki Jun 10 '16 at 15:44
  • Doesn't fix on my end. The file is one of the test files from the github repository. – nh2 Jun 10 '16 at 15:51
  • Is it possible the quotes around Canonical Name are throwing it off? It may be reading each line as 3 separate lines. – Erick Jun 10 '16 at 16:33
  • It shouldn't because this is specifically a test file from the repository's test suites. Regardless, if I change the values of this column to "a" I still get an error. – nh2 Jun 10 '16 at 16:42
  • you could investigate what is being referenced in the FsLab.fsx file. – s952163 Jun 13 '16 at 11:52
  • As far as I can tell the references are the same. In `FsLab.fsx` `#I "../FSharp.Data/lib/net40"` goes up to the packages folder and then into FSharp.Data just like the hand-coded version that works. I'm updating to VS now to see if that helps. – nh2 Jun 13 '16 at 11:59
  • I'll download a new FsLab and see if it behaves differently but these path or version related issues can be difficult to troubleshoot...maybe someone else seen this issue. – s952163 Jun 13 '16 at 13:10
  • it's a shot in the dark but 32-bit FSI or 64-bit FSI behaves the same? – s952163 Jun 13 '16 at 13:14
  • I just grabbed an old version I had on my computer: FsLab.0.3.10 and FSharp.Data 2.2.5. When I copied over the new libraries with the old versions, everything works correctly. – nh2 Jun 13 '16 at 13:54
  • 1
    The important issue is FSharp.Data.2.2.5 vs. 2.3.0. Switching these out fixes my problems. – nh2 Jun 13 '16 at 14:07
  • great. you can add that as the correct answer. – s952163 Jun 13 '16 at 15:03

2 Answers2

1

There is nothing wrong with the file. This for example works:

#load @"..\..\FSLAB\packages\FsLab\FsLab.fsx"

open System.IO
open FSharp.Data

[<Literal>]
let csvFile = @"C:\tmp\adwords.csv"
File.Exists csvFile

type Csv = CsvProvider<csvFile>
let csv = Csv.Load(csvFile)
csv.Rows

There is something wrong with your FsLab of FSharp.Data installation or type providers security maybe. Try the following, specify the path to the file directly. If it still doesn't work just nuget FSharp.Data and try using the csv type provider directly in a new project.

Other info is also helpful. VS version, FSLab version, wha other references you have. etc.

EDIT: Thanks for the debug info. That's actually quite helpful. VS2015 Update 1 broke two things, the Binding Redirect for Fsharp and the type providers (that might have been FSharp Tools, I forgot). I would upgrade to Update 2. If that's not possible please check if your FSharp.Data.TypeProviders.dll is in C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\Type Providers.

As referencing the dlls directly works, it's probably a version mismatch issue. My FsLab install predates VS2015 Update 1 and 2, so will see if it behaves differently with a new download.

s952163
  • 6,276
  • 4
  • 23
  • 47
  • Yes, I agree the file is fine (why I chose it). I added some debug info to the bottom of the question. – nh2 Jun 13 '16 at 10:34
  • @nh2 see my comment in the Edit. – s952163 Jun 13 '16 at 11:44
  • I've upgraded Visual Studio Community edition to Update 2. I still have the same problems. I also confirmed that the typeProviders.dll is in the right spot. – nh2 Jun 13 '16 at 12:21
1

There is some issue with the installation of FSharp.Data currently bundled with FsLab (as of June 2016). This issue is with version 2.3.0. If you instead use FSharp.Data 2.2.5 the code works as expected.

Delete the packages/FSharp.Data folder and replace with version 2.2.5. I did it from an old installation but you could do it from Nuget

nh2
  • 610
  • 3
  • 10