I think I must be doing something stupid here but ...
# sst
r
I think I must be doing something stupid here but I just can't see it. I have a stack that I'm trying to run locally. My sst.json looks like:
Copy code
{
  "name": "some-name",
  "stage": "local",
  "region": "eu-west-2",
  "lint": true,
  "typeCheck": true
}
I have a file in the root of the application named
.env.local
In the docs here it says that the app should load the env vars in that file because my stage is local. However, this doesn't happen. If I console log
process.env
inside my stack definition ts file, none of the vars are there. What am I missing?
I'm launching via VSCode debugger. Config is:
Copy code
{
            "type": "node",
            "request": "launch",
            "name": "Launch SST",
            "runtimeVersion": "14.17.0",
            "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/sst",
            "runtimeArgs": ["start", "--increase-timeout"],
            "port": 9229,
            "skipFiles": ["<node_internals>/**"],
            "console": "integratedTerminal",            
            }
        }
Maybe it's a timing thing. Just scaffolded a brand new app. In the first stage I see
Copy code
=======================
 Deploying debug stack
=======================
and the env vars from .env.local are not there. Then after
Copy code
===============
 Deploying app
===============
they are there. The tricky thing here is that in my stack I'm performing some checks to see if those variables are defined (we have a lot so want to provide meaningful error messages if some are missed) However, this check fails on the first pass as they're not there yet
I think I need to do the checks in the stack constructor
f
Hey @Ross Coundon, currently the
.env
variables are available anywhere in your CDK code. FYI, they are loaded at the start of the CDK process. Where are you doing the checks currently?
^btw,
.env.local
is a special file gets loaded for all stages. (ie. even if the stage is
dev
) Where as
.env.dev.local
will only get loaded in the
dev
stage.
r
If I try to access outside of the stack class, they're not available while the 'Deploying debug stack' phase runs but are available when the 'Deploying app' phase runs
I've solved the problem by moving the code that access to them into the constructor
f
hmm.. the CDK code only gets run when the “Deploying app” phase runs. Can you show me a snippet how you were checking it in the “Deploying debug stack”?
r
Like this:
Copy code
if(process.env.SOME_VAR === 'blahdy blah') throw new Error('SOME_VAR missing');

export default class OmwOfscBeStack extends sst.Stack {
 /// ...content omitted
}
f
Ah ic. I will give that a try.
r
To be fair, what I have now works which is effectively
Copy code
export default class OmwOfscBeStack extends sst.Stack {
  constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
    super(scope, id, props);
    if(process.env.SOME_VAR === 'blahdy blah') throw new Error('SOME_VAR missing');
  }
}
However, it'd be faster to fail if it were possible to do the check on the first pass
f
Yup.. I think it’s fair to expect the .env vars are available anywhere in the CDK file (even outside of the constructor).
Hey @Ross Coundon, this should be fixed in v0.28.1.
.env
vars are now set before deploying the debug stack.
r
Great, just saw that release notification and thought it was. Thanks again