11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33
4+ #pragma warning disable SA1202 // Elements should be ordered by access - because field initializer depend on each other
5+
6+ using System ;
47using System . Collections . Immutable ;
8+ using System . IO ;
59using System . Net ;
610
711namespace Microsoft . VisualStudio . Threading . Analyzers . Tests ;
812
913internal static class ReferencesHelper
1014{
15+ private static readonly string NuGetConfigPath = FindNuGetConfigPath ( ) ;
16+
1117#if NETFRAMEWORK
1218 public static ReferenceAssemblies DefaultReferences = ReferenceAssemblies . NetFramework . Net471 . Default
1319#elif NET8_0
1420 public static ReferenceAssemblies DefaultReferences = ReferenceAssemblies . Net . Net80
1521#else
1622#error Fix TFM conditions
1723#endif
24+ . WithNuGetConfigFilePath ( NuGetConfigPath )
1825 . WithPackages ( ImmutableArray . Create (
1926 new PackageIdentity ( "System . Collections . Immutable ", "6.0 .0 ") ,
2027 new PackageIdentity ( "System. Threading . Tasks . Extensions ", "4.6 .0 ") ,
@@ -32,4 +39,21 @@ static ReferencesHelper()
3239 ServicePointManager . SecurityProtocol = SecurityProtocolType . Tls12 ;
3340#pragma warning restore RS0030 // Do not used banned APIs
3441 }
42+
43+ private static string FindNuGetConfigPath ( )
44+ {
45+ string ? path = AppContext . BaseDirectory ;
46+ while ( path is not null )
47+ {
48+ string candidate = Path . Combine ( path , "nuget.config" ) ;
49+ if ( File . Exists ( candidate ) )
50+ {
51+ return candidate ;
52+ }
53+
54+ path = Path . GetDirectoryName ( path ) ;
55+ }
56+
57+ throw new InvalidOperationException ( "Could not find NuGet.config by searching up from " + AppContext . BaseDirectory ) ;
58+ }
3559}
0 commit comments