diff --git a/docs/using-diffci.md b/docs/using-diffci.md index 8a7833ab..ebf50794 100644 --- a/docs/using-diffci.md +++ b/docs/using-diffci.md @@ -479,7 +479,7 @@ For `Unknown changed file` and `Deleted source/asset`, `observe --json` also ret | `Infrastructure definition(s) changed; full validation required` | Edit a `Dockerfile` or `terraform/main.tf` | Yes: declare it in `inputs` | | `Database definition(s) changed; full validation required` | Add a file under `migrations/` | Yes: declare it in `inputs` | | `Language/framework configuration changed: …` | Edit `go.mod` or `pom.xml` | No | -| `Tool configuration changed: … (…); full validation required` | Change `.nvmrc`, `.node-version` or `.npmrc` | No | +| `Tool configuration changed: … (…); full validation required` | Change `.nvmrc`, `.node-version`, `.npmrc`, `tsconfig.base.json` or `tsconfig.test.json` | No | | `Unknown changed file: …` | Change a file type no adapter models, such as `data.bin` read at runtime | Yes: declare it in `inputs` | | `Infrastructure code … is outside the dependency graph; full validation required` | Edit a script under `ops/` that no test imports | Yes: declare it in `inputs` | | `Lockfile readers could not be resolved: …; full validation required` | Change `yarn.lock` while repository code reads it in a way DiffCI cannot trace | No | diff --git a/release-files.json b/release-files.json index 8a0afde2..e3de03d4 100644 --- a/release-files.json +++ b/release-files.json @@ -264,6 +264,7 @@ "tests/repo/test-fixture-ownership.test.ts", "tests/repo/test-framework.test.ts", "tests/repo/tool-config-reasons.test.ts", + "tests/repo/tsconfig-variant-reason.test.ts", "tests/repo/tsconfig-newer-options.test.ts", "tests/repo/vite-config-change.test.ts", "tests/repo/vitest-config-fallback.test.ts", diff --git a/src/git/git-diff.ts b/src/git/git-diff.ts index e39646c8..960ba321 100644 --- a/src/git/git-diff.ts +++ b/src/git/git-diff.ts @@ -200,6 +200,12 @@ export const CONFIG_PATTERNS = [ /^next\.config\./, /^tsconfig\.json$/, /^jsconfig\.json$/, + // `tsconfig.base.json`, `tsconfig.test.json`, `jsconfig.build.json`: monorepos keep shared options in + // the first and projects carry per-target variants of the rest (issue #141). Deliberately narrower + // than type-program.ts's `tsconfig[^/]*\.json`, which would also match the package name + // `tsconfig-paths.json`. + /^tsconfig\.[^/]+\.json$/, + /^jsconfig\.[^/]+\.json$/, /^eslint\.config\./, /^\.eslint/, /^postcss\.config\./, diff --git a/src/repo/impact.ts b/src/repo/impact.ts index 7dc4e904..a756c093 100644 --- a/src/repo/impact.ts +++ b/src/repo/impact.ts @@ -61,6 +61,7 @@ function toolConfigurationSubject(filePath: string): string | undefined { const base = posix.basename(filePath); if (base === ".nvmrc" || base === ".node-version") return "Node version"; if (base === ".npmrc") return "package manager settings"; + if (/^tsconfig\.[^/]+\.json$/.test(base) || /^jsconfig\.[^/]+\.json$/.test(base)) return "TypeScript configuration"; return undefined; } diff --git a/tests/repo/config-patterns.test.ts b/tests/repo/config-patterns.test.ts index 2d98e7bc..37e09a04 100644 --- a/tests/repo/config-patterns.test.ts +++ b/tests/repo/config-patterns.test.ts @@ -16,6 +16,8 @@ const EXAMPLES = [ "next.config.mjs", "tsconfig.json", "jsconfig.json", + "tsconfig.base.json", + "jsconfig.test.json", "eslint.config.js", ".eslintrc.json", "postcss.config.cjs", @@ -63,7 +65,7 @@ CONFIG_PATTERNS.forEach((pattern, i) => { }); }); -for (const name of ["eslint.rc", "npm-lock.json", "yarn-lock.yaml"]) { +for (const name of ["eslint.rc", "npm-lock.json", "yarn-lock.yaml", "tsconfig-paths.json"]) { test(`no entry matches ${name}, which no tool reads`, () => { assert.ok(!CONFIG_PATTERNS.some((pattern) => pattern.test(name))); }); diff --git a/tests/repo/tsconfig-variant-reason.test.ts b/tests/repo/tsconfig-variant-reason.test.ts new file mode 100644 index 00000000..b3988713 --- /dev/null +++ b/tests/repo/tsconfig-variant-reason.test.ts @@ -0,0 +1,119 @@ +// SPDX-License-Identifier: MIT +/** + * `tsconfig.*.json` and `jsconfig.*.json` variants (issue #141). + * + * `tsconfig.json` changes were already configuration; a change to only `tsconfig.base.json` (extended by + * `tsconfig.json`, as monorepos do) or a standalone `tsconfig.test.json` fell back with + * `Unknown changed file: `. Running everything is right — a compiler configuration decides every + * file's types — but the reason read like a gap in DiffCI, so each is now named: + * `Tool configuration changed: TypeScript configuration (tsconfig.base.json); full validation required`. + * + * The patterns are deliberately narrower than `type-program.ts`'s `tsconfig[^/]*\.json`: `tsconfig-paths.json` + * is a package name, not a project configuration, and must stay unknown. + */ +import assert from "node:assert/strict"; +import { execFileSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { test } from "node:test"; +import { analyzeCheckout } from "../../src/analyze.js"; + +const TESTS = ["src/math.test.ts", "src/text.test.ts"]; + +const STRICT = JSON.stringify({ + compilerOptions: { strict: true, target: "es2022", module: "esnext", moduleResolution: "bundler" }, + include: ["src/**/*"], +}); +const LOOSE = JSON.stringify({ + compilerOptions: { strict: false, target: "es2022", module: "esnext", moduleResolution: "bundler" }, + include: ["src/**/*"], +}); + +function tsProject(extra: Record): Record { + return { + "package.json": JSON.stringify({ + name: "ts-config-fixture", + scripts: { test: "vitest" }, + devDependencies: { vitest: "3.0.0" }, + }), + "tsconfig.json": STRICT, + ...extra, + "src/math.ts": "export const add = (a: number, b: number) => a + b;\n", + "src/math.test.ts": "import { add } from './math';\ntest('add', () => expect(add(1, 2)).toBe(3));\n", + "src/text.ts": "export const shout = (s: string) => s.toUpperCase();\n", + "src/text.test.ts": "import { shout } from './text';\ntest('shout', () => expect(shout('a')).toBe('A'));\n", + }; +} + +async function planForChange(files: Record, changedPath: string, changedContent: string) { + const root = mkdtempSync(join(tmpdir(), "diffci-tsconfig-")); + try { + const git = (...args: string[]) => + execFileSync("git", args, { cwd: root, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim(); + git("init"); + git("config", "user.name", "Core Test"); + git("config", "user.email", "test@example.invalid"); + git("config", "commit.gpgsign", "false"); + for (const [path, content] of Object.entries(files)) { + mkdirSync(dirname(join(root, path)), { recursive: true }); + writeFileSync(join(root, path), content); + } + git("add", "."); + git("commit", "-m", "base"); + const base = git("rev-parse", "HEAD"); + writeFileSync(join(root, changedPath), changedContent); + git("add", "."); + git("commit", "-m", `change ${changedPath}`); + const head = git("rev-parse", "HEAD"); + return (await analyzeCheckout({ repoPath: root, base, head })).plan; + } finally { + rmSync(root, { recursive: true, force: true }); + } +} + +// The issue's two cases, plus a jsconfig variant. +for (const file of ["tsconfig.base.json", "tsconfig.test.json", "jsconfig.test.json"]) { + test(`a change to only ${file} runs every test and names it as TypeScript configuration`, async () => { + const plan = await planForChange(tsProject({ [file]: LOOSE }), file, STRICT); + assert.equal(plan.mode, "FULL"); + assert.equal(plan.safety.fallbackRequired, true); + assert.deepEqual([...plan.selectedTests].sort(), TESTS); + assert.deepEqual(plan.skippedTests, []); + assert.ok( + plan.fallbackReasons.some( + (text) => text.startsWith("Tool configuration changed: TypeScript configuration") && text.includes(file), + ), + plan.fallbackReasons.join("; "), + ); + assert.ok( + !plan.fallbackReasons.some((text) => text.includes(`Unknown changed file: ${file}`)), + `still reported as unknown: ${plan.fallbackReasons.join("; ")}`, + ); + }); +} + +// The root `tsconfig.json` keeps its original reason: the generic configuration sentence covers it, and +// this change must not move it. +test("a change to only tsconfig.json keeps the general configuration reason", async () => { + const plan = await planForChange(tsProject({}), "tsconfig.json", LOOSE); + assert.equal(plan.mode, "FULL"); + assert.ok( + plan.fallbackReasons.some((text) => text.startsWith("Configuration file(s) changed")), + plan.fallbackReasons.join("; "), + ); + assert.ok( + !plan.fallbackReasons.some((text) => text.startsWith("Tool configuration changed: TypeScript configuration")), + plan.fallbackReasons.join("; "), + ); +}); + +// A name the pattern must not match: `tsconfig-paths.json` is a package's config file, not a project one. +test("tsconfig-paths.json stays unknown", async () => { + const plan = await planForChange(tsProject({ "tsconfig-paths.json": "x\n" }), "tsconfig-paths.json", "y\n"); + assert.equal(plan.mode, "FULL"); + assert.ok( + plan.fallbackReasons.some((text) => text.includes("Unknown changed file: tsconfig-paths.json")), + plan.fallbackReasons.join("; "), + ); +});