Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev: Pass .env vars as an object to child process #764

Merged
merged 3 commits into from
Mar 25, 2020
Merged

Conversation

RaeesBhatti
Copy link
Contributor

@RaeesBhatti RaeesBhatti commented Mar 24, 2020

- Summary
This is to fix an inconsistency in NodeJS which results in process.env not realizing changes if they're accessed shortly after.
Fixes: #747

- Test plan

  • Have your create-react-app project connected to Netlify production with netlify link.
  • Have some env variable in Netlify production
  • Have a .env file in project root locally that overrides that env variable
  • Run netlify dev and observe the value of that env variable

- Description for the changelog
Pass the variables from .env file as setting.env object to startDevServer, so that, they're set explicitly on the new process.

- A picture of a cute animal (not mandatory but encouraged)
馃惓

@@ -424,6 +424,7 @@ class DevCommand extends Command {
const envFile = path.resolve(site.root, '.env')
if (fs.existsSync(envFile)) {
const vars = dotenv.parse(fs.readFileSync(envFile)) || {}
settings.env = { ...settings.env, ...vars }
console.log(`${NETLIFYDEVLOG} Overriding the following env variables with ${chalk.blue('.env')} file:`, chalk.yellow(Object.keys(vars)))
Object.entries(vars).forEach(([key, val]) => process.env[key] = val)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process.env[key] being set here. Is not accessible two lines down when accessed in startDevServer.
cc @erquhart

Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cause for this bug is the spread order of properties here:

env: { ...process.env, ...settings.env, FORCE_COLOR: 'true' },

settings.env properties override process.env properties.

Changing that line to env: { ...settings.env,...process.env, FORCE_COLOR: 'true' }, fixes it

@RaeesBhatti
Copy link
Contributor Author

Ah I see. Thanks a bunch @erezrokah. This was a head scratcher. I never looked at the detectors logic, I thought they only returned application specific env variables.

@RaeesBhatti
Copy link
Contributor Author

Can you please take another look @erezrokah

@erezrokah
Copy link
Contributor

Sure, reviewing now

Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change works since each detector inherits env from process.env right?
So future detectors will have to make sure to do the same?
Don't know if that is a real issue or not

@RaeesBhatti
Copy link
Contributor Author

Yeah. All of the detectors inherit process.env. And it is part of the template, so, the future detectors will do the same.
I suggest that we don't change this since it's explained in the docs and example. But if we do need to, we can do that later.

@RaeesBhatti RaeesBhatti merged commit e23a190 into master Mar 25, 2020
@RaeesBhatti RaeesBhatti deleted the raees/env-fix branch March 25, 2020 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Local environment variables not loaded in CLI 2.38.0+
2 participants