From 31c2399926efbef5d502111650f16888dbc2cb42 Mon Sep 17 00:00:00 2001 From: Ashot Petrosian Date: Sat, 5 Dec 2026 17:56:57 -0500 Subject: [PATCH 1/2] cast check finds a workspace monorepo's packages when it has no root tsconfig or config --- packages/cli/src/check.test.ts | 16 +++++++++++ packages/cli/src/check.ts | 39 ++++++++++++++++++++++++-- packages/cli/src/checkAffected.test.ts | 18 ++++++++++++ packages/cli/src/checkAffected.ts | 7 +++-- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/check.test.ts b/packages/cli/src/check.test.ts index 6c1acba216..702a317ecd 100644 --- a/packages/cli/src/check.test.ts +++ b/packages/cli/src/check.test.ts @@ -260,6 +260,22 @@ 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 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'); diff --git a/packages/cli/src/check.ts b/packages/cli/src/check.ts index e24c8ddf98..44558eab5c 100644 --- a/packages/cli/src/check.ts +++ b/packages/cli/src/check.ts @@ -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(); + 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. */ @@ -89,8 +122,10 @@ 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}`, ); } diff --git a/packages/cli/src/checkAffected.test.ts b/packages/cli/src/checkAffected.test.ts index e3b03ecf29..9b1a5a90a3 100644 --- a/packages/cli/src/checkAffected.test.ts +++ b/packages/cli/src/checkAffected.test.ts @@ -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"]); diff --git a/packages/cli/src/checkAffected.ts b/packages/cli/src/checkAffected.ts index 53bcc249fe..29e32abeee 100644 --- a/packages/cli/src/checkAffected.ts +++ b/packages/cli/src/checkAffected.ts @@ -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 @@ -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); } From 886220cd32bc3c462fb52e5904f1ff8581abe884 Mon Sep 17 00:00:00 2001 From: Ashot Petrosian Date: Sat, 5 Dec 2026 18:21:35 -0500 Subject: [PATCH 2/2] cast check reads back the names it mints for workspace packages Codecast-Session: https://codecast.sh/conversation/jx723v27sc92rh0vh5yngz1d9x8kh072 --- packages/cli/src/check.test.ts | 3 +++ packages/cli/src/check.ts | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/check.test.ts b/packages/cli/src/check.test.ts index 702a317ecd..ef8335b1e9 100644 --- a/packages/cli/src/check.test.ts +++ b/packages/cli/src/check.test.ts @@ -271,6 +271,9 @@ describe("which programs a tree carries", () => { { 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 ` 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" }]); diff --git a/packages/cli/src/check.ts b/packages/cli/src/check.ts index 44558eab5c..2ba2c91061 100644 --- a/packages/cli/src/check.ts +++ b/packages/cli/src/check.ts @@ -131,8 +131,11 @@ export function resolveProjects(root: string, names: string[], cwd = process.cwd } 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). @@ -157,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)`); }