Skip to content

[Bug]: Glob pattern data loss in CopilotRule roundtrip conversion #5

Open

Description

@flanny7

Hello, I always appreciate rulesync.

Problem

I found a data loss bug in CopilotRule.toRulesyncRule(). When converting a root rule with applyTo: "**", the value is normalized to "**/*" during conversion to a RulesyncRule, but this original value is never restored on the way back, causing the roundtrip to be non-idempotent.

Evidence

In src/features/rules/copilot-rule.ts around line 117–118:

if (this.isRoot()) {
  globs = ["**/*"];  // Always hardcoded — original "**" is lost
}

And in fromRulesyncRule() around line 160:

applyTo: rulesyncFrontmatter.globs?.length
  ? rulesyncFrontmatter.globs.join(",")
  : undefined,

The roundtrip results in:

Step Value
Input (applyTo) "**"
After toRulesyncRule (globs) ["**/*"]
After fromRulesyncRule (applyTo) "**/*"

The original "**" is permanently replaced with "**/*" after a single conversion cycle.

Reproduction Steps

  1. Create a CopilotRule with applyTo: "**" in frontmatter
  2. Call .toRulesyncRule() → observe globs: ["**/*"]
  3. Call CopilotRule.fromRulesyncRule() on the result → observe applyTo: "**/*" (not "**")

Expected Behavior

A roundtrip conversion should preserve the original glob value, or at minimum, the two values ("**" and "**/*") should be treated as equivalent throughout the pipeline.

Actual Behavior

"**" is silently replaced with "**/*", causing inconsistency when re-importing or re-syncing rules.

Your consideration would be appreciated.

Activity

  1. flanny7 commented on Apr 22, 2026

    @flanny7
    OwnerAuthor

    Update: Real-World Impact Scenarios and Reproduction Steps

    When Does This Bug Appear?

    The bug surfaces when users perform a generate → import roundtrip, which is a common workflow when syncing rules across team members or migrating an existing project.

    Scenario 1: Syncing a shared repository

    1. Team member A writes .rulesync/rules/overview.md with root: true and no globs:
      ---
      targets: ["*"]
      root: true
      ---
      Shared coding guidelines here.
    2. Runs rulesync generate --targets copilot → .github/copilot-instructions.md is created correctly (body only, no frontmatter)
    3. Team member B checks out the repo and runs rulesync import --targets copilot to sync
    4. Result: A new .rulesync/rules/copilot-instructions.md is created with unexpected globs: ["**/*"], and overview.md remains untouched — causing two conflicting root rules

    Scenario 2: Re-importing after editing the Copilot file directly

    1. User edits .github/copilot-instructions.md directly (e.g., quick hotfix)
    2. Runs rulesync import --targets copilot to sync the change back
    3. Result: The imported rulesync rule gains globs: ["**/*"] that was not in the original source rule

    Reproduction Steps (Verified)

    # 1. Create a minimal test project
    mkdir -p /tmp/rulesync-bug-test/.rulesync/rules
    
    cat > /tmp/rulesync-bug-test/rulesync.jsonc << 'JSON'
    {"targets": ["copilot"], "features": ["rules"]}
    JSON
    
    # 2. Create a root rule WITHOUT globs (as a typical user would)
    cat > /tmp/rulesync-bug-test/.rulesync/rules/overview.md << 'MD'
    ---
    targets: ["*"]
    root: true
    ---
    This is the overview rule.
    MD
    
    # 3. Generate Copilot files
    cd /tmp/rulesync-bug-test && bunx rulesync@latest generate --targets copilot --features rules
    # → .github/copilot-instructions.md is created correctly (body only)
    
    # 4. Verify the generated file (expected: no frontmatter)
    cat /tmp/rulesync-bug-test/.github/copilot-instructions.md
    # Output: "This is the overview rule."  ✅
    
    # 5. Run import (this is where the bug triggers)
    cd /tmp/rulesync-bug-test && bunx rulesync@latest import --targets copilot
    
    # 6. Inspect the imported rulesync rule
    cat /tmp/rulesync-bug-test/.rulesync/rules/copilot-instructions.md

    Actual output after step 6:

    ---
    root: true
    targets:
      - '*'
    globs:
      - '**/*'    # ← Unexpected! Was not in the original overview.md
    ---
    This is the overview rule.

    Expected output after step 6:

    ---
    root: true
    targets:
      - '*'
    # globs should be absent, or at minimum not silently added
    ---
    This is the overview rule.

    Cascade Failure: Second Generate Breaks

    After the import, running generate --dry-run again produces an error:

    cd /tmp/rulesync-bug-test && bunx rulesync@latest generate --targets copilot --features rules --dry-run
    # Error: Multiple root rulesync rules found for target 'copilot':
    #   .rulesync/rules/copilot-instructions.md
    #   .rulesync/rules/overview.md

    Because import creates a new file (copilot-instructions.md) rather than updating the original (overview.md), the project ends up with two root rules, and subsequent generate invocations fail entirely.

    There is no rush at all. Your consideration would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions