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

Recent change silently makes environment variables undefined #36528

Closed
2 tasks done
j127 opened this issue Sep 3, 2022 · 7 comments
Closed
2 tasks done

Recent change silently makes environment variables undefined #36528

j127 opened this issue Sep 3, 2022 · 7 comments
Labels
status: needs more info Needs triaging and reproducible examples or more information to be resolved

Comments

@j127
Copy link
Contributor

j127 commented Sep 3, 2022

Preliminary Checks

Description

This valid JavaScript was working in Gatsby components until a couple of weeks ago:

const { GATSBY_MY_VAR } = process.env;

It now silently fails as undefined and only works with this syntax:

const GATSBY_MY_VAR = process.env.GATSBY_MY_VAR;

Reproduction Link

https://github.com/j127/gatsby-bug

Steps to Reproduce

  1. Clone the repo.
  2. Run npm i
  3. Run npm run build
  4. Check out the error in the terminal.
  5. Check here to see the code.

Expected Result

const { GATSBY_MY_VAR } = process.env; should load the environment variable.

Actual Result

The variable is undefined.

Environment

System:
    OS: macOS 12.5.1
    CPU: (10) arm64 Apple M1 Max
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.15.1/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
  Languages:
    Python: 3.10.2 - /Users/username/.pyenv/shims/python
  Browsers:
    Firefox: 104.0.1
    Safari: 15.6.1
  npmPackages:
    gatsby: ^4.22.0 => 4.22.0


### Config Flags

_No response_
@j127 j127 added the type: bug An issue or pull request relating to a bug in Gatsby label Sep 3, 2022
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Sep 3, 2022
@KyleAMathews
Copy link
Contributor

Do you know what version this last worked on? Thanks for the reproduction!

@j127
Copy link
Contributor Author

j127 commented Sep 4, 2022

My package.json says ^4.16.0, but installed version is 4.21.0.

August 16th is the last time I can verify that it definitely worked, and the package-lock.json file changed on August 16th or 17th from 4.16.0 to 4.21.0, so it should be in that range.

@marvinjude
Copy link
Contributor

marvinjude commented Sep 5, 2022

Hi @j127,

Seem to me that this has always been the case even before 4.16.0. I tested 4.15 through 4.22 and the behaviour is the same.

Edit: This behaviour was introduced in #10030 (~4yrs ago) and hasn't changed since then

I'll close this for now. Feel free to reopen if you feel otherwise

@marvinjude marvinjude added status: needs more info Needs triaging and reproducible examples or more information to be resolved and removed type: bug An issue or pull request relating to a bug in Gatsby status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Sep 5, 2022
@j127
Copy link
Contributor Author

j127 commented Sep 5, 2022

Strange. I'm not sure what is going on, because this code was working until August 16th (on Gatsby 4.16.0), but stopped working as soon as it was updated. I can even go back to 2021 in the file history, and it was successfully making API requests when loading the environment variable this:

const { GATSBY_API_KEY } = process.env;

I tried that bug repo with 4.16.0 and am getting a _gatsbyScript.PartytownContext is undefined for some reason, so I'll leave it.

@marvinjude
Copy link
Contributor

I tried that bug repo with 4.16.0 and am getting a _gatsbyScript.PartytownContext is undefined for some reason, so I'll leave it.

You are seeing this error because your project has a newer version of gatsby-script which no longer has PartytownContext. As a workaround, to enable you to test older versions you'd want to pin the "gatsby-script" version(with resolutions) and force install dependencies.

We've made the change that wouldn't make const { GATSBY_API_KEY } = process.env possible since 2018 so It's hard to tell why you were able to do this in a recent version.

I'm happy to follow up on this if you find anything.

@j127
Copy link
Contributor Author

j127 commented Sep 6, 2022

We've made the change that wouldn't make const { GATSBY_API_KEY } = process.env possible since 2018 so It's hard to tell why you were able to do this in a recent version.

It's strange, because that syntax was definitely working on my site until August 16th. I can't see any way that people could have used that API without that API key loading. The moment it became undefined, the API calls stopped working until I changed that line.

I'll post back here if I ever find the answer. It might be worth fixing anyway, because there are no warnings about why valid JS is failing. If a user sees that const { GATSBY_API_KEY } = process.env is failing to load the variable, they probably aren't going to intuitively think that const GATSBY_API_KEY = process.env.GATSBY_API_KEY would be the solution.

@schmod
Copy link

schmod commented Dec 16, 2022

For us, this appears to have regressed from 4.14.1 to 4.15.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs more info Needs triaging and reproducible examples or more information to be resolved
Projects
None yet
Development

No branches or pull requests

4 participants