Issues using opam libraries


I am new to OCaml and am trying to “import” an OPAM package I installed.
I am not sure how this is done.

I have a few lines of code in my ~/.ocamlinit which I am assuming allow me to run the following code:
#require “ez_file”;;
FileString.read_file “data.json”;;

This works perfectly on the REPL but I am not able to make it run as a project.
I have a project set up using:

  • VSCode WSL on Ubuntu 20.04 on Windows 10
  • dune set up

When I run a simple hello world code, this set up is working fine.
However when i use the code I used in REPL (given above) I get an error on the “#”

I am really confused as to how do I use this library in my project?

Adding few more details I forgot to add which might be helpful:

  • I am using the vscode OCaml Platform extension
  • I am running the project with the command “dune exec ./app.exe”

When i use the code: #require “ez_file”;;
I get an error on the “#”

When I instead use: open Ez_File;;
I get the unbound module error

I am sorry but this is extremely confusing for me :frowning: and I am not sure if I am able to explain the problem properly or not

Hi @sagarjs,

Welcome to OCaml :slight_smile: ! It sounds like this might be a problem with your dune file. When building your app.exe with dune you need to also specify the libraries it uses in a dune file. Something like the following should work:

 (name app)
 (libraries ez_file))

This tells dune that will be the entry-point for your code and that you will be using the ez_file library.

And in your file

let () = 
  print_string (FileString.read_file "data.json")

So your directory will look like:

|-- data.json
`-- dune

From the ez_file documentation we can see the exposed modules. FileString is one of them hence we don’t need to open EzFile (note there is no underscore unlike the library name). The #require directive is used in the REPL to include those packages so you can use them as you described but it is not part of the OCaml syntax, it is for the REPL.

Hope this helps :slight_smile:

My code is working now.

However VSCode is giving me an unbound module error on FileString
Running dune exec ./app.exe still works fine though.

Am I missing something in my vscode setup?

Glad it worked. To use VS Code Platform make sure you have done the following:

  1. Installed the correct extension for VS Code.
  2. Install LSP Server - this hasn’t been officially released on opam yet so you’ll need to pin it to the Github repository:
$ opam pin add ocaml-lsp-server
$ opam install ocaml-lsp-server

Note as well you’ll need to run dune build or similar to generate the appropriate files to makes this work. Also make sure you select the correct opam switch and maybe even restart the language server.

To do this open the command palette (on Mac this is cmd + p) and type > OCaml: select... to select the opam switch and > OCaml: Restart Language Server to do that.

Hopefully with all this information you will be able to get it up and running :slight_smile:

Selecting the switch again and restarting the language server made it work!

Thanks a lot!

Now I can do some coding :smiley:

1 Like