nx
Version:
88 lines (87 loc) • 4.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isHandoffGitignoreMigration = isHandoffGitignoreMigration;
exports.applyAgenticHandoffGitignoreFallback = applyAgenticHandoffGitignoreFallback;
const tslib_1 = require("tslib");
const semver_1 = require("semver");
const pc = tslib_1.__importStar(require("picocolors"));
const add_migrate_runs_to_git_ignore_1 = tslib_1.__importDefault(require("../../../migrations/update-23-0-0/add-migrate-runs-to-git-ignore"));
const tree_1 = require("../../../generators/tree");
const git_utils_1 = require("../../../utils/git-utils");
const logger_1 = require("../../../utils/logger");
/**
* Composite identity of the v23 migration that adds `.nx/migrate-runs` to
* `.gitignore`. Hard-coded because the agentic preflight is a deliberate
* one-off coupling: this exact migration owns the entry that keeps
* `.nx/migrate-runs/<run-id>/...` scratch out of per-migration commits. If
* the migration is ever renamed, this entry must move with it.
*/
const HANDOFF_GITIGNORE_MIGRATION_PACKAGE = 'nx';
const HANDOFF_GITIGNORE_MIGRATION_NAME = '23-0-0-add-migrate-runs-to-git-ignore';
function isHandoffGitignoreMigration(m) {
return (m.package === HANDOFF_GITIGNORE_MIGRATION_PACKAGE &&
m.name === HANDOFF_GITIGNORE_MIGRATION_NAME);
}
/**
* Under `--agentic`, the runner writes per-run scratch under
* `.nx/migrate-runs/<run-id>/`. The v23 migration
* `23-0-0-add-migrate-runs-to-git-ignore` adds `.nx/migrate-runs` to
* `.gitignore`; without intervention it would run in its declared slot
* (typically late), so earlier per-migration commits would absorb the
* scratch into the user-visible diff.
*
* Two paths cover the leak, with no overlap:
*
* 1. HOIST — handled by the sort comparator in `executeMigrations`. When
* the v23 migration is in the queue, it sorts to position 0 and runs
* first via the normal migration runner (with its own log line and
* commit). Fully traceable in `git log`.
*
* 2. INLINE FALLBACK — this function. When the migration is NOT in the
* queue AND the highest target version is < v23 (intra-pre-v23
* `--agentic` run), the migration won't run at all. Apply its body
* inline against an `FsTree` and commit it as a standalone preflight
* commit (or leave in the working tree under `--no-create-commits`).
*
* When the migration is not in the queue AND target >= v23, the user is
* past v23 already. They had the entry historically; if it's gone, that's
* a conscious removal we respect.
*/
async function applyAgenticHandoffGitignoreFallback({ migrations, installedNxVersion, effectiveCreateCommits, commitPrefix, root, }) {
if (migrations.some(isHandoffGitignoreMigration)) {
// The hoist path handles this via the sort comparator.
return;
}
if ((0, semver_1.major)(installedNxVersion) >= 23) {
// User is past v23. Respect their `.gitignore` state — if the entry
// is missing, that's a conscious removal.
return;
}
const tree = new tree_1.FsTree(root, false);
await (0, add_migrate_runs_to_git_ignore_1.default)(tree);
const changes = tree.listChanges();
if (changes.length === 0) {
// Migration body short-circuited (no `.gitignore`, Lerna without nx.json,
// or the entry is already covered by an existing pattern).
return;
}
(0, tree_1.flushChanges)(root, changes);
logger_1.logger.info(pc.dim(`- Added .nx/migrate-runs to .gitignore so this --agentic run's handoff scratch is ignored.`));
if (!effectiveCreateCommits)
return;
if (!(0, git_utils_1.hasUncommittedChanges)(root))
return;
try {
const sha = (0, git_utils_1.tryCommitChanges)(`${commitPrefix}add .nx/migrate-runs to .gitignore`, root);
if (sha) {
logger_1.logger.info(pc.dim(` Commit: ${sha}`));
}
// `null` return = commit landed but `git rev-parse HEAD` raced. The
// diff cleared from the working tree; nothing more to say.
}
catch (err) {
const reason = err instanceof Error ? err.message : String(err);
logger_1.logger.info(pc.yellow(`Could not create the agentic preflight commit:\n${reason}\n` +
`The .gitignore change remains in the working tree; commit it manually or it will be absorbed into the first per-migration commit.`));
}
}