Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add to graph scripts missing from package.json #593

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Don't add to graph scripts missing from package.json",
"packageName": "@lage-run/target-graph",
"email": "felescoto95@hotmail.com",
"dependentChangeType": "patch"
}
4 changes: 2 additions & 2 deletions packages/e2e-tests/src/transitiveTaskDeps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { filterEntry, parseNdJson } from "./parseNdJson.js";
describe("transitive task deps test", () => {
// This test follows the model as documented here:
// https://microsoft.github.io/lage/guide/levels.html
it("produces a build graph even when some scripts are missing in package.json", () => {
it("produces a the correct build graph when some scripts are missing in package.json", () => {
const repo = new Monorepo("transitiveDeps");

repo.init();
Expand Down Expand Up @@ -44,7 +44,7 @@ describe("transitive task deps test", () => {

expect(indices[getTargetId("a", "build")]).toBeLessThan(indices[getTargetId("a", "test")]);

expect(indices[getTargetId("b", "build")]).toBeLessThan(indices[getTargetId("a", "test")]);
expect(indices[getTargetId("b", "build")]).toBeUndefined();

repo.cleanup();
});
Expand Down
12 changes: 10 additions & 2 deletions packages/target-graph/src/WorkspaceTargetGraphBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,19 @@ export class WorkspaceTargetGraphBuilder {
for (const task of tasks) {
if (scope) {
for (const packageName of scope) {
subGraphEntries.push(getTargetId(packageName, task));
const packageInfo = this.packageInfos[packageName];
// Do not add a package to the subgraph if the package does not have the task
if (packageInfo.scripts && packageInfo.scripts[task]) {
subGraphEntries.push(getTargetId(packageName, task));
}
}
} else {
for (const packageName of Object.keys(this.packageInfos)) {
subGraphEntries.push(getTargetId(packageName, task));
const packageInfo = this.packageInfos[packageName];
// Do not add a package to the subgraph if the package does not have the task
if (packageInfo.scripts && packageInfo.scripts[task]) {
subGraphEntries.push(getTargetId(packageName, task));
}
}
}
}
Expand Down
87 changes: 68 additions & 19 deletions packages/target-graph/tests/WorkspaceTargetGraphBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import type { PackageInfos } from "workspace-tools";
import { WorkspaceTargetGraphBuilder } from "../src/WorkspaceTargetGraphBuilder";
import type { TargetGraph } from "../src/types/TargetGraph";

function createPackageInfo(packages: { [id: string]: string[] }) {
function createPackageInfo(packages: { [id: string]: { tasks: string[]; dependencies: string[] } }) {
const packageInfos: PackageInfos = {};
Object.keys(packages).forEach((id) => {
packageInfos[id] = {
packageJsonPath: `/path/to/${id}/package.json`,
name: id,
version: "1.0.0",
dependencies: packages[id].reduce((acc, dep) => {
scripts: packages[id].tasks.reduce((acc, task) => {
return { ...acc, [task]: "noop" };
}, {}),
dependencies: packages[id].dependencies.reduce((acc, dep) => {
return { ...acc, [dep]: "*" };
}, {}),
};
Expand All @@ -34,8 +37,8 @@ describe("workspace target graph builder", () => {
const root = "/repos/a";

const packageInfos = createPackageInfo({
a: ["b"],
b: [],
a: { dependencies: ["b"], tasks: ["build"] },
b: { dependencies: [], tasks: ["build"] },
});

const builder = new WorkspaceTargetGraphBuilder(root, packageInfos);
Expand Down Expand Up @@ -69,8 +72,8 @@ describe("workspace target graph builder", () => {
it("should generate target graphs for tasks that do not depend on each other", () => {
const root = "/repos/a";
const packageInfos = createPackageInfo({
a: ["b"],
b: [],
a: { dependencies: ["b"], tasks: ["test", "lint"] },
b: { dependencies: [], tasks: ["test", "lint"] },
});

const builder = new WorkspaceTargetGraphBuilder(root, packageInfos);
Expand Down Expand Up @@ -107,9 +110,9 @@ describe("workspace target graph builder", () => {
const root = "/repos/a";

const packageInfos = createPackageInfo({
a: ["b"],
b: [],
c: ["b"],
a: { dependencies: ["b"], tasks: ["build"] },
b: { dependencies: [], tasks: ["build"] },
c: { dependencies: ["b"], tasks: ["build"] },
});

const builder = new WorkspaceTargetGraphBuilder(root, packageInfos);
Expand Down Expand Up @@ -149,9 +152,9 @@ describe("workspace target graph builder", () => {
const root = "/repos/a";

const packageInfos = createPackageInfo({
a: ["b"],
b: [],
c: ["b"],
a: { dependencies: ["b"], tasks: ["build"] },
b: { dependencies: [], tasks: ["build"] },
c: { dependencies: ["b"], tasks: ["build"] },
});

const builder = new WorkspaceTargetGraphBuilder(root, packageInfos);
Expand Down Expand Up @@ -179,13 +182,59 @@ describe("workspace target graph builder", () => {
`);
});

it("should generate targetGraph without dependencies not needed to run the target", () => {
const root = "/repos/a";

const packageInfos = createPackageInfo({
a: { dependencies: ["b"], tasks: ["build"] },
b: { dependencies: [], tasks: ["build"] },
c: { dependencies: ["b"], tasks: ["build", "test"] },
});

const builder = new WorkspaceTargetGraphBuilder(root, packageInfos);

builder.addTargetConfig("build", {
dependsOn: ["^build"],
});

builder.addTargetConfig("test", {
dependsOn: ["build"],
});

const targetGraph = builder.build(["test"], ["a", "b", "c"]);
expect(getGraphFromTargets(targetGraph)).toMatchInlineSnapshot(`
[
[
"__start",
"c#test",
],
[
"c#build",
"c#test",
],
[
"__start",
"c#build",
],
[
"b#build",
"c#build",
],
[
"__start",
"b#build",
],
]
`);
});

it("should generate targetGraph with transitive dependencies", () => {
const root = "/repos/a";

const packageInfos = createPackageInfo({
a: ["b"],
b: ["c"],
c: [],
a: { dependencies: ["b"], tasks: ["bundle", "transpile"] },
b: { dependencies: ["c"], tasks: ["bundle", "transpile"] },
c: { dependencies: [], tasks: ["bundle", "transpile"] },
});

const builder = new WorkspaceTargetGraphBuilder(root, packageInfos);
Expand Down Expand Up @@ -227,10 +276,10 @@ describe("workspace target graph builder", () => {
const root = "/repos/a";

const packageInfos = createPackageInfo({
a: [],
b: [],
c: [],
common: [],
a: { dependencies: [], tasks: ["build"] },
b: { dependencies: [], tasks: ["build"] },
c: { dependencies: [], tasks: ["build"] },
common: { dependencies: [], tasks: ["build", "copy"] },
});

const builder = new WorkspaceTargetGraphBuilder(root, packageInfos);
Expand Down