From 88843657f4be9fa1fa4fe5e0370b6f120ee3b090 Mon Sep 17 00:00:00 2001 From: Tyler Butler Date: Thu, 17 Oct 2024 10:01:47 -0700 Subject: [PATCH] fix(fluid-build): Load default config when no config is found (#22825) fluid-build was failing to load in repos without a config file. This behavior was inadvertently broken when the `getFluidBuildConfig` function was changed to throw an error when a config was not found. This prevented existing default config fallback logic to be used. This change clarifies what the default config is, returns it consistently from `getFluidBuildConfig` when no config is found, and updates the FluidRepo loading logic to account for the change. --- .../src/fluidBuild/buildContext.ts | 2 +- .../build-tools/src/fluidBuild/buildGraph.ts | 2 +- .../build-tools/src/fluidBuild/fluidBuild.ts | 2 +- .../src/fluidBuild/fluidBuildConfig.ts | 10 +++++ .../src/fluidBuild/fluidRepoBuild.ts | 18 +-------- .../build-tools/src/fluidBuild/fluidUtils.ts | 38 ++++++++++++------- 6 files changed, 40 insertions(+), 32 deletions(-) diff --git a/build-tools/packages/build-tools/src/fluidBuild/buildContext.ts b/build-tools/packages/build-tools/src/fluidBuild/buildContext.ts index 7661ea7aa08a..b7291541ac11 100644 --- a/build-tools/packages/build-tools/src/fluidBuild/buildContext.ts +++ b/build-tools/packages/build-tools/src/fluidBuild/buildContext.ts @@ -14,7 +14,7 @@ export interface BuildContext { /** * The fluid-build configuration for the repo. */ - readonly fluidBuildConfig: IFluidBuildConfig | undefined; + readonly fluidBuildConfig: IFluidBuildConfig; /** * The absolute path to the root of the repo. diff --git a/build-tools/packages/build-tools/src/fluidBuild/buildGraph.ts b/build-tools/packages/build-tools/src/fluidBuild/buildGraph.ts index 6a86e4d45bff..b1fdbbb55d5f 100644 --- a/build-tools/packages/build-tools/src/fluidBuild/buildGraph.ts +++ b/build-tools/packages/build-tools/src/fluidBuild/buildGraph.ts @@ -67,7 +67,7 @@ class BuildGraphContext implements BuildContext { public readonly fileHashCache = new FileHashCache(); public readonly taskStats = new TaskStats(); public readonly failedTaskLines: string[] = []; - public readonly fluidBuildConfig: IFluidBuildConfig | undefined; + public readonly fluidBuildConfig: IFluidBuildConfig; public readonly repoRoot: string; public readonly gitRepo: GitRepo; constructor( diff --git a/build-tools/packages/build-tools/src/fluidBuild/fluidBuild.ts b/build-tools/packages/build-tools/src/fluidBuild/fluidBuild.ts index 6ac0c2e4d1cd..fb5cc8b5c485 100644 --- a/build-tools/packages/build-tools/src/fluidBuild/fluidBuild.ts +++ b/build-tools/packages/build-tools/src/fluidBuild/fluidBuild.ts @@ -25,7 +25,7 @@ async function main() { log(`Build Root: ${resolvedRoot}`); // Load the packages - const repo = FluidRepoBuild.create({ + const repo = new FluidRepoBuild({ repoRoot: resolvedRoot, gitRepo: new GitRepo(resolvedRoot), fluidBuildConfig: getFluidBuildConfig(resolvedRoot), diff --git a/build-tools/packages/build-tools/src/fluidBuild/fluidBuildConfig.ts b/build-tools/packages/build-tools/src/fluidBuild/fluidBuildConfig.ts index bf2554b08184..3489f277ac64 100644 --- a/build-tools/packages/build-tools/src/fluidBuild/fluidBuildConfig.ts +++ b/build-tools/packages/build-tools/src/fluidBuild/fluidBuildConfig.ts @@ -15,6 +15,16 @@ import { TaskDefinitionsOnDisk } from "./fluidTaskDefinitions"; */ export const FLUIDBUILD_CONFIG_VERSION = 1; +/** + * The default fluid-build config if one is not found. + */ +export const DEFAULT_FLUIDBUILD_CONFIG: IFluidBuildConfig = { + version: FLUIDBUILD_CONFIG_VERSION, + repoPackages: { + root: "", + }, +}; + /** * Top-most configuration for repo build settings. */ diff --git a/build-tools/packages/build-tools/src/fluidBuild/fluidRepoBuild.ts b/build-tools/packages/build-tools/src/fluidBuild/fluidRepoBuild.ts index 18d06365ebed..2120c4f9e3c2 100644 --- a/build-tools/packages/build-tools/src/fluidBuild/fluidRepoBuild.ts +++ b/build-tools/packages/build-tools/src/fluidBuild/fluidRepoBuild.ts @@ -14,7 +14,6 @@ import { Package, Packages } from "../common/npmPackage"; import { ExecAsyncResult, isSameFileOrDir, lookUpDirSync } from "../common/utils"; import type { BuildContext } from "./buildContext"; import { BuildGraph } from "./buildGraph"; -import type { IFluidBuildDirs } from "./fluidBuildConfig"; import { FluidRepo } from "./fluidRepo"; import { getFluidBuildConfig } from "./fluidUtils"; import { NpmDepChecker } from "./npmDepChecker"; @@ -33,21 +32,8 @@ export interface IPackageMatchedOptions { } export class FluidRepoBuild extends FluidRepo { - public static create(context: BuildContext) { - // Default to just resolveRoot if no config is found - const packageManifest = context.fluidBuildConfig ?? { - repoPackages: { - root: "", - }, - }; - return new FluidRepoBuild(context.repoRoot, context, packageManifest.repoPackages); - } - private constructor( - resolvedRoot: string, - protected context: BuildContext, - repoPackages?: IFluidBuildDirs, - ) { - super(resolvedRoot, repoPackages); + public constructor(protected context: BuildContext) { + super(context.repoRoot, context.fluidBuildConfig.repoPackages); } public async clean() { diff --git a/build-tools/packages/build-tools/src/fluidBuild/fluidUtils.ts b/build-tools/packages/build-tools/src/fluidBuild/fluidUtils.ts index 0afa3e1480e5..5d32da2311ca 100644 --- a/build-tools/packages/build-tools/src/fluidBuild/fluidUtils.ts +++ b/build-tools/packages/build-tools/src/fluidBuild/fluidUtils.ts @@ -14,7 +14,11 @@ import { readJson } from "fs-extra"; import { defaultLogger } from "../common/logging"; import { commonOptions } from "./commonOptions"; -import { FLUIDBUILD_CONFIG_VERSION, type IFluidBuildConfig } from "./fluidBuildConfig"; +import { + DEFAULT_FLUIDBUILD_CONFIG, + FLUIDBUILD_CONFIG_VERSION, + type IFluidBuildConfig, +} from "./fluidBuildConfig"; // switch to regular import once building ESM const findUp = import("find-up"); @@ -140,29 +144,37 @@ const configExplorer = cosmiconfigSync(configName, { packageProp: [configName], }); +/** + * Set to true when the default config is returned by getFluidBuildConfig so that repeated calls to the function don't + * result in repeated searches for config. + */ +let defaultConfigLoaded = false; + /** * Get an IFluidBuildConfig from the fluidBuild property in a package.json file, or from fluidBuild.config.[c]js. * - * @param rootDir - The path to the root package.json to load. - * @param noCache - If true, the config cache will be cleared and the config will be reloaded. - * @returns The fluidBuild section of the package.json, or undefined if not found + * @param searchDir - The path to search for the config. The search will look up the folder hierarchy for a config in + * either a standalone file or package.json + * @returns The the loaded fluidBuild config, or the default config if one is not found. */ export function getFluidBuildConfig( - rootDir: string, - noCache = false, + searchDir: string, log = defaultLogger, ): IFluidBuildConfig { - if (noCache === true) { - configExplorer.clearCaches(); + if (defaultConfigLoaded) { + return DEFAULT_FLUIDBUILD_CONFIG; } - const configResult = configExplorer.search(rootDir); - const config = configResult?.config as IFluidBuildConfig | undefined; - - if (config === undefined) { - throw new Error("No fluidBuild configuration found."); + const configResult = configExplorer.search(searchDir); + if (configResult?.config === undefined) { + log.warning( + `No fluidBuild config found when searching ${searchDir}; default configuration loaded. Packages and tasks will be inferred.`, + ); + defaultConfigLoaded = true; + return DEFAULT_FLUIDBUILD_CONFIG; } + const config = configResult.config; if (config.version === undefined) { log.warning( "fluidBuild config has no version field. This field will be required in a future release.",