** (KeyError) key “SECRET_KEY_BASE” not found in: %{“Apple_PubSub_Socket_Render” =>

phoenix
testing
troubleshooting

#1

I am receiving error below when I run $ mix test. Also this error shows consistently on “Problems” tab VS code editor. Appreciate your help and hanks.

By the way I am in Dev. environment on my local machine. Deployed code is working fine but some inconsistency…

** (KeyError) key "SECRET_KEY_BASE" not found in: %{"Apple_PubSub_Socket_Render" => "/private/tmp/com.apple.launchd.32ceukwcV7/Render", "BINDIR" => "/usr/local/Cellar/erlang/21.2.4/lib/erlang/erts-10.2.3/bin", "EMU" => "beam", "HOME" => "/Users/myname", "LANG" => "en_US.UTF-8", "LOGNAME" => "myname", "PATH" => "/usr/local/Cellar/erlang/21.2.4/lib/erlang/erts-10.2.3/bin:/usr/local/Cellar/erlang/21.2.4/lib/erlang/bin:/usr/local/opt/sqlite/bin:/usr/local/opt/openssl/bin:/usr/local/opt/sqlite/bin:/usr/local/opt/openssl/bin:/usr/local/opt/icu4c/sbin:/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin:/usr/local/opt/icu4c/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/opt/sqlite/bin:/usr/local/opt/openssl/bin:/usr/local/opt/icu4c/sbin:/usr/local/opt/icu4c/bin", "PROGNAME" => "erl", "PWD" => "/Users/myname/MyApp/MyappApi", "ROOTDIR" => "/usr/local/Cellar/erlang/21.2.4/lib/erlang", "SHELL" => "/bin/bash", "SHLVL" => "2", "SSH_AUTH_SOCK" => "/private/tmp/com.apple.launchd.rZGnGqtT6S/Listeners", "TERM" => "xterm-256color", "TERM_PROGRAM" => "vscode", "TERM_PROGRAM_VERSION" => "1.30.2", "TMPDIR" => "/var/folders/y3/nflmwvxj58b4xrlcs9m4trym0000gn/T/", "USER" => "myname", "XPC_FLAGS" => "0x0", "XPC_SERVICE_NAME" => "0", "__CF_USER_TEXT_ENCODING" => "0x1F5:0x0:0x0"}
    (stdlib) :maps.get("SECRET_KEY_BASE", %{"Apple_PubSub_Socket_Render" => "/private/tmp/com.apple.launchd.32ceukwcV7/Render", "BINDIR" => "/usr/local/Cellar/erlang/21.2.4/lib/erlang/erts-10.2.3/bin", "EMU" => "beam", "HOME" => "/Users/myname", "LANG" => "en_US.UTF-8", "LOGNAME" => "myname", "PATH" => "/usr/local/Cellar/erlang/21.2.4/lib/erlang/erts-10.2.3/bin:/usr/local/Cellar/erlang/21.2.4/lib/erlang/bin:/usr/local/opt/sqlite/bin:/usr/local/opt/openssl/bin:/usr/local/opt/sqlite/bin:/usr/local/opt/openssl/bin:/usr/local/opt/icu4c/sbin:/usr/local/opt/icu4c/bin:/usr/local/opt/icu4c/sbin:/usr/local/opt/icu4c/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/opt/sqlite/bin:/usr/local/opt/openssl/bin:/usr/local/opt/icu4c/sbin:/usr/local/opt/icu4c/bin", "PROGNAME" => "erl", "PWD" => "/Users/myname/MyApp/MyappApi", "ROOTDIR" => "/usr/local/Cellar/erlang/21.2.4/lib/erlang", "SHELL" => "/bin/bash", "SHLVL" => "2", "SSH_AUTH_SOCK" => "/private/tmp/com.apple.launchd.rZGnGqtT6S/Listeners", "TERM" => "xterm-256color", "TERM_PROGRAM" => "vscode", "TERM_PROGRAM_VERSION" => "1.30.2", "TMPDIR" => "/var/folders/y3/nflmwvxj58b4xrlcs9m4trym0000gn/T/", "USER" => "myname", "XPC_FLAGS" => "0x0", "XPC_SERVICE_NAME" => "0", "__CF_USER_TEXT_ENCODING" => "0x1F5:0x0:0x0"})
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
    (stdlib) erl_eval.erl:888: :erl_eval.expr_list/6
    (stdlib) erl_eval.erl:240: :erl_eval.expr/5
    (stdlib) erl_eval.erl:232: :erl_eval.expr/5
    (stdlib) erl_eval.erl:233: :erl_eval.expr/5
    (stdlib) erl_eval.erl:888: :erl_eval.expr_list/6
    (stdlib) erl_eval.erl:411: :erl_eval.expr/5

#2

This seems as if something in your application tries to read from the systems environment and the variable it tries to read does not exist.

You need to search for that place in your codebase.

I’d start with the output of git grep SECRET_KEY_BASE.


#3

Thanks much for your reply and here is git grep output

git grep SECRET_KEY_BASE
config/prod.exs:secret_key_base: Map.fetch!(System.get_env(), “SECRET_KEY_BASE”),
config/test.exs: secret_key_base: Map.fetch!(System.get_env(), “SECRET_KEY_BASE”),


#4

This proofes my theory. During prod and during test builds, it tries to read that from the systems env-vars.

Basically, it is written in a way to fail if that env_var does not exist.

On the other hand side, I’m wondering why its not necessary in dev, but in test to fetch it from the env. this only is known by the devs of the project.

I’d ask them and try to convince them to use a static value for testing. Thats usually not problem to do…


#5

Thanks much for quick response!

Somehow when I pushed static values to Heroku, it did NOT take them and I had to change test config and maintain env-vars for Heroku which is test environment. (I will try again with static values for test on Heroku)

Since then this error is shown on my dev - (my localhost) popping up, and app works as intended, but $ mix test stops with this error message. Any advise.


#6

Perhaps use System.get_env/2 and provide a default value or set the environment variable on your device machine.


#7

That’s a good idea and i will try. Thanks much for great suggestion.