Skip to content

Commit b486b74

Browse files
authored
Don't bind against non-shipping contract assemblies (#78730)
Manual backport of c8503d3 Fixes #77988 Unblocks #78532 Introduce the AnnotateTargetPathWithContract switch to make it configure-able when a source project should return the reference project's assembly instead of the source assembly, when other projects compile against it. Set it so that reference assemblies are only returned for NetCoreAppCurrent tfms or when the project isn't packable. - Fix System.DirectoryServices.AccountManagement build System.DirectoryServices.AccountManagement now builds against src/System.DirectoryServices instead of ref/System.DirectoryServices (because the package doesn't contain the ref assembly). Because of that, the compiler now gets confused because of the System.DirectoryServices.Interop namespace and the global Interop class. This happens even though the DirectoryServices.Interop namespace doesn't include any public types. That results in the following errors: src\libraries\System.DirectoryServices.AccountManagement\src\System\DirectoryServices\AccountManagement\AD\SidList.cs(50,26): error CS0246: The type or namespace name 'SID_AND_ATTRIBUTES' could not be found (are you missing a using directive or an assembly reference?) src\libraries\System.DirectoryServices.AccountManagement\src\System\DirectoryServices\AccountManagement\interopt.cs(439,20): error CS0246: The type or namespace name 'UNICODE_INTPTR_STRING' could not be found (are you missing a using directive or an assembly reference?) This commit fixes that by removing the System.DirectoryServices.Interop namespace and moving the types into the parent namespace. - Suppress nullable warnings in Serialization.Schema Now that Schema compiles against the source assembly of System.CodeDom, it receives nullability errors. I'm suppressing them manually for now but am filing an issue to correctly fix those. Related: #78036
1 parent 5223f01 commit b486b74

26 files changed

+34
-34
lines changed

eng/resolveContract.targets

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@
2020
<HasMatchingContract Condition="'$(HasMatchingContract)' == '' and Exists('$(ContractProject)')">true</HasMatchingContract>
2121
<!-- Disable API compat if the project doesn't have reference assembly -->
2222
<RunApiCompat Condition="'$(HasMatchingContract)' != 'true'">false</RunApiCompat>
23+
<!-- Don't build against reference assemblies when projects are packable and the tfm is not the latest .NETCoreApp as
24+
such reference assemblies don't ship to customers and only exist for tooling scenarios. -->
25+
<AnnotateTargetPathWithContract Condition="'$(AnnotateTargetPathWithContract)' == '' and
26+
'$(HasMatchingContract)' == 'true' and
27+
(
28+
'$(IsPackable)' != 'true' or
29+
(
30+
'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and
31+
'$(TargetFrameworkVersion)' == 'v$(NetCoreAppCurrentVersion)'
32+
)
33+
)">true</AnnotateTargetPathWithContract>
2334
</PropertyGroup>
2435

2536
<ItemGroup Condition="'$(IsSourceProject)' == 'true' or '$(IsReferenceAssemblyProject)' == 'true' or '$(IsPartialFacadeAssembly)' == 'true'">
@@ -36,7 +47,7 @@
3647

3748
<!-- Allow P2Ps that target a source project to build against the corresponding ref project. -->
3849
<Target Name="AnnotateTargetPathWithTargetPlatformMonikerWithReferenceAssembly"
39-
Condition="'$(HasMatchingContract)' == 'true'"
50+
Condition="'$(AnnotateTargetPathWithContract)' == 'true'"
4051
DependsOnTargets="ResolveProjectReferences"
4152
AfterTargets="GetTargetPathWithTargetPlatformMoniker">
4253
<ItemGroup>

src/libraries/System.DirectoryServices/src/ILLink/ILLink.Suppressions.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<argument>ILLink</argument>
66
<argument>IL2050</argument>
77
<property name="Scope">member</property>
8-
<property name="Target">M:System.DirectoryServices.Interop.UnsafeNativeMethods.ADsOpenObject(System.String,System.String,System.String,System.Int32,System.Guid@,System.Object@)</property>
8+
<property name="Target">M:System.DirectoryServices.UnsafeNativeMethods.ADsOpenObject(System.String,System.String,System.String,System.Int32,System.Guid@,System.Object@)</property>
99
</attribute>
1010
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
1111
<argument>ILLink</argument>

src/libraries/System.DirectoryServices/src/Interop/AdsOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace System.DirectoryServices.Interop
4+
namespace System.DirectoryServices
55
{
66
internal enum AdsOptions
77
{

src/libraries/System.DirectoryServices/src/Interop/AdsPropertyOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace System.DirectoryServices.Interop
4+
namespace System.DirectoryServices
55
{
66
internal enum AdsPropertyOperation
77
{

src/libraries/System.DirectoryServices/src/Interop/AdsSearchColumn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Runtime.InteropServices;
55

6-
namespace System.DirectoryServices.Interop
6+
namespace System.DirectoryServices
77
{
88
[StructLayout(LayoutKind.Sequential)]
99
internal unsafe struct AdsSearchColumn

src/libraries/System.DirectoryServices/src/Interop/AdsSearchPreferenceInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Runtime.InteropServices;
55

6-
namespace System.DirectoryServices.Interop
6+
namespace System.DirectoryServices
77
{
88
[StructLayout(LayoutKind.Sequential)]
99
internal struct AdsSearchPreferenceInfo

src/libraries/System.DirectoryServices/src/Interop/AdsSearchPreferences.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace System.DirectoryServices.Interop
4+
namespace System.DirectoryServices
55
{
66
internal enum AdsSearchPreferences
77
{

src/libraries/System.DirectoryServices/src/Interop/AdsSortKey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Runtime.InteropServices;
55

6-
namespace System.DirectoryServices.Interop
6+
namespace System.DirectoryServices
77
{
88
[StructLayout(LayoutKind.Sequential)]
99
internal struct AdsSortKey

src/libraries/System.DirectoryServices/src/Interop/AdsType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace System.DirectoryServices.Interop
4+
namespace System.DirectoryServices
55
{
66
internal enum AdsType
77
{

src/libraries/System.DirectoryServices/src/Interop/AdsValue2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Runtime.InteropServices;
55

6-
namespace System.DirectoryServices.Interop
6+
namespace System.DirectoryServices
77
{
88
[StructLayout(LayoutKind.Sequential)]
99
internal struct Ads_Pointer

src/libraries/System.DirectoryServices/src/Interop/AdsValueHelper2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Runtime.InteropServices;
66
using System.Globalization;
77

8-
namespace System.DirectoryServices.Interop
8+
namespace System.DirectoryServices
99
{
1010
[StructLayout(LayoutKind.Sequential)]
1111
internal struct SystemTime

src/libraries/System.DirectoryServices/src/Interop/NativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace System.DirectoryServices.Interop
4+
namespace System.DirectoryServices
55
{
66
internal static class NativeMethods
77
{

src/libraries/System.DirectoryServices/src/Interop/SafeNativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Security;
66
using System.Runtime.InteropServices;
77

8-
namespace System.DirectoryServices.Interop
8+
namespace System.DirectoryServices
99
{
1010
internal static partial class SafeNativeMethods
1111
{

src/libraries/System.DirectoryServices/src/Interop/UnsafeNativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Runtime.InteropServices;
66
using System.Security;
77

8-
namespace System.DirectoryServices.Interop
8+
namespace System.DirectoryServices
99
{
1010

1111
[StructLayout(LayoutKind.Explicit)]

src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/DomainController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ public void SeizeRoleOwnership(ActiveDirectoryRole role)
410410
// Increment the RIDAvailablePool by 30k.
411411
if (role == ActiveDirectoryRole.RidRole)
412412
{
413-
System.DirectoryServices.Interop.UnsafeNativeMethods.IADsLargeInteger ridPool = (System.DirectoryServices.Interop.UnsafeNativeMethods.IADsLargeInteger)roleObjectEntry.Properties[PropertyManager.RIDAvailablePool].Value!;
413+
System.DirectoryServices.UnsafeNativeMethods.IADsLargeInteger ridPool = (System.DirectoryServices.UnsafeNativeMethods.IADsLargeInteger)roleObjectEntry.Properties[PropertyManager.RIDAvailablePool].Value!;
414414

415415
// check the overflow of the low part
416416
if (ridPool.LowPart + UpdateRidPoolSeizureValue < ridPool.LowPart)

src/libraries/System.DirectoryServices/src/System/DirectoryServices/AuthenticationTypes.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.DirectoryServices.Interop;
5-
64
namespace System.DirectoryServices
75
{
86
/// <devdoc>

src/libraries/System.DirectoryServices/src/System/DirectoryServices/DirectoryEntries.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Runtime.InteropServices;
55
using System.Collections;
6-
using System.DirectoryServices.Interop;
76

87
namespace System.DirectoryServices
98
{

src/libraries/System.DirectoryServices/src/System/DirectoryServices/DirectoryEntry.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Runtime.InteropServices;
55
using System.Diagnostics;
6-
using System.DirectoryServices.Interop;
76
using System.ComponentModel;
87
using System.Threading;
98
using System.Reflection;

src/libraries/System.DirectoryServices/src/System/DirectoryServices/DirectoryEntryConfiguration.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.DirectoryServices.Interop;
54
using System.ComponentModel;
65

76
namespace System.DirectoryServices

src/libraries/System.DirectoryServices/src/System/DirectoryServices/DirectorySearcher.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Runtime.InteropServices;
55
using System.Collections;
66
using System.Collections.Specialized;
7-
using System.DirectoryServices.Interop;
87
using System.ComponentModel;
98

109
using INTPTR_INTPTRCAST = System.IntPtr;

src/libraries/System.DirectoryServices/src/System/DirectoryServices/DirectoryServicesCOMException.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.ComponentModel;
55
using System.Runtime.InteropServices;
66
using System.Runtime.Serialization;
7-
using System.DirectoryServices.Interop;
87

98
namespace System.DirectoryServices
109
{

src/libraries/System.DirectoryServices/src/System/DirectoryServices/PropertyCollection.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System.Runtime.InteropServices;
55
using System.Collections;
6-
using System.DirectoryServices.Interop;
76
using System.Globalization;
87

98
namespace System.DirectoryServices

src/libraries/System.DirectoryServices/src/System/DirectoryServices/PropertyValueCollection.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections;
5-
using System.DirectoryServices.Interop;
65

76
namespace System.DirectoryServices
87
{

src/libraries/System.DirectoryServices/src/System/DirectoryServices/SchemaNameCollection.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Collections;
5-
using System.DirectoryServices.Interop;
65

76
namespace System.DirectoryServices
87
{

src/libraries/System.DirectoryServices/src/System/DirectoryServices/SearchResultCollection.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Net;
55
using System.Runtime.InteropServices;
66
using System.Collections;
7-
using System.DirectoryServices.Interop;
87
using System.Text;
98

109
using INTPTR_INTPTRCAST = System.IntPtr;

src/libraries/System.Runtime.Serialization.Schema/src/System/Runtime/Serialization/Schema/CodeExporter.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ private static CodeTypeDeclaration CreateTypeDeclaration(string typeName, DataCo
520520
CodeAttributeDeclaration generatedCodeAttribute = new CodeAttributeDeclaration(typeof(GeneratedCodeAttribute).FullName!);
521521

522522
AssemblyName assemblyName = Assembly.GetExecutingAssembly().GetName();
523-
generatedCodeAttribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(assemblyName.Name)));
524-
generatedCodeAttribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(assemblyName.Version?.ToString())));
523+
generatedCodeAttribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(assemblyName.Name!)));
524+
generatedCodeAttribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(assemblyName.Version?.ToString()!)));
525525

526526
// System.Diagnostics.DebuggerStepThroughAttribute not allowed on enums
527527
// ensure that the attribute is only generated on types that are not enums
@@ -820,7 +820,7 @@ private void ExportClassDataContract(DataContract classDataContract, ContractCod
820820
{
821821
ContractCodeDomInfo baseContractCodeDomInfo = GetContractCodeDomInfo(classDataContract.BaseContract);
822822
Debug.Assert(baseContractCodeDomInfo.IsProcessed, "Cannot generate code for type if code for base type has not been generated");
823-
type.BaseTypes.Add(baseContractCodeDomInfo.TypeReference);
823+
type.BaseTypes.Add(baseContractCodeDomInfo.TypeReference!);
824824
AddBaseMemberNames(baseContractCodeDomInfo, contractCodeDomInfo);
825825
if (baseContractCodeDomInfo.ReferencedTypeExists)
826826
{
@@ -1153,7 +1153,7 @@ private void ExportISerializableDataContract(DataContract classDataContract, Con
11531153
{
11541154
ContractCodeDomInfo baseContractCodeDomInfo = GetContractCodeDomInfo(classDataContract.BaseContract);
11551155
GenerateType(classDataContract.BaseContract, baseContractCodeDomInfo);
1156-
type.BaseTypes.Add(baseContractCodeDomInfo.TypeReference);
1156+
type.BaseTypes.Add(baseContractCodeDomInfo.TypeReference!);
11571157
if (baseContractCodeDomInfo.ReferencedTypeExists)
11581158
{
11591159
Type? actualType = (Type?)baseContractCodeDomInfo.TypeReference?.UserData[s_codeUserDataActualTypeKey];
@@ -1238,7 +1238,7 @@ private void ExportCollectionDataContract(DataContract collectionContract, Contr
12381238
Debug.Assert(contractCodeDomInfo.TypeDeclaration != null);
12391239

12401240
CodeTypeDeclaration generatedType = contractCodeDomInfo.TypeDeclaration;
1241-
generatedType.BaseTypes.Add(baseTypeReference);
1241+
generatedType.BaseTypes.Add(baseTypeReference!);
12421242
CodeAttributeDeclaration collectionContractAttribute = new CodeAttributeDeclaration(GetClrTypeFullName(typeof(CollectionDataContractAttribute)));
12431243
collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(ImportGlobals.NameProperty, new CodePrimitiveExpression(dataContractName)));
12441244
collectionContractAttribute.Arguments.Add(new CodeAttributeArgument(ImportGlobals.NamespaceProperty, new CodePrimitiveExpression(collectionContract.XmlName.Namespace)));
@@ -1673,7 +1673,7 @@ private static CodeThisReferenceExpression ThisReference
16731673

16741674
private static CodePrimitiveExpression NullReference
16751675
{
1676-
get { return new CodePrimitiveExpression(null); }
1676+
get { return new CodePrimitiveExpression(null!); }
16771677
}
16781678

16791679
private CodeParameterDeclarationExpression SerializationInfoParameter
@@ -1784,12 +1784,12 @@ private CodeMemberMethod GetSchemaStaticMethod
17841784
new CodeTypeReferenceExpression(GetCodeTypeReference(typeof(XmlSerializableServices))),
17851785
nameof(XmlSerializableServices.AddDefaultSchema),
17861786
new CodeArgumentReferenceExpression(paramDeclaration.Name),
1787-
new CodeFieldReferenceExpression(null, TypeNameFieldName)
1787+
new CodeFieldReferenceExpression(null!, TypeNameFieldName)
17881788
)
17891789
);
17901790
getSchemaStaticMethod.Statements.Add(
17911791
new CodeMethodReturnStatement(
1792-
new CodeFieldReferenceExpression(null, TypeNameFieldName)
1792+
new CodeFieldReferenceExpression(null!, TypeNameFieldName)
17931793
)
17941794
);
17951795
return getSchemaStaticMethod;

0 commit comments

Comments
 (0)