Here’s a Commandbox/Coldbox combo question: with t...
# box-products
p
Here’s a Commandbox/Coldbox combo question: with the commandbox-dotenv module, we can create a
.env
file in the root that Commandbox will read an incorporate into Java args. In the Coldbox
config.cfc
file there is note in the “Environments” section which says
* By default we look in your
.env` file for an
environment
key, if not, then we look into this structure or if you have a function called
detectEnvironment()
. So is this just a reference to the Coldbox
dotenv
module or is it a separate process by which the CF code itself looks for the
.env
file and reads in the value?
w
you can see for yourself bij looking at the detectEnvironment function in ApplicationLoader.cfc. First it is calling your own detectenvironment function if present, and next it is checking this
Copy code
else if ( len( util.getSystemSetting( "ENVIRONMENT", "" ) ) ) {
            configStruct.environment = util.getSystemSetting( "ENVIRONMENT", "" );
        }
so it is just calling getSystemSetting which is just checking if this ENVIRONMENT key is present in your environment variables. There are more ways to load these environment variables. In a commandbox setting dotenv is a popular way, but in some standard hosting setup with apache and lucee we didn’t need the dotenv stuff. So it is not dependant on dotenv, but in a commandbox env is it certainly an easy way to populate your vars with dotenv in a .env file. I had quite some issues with other automatic detection methods. Environmental variables is certainly the most reliable way to set the correct production or dev environment.
e
The docs are slightly wrong here. ColdBox I’ll look in your system environment variables and then your system Java properties for an
ENVIRONMENT
key. CommandBox DotEnv is just one way to supply those.
Good pull request opportunity. ;-)
b
Agreed, I've never cared for the wording of that comment. ColdBox does not "look in" your .env file. That's sort of presumptuous and ignores the man other ways env vars (and java system props) can be set.
we can create a
.env
file in the root that Commandbox will read an incorporate into Java args.
@Peter Hoopes Just a clarification here. The
.env
file creates environment variables in the server process, which are not the same as Java system properties. Now that said, the commandbox-dotenv module does actually have a legacy behavior which turns
Copy code
foo=bar
in your
.env
file into
Copy code
-Dfoo=bar
JVM args, but it's behind a flag which is enabled for now, but will be disabled by default at some point in the future. Generally speaking, this doesn't really affect your question since ColdBox's
getSystemSetting()
function checks both Java system props and actual environment variables, so it will find it either way.
e
Huh…when did that change? I’ve thought it was still providing them as Java system props. 😅
b
I didn't say it changed. I said it would change in the future 🙂
That's what you and I talked about when we added that flag
The
legacyLoadToServerJVMArgs
setting is still set to
true
for now, but that was just going to be until the next major version.
e
Just a clarification here. The .env file creates environment variables in the server process, which are not the same as Java system properties.
This here. What is the syntax to create “environment variables in the server process?”
b
I'm not sure what you mean regarding syntax since the .env file, which is just a properties file is the only syntax the user needs to know.
but if you're asking where the CommandBox source code is that put commandbox env vars into the actual server process, that is here https://github.com/Ortus-Solutions/commandbox/blob/development/src/cfml/system/services/ServerService.cfc#L1527-L1529
Which simply provides a
Map
of key/value pairs to the process builder environment (which is a Map itself)
e
Huh….cool. Didn’t know that was an option.
b
Yep, that's why we put the JVM args behind a flag with the idea to turn them off now that dotenv was able to create "proper" env vars (which wasn't the case originally)
p
Most of this makes sense, so thanks for the feedback. I wasn’t separating “server process” from “Java system props”, but in my brain now I am. 👍