env_generator.ml ends up stale and will have let env = "default"
Is there a better built-in method to generate that file?
Is there a way to avoid it being cached and requiring a _build directory cleanup before running MY_ENV .. to avoid stale values?
I make no claims about being standard, there is this example ppx rewriter that injects the compilation-time value of an environment variable. You would still want to declare the dependency for dune though.
(action (write-file foo.ml "let env = \"%{env:MY_ENV=default}\"")) may work if you don’t need special characters to be quoted (especially MY_ENV can’t contain ")
For completeness I’ll mention that on a library level, you need to add (preprocessor_deps (env_var MY_ENV)), to use ppx_get_env, or in general to get convenient build-time-configurable PPX.