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) => ({ 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