LSP throwing strange warnings when using File.write

def write_boilerplate(project_name) do
    file_name = project_name <> ".ex"
    file_path = Path.join([project_name, file_name])
    boilerplate = "defmodule #{project_name} do\nend"
    File.write(file_path, boilerplate, mode: :append)
  end

the LSP i use is elixir_ls
what happens is the code runs smoothly but while in neovim i get this strange warning:

The function call will not succeed. File.write(_file_path :: binary(), _boilerplate :: <<_::8, _::size(1)>>, [{:mode, :append}]) breaks the contract (Path.t(), iodata(), [mode()]) :: :ok | {:error, posix()} 

why is this happening? can’t find an answer in the docs.

Hi @donnieparka the error is a bit esoteric but it’s telling you that what you’re passing in doesn’t match what the function expects. Specifically if you look at what it says you’re doing, the part that doesn’t match is that you’re doing [{:mode, :append}] but the function expects [mode()]. That is to say you should just have [:append] not [mode: :append]

thanks a lot i’m still trying to understand how to read elixir docs :grin:
now the error has become even more esoteric:

File.write(_file_path :: binary(), _boilerplate :: <<_::8, _::size(1)>>, :append) will never return since the success typing is: ( binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | char(), binary() | [] ), binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), [any()] ) :: :ok | {:error, atom()} and the contract is (Path.t(), iodata(), [mode()]) :: :ok | {:error, posix()} 

no, ok i was dumb. I had to pass a list:

    File.write(file_path, boilerplate, [:append])
1 Like