Closed szabolcsmaj closed 8 years ago
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
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
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
:Step 5:
Run the following:
So far so good!
Now do Step 3 with the following:
config :potato, the_name: Enum.join(["pot","oooooooo"])
Do Step 5 (
deps.get
andcompile
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:
Now don't let that last tarball stuff fool you, the issue is in
sys.config
atrel/potato/releases/0.1.0/sys.config
: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?