Skip to content

Commit

Permalink
feat: pass default config for resolveUpdatedConfig instead of cached …
Browse files Browse the repository at this point in the history
…config (#5761)

* 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 <[email protected]>

---------

Co-authored-by: Eduardo Bouças <[email protected]>
  • Loading branch information
lukasholzer and eduardoboucas authored Jul 11, 2024
1 parent 0b6b015 commit 011ce3a
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 35 deletions.
15 changes: 8 additions & 7 deletions packages/build/src/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const tExecBuild = async function ({
} = await loadConfig({
configOpts,
cachedConfig,
defaultConfig,
cachedConfigPath,
envOpt,
debug,
Expand Down Expand Up @@ -180,7 +181,7 @@ const tExecBuild = async function ({
} = await runAndReportBuild({
pluginsOptions,
netlifyConfig,
cachedConfig,
defaultConfig,
configOpts,
siteInfo,
configPath,
Expand Down Expand Up @@ -241,7 +242,7 @@ export const execBuild = measureDuration(tExecBuild, 'total', { parentTag: 'buil
export const runAndReportBuild = async function ({
pluginsOptions,
netlifyConfig,
cachedConfig,
defaultConfig,
configOpts,
siteInfo,
configPath,
Expand Down Expand Up @@ -297,7 +298,7 @@ export const runAndReportBuild = async function ({
} = await initAndRunBuild({
pluginsOptions,
netlifyConfig,
cachedConfig,
defaultConfig,
configOpts,
siteInfo,
configPath,
Expand Down Expand Up @@ -403,7 +404,7 @@ export const runAndReportBuild = async function ({
const initAndRunBuild = async function ({
pluginsOptions,
netlifyConfig,
cachedConfig,
defaultConfig,
configOpts,
siteInfo,
configPath,
Expand Down Expand Up @@ -512,7 +513,7 @@ const initAndRunBuild = async function ({
childProcesses,
pluginsOptions: pluginsOptionsA,
netlifyConfig,
cachedConfig,
defaultConfig,
configOpts,
packageJson,
configPath,
Expand Down Expand Up @@ -588,7 +589,7 @@ const runBuild = async function ({
childProcesses,
pluginsOptions,
netlifyConfig,
cachedConfig,
defaultConfig,
configOpts,
packageJson,
configPath,
Expand Down Expand Up @@ -677,7 +678,7 @@ const runBuild = async function ({
deployId,
errorParams,
netlifyConfig,
cachedConfig,
defaultConfig,
configOpts,
logs,
debug,
Expand Down
18 changes: 8 additions & 10 deletions packages/build/src/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const getConfigOpts = function ({
const tLoadConfig = async function ({
configOpts,
cachedConfig,
defaultConfig,
cachedConfigPath,
envOpt,
debug,
Expand All @@ -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 })
}
Expand Down Expand Up @@ -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 }) {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/build/src/steps/core_step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const fireCoreStep = async function ({
errorParams,
configOpts,
netlifyConfig,
cachedConfig,
defaultConfig,
configMutations,
headersPath,
redirectsPath,
Expand Down Expand Up @@ -66,7 +66,7 @@ export const fireCoreStep = async function ({
branch,
childEnv: childEnvA,
netlifyConfig,
cachedConfig,
defaultConfig,
nodePath,
configMutations,
headersPath,
Expand All @@ -88,7 +88,7 @@ export const fireCoreStep = async function ({
} = await updateNetlifyConfig({
configOpts,
netlifyConfig,
cachedConfig,
defaultConfig,
headersPath,
redirectsPath,
configMutations,
Expand Down
4 changes: 2 additions & 2 deletions packages/build/src/steps/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const firePluginStep = async function ({
errorParams,
configOpts,
netlifyConfig,
cachedConfig,
defaultConfig,
configMutations,
headersPath,
redirectsPath,
Expand Down Expand Up @@ -77,7 +77,7 @@ export const firePluginStep = async function ({
} = await updateNetlifyConfig({
configOpts,
netlifyConfig,
cachedConfig,
defaultConfig,
headersPath,
packagePath,
redirectsPath,
Expand Down
6 changes: 3 additions & 3 deletions packages/build/src/steps/run_core_steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const executeBuildStep = async function ({

try {
const { netlifyConfig: netlifyConfigA, configMutations } = await runBuildStep({
cachedConfig,
defaultConfig,
netlifyConfig,
buildDir,
nodePath,
Expand Down Expand Up @@ -149,7 +149,7 @@ const executeBuildStep = async function ({
}

const runBuildStep = async function ({
cachedConfig,
defaultConfig,
netlifyConfig,
buildDir,
nodePath,
Expand All @@ -172,7 +172,7 @@ const runBuildStep = async function ({
nodePath,
constants,
netlifyConfig,
cachedConfig,
defaultConfig,
logs,
debug,
timers: [],
Expand Down
10 changes: 5 additions & 5 deletions packages/build/src/steps/run_step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const runStep = async function ({
failedPlugins,
configOpts,
netlifyConfig,
cachedConfig,
defaultConfig,
configMutations,
headersPath,
redirectsPath,
Expand Down Expand Up @@ -144,7 +144,7 @@ export const runStep = async function ({
durationNs,
metrics,
} = await fireStep({
cachedConfig,
defaultConfig,
event,
childProcess,
packageName,
Expand Down Expand Up @@ -306,7 +306,7 @@ const getFireStep = function (packageName: string, coreStepId?: string, event?:
}

const tFireStep = function ({
cachedConfig,
defaultConfig,
event,
childProcess,
packageName,
Expand Down Expand Up @@ -374,7 +374,7 @@ const tFireStep = function ({
errorParams,
configOpts,
netlifyConfig,
cachedConfig,
defaultConfig,
configMutations,
headersPath,
redirectsPath,
Expand Down Expand Up @@ -402,7 +402,7 @@ const tFireStep = function ({
errorParams,
configOpts,
netlifyConfig,
cachedConfig,
defaultConfig,
configMutations,
headersPath,
redirectsPath,
Expand Down
4 changes: 2 additions & 2 deletions packages/build/src/steps/run_steps.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -135,7 +135,7 @@ export const runSteps = async function ({
error,
failedPlugins,
configOpts,
cachedConfig,
defaultConfig,
netlifyConfig: netlifyConfigA,
configMutations,
headersPath: headersPathA,
Expand Down
4 changes: 2 additions & 2 deletions packages/build/src/steps/update_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { logConfigMutations, systemLogConfigMutations } from '../log/messages/mu
export const updateNetlifyConfig = async function ({
configOpts,
netlifyConfig,
cachedConfig,
defaultConfig,
headersPath,
redirectsPath,
configMutations,
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion packages/config/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
27 changes: 27 additions & 0 deletions packages/config/tests/mutate/tests.js
Original file line number Diff line number Diff line change
@@ -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))

Expand Down Expand Up @@ -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'))
})

0 comments on commit 011ce3a

Please sign in to comment.