Skip to content

Commit 03103fc

Browse files
committed
fix: load correct environment variables in Preview Server context
This changeset fixes environment-variable loading a Preview Server context. Previously, we were missing `dev-server` from the CLI's list of supported contexts. Because of a variable that (unnecessarily) mixes the concept of branches and contexts, when we run into an unsupported context, we assume we must be running a branch preview, and try to find variables for the branch named `dev-server`. For the majority of users, variables for that branch won't exist, and they'll end up with an unexpectedly empty variable. Why do we assume it's a branch preview when we have enough information available to us to know it's not one? Why do we throw away the type information that would have caught this? Why do we load environment variables from the API in the CLI even though `@netlify/config` already does this? These are the mysteries of the universe.
1 parent 99a2f05 commit 03103fc

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/utils/env/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { SiteInfo, EnvironmentVariableSource } from '../../utils/types.js'
88
* These all match possible `context` values returned by the Envelope API.
99
* Note that a user may also specify a branch name with the special `branch:my-branch-name` format.
1010
*/
11-
export const SUPPORTED_CONTEXTS = ['all', 'production', 'deploy-preview', 'branch-deploy', 'dev'] as const
11+
export const SUPPORTED_CONTEXTS = ['all', 'production', 'deploy-preview', 'branch-deploy', 'dev', 'dev-server'] as const
1212
/**
1313
* Additional aliases for the user-provided env `context` option.
1414
*/
@@ -30,7 +30,13 @@ type EnvelopeEnvVarScope =
3030
type EnvelopeEnvVar = Awaited<ReturnType<NetlifyAPI['getEnvVars']>>[number] & {
3131
scopes: EnvelopeEnvVarScope[]
3232
}
33-
type EnvelopeEnvVarContext = NonNullable<NonNullable<EnvelopeEnvVar['values']>[number]['context']>
33+
34+
type EnvelopeEnvVarContext = NonNullable<
35+
| NonNullable<EnvelopeEnvVar['values']>[number]['context']
36+
// TODO(ndhoule): Netlify API is incorrect - Update OpenAPI types with this context type ..
37+
| 'dev-server'
38+
>
39+
3440
export type EnvelopeEnvVarValue = {
3541
/**
3642
* The deploy context of the this env var value
@@ -104,6 +110,12 @@ export const getValueForContext = (
104110
): EnvelopeEnvVarValue | undefined => {
105111
const isSupportedContext = (SUPPORTED_CONTEXTS as readonly string[]).includes(contextOrBranch)
106112
if (!isSupportedContext) {
113+
// FIXME(ndhoule): If it's not a supported context, we just assume this is a branch deploy. This
114+
// means that if you ever add a deploy context but forget to add it to SUPPORTED_CONTEXTS, we'll
115+
// just load the wrong environment variables. (This bug is not theoretical: it's why I'm writing
116+
// this comment.) We should instead pass a `context` and optional `branch` parameter to this
117+
// function rather than mix the two concepts, and we should fail when an unsupported context is
118+
// provided.
107119
const valueMatchingAsBranch = values.find((val) => val.context_parameter === contextOrBranch)
108120
// This is a `branch` context, which is an override, so it takes precedence
109121
if (valueMatchingAsBranch != null) {

tests/integration/commands/env/env-set.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ describe('env:set command', async () => {
196196
(request) => request.method === 'PUT' && request.path === '/api/v1/accounts/test-account/env/OTHER_VAR',
197197
)
198198
expect(putRequest).toHaveProperty('body.is_secret', true)
199-
expect(putRequest).toHaveProperty('body.values.length', 4)
199+
expect(putRequest).toHaveProperty('body.values.length', 5)
200200
expect(putRequest).toHaveProperty('body.values[0].context', 'production')
201201
expect(putRequest).toHaveProperty('body.values[0].value', 'envelope-all-value')
202202
expect(putRequest).toHaveProperty('body.values[1].context', 'deploy-preview')

tests/integration/commands/env/env-unset.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('env:unset command', async () => {
7272
(request) => request.method === 'PATCH' && request.path === '/api/v1/accounts/test-account/env/OTHER_VAR',
7373
)
7474

75-
expect(patchRequests).toHaveLength(3)
75+
expect(patchRequests).toHaveLength(4)
7676
})
7777
})
7878

tests/unit/utils/env/index.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ test('should return a matching value from a given context', () => {
2323
context: 'dev',
2424
value: 'bar',
2525
},
26+
{
27+
context: 'dev-server',
28+
value: 'bar',
29+
},
2630
])
27-
const result = getValueForContext(values, 'dev')
28-
expect(result).toHaveProperty('value', 'bar')
31+
expect(getValueForContext(values, 'production')).toHaveProperty('value', 'foo')
32+
expect(getValueForContext(values, 'dev')).toHaveProperty('value', 'bar')
33+
expect(getValueForContext(values, 'dev-server')).toHaveProperty('value', 'bar')
2934
})
3035

3136
test('should return a value from the `branch` context with a matching `context_parameter` given a branch', () => {

0 commit comments

Comments
 (0)