Skip to content

Commit eaf21e9

Browse files
authored
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 #
1 parent ccfcc40 commit eaf21e9

File tree

10 files changed

+115
-0
lines changed

10 files changed

+115
-0
lines changed

docs/generated/manifests/nx-api.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3414,6 +3414,16 @@
34143414
}
34153415
},
34163416
"migrations": {
3417+
"/nx-api/nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files": {
3418+
"description": "Adds **/nx-rules.mdc and **/nx.instructions.md to .gitignore if not present",
3419+
"file": "generated/packages/nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files.json",
3420+
"hidden": false,
3421+
"name": "21-1-0-add-ignore-entries-for-nx-rule-files",
3422+
"version": "21.1.0-beta.2",
3423+
"originalFilePath": "/packages/nx",
3424+
"path": "/nx-api/nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files",
3425+
"type": "migration"
3426+
},
34173427
"/nx-api/nx/migrations/release-version-config-changes": {
34183428
"description": "Updates release version config based on the breaking changes in Nx v21",
34193429
"file": "generated/packages/nx/migrations/release-version-config-changes.json",

docs/generated/packages-metadata.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3390,6 +3390,16 @@
33903390
}
33913391
],
33923392
"migrations": [
3393+
{
3394+
"description": "Adds **/nx-rules.mdc and **/nx.instructions.md to .gitignore if not present",
3395+
"file": "generated/packages/nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files.json",
3396+
"hidden": false,
3397+
"name": "21-1-0-add-ignore-entries-for-nx-rule-files",
3398+
"version": "21.1.0-beta.2",
3399+
"originalFilePath": "/packages/nx",
3400+
"path": "nx/migrations/21-1-0-add-ignore-entries-for-nx-rule-files",
3401+
"type": "migration"
3402+
},
33933403
{
33943404
"description": "Updates release version config based on the breaking changes in Nx v21",
33953405
"file": "generated/packages/nx/migrations/release-version-config-changes.json",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "21-1-0-add-ignore-entries-for-nx-rule-files",
3+
"version": "21.1.0-beta.2",
4+
"description": "Adds **/nx-rules.mdc and **/nx.instructions.md to .gitignore if not present",
5+
"implementation": "/packages/nx/src/migrations/update-21-1-0/add-gitignore-entry.ts",
6+
"aliases": [],
7+
"hidden": false,
8+
"path": "/packages/nx",
9+
"schema": null,
10+
"type": "migration",
11+
"examplesFile": ""
12+
}

packages/nx/migrations.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
"version": "21.0.0-beta.11",
8484
"description": "Updates release changelog config based on the breaking changes in Nx v21",
8585
"implementation": "./src/migrations/update-21-0-0/release-changelog-config-changes"
86+
},
87+
"21-1-0-add-ignore-entries-for-nx-rule-files": {
88+
"version": "21.1.0-beta.2",
89+
"description": "Adds **/nx-rules.mdc and **/nx.instructions.md to .gitignore if not present",
90+
"implementation": "./src/migrations/update-21-1-0/add-gitignore-entry"
8691
}
8792
}
8893
}

packages/nx/src/command-line/init/implementation/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ export function updateGitIgnore(root: string) {
222222
}
223223
lines.push('.nx/workspace-data');
224224
}
225+
if (!contents.includes('.cursor/rules/nx-rules.mdc')) {
226+
if (!sepIncluded) {
227+
lines.push('\n');
228+
sepIncluded = true;
229+
}
230+
lines.push('.cursor/rules/nx-rules.mdc');
231+
}
232+
if (!contents.includes('.github/instructions/nx.instructions.md')) {
233+
if (!sepIncluded) {
234+
lines.push('\n');
235+
sepIncluded = true;
236+
}
237+
lines.push('.github/instructions/nx.instructions.md');
238+
}
239+
225240
writeFileSync(ignorePath, lines.join('\n'), 'utf-8');
226241
} catch {}
227242
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
2+
import addGitignoreEntry from './add-gitignore-entry';
3+
4+
describe('addGitignoreEntry migration', () => {
5+
it('should add .cursor/rules/nx-rules.mdc and .github/instructions/nx.instructions.md to .gitignore if not present', async () => {
6+
const tree = createTreeWithEmptyWorkspace();
7+
tree.write('.gitignore', 'dist\nnode_modules\n');
8+
await addGitignoreEntry(tree);
9+
const result = tree.read('.gitignore', 'utf-8');
10+
expect(result).toContain('.cursor/rules/nx-rules.mdc');
11+
expect(result).toContain('.github/instructions/nx.instructions.md');
12+
});
13+
14+
it('should not add duplicate entries if already present', async () => {
15+
const tree = createTreeWithEmptyWorkspace();
16+
tree.write(
17+
'.gitignore',
18+
'dist\n.cursor/rules/nx-rules.mdc\n.github/instructions/nx.instructions.md\n'
19+
);
20+
await addGitignoreEntry(tree);
21+
const result = tree.read('.gitignore', 'utf-8');
22+
expect(result.match(/\.cursor\/rules\/nx-rules\.mdc/g)?.length).toBe(1);
23+
expect(
24+
result.match(/\.github\/instructions\/nx\.instructions\.md/g)?.length
25+
).toBe(1);
26+
});
27+
28+
it('should do nothing if .gitignore does not exist', async () => {
29+
const tree = createTreeWithEmptyWorkspace();
30+
await addGitignoreEntry(tree);
31+
expect(tree.exists('.gitignore')).toBe(false);
32+
});
33+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Tree } from '../../generators/tree';
2+
import ignore from 'ignore';
3+
4+
export default async function addGitignoreEntry(tree: Tree) {
5+
if (!tree.exists('nx.json')) {
6+
return;
7+
}
8+
9+
const GITIGNORE_ENTRIES = [
10+
'.cursor/rules/nx-rules.mdc',
11+
'.github/instructions/nx.instructions.md',
12+
];
13+
if (!tree.exists('.gitignore')) {
14+
return;
15+
}
16+
let content = tree.read('.gitignore', 'utf-8') || '';
17+
const ig = ignore().add(content);
18+
for (const entry of GITIGNORE_ENTRIES) {
19+
if (!ig.ignores(entry)) {
20+
content = content.trimEnd() + '\n' + entry + '\n';
21+
}
22+
}
23+
tree.write('.gitignore', content);
24+
}

packages/workspace/src/generators/new/files-integrated-repo/__dot__gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ Thumbs.db
4040

4141
.nx/cache
4242
.nx/workspace-data
43+
.cursor/rules/nx-rules.mdc
44+
.github/instructions/nx.instructions.md

packages/workspace/src/generators/new/files-package-based-repo/__dot__gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ Thumbs.db
4040

4141
.nx/cache
4242
.nx/workspace-data
43+
.cursor/rules/nx-rules.mdc
44+
.github/instructions/nx.instructions.md

packages/workspace/src/generators/new/files-root-app/__dot__gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,5 @@ Thumbs.db
4040

4141
.nx/cache
4242
.nx/workspace-data
43+
.cursor/rules/nx-rules.mdc
44+
.github/instructions/nx.instructions.md

0 commit comments

Comments
 (0)