Skip to content

Commit

Permalink
Deprecate API and add NFW telemetry for it
Browse files Browse the repository at this point in the history
  • Loading branch information
CyrusNajmabadi committed Oct 12, 2023
1 parent d49b29e commit 725ba83
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Compilers/CSharp/Portable/Syntax/CSharpSyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;

Expand Down Expand Up @@ -182,6 +183,7 @@ public new SyntaxTriviaList GetTrailingTrivia()
/// <summary>
/// Deserialize a syntax node from the byte stream.
/// </summary>
[Obsolete("Syntax serialization support is deprecated and will be removed in a future version of this API", error: false)]
public static SyntaxNode DeserializeFrom(Stream stream, CancellationToken cancellationToken = default)
{
if (stream == null)
Expand All @@ -194,6 +196,8 @@ public static SyntaxNode DeserializeFrom(Stream stream, CancellationToken cancel
throw new InvalidOperationException(CodeAnalysisResources.TheStreamCannotBeReadFrom);
}

// Report NFW to see if this is being used in the wild.
FatalError.ReportAndPropagate(new SerializationDeprecationException());
using var reader = ObjectReader.TryGetReader(stream, leaveOpen: true, cancellationToken);

if (reader == null)
Expand Down
4 changes: 4 additions & 0 deletions src/Compilers/CSharp/Test/Syntax/Syntax/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Roslyn.Test.Utilities;
using Xunit;

#pragma warning disable CS0618 // Type or member is obsolete

namespace Microsoft.CodeAnalysis.CSharp.UnitTests
{
public class SerializationTests
Expand Down Expand Up @@ -305,3 +307,5 @@ public class C
}
}
}

#pragma warning restore CS0618 // Type or member is obsolete
16 changes: 16 additions & 0 deletions src/Compilers/Core/Portable/Syntax/SyntaxNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;

Expand Down Expand Up @@ -1391,6 +1392,7 @@ public bool IsEquivalentTo(SyntaxNode node, bool topLevel = false)
/// Serializes the node to the given <paramref name="stream"/>.
/// Leaves the <paramref name="stream"/> open for further writes.
/// </summary>
[Obsolete("Syntax serialization support is deprecated and will be removed in a future version of this API", error: false)]
public virtual void SerializeTo(Stream stream, CancellationToken cancellationToken = default)
{
if (stream == null)
Expand All @@ -1403,10 +1405,24 @@ public virtual void SerializeTo(Stream stream, CancellationToken cancellationTok
throw new InvalidOperationException(CodeAnalysisResources.TheStreamCannotBeWrittenTo);
}

// Report NFW to see if this is being used in the wild.
FatalError.ReportAndPropagate(new SerializationDeprecationException());
using var writer = new ObjectWriter(stream, leaveOpen: true, cancellationToken);
writer.WriteValue(Green);
}

/// <summary>
/// Specialized exception subtype to make it easier to search telemetry streams for this specific case.
/// </summary>
private protected sealed class SerializationDeprecationException : Exception
{
public SerializationDeprecationException()
: base("Syntax serialization support is deprecated and will be removed in a future version of this API")
{

}
}

#region Core Methods

/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Compilers/Test/Utilities/CSharp/CSharpTestSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ private CSharpTestSource(object value)

private static void CheckSerializable(SyntaxTree tree)
{
#pragma warning disable CS0618 // Type or member is obsolete
using var stream = new MemoryStream();
var root = tree.GetRoot();
root.SerializeTo(stream);
stream.Position = 0;

// verify absence of exception:
_ = CSharpSyntaxNode.DeserializeFrom(stream);
#pragma warning restore CS0618 // Type or member is obsolete
}

public SyntaxTree[] GetSyntaxTrees(CSharpParseOptions parseOptions, string sourceFileName = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Reflection
Imports System.Threading
Imports Microsoft.CodeAnalysis.ErrorReporting
Imports Microsoft.CodeAnalysis.PooledObjects
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.VisualBasic.Symbols
Expand Down Expand Up @@ -125,6 +126,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
''' <summary>
''' Deserialize a syntax node from a byte stream.
''' </summary>
<Obsolete("Syntax serialization support is deprecated and will be removed in a future version of this API", False)>
Public Shared Function DeserializeFrom(stream As IO.Stream, Optional cancellationToken As CancellationToken = Nothing) As SyntaxNode
If stream Is Nothing Then
Throw New ArgumentNullException(NameOf(stream))
Expand All @@ -134,6 +136,9 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Throw New InvalidOperationException(CodeAnalysisResources.TheStreamCannotBeReadFrom)
End If

' Report NFW to see if this Is being used in the wild.
FatalError.ReportAndPropagate(New SerializationDeprecationException())

Using reader = ObjectReader.TryGetReader(stream, leaveOpen:=True, cancellationToken:=cancellationToken)
If reader Is Nothing Then
Throw New ArgumentException(CodeAnalysisResources.Stream_contains_invalid_data, NameOf(stream))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports Roslyn.Test.Utilities
Imports Xunit

Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests
#Disable Warning BC40000 ' Type or member is obsolete

Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests
Public Class SerializationTests

Private Sub RoundTrip(text As String, Optional expectRecursive As Boolean = True)
Expand Down Expand Up @@ -461,3 +462,5 @@ End Module
End Sub
End Class
End Namespace

#Enable Warning BC40000 ' Type or member is obsolete
4 changes: 4 additions & 0 deletions src/Tools/IdeCoreBenchmarks/SerializationBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;

#pragma warning disable CS0618 // Type or member is obsolete

namespace IdeCoreBenchmarks
{
[MemoryDiagnoser]
Expand Down Expand Up @@ -96,3 +98,5 @@ public void DeserializeSyntaxNode()
}
}
}

#pragma warning restore CS0618 // Type or member is obsolete

0 comments on commit 725ba83

Please sign in to comment.