From 011ce3ad392f8cc7ff08fd0a0c37bd5fb58e6a5f Mon Sep 17 00:00:00 2001 From: Lukas Holzer Date: Thu, 11 Jul 2024 15:47:06 +0200 Subject: [PATCH] feat: pass default config for resolveUpdatedConfig instead of cached config (#5761) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: pass default config for resolveUpdatedConfig instead of cached config * chore: fix formatting and linting * chore: return cached config only if there is no defaultConfig * chore: fix the condition * Update packages/config/src/main.ts Co-authored-by: Eduardo Bouças --------- Co-authored-by: Eduardo Bouças --- packages/build/src/core/build.ts | 15 ++++++------ packages/build/src/core/config.js | 18 +++++++-------- packages/build/src/steps/core_step.ts | 6 ++--- packages/build/src/steps/plugin.js | 4 ++-- packages/build/src/steps/run_core_steps.ts | 6 ++--- packages/build/src/steps/run_step.ts | 10 ++++---- packages/build/src/steps/run_steps.js | 4 ++-- packages/build/src/steps/update_config.js | 4 ++-- packages/config/src/main.ts | 5 +++- packages/config/tests/mutate/tests.js | 27 ++++++++++++++++++++++ 10 files changed, 64 insertions(+), 35 deletions(-) diff --git a/packages/build/src/core/build.ts b/packages/build/src/core/build.ts index 22f474da23..e12b5a9755 100644 --- a/packages/build/src/core/build.ts +++ b/packages/build/src/core/build.ts @@ -128,6 +128,7 @@ const tExecBuild = async function ({ } = await loadConfig({ configOpts, cachedConfig, + defaultConfig, cachedConfigPath, envOpt, debug, @@ -180,7 +181,7 @@ const tExecBuild = async function ({ } = await runAndReportBuild({ pluginsOptions, netlifyConfig, - cachedConfig, + defaultConfig, configOpts, siteInfo, configPath, @@ -241,7 +242,7 @@ export const execBuild = measureDuration(tExecBuild, 'total', { parentTag: 'buil export const runAndReportBuild = async function ({ pluginsOptions, netlifyConfig, - cachedConfig, + defaultConfig, configOpts, siteInfo, configPath, @@ -297,7 +298,7 @@ export const runAndReportBuild = async function ({ } = await initAndRunBuild({ pluginsOptions, netlifyConfig, - cachedConfig, + defaultConfig, configOpts, siteInfo, configPath, @@ -403,7 +404,7 @@ export const runAndReportBuild = async function ({ const initAndRunBuild = async function ({ pluginsOptions, netlifyConfig, - cachedConfig, + defaultConfig, configOpts, siteInfo, configPath, @@ -512,7 +513,7 @@ const initAndRunBuild = async function ({ childProcesses, pluginsOptions: pluginsOptionsA, netlifyConfig, - cachedConfig, + defaultConfig, configOpts, packageJson, configPath, @@ -588,7 +589,7 @@ const runBuild = async function ({ childProcesses, pluginsOptions, netlifyConfig, - cachedConfig, + defaultConfig, configOpts, packageJson, configPath, @@ -677,7 +678,7 @@ const runBuild = async function ({ deployId, errorParams, netlifyConfig, - cachedConfig, + defaultConfig, configOpts, logs, debug, diff --git a/packages/build/src/core/config.js b/packages/build/src/core/config.js index ef7a7aaf2e..a07bd1585b 100644 --- a/packages/build/src/core/config.js +++ b/packages/build/src/core/config.js @@ -64,6 +64,7 @@ export const getConfigOpts = function ({ const tLoadConfig = async function ({ configOpts, cachedConfig, + defaultConfig, cachedConfigPath, envOpt, debug, @@ -86,8 +87,7 @@ const tLoadConfig = async function ({ siteInfo, env, integrations, - } = await resolveInitialConfig(configOpts, cachedConfig, cachedConfigPath, featureFlags) - + } = await resolveInitialConfig(configOpts, cachedConfig, defaultConfig, cachedConfigPath, featureFlags) if (!quiet) { logConfigInfo({ logs, configPath, buildDir, netlifyConfig, context: contextA, debug }) } @@ -120,8 +120,8 @@ export const loadConfig = measureDuration(tLoadConfig, 'resolve_config') // Retrieve initial configuration. // In the buildbot and CLI, we re-use the already parsed `@netlify/config` // return value which is passed as `cachedConfig`/`cachedConfigPath`. -const resolveInitialConfig = async function (configOpts, cachedConfig, cachedConfigPath, featureFlags) { - return await resolveConfig({ ...configOpts, cachedConfig, cachedConfigPath, featureFlags }) +const resolveInitialConfig = async function (configOpts, cachedConfig, defaultConfig, cachedConfigPath, featureFlags) { + return await resolveConfig({ ...configOpts, cachedConfig, defaultConfig, cachedConfigPath, featureFlags }) } const logConfigInfo = function ({ logs, configPath, buildDir, netlifyConfig, context, debug }) { @@ -138,17 +138,15 @@ const logConfigInfo = function ({ logs, configPath, buildDir, netlifyConfig, con // change would create debug logs which would be too verbose. // Errors are propagated and assigned to the specific plugin or core step // which changed the configuration. -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export const resolveUpdatedConfig = async function (configOpts, configMutations, cachedConfig) { +export const resolveUpdatedConfig = async function (configOpts, configMutations, defaultConfig) { try { - return await resolveConfig({ + const resolved = await resolveConfig({ ...configOpts, configMutations, - // TODO: remove cached Config here again as this causes tests to fail in the CLI - // Currently investigating the root cause. - // cachedConfig, + defaultConfig, debug: false, }) + return resolved } catch (error) { changeErrorType(error, 'resolveConfig', 'pluginValidation') throw error diff --git a/packages/build/src/steps/core_step.ts b/packages/build/src/steps/core_step.ts index dd8f3c9c88..acf36ab6f4 100644 --- a/packages/build/src/steps/core_step.ts +++ b/packages/build/src/steps/core_step.ts @@ -27,7 +27,7 @@ export const fireCoreStep = async function ({ errorParams, configOpts, netlifyConfig, - cachedConfig, + defaultConfig, configMutations, headersPath, redirectsPath, @@ -66,7 +66,7 @@ export const fireCoreStep = async function ({ branch, childEnv: childEnvA, netlifyConfig, - cachedConfig, + defaultConfig, nodePath, configMutations, headersPath, @@ -88,7 +88,7 @@ export const fireCoreStep = async function ({ } = await updateNetlifyConfig({ configOpts, netlifyConfig, - cachedConfig, + defaultConfig, headersPath, redirectsPath, configMutations, diff --git a/packages/build/src/steps/plugin.js b/packages/build/src/steps/plugin.js index b4550d634b..58053fbf23 100644 --- a/packages/build/src/steps/plugin.js +++ b/packages/build/src/steps/plugin.js @@ -26,7 +26,7 @@ export const firePluginStep = async function ({ errorParams, configOpts, netlifyConfig, - cachedConfig, + defaultConfig, configMutations, headersPath, redirectsPath, @@ -77,7 +77,7 @@ export const firePluginStep = async function ({ } = await updateNetlifyConfig({ configOpts, netlifyConfig, - cachedConfig, + defaultConfig, headersPath, packagePath, redirectsPath, diff --git a/packages/build/src/steps/run_core_steps.ts b/packages/build/src/steps/run_core_steps.ts index 7da5ee43ed..a68437f432 100644 --- a/packages/build/src/steps/run_core_steps.ts +++ b/packages/build/src/steps/run_core_steps.ts @@ -108,7 +108,7 @@ const executeBuildStep = async function ({ try { const { netlifyConfig: netlifyConfigA, configMutations } = await runBuildStep({ - cachedConfig, + defaultConfig, netlifyConfig, buildDir, nodePath, @@ -149,7 +149,7 @@ const executeBuildStep = async function ({ } const runBuildStep = async function ({ - cachedConfig, + defaultConfig, netlifyConfig, buildDir, nodePath, @@ -172,7 +172,7 @@ const runBuildStep = async function ({ nodePath, constants, netlifyConfig, - cachedConfig, + defaultConfig, logs, debug, timers: [], diff --git a/packages/build/src/steps/run_step.ts b/packages/build/src/steps/run_step.ts index c8cc2df040..e1baac58ee 100644 --- a/packages/build/src/steps/run_step.ts +++ b/packages/build/src/steps/run_step.ts @@ -53,7 +53,7 @@ export const runStep = async function ({ failedPlugins, configOpts, netlifyConfig, - cachedConfig, + defaultConfig, configMutations, headersPath, redirectsPath, @@ -144,7 +144,7 @@ export const runStep = async function ({ durationNs, metrics, } = await fireStep({ - cachedConfig, + defaultConfig, event, childProcess, packageName, @@ -306,7 +306,7 @@ const getFireStep = function (packageName: string, coreStepId?: string, event?: } const tFireStep = function ({ - cachedConfig, + defaultConfig, event, childProcess, packageName, @@ -374,7 +374,7 @@ const tFireStep = function ({ errorParams, configOpts, netlifyConfig, - cachedConfig, + defaultConfig, configMutations, headersPath, redirectsPath, @@ -402,7 +402,7 @@ const tFireStep = function ({ errorParams, configOpts, netlifyConfig, - cachedConfig, + defaultConfig, configMutations, headersPath, redirectsPath, diff --git a/packages/build/src/steps/run_steps.js b/packages/build/src/steps/run_steps.js index 4f24831b37..21df92c67f 100644 --- a/packages/build/src/steps/run_steps.js +++ b/packages/build/src/steps/run_steps.js @@ -11,7 +11,7 @@ import { runStep } from './run_step.js' // If an error arises, runs `onError` events. // Runs `onEnd` events at the end, whether an error was thrown or not. export const runSteps = async function ({ - cachedConfig, + defaultConfig, steps, buildbotServerSocket, events, @@ -135,7 +135,7 @@ export const runSteps = async function ({ error, failedPlugins, configOpts, - cachedConfig, + defaultConfig, netlifyConfig: netlifyConfigA, configMutations, headersPath: headersPathA, diff --git a/packages/build/src/steps/update_config.js b/packages/build/src/steps/update_config.js index 449736712a..e449c86caf 100644 --- a/packages/build/src/steps/update_config.js +++ b/packages/build/src/steps/update_config.js @@ -13,7 +13,7 @@ import { logConfigMutations, systemLogConfigMutations } from '../log/messages/mu export const updateNetlifyConfig = async function ({ configOpts, netlifyConfig, - cachedConfig, + defaultConfig, headersPath, redirectsPath, configMutations, @@ -48,7 +48,7 @@ export const updateNetlifyConfig = async function ({ config: netlifyConfigA, headersPath: headersPathA, redirectsPath: redirectsPathA, - } = await resolveUpdatedConfig(configOpts, mergedConfigMutations, cachedConfig) + } = await resolveUpdatedConfig(configOpts, mergedConfigMutations, defaultConfig) logConfigOnUpdate({ logs, netlifyConfig: netlifyConfigA, debug }) errorParams.netlifyConfig = netlifyConfigA diff --git a/packages/config/src/main.ts b/packages/config/src/main.ts index 6b0eae32d7..9a05eada13 100644 --- a/packages/config/src/main.ts +++ b/packages/config/src/main.ts @@ -42,7 +42,10 @@ export const resolveConfig = async function (opts) { const api = getApiClient({ token, offline, host, scheme, pathPrefix, testOpts }) const parsedCachedConfig = await getCachedConfig({ cachedConfig, cachedConfigPath, token, api }) - if (parsedCachedConfig !== undefined) { + // If there is a cached config, use it. The exception is when a default config, + // which consumers like the CLI can set, is present. In those cases, let the + // flow continue so that the default config is parsed and used. + if (parsedCachedConfig !== undefined && opts.defaultConfig === undefined) { return parsedCachedConfig } diff --git a/packages/config/tests/mutate/tests.js b/packages/config/tests/mutate/tests.js index 82bcdeb3d1..9b3db9334b 100644 --- a/packages/config/tests/mutate/tests.js +++ b/packages/config/tests/mutate/tests.js @@ -1,11 +1,13 @@ import { existsSync } from 'fs' import { copyFile, rm } from 'fs/promises' +import { join } from 'path' import { fileURLToPath } from 'url' import { Fixture, normalizeOutput } from '@netlify/testing' import test from 'ava' import { updateConfig } from '../../lib/index.js' +import { resolveConfig } from '../../lib/main.js' const FIXTURES_DIR = fileURLToPath(new URL('fixtures', import.meta.url)) @@ -140,3 +142,28 @@ test('updateConfig() does not delete _headers if headersPath not provided', asyn t.is(typeof headersPath, 'string') t.true(existsSync(headersPath)) }) + +test('Programmatic resolveConfig with configMutations', async (t) => { + const { config } = await resolveConfig({ + mode: 'cli', + context: 'production', + configMutations: [{ keys: ['functions', 'directory'], value: 'new_functions', event: 'onPreBuild' }], + }) + t.is(config.functionsDirectory, join(process.cwd(), 'new_functions')) + t.is(config.build.functions, join(process.cwd(), 'new_functions')) +}) + +test('Programmatic resolveConfig with configMutations and defaultConfig', async (t) => { + const { config } = await resolveConfig({ + mode: 'cli', + context: 'production', + defaultConfig: { + functionsDirectory: 'functions', + build: { functions: 'functions' }, + }, + configMutations: [{ keys: ['functions', 'directory'], value: 'new_functions', event: 'onPreBuild' }], + }) + + t.is(config.functionsDirectory, join(process.cwd(), 'new_functions')) + t.is(config.build.functions, join(process.cwd(), 'new_functions')) +})