Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 5, 2026

The XML documentation for Ancestors(), AncestorsAndSelf(), and FirstAncestorOrSelf() methods did not specify the traversal order, while similar methods like DescendantNodes() explicitly document ordering as "in prefix document order."

Changes

Updated XML documentation for four public methods in SyntaxNode:

  • Ancestors(): Returns "ancestor nodes in order from the innermost containing syntactic ancestor to the outermost"
  • AncestorsAndSelf(): Returns "ancestor nodes (including this node) in order from this node to the outermost ancestor"
  • FirstAncestorOrSelf<TNode>(): Clarifies "ancestors are searched in order from this node to the outermost ancestor"
  • FirstAncestorOrSelf<TNode, TArg>(): Same clarification as above

Example

var expr = SyntaxFactory.ParseExpression("a + (b - (c * (d / e)))");
var e = expr.DescendantNodes().OfType<IdentifierNameSyntax>().First(n => n.Identifier.Text == "e");

var ancestors = e.Ancestors().ToList();
// ancestors[0] is DivideExpression (immediate parent)
// ancestors[1] is ParenthesizedExpression
// ancestors[2] is MultiplyExpression
// ...
// ancestors[6] is AddExpression (root)

This ordering allows reliable use of LINQ methods like .First() to get the immediate parent or closest matching ancestor.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • kijvsblobprodcus387.vsblob.vsassets.io
    • Triggering command: /usr/share/dotnet/dotnet dotnet build src/Compilers/Core/Portable/Microsoft.CodeAnalysis.csproj --no-incremental /home/REDACTED/work/roslyn/roslyn/src/ExpressionEvaluator/Core/Test/ResultProvider/Debugger/Engine/DkmSuccessEvaluationResult.cs /home/REDACTED/work/roslyn/roslyn/src/ExpressionEvaluator/Core/Test/ResultProvider/Debugger/Engine/DkmCompilerId.cs /home/REDACTED/work/roslyn/roslyn/src/ExpressionEvaluator/Core/Test/ResultProvider/Debugger/Engine/DkmClrAppDomain.cs /home/REDACTED/work/roslyn/roslyn/src/ExpressionEvaluator/Core/Test/ResultProvider/Debugger/Engine/IDkmClrFormatter.cs /home/REDACTED/work/roslyn/roslyn/src/ExpressionEvaluator/Core/Test/ResultProvider/Debugger/Engine/DkmEvaluationResultFlags.cs /home/REDACTED/work/roslyn/roslyn/src/ExpressionEvaluator/Core/Test/ResultProvider/Deb�� /home/REDACTED/work/roslyn/roslyn/src/ExpressionEvaluator/Core/Test/ResultProvider/Debugger/Engine/DkmDataItem.cs /home/REDACTED/work/roslyn/roslyn/src/ExpressionEvaluator/Core/Test/ResultProvider/Debugger/Engine/DkmEvaluationFlags.cs (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>SyntaxNode.Ancestors() does not specify order of returned list</issue_title>
<issue_description>### Type of issue

Missing information

Description

The documentation for SyntaxNode.Ancestors() and .AncestorsAndSelf() does not specify the order of the nodes in the returned list. Knowing the order is important because it affects whether we can use LINQ selectors like .First() on the result and get what we expect.

By comparison, other docs pages (like ChildNodes, DescendantNodes, etc) do mention the ordering.

Page URL

https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.syntaxnode.ancestors?view=roslyn-dotnet-5.0.0&viewFallbackFrom=net-8.0

Content source URL

https://github.com/dotnet/roslyn-api-docs/blob/live/dotnet/xml/Microsoft.CodeAnalysis/SyntaxNode.xml</issue_description>

<agent_instructions>@copilot Look for as many public 'AncestorXXX' methods you can find in the syntax model and document them. This is at least 'Ancestors' and 'AncestorsAndSelf'.

This should document that this returns ancestors from innermost containing syntactic ancestor to outermost. </agent_instructions>

Comments on the Issue (you are @copilot in this section)

@jjonescz If the docs don't mention ordering, that likely means the order is not guaranteed. Might need API review to change that.

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Copilot AI changed the title [WIP] Update documentation for SyntaxNode.Ancestors() order Document ordering of SyntaxNode Ancestor methods Jan 5, 2026
Copilot AI requested a review from CyrusNajmabadi January 5, 2026 17:12
@CyrusNajmabadi
Copy link
Member

@dotnet/roslyn-compiler for more eyes.

@jcouv
Copy link
Member

jcouv commented Jan 5, 2026

for more eyes.

FYI, the PR is in Draft state and doesn't have any labels

@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review January 5, 2026 20:34
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner January 5, 2026 20:34
Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks (commit 2)

@jcouv
Copy link
Member

jcouv commented Jan 5, 2026

Can be merged with a single review single doc-only change. Thanks

@jcouv jcouv self-assigned this Jan 5, 2026
@CyrusNajmabadi
Copy link
Member

Can be merged with a single review single doc-only change. Thanks

Good to know. Thanks!

@CyrusNajmabadi CyrusNajmabadi merged commit 387221e into main Jan 5, 2026
25 checks passed
@CyrusNajmabadi CyrusNajmabadi deleted the copilot/update-syntaxnode-ancestors-docs branch January 5, 2026 21:57
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jan 5, 2026
@davidwengier davidwengier modified the milestones: Next, 18.3 Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SyntaxNode.Ancestors() does not specify order of returned list

5 participants