Skip to content
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

Error when TearDown method is defined in partial test classes - Syntax node is not within syntax tree #766

Closed
KaiBNET opened this issue Jul 5, 2024 · 1 comment · Fixed by #774
Assignees
Milestone

Comments

@KaiBNET
Copy link

KaiBNET commented Jul 5, 2024

When a test class is separated into different files via partial classes and contain a method marked with TearDown attribute, I get the following error:

Error AD0001: Analyzer 'NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer' threw an exception of type 'System.ArgumentException' with message 'Syntax node is not within syntax tree'.

When I move the TearDown method into a base class, everything works.

@izhovkin
Copy link

izhovkin commented Jul 16, 2024

Have the same error in partial test class, however, it does not have TearDown methods. Looks like it occurs when analyzing field assignments.

Stacktrace:

Errors
            CSC error AD0001: Analyzer 'NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer' threw an exception of type 'System.ArgumentException' with message 'Syntax node is not within syntax tree'. [***.csproj]
        Exception occurred with following context:
        Compilation: ***.UnitTests
        SyntaxTree: ***Tests.cs
        SyntaxNode: [TestFixture] public partial class ... [ClassDeclarationSyntax]@[664..25904) (19,4)-(572,5)
        System.ArgumentException: Syntax node is not within syntax tree
        at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.CheckSyntaxNode(CSharpSyntaxNode syntax)
        at Microsoft.CodeAnalysis.CSharp.CSharpSemanticModel.GetTypeInfo(ExpressionSyntax expression, CancellationToken cancellationToken)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.NeedsDisposal(SemanticModel model, ExpressionSyntax expression)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, ExpressionSyntax expression)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, StatementSyntax statement)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, SyntaxList`1 statements)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, StatementSyntax statement)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, BaseMethodDeclarationSyntax method)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, IMethodSymbol symbol)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AssignedIn(Parameters parameters, HashSet`1 assignments, IEnumerable`1 methods)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AnalyzeAssignedButNotDisposed(SyntaxNodeAnalysisContext context, Dictionary`2 symbols, Parameters parameters, String where, IEnumerable`1 setUpMethods, IEnumerable`1 tearDownMethods, HashSet`1 assignedWithInitializers)
        at NUnit.Analyzers.DisposeFieldsInTearDown.DisposeFieldsAndPropertiesInTearDownAnalyzer.AnalyzeDisposableFields(SyntaxNodeAnalysisContext context)
        at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__52`1.<ExecuteSyntaxNodeAction>b__52_0(ValueTuple`2 data)
        at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)
        -----
        Suppress the following diagnostics to disable this analyzer: NUnit1032

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants