4.8 KiB
Roslyn SARIF Generation and Codacy Integration
Overview
QuanTAlib now automatically generates Roslyn SARIF (Static Analysis Results Interchange Format) files during every build and uploads them to Codacy for continuous code quality monitoring.
Configuration
Build Configuration
The Directory.Build.props file has been configured to generate SARIF files for all projects:
<PropertyGroup>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<TreatWarningsAsErrors Condition="'$(Configuration)' == 'Release'">true</TreatWarningsAsErrors>
<ErrorLog>$(MSBuildProjectDirectory)/roslyn.sarif</ErrorLog>
<ErrorLogFormat>SARIF2.1</ErrorLogFormat>
</PropertyGroup>
Git Configuration
SARIF files are excluded from version control via .gitignore:
# Roslyn SARIF files (generated during build and uploaded to Codacy)
**/roslyn.sarif
roslyn.sarif
CI/CD Pipeline
Build Phase
The GitHub Actions workflow (Publish.yml) includes SARIF generation in the build step:
- Build Projects: All projects are built in Debug configuration
- Collect SARIF Files: All
roslyn.sariffiles are collected from project directories - Upload Artifacts: SARIF files are uploaded as artifacts for downstream jobs
Codacy Upload Phase
A dedicated job (Codacy_SARIF_Upload) handles SARIF file uploads:
- Download SARIF Reports: Retrieves SARIF artifacts from the build job
- Install Codacy CLI: Downloads the latest Codacy Analysis CLI
- Upload to Codacy: Uploads each SARIF file using the Codacy CLI with project metadata
Local Development
Generate SARIF Files
SARIF files are automatically generated during any build:
dotnet build --configuration Debug
After building, SARIF files will be located in each project directory:
lib/roslyn.sarif- Main library analysisquantower/roslyn.sarif- Quantower adapter analysis
View SARIF Files
SARIF files are JSON-formatted and can be viewed with:
- Visual Studio Code with SARIF Viewer extension
- Any text editor (JSON format)
- Codacy web interface (after upload)
Analyzers Included
The following Roslyn analyzers contribute to the SARIF reports:
-
Roslynator.Analyzers (v4.12.9)
- Code style and quality rules
- Performance optimizations
- Modern C# patterns
-
Meziantou.Analyzer (v2.0.183)
- Security and correctness rules
- API usage guidelines
- Best practices enforcement
-
SonarAnalyzer.CSharp (v10.x)
- Code smells and bugs
- Security vulnerabilities
- Maintainability issues
-
.NET SDK Analyzers
- Framework-specific rules
- API compatibility
- Performance guidelines
Suppressed Rules
Certain rules are suppressed globally in Directory.Build.props:
<NoWarn>$(NoWarn);S1144;S1944;S2053;S2245;S2259;S2583;S2589;S3329;S3655;S3776;S3949;S3966;S4158;S4347;S5773;S6781;MA0048;MA0051</NoWarn>
These suppressions are intentional design decisions aligned with QuanTAlib's high-performance requirements.
Codacy Integration
Required Secrets
The GitHub Actions workflow requires the following secret:
CODACY_PROJECT_TOKEN: API token for uploading results to Codacy
Upload Process
- SARIF files are collected after build
- Each SARIF file is uploaded individually
- Results are associated with the specific commit SHA
- Tool identifier:
roslyn - Upload continues even if individual files fail
View Results
Analysis results are available at: https://app.codacy.com/gh/mihakralj/QuanTAlib
Troubleshooting
SARIF Not Generated
If SARIF files are not being generated:
- Verify
ErrorLogproperty is set inDirectory.Build.props - Ensure analyzers are installed (check NuGet packages)
- Build in Debug or Release configuration (not Clean)
- Check MSBuild output for analyzer warnings
Upload Failures
If Codacy uploads fail:
- Verify
CODACY_PROJECT_TOKENsecret is set - Check GitHub Actions logs for specific errors
- Ensure SARIF files contain valid JSON
- Verify network connectivity to Codacy API
Large SARIF Files
If SARIF files become too large:
- Increase
upload-batch-sizein the workflow - Consider splitting uploads by project
- Review suppressed warnings (might need adjustment)
- Use
--upload-batch-size 100000for very large files
Performance Impact
- Build Time: +5-10% due to analyzer execution
- SARIF Generation: <1s per project
- File Size: 100KB-500KB per project
- Upload Time: 2-5s per SARIF file
Future Enhancements
Potential improvements for consideration:
- Differential Analysis: Upload only changed files
- Parallel Uploads: Upload multiple SARIF files concurrently
- Local Validation: Pre-commit hooks to validate SARIF
- Custom Rules: Project-specific analyzer configurations
- Trend Analysis: Track metrics over time