Skip to content

Commit 015a854

Browse files
authored
More improvements to source packages (#78587)
* Editor configs for source packages * Add nullable enable * Include linked files in source packages * Suppress RS1024 * Remove duplicate nullability directives * Remove unnecessary extensions * Move FatalError and FailFast to Contracts package * Add disclaimer
1 parent 6ca368c commit 015a854

File tree

89 files changed

+205
-182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+205
-182
lines changed

eng/targets/Imports.targets

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,35 @@
9797
<EditorConfigFiles Condition="'$(IsShipping)' != 'true'" Include="$(RepositoryEngineeringDir)config\globalconfigs\NonShipping.globalconfig" />
9898
</ItemGroup>
9999

100+
<!--
101+
Common content for all Roslyn source packages.
102+
-->
103+
104+
<PropertyGroup Condition="'$(IsSourcePackage)' == 'true'">
105+
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddEditorConfigToSourcePackage;_AddLinkedCompileItemsToSourcePackage</TargetsForTfmSpecificContentInPackage>
106+
<PackageDescription>
107+
$(PackageDescription)
108+
109+
The source code included in this package is subject to arbitrary changes in future versions.
110+
Updating a reference to this package to a newer version of the package may require changes in the referencing project.
111+
No compatibility guarantees are provided.
112+
</PackageDescription>
113+
</PropertyGroup>
114+
115+
<!-- Include SourcePackage.editorconfig in all source packages. -->
116+
<Target Name="_AddEditorConfigToSourcePackage">
117+
<ItemGroup>
118+
<TfmSpecificPackageFile Include="$(MSBuildThisFileDirectory)..\config\SourcePackage.editorconfig" PackagePath="contentFiles/cs/$(TargetFramework)/.editorconfig" />
119+
</ItemGroup>
120+
</Target>
121+
122+
<!-- Include linked files. Arcade SDK only includes files in the project directory. -->
123+
<Target Name="_AddLinkedCompileItemsToSourcePackage">
124+
<ItemGroup>
125+
<TfmSpecificPackageFile Include="@(Compile)" Condition="'%(Compile.Link)' != ''" PackagePath="contentFiles/cs/$(TargetFramework)/%(Compile.Link)" BuildAction="Compile"/>
126+
</ItemGroup>
127+
</Target>
128+
100129
<!-- Check that all shipping assemblies are packaged -->
101130
<Target Name="_CheckTestProjectTargetFileName" BeforeTargets="Build" Condition="'$(IsShippingAssembly)' == 'true' and '$(IsPackable)' != 'true'">
102131
<Error Text="Project output assembly is shipping (IsShipping == $(IsShipping), IsShippingAssembly == '$(IsShippingAssembly)', IsShippingPackage == '$(IsShippingPackage)') but is not packaged (IsPackable == '$(IsPackable)')" />

src/Compilers/CSharp/Portable/CommandLine/CommandLineDiagnosticFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ internal string RelativizeNormalizedPath(string normalizedPath)
7171
if (PathUtilities.IsSameDirectoryOrChildOf(normalizedDirectory, normalizedBaseDirectory))
7272
{
7373
return normalizedPath.Substring(
74-
PathUtilities.IsDirectorySeparator(normalizedBaseDirectory.Last())
74+
PathUtilities.IsDirectorySeparator(normalizedBaseDirectory[^1])
7575
? normalizedBaseDirectory.Length
7676
: normalizedBaseDirectory.Length + 1);
7777
}

src/Compilers/CSharp/Portable/Syntax/SyntaxNormalizer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,8 @@ private static bool NeedsSeparator(SyntaxToken token, SyntaxToken next)
927927
}
928928
else if (token.Width > 1 && next.Width > 1)
929929
{
930-
var tokenLastChar = token.Text.Last();
931-
var nextFirstChar = next.Text.First();
930+
var tokenLastChar = token.Text[^1];
931+
var nextFirstChar = next.Text[0];
932932
if (tokenLastChar == nextFirstChar && TokenCharacterCanBeDoubled(tokenLastChar))
933933
{
934934
return true;
@@ -1251,7 +1251,7 @@ private static bool EndsInLineBreak(SyntaxTrivia trivia)
12511251
if (trivia.Kind() == SyntaxKind.PreprocessingMessageTrivia || trivia.Kind() == SyntaxKind.DisabledTextTrivia)
12521252
{
12531253
var text = trivia.ToFullString();
1254-
return text.Length > 0 && SyntaxFacts.IsNewLine(text.Last());
1254+
return text.Length > 0 && SyntaxFacts.IsNewLine(text[^1]);
12551255
}
12561256

12571257
if (trivia.HasStructure)

src/Compilers/Core/Portable/InternalUtilities/Hash.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
68
using System.Collections.Generic;
79
using System.Collections.Immutable;

src/Compilers/Core/Portable/InternalUtilities/RoslynString.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System.Diagnostics.CodeAnalysis;
68

79
namespace Roslyn.Utilities

src/Compilers/Core/Portable/InternalUtilities/StringExtensions.cs

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
68
using System.Collections.Generic;
79
using System.Collections.Immutable;
@@ -13,6 +15,8 @@ namespace Roslyn.Utilities
1315
{
1416
internal static class StringExtensions
1517
{
18+
private static readonly Func<char, char> s_toLower = char.ToLower;
19+
private static readonly Func<char, char> s_toUpper = char.ToUpper;
1620
private static ImmutableArray<string> s_lazyNumerals;
1721
private static UTF8Encoding? s_lazyUtf8;
1822

@@ -30,19 +34,7 @@ internal static string GetNumeral(int number)
3034
}
3135

3236
public static string Join(this IEnumerable<string?> source, string separator)
33-
{
34-
if (source == null)
35-
{
36-
throw new ArgumentNullException(nameof(source));
37-
}
38-
39-
if (separator == null)
40-
{
41-
throw new ArgumentNullException(nameof(separator));
42-
}
43-
44-
return string.Join(separator, source);
45-
}
37+
=> string.Join(separator, source);
4638

4739
public static bool LooksLikeInterfaceName(this string name)
4840
{
@@ -54,9 +46,6 @@ public static bool LooksLikeTypeParameterName(this string name)
5446
return name.Length >= 3 && name[0] == 'T' && char.IsUpper(name[1]) && char.IsLower(name[2]);
5547
}
5648

57-
private static readonly Func<char, char> s_toLower = char.ToLower;
58-
private static readonly Func<char, char> s_toUpper = char.ToUpper;
59-
6049
[return: NotNullIfNotNull(parameterName: nameof(shortName))]
6150
public static string? ToPascalCase(
6251
this string? shortName,
@@ -228,32 +217,6 @@ internal static string Unquote(this string arg, out bool quoted)
228217
}
229218
}
230219

231-
// String isn't IEnumerable<char> in the current Portable profile.
232-
internal static char First(this string arg)
233-
{
234-
return arg[0];
235-
}
236-
237-
// String isn't IEnumerable<char> in the current Portable profile.
238-
internal static char Last(this string arg)
239-
{
240-
return arg[arg.Length - 1];
241-
}
242-
243-
// String isn't IEnumerable<char> in the current Portable profile.
244-
internal static bool All(this string arg, Predicate<char> predicate)
245-
{
246-
foreach (char c in arg)
247-
{
248-
if (!predicate(c))
249-
{
250-
return false;
251-
}
252-
}
253-
254-
return true;
255-
}
256-
257220
public static int GetCaseInsensitivePrefixLength(this string string1, string string2)
258221
{
259222
int x = 0;

src/Compilers/Core/Portable/SpecialTypeExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
68
using System.Diagnostics;
7-
using Roslyn.Utilities;
89

910
namespace Microsoft.CodeAnalysis
1011
{

src/Compilers/Core/Portable/InternalUtilities/FailFast.cs renamed to src/Dependencies/Contracts/ErrorReporting/FailFast.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
7+
#if !MICROSOFT_CODEANALYSIS_CONTRACTS_NO_ERROR_REPORTING
8+
59
using System;
610
using System.Diagnostics;
711
using System.Diagnostics.CodeAnalysis;
812
using System.Runtime.CompilerServices;
9-
using Microsoft.CodeAnalysis.ErrorReporting;
10-
using Roslyn.Utilities;
1113

12-
namespace Microsoft.CodeAnalysis
14+
namespace Microsoft.CodeAnalysis.ErrorReporting
1315
{
1416
internal static class FailFast
1517
{
@@ -109,3 +111,4 @@ internal static void Assert([DoesNotReturnIf(false)] bool condition, string? mes
109111
}
110112
}
111113
}
114+
#endif

src/Compilers/Core/Portable/InternalUtilities/FatalError.cs renamed to src/Dependencies/Contracts/ErrorReporting/FatalError.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
7+
#if !MICROSOFT_CODEANALYSIS_CONTRACTS_NO_ERROR_REPORTING
8+
59
using System;
610
using System.Diagnostics;
711
using System.Reflection;
812
using System.Runtime.CompilerServices;
913
using System.Threading;
14+
using System.Threading.Tasks;
1015

1116
namespace Microsoft.CodeAnalysis.ErrorReporting
1217
{
@@ -221,6 +226,25 @@ public static bool ReportAndCatchUnlessCanceled(Exception exception, Cancellatio
221226
return ReportAndCatch(exception, severity);
222227
}
223228

229+
public static Task ReportNonFatalErrorAsync(this Task task)
230+
{
231+
_ = task.ContinueWith(p => ReportAndCatchUnlessCanceled(p.Exception!),
232+
CancellationToken.None,
233+
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,
234+
TaskScheduler.Default);
235+
236+
return task;
237+
}
238+
239+
public static Task ReportNonFatalErrorUnlessCancelledAsync(this Task task, CancellationToken cancellationToken)
240+
{
241+
_ = task.ContinueWith(p => ReportAndCatchUnlessCanceled(p.Exception!, cancellationToken),
242+
CancellationToken.None,
243+
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously,
244+
TaskScheduler.Default);
245+
246+
return task;
247+
}
224248
#endif
225249

226250
// We use a Guid for the marker because it is used as a key in an exceptions Data dictionary, so we must make sure
@@ -312,3 +336,5 @@ internal enum ErrorSeverity
312336
Critical
313337
}
314338
}
339+
340+
#endif

0 commit comments

Comments
 (0)