FSharp.Data icon indicating copy to clipboard operation
FSharp.Data copied to clipboard

CSV type provider throws "error FS0039: The type <x> does not define the field, constructor or member <y>"

Open bmitc opened this issue 4 years ago • 3 comments

Problem

I am unable to get the CSV type provider to work. Even the example in the official documentation doesn't work, which showcases the same problem I am having. The problem is that the properties of the generated type are not recognized in FSI, whether directly using FSI or loaded using a script. When developing an .fsx script in Visual Studio, intellisense correctly finds the right properties (such as Date below) and Visual Studio shows no errors in the file. But the script is unable to be executed in FSI.

Versions

  • F# 5.0
  • F# Interactive version 11.0.0.0 for F# 5.0

To Reproduce

  1. Navigate here.
  2. Download the MSFT.csv file referenced.
  3. Open an FSI session in the directory of the file.
  4. Follow the below script, which follows the steps in the documentation. (Note that Load doesn't use the working directory.:
    > #r "nuget: FSharp.Data";;
    [Loading C:\Users\<username>\AppData\Local\Temp\nuget\22296--41ea47a6-da20-4b8e-b398-5e416442c1a9\Project.fsproj.fsx]
    namespace FSI_0002.Project
    
    > open FSharp.Data;;
    > type Stocks = CsvProvider<"./MSFT.csv">;;
    type Stocks = CsvProvider<...>
    
    > let msft = Stocks.Load("C:/Users/bmitc/Downloads/MSFT.csv");;
    val msft : CsvProvider<...>
    
    > msft;;
    val it : CsvProvider<...> =
      FSharp.Data.Runtime.CsvFile`1[System.String]
        {Headers = Some [|"Date"; "Open"; "High"; "Low"; "Close"; "Volume"|];
         NumberOfColumns = 6;
         Quote = '"';
         Rows = seq ["9-Oct-17"; "6-Oct-17"; "5-Oct-17"; "4-Oct-17"; ...];
         Separators = ",";}
    
    > let firstRow = msft.Rows |> Seq.head;;
    val firstRow : CsvProvider<...>.Row = "9-Oct-17"
    
    > let lastDate = firstRow.Date;;
    
      let lastDate = firstRow.Date;;
      ------------------------^^^^
    
    stdin(7,25): error FS0039: The type 'Row' does not define the field, constructor or member 'Date'.
    

bmitc avatar Dec 19 '20 08:12 bmitc

In my case it was compile-time error: F# compiler didn't find sample CSV files from which to construct the CSV type provider.

It started working after I have set absolute file path to document when defining CsvProvider type (i.e. the first path).

pkese avatar Oct 23 '21 22:10 pkese

type Stocks = CsvProvider< const(__SOURCE_DIRECTORY__ + "/MSFT.csv") > is how I do it, which works with all versions of compilers and the library.

smoothdeveloper avatar Oct 24 '21 06:10 smoothdeveloper

It would be really nice if the compiler would provide some meaningful message explaining that it couldn't find the file, because the current error report is closer to misleading than helpful:
error FS0039: The type <x> does not define the field, constructor or member <y>

pkese avatar Oct 24 '21 20:10 pkese