|
| 1 | +# GitHub Copilot Instructions for ASP.NET Core Razor |
| 2 | + |
| 3 | +This repository contains the **ASP.NET Core Razor** compiler and tooling implementation. It provides the Razor language experience across Visual Studio, Visual Studio Code, and other development environments. |
| 4 | + |
| 5 | +## Repository Overview |
| 6 | + |
| 7 | +This repository implements: |
| 8 | + |
| 9 | +- **Razor Compiler**: The core Razor compilation engine and source generators |
| 10 | +- **Language Server**: LSP-based language services for cross-platform editor support |
| 11 | +- **Visual Studio Integration**: Rich editing experience and tooling for Visual Studio |
| 12 | +- **IDE Tools**: Debugging, IntelliSense, formatting, and other developer productivity features |
| 13 | + |
| 14 | +### Key Components |
| 15 | + |
| 16 | +| Component | Purpose | Key Projects | |
| 17 | +|-----------|---------|--------------| |
| 18 | +| **Compiler** | Core Razor compilation and code generation | `Microsoft.AspNetCore.Razor.Language`, `Microsoft.CodeAnalysis.Razor.Compiler` | |
| 19 | +| **Language Server** | Cross-platform language services via LSP | `Microsoft.AspNetCore.Razor.LanguageServer` | |
| 20 | +| **Visual Studio** | VS-specific tooling and integration | `Microsoft.VisualStudio.RazorExtension`, `Microsoft.VisualStudio.LanguageServices.Razor` | |
| 21 | +| **Workspaces** | Project system and document management | `Microsoft.CodeAnalysis.Razor.Workspaces` | |
| 22 | + |
| 23 | +## Razor Language Concepts |
| 24 | + |
| 25 | +When working with this codebase, understand these core Razor concepts: |
| 26 | + |
| 27 | +### File Types and Extensions |
| 28 | +- `.razor` - Blazor components (client-side and server-side) |
| 29 | +- `.cshtml` - Razor views and pages (ASP.NET Core MVC/Pages) |
| 30 | + |
| 31 | +### Language Kinds |
| 32 | +Razor documents contain multiple languages: |
| 33 | +- **Razor syntax** - `@` expressions, directives, code blocks |
| 34 | +- **C# code** - Server-side logic embedded in Razor |
| 35 | +- **HTML markup** - Static HTML and dynamic content |
| 36 | +- **JavaScript/CSS** - Client-side code within Razor files |
| 37 | + |
| 38 | +### Key Architecture Patterns |
| 39 | +- **RazorCodeDocument** - Central document model containing parsed syntax tree and generated outputs |
| 40 | +- **Language Server Protocol (LSP)** - Cross-platform editor integration |
| 41 | +- **Virtual Documents** - Separate C# and HTML projections for tooling |
| 42 | +- **Document Mapping** - Translation between Razor and generated C# positions |
| 43 | + |
| 44 | +## Development Guidelines |
| 45 | + |
| 46 | +### Code Style and Patterns |
| 47 | + |
| 48 | +Follow these patterns when contributing: |
| 49 | + |
| 50 | +```csharp |
| 51 | +// ✅ Prefer readonly fields and properties |
| 52 | +private readonly ILogger _logger; |
| 53 | + |
| 54 | +// ✅ Use explicit interface implementations for internal contracts |
| 55 | +internal sealed class MyService : IMyService |
| 56 | +{ |
| 57 | + public void DoWork() { } |
| 58 | +} |
| 59 | + |
| 60 | +// ✅ Use cancellation tokens in async methods |
| 61 | +public async Task<Result> ProcessAsync(CancellationToken cancellationToken) |
| 62 | +{ |
| 63 | + // Implementation |
| 64 | +} |
| 65 | + |
| 66 | +// ✅ Follow null-checking patterns |
| 67 | +public void Method(string? value) |
| 68 | +{ |
| 69 | + if (value is null) |
| 70 | + { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // Use value here |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### Testing Patterns |
| 79 | + |
| 80 | +- Prefer `TestCode` over plain strings for before/after test scenarios |
| 81 | +- Prefer raw string literals over verbatim strings |
| 82 | +- Ideally we test the end user scenario, not implementation details |
| 83 | + |
| 84 | +### Architecture Considerations |
| 85 | + |
| 86 | +- **Performance**: Razor compilation happens on every keystroke - optimize for speed |
| 87 | +- **Cross-platform**: Code should work on Windows, macOS, and Linux |
| 88 | +- **Editor integration**: Consider both Visual Studio and VS Code experiences |
| 89 | +- **Backwards compatibility**: Maintain compatibility with existing Razor syntax |
| 90 | + |
| 91 | +## Build and Development |
| 92 | + |
| 93 | +### Prerequisites |
| 94 | +- .NET 8.0+ SDK (latest version specified in `global.json`) |
| 95 | +- Visual Studio 2022 17.8+ or VS Code with C# extension |
| 96 | +- PowerShell (for Windows build scripts) |
| 97 | + |
| 98 | +### Building |
| 99 | +- `./restore.sh` - Restore dependencies |
| 100 | +- `./build.sh` - Full build |
| 101 | +- `./build.sh -test` - Build and run tests |
| 102 | + |
| 103 | +### Visual Studio Development |
| 104 | +- `./startvs.ps1` - Open Visual Studio with correct environment |
| 105 | +- `./build.cmd -deploy` - Build and deploy VS extension for testing |
| 106 | + |
| 107 | +## Project Structure Navigation |
| 108 | + |
| 109 | +``` |
| 110 | +src/ |
| 111 | +├── Compiler/ # Core Razor compilation engine |
| 112 | +│ ├── Microsoft.AspNetCore.Razor.Language/ # Core language APIs |
| 113 | +│ └── Microsoft.CodeAnalysis.Razor.Compiler/ # Roslyn integration |
| 114 | +├── Razor/ # Language server and tooling |
| 115 | +│ ├── src/Microsoft.AspNetCore.Razor.LanguageServer/ # LSP implementation |
| 116 | +│ ├── src/Microsoft.VisualStudio.RazorExtension/ # VS package |
| 117 | +│ └── test/ # Language server tests |
| 118 | +└── Shared/ # Common utilities and test helpers |
| 119 | +``` |
| 120 | + |
| 121 | +## Debugging and Diagnostics |
| 122 | + |
| 123 | +### Language Server Debugging |
| 124 | +- Use F5 debugging in VS with `Microsoft.VisualStudio.RazorExtension` as startup project |
| 125 | +- Check language server logs in VS Output window (Razor Logger Output) |
| 126 | + |
| 127 | +### Common Issues |
| 128 | +- **Performance**: Check document mapping efficiency and async/await usage |
| 129 | +- **Cross-platform**: Test path handling and case sensitivity |
| 130 | +- **Memory**: Use `using` statements for disposable resources |
| 131 | +- **Threading**: Ensure proper async/await patterns, avoid `Task.Wait()` |
| 132 | + |
| 133 | +## Contributing |
| 134 | + |
| 135 | +- Follow the [Contributing Guidelines](CONTRIBUTING.md) |
| 136 | +- Build and test locally before submitting PRs |
| 137 | +- Add appropriate test coverage for new features |
| 138 | +- Update documentation for public API changes |
| 139 | +- Consider cross-platform compatibility in all changes |
| 140 | + |
| 141 | +## Useful Resources |
| 142 | + |
| 143 | +- [ASP.NET Core Razor documentation](https://docs.microsoft.com/aspnet/core/razor-pages) |
| 144 | +- [Blazor documentation](https://learn.microsoft.com/en-gb/aspnet/core/blazor/?) |
| 145 | +- [Language Server Protocol specification](https://microsoft.github.io/language-server-protocol/) |
| 146 | +- [Build from source instructions](docs/contributing/BuildFromSource.md) |
0 commit comments