Skip to content

Commit

Permalink
Add merge logic for targetConfig #816
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvv committed Dec 18, 2024
1 parent f3df3a0 commit 3a9d1fe
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/commands/info/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export async function infoAction(options: InfoActionOptions, command: Command) {
outputs: config.cacheOptions.outputGlob,
tasks,
packageInfos,
enableTargetConfigMerging: config.enableTargetConfigMerging,
});

const scope = getFilteredPackages({
Expand Down
19 changes: 17 additions & 2 deletions packages/cli/src/commands/run/createTargetGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface CreateTargetGraphOptions {
outputs: string[];
tasks: string[];
packageInfos: PackageInfos;
enableTargetConfigMerging: boolean;
}

function getChangedFiles(since: string, cwd: string) {
Expand All @@ -37,9 +38,23 @@ function getChangedFiles(since: string, cwd: string) {
}

export async function createTargetGraph(options: CreateTargetGraphOptions) {
const { logger, root, dependencies, dependents, since, scope, repoWideChanges, ignore, pipeline, outputs, tasks, packageInfos } = options;
const {
logger,
root,
dependencies,
dependents,
enableTargetConfigMerging,
since,
scope,
repoWideChanges,
ignore,
pipeline,
outputs,
tasks,
packageInfos,
} = options;

const builder = new WorkspaceTargetGraphBuilder(root, packageInfos);
const builder = new WorkspaceTargetGraphBuilder(root, packageInfos, enableTargetConfigMerging);

const packages = getFilteredPackages({
root,
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/run/runAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export async function runAction(options: RunOptions, command: Command) {
outputs: config.cacheOptions.outputGlob,
tasks,
packageInfos,
enableTargetConfigMerging: config.enableTargetConfigMerging,
});

validateTargetGraph(targetGraph, allowNoTargetRuns);
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/run/watchAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export async function watchAction(options: RunOptions, command: Command) {
outputs: config.cacheOptions.outputGlob,
tasks,
packageInfos,
enableTargetConfigMerging: config.enableTargetConfigMerging,
});

// Make sure we do not attempt writeRemoteCache in watch mode
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/server/lageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async function createInitializedPromise({ cwd, logger, serverControls, nodeArg,
outputs: config.cacheOptions.outputGlob,
tasks,
packageInfos,
enableTargetConfigMerging: config.enableTargetConfigMerging,
});

const dependencyMap = createDependencyMap(packageInfos, { withDevDependencies: true, withPeerDependencies: false });
Expand Down
47 changes: 43 additions & 4 deletions packages/cli/tests/__snapshots__/createTargetGraph.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ exports[`createTargetGraph Basic graph, spanning tasks 1`] = `
]"
`;

exports[`createTargetGraph PackageJsonOverrideForTargetDeps 1`] = `
exports[`createTargetGraph Merging Dependencies in pipeline and package.json override 1`] = `
"[
{
"id": "__start",
Expand All @@ -68,7 +68,9 @@ exports[`createTargetGraph PackageJsonOverrideForTargetDeps 1`] = `
"id": "foo1#build",
"dependencies": [
"__start",
"foo2#build"
"foo2#build",
"foo3#build",
"foo4#build"
]
},
{
Expand All @@ -78,16 +80,53 @@ exports[`createTargetGraph PackageJsonOverrideForTargetDeps 1`] = `
]
},
{
"id": "foo1#test",
"id": "foo3#build",
"dependencies": [
"__start"
]
},
{
"id": "foo2#test",
"id": "foo4#build",
"dependencies": [
"__start"
]
}
]"
`;

exports[`createTargetGraph Merging inputs and outputs in pipeline and package.json override 1`] = `
"[
{
"id": "__start",
"dependencies": []
},
{
"id": "foo1#build",
"dependencies": [
"__start",
"foo2#build"
],
"inputs": [
"src/**",
"src/**",
"myTool.config",
"tsconfig.json"
],
"outputs": [
"lib/**",
"dist/**"
]
},
{
"id": "foo2#build",
"dependencies": [
"__start"
],
"inputs": [
"src/**"
],
"outputs": [
"lib/**"
]
}
]"
Expand Down
48 changes: 36 additions & 12 deletions packages/cli/tests/createTargetGraph.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import { getFilteredPackages } from "../src/filter/getFilteredPackages.js";
import { getBinPaths } from "../src/getBinPaths.js";

describe("createTargetGraph", () => {
const logger = new Logger();

it("Basic graph, seperate nodes", async () => {
const packageInfos: PackageInfos = {
foo1: stubPackage({ name: "foo1", deps: ["foo2"] }),
foo2: stubPackage({ name: "foo2" }),
foo1: stubPackage({ name: "foo1", scripts: ["build"], deps: ["foo2"] }),
foo2: stubPackage({ name: "foo2", scripts: ["build"] }),
};

const pipeline: PipelineDefinition = {
Expand All @@ -28,8 +26,8 @@ describe("createTargetGraph", () => {

it("Basic graph, spanning tasks", async () => {
const packageInfos: PackageInfos = {
foo1: stubPackage({ name: "foo1", deps: ["foo2"] }),
foo2: stubPackage({ name: "foo2" }),
foo1: stubPackage({ name: "foo1", scripts: ["build"], deps: ["foo2"] }),
foo2: stubPackage({ name: "foo2", scripts: ["build"] }),
};

const pipeline: PipelineDefinition = {
Expand All @@ -41,18 +39,43 @@ describe("createTargetGraph", () => {
expect(result).toMatchSnapshot();
});

it("PackageJsonOverrideForTargetDeps", async () => {
it("Merging Dependencies in pipeline and package.json override", async () => {
const packageInfos: PackageInfos = {
foo1: stubPackage({ name: "foo1", deps: ["foo2"] }),
foo2: stubPackage({ name: "foo2", fields: { lage: { test: ["build"] } } }),
foo1: stubPackage({ name: "foo1", scripts: ["build"], deps: ["foo2"], fields: { lage: { build: ["foo4#build"] } } }),
foo2: stubPackage({ name: "foo2", scripts: ["build"] }),
foo3: stubPackage({ name: "foo3", scripts: ["build"] }),
foo4: stubPackage({ name: "foo4", scripts: ["build"] }),
};

const pipeline: PipelineDefinition = {
build: ["^build"],
test: [],
"foo1#build": ["foo3#build"],
};

const result = await createAndPrintPackageTasks(["build", "test"], packageInfos, pipeline, ["id", "dependencies"]);
const result = await createAndPrintPackageTasks(["build"], packageInfos, pipeline, ["id", "dependencies"]);
expect(result).toMatchSnapshot();
});

it("Merging inputs and outputs in pipeline and package.json override", async () => {
const packageInfos: PackageInfos = {
foo1: stubPackage({
name: "foo1",
deps: ["foo2"],
scripts: ["build"],
fields: { lage: { build: { inputs: ["tsconfig.json"], outputs: ["dist/**"] } } },
}),
foo2: stubPackage({ name: "foo2", scripts: ["build"] }),
};

const pipeline: PipelineDefinition = {
build: { inputs: ["src/**"], outputs: ["lib/**"] },
"foo1#build": {
inputs: ["src/**", "myTool.config"],
dependsOn: ["foo2#build"],
},
};

const result = await createAndPrintPackageTasks(["build"], packageInfos, pipeline, ["id", "dependencies", "inputs", "outputs"]);
expect(result).toMatchSnapshot();
});
});
Expand All @@ -64,7 +87,7 @@ async function createAndPrintPackageTasks(
fields: string[]
): Promise<string> {
const packageTasks = await createPackageTasks(tasks, packageInfos, pipeline);
const expected = filterObjects(packageTasks, ["id", "dependencies"]);
const expected = filterObjects(packageTasks, fields);
return JSON.stringify(expected, null, 2);
}

Expand Down Expand Up @@ -114,6 +137,7 @@ async function createPackageTasks(tasks: string[], packageInfos: PackageInfos, p
outputs: config.cacheOptions.outputGlob,
tasks,
packageInfos,
enableTargetConfigMerging: true,
});

const scope = getFilteredPackages({
Expand Down
1 change: 1 addition & 0 deletions packages/config/src/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ export async function getConfig(cwd: string): Promise<ConfigOptions> {
workerIdleMemoryLimit: config?.workerIdleMemoryLimit ?? os.totalmem(), // 0 means no limit,
concurrency: config?.concurrency ?? availableParallelism,
allowNoTargetRuns: config?.allowNoTargetRuns ?? false,
enableTargetConfigMerging: config?.enableTargetConfigMerging ?? false,
};
}
5 changes: 5 additions & 0 deletions packages/config/src/types/ConfigOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ export interface ConfigOptions {
* Allows for no targets run
*/
allowNoTargetRuns: boolean;

/**
* Enables the merging of target config files, rather than simply replace it when multiple matches are encoutered
*/
enableTargetConfigMerging: boolean;
}
14 changes: 14 additions & 0 deletions packages/e2e-tests/src/__snapshots__/info.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exports[`info command basic info test case 1`] = `
"id": "__start",
"package": "",
"task": "__start",
"weight": 1,
"workingDirectory": "",
},
{
Expand All @@ -25,8 +26,10 @@ exports[`info command basic info test case 1`] = `
"a#build",
],
"id": "a#test",
"options": {},
"package": "a",
"task": "test",
"weight": 1,
"workingDirectory": "packages/a",
},
{
Expand All @@ -38,8 +41,10 @@ exports[`info command basic info test case 1`] = `
"b#build",
],
"id": "b#test",
"options": {},
"package": "b",
"task": "test",
"weight": 1,
"workingDirectory": "packages/b",
},
{
Expand All @@ -51,8 +56,10 @@ exports[`info command basic info test case 1`] = `
"b#build",
],
"id": "a#build",
"options": {},
"package": "a",
"task": "build",
"weight": 1,
"workingDirectory": "packages/a",
},
{
Expand All @@ -64,8 +71,10 @@ exports[`info command basic info test case 1`] = `
"__start",
],
"id": "b#build",
"options": {},
"package": "b",
"task": "build",
"weight": 1,
"workingDirectory": "packages/b",
},
],
Expand Down Expand Up @@ -94,6 +103,7 @@ exports[`info command scoped info test case 1`] = `
"id": "__start",
"package": "",
"task": "__start",
"weight": 1,
"workingDirectory": "",
},
{
Expand All @@ -105,8 +115,10 @@ exports[`info command scoped info test case 1`] = `
"b#build",
],
"id": "b#test",
"options": {},
"package": "b",
"task": "test",
"weight": 1,
"workingDirectory": "packages/b",
},
{
Expand All @@ -118,8 +130,10 @@ exports[`info command scoped info test case 1`] = `
"__start",
],
"id": "b#build",
"options": {},
"package": "b",
"task": "build",
"weight": 1,
"workingDirectory": "packages/b",
},
],
Expand Down
3 changes: 2 additions & 1 deletion packages/target-graph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"lint": "monorepo-scripts lint"
},
"dependencies": {
"mergician": "^2.0.2",
"p-limit": "^3.1.0",
"workspace-tools": "0.38.1"
},
Expand All @@ -27,4 +28,4 @@
"publishConfig": {
"access": "public"
}
}
}
Loading

0 comments on commit 3a9d1fe

Please sign in to comment.