From 99b5cb9eb262910f63f408f8fd0f45009f24260c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Fri, 2 Feb 2024 09:54:12 +0100 Subject: [PATCH 1/2] Adding a `injectProjectRootAsWatchFolder` argument and fallback watchFolders to an empty array --- packages/metro-config/src/loadConfig.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/metro-config/src/loadConfig.js b/packages/metro-config/src/loadConfig.js index d2c65b0438..7e32b3d63a 100644 --- a/packages/metro-config/src/loadConfig.js +++ b/packages/metro-config/src/loadConfig.js @@ -290,13 +290,15 @@ function overrideConfigWithArguments( /** * Load the metro configuration from disk - * @param {object} argv Arguments coming from the CLI, can be empty - * @param {object} defaultConfigOverrides A configuration that can override the default config - * @return {object} Configuration returned + * @param {object} argv Arguments coming from the CLI, can be empty + * @param {object} defaultConfigOverrides A configuration that can override the default config + * @param {boolean} injectProjectRootAsWatchFolder Will ensure `projectRoot` is present in the `watchFolders` + * @return {object} Configuration returned */ async function loadConfig( argvInput?: YargArguments = {}, defaultConfigOverrides?: InputConfigT = {}, + injectProjectRootAsWatchFolder?: boolean = true, ): Promise { const argv = {...argvInput, config: overrideArgument(argvInput.config)}; @@ -321,10 +323,12 @@ async function loadConfig( const overriddenConfig: {[string]: mixed} = {}; - overriddenConfig.watchFolders = [ - configWithArgs.projectRoot, - ...configWithArgs.watchFolders, - ]; + if (injectProjectRootAsWatchFolder) { + overriddenConfig.watchFolders = [ + configWithArgs.projectRoot, + ...(configWithArgs.watchFolders ?? []), + ]; + } // Set the watchfolders to include the projectRoot, as Metro assumes that is // the case From e6de60117ea1e099cb417131d57de52250338ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kr=C3=A6n=20Hansen?= Date: Fri, 2 Feb 2024 09:54:18 +0100 Subject: [PATCH 2/2] Adding a test --- .../metro-config/src/__tests__/loadConfig-test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/metro-config/src/__tests__/loadConfig-test.js b/packages/metro-config/src/__tests__/loadConfig-test.js index 247913f8a7..ebf6355aa5 100644 --- a/packages/metro-config/src/__tests__/loadConfig-test.js +++ b/packages/metro-config/src/__tests__/loadConfig-test.js @@ -166,6 +166,21 @@ describe('loadConfig', () => { expect(prettyFormat(result)).toEqual(prettyFormat(defaultConfig)); }); + it('does not inject projectRoot into watchFolders when called with a third `false` argument', async () => { + cosmiconfig.setReturnNull(true); + + const result = await loadConfig( + {cwd: process.cwd()}, + {watchFolders: undefined}, + false, + ); + const defaultConfig = { + ...(await getDefaultConfig(process.cwd())), + watchFolders: undefined, + }; + expect(prettyFormat(result)).toEqual(prettyFormat(defaultConfig)); + }); + it('validates config for server', async () => { expect.assertions(1); const config = (defaultConfig: any) => ({