From 2dea9b5c778faceb41aeeca387e070e7ed749b41 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Wed, 12 Apr 2023 16:31:10 +0200 Subject: [PATCH 1/4] feat: inject environment variables from .env files into edge functions locally --- src/commands/dev/dev-exec.mjs | 5 ++-- src/commands/dev/dev.mjs | 5 ++-- src/commands/functions/functions-create.mjs | 5 ++-- src/commands/functions/functions-serve.mjs | 7 ++--- src/commands/serve/serve.mjs | 5 ++-- src/lib/edge-functions/registry.mjs | 10 +++++-- src/utils/dev.mjs | 30 ++++++++++++++------- 7 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/commands/dev/dev-exec.mjs b/src/commands/dev/dev-exec.mjs index 9bd518fa0c1..22505f618e8 100644 --- a/src/commands/dev/dev-exec.mjs +++ b/src/commands/dev/dev-exec.mjs @@ -1,6 +1,6 @@ import execa from 'execa' -import { injectEnvVariables } from '../../utils/dev.mjs' +import { addDotEnvVariables, injectEnvVariables } from '../../utils/dev.mjs' import { getEnvelopeEnv, normalizeContext } from '../../utils/env/index.mjs' /** @@ -16,7 +16,8 @@ const devExec = async (cmd, options, command) => { env = await getEnvelopeEnv({ api, context: options.context, env, siteInfo }) } - await injectEnvVariables({ devConfig: { ...config.dev }, env, site }) + env = await addDotEnvVariables({ devConfig: { ...config.dev }, env, site }) + injectEnvVariables(env) await execa(cmd, command.args.slice(1), { stdio: 'inherit', diff --git a/src/commands/dev/dev.mjs b/src/commands/dev/dev.mjs index bfaa5084b6a..c00a1a9581c 100644 --- a/src/commands/dev/dev.mjs +++ b/src/commands/dev/dev.mjs @@ -18,7 +18,7 @@ import { normalizeConfig, } from '../../utils/command-helpers.mjs' import detectServerSettings, { getConfigWithPlugins } from '../../utils/detect-server-settings.mjs' -import { getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' +import { addDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' import { getEnvelopeEnv, normalizeContext } from '../../utils/env/index.mjs' import { ensureNetlifyIgnore } from '../../utils/gitignore.mjs' import { startNetlifyGraph, startPollingForAPIAuthentication } from '../../utils/graph.mjs' @@ -96,7 +96,8 @@ const dev = async (options, command) => { log(`${NETLIFYDEVLOG} Injecting environment variable values for ${chalk.yellow('all scopes')}`) } - await injectEnvVariables({ devConfig, env, site }) + env = await addDotEnvVariables({ devConfig, env, site }) + injectEnvVariables(env) await promptEditorHelper({ chalk, config, log, NETLIFYDEVLOG, repositoryRoot, state }) const { addonsUrls, capabilities, siteUrl, timeouts } = await getSiteInformation({ diff --git a/src/commands/functions/functions-create.mjs b/src/commands/functions/functions-create.mjs index 7a9f704c452..b13ba22d386 100644 --- a/src/commands/functions/functions-create.mjs +++ b/src/commands/functions/functions-create.mjs @@ -19,7 +19,7 @@ import ora from 'ora' import { fileExistsAsync } from '../../lib/fs.mjs' import { getAddons, getCurrentAddon, getSiteData } from '../../utils/addons/prepare.mjs' import { NETLIFYDEVERR, NETLIFYDEVLOG, NETLIFYDEVWARN, chalk, error, log } from '../../utils/command-helpers.mjs' -import { injectEnvVariables } from '../../utils/dev.mjs' +import { addDotEnvVariables, injectEnvVariables } from '../../utils/dev.mjs' import execa from '../../utils/execa.mjs' import { readRepoURL, validateRepoURL } from '../../utils/read-repo-url.mjs' @@ -549,11 +549,12 @@ const handleOnComplete = async ({ command, onComplete }) => { const { config } = command.netlify if (onComplete) { - await injectEnvVariables({ + const env = await addDotEnvVariables({ devConfig: { ...config.dev }, env: command.netlify.cachedConfig.env, site: command.netlify.site, }) + injectEnvVariables(env) await onComplete.call(command) } } diff --git a/src/commands/functions/functions-serve.mjs b/src/commands/functions/functions-serve.mjs index 9b077fc2157..55555160843 100644 --- a/src/commands/functions/functions-serve.mjs +++ b/src/commands/functions/functions-serve.mjs @@ -2,7 +2,7 @@ import { join } from 'path' import { startFunctionsServer } from '../../lib/functions/server.mjs' -import { acquirePort, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' +import { acquirePort, addDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' import { getFunctionsDir } from '../../utils/functions/index.mjs' const DEFAULT_PORT = 9999 @@ -16,11 +16,12 @@ const functionsServe = async (options, command) => { const { api, config, site, siteInfo } = command.netlify const functionsDir = getFunctionsDir({ options, config }, join('netlify', 'functions')) - const { env } = command.netlify.cachedConfig + let { env } = command.netlify.cachedConfig env.NETLIFY_DEV = { sources: ['internal'], value: 'true' } - await injectEnvVariables({ devConfig: { ...config.dev }, env, site }) + env = await addDotEnvVariables({ devConfig: { ...config.dev }, env, site }) + injectEnvVariables(env) const { capabilities, siteUrl, timeouts } = await getSiteInformation({ offline: options.offline, diff --git a/src/commands/serve/serve.mjs b/src/commands/serve/serve.mjs index b14ccb6e28f..17a932be2ea 100644 --- a/src/commands/serve/serve.mjs +++ b/src/commands/serve/serve.mjs @@ -16,7 +16,7 @@ import { normalizeConfig, } from '../../utils/command-helpers.mjs' import detectServerSettings, { getConfigWithPlugins } from '../../utils/detect-server-settings.mjs' -import { getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' +import { addDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' import { getEnvelopeEnv, normalizeContext } from '../../utils/env/index.mjs' import { getInternalFunctionsDir } from '../../utils/functions/functions.mjs' import { ensureNetlifyIgnore } from '../../utils/gitignore.mjs' @@ -52,7 +52,8 @@ const serve = async (options, command) => { log(`${NETLIFYDEVLOG} Injecting environment variable values for ${chalk.yellow('all scopes')}`) } - await injectEnvVariables({ devConfig, env, site }) + env = await addDotEnvVariables({ devConfig, env, site }) + injectEnvVariables(env) await promptEditorHelper({ chalk, config, log, NETLIFYDEVLOG, repositoryRoot, state }) const { addonsUrls, capabilities, siteUrl, timeouts } = await getSiteInformation({ diff --git a/src/lib/edge-functions/registry.mjs b/src/lib/edge-functions/registry.mjs index a57ad4090a6..d3979c9caed 100644 --- a/src/lib/edge-functions/registry.mjs +++ b/src/lib/edge-functions/registry.mjs @@ -15,7 +15,7 @@ export class EdgeFunctionsRegistry { * @param {object} opts.config * @param {string} opts.configPath * @param {string[]} opts.directories - * @param {Record} opts.env + * @param {Record} opts.env * @param {() => Promise} opts.getUpdatedConfig * @param {Declaration[]} opts.internalFunctions * @param {string} opts.projectDir @@ -178,6 +178,11 @@ export class EdgeFunctionsRegistry { return edgeFunctions } + /** + * + * @param {Record} envConfig + * @returns {Record} + */ static getEnvironmentVariables(envConfig) { const env = Object.create(null) Object.entries(envConfig).forEach(([key, variable]) => { @@ -185,7 +190,8 @@ export class EdgeFunctionsRegistry { variable.sources.includes('ui') || variable.sources.includes('account') || variable.sources.includes('addons') || - variable.sources.includes('internal') + variable.sources.includes('internal') || + variable.sources.some((source) => source.startsWith('.env')) ) { env[key] = variable.value } diff --git a/src/utils/dev.mjs b/src/utils/dev.mjs index 233c682bd56..09520a66ed3 100644 --- a/src/utils/dev.mjs +++ b/src/utils/dev.mjs @@ -137,30 +137,40 @@ const getEnvSourceName = (source) => { return printFn(name) } -// Takes a set of environment variables in the format provided by @netlify/config, augments it with variables from both -// dot-env files and the process itself, and injects into `process.env`. -export const injectEnvVariables = async ({ devConfig, env, site }) => { - const environment = new Map(Object.entries(env)) +/** + * @param {{devConfig: any, env: Record, site: any}} param0 + * @returns {Promise>} + */ +export const addDotEnvVariables = async ({ devConfig, env, site }) => { const dotEnvFiles = await loadDotEnvFiles({ envFiles: devConfig.envFiles, projectDir: site.root }) - dotEnvFiles.forEach(({ env: fileEnv, file }) => { + const newSourceName = `${file} file` + Object.keys(fileEnv).forEach((key) => { - const newSourceName = `${file} file` - const sources = environment.has(key) ? [newSourceName, ...environment.get(key).sources] : [newSourceName] + const sources = key in env ? [newSourceName, ...env[key].sources] : [newSourceName] if (sources.includes('internal')) { return } - environment.set(key, { + env[key] = { sources, value: fileEnv[key], - }) + } }) }) + return env +} + +/** + * Takes a set of environment variables in the format provided by @netlify/config and injects them into `process.env` + * @param {Record} env + * @return {void} + */ +export const injectEnvVariables = (env) => { // eslint-disable-next-line fp/no-loops - for (const [key, variable] of environment) { + for (const [key, variable] of Object.entries(env)) { const existsInProcess = process.env[key] !== undefined const [usedSource, ...overriddenSources] = existsInProcess ? ['process', ...variable.sources] : variable.sources const usedSourceName = getEnvSourceName(usedSource) From 5940f0e128d9c6063dd1d8e1fe014a87f628c968 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Wed, 12 Apr 2023 16:54:05 +0200 Subject: [PATCH 2/4] chore: test --- tests/integration/100.command.dev.test.cjs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/integration/100.command.dev.test.cjs b/tests/integration/100.command.dev.test.cjs index 0e4c8af9bf8..d36ffb44f2a 100644 --- a/tests/integration/100.command.dev.test.cjs +++ b/tests/integration/100.command.dev.test.cjs @@ -1,4 +1,3 @@ -// Handlers are meant to be async outside tests const path = require('path') // eslint-disable-next-line ava/use-test @@ -1020,6 +1019,10 @@ test('should have only allowed environment variables set', async (t) => { handler: () => new Response(`${JSON.stringify(Deno.env.toObject())}`), name: 'env', }) + .withContentFile({ + content: 'FROM_ENV="YAS"', + path: '.env', + }) await builder.buildAsync() @@ -1040,16 +1043,21 @@ test('should have only allowed environment variables set', async (t) => { ) const envKeys = Object.keys(response) - t.false(envKeys.includes('DENO_DEPLOYMENT_ID')) - // t.true(envKeys.includes('DENO_DEPLOYMENT_ID')) - // t.is(response.DENO_DEPLOYMENT_ID, 'xxx=') + t.true(envKeys.includes('PATH')) + t.true(envKeys.includes('DENO_REGION')) t.is(response.DENO_REGION, 'local') + t.true(envKeys.includes('NETLIFY_DEV')) t.is(response.NETLIFY_DEV, 'true') + t.true(envKeys.includes('SECRET_ENV')) t.is(response.SECRET_ENV, 'true') + t.true(envKeys.includes('FROM_ENV')) + t.is(response.FROM_ENV, 'YAS') + + t.false(envKeys.includes('DENO_DEPLOYMENT_ID')) t.false(envKeys.includes('NODE_ENV')) t.false(envKeys.includes('DEPLOY_URL')) t.false(envKeys.includes('URL')) From 84d4c249d7e919110cefde412ec60e17f742da6a Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Mon, 17 Apr 2023 13:42:19 +0200 Subject: [PATCH 3/4] chore: rename function --- src/commands/dev/dev-exec.mjs | 4 ++-- src/commands/dev/dev.mjs | 4 ++-- src/commands/functions/functions-create.mjs | 4 ++-- src/commands/functions/functions-serve.mjs | 4 ++-- src/commands/serve/serve.mjs | 4 ++-- src/utils/dev.mjs | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/commands/dev/dev-exec.mjs b/src/commands/dev/dev-exec.mjs index 22505f618e8..a63e7f0ca86 100644 --- a/src/commands/dev/dev-exec.mjs +++ b/src/commands/dev/dev-exec.mjs @@ -1,6 +1,6 @@ import execa from 'execa' -import { addDotEnvVariables, injectEnvVariables } from '../../utils/dev.mjs' +import { getDotEnvVariables, injectEnvVariables } from '../../utils/dev.mjs' import { getEnvelopeEnv, normalizeContext } from '../../utils/env/index.mjs' /** @@ -16,7 +16,7 @@ const devExec = async (cmd, options, command) => { env = await getEnvelopeEnv({ api, context: options.context, env, siteInfo }) } - env = await addDotEnvVariables({ devConfig: { ...config.dev }, env, site }) + env = await getDotEnvVariables({ devConfig: { ...config.dev }, env, site }) injectEnvVariables(env) await execa(cmd, command.args.slice(1), { diff --git a/src/commands/dev/dev.mjs b/src/commands/dev/dev.mjs index c00a1a9581c..b922b72ce9b 100644 --- a/src/commands/dev/dev.mjs +++ b/src/commands/dev/dev.mjs @@ -18,7 +18,7 @@ import { normalizeConfig, } from '../../utils/command-helpers.mjs' import detectServerSettings, { getConfigWithPlugins } from '../../utils/detect-server-settings.mjs' -import { addDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' +import { getDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' import { getEnvelopeEnv, normalizeContext } from '../../utils/env/index.mjs' import { ensureNetlifyIgnore } from '../../utils/gitignore.mjs' import { startNetlifyGraph, startPollingForAPIAuthentication } from '../../utils/graph.mjs' @@ -96,7 +96,7 @@ const dev = async (options, command) => { log(`${NETLIFYDEVLOG} Injecting environment variable values for ${chalk.yellow('all scopes')}`) } - env = await addDotEnvVariables({ devConfig, env, site }) + env = await getDotEnvVariables({ devConfig, env, site }) injectEnvVariables(env) await promptEditorHelper({ chalk, config, log, NETLIFYDEVLOG, repositoryRoot, state }) diff --git a/src/commands/functions/functions-create.mjs b/src/commands/functions/functions-create.mjs index b13ba22d386..115173df269 100644 --- a/src/commands/functions/functions-create.mjs +++ b/src/commands/functions/functions-create.mjs @@ -19,7 +19,7 @@ import ora from 'ora' import { fileExistsAsync } from '../../lib/fs.mjs' import { getAddons, getCurrentAddon, getSiteData } from '../../utils/addons/prepare.mjs' import { NETLIFYDEVERR, NETLIFYDEVLOG, NETLIFYDEVWARN, chalk, error, log } from '../../utils/command-helpers.mjs' -import { addDotEnvVariables, injectEnvVariables } from '../../utils/dev.mjs' +import { getDotEnvVariables, injectEnvVariables } from '../../utils/dev.mjs' import execa from '../../utils/execa.mjs' import { readRepoURL, validateRepoURL } from '../../utils/read-repo-url.mjs' @@ -549,7 +549,7 @@ const handleOnComplete = async ({ command, onComplete }) => { const { config } = command.netlify if (onComplete) { - const env = await addDotEnvVariables({ + const env = await getDotEnvVariables({ devConfig: { ...config.dev }, env: command.netlify.cachedConfig.env, site: command.netlify.site, diff --git a/src/commands/functions/functions-serve.mjs b/src/commands/functions/functions-serve.mjs index 55555160843..c14ceb3af37 100644 --- a/src/commands/functions/functions-serve.mjs +++ b/src/commands/functions/functions-serve.mjs @@ -2,7 +2,7 @@ import { join } from 'path' import { startFunctionsServer } from '../../lib/functions/server.mjs' -import { acquirePort, addDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' +import { acquirePort, getDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' import { getFunctionsDir } from '../../utils/functions/index.mjs' const DEFAULT_PORT = 9999 @@ -20,7 +20,7 @@ const functionsServe = async (options, command) => { env.NETLIFY_DEV = { sources: ['internal'], value: 'true' } - env = await addDotEnvVariables({ devConfig: { ...config.dev }, env, site }) + env = await getDotEnvVariables({ devConfig: { ...config.dev }, env, site }) injectEnvVariables(env) const { capabilities, siteUrl, timeouts } = await getSiteInformation({ diff --git a/src/commands/serve/serve.mjs b/src/commands/serve/serve.mjs index 17a932be2ea..00fbdf27e80 100644 --- a/src/commands/serve/serve.mjs +++ b/src/commands/serve/serve.mjs @@ -16,7 +16,7 @@ import { normalizeConfig, } from '../../utils/command-helpers.mjs' import detectServerSettings, { getConfigWithPlugins } from '../../utils/detect-server-settings.mjs' -import { addDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' +import { getDotEnvVariables, getSiteInformation, injectEnvVariables } from '../../utils/dev.mjs' import { getEnvelopeEnv, normalizeContext } from '../../utils/env/index.mjs' import { getInternalFunctionsDir } from '../../utils/functions/functions.mjs' import { ensureNetlifyIgnore } from '../../utils/gitignore.mjs' @@ -52,7 +52,7 @@ const serve = async (options, command) => { log(`${NETLIFYDEVLOG} Injecting environment variable values for ${chalk.yellow('all scopes')}`) } - env = await addDotEnvVariables({ devConfig, env, site }) + env = await getDotEnvVariables({ devConfig, env, site }) injectEnvVariables(env) await promptEditorHelper({ chalk, config, log, NETLIFYDEVLOG, repositoryRoot, state }) diff --git a/src/utils/dev.mjs b/src/utils/dev.mjs index 09520a66ed3..b96f941a891 100644 --- a/src/utils/dev.mjs +++ b/src/utils/dev.mjs @@ -141,7 +141,7 @@ const getEnvSourceName = (source) => { * @param {{devConfig: any, env: Record, site: any}} param0 * @returns {Promise>} */ -export const addDotEnvVariables = async ({ devConfig, env, site }) => { +export const getDotEnvVariables = async ({ devConfig, env, site }) => { const dotEnvFiles = await loadDotEnvFiles({ envFiles: devConfig.envFiles, projectDir: site.root }) dotEnvFiles.forEach(({ env: fileEnv, file }) => { const newSourceName = `${file} file` From 597839deafe03ab9b5584c22317de9516203079f Mon Sep 17 00:00:00 2001 From: Daniel Tschinder <231804+danez@users.noreply.github.com> Date: Mon, 17 Apr 2023 14:58:49 +0200 Subject: [PATCH 4/4] chore: fix test on windows --- tests/integration/100.command.dev.test.cjs | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration/100.command.dev.test.cjs b/tests/integration/100.command.dev.test.cjs index d36ffb44f2a..1d493f11056 100644 --- a/tests/integration/100.command.dev.test.cjs +++ b/tests/integration/100.command.dev.test.cjs @@ -1043,8 +1043,6 @@ test('should have only allowed environment variables set', async (t) => { ) const envKeys = Object.keys(response) - t.true(envKeys.includes('PATH')) - t.true(envKeys.includes('DENO_REGION')) t.is(response.DENO_REGION, 'local')