|
| 1 | +--- |
| 2 | +description: 'Guidelines for creating high-quality custom instruction files for GitHub Copilot' |
| 3 | +applyTo: '**/*.instructions.md' |
| 4 | +--- |
| 5 | + |
| 6 | +# Custom Instructions File Guidelines |
| 7 | + |
| 8 | +Instructions for creating effective and maintainable custom instruction files that guide GitHub Copilot in generating domain-specific code and following project conventions. |
| 9 | + |
| 10 | + |
| 11 | +## Project Context |
| 12 | + |
| 13 | +- Target audience: Developers and GitHub Copilot working with domain-specific code |
| 14 | +- File format: Markdown with YAML frontmatter |
| 15 | +- File naming convention: lowercase with hyphens (e.g., `react-best-practices.instructions.md`) |
| 16 | +- Location: `.github/instructions/` directory |
| 17 | +- Purpose: Provide context-aware guidance for code generation, review, and documentation |
| 18 | + |
| 19 | +## Required Frontmatter |
| 20 | + |
| 21 | +Every instruction file must include YAML frontmatter with the following fields: |
| 22 | + |
| 23 | +```yaml |
| 24 | +--- |
| 25 | +description: 'Brief description of the instruction purpose and scope' |
| 26 | +applyTo: 'glob pattern for target files (e.g., **/*.ts, **/*.py)' |
| 27 | +--- |
| 28 | +``` |
| 29 | + |
| 30 | +### Frontmatter Guidelines |
| 31 | + |
| 32 | +- **description**: Single-quoted string, 1-500 characters, clearly stating the purpose |
| 33 | +- **applyTo**: Glob pattern(s) specifying which files these instructions apply to |
| 34 | + - Single pattern: `'**/*.ts'` |
| 35 | + - Multiple patterns: `'**/*.ts, **/*.tsx, **/*.js'` |
| 36 | + - Specific files: `'src/**/*.py'` |
| 37 | + - All files: `'**'` |
| 38 | + |
| 39 | +## File Structure |
| 40 | + |
| 41 | +A well-structured instruction file should include the following sections: |
| 42 | + |
| 43 | +### 1. Title and Overview |
| 44 | + |
| 45 | +- Clear, descriptive title using `#` heading |
| 46 | +- Brief introduction explaining the purpose and scope |
| 47 | +- Optional: Project context section with key technologies and versions |
| 48 | + |
| 49 | +### 2. Core Sections |
| 50 | + |
| 51 | +Organize content into logical sections based on the domain: |
| 52 | + |
| 53 | +- **General Instructions**: High-level guidelines and principles |
| 54 | +- **Best Practices**: Recommended patterns and approaches |
| 55 | +- **Code Standards**: Naming conventions, formatting, style rules |
| 56 | +- **Architecture/Structure**: Project organization and design patterns |
| 57 | +- **Common Patterns**: Frequently used implementations |
| 58 | +- **Security**: Security considerations (if applicable) |
| 59 | +- **Performance**: Optimization guidelines (if applicable) |
| 60 | +- **Testing**: Testing standards and approaches (if applicable) |
| 61 | + |
| 62 | +### 3. Examples and Code Snippets |
| 63 | + |
| 64 | +Provide concrete examples with clear labels: |
| 65 | + |
| 66 | +```markdown |
| 67 | +### Good Example |
| 68 | +\`\`\`language |
| 69 | +// Recommended approach |
| 70 | +code example here |
| 71 | +\`\`\` |
| 72 | + |
| 73 | +### Bad Example |
| 74 | +\`\`\`language |
| 75 | +// Avoid this pattern |
| 76 | +code example here |
| 77 | +\`\`\` |
| 78 | +``` |
| 79 | + |
| 80 | +### 4. Validation and Verification (Optional but Recommended) |
| 81 | + |
| 82 | +- Build commands to verify code |
| 83 | +- Linting and formatting tools |
| 84 | +- Testing requirements |
| 85 | +- Verification steps |
| 86 | + |
| 87 | +## Content Guidelines |
| 88 | + |
| 89 | +### Writing Style |
| 90 | + |
| 91 | +- Use clear, concise language |
| 92 | +- Write in imperative mood ("Use", "Implement", "Avoid") |
| 93 | +- Be specific and actionable |
| 94 | +- Avoid ambiguous terms like "should", "might", "possibly" |
| 95 | +- Use bullet points and lists for readability |
| 96 | +- Keep sections focused and scannable |
| 97 | + |
| 98 | +### Best Practices |
| 99 | + |
| 100 | +- **Be Specific**: Provide concrete examples rather than abstract concepts |
| 101 | +- **Show Why**: Explain the reasoning behind recommendations when it adds value |
| 102 | +- **Use Tables**: For comparing options, listing rules, or showing patterns |
| 103 | +- **Include Examples**: Real code snippets are more effective than descriptions |
| 104 | +- **Stay Current**: Reference current versions and best practices |
| 105 | +- **Link Resources**: Include official documentation and authoritative sources |
| 106 | + |
| 107 | +### Common Patterns to Include |
| 108 | + |
| 109 | +1. **Naming Conventions**: How to name variables, functions, classes, files |
| 110 | +2. **Code Organization**: File structure, module organization, import order |
| 111 | +3. **Error Handling**: Preferred error handling patterns |
| 112 | +4. **Dependencies**: How to manage and document dependencies |
| 113 | +5. **Comments and Documentation**: When and how to document code |
| 114 | +6. **Version Information**: Target language/framework versions |
| 115 | + |
| 116 | +## Patterns to Follow |
| 117 | + |
| 118 | +### Bullet Points and Lists |
| 119 | + |
| 120 | +```markdown |
| 121 | +## Security Best Practices |
| 122 | + |
| 123 | +- Always validate user input before processing |
| 124 | +- Use parameterized queries to prevent SQL injection |
| 125 | +- Store secrets in environment variables, never in code |
| 126 | +- Implement proper authentication and authorization |
| 127 | +- Enable HTTPS for all production endpoints |
| 128 | +``` |
| 129 | + |
| 130 | +### Tables for Structured Information |
| 131 | + |
| 132 | +```markdown |
| 133 | +## Common Issues |
| 134 | + |
| 135 | +| Issue | Solution | Example | |
| 136 | +| ---------------- | ------------------- | ----------------------------- | |
| 137 | +| Magic numbers | Use named constants | `const MAX_RETRIES = 3` | |
| 138 | +| Deep nesting | Extract functions | Refactor nested if statements | |
| 139 | +| Hardcoded values | Use configuration | Store API URLs in config | |
| 140 | +``` |
| 141 | + |
| 142 | +### Code Comparison |
| 143 | + |
| 144 | +```markdown |
| 145 | +### Good Example - Using TypeScript interfaces |
| 146 | +\`\`\`typescript |
| 147 | +interface User { |
| 148 | + id: string; |
| 149 | + name: string; |
| 150 | + email: string; |
| 151 | +} |
| 152 | + |
| 153 | +function getUser(id: string): User { |
| 154 | + // Implementation |
| 155 | +} |
| 156 | +\`\`\` |
| 157 | + |
| 158 | +### Bad Example - Using any type |
| 159 | +\`\`\`typescript |
| 160 | +function getUser(id: any): any { |
| 161 | + // Loses type safety |
| 162 | +} |
| 163 | +\`\`\` |
| 164 | +``` |
| 165 | + |
| 166 | +### Conditional Guidance |
| 167 | + |
| 168 | +```markdown |
| 169 | +## Framework Selection |
| 170 | + |
| 171 | +- **For small projects**: Use Minimal API approach |
| 172 | +- **For large projects**: Use controller-based architecture with clear separation |
| 173 | +- **For microservices**: Consider domain-driven design patterns |
| 174 | +``` |
| 175 | + |
| 176 | +## Patterns to Avoid |
| 177 | + |
| 178 | +- **Overly verbose explanations**: Keep it concise and scannable |
| 179 | +- **Outdated information**: Always reference current versions and practices |
| 180 | +- **Ambiguous guidelines**: Be specific about what to do or avoid |
| 181 | +- **Missing examples**: Abstract rules without concrete code examples |
| 182 | +- **Contradictory advice**: Ensure consistency throughout the file |
| 183 | +- **Copy-paste from documentation**: Add value by distilling and contextualizing |
| 184 | + |
| 185 | +## Testing Your Instructions |
| 186 | + |
| 187 | +Before finalizing instruction files: |
| 188 | + |
| 189 | +1. **Test with Copilot**: Try the instructions with actual prompts in VS Code |
| 190 | +2. **Verify Examples**: Ensure code examples are correct and run without errors |
| 191 | +3. **Check Glob Patterns**: Confirm `applyTo` patterns match intended files |
| 192 | + |
| 193 | +## Example Structure |
| 194 | + |
| 195 | +Here's a minimal example structure for a new instruction file: |
| 196 | + |
| 197 | +```markdown |
| 198 | +--- |
| 199 | +description: 'Brief description of purpose' |
| 200 | +applyTo: '**/*.ext' |
| 201 | +--- |
| 202 | + |
| 203 | +# Technology Name Development |
| 204 | + |
| 205 | +Brief introduction and context. |
| 206 | + |
| 207 | +## General Instructions |
| 208 | + |
| 209 | +- High-level guideline 1 |
| 210 | +- High-level guideline 2 |
| 211 | + |
| 212 | +## Best Practices |
| 213 | + |
| 214 | +- Specific practice 1 |
| 215 | +- Specific practice 2 |
| 216 | + |
| 217 | +## Code Standards |
| 218 | + |
| 219 | +### Naming Conventions |
| 220 | +- Rule 1 |
| 221 | +- Rule 2 |
| 222 | + |
| 223 | +### File Organization |
| 224 | +- Structure 1 |
| 225 | +- Structure 2 |
| 226 | + |
| 227 | +## Common Patterns |
| 228 | + |
| 229 | +### Pattern 1 |
| 230 | +Description and example |
| 231 | + |
| 232 | +\`\`\`language |
| 233 | +code example |
| 234 | +\`\`\` |
| 235 | + |
| 236 | +### Pattern 2 |
| 237 | +Description and example |
| 238 | + |
| 239 | +## Validation |
| 240 | + |
| 241 | +- Build command: `command to verify` |
| 242 | +- Linting: `command to lint` |
| 243 | +- Testing: `command to test` |
| 244 | +``` |
| 245 | + |
| 246 | +## Maintenance |
| 247 | + |
| 248 | +- Review instructions when dependencies or frameworks are updated |
| 249 | +- Update examples to reflect current best practices |
| 250 | +- Remove outdated patterns or deprecated features |
| 251 | +- Add new patterns as they emerge in the community |
| 252 | +- Keep glob patterns accurate as project structure evolves |
| 253 | + |
| 254 | +## Additional Resources |
| 255 | + |
| 256 | +- [Custom Instructions Documentation](https://code.visualstudio.com/docs/copilot/customization/custom-instructions) |
| 257 | +- [Awesome Copilot Instructions](https://github.com/github/awesome-copilot/tree/main/instructions) |
0 commit comments