-
Notifications
You must be signed in to change notification settings - Fork 53
Add DateTimeOffset.Now and DateTimeOffset.UtcNow detection to Roslyn analyzer #547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
…roperty handling Co-authored-by: YunchuWang <12449837+YunchuWang@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR extends the Roslyn analyzer to detect non-deterministic DateTimeOffset.Now and DateTimeOffset.UtcNow usage in orchestrations, matching the existing detection for DateTime equivalents. The change addresses a gap where only DateTime properties were being analyzed while DateTimeOffset was overlooked.
Key Changes:
- Extended analyzer to detect
DateTimeOffset.NowandUtcNowalongside existingDateTimechecks - Fixed a bug where
returnwas changed tocontinueto avoid skipping properties in the loop - Updated code fixer to generate appropriate cast expressions
(DateTimeOffset)context.CurrentUtcDateTimewhen fixing DateTimeOffset usage
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Analyzers/Orchestration/DateTimeOrchestrationAnalyzer.cs |
Added DateTimeOffset type symbol lookup and extended property checking logic to handle both DateTime and DateTimeOffset types; fixed loop bug by changing return to continue |
src/Analyzers/Orchestration/DateTimeOrchestrationFixer.cs |
Enhanced fixer to use semantic analysis for type detection and generate cast expressions for DateTimeOffset replacements |
test/Analyzers.Tests/Orchestration/DateTimeOrchestrationAnalyzerTests.cs |
Added comprehensive test coverage for DateTimeOffset.Now and UtcNow across DurableFunction, TaskOrchestrator, and FuncOrchestrator patterns |
The code changes are well-structured and properly implement the feature as described. The implementation correctly handles the differences between DateTime and DateTimeOffset (notably that DateTimeOffset lacks a Today property), and the test coverage is comprehensive across all orchestration patterns. The bug fix changing return to continue on line 72 is particularly important as it ensures all properties in a method are analyzed rather than stopping after the first non-matching property.
|
@copilot update pr description to follow .github/PULL_REQUEST_TEMPLATE.md |
Updated PR description to follow the template structure: SummaryWhat changed?
Why is this change needed?The Roslyn analyzer already detects non-deterministic Issues / work itemsProject checklist
AI-assisted code disclosure (required)Was an AI tool used? (select one)
If AI was used:
AI verification (required if AI was used):
TestingAutomated tests
Manual validation (only if runtime/behavior changed)
Notes for reviewers
ExampleBefore this change: [Function("Run")]
DateTimeOffset Run([OrchestrationTrigger] TaskOrchestrationContext context)
{
return DateTimeOffset.Now; // No warning
}After this change, the analyzer reports a diagnostic (DURABLE0001) and suggests: return (DateTimeOffset)context.CurrentUtcDateTime; |
|
found the orchestration analyzer does not detect non function orchestration correctly, will fix in another pr. |

The Roslyn analyzer detects non-deterministic
DateTime.Now,DateTime.UtcNow, andDateTime.Todayusage in orchestrations but was missing equivalentDateTimeOffsetproperties.Changes
Analyzer (
DateTimeOrchestrationAnalyzer.cs)DateTimeOffsetsymbol lookup alongsideDateTimeNowandUtcNowfor both types;Todayonly forDateTime(DateTimeOffset lacks this property)returntocontinuein property loop to avoid skipping subsequent propertiesFixer (
DateTimeOrchestrationFixer.cs)DateTimeOffsettype instead of string matchingCurrentUtcDateTimetoDateTimeOffsetwhen needed:(DateTimeOffset)context.CurrentUtcDateTimeTests (
DateTimeOrchestrationAnalyzerTests.cs)DateTimeOffset.NowandDateTimeOffset.UtcNowacross DurableFunction, TaskOrchestrator, and FuncOrchestrator patternsExample
Before, this would not be detected:
After, the analyzer reports a diagnostic and suggests:
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.