bitwalker / exrm

Automatically generate a release for your Elixir project!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't generate release if config contains values set by an anonymous function

szabolcsmaj opened this issue · comments

How to generate:

What works:

Step 1:

Create a new project (in /tmp for example):
mix new potato

Step 2:

In mix.exs add to deps:
{:exrm, "~> 1.0"}

Step 3:

In config/config.exs:
config :potato, the_name: "potato"

Step 4:

In lib/potato.ex:

defmodule Potato do
  def say_my_name do
    IO.puts Application.get_env(:potato, :the_name)
  end
end

Step 5:

Run the following:

mix deps.get
mix compile
mix release
rel/potato/bin/potato console
iex(potato@127.0.0.1)1> Potato.say_my_name
potato
:ok

So far so good!

Now do Step 3 with the following:
config :potato, the_name: Enum.join(["pot","oooooooo"])

Do Step 5 (deps.get and compile is not necessary). Still works!

What is problematic:

Now go to config/confix.exs and change the previously added part to this:
config :potato, the_name: fn -> "potato" end

Now run mix release

Result:

...
Cannot add file sys.config to tar file - [{error,{2,erl_parse,["syntax error before: ",["Fun"]]}}]
===> Provider (tar) failed with: {error,
                                         {rlx_prv_archive,
                                          {tar_unknown_generation_error,
                                           "potato","0.1.0"}}}

==> ERROR: "Unknown error occurred when generating tarball for potato-0.1.0\n  Do you have any file names longer than 100 characters? That is a known issue with systools."

Now don't let that last tarball stuff fool you, the issue is in sys.config at rel/potato/releases/0.1.0/sys.config:

[{sasl,[{errlog_type,error}]},
 {potato,[{the_name_is,#Fun<erl_eval.20.52032458>}]}].

The issue:

If a config file has an anonymous(!) function, release task fails to evaluate it.
This issue came up when I used Guardian plugin for authentication and was trying to read the secret key for tokens from a file. You can read about it here. This issue also comes up with Distillery.

Is this intentional or just a bug?

I ran into this as well and I believe it's actually a limitation with Mix.Config.read!/1.

In distillery, that function is used to read the base configuration:
https://github.com/bitwalker/distillery/blob/master/lib/mix/lib/releases/assembler.ex#L558

Then it's merged with the release configuration and written:
https://github.com/bitwalker/distillery/blob/master/lib/mix/lib/releases/assembler.ex#L537

Observations

Create a new project and add a key to the config whose value is an anonymous function:

mix new potato && cd potato
echo "config :potato, my_var: fn -> :ok end" >> config/config.exs

Start an iex session, read the config, and notice the output:

iex> config = Mix.Config.read!("config/config.exs")
[potato: [my_var: #Function<12.3456789/0 in :erl_eval.expr/5>]]

Correct me if I'm wrong, but that function will only exist in the current BEAM instance. We can't serialize/write it and expect it to be valid during a subsequent execution of the program when we read it; it will be redefined at run time in the new BEAM instance.

Edit: Removing the last observation as I misunderstood the purpose of Mix.Config.persist/1.

@pcewing It's not actually a problem with Mix.Config.read!/1, as it is supposed to return the captured function. The issue is that we need to write a function in capture syntax to the sys.config file, so that when it is evaluated by the VM, we get a valid capture back. What happens now is we serialize the capture, which of course is invalid. It's something I have some ideas on how to tackle, but I'll be doing so in distillery rather than exrm.

Thank you @bitwalker! I'll close this since directions are clear --> Switch to distillery