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

loadconfig not clearing application env #13246

Closed
lukaszsamson opened this issue Jan 12, 2024 · 2 comments
Closed

loadconfig not clearing application env #13246

lukaszsamson opened this issue Jan 12, 2024 · 2 comments

Comments

@lukaszsamson
Copy link
Contributor

Elixir and Erlang/OTP versions

Erlang/OTP 25 [erts-13.2.2.4] [source] [64-bit] [smp:12:12] [ds:12:12:10] [async-threads:1]

Elixir 1.16.0 (compiled with Erlang/OTP 25)

Operating system

macOS

Current behavior

When loadconfig is called repeatedly it merges application configs instead of overwriting.
Repro:

  1. create an empty mix app
  2. add config/config.exs
  3. add to config
config :logger, foo: 1
  1. in iex run
Mix.start()
Code.compile_file("mix.exs")
Mix.Task.run("loadconfig")
[
  logger: [foo: 1]
]
Application.get_env(:logger, :foo)         
1
  1. rename :foo to :bar
Mix.Task.run("loadconfig")
[
  logger: [bar: 1]
]
Application.get_env(:logger, :bar)         
1
  1. Verify that old key is still in app env
Application.get_env(:logger, :foo)         
1

Originally reported in https://elixirforum.com/t/how-can-i-prevent-the-lsp-from-invoking-my-custom-mix-compiler/59802/6 and followed up in elixir-lsp/elixir-ls#1030

Expected behavior

I'm not sure this is a bug but there is no mention of this behavior in https://hexdocs.pm/mix/1.16.0/Mix.Tasks.Loadconfig.html. This comment states thet the configs are not deep merged

# Loads compile-time configuration, they support imports, and are not deep merged.

@josevalim
Copy link
Member

I will improve the docs but the behavior you see is correct. The deep merging is only within an application. We don’t erase previously defined application keys, regardless of how they were set.

@josevalim
Copy link
Member

In your case, you may need to delete all application keys from the environment with persistent: true using https://www.erlang.org/doc/man/application#unset_env-2. Then when you call load_config again and load the applications, you go back to the original state.

But this brings up back to previous conversations, where I think it is better to separate the LS runtime from the application runtime. Then to restart the application, you boot a new runtime. It is the best way to guarantee there is no state carried across. Because if you revert the application environment, you revert the supervision trees, people can always change the application environment directly, start named processes outside of supervision trees, etc.

lukaszsamson added a commit to elixir-lsp/elixir-ls that referenced this issue Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants