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

Environment variables not detected when used in config file from an imported source #9688

Closed
dvlden opened this issue Aug 18, 2021 · 5 comments

Comments

@dvlden
Copy link

dvlden commented Aug 18, 2021

Versions

  • nuxt: v2.15.7
  • node: v16.7.0

Reproduction

https://github.com/dvlden/nuxt-generate-bug

Steps to reproduce

  • clone reproduction repository
  • install dependencies
  • yarn run generate
  • check dist/index.html, the custom meta tag will contain value of APP_SPECIAL env. variable, which is holy.sh
  • edit contents of .env file by changing APP_SPECIAL to something else
  • yarn run generate
  • check dist/index.html, the custom meta tag will remain untouched, the meta content will be what it was initially
  • rm -rf dist .nuxt
  • yarn run generate
  • still old value, it doesn't detect the change

What is Expected?

All process.env variables should exist so that their change can be tracked. This isn't the case when something is imported inside nuxt.config.js and used for configuration.

What is actually happening?

It seems that nuxt doesn't pick up env. variables that appear to be unused?

If you inspect @nuxt/cli/dist/cli-generate.js and log the currentBuild, you'll notice that process.env only contains APP_SOMETHING which is used in nuxt.config.js directly, through the constant.

However, APP_SPECIAL is non-existent within process.env, but it's being imported from a file and used in example to create contents of a meta tag. I assume we could've use the imported constant for some other purpose too and it wouldn't detect a change.

This problem is happening for all nuxt scripts, not just generate.

Copy link
Member

@dvlden In full static mode, Nuxt tries to skip a webpack rebuild where possible. To avoid rebuilding every time an unimportant env variable changes, Nuxt checks your nuxt.config file for environment variable usage ([source here]https://github.com/nuxt/nuxt.js/blob/b116d0ded4518d97cceea13a230c70e8e0628845/packages/cli/src/utils/generate.js#L63-L71)). You can force a rebuild every time by disabling the generate cache:

export default {
  generate: {
    cache: false
  }
}

@dvlden
Copy link
Author

dvlden commented Aug 24, 2021

@danielpost That works, but slows down the repeated process, as it doesn't cache anything.

"To avoid rebuilding when an unimportant env variable changes, Nuxt checks your nuxt.config for usage."
Yes I noticed that, but how come it doesn't check for env variables that were used in other files and were imported in config and used for some part of config? That bit kind of makes no sense. I just used a single file to cleanup nuxt config file and extract some utilities there, that happen to use env variables... Is there a way to at least tell it Nuxt, to also check some other files for important env variable changes?

Copy link
Member

danielroe commented Aug 24, 2021

@dvlden The reason is that detecting usage becomes much more complicated once there is an import tree to trace. PRs are definitely welcome. For now, a workaround could be to include the environment variables - even just in a comment - in your nuxt.config, e.g.:

// process.env.MY_CONFIG, process.env.MY_OTHER_CONFIG
export default {
  // ...
}

@pi0
Copy link
Member

pi0 commented Aug 24, 2021

A new ignoreEnv option is coming with 2.16 (#8955) which you can try by using nuxt-edge or using the workaround mentioned by @danielroe to explicitly add used envs.

/note: For nuxi/nuxt3 we should try a proxy-based implementation to track process.env accessing

@dvlden
Copy link
Author

dvlden commented Aug 24, 2021

Perfect!
Thank you @danielpost for that workaround and thank you @pi0 for useful information about a new upcoming feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants