Search code examples
elixirphoenix-framework

Strange syntax error in with..do..else statement


I have a syntax error that I don't know where it comes from. Here is my function (in persona_from_auth.ex):

  # find or create the user.
  # if you login with oauth2, your account will auto created
  def find_or_create(%Auth{provider: :github} = auth) do
    with
      {:notfound} <- check_github_email(auth.info.email),
      {:notfound} <- check_google_email(auth.info.email)
    do
        create(auth)
    else
      {:ok, persona} -> update(auth, persona)
    end
  end

This returns the following error:

== Compilation error on file web/models/persona_from_auth.ex ==
** (SyntaxError) web/models/persona_from_auth.ex:18: syntax error before: do
    (elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

Line 18 is the line before the create() call.

I checked for the correct elixir version. Turned out in mix.exs I had 1.2, I changed that to 1.4.2 but still the same error. Is it possible that compilation still uses 1.2? How do I check that?


Solution

  • The first statement after with must either be on the same line or the arguments need to be in parentheses, or else Elixir thinks you're trying to call with/0 and then the following lines don't make sense, resulting in the syntax error.

    Either of the following should work:

    with {:notfound} <- check_github_email(auth.info.email),
         {:notfound} <- check_google_email(auth.info.email)
    do
    
    with(
      {:notfound} <- check_github_email(auth.info.email),
      {:notfound} <- check_google_email(auth.info.email)
    ) do