Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate keys in *.app env entry causes -config option to be ignored #5877

Closed
jesperes opened this issue Apr 11, 2022 · 7 comments · Fixed by #5878
Closed

Duplicate keys in *.app env entry causes -config option to be ignored #5877

jesperes opened this issue Apr 11, 2022 · 7 comments · Fixed by #5878
Assignees
Labels
bug Issue is reported as a bug in progress team:VM Assigned to OTP team VM

Comments

@jesperes
Copy link
Contributor

jesperes commented Apr 11, 2022

Describe the bug
If an OTP application has duplicate keys in the env sections of it's *.app file, any file specified using the -config option to erl is ignored, and instead value of the last duplicate key is used.

To Reproduce

  1. Create a minimal OTP application, e.g. using rebar3 new testapp
  2. Add a duplicate key to *.app.src:
  {env,[ {key1, '00_this_value_comes_from_app_env'}
       , {key1, '00_this_is_a_duplicate_value_from_app_env'}
       ]},
  1. Create a sys.config file with an override:
[{testapp,
  [{key1, '10_this_value_comes_from_sysconfig'}]}
].
  1. Run erl and print the app env:
rebar3 compile
export ERL_LIBS=_build/default/lib
erl -noshell -config sys.config -eval 'application:start(testapp), io:format("~p~n", [application:get_all_env(testapp)]).' -s erlang halt
  1. The value of key1 is the duplicate value in testapp.app.src, not the override in the sys.config file.
12:51 $ ./run.sh 
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling testapp
[{key1,'00_this_is_a_duplicate_value_from_app_env'}]
  1. If you comment out the duplicate value in testapp.app.src:
  {env,[ {key1, '00_this_value_comes_from_app_env'}
       %% , {key1, '00_this_is_a_duplicate_value_from_app_env'}
       ]},

Then the override takes precedence as expected:

12:51 $ ./run.sh 
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling testapp
[{key1,'10_this_value_comes_from_sysconfig'}]

Expected behavior
According to the docs:

Configuration parameter values in a configuration file or file descriptor override the values in the application resource files (see app(4)). The values in the configuration file are always overridden by command-line flags (see erts:erl(1)).

So, I would expect the value key1 to always be 10_this_value_comes_from_sysconfig, overriding any value in testapp.app.src.

Affected versions
I've been able to reproduce this in OTP-22.3.4.20, OTP-23.3.4.2, OTP-24.1.3, OTP-24.2.1, OTP-25.0-rc1

@jesperes jesperes added the bug Issue is reported as a bug label Apr 11, 2022
@jesperes
Copy link
Contributor Author

Admittedly, having duplicate keys in *.app files can be considered a bug the application itself, and I would be fine with OTP just picking any of the duplicate keys, but ignoring any -config option just because there is a duplicate key is a (very) blatant violation of "the principle of least astonishment".

@garazdawi
Copy link
Contributor

So what happens is this:

> Keys = application_controller:merge_app_env([{key1,app1},{key1,app2}],[{key1,config1}]).
[{key1,config1},{key1,app2}].
> [ets:insert(ac_tab, Tuple) || Tuple <- Keys].
[ok, ok].

That is, the config value gets merged into the first key and then the first key gets overwritten by the second key when the environment is inserted in the application_controller table.

This is obviously a bug, but I'm unsure what to do about it. Either reject any .app file with duplicate keys defined, or normalize the .app env values before merging with the sys.config value.

@jesperes
Copy link
Contributor Author

Reject, I would say. The -config option already does this:

11:09 $ erl -config dups.config
{"could not start kernel pid",application_controller,"invalid config data: application: kdb; duplicate parameter: key1"}
could not start kernel pid (application_controller) (invalid config data: application: kdb; duplicate parameter: key1)

Crash dump is being written to: erl_crash.dump...done

@jesperes
Copy link
Contributor Author

Although rejecting might have some backwards compatibility issues which hits existing applications.

@rickard-green rickard-green added the team:VM Assigned to OTP team VM label Apr 11, 2022
@garazdawi
Copy link
Contributor

I did not realize that -config already rejected duplicate keys. Seems reasonable that .app files should follow the same logic.

@garazdawi
Copy link
Contributor

A potential fix is available in #5878. I'm not sure if I want to make this part of Erlang/OTP 25, or leave it for 26. It has just missed the window for the last rc and we don't like to add additional potentially breaking changes after the final rc.

@jesperes
Copy link
Contributor Author

I'm fine with leaving it for OTP 26.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue is reported as a bug in progress team:VM Assigned to OTP team VM
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants