From 219c9f137f779a2c3de74c3d2fefd4a4883ac4e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Tue, 24 Jan 2023 12:24:17 +0000 Subject: [PATCH 1/3] fix: inject `NETLIFY_DEV` env var in the process --- src/commands/dev/dev.mjs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/commands/dev/dev.mjs b/src/commands/dev/dev.mjs index 42f1bf6ce8c..af0ec43bc45 100644 --- a/src/commands/dev/dev.mjs +++ b/src/commands/dev/dev.mjs @@ -88,7 +88,8 @@ const dev = async (options, command) => { let { env } = cachedConfig - env.NETLIFY_DEV = { sources: ['internal'], value: 'true' } + process.env.NETLIFY_DEV = 'true' + env.NETLIFY_DEV = { sources: ['internal'], value: process.env.NETLIFY_DEV } if (!options.offline && siteInfo.use_envelope) { env = await getEnvelopeEnv({ api, context: options.context, env, siteInfo }) From f2284c36db8736240a0bc8dca20f9eab73d25f55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Tue, 24 Jan 2023 13:48:09 +0000 Subject: [PATCH 2/3] refactor: add internal env vars to envelope method --- src/commands/dev/dev.mjs | 3 +- src/utils/env/index.mjs | 2 + tests/integration/100.command.dev.test.cjs | 120 ++++++++++++++++++ .../integration/utils/external-server-cli.cjs | 13 ++ tests/integration/utils/external-server.cjs | 8 +- 5 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 tests/integration/utils/external-server-cli.cjs diff --git a/src/commands/dev/dev.mjs b/src/commands/dev/dev.mjs index af0ec43bc45..42f1bf6ce8c 100644 --- a/src/commands/dev/dev.mjs +++ b/src/commands/dev/dev.mjs @@ -88,8 +88,7 @@ const dev = async (options, command) => { let { env } = cachedConfig - process.env.NETLIFY_DEV = 'true' - env.NETLIFY_DEV = { sources: ['internal'], value: process.env.NETLIFY_DEV } + env.NETLIFY_DEV = { sources: ['internal'], value: 'true' } if (!options.offline && siteInfo.use_envelope) { env = await getEnvelopeEnv({ api, context: options.context, env, siteInfo }) diff --git a/src/utils/env/index.mjs b/src/utils/env/index.mjs index 3793e52615d..ffdde4441cf 100644 --- a/src/utils/env/index.mjs +++ b/src/utils/env/index.mjs @@ -146,6 +146,7 @@ export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', scop const accountEnv = formatEnvelopeData({ context, envelopeItems: accountEnvelopeItems, scope, source: 'account' }) const siteEnv = formatEnvelopeData({ context, envelopeItems: siteEnvelopeItems, scope, source: 'ui' }) const generalEnv = filterEnvBySource(env, 'general') + const internalEnv = filterEnvBySource(env, 'internal') const addonsEnv = filterEnvBySource(env, 'addons') const configFileEnv = filterEnvBySource(env, 'configFile') @@ -159,6 +160,7 @@ export const getEnvelopeEnv = async ({ api, context = 'dev', env, key = '', scop ...(includeConfigEnvVars ? addonsEnv : {}), ...siteEnv, ...(includeConfigEnvVars ? configFileEnv : {}), + ...internalEnv, } } diff --git a/tests/integration/100.command.dev.test.cjs b/tests/integration/100.command.dev.test.cjs index a8a329f3427..1537b4548e6 100644 --- a/tests/integration/100.command.dev.test.cjs +++ b/tests/integration/100.command.dev.test.cjs @@ -5,6 +5,7 @@ const path = require('path') const avaTest = require('ava') const { isCI } = require('ci-info') const dotProp = require('dot-prop') +const getAvailablePort = require('get-port') const jwt = require('jsonwebtoken') const { Response } = require('node-fetch') @@ -964,3 +965,122 @@ test('should have only allowed environment variables set', async (t) => { }) }) }) + +test('should inject the `NETLIFY_DEV` environment variable in the process (legacy environment variables)', async (t) => { + const externalServerPort = await getAvailablePort() + const externalServerPath = path.join(__dirname, 'utils', 'external-server-cli.cjs') + const command = `node ${externalServerPath} ${externalServerPort}` + + await withSiteBuilder('site-with-edge-functions-and-env', async (builder) => { + const publicDir = 'public' + + await builder + .withNetlifyToml({ + config: { + build: { + publish: publicDir, + }, + dev: { + command, + publish: publicDir, + targetPort: externalServerPort, + framework: '#custom', + }, + }, + }) + .buildAsync() + + await withDevServer({ cwd: builder.directory }, async ({ port }) => { + const response = await got(`http://localhost:${port}/`).json() + + t.is(response.env.NETLIFY_DEV, 'true') + }) + }) +}) + +test('should inject the `NETLIFY_DEV` environment variable in the process', async (t) => { + const siteInfo = { + account_slug: 'test-account', + build_settings: { + env: {}, + }, + id: 'site_id', + name: 'site-name', + use_envelope: true, + } + const existingVar = { + key: 'EXISTING_VAR', + scopes: ['builds', 'functions'], + values: [ + { + id: '1234', + context: 'production', + value: 'envelope-prod-value', + }, + { + id: '2345', + context: 'dev', + value: 'envelope-dev-value', + }, + ], + } + const routes = [ + { path: 'sites/site_id', response: siteInfo }, + { path: 'sites/site_id/service-instances', response: [] }, + { + path: 'accounts', + response: [{ slug: siteInfo.account_slug }], + }, + { + path: 'accounts/test-account/env/EXISTING_VAR', + response: existingVar, + }, + { + path: 'accounts/test-account/env', + response: [existingVar], + }, + ] + + const externalServerPort = await getAvailablePort() + const externalServerPath = path.join(__dirname, 'utils', 'external-server-cli.cjs') + const command = `node ${externalServerPath} ${externalServerPort}` + + await withSiteBuilder('site-with-edge-functions-and-env', async (builder) => { + const publicDir = 'public' + + await builder + .withNetlifyToml({ + config: { + build: { + publish: publicDir, + }, + dev: { + command, + publish: publicDir, + targetPort: externalServerPort, + framework: '#custom', + }, + }, + }) + .buildAsync() + + await withMockApi(routes, async ({ apiUrl }) => { + await withDevServer( + { + cwd: builder.directory, + offline: false, + env: { + NETLIFY_API_URL: apiUrl, + NETLIFY_SITE_ID: 'site_id', + NETLIFY_AUTH_TOKEN: 'fake-token', + }, + }, + async ({ port }) => { + const response = await got(`http://localhost:${port}/`).json() + + t.is(response.env.NETLIFY_DEV, 'true') + }, + ) + }) + }) +}) diff --git a/tests/integration/utils/external-server-cli.cjs b/tests/integration/utils/external-server-cli.cjs new file mode 100644 index 00000000000..62ce27a6d7d --- /dev/null +++ b/tests/integration/utils/external-server-cli.cjs @@ -0,0 +1,13 @@ +const process = require('process') + +const { startExternalServer } = require('./external-server.cjs') + +const port = Number.parseInt(process.argv[2]) + +if (Number.isNaN(port)) { + throw new TypeError(`Invalid port`) +} + +console.log('Running external server on port', port, process.env.NETLIFY_DEV) + +startExternalServer({ port }) diff --git a/tests/integration/utils/external-server.cjs b/tests/integration/utils/external-server.cjs index 9a0c00ee3bc..2fae8c637ed 100644 --- a/tests/integration/utils/external-server.cjs +++ b/tests/integration/utils/external-server.cjs @@ -1,13 +1,15 @@ +const { env } = require('process') + const express = require('express') -const startExternalServer = () => { +const startExternalServer = ({ port } = {}) => { const app = express() app.use(express.urlencoded({ extended: true })) app.all('*', function onRequest(req, res) { - res.json({ url: req.url, body: req.body, method: req.method, headers: req.headers }) + res.json({ url: req.url, body: req.body, method: req.method, headers: req.headers, env }) }) - return app.listen() + return app.listen({ port }) } module.exports = { From ecb7a9c0f59b6d40234967755097bac8065c6f92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Bou=C3=A7as?= Date: Tue, 24 Jan 2023 13:51:47 +0000 Subject: [PATCH 3/3] chore: update site names in tests --- tests/integration/100.command.dev.test.cjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/100.command.dev.test.cjs b/tests/integration/100.command.dev.test.cjs index 1537b4548e6..694fd23d25e 100644 --- a/tests/integration/100.command.dev.test.cjs +++ b/tests/integration/100.command.dev.test.cjs @@ -971,7 +971,7 @@ test('should inject the `NETLIFY_DEV` environment variable in the process (legac const externalServerPath = path.join(__dirname, 'utils', 'external-server-cli.cjs') const command = `node ${externalServerPath} ${externalServerPort}` - await withSiteBuilder('site-with-edge-functions-and-env', async (builder) => { + await withSiteBuilder('site-with-legacy-env-vars', async (builder) => { const publicDir = 'public' await builder @@ -1045,7 +1045,7 @@ test('should inject the `NETLIFY_DEV` environment variable in the process', asyn const externalServerPath = path.join(__dirname, 'utils', 'external-server-cli.cjs') const command = `node ${externalServerPath} ${externalServerPort}` - await withSiteBuilder('site-with-edge-functions-and-env', async (builder) => { + await withSiteBuilder('site-with-env-vars', async (builder) => { const publicDir = 'public' await builder