Help us understand the problem. What is going on with this article?

[Elixir] System.get_env/2で読みだした環境変数をModule attributeに入れておいて一度実行したあと、環境変数を書き換えてもModule attributeの値が変わらないなあとおもったら、それ再コンパイルが必要

はじめに

インストール

  • まずはElixirをインストールしましょう
  • 手前味噌ですが、インストール等ご参考にしてください

mix new

  • 適当にプロジェクトを作ります
$ mix new hello_env
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/hello_env.ex
* creating test
* creating test/test_helper.exs
* creating test/hello_env_test.exs

Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:

    cd hello_env
    mix test

Run "mix help" for more commands.

System.get_env/2で環境変数を読み出してModule attributeに格納しておくモジュールを作ります

lib/hello_env.ex
defmodule HelloEnv do
  @greet System.get_env("AWESOME_ENVIRONMENT_VARIABLE")

  def greet do
    @greet
  end
end

環境変数を設定します

  • 私はAppleに言われるままにzshを使っています
  • お使いの環境にあわせていい感じのファイルに書いてください(~/.profileや~/.bash_profile等)
.zshenv
export AWESOME_ENVIRONMENT_VARIABLE="I was born to love Elixir."

実行します

$ source ~/.zshenv
$ cd hello_env
$ iex -S mix      
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Compiling 1 file (.ex)
Generated hello_env app
Interactive Elixir (1.10.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> HelloEnv.greet
"I was born to love Elixir."
  • いいね :thumbsup:

AWESOME_ENVIRONMENT_VARIABLEを変えたくなりました

  • IExを終わらせます
iex(2)> System.halt
  • 環境変数を変更します
.zshenv
export AWESOME_ENVIRONMENT_VARIABLE="I was born to love Elixir. We are the Alchemists, my friends."
$ source ~/.zshenv
$ iex -S mix      
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Interactive Elixir (1.10.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>  HelloEnv.greet  
"I was born to love Elixir."
  • あれ!?、変更内容が反映されていないなあ

解決法①

iex(2)> recompile force: true
Compiling 1 file (.ex)
Generated hello_env app
:ok
iex(3)> HelloEnv.greet      
"I was born to love Elixir. We are the Alchemists, my friends.

解決法②

$ source ~/.zshenv
$ mix clean
$ iex -S mix      
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Compiling 1 file (.ex)
Generated hello_env app
Interactive Elixir (1.10.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> HelloEnv.greet
"I was born to love Elixir. We are the Alchemists, my friends."

解決法③(ダサいけどいつも私がやっていたこと)

  • lib/hello_env.exがコンパイルエラーを起こすように変なことをしておく
iex -S mix
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Compiling 1 file (.ex)

== Compilation error in file lib/hello_env.ex ==
** (SyntaxError) lib/hello_env.ex:24: unexpected token: end
    (elixir 1.10.4) lib/kernel/parallel_compiler.ex:304: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
  • コンパイルが通るようにする
iex -S mix
Erlang/OTP 23 [erts-11.0.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Compiling 1 file (.ex)
Interactive Elixir (1.10.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> HelloEnv.greet
"I was born to love Elixir. We are the Alchemists, my friends."

Wrapping Up :qiitan:

fukuokaex
エンジニア/企業向けにElixirプロダクト開発・SI案件開発を支援する福岡のコミュニティ
https://fukuokaex.fun/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away