Skip to content

Commit

Permalink
fix(fluid-build): Load default config when no config is found (#22825)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tylerbutler authored Oct 17, 2024
1 parent 693dcf2 commit 8884365
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
18 changes: 2 additions & 16 deletions build-tools/packages/build-tools/src/fluidBuild/fluidRepoBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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() {
Expand Down
38 changes: 25 additions & 13 deletions build-tools/packages/build-tools/src/fluidBuild/fluidUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand 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.",
Expand Down

0 comments on commit 8884365

Please sign in to comment.