weavejester / environ

Library for managing environment variables in Clojure
924 stars 71 forks source link

File .lein-env is emptied by lein uberjar command #76

Closed ILoveHubGit closed 6 years ago

ILoveHubGit commented 6 years ago

I'm using environ "1.1.0" as dependency and lein-environ "1.1.0" as plugin. If I run my project with lein run the information from my profiles.clj is copied to a file .lein-env and the application runs as expected. However if I try to build the jar file (lein uberjar) the file .lein-env is left emptied which result in a "java.lang.NumberFormatException: null, compiling:(" error because the environment variables can't be found. Am I missing some dependency or some setting in my project.clj?

weavejester commented 6 years ago

Why do you expect the execution environment to be present when you're compiling?

ILoveHubGit commented 6 years ago

I don't, but when I start "lein run" the file ".lein-env" is filled with information from "profiles.clj". However, when I start "lein uberjar" the ".lein-env" is emptied and I get the error during compiling my environment.clj. The only thing that is happening in this file is reading the environment variables. Maybe because I do a direct "Integer/parseInt" in this file it complains about the null value of a environment variable since ".lein-env" only has {} as content. If it isn't ".lein-env" which is checked during compiling, what does then cause this error?

weavejester commented 6 years ago

Environ gives you access to the runtime environment, but during compilation you can't rely on the environment being in place. If you have something like:

(def port
  (Integer/parseInt (env :port)))

Then when you're compiling you're going to run into trouble, because that def will be evaluated. Instead, put the port parsing inside a let, a function, or a delay.

ILoveHubGit commented 6 years ago

Thanks a lot that was exactly what I did. So I changed them all to functions and now call them in my app with (port). With lein run my app works as designed. I can compile it now with lein uberjar :-) However if I try to start my app with java -Dconf=my-config.edn -jar myapp.jar I get an exception (java.lang.NumberFormatException) on my port function

(def port
  (Integer/parseInt (env :port)))

My my-config.edn file looks like this with some more options on which my-app depends

{:port 3080
 :logid "app1"}

This logid is used in the logname, however running it as "java -Dconf..." the log file name is __.log. So the config file is not available to my app when starting the jar file. My goal is to develop an application which behavior depends on the information in the config file.

weavejester commented 6 years ago

I don't understand why you think -Dconf=myconfig.edn would work. Environ doesn't have any functionality to handle that.

ILoveHubGit commented 6 years ago

Thanks for your answer. I'll replace environ with cprop in my project so I can achive my goal.