From 3330b2c6712429b72914d6d8ac65887cdfeaaeec Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Wed, 2 Jul 2025 11:07:12 +0530 Subject: [PATCH 1/4] feat: prefix folder path for colocated rules --- .../src/markdown/markdownToRule.test.ts | 78 +++++++++++++++++-- .../src/markdown/markdownToRule.ts | 27 ++++++- 2 files changed, 97 insertions(+), 8 deletions(-) diff --git a/packages/config-yaml/src/markdown/markdownToRule.test.ts b/packages/config-yaml/src/markdown/markdownToRule.test.ts index 1b930220a..3f9cfc396 100644 --- a/packages/config-yaml/src/markdown/markdownToRule.test.ts +++ b/packages/config-yaml/src/markdown/markdownToRule.test.ts @@ -20,7 +20,7 @@ This is a test rule.`; const result = markdownToRule(content, mockId); expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); - expect(result.globs).toBe("**/test/**/*.kt"); + expect(result.globs).toBe("/path/to/**/test/**/*.kt"); expect(result.name).toBe("Custom Name"); }); @@ -34,7 +34,7 @@ globs: "**/test/**/*.kt" This is a test rule.`; const result = markdownToRule(content, mockId); - expect(result.globs).toBe("**/test/**/*.kt"); + expect(result.globs).toBe("/path/to/**/test/**/*.kt"); expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); expect(result.name).toBe("to/file"); // Should use last two path segments }); @@ -45,7 +45,7 @@ This is a test rule.`; This is a test rule without frontmatter.`; const result = markdownToRule(content, mockId); - expect(result.globs).toBeUndefined(); + expect(result.globs).toBe("/path/to/**/*"); expect(result.rule).toBe(content); expect(result.name).toBe("to/file"); // Should use last two path segments }); @@ -59,13 +59,77 @@ This is a test rule without frontmatter.`; This is a test rule with empty frontmatter.`; const result = markdownToRule(content, mockId); - expect(result.globs).toBeUndefined(); + expect(result.globs).toBe("/path/to/**/*"); expect(result.rule).toBe( "# Test Rule\n\nThis is a test rule with empty frontmatter.", ); expect(result.name).toBe("to/file"); // Should use last two path segments }); + describe("glob patterns", () => { + it("should match glob pattern for file extensions", () => { + const content = `--- +globs: "*.ts" +name: glob pattern testing +--- + + # Test Rule + + This is a test rule.`; + + const result = markdownToRule(content, mockId); + expect(result.globs).toBe("/path/to/**/*.ts"); + }); + + it("should match glob pattern for dotfiles", () => { + const content = `--- +globs: ".gitignore" +name: glob pattern testing +--- + + # Test Rule + + This is a test rule.`; + + const result = markdownToRule(content, mockId); + expect(result.globs).toBe("/path/to/**/.gitignore"); + }); + + it("should also work in root as base directory", () => { + const content = `--- +globs: "src/**/Dockerfile" +name: glob pattern testing +--- + + # Test Rule + + This is a test rule.`; + + const result = markdownToRule(content, { + uriType: "file", + filePath: "/", + }); + expect(result.globs).toBe("/src/**/Dockerfile"); + }); + + it("should match have nested folder for multiple globs", () => { + const content = `--- +globs: ["**/nested/**/deeper/**/*.rs", ".zshrc"] +name: glob pattern testing +--- + +# Test Rule + +This is a test rule.`; + + const result = markdownToRule(content, mockId); + expect(result.globs).toEqual([ + "/path/to/**/nested/**/deeper/**/*.rs", + "/path/to/**/.zshrc", + ]); + }); + }); + it("should handle frontmatter with whitespace", () => { const content = `--- globs: "**/test/**/*.kt" @@ -76,7 +140,7 @@ globs: "**/test/**/*.kt" This is a test rule.`; const result = markdownToRule(content, mockId); - expect(result.globs).toBe("**/test/**/*.kt"); + expect(result.globs).toBe("/path/to/**/test/**/*.kt"); expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); expect(result.name).toBe("to/file"); // Should use last two path segments }); @@ -92,7 +156,7 @@ globs: "**/test/**/*.kt"\r This is a test rule.`; const result = markdownToRule(content, mockId); - expect(result.globs).toBe("**/test/**/*.kt"); + expect(result.globs).toBe("/path/to/**/test/**/*.kt"); // The result should be normalized to \n expect(result.rule).toBe("# Test Rule\n\nThis is a test rule."); expect(result.name).toBe("to/file"); // Should use last two path segments @@ -110,7 +174,7 @@ This is a test rule.`; // Should treat as only markdown when frontmatter is malformed const result = markdownToRule(content, mockId); - expect(result.globs).toBeUndefined(); + expect(result.globs).toBe("/path/to/**/*"); expect(result.rule).toBe(content); expect(result.name).toBe("to/file"); // Should use last two path segments }); diff --git a/packages/config-yaml/src/markdown/markdownToRule.ts b/packages/config-yaml/src/markdown/markdownToRule.ts index 3add16b6c..bc88e1018 100644 --- a/packages/config-yaml/src/markdown/markdownToRule.ts +++ b/packages/config-yaml/src/markdown/markdownToRule.ts @@ -1,3 +1,4 @@ +import path from "path"; import * as YAML from "yaml"; import { PackageIdentifier, @@ -69,6 +70,30 @@ export function getRuleName( return displayName; } +function getGlobPattern( + globs: RuleFrontmatter["globs"], + id: PackageIdentifier, +) { + if (id.uriType !== "file") { + return globs; + } + const dir = path.dirname(id.filePath); + const prependDirAndApplyGlobstar = (glob: string) => { + // skip if glob is a directory + if (glob.includes("/")) { + return path.join(dir, glob); + } + return path.join(dir, `**/${glob}`); + }; + if (!globs) { + return prependDirAndApplyGlobstar("**/*"); + } + if (Array.isArray(globs)) { + return globs.map(prependDirAndApplyGlobstar); + } + return prependDirAndApplyGlobstar(globs); +} + export function markdownToRule( rule: string, id: PackageIdentifier, @@ -78,7 +103,7 @@ export function markdownToRule( return { name: getRuleName(frontmatter, id), rule: markdown, - globs: frontmatter.globs, + globs: getGlobPattern(frontmatter.globs, id), regex: frontmatter.regex, description: frontmatter.description, alwaysApply: frontmatter.alwaysApply, From 581ff1af9a347e2ae6a087c27087aa16a91e5dc9 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Thu, 3 Jul 2025 13:02:38 +0530 Subject: [PATCH 2/4] fix dir prepend and .continue append --- .../src/markdown/markdownToRule.test.ts | 35 ++++++++++++++++--- .../src/markdown/markdownToRule.ts | 8 +++-- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/config-yaml/src/markdown/markdownToRule.test.ts b/packages/config-yaml/src/markdown/markdownToRule.test.ts index 3f9cfc396..0d96ab19e 100644 --- a/packages/config-yaml/src/markdown/markdownToRule.test.ts +++ b/packages/config-yaml/src/markdown/markdownToRule.test.ts @@ -67,6 +67,14 @@ This is a test rule with empty frontmatter.`; }); describe("glob patterns", () => { + // we want to ensure the following glob changes happen + // *.ts -> path/to/**/*.ts + // **/subdir/** -> path/to/**/**/subdir/** OR path/to/**/subdir/** + // myfile -> path/to/**/myfile (any depth including 0) + // mydir/ -> path/to/**/mydir/** + // **abc** -> path/to/**abc** + // *xyz* -> path/to/**/*xyz* + it("should match glob pattern for file extensions", () => { const content = `--- globs: "*.ts" @@ -109,12 +117,12 @@ name: glob pattern testing uriType: "file", filePath: "/", }); - expect(result.globs).toBe("/src/**/Dockerfile"); + expect(result.globs).toBe("/**/src/**/Dockerfile"); }); - it("should match have nested folder for multiple globs", () => { + it("should match for multiple globs", () => { const content = `--- -globs: ["**/nested/**/deeper/**/*.rs", ".zshrc"] +globs: ["**/nested/**/deeper/**/*.rs", ".zshrc1", "**abc**", "*xyz*"] name: glob pattern testing --- @@ -125,9 +133,28 @@ This is a test rule.`; const result = markdownToRule(content, mockId); expect(result.globs).toEqual([ "/path/to/**/nested/**/deeper/**/*.rs", - "/path/to/**/.zshrc", + "/path/to/**/.zshrc1", + "/path/to/**abc**", + "/path/to/**/*xyz*", ]); }); + + it("should not prepend when inside .continue", () => { + const content = `--- +globs: ".git" +name: glob pattern testing +--- + + # Test Rule + + This is a test rule.`; + + const result = markdownToRule(content, { + uriType: "file", + filePath: "/Documents/myproject/.continue/rules/rule1.md", + }); + expect(result.globs).toBe(".git"); + }); }); it("should handle frontmatter with whitespace", () => { diff --git a/packages/config-yaml/src/markdown/markdownToRule.ts b/packages/config-yaml/src/markdown/markdownToRule.ts index bc88e1018..41306fe3d 100644 --- a/packages/config-yaml/src/markdown/markdownToRule.ts +++ b/packages/config-yaml/src/markdown/markdownToRule.ts @@ -78,15 +78,17 @@ function getGlobPattern( return globs; } const dir = path.dirname(id.filePath); + if (dir.includes(".continue")) { + return globs; + } const prependDirAndApplyGlobstar = (glob: string) => { - // skip if glob is a directory - if (glob.includes("/")) { + if (glob.startsWith("**")) { return path.join(dir, glob); } return path.join(dir, `**/${glob}`); }; if (!globs) { - return prependDirAndApplyGlobstar("**/*"); + return path.join(dir, "**/*"); } if (Array.isArray(globs)) { return globs.map(prependDirAndApplyGlobstar); From d30483f96710b707492bbfbd5eafffe07e3d1940 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Thu, 3 Jul 2025 13:11:03 +0530 Subject: [PATCH 3/4] fix tests --- .../src/markdown/createMarkdownRule.test.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/config-yaml/src/markdown/createMarkdownRule.test.ts b/packages/config-yaml/src/markdown/createMarkdownRule.test.ts index 52fb9db33..982e823bf 100644 --- a/packages/config-yaml/src/markdown/createMarkdownRule.test.ts +++ b/packages/config-yaml/src/markdown/createMarkdownRule.test.ts @@ -9,7 +9,7 @@ import { markdownToRule } from "./markdownToRule.js"; // Mock package identifier for testing const mockPackageId: PackageIdentifier = { uriType: "file", - filePath: "/path/to/file", + filePath: "/path/to/folder", }; describe("sanitizeRuleName", () => { @@ -85,7 +85,9 @@ Just markdown content.`; expect(parsed.name).toBe(originalFrontmatter.name); expect(parsed.description).toBe(originalFrontmatter.description); - expect(parsed.globs).toEqual(originalFrontmatter.globs); + expect(parsed.globs).toEqual( + originalFrontmatter.globs.map((glob) => `/path/to/**/${glob}`), + ); expect(parsed.alwaysApply).toBe(originalFrontmatter.alwaysApply); expect(parsed.rule).toBe(originalMarkdown); }); @@ -102,7 +104,7 @@ describe("createRuleMarkdown", () => { const parsed = markdownToRule(result, mockPackageId); expect(parsed.description).toBe("Test description"); - expect(parsed.globs).toEqual(["*.ts", "*.js"]); + expect(parsed.globs).toEqual(["/path/to/**/*.ts", "/path/to/**/*.js"]); expect(parsed.alwaysApply).toBe(true); expect(parsed.rule).toBe("This is the rule content"); }); @@ -113,7 +115,7 @@ describe("createRuleMarkdown", () => { const parsed = markdownToRule(result, mockPackageId); expect(parsed.description).toBeUndefined(); - expect(parsed.globs).toBeUndefined(); + expect(parsed.globs).toBe("/path/to/**/*"); expect(parsed.alwaysApply).toBeUndefined(); expect(parsed.rule).toBe("Simple content"); }); @@ -124,7 +126,7 @@ describe("createRuleMarkdown", () => { }); const parsed = markdownToRule(result, mockPackageId); - expect(parsed.globs).toBe("*.py"); + expect(parsed.globs).toBe("/path/to/**/*.py"); }); it("should trim description and globs", () => { @@ -135,7 +137,7 @@ describe("createRuleMarkdown", () => { const parsed = markdownToRule(result, mockPackageId); expect(parsed.description).toBe("spaced description"); - expect(parsed.globs).toBe("*.ts"); + expect(parsed.globs).toBe("/path/to/**/*.ts"); }); it("should handle alwaysApply false explicitly", () => { From 4d5423294aa6aa888d70ef3f94a8bd6aa76bd111 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Thu, 10 Jul 2025 08:49:32 +0530 Subject: [PATCH 4/4] use string concat instead of path.join --- packages/config-yaml/src/markdown/markdownToRule.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/config-yaml/src/markdown/markdownToRule.ts b/packages/config-yaml/src/markdown/markdownToRule.ts index 41306fe3d..231b2ab4a 100644 --- a/packages/config-yaml/src/markdown/markdownToRule.ts +++ b/packages/config-yaml/src/markdown/markdownToRule.ts @@ -77,18 +77,21 @@ function getGlobPattern( if (id.uriType !== "file") { return globs; } - const dir = path.dirname(id.filePath); + let dir = path.dirname(id.filePath); if (dir.includes(".continue")) { return globs; } + if (!dir.endsWith("/")) { + dir = dir.concat("/"); + } const prependDirAndApplyGlobstar = (glob: string) => { if (glob.startsWith("**")) { - return path.join(dir, glob); + return dir.concat(glob); } - return path.join(dir, `**/${glob}`); + return dir.concat("**/", glob); }; if (!globs) { - return path.join(dir, "**/*"); + return dir.concat("**/*"); } if (Array.isArray(globs)) { return globs.map(prependDirAndApplyGlobstar);