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 from the context.production toml config are being injected during netlify dev #1873

Closed
askrzypczak opened this issue Feb 12, 2021 · 5 comments · Fixed by #1878

Comments

@askrzypczak
Copy link

askrzypczak commented Feb 12, 2021

Describe the bug

My environment variables from the context.production toml config are being injected during netlify dev

Configuration

[build]
  base = ""
  functions = "functions"
  command = "/bin/bash ./netlify-build.sh"
  environment = { GRIDSOME_DEPLOY_MODE = "development", NODE_VERSION="12", AWS_LAMBDA_JS_RUNTIME="nodejs12.x" }

[context.staging]
  environment = { GRIDSOME_DEPLOY_MODE = "staging", NODE_ENV="production" }

[context.feature-branch]
  environment = { GRIDSOME_DEPLOY_MODE = "feature-branch", NODE_ENV="production" }
  
[context.production]
  environment = { GRIDSOME_DEPLOY_MODE = "production", NODE_ENV="production" }

in my main.js i have the following: console.debug("deploy mode: ", process.env.GRIDSOME_DEPLOY_MODE)

(some background: gridsome includes any env var which is prefixed by 'GRIDSOME" in the client env, which is why you can console.log it)

the output is deploy mode: production Additionally, the webpack config files that I am using are listening to NODE_ENV and are packing for production instead of dev.

  • Did you run your build through the UI or the CLI?
    I used the CLI
  • If using the CLI, which flags did you use?
    no flags, just netlify dev
  • If using the CLI, please enter the following command in a terminal and copy/paste its output:
    OS: Windows 10 10.0.19042
    CPU: (6) x64 Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
    Memory: 7.07 GB / 15.92 GB
  Binaries:
    Node: 12.13.1 - C:\Program Files\nodejs\node.EXE    
    Yarn: 1.22.10 - C:\Projects\virtualship\node_modules\.bin\yarn.CMD
    npm: 6.12.1 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    netlify-cli: ^3.7.1 => 3.7.1

I just updated from [email protected] to [email protected].
Before the update the GRIDSOME_DEPLOY_MODE var was always undefined if you did not have it set in a .env (the build.environment was being ignored which was OK with me)

I have been wracking my brain looking for a way to force the env var to 'development' and all I have right now is to set it in a .env file but this is REALLY not something that I want to do, as it means using an affirmative action to force a default behavior: i.e. anyone that clones the repo and doesnt configure their gitignored .env file with the deploy mode set will accidentally run in prod mode. it should fail into 'undefined' instead of 'context.production.environment' from the toml.

I tried using [dev.environment] in the toml but it seems to just be ignored, which is not surprising to me since there is no documentation that I could find saying that dev.environment is a valid dev config

Background at netlify/build#792

@askrzypczak askrzypczak changed the title Please replace with a clear and descriptive title Environment variables from the context.production toml config are being injected during netlify dev Feb 12, 2021
@ehmicky ehmicky transferred this issue from netlify/build Feb 12, 2021
@anAgent
Copy link

anAgent commented Feb 13, 2021

I'm seeing this same issue. Here's what I can see based on the following configuration.

netlify.toml

[build]
  command = "npm run build"
  publish = "./dist/apps/web-ui"

[[redirects]]
  from = "/v1/graphql"
  to = "https://my-graphqlserver.com/v1/graphql/"
  force = false
  status = 200

[dev]
#  command="npm run start"
#  targetPort=4200
#  framework="#custom"

[context.local.environment]
  NX_GRAPH_PROXY_URL="http://localhost:8080/v1/graphql"
  NX_ENABLE_PROXY="true"
  NX_GRAPH_WEBSOCKET_URL="ws://localhost:8080/v1/graphql"

Listing Variables

The current solution is on the master branch.

command: netlify env:list --debug
This command returns the variables on the Netlify site. As expected, it does not use the context.local.envrionment variables

Output

> Initial build environment
cwd: C:\Users\dev\source\repo\netlify-support-poc\my-workspace
mode: cli
siteId: c176d949-e6b2-4f4c-1123-1ca47f34c02f
​
> UI build settings
baseRelDir: true
build:
  command: npm run build
  publish: dist/apps/web-ui
​
> Resolved build environment
branch: master
buildDir: C:\Users\dev\source\repo\netlify-support-poc\my-workspace
configPath: C:\Users\dev\source\repo\netlify-support-poc\my-workspace\netlify.toml
context: production

Setting CONTEXT

command: CONTEXT=local netlify env:list --debug
As you can from the output below, the expected variables are now being returned based on the [context.local.environment] settings.

Output

> Initial build environment
cwd: C:\Users\dev\source\repo\netlify-support-poc\my-workspace
mode: cli
siteId: c176d949-e6b2-4f4c-1123-1ca47f34c02f
​
> UI build settings
baseRelDir: true
build:
  command: npm run build
  publish: dist/apps/web-ui
​
> Resolved build environment
branch: master
buildDir: C:\Users\dev\source\repo\netlify-support-poc\my-workspace
configPath: C:\Users\dev\source\repo\netlify-support-poc\my-workspace\netlify.toml
context: local
.... REMOVED
.----------------------------------------------------------------------------.
|                           Environment variables                            |
|----------------------------------------------------------------------------|
|           Key           |                      Value                       |
|-------------------------|--------------------------------------------------|
| NX_GRAPH_PROXY_URL      | http://localhost:8080/v1/graphql                 |
| NX_ENABLE_PROXY         | true                                             |
| NX_GRAPH_WEBSOCKET_URL  | ws://localhost:8080/v1/graphql                   |
'----------------------------------------------------------------------------'

However, running the site in the same context (CONTEXT=local netlify dev --debug) the cli continues to use the production variables.

I've manually adding some code ( with value: ${chalk.red(variable.value)}) in the node_modules\netlify-cli\src\utils\dev.js file so I can see the values that are being set in the process.env:

    if (!existsInProcess) {
      // Omitting `general` env vars to reduce noise in the logs.
      if (usedSource !== 'general') {
        log(`${NETLIFYDEVLOG} Injected ${usedSourceName} env var: ${chalk.yellow(key)} with value: ${chalk.red(variable.value)}`)
      }

      process.env[key] = variable.value
    }

The output of that statement shows the expected values:

◈ Ignored build settings env var: NX_GRAPH_URL (defined in netlify.toml file)
◈ Injected netlify.toml file env var: NX_GRAPH_URL with value: http://localhost:8080/v1/graphql
◈ Injected netlify.toml file env var: NX_GRAPH_PROXY_URL with value: http://localhost:8080/v1/graphql
◈ Ignored build settings env var: NX_ENABLE_PROXY (defined in netlify.toml file)
◈ Injected netlify.toml file env var: NX_ENABLE_PROXY with value: true
◈ Ignored build settings env var: NX_GRAPH_WEBSOCKET_URL (defined in netlify.toml file)
◈ Injected netlify.toml file env var: NX_GRAPH_WEBSOCKET_URL with value: ws://localhost:8080/v1/graphql
◈ Ignored general context env var: CONTEXT (defined in process)
◈ Ignored general context env var: LANG (defined in process)
◈ No app server detected and no "command" specified
◈ Running static server from "my-workspace\dist\apps\web-ui"

As stated above, it loads the server without the dev values.

Other Comments

  • I've also tested this by creating .env files and it has the same result - production variables are used.
  • I've tried testing this using v3.0.0 and it had the same results. My assumption is that it is one of the associated packages that are consumed by the cli, but take that with grain of salt.
$ CONTEXT=local npm run netlify -- -v &&  npm run netlify -- dev --debug

> [email protected] netlify C:\Users\aaron\source\repo\netlify-support-poc\my-workspace
> netlify "-v"

netlify-cli/3.0.0 win32-x64 node-v12.20.0

@erezrokah
Copy link
Contributor

Hi @anAgent and @askrzypczak. This is due to a recent change we made in how the CLI resolves netlify.toml.
This PR should fix it. We'll publish it once it's reviewed

@erezrokah
Copy link
Contributor

A fix for this issue has been published in version 3.8.1

@lloydh
Copy link

lloydh commented Mar 17, 2021

I have a related issue in 3.13.1

I set NODE_ENV to production in the Netlify UI as I understand this is the only way to set env vars for functions.

The issue I have is that netlify-cli is using this value when running dev locally, causing webpack dev build errors. I'm unable to override this using context.local.environment in netlify.toml.

@erezrokah
Copy link
Contributor

Hi @lloydh, this should work:

[build.environment]
NODE_ENV = "production"

[context.dev.environment]
NODE_ENV = "dev"

If that doesn't work, please open a new issue and follow the issue template.

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

Successfully merging a pull request may close this issue.

4 participants