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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: environment variable build context filtering fix #5887

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

wconrad265
Copy link

@wconrad265 wconrad265 commented Oct 18, 2024

🎉 Thanks for submitting a pull request! 🎉

Summary

The pull request addresses the issue below.

The getEnv function within the file packages/config/src/env/envelope.ts was filtering out env variables that did not belong to the all or dev contexts.

More information on how to replicate and how we isolated the issue is below.

Understanding and Isolating the Problem

In order to replicate the issue, we did the following steps.

  1. We created a simple vite react app, you can find the app for this test. here.
    1. https://github.com/wconrad265/env-test
    2. ran npm install to install everything needed
  2. ran netlify sites:create to create a new site and link the repo to it
  3. We created two different env variables, on the netlify website.
    1. VITE_All_Context This env variable has the same value in all contexts.
      image

    2. VITE_Hello this env variable has different values depending on the context
      image

  4. We placed the env variables within the main react component and ran the following commands
    1. env variables have to start with VITE in order for vite to recognize them

Next, we ran a series of deploy commands to see the result

Next we created second environment variable declared for all contexts to understand how varaibles declared within all contexts were being handled across different values passed to the --context flag.

command netlify deploy --build --prod --context

VITE_Hello VITE_All_Context —build —prod —context
no yes x x n/a
yes yes x x dev
no yes x x production
no yes x x deploy-preview
no yes x x branch-deploy

Next we examined the handling of environment variables in netlify dev

netlity dev

VITE_Hello VITE_All_Context —context
yes yes n/a
yes yes dev
yes yes production
yes yes deploy-preview
yes yes branch-deploy

We dug further to isolate the problem by running netlify build

netlify build context --dev

As we can see below the value of the env variable when using the dev context is the correct value.

The VITE_All_Contextvariable is the correct value

image

netlify build context --production

As we can see below, the value of the VITE_Hello when using the production context is void, however All_Context env variable is showing the correct value

image

the context of --branch-deploy and deploy-preview are the same as above. This led us to believe the issue is in the netlify build command

Note this is only an issue when deploying from the terminal with the above commands. When deploying from the website, the environment variables were injected correctly.

Solution

The netlify build command gathers environment variables by running getEnv().

When running this function, if siteInfo.use_envelope is a truthy value, it will invoke getEnvelope . We isolated this function as the source of env variable loss. Environment variables not belonging to the dev or all contexts were filtered out.

We updated the getEnvelopeto filter by all and context (the argument passed by user associated with the —context flag) and reran our isolated tests with the vite project above.

Results:

netlify deploy --build --prod

VITE_Hello VITE_All_Context —build —prod —context
yes yes x x n/a
yes yes x x dev
yes yes x x production
yes yes x x deploy-preview
yes yes x x branch-deploy

Also siteId was not passed in the function getEnvelope(), so we adjusted the function to make sure it was passed in.

---

For us to review and ship your PR efficiently, please perform the following steps:

  • Open a bug/issue before writing your code 🧑‍💻. This ensures
    we can discuss the changes and get feedback from everyone that should be involved. If you`re fixing a typo or
    something that`s on fire 🔥 (e.g. incident related), you can skip this step.
  • Read the contribution guidelines 📖. This ensures
    your code follows our style guide and passes our tests.
  • Update or add tests (if any source code was changed or added) 🧪
  • Update or add documentation (if features were changed or added) 📝
  • Make sure the status checks below are successful ✅

A picture of a cute animal (not mandatory, but encouraged)

wconrad265 and others added 2 commits October 18, 2024 10:16
initial commit

Co-authored-by: Thomas Lane <[email protected]>
fixed issue where build was not injecting env variables that were not dev or all context

Co-authored-by: Thomas Lane <[email protected]>
Copy link

sonarcloud bot commented Oct 18, 2024

@wconrad265 wconrad265 changed the title Build env context fix fix: enviroment variable build context filtering fix Oct 18, 2024
@wconrad265 wconrad265 changed the title fix: enviroment variable build context filtering fix fix: environment variable build context filtering fix Oct 18, 2024
@wconrad265 wconrad265 marked this pull request as ready for review October 18, 2024 16:34
Copy link

@tlane25 tlane25 left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link

@DanielSLew DanielSLew left a comment

Choose a reason for hiding this comment

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

Nice catch! Looks good to me

@wconrad265
Copy link
Author

Nice catch! Looks good to me

Thanks! Do you know why the circleCi tests are failing?

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.

3 participants