Skip to content
Merged
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
19 changes: 19 additions & 0 deletions packages/cli/src/check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,25 @@ describe("which programs a tree carries", () => {
expect(() => resolveProjects(tree, ["nope"], tree)).toThrow(/one of a, b, gone/);
});

test("with no config and no tsconfig above the caller, the workspace packages that hold one", () => {
expect(() => resolveProjects(tree, [], tree)).toThrow(/no workspace package holds one/);
fs.mkdirSync(path.join(tree, "tools/x"), { recursive: true });
fs.writeFileSync(path.join(tree, "tools/x/tsconfig.json"), "{}");
fs.mkdirSync(path.join(tree, "packages/no-ts"), { recursive: true });
fs.writeFileSync(path.join(tree, "package.json"), JSON.stringify({ workspaces: ["packages/*", "./tools/x"] }));
expect(resolveProjects(tree, [], tree)).toEqual([
{ name: "packages-a", tsconfig: "packages/a/tsconfig.json" },
{ name: "packages-b", tsconfig: "packages/b/tsconfig.json" },
{ name: "tools-x", tsconfig: "tools/x/tsconfig.json" },
]);
// The names it mints read back, as `cast check --fresh <name>` hints write them.
expect(resolveProjects(tree, ["packages-b"], tree)).toEqual([{ name: "packages-b", tsconfig: "packages/b/tsconfig.json" }]);
expect(() => resolveProjects(tree, ["nope"], tree)).toThrow(/one of packages-a, packages-b, tools-x/);
// The yarn form, { packages: [...] }.
fs.writeFileSync(path.join(tree, "package.json"), JSON.stringify({ workspaces: { packages: ["packages/a"] } }));
expect(resolveProjects(tree, [], tree)).toEqual([{ name: "packages-a", tsconfig: "packages/a/tsconfig.json" }]);
});

test("a path to a configured project's tsconfig is that project, not a second watcher", () => {
fs.mkdirSync(path.join(tree, ".codecast"));
fs.writeFileSync(path.join(tree, ".codecast/check.toml"), '[projects]\na = "packages/a/tsconfig.json"\n');
Expand Down
46 changes: 42 additions & 4 deletions packages/cli/src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,42 @@ export function nearestTsconfig(root: string, from: string): string | null {
}
}

/**
* The programs of a workspace monorepo with no config and no root tsconfig:
* each package the root package.json's `workspaces` names ("packages/*" or a
* plain path) that holds a tsconfig.json. Empty when there are none.
*/
export function workspaceProjects(root: string): CheckProject[] {
let globs: unknown;
try {
const pkg = JSON.parse(fs.readFileSync(path.join(root, "package.json"), "utf-8"));
globs = Array.isArray(pkg.workspaces) ? pkg.workspaces : pkg.workspaces?.packages;
} catch {
return [];
}
if (!Array.isArray(globs)) return [];
const dirs = new Set<string>();
for (const glob of globs) {
if (typeof glob !== "string" || glob.startsWith("!")) continue;
const norm = glob.replace(/^\.\//, "").replace(/\/+$/, "");
if (norm.endsWith("/*") && !norm.slice(0, -2).includes("*")) {
const parent = norm.slice(0, -2);
let entries: fs.Dirent[] = [];
try { entries = fs.readdirSync(path.join(root, parent), { withFileTypes: true }); } catch {}
for (const e of entries) if (e.isDirectory()) dirs.add(`${parent}/${e.name}`);
} else if (!norm.includes("*")) {
dirs.add(norm);
}
}
return [...dirs].sort()
.filter((dir) => fs.existsSync(path.join(root, dir, "tsconfig.json")))
.map((dir) => ({ name: slugOf(dir), tsconfig: `${dir}/tsconfig.json` }));
}

/**
* The projects a call means. No names: every project the tree's config
* lists, or, without a config, the program nearest the caller's directory.
* lists, or, without a config, the program nearest the caller's directory,
* or, with none above it either, every workspace package's program.
* A name is a configured project, or the path of a directory holding a
* tsconfig.json, or the path of a tsconfig file.
*/
Expand All @@ -89,15 +122,20 @@ export function resolveProjects(root: string, names: string[], cwd = process.cwd
if (configured) return configured.filter((p) => fs.existsSync(path.join(root, p.tsconfig)));
const nearest = nearestTsconfig(root, cwd);
if (!nearest) {
const packages = workspaceProjects(root);
if (packages.length) return packages;
throw new Error(
`no tsconfig.json between ${cwd} and ${root}, and no ${CHECK_CONFIG_REL_PATH} names the tree's projects; ` +
`no tsconfig.json between ${cwd} and ${root}, no workspace package holds one, and no ${CHECK_CONFIG_REL_PATH} names the tree's projects; ` +
`name one by path (cast check packages/foo), or add a [projects] table to ${CHECK_CONFIG_REL_PATH}`,
);
}
return [{ name: slugOf(path.dirname(nearest)), tsconfig: nearest }];
}
// Without a config, the names a bare call minted for workspace packages
// (`packages-web`) read back, so a hint like `cast check --fresh packages-web` works.
const named = configured ?? workspaceProjects(root);
return names.map((name) => {
const hit = configured?.find((p) => p.name === name);
const hit = named.find((p) => p.name === name);
if (hit) return hit;
// A path reads from the caller's directory first, then from the tree root,
// the way the config and the docs write paths (`outreach/backend` from inside outreach/backend).
Expand All @@ -122,7 +160,7 @@ export function resolveProjects(root: string, names: string[], cwd = process.cwd
// name would start a second watcher holding the same program.
return configured?.find((p) => path.normalize(p.tsconfig) === rel) ?? { name: slugOf(path.dirname(rel)), tsconfig: rel };
}
const known = configured?.map((p) => p.name).join(", ");
const known = named.map((p) => p.name).join(", ");
if (fs.existsSync(file)) {
throw new Error(`'${name}' is outside this tree (${root}); a watcher belongs to one tree, so run cast check from inside that folder (cd ${path.dirname(file)} && cast check)`);
}
Expand Down
18 changes: 18 additions & 0 deletions packages/cli/src/checkAffected.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ describe("projectsForAsk", () => {
expect(changedFiles(root, "origin/main")).toEqual(["packages/cli/src/a.ts", "packages/web/café.ts"]);
});

test("bare, with no config or root tsconfig: the workspace packages the changes reach", () => {
fs.rmSync(path.join(root, ".codecast"), { recursive: true });
write("package.json", JSON.stringify({ workspaces: ["packages/*"] }));
for (const p of ["cli", "convex", "web"]) {
const from = watchDir(root, p);
const to = watchDir(root, `packages-${p}`);
fs.mkdirSync(to, { recursive: true });
fs.copyFileSync(buildInfoPath(from), buildInfoPath(to));
}
git("add", "-A");
git("commit", "-qm", "workspaces");
git("update-ref", "refs/remotes/origin/main", "HEAD");
write("packages/cli/src/a.ts", "export const a = 1;\n");
expect(names()).toEqual(["packages-cli"]);
// Inside a package, its own program is the one asked about.
expect(projectsForAsk(root, [], { cwd: path.join(root, "packages/web") }).projects.map((p) => p.name)).toEqual(["packages-web"]);
});

test("bare: a change to convex reaches every program that compiles it", () => {
write("packages/convex/f.ts", "export const f = 1;\n");
expect(names()).toEqual(["convex", "web"]);
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/checkAffected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import * as fs from "node:fs";
import * as path from "node:path";
import { spawnSync } from "./proc.js";
import { buildInfoPath, gitLines, readBuildInfo, readCheckConfig, resolveProjects, watchDir, type CheckProject } from "./check.js";
import { buildInfoPath, gitLines, nearestTsconfig, readBuildInfo, readCheckConfig, resolveProjects, watchDir, type CheckProject } from "./check.js";

/**
* Repo-relative paths in a project's program, lowercased, read from
Expand Down Expand Up @@ -183,7 +183,10 @@ export function changedFiles(root: string, base: string): string[] | null {
*/
export function projectsForAsk(root: string, names: string[], opts: { all?: boolean; cwd?: string } = {}): { projects: CheckProject[]; why: string | null } {
const listed = resolveProjects(root, names, opts.cwd);
if (names.length || opts.all || !readCheckConfig(root)) return { projects: listed, why: null };
if (names.length || opts.all) return { projects: listed, why: null };
// Without a config the set is the one program nearest the caller, or, in a
// monorepo with no root tsconfig, every workspace package: narrow only that.
if (!readCheckConfig(root) && nearestTsconfig(root, opts.cwd ?? process.cwd())) return { projects: listed, why: null };
return affectedProjects(root, listed);
}

Expand Down
Loading