LoginSignup
5
2

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-10-17

はじめに

インストール

  • まずは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:

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2