feat(core): add ai rule files to gitignore (#31238)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
Rule files are created by Nx Console, and gitignore updates are done
there.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Rule files are still created by Nx Console, but there is now a migration
to add those files to the gitignore rules on migration and new
workspaces.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Jonathan Cammisuli 2025-05-16 14:13:51 -04:00 committed by GitHub
parent ccfcc4097f
commit eaf21e9854
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 115 additions and 0 deletions

View File

@ -3414,6 +3414,16 @@
}
},
"migrations": {
"/nx-api/nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files": {
"description": "Adds **/nx-rules.mdc and **/nx.instructions.md to .gitignore if not present",
"file": "generated/packages/nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files.json",
"hidden": false,
"name": "21-1-0-add-ignore-entries-for-nx-rule-files",
"version": "21.1.0-beta.2",
"originalFilePath": "/packages/nx",
"path": "/nx-api/nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files",
"type": "migration"
},
"/nx-api/nx/migrations/release-version-config-changes": {
"description": "Updates release version config based on the breaking changes in Nx v21",
"file": "generated/packages/nx/migrations/release-version-config-changes.json",

View File

@ -3390,6 +3390,16 @@
}
],
"migrations": [
{
"description": "Adds **/nx-rules.mdc and **/nx.instructions.md to .gitignore if not present",
"file": "generated/packages/nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files.json",
"hidden": false,
"name": "21-1-0-add-ignore-entries-for-nx-rule-files",
"version": "21.1.0-beta.2",
"originalFilePath": "/packages/nx",
"path": "nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files",
"type": "migration"
},
{
"description": "Updates release version config based on the breaking changes in Nx v21",
"file": "generated/packages/nx/migrations/release-version-config-changes.json",

View File

@ -0,0 +1,12 @@
{
"name": "21-1-0-add-ignore-entries-for-nx-rule-files",
"version": "21.1.0-beta.2",
"description": "Adds **/nx-rules.mdc and **/nx.instructions.md to .gitignore if not present",
"implementation": "/packages/nx/src/migrations/update-21-1-0/add-gitignore-entry.ts",
"aliases": [],
"hidden": false,
"path": "/packages/nx",
"schema": null,
"type": "migration",
"examplesFile": ""
}

View File

@ -83,6 +83,11 @@
"version": "21.0.0-beta.11",
"description": "Updates release changelog config based on the breaking changes in Nx v21",
"implementation": "./src/migrations/update-21-0-0/release-changelog-config-changes"
},
"21-1-0-add-ignore-entries-for-nx-rule-files": {
"version": "21.1.0-beta.2",
"description": "Adds **/nx-rules.mdc and **/nx.instructions.md to .gitignore if not present",
"implementation": "./src/migrations/update-21-1-0/add-gitignore-entry"
}
}
}

View File

@ -222,6 +222,21 @@ export function updateGitIgnore(root: string) {
}
lines.push('.nx/workspace-data');
}
if (!contents.includes('.cursor/rules/nx-rules.mdc')) {
if (!sepIncluded) {
lines.push('\n');
sepIncluded = true;
}
lines.push('.cursor/rules/nx-rules.mdc');
}
if (!contents.includes('.github/instructions/nx.instructions.md')) {
if (!sepIncluded) {
lines.push('\n');
sepIncluded = true;
}
lines.push('.github/instructions/nx.instructions.md');
}
writeFileSync(ignorePath, lines.join('\n'), 'utf-8');
} catch {}
}

View File

@ -0,0 +1,33 @@
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import addGitignoreEntry from './add-gitignore-entry';
describe('addGitignoreEntry migration', () => {
it('should add .cursor/rules/nx-rules.mdc and .github/instructions/nx.instructions.md to .gitignore if not present', async () => {
const tree = createTreeWithEmptyWorkspace();
tree.write('.gitignore', 'dist\nnode_modules\n');
await addGitignoreEntry(tree);
const result = tree.read('.gitignore', 'utf-8');
expect(result).toContain('.cursor/rules/nx-rules.mdc');
expect(result).toContain('.github/instructions/nx.instructions.md');
});
it('should not add duplicate entries if already present', async () => {
const tree = createTreeWithEmptyWorkspace();
tree.write(
'.gitignore',
'dist\n.cursor/rules/nx-rules.mdc\n.github/instructions/nx.instructions.md\n'
);
await addGitignoreEntry(tree);
const result = tree.read('.gitignore', 'utf-8');
expect(result.match(/\.cursor\/rules\/nx-rules\.mdc/g)?.length).toBe(1);
expect(
result.match(/\.github\/instructions\/nx\.instructions\.md/g)?.length
).toBe(1);
});
it('should do nothing if .gitignore does not exist', async () => {
const tree = createTreeWithEmptyWorkspace();
await addGitignoreEntry(tree);
expect(tree.exists('.gitignore')).toBe(false);
});
});

View File

@ -0,0 +1,24 @@
import { Tree } from '../../generators/tree';
import ignore from 'ignore';
export default async function addGitignoreEntry(tree: Tree) {
if (!tree.exists('nx.json')) {
return;
}
const GITIGNORE_ENTRIES = [
'.cursor/rules/nx-rules.mdc',
'.github/instructions/nx.instructions.md',
];
if (!tree.exists('.gitignore')) {
return;
}
let content = tree.read('.gitignore', 'utf-8') || '';
const ig = ignore().add(content);
for (const entry of GITIGNORE_ENTRIES) {
if (!ig.ignores(entry)) {
content = content.trimEnd() + '\n' + entry + '\n';
}
}
tree.write('.gitignore', content);
}

View File

@ -40,3 +40,5 @@ Thumbs.db
.nx/cache
.nx/workspace-data
.cursor/rules/nx-rules.mdc
.github/instructions/nx.instructions.md

View File

@ -40,3 +40,5 @@ Thumbs.db
.nx/cache
.nx/workspace-data
.cursor/rules/nx-rules.mdc
.github/instructions/nx.instructions.md

View File

@ -40,3 +40,5 @@ Thumbs.db
.nx/cache
.nx/workspace-data
.cursor/rules/nx-rules.mdc
.github/instructions/nx.instructions.md