Skip to content

[BUG] Compilation fails on applyTo patterns with multiple brace groups #153

@sergio-sisternes-epam

Description

@sergio-sisternes-epam

Describe the bug

apm compile fails with Distributed compilation failed: 'ts,js,mts,mjs' when an instruction uses an applyTo pattern containing multiple brace groups, e.g. **/*.{test,spec}.{ts,js,mts,mjs}.

The root cause is in _expand_glob_pattern() at src/apm_cli/compilation/context_optimizer.py (line ~755). The method:

  1. Finds the first brace group {test,spec} via regex
  2. Replaces it with {}, producing **/*.{}.{ts,js,mts,mjs}
  3. Calls Python's str.format() on the result — which interprets the second brace group {ts,js,mts,mjs} as a format placeholder
  4. This raises KeyError: 'ts,js,mts,mjs'

The exception propagates up to compile_distributed() which catches it generically, producing the unhelpful error message.

To Reproduce

  1. Create an instruction file with applyTo: "**/*.{test,spec}.{ts,js,mts,mjs}" in its frontmatter
  2. Run apm compile --verbose
  3. Compilation fails with:
    Compilation failed with 1 errors:
      ❌ Distributed compilation failed: 'ts,js,mts,mjs'
    

Expected behavior

The pattern should be fully expanded into all combinations:

  • **/*.test.ts, **/*.test.js, **/*.test.mts, **/*.test.mjs
  • **/*.spec.ts, **/*.spec.js, **/*.spec.mts, **/*.spec.mjs

Environment (please complete the following information):

  • OS: macOS
  • Python Version: 3.13.11
  • APM Version: 0.7.4
  • VSCode Version: N/A (CLI only)

Logs

⚙️ Starting context compilation...
Compiling for AGENTS.md (VSCode/Copilot) - detected .github/ folder
Verbose mode: showing source attribution and optimizer analysis
⏱️  📊 Project Analysis: 7.7ms
Compilation failed with 1 errors:
  ❌ Distributed compilation failed: 'ts,js,mts,mjs'

Additional context

Single brace group patterns like **/*.{ts,tsx} are widely used across the APM ecosystem — in APM's own templates (hello-world), official docs (examples.md), and tests. The brace expansion logic in _expand_glob_pattern handles single groups correctly but fails on multiple groups. Multi-brace patterns like **/*.{test,spec}.{ts,js,mts,mjs} are standard glob syntax (supported by VS Code's files.include/files.exclude, bash, zsh, etc.) and should be expected in applyTo values.

The fix should recursively expand all brace groups in a pattern, not just the first one. Additionally, str.format() should be avoided since remaining brace groups in the string will be misinterpreted as format placeholders — string concatenation or str.replace() (with maxcount=1) would be safer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions