Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): upgrade's filesChanged counts files, not (codemod, file) pairs (#6622)

Joeya89c7d68cc85
diff --git a/.changeset/cli-files-changed-counts-files.md b/.changeset/cli-files-changed-counts-files.md
new file mode 100644
index 0000000000..d1daeb9f1a
--- /dev/null
+++ b/.changeset/cli-files-changed-counts-files.md
@@ -0,0 +1,11 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `upgrade`'s `filesChanged` counts files, not (codemod, file) pairs (#6622)
+
+One source file that four codemods each changed was reported as four files changed, so `filesChanged` matched `transformsApplied` and the documented meaning, "Total files changed", was not true. The human summary said the same thing: "Found 4 changes across 4 files" for one file.
+
+`filesChanged` is now the count of distinct files. `transformsApplied` is unchanged: a code or config codemod counts once for each file it changed, and a project codemod counts once. A file that both a core codemod and an integration codemod changed counts once in `filesChanged`.
+
+@josephfarina
diff --git a/packages/cli/api/upgrade/run/files-changed.test.mjs b/packages/cli/api/upgrade/run/files-changed.test.mjs
new file mode 100644
index 0000000000..c31223b198
--- /dev/null
+++ b/packages/cli/api/upgrade/run/files-changed.test.mjs
@@ -0,0 +1,111 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file The upgrade receipt counts a file once when a core codemod AND an
+ * integration codemod both change it: `filesChanged` is the union of the two
+ * runners' files, while `transformsApplied` adds their changes.
+ *
+ * Uses a real consumer under a repo-local temp dir (Vite blocks dynamic import
+ * of config and integration modules from /tmp), the real core registry, and an
+ * installed integration whose code codemod stamps the same file.
+ */
+
+import {describe, it, expect, afterEach} from 'vitest';
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+import {upgrade} from '../upgrade.mjs';
+import {
+ latestVersion,
+ versions,
+ getTransformsBetween,
+} from '../../../assets/codemods/registry.mjs';
+
+const SLOW = 60_000;
+
+/** The registry version whose manifest ships the authoring migration. */
+async function authoringTier() {
+ const all = await getTransformsBetween('0.0.0', latestVersion);
+ const tier = all.find(({transforms}) =>
+ transforms.some(t => t.name === 'migrate-authoring-imports'),
+ );
+ if (!tier) throw new Error('no registry version ships the authoring migration');
+ return tier.version;
+}
+
+/**
+ * A consumer with installed core at the registry's latest version and one
+ * old-surface file that the core authoring codemods rewrite.
+ * @param {string} dir
+ * @param {{integrationCodemod: boolean}} options
+ */
+function seed(dir, {integrationCodemod}) {
+ fs.writeFileSync(
+ path.join(dir, 'package.json'),
+ JSON.stringify({name: 'consumer', version: '1.0.0'}),
+ );
+ const core = path.join(dir, 'node_modules', '@astryxdesign', 'core');
+ fs.mkdirSync(core, {recursive: true});
+ fs.writeFileSync(
+ path.join(core, 'package.json'),
+ JSON.stringify({name: '@astryxdesign/core', version: latestVersion}),
+ );
+ fs.mkdirSync(path.join(dir, 'src'), {recursive: true});
+ fs.writeFileSync(
+ path.join(dir, 'src', 'Button.doc.mjs'),
+ [
+ "import {createComponentDoc} from '@astryxdesign/core/authoring';",
+ "export default createComponentDoc({name: 'Button', props: []});",
+ '',
+ ].join('\n'),
+ );
+ if (!integrationCodemod) return;
+ fs.writeFileSync(
+ path.join(dir, 'astryx.config.mjs'),
+ "export default {integrations: ['@acme/widgets']};\n",
+ );
+ const pkg = path.join(dir, 'node_modules', '@acme', 'widgets');
+ fs.mkdirSync(path.join(pkg, 'codemods', latestVersion), {recursive: true});
+ fs.writeFileSync(
+ path.join(pkg, 'package.json'),
+ JSON.stringify({name: '@acme/widgets', version: '1.0.0'}),
+ );
+ fs.writeFileSync(
+ path.join(pkg, 'astryx.integration.mjs'),
+ "export default {codemods: './codemods'};\n",
+ );
+ fs.writeFileSync(
+ path.join(pkg, 'codemods', latestVersion, 'acme-stamp.mjs'),
+ "export default {type: 'code', title: 'Stamp', transform: file => (file.source.includes('// acme') ? null : `${file.source}// acme\\n`)};\n",
+ );
+}
+
+describe('upgrade receipt — filesChanged across core and integration codemods', () => {
+ /** @type {string[]} */
+ const dirs = [];
+ afterEach(() => {
+ for (const dir of dirs.splice(0)) fs.rmSync(dir, {recursive: true, force: true});
+ });
+
+ /** @param {{integrationCodemod: boolean}} options */
+ async function run(options) {
+ const dir = fs.mkdtempSync(path.join(process.cwd(), '.astryx-files-changed-'));
+ dirs.push(dir);
+ seed(dir, options);
+ const tier = await authoringTier();
+ const from = versions[versions.indexOf(tier) - 1];
+ const res = await upgrade({from, path: 'src'}, {cwd: dir});
+ expect(res.type).toBe('upgrade.run');
+ return res.data;
+ }
+
+ it('counts a file changed by both a core and an integration codemod once', async () => {
+ const coreOnly = await run({integrationCodemod: false});
+ expect(coreOnly.filesChanged).toBe(1);
+ expect(coreOnly.transformsApplied).toBeGreaterThan(0);
+
+ const both = await run({integrationCodemod: true});
+ expect(both.integrations).toEqual(['@acme/widgets']);
+ expect(both.filesChanged).toBe(1);
+ expect(both.transformsApplied).toBe(coreOnly.transformsApplied + 1);
+ }, SLOW);
+});
diff --git a/packages/cli/api/upgrade/run/run.mjs b/packages/cli/api/upgrade/run/run.mjs
index 4e87d632df..12273ce6e8 100644
--- a/packages/cli/api/upgrade/run/run.mjs
+++ b/packages/cli/api/upgrade/run/run.mjs
@@ -437,9 +437,11 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
const registryResult = await reconcileCompositions();
- const mergedFilesChanged =
- (coreResult?.totalFilesChanged ?? 0) +
- (integrationResult?.totalFilesChanged ?? 0);
+ // A file a core codemod AND an integration codemod both changed is one file.
+ const mergedFilesChanged = new Set([
+ ...(coreResult?.changedFiles ?? []),
+ ...(integrationResult?.changedFiles ?? []),
+ ]).size;
const mergedTransformsApplied =
(coreResult?.totalTransformsApplied ?? 0) +
(integrationResult?.totalTransformsApplied ?? 0);
diff --git a/packages/cli/api/upgrade/upgrade.type.mjs b/packages/cli/api/upgrade/upgrade.type.mjs
index 23bbe2c5ef..07da1f1e83 100644
--- a/packages/cli/api/upgrade/upgrade.type.mjs
+++ b/packages/cli/api/upgrade/upgrade.type.mjs
@@ -119,11 +119,11 @@
* @property {RegistryCompositionSummary} [data.registryCompositions]
* @property {boolean} [data.complete] False when protected required changes remain.
* @property {'ERR_CODEMOD_PROTECTED'} [data.errorCode] Stable incomplete-result code when complete is false.
- * @property {number} [data.filesChanged] Total files changed across core + integration codemods (apply mode).
+ * @property {number} [data.filesChanged] Distinct files changed across core + integration codemods (apply mode). One file that four codemods each changed counts once.
* @property {string[]} [data.modifiedFiles] Project-relative files changed or previewed.
* @property {ProtectedCodemodFile[]} [data.protectedFiles] Protected files that still require a codemod change after regeneration.
* @property {Array<{file: string, location?: string, reason: string}>} [data.declinedCandidates] Candidates left unchanged because proof was insufficient.
- * @property {number} [data.transformsApplied] Total transforms that reported a change.
+ * @property {number} [data.transformsApplied] Total codemod changes. A code or config codemod counts once for each file it changed, so one file changed by four of them counts four times; a project codemod counts once, however many files it writes.
* @property {Array<{file: string, codemod: string, error: string}>} [data.errors] Per-codemod errors, when any codemod failed.
*/
diff --git a/packages/cli/assets/codemods/__tests__/runner.test.mjs b/packages/cli/assets/codemods/__tests__/runner.test.mjs
index 72e2a28c1f..8ed47d95bb 100644
--- a/packages/cli/assets/codemods/__tests__/runner.test.mjs
+++ b/packages/cli/assets/codemods/__tests__/runner.test.mjs
@@ -46,7 +46,9 @@ describe('runCodemods — ordered dry-run state', () => {
silent: true,
});
- expect(preview.totalFilesChanged).toBe(2);
+ // One file that two transforms changed is one file and two changes.
+ expect(preview.totalFilesChanged).toBe(1);
+ expect(preview.totalTransformsApplied).toBe(2);
expect(preview.changedFiles).toEqual([
path.join(srcDir, 'a.ts'),
path.join(srcDir, 'a.ts'),
diff --git a/packages/cli/assets/codemods/file-count.test.mjs b/packages/cli/assets/codemods/file-count.test.mjs
new file mode 100644
index 0000000000..57a875345e
--- /dev/null
+++ b/packages/cli/assets/codemods/file-count.test.mjs
@@ -0,0 +1,163 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file `filesChanged` counts FILES, not (codemod, file) pairs.
+ *
+ * One source file that four codemods each changed was reported as four files
+ * changed — the total was incremented once per transform per file, so it
+ * equalled `transformsApplied` in every run and the documented meaning of the
+ * field ("Total files changed") was never true. The two numbers answer
+ * different questions and both are in the receipt.
+ */
+
+import {describe, it, expect, beforeEach, afterEach} from 'vitest';
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import jscodeshift from 'jscodeshift';
+import {runCodemods} from './runner.mjs';
+import {runIntegrationCodemods} from './integration-runner.mjs';
+
+let dir;
+
+beforeEach(() => {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-file-count-'));
+});
+afterEach(() => fs.rmSync(dir, {recursive: true, force: true}));
+
+/** A transform that rewrites one distinctive token, so several can stack. */
+const renaming = (from, to) => (file) =>
+ file.source.includes(from) ? file.source.split(from).join(to) : null;
+
+/** @param {string} name @param {string[]} contents */
+function writeSources(...contents) {
+ return contents.map((content, i) => {
+ const file = path.join(dir, `file${i}.ts`);
+ fs.writeFileSync(file, content);
+ return file;
+ });
+}
+
+describe('core codemod runner — filesChanged counts files', () => {
+ it('reports 1 file for one file changed by four codemods', async () => {
+ writeSources('const a = ONE + TWO + THREE + FOUR;\n');
+
+ const result = await runCodemods(
+ [
+ {
+ version: '0.0.2',
+ transforms: [
+ {name: 'one', transform: renaming('ONE', '1'), meta: {title: 'one'}},
+ {name: 'two', transform: renaming('TWO', '2'), meta: {title: 'two'}},
+ {name: 'three', transform: renaming('THREE', '3'), meta: {title: 'three'}},
+ {name: 'four', transform: renaming('FOUR', '4'), meta: {title: 'four'}},
+ ],
+ },
+ ],
+ {apply: true, path: dir, root: dir, codemod: undefined, skipCodemods: new Set(), silent: true},
+ );
+
+ expect(result.totalTransformsApplied).toBe(4);
+ expect(result.totalFilesChanged).toBe(1);
+ expect(new Set(result.changedFiles).size).toBe(1);
+ });
+
+ it('still counts two files as two', async () => {
+ writeSources('const a = ONE;\n', 'const b = ONE;\n');
+
+ const result = await runCodemods(
+ [
+ {
+ version: '0.0.2',
+ transforms: [
+ {name: 'one', transform: renaming('ONE', '1'), meta: {title: 'one'}},
+ ],
+ },
+ ],
+ {apply: true, path: dir, root: dir, codemod: undefined, skipCodemods: new Set(), silent: true},
+ );
+
+ expect(result.totalTransformsApplied).toBe(2);
+ expect(result.totalFilesChanged).toBe(2);
+ });
+
+ it('reports 0 when nothing matched', async () => {
+ writeSources('const a = 1;\n');
+
+ const result = await runCodemods(
+ [
+ {
+ version: '0.0.2',
+ transforms: [
+ {name: 'one', transform: renaming('ONE', '1'), meta: {title: 'one'}},
+ ],
+ },
+ ],
+ {apply: true, path: dir, root: dir, codemod: undefined, skipCodemods: new Set(), silent: true},
+ );
+
+ expect(result.totalFilesChanged).toBe(0);
+ expect(result.totalTransformsApplied).toBe(0);
+ });
+});
+
+describe('integration codemod runner — filesChanged counts files', () => {
+ it('reports 1 file for one file changed by three integration codemods', () => {
+ writeSources('const a = ONE + TWO + THREE;\n');
+
+ const entry = (id, from, to) => ({
+ id,
+ package: '@acme/widgets',
+ type: 'code',
+ codemod: {title: id, transform: renaming(from, to)},
+ });
+
+ const result = runIntegrationCodemods(
+ [
+ {
+ version: '1.0.0',
+ codemods: [
+ entry('one', 'ONE', '1'),
+ entry('two', 'TWO', '2'),
+ entry('three', 'THREE', '3'),
+ ],
+ },
+ ],
+ {apply: true, path: dir, root: dir, skipCodemods: new Set(), jscodeshift, silent: true},
+ );
+
+ expect(result.totalTransformsApplied).toBe(3);
+ expect(result.totalFilesChanged).toBe(1);
+ expect(new Set(result.changedFiles).size).toBe(1);
+ });
+});
+
+describe('core codemod runner — a project codemod', () => {
+ it('counts every file it writes, and one change', async () => {
+ const result = await runCodemods(
+ [
+ {
+ version: '0.0.2',
+ transforms: [
+ {
+ name: 'project-plan',
+ meta: {title: 'project plan', codemodType: 'project'},
+ transform: async root => ({
+ writes: [
+ {path: path.join(root, 'a.ts'), contents: 'a\n'},
+ {path: path.join(root, 'b.ts'), contents: 'b\n'},
+ ],
+ deletes: [],
+ problems: [],
+ }),
+ },
+ ],
+ },
+ ],
+ {apply: true, path: dir, root: dir, codemod: undefined, skipCodemods: new Set(), silent: true},
+ );
+
+ expect(result.totalFilesChanged).toBe(2);
+ expect(result.totalTransformsApplied).toBe(1);
+ });
+});
diff --git a/packages/cli/assets/codemods/integration-runner.mjs b/packages/cli/assets/codemods/integration-runner.mjs
index ae4313345e..3486fa5f11 100644
--- a/packages/cli/assets/codemods/integration-runner.mjs
+++ b/packages/cli/assets/codemods/integration-runner.mjs
@@ -67,7 +67,6 @@ export function runIntegrationCodemods(
/** In-memory pipeline state keeps ordered dry-runs equivalent to apply. */
const virtualContents = new Map(providedContents ?? []);
- let totalFilesChanged = 0;
let totalTransformsApplied = 0;
/** @type {string[]} */
const changedFiles = [];
@@ -115,7 +114,6 @@ export function runIntegrationCodemods(
protection,
contents: virtualContents,
});
- totalFilesChanged += r.filesChanged;
totalTransformsApplied += r.filesChanged;
changedFiles.push(...r.changedFiles);
writtenFiles.push(...r.writtenFiles);
@@ -145,7 +143,6 @@ export function runIntegrationCodemods(
protection,
contents: virtualContents,
});
- totalFilesChanged += r.filesChanged;
totalTransformsApplied += r.filesChanged;
changedFiles.push(...r.changedFiles);
writtenFiles.push(...r.writtenFiles);
@@ -154,6 +151,9 @@ export function runIntegrationCodemods(
}
}
+ // A file several codemods changed is one file; transforms count each change.
+ const totalFilesChanged = new Set(changedFiles).size;
+
return {
totalFilesChanged,
totalTransformsApplied,
diff --git a/packages/cli/assets/codemods/runner.mjs b/packages/cli/assets/codemods/runner.mjs
index 7a827c8e41..db81f84d5d 100644
--- a/packages/cli/assets/codemods/runner.mjs
+++ b/packages/cli/assets/codemods/runner.mjs
@@ -486,7 +486,6 @@ export async function runCodemods(
(await import('jscodeshift')).default
);
- let totalFilesChanged = 0;
let totalTransformsApplied = 0;
let totalValidationBlocked = 0;
/** @type {Array<{file: string, codemod: string, error: string}>} */
@@ -554,7 +553,6 @@ export async function runCodemods(
protectionWriteCount = writtenFiles.length;
}
if (result.filesChanged > 0) {
- totalFilesChanged += result.filesChanged;
totalTransformsApplied += 1;
}
continue;
@@ -577,7 +575,6 @@ export async function runCodemods(
changedFiles.push(...result.changedFiles);
writtenFiles.push(...result.writtenFiles);
if (result.filesChanged > 0) {
- totalFilesChanged += result.filesChanged;
totalTransformsApplied += result.filesChanged;
}
continue;
@@ -592,7 +589,6 @@ export async function runCodemods(
protectedFiles.push(...result.protectedFiles);
changedFiles.push(...result.changedFiles);
writtenFiles.push(...result.writtenFiles);
- totalFilesChanged += result.filesChanged;
totalTransformsApplied += result.filesChanged;
totalValidationBlocked += result.errors.filter(
error =>
@@ -630,6 +626,11 @@ export async function runCodemods(
);
}
+ // A file several codemods changed is one file. `totalTransformsApplied` is
+ // unchanged: a code or config codemod counts each file it changed, and a
+ // project codemod counts once. The two answer different questions.
+ const totalFilesChanged = new Set(changedFiles).size;
+
if (protectedFiles.length > 0) {
const files = [...new Set(protectedFiles.map(item => item.file))];
log.warn(