From ffb9f66c465459769d5cfb4bf1fb86023e45c2ed Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Tue, 28 Jul 2020 19:42:38 -0700 Subject: [PATCH 01/12] Add an analyzer for PublishSingleFile When publishing single-file applications, certain APIs are very likely to return incorrect results. Most notably, asking for the Location of any Assembly loaded from the single-file will return an empty string, instead of a file path. To minimize suprise, this analyzer flags all usages of the APIs most likely to break when used in single-file when the PublishSingleFile property is set to true, and the IncludeAllContentForSelfExtractProperty is not true (which provides the legacy API behavior). --- .../Core/AnalyzerReleases.Unshipped.md | 1 + .../MicrosoftNetCoreAnalyzersResources.resx | 63 ++++--- .../AvoidAssemblyLocationInSingleFile.cs | 149 +++++++++++++++ ...otCallDangerousMethodsInDeserialization.cs | 2 +- .../MicrosoftNetCoreAnalyzersResources.cs.xlf | 15 ++ .../MicrosoftNetCoreAnalyzersResources.de.xlf | 15 ++ .../MicrosoftNetCoreAnalyzersResources.es.xlf | 15 ++ .../MicrosoftNetCoreAnalyzersResources.fr.xlf | 15 ++ .../MicrosoftNetCoreAnalyzersResources.it.xlf | 15 ++ .../MicrosoftNetCoreAnalyzersResources.ja.xlf | 15 ++ .../MicrosoftNetCoreAnalyzersResources.ko.xlf | 15 ++ .../MicrosoftNetCoreAnalyzersResources.pl.xlf | 15 ++ ...crosoftNetCoreAnalyzersResources.pt-BR.xlf | 15 ++ .../MicrosoftNetCoreAnalyzersResources.ru.xlf | 15 ++ .../MicrosoftNetCoreAnalyzersResources.tr.xlf | 15 ++ ...osoftNetCoreAnalyzersResources.zh-Hans.xlf | 15 ++ ...osoftNetCoreAnalyzersResources.zh-Hant.xlf | 15 ++ .../AvoidAssemblyLocationInSingleFileTests.cs | 169 ++++++++++++++++++ src/Utilities/Compiler/DiagnosticCategory.cs | 1 + .../Options/MSBuildPropertyOptionNames.cs | 2 + src/Utilities/Compiler/WellKnownTypeNames.cs | 3 +- .../Analysis/TaintedDataAnalysis/DllSinks.cs | 2 +- 22 files changed, 557 insertions(+), 30 deletions(-) create mode 100644 src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs create mode 100644 src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs diff --git a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md index f9b4bbcfa3..a15033321b 100644 --- a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md +++ b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md @@ -38,6 +38,7 @@ CA2353 | Security | Disabled | DataSetDataTableInSerializableTypeAnalyzer, [Docu CA2354 | Security | Disabled | DataSetDataTableInIFormatterSerializableObjectGraphAnalyzer, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca2354) CA2355 | Security | Disabled | DataSetDataTableInSerializableObjectGraphAnalyzer, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca2355) CA2356 | Security | Disabled | DataSetDataTableInWebSerializableObjectGraphAnalyzer, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca2356) +CA3000 | Publish | Warning | DoNotUseAssemblyLocationInSingleFile, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca3000) ### Changed Rules Rule ID | New Category | New Severity | Old Category | Old Severity | Notes diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx index 83435a1116..d7afab36ab 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx @@ -1,17 +1,17 @@  - @@ -1422,4 +1422,13 @@ Do not use 'OutAttribute' on string parameters for P/Invokes + + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + Avoid `{0}` when publishing as a single-file. + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + \ No newline at end of file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs new file mode 100644 index 0000000000..bd9b04724b --- /dev/null +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs @@ -0,0 +1,149 @@ + +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +#nullable enable + +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using Analyzer.Utilities; +using Analyzer.Utilities.Extensions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.Operations; + +namespace Microsoft.NetCore.Analyzers.Publish +{ + /// + /// CA3000: Do not use Assembly.Location in single-file publish + /// + [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] + public sealed class AvoidAssemblyLocationInSingleFile : DiagnosticAnalyzer + { + internal const string RuleId = "CA3000"; + + private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString( + nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileTitle), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)); + private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString( + nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileMessage), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)); + private static readonly LocalizableString s_localizableDescription = new LocalizableResourceString( + nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileDescription), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)); + + internal static DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(RuleId, + s_localizableTitle, + s_localizableMessage, + DiagnosticCategory.Publish, + RuleLevel.BuildWarning, + s_localizableDescription, + isPortedFxCopRule: false, + isDataflowRule: false); + + public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); + + public override void Initialize(AnalysisContext context) + { + context.EnableConcurrentExecution(); + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.ReportDiagnostics); + + context.RegisterCompilationStartAction(context => + { + var compilation = context.Compilation; + var isSingleFilePublish = context.Options.GetMSBuildPropertyValue( + MSBuildPropertyOptionNames.PublishSingleFile, compilation, context.CancellationToken); + if (!string.Equals(isSingleFilePublish?.Trim(), "true", StringComparison.OrdinalIgnoreCase)) + { + return; + } + var includesAllContent = context.Options.GetMSBuildPropertyValue( + MSBuildPropertyOptionNames.IncludeAllContentForSelfExtract, compilation, context.CancellationToken); + if (string.Equals(includesAllContent?.Trim(), "true", StringComparison.OrdinalIgnoreCase)) + { + return; + } + + HashSet properties = new HashSet(SymbolEqualityComparer.Default); + HashSet methods = new HashSet(SymbolEqualityComparer.Default); + + var assemblyType = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssembly); + if (assemblyType is not null) + { + // properties + addIfNotNull(properties, tryGetSingleSymbol(assemblyType.GetMembers("Location"))); + addIfNotNull(properties, tryGetSingleSymbol(assemblyType.GetMembers("CodeBase"))); + addIfNotNull(properties, tryGetSingleSymbol(assemblyType.GetMembers("EscapedCodeBase"))); + + // methods + methods.UnionWith(assemblyType.GetMembers("GetFile").OfType()); + methods.UnionWith(assemblyType.GetMembers("GetFiles").OfType()); + } + + var assemblyNameType = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssemblyName); + if (assemblyNameType is not null) + { + addIfNotNull(properties, tryGetSingleSymbol(assemblyNameType.GetMembers("CodeBase"))); + addIfNotNull(properties, tryGetSingleSymbol(assemblyNameType.GetMembers("EscapedCodeBase"))); + } + + context.RegisterOperationAction(operationContext => + { + var access = (IPropertyReferenceOperation)operationContext.Operation; + var property = access.Property; + if (!properties.Contains(property)) + { + return; + } + + operationContext.ReportDiagnostic(Diagnostic.Create( + Rule, + access.Syntax.GetLocation(), + property)); + }, OperationKind.PropertyReference); + + context.RegisterOperationAction(operationContext => + { + var invocation = (IInvocationOperation)operationContext.Operation; + var targetMethod = invocation.TargetMethod; + if (!methods.Contains(targetMethod)) + { + return; + } + + operationContext.ReportDiagnostic(Diagnostic.Create( + Rule, + invocation.Syntax.GetLocation(), + targetMethod)); + }, OperationKind.Invocation); + + return; + + static TSymbol? tryGetSingleSymbol(ImmutableArray members) where TSymbol : class, ISymbol + { + TSymbol? candidate = null; + foreach (var m in members) + { + if (m is TSymbol tsym) + { + if (candidate is null) + { + candidate = tsym; + } + else + { + return null; + } + } + } + return candidate; + } + + static void addIfNotNull(HashSet properties, TSymbol? p) where TSymbol : class, ISymbol + { + if (p is not null) + { + properties.Add(p); + } + } + }); + } + } +} diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Security/DoNotCallDangerousMethodsInDeserialization.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Security/DoNotCallDangerousMethodsInDeserialization.cs index c7ee83d921..f373d42a05 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Security/DoNotCallDangerousMethodsInDeserialization.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Security/DoNotCallDangerousMethodsInDeserialization.cs @@ -38,7 +38,7 @@ public sealed class DoNotCallDangerousMethodsInDeserialization : DiagnosticAnaly (WellKnownTypeNames.SystemIOFileInfo, new[] { "Delete" }), (WellKnownTypeNames.SystemIODirectoryInfo, new[] { "Delete" }), (WellKnownTypeNames.SystemIOLogLogStore, new[] { "Delete" }), - (WellKnownTypeNames.SystemReflectionAssemblyFullName, new[] { "GetLoadedModules", "Load", "LoadFile", "LoadFrom", "LoadModule", "LoadWithPartialName", "ReflectionOnlyLoad", "ReflectionOnlyLoadFrom", "UnsafeLoadFrom" }) + (WellKnownTypeNames.SystemReflectionAssembly, new[] { "GetLoadedModules", "Load", "LoadFile", "LoadFrom", "LoadModule", "LoadWithPartialName", "ReflectionOnlyLoad", "ReflectionOnlyLoadFrom", "UnsafeLoadFrom" }) ); internal static DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create( diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf index 25ceee5c3a..54a7230567 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf @@ -47,6 +47,21 @@ Literály řetězců atributů by se měly správně parsovat + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. Knihovna tříd .NET Framework poskytuje metody pro načítání vlastních atributů. Ve výchozím nastavení tyto metody prohledávají hierarchii dědičnosti atributů. Zapečetění atributu eliminuje prohledávání hierarchie dědičnosti a může zvýšit výkon. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf index 6d50a84977..c06a16eb37 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf @@ -47,6 +47,21 @@ Attributzeichenfolgenliterale müssen richtig analysiert werden + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. Die .NET Framework-Klassenbibliothek stellt Methoden zum Abrufen benutzerdefinierter Attribute bereit. Standardmäßig durchsuchen diese Methoden die Attributvererbungshierarchie. Durch das Versiegeln des Attributs entfällt das Durchsuchen der Vererbungshierarchie, und die Leistung kann gesteigert werden. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf index 57c489de5b..9bbbf7f5c3 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf @@ -47,6 +47,21 @@ Los literales de cadena de atributo se deben analizar correctamente + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. La biblioteca de clases de .NET Framework proporciona los métodos para recuperar los atributos personalizados. De forma predeterminada, estos métodos buscan la jerarquía de herencia del atributo. Al sellar el atributo, se elimina la búsqueda a través de la jerarquía de herencia y puede mejorarse el rendimiento. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf index 01ff18a2be..41e0ab283b 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf @@ -47,6 +47,21 @@ Les littéraux de chaîne d'attribut doivent être analysés correctement + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. La bibliothèque de classes .NET Framework fournit des méthodes pour récupérer les attributs personnalisés. Par défaut, ces méthodes effectuent des recherches dans la hiérarchie d'héritage des attributs. Le scellement de l'attribut permet d'éviter les recherches dans la hiérarchie d'héritage et contribue à l'amélioration des performances. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf index 2a7b5aab6d..0742ed1481 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf @@ -47,6 +47,21 @@ I valori letterali stringa dell'attributo devono essere analizzati correttamente + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. La libreria di classi .NET Framework fornisce i metodi per recuperare gli attributi personalizzati. Per impostazione predefinita, questi metodi eseguono ricerche nella gerarchia di ereditarietà degli attributi. L'uso di attributi sealed consente di evitare ricerche nella gerarchia di ereditarietà e può migliorare le prestazioni. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf index 935d350a6c..4da8e75cfe 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf @@ -47,6 +47,21 @@ 属性文字列リテラルは、正しく解析する必要があります + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. .NET Framework クラス ライブラリには、カスタム属性を取得するためのメソッドが用意されています。既定では、これらのメソッドは属性継承階層を検索します。属性をシールすると、継承階層全体を検索しなくなるため、パフォーマンスが向上します。 diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf index 50cb7bb804..2c0d3bdc61 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf @@ -47,6 +47,21 @@ 특성 문자열 리터럴이 올바르게 구문 분석되어야 합니다. + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. .NET Framework 클래스 라이브러리에서 사용자 지정 특성을 검색하기 위한 메서드를 제공합니다. 기본적으로 이 메서드는 특성 상속 계층 구조를 검색합니다. 특성을 봉인하면 상속 계층 구조를 통한 검색을 중단하여 성능을 향상시킬 수 있습니다. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf index e77cd9a750..488e20ab35 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf @@ -47,6 +47,21 @@ Analiza literałów ciągu atrybutu powinna kończyć się powodzeniem + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. Biblioteka klas programu .NET Framework udostępnia metody umożliwiające pobieranie atrybutów niestandardowych. Domyślnie te metody przeszukują hierarchię dziedziczenia atrybutów. Zapieczętowanie atrybutu eliminuje potrzebę przeszukiwania hierarchii dziedziczenia i może podwyższyć wydajność. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf index 7618418e3e..7e451c3d23 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf @@ -47,6 +47,21 @@ Literais de cadeias de caracteres de atributos devem ser analisados corretamente + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. A biblioteca de classes .NET Framework oferece métodos para recuperar atributos personalizados. Por padrão, esses métodos pesquisam a hierarquia de herança de atributos. Selar o atributo elimina a pesquisa por meio da hierarquia de herança e pode melhorar o desempenho. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf index 4ef578c9e3..db2c0a15f1 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf @@ -47,6 +47,21 @@ Синтаксический анализ строковых литералов атрибута должен осуществляться правильно + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. Эта библиотека классов платформы .NET Framework предоставляет методы для извлечения настраиваемых атрибутов. По умолчанию эти методы выполняют поиск по иерархии наследования атрибутов. Запечатывание атрибута устраняет поиск по иерархии наследования и позволяет повысить производительность. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf index c85927f693..35ed142ce7 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf @@ -47,6 +47,21 @@ Öznitelik dizesinin sabit değerleri doğru ayrıştırılmalıdır + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. .NET Framework sınıf kitaplığı, özel öznitelikleri almaya yönelik yöntemler sağlar. Varsayılan olarak bu yöntemler öznitelik devralma hiyerarşisinde arama yapar. Özniteliğin mühürlenmesi, devralma hiyerarşisinde arama yapılmasını engeller ve performansı artırabilir. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf index 4ffb59239d..170c0c3dee 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf @@ -47,6 +47,21 @@ 特性字符串文本应正确分析 + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. .NET Framework 类库提供用于检索自定义特性的方法。默认情况下,这些方法搜索特性继承层次结构。通过密封特性,将无需搜索继承层次结构并可提高性能。 diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf index a253387172..8b84e734ce 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf @@ -47,6 +47,21 @@ 屬性字串常值應正確剖析 + + Accessing Assembly location or file path is not valid when publishing as a single-file. + Accessing Assembly location or file path is not valid when publishing as a single-file. + + + + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + + + + Avoid `{0}` when publishing as a single-file. + Avoid `{0}` when publishing as a single-file. + + The .NET Framework class library provides methods for retrieving custom attributes. By default, these methods search the attribute inheritance hierarchy. Sealing the attribute eliminates the search through the inheritance hierarchy and can improve performance. .NET Framework 類別庫可提供擷取自訂屬性的方法。根據預設,這些方法會搜尋屬性繼承階層。使用密封屬性可免於搜尋整個繼承階層,因而能提升效能。 diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs new file mode 100644 index 0000000000..0206a13c68 --- /dev/null +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs @@ -0,0 +1,169 @@ +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Testing; +using Xunit; +using VerifyCS = Test.Utilities.CSharpCodeFixVerifier< + Microsoft.NetCore.Analyzers.Publish.AvoidAssemblyLocationInSingleFile, + Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; +using static Analyzer.Utilities.MSBuildPropertyOptionNames; +using System.Collections.Generic; + +namespace Microsoft.NetCore.Analyzers.Publish.UnitTests +{ + public class AvoidAssemblyLocationInSingleFileTests + { + [Theory] + [CombinatorialData] + public Task GetExecutingAssemblyLocation( + [CombinatorialValues(true, false, null)] bool? publish, + [CombinatorialValues(true, false, null)] bool? includeContent) + { + const string source = @" +using System.Reflection; +class C +{ + public string M() => Assembly.GetExecutingAssembly().Location; +}"; + string analyzerConfig = ""; + if (publish is not null) + { + analyzerConfig += $"build_property.{PublishSingleFile} = {publish}" + Environment.NewLine; + } + if (includeContent is not null) + { + analyzerConfig += $"build_property.{IncludeAllContentForSelfExtract} = {includeContent}"; + } + + var test = new VerifyCS.Test + { + TestCode = source, + AnalyzerConfigDocument = analyzerConfig + }; + + DiagnosticResult[] diagnostics; + if (publish is true && includeContent is not true) + { + diagnostics = new[] { +// /0/Test0.cs(5,26): warning CA3000: Avoid `System.Reflection.Assembly.Location` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. +VerifyCS.Diagnostic().WithSpan(5, 26, 5, 66).WithArguments("System.Reflection.Assembly.Location") + }; + } + else + { + diagnostics = Array.Empty(); + } + + test.ExpectedDiagnostics.AddRange(diagnostics); + return test.RunAsync(); + } + + [Fact] + public Task AssemblyProperties() + { + var src = @" +using System.Reflection; +class C +{ + public void M() + { + var a = Assembly.GetExecutingAssembly(); + _ = a.Location; + _ = a.CodeBase; + _ = a.EscapedCodeBase; + } +}"; + return VerifyDiagnosticsAsync(src, + // /0/Test0.cs(8,13): warning CA3000: Avoid `System.Reflection.Assembly.Location` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + VerifyCS.Diagnostic().WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location"), + // /0/Test0.cs(9,13): warning CA3000: Avoid `System.Reflection.Assembly.CodeBase` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + VerifyCS.Diagnostic().WithSpan(9, 13, 9, 23).WithArguments("System.Reflection.Assembly.CodeBase"), + // /0/Test0.cs(10,13): warning CA3000: Avoid `System.Reflection.Assembly.EscapedCodeBase` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + VerifyCS.Diagnostic().WithSpan(10, 13, 10, 30).WithArguments("System.Reflection.Assembly.EscapedCodeBase") + ); + } + + [Fact] + public Task AssemblyMethods() + { + var src = @" +using System.Reflection; +class C +{ + public void M() + { + var a = Assembly.GetExecutingAssembly(); + _ = a.GetFile(""/some/file/path""); + _ = a.GetFiles(); + } +}"; + return VerifyDiagnosticsAsync(src, + // /0/Test0.cs(8,13): warning CA3000: Avoid `System.Reflection.Assembly.GetFile(string)` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + VerifyCS.Diagnostic().WithSpan(8, 13, 8, 41).WithArguments("System.Reflection.Assembly.GetFile(string)"), + // /0/Test0.cs(9,13): warning CA3000: Avoid `System.Reflection.Assembly.GetFiles()` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + VerifyCS.Diagnostic().WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()") + ); + } + + [Fact] + public Task AssemblyNameAttributes() + { + var src = @" +using System.Reflection; +class C +{ + public void M() + { + var a = Assembly.GetExecutingAssembly().GetName(); + _ = a.CodeBase; + _ = a.EscapedCodeBase; + } +}"; + return VerifyDiagnosticsAsync(src, + // /0/Test0.cs(8,13): warning CA3000: Avoid `System.Reflection.AssemblyName.CodeBase` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + VerifyCS.Diagnostic().WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.AssemblyName.CodeBase"), + // /0/Test0.cs(9,13): warning CA3000: Avoid `System.Reflection.AssemblyName.EscapedCodeBase` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + VerifyCS.Diagnostic().WithSpan(9, 13, 9, 30).WithArguments("System.Reflection.AssemblyName.EscapedCodeBase") + ); + } + + [Fact] + public Task FalsePositive() + { + // This is an OK use of Location and GetFile since these assemblies were loaded from + // a file, but the analyzer is conservative + var src = @" +using System.Reflection; +class C +{ + public void M() + { + var a = Assembly.LoadFrom(""/some/path/not/in/bundle""); + _ = a.Location; + _ = a.GetFiles(); + } +}"; + return VerifyDiagnosticsAsync(src, + // /0/Test0.cs(8,13): warning CA3000: Avoid `System.Reflection.Assembly.Location` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + VerifyCS.Diagnostic().WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location"), + // /0/Test0.cs(9,13): warning CA3000: Avoid `System.Reflection.Assembly.GetFiles()` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + VerifyCS.Diagnostic().WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()")); + } + + private Task VerifyDiagnosticsAsync(string source, params DiagnosticResult[] expected) + { + const string singleFilePublishConfig = @" +build_property." + PublishSingleFile + " = true"; + + var test = new VerifyCS.Test + { + TestCode = source, + AnalyzerConfigDocument = singleFilePublishConfig + }; + + test.ExpectedDiagnostics.AddRange(expected); + return test.RunAsync(); + } + } +} \ No newline at end of file diff --git a/src/Utilities/Compiler/DiagnosticCategory.cs b/src/Utilities/Compiler/DiagnosticCategory.cs index 3b32ebccb3..69def675b9 100644 --- a/src/Utilities/Compiler/DiagnosticCategory.cs +++ b/src/Utilities/Compiler/DiagnosticCategory.cs @@ -16,6 +16,7 @@ internal static class DiagnosticCategory public const string Library = nameof(Library); public const string Documentation = nameof(Documentation); public const string Maintainability = nameof(Maintainability); + public const string Publish = nameof(Publish); public const string RoslynDiagnosticsDesign = nameof(RoslynDiagnosticsDesign); public const string RoslynDiagnosticsMaintainability = nameof(RoslynDiagnosticsMaintainability); diff --git a/src/Utilities/Compiler/Options/MSBuildPropertyOptionNames.cs b/src/Utilities/Compiler/Options/MSBuildPropertyOptionNames.cs index bb50ea6607..45a53d4f55 100644 --- a/src/Utilities/Compiler/Options/MSBuildPropertyOptionNames.cs +++ b/src/Utilities/Compiler/Options/MSBuildPropertyOptionNames.cs @@ -11,5 +11,7 @@ internal static class MSBuildPropertyOptionNames public const string TargetPlatformMinVersion = nameof(TargetPlatformMinVersion); public const string UsingMicrosoftNETSdkWeb = nameof(UsingMicrosoftNETSdkWeb); public const string ProjectTypeGuids = nameof(ProjectTypeGuids); + public const string PublishSingleFile = nameof(PublishSingleFile); + public const string IncludeAllContentForSelfExtract = nameof(IncludeAllContentForSelfExtract); } } diff --git a/src/Utilities/Compiler/WellKnownTypeNames.cs b/src/Utilities/Compiler/WellKnownTypeNames.cs index 138c699f59..1925cdb3f5 100644 --- a/src/Utilities/Compiler/WellKnownTypeNames.cs +++ b/src/Utilities/Compiler/WellKnownTypeNames.cs @@ -210,7 +210,8 @@ internal static class WellKnownTypeNames public const string SystemRange = "System.Range"; public const string SystemReadOnlyMemory1 = "System.ReadOnlyMemory`1"; public const string SystemReadOnlySpan1 = "System.ReadOnlySpan`1"; - public const string SystemReflectionAssemblyFullName = "System.Reflection.Assembly"; + public const string SystemReflectionAssembly = "System.Reflection.Assembly"; + public const string SystemReflectionAssemblyName = "System.Reflection.AssemblyName"; public const string SystemReflectionAssemblyVersionAttribute = "System.Reflection.AssemblyVersionAttribute"; public const string SystemReflectionMemberInfo = "System.Reflection.MemberInfo"; public const string SystemReflectionParameterInfo = "System.Reflection.ParameterInfo"; diff --git a/src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/TaintedDataAnalysis/DllSinks.cs b/src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/TaintedDataAnalysis/DllSinks.cs index b24ada3849..72255e6190 100644 --- a/src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/TaintedDataAnalysis/DllSinks.cs +++ b/src/Utilities/FlowAnalysis/FlowAnalysis/Analysis/TaintedDataAnalysis/DllSinks.cs @@ -17,7 +17,7 @@ static DllSinks() var sinkInfosBuilder = PooledHashSet.GetInstance(); sinkInfosBuilder.AddSinkInfo( - WellKnownTypeNames.SystemReflectionAssemblyFullName, + WellKnownTypeNames.SystemReflectionAssembly, SinkKind.Dll, isInterface: false, isAnyStringParameterInConstructorASink: false, From 5920d194d5e78835c0b7513f7bda6e14315e6d5a Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Wed, 29 Jul 2020 14:29:27 -0700 Subject: [PATCH 02/12] Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amaury Levé --- .../MicrosoftNetCoreAnalyzersResources.resx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx index d7afab36ab..f3b755e16b 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx @@ -1426,9 +1426,9 @@ Accessing Assembly location or file path is not valid when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid '{0}'`' when publishing as a single-file Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - \ No newline at end of file + From 24e739dd864b2435526c7c537d790f5ed5ef2e7b Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Wed, 29 Jul 2020 14:29:38 -0700 Subject: [PATCH 03/12] Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amaury Levé --- .../MicrosoftNetCoreAnalyzersResources.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx index f3b755e16b..ae2d2bc361 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx @@ -1429,6 +1429,6 @@ Avoid '{0}'`' when publishing as a single-file - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + Avoid '{0}' when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. From 93473501755a6c114edb26f0a8e677194f329f7c Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Wed, 29 Jul 2020 14:29:46 -0700 Subject: [PATCH 04/12] Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amaury Levé --- .../Publish/AvoidAssemblyLocationInSingleFile.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs index bd9b04724b..1f204a9de7 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs @@ -1,7 +1,6 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -#nullable enable using System; using System.Collections.Generic; From a9ca099881d3cc56058dc93de6a68e297a95db09 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Wed, 29 Jul 2020 14:29:56 -0700 Subject: [PATCH 05/12] Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amaury Levé --- .../Publish/AvoidAssemblyLocationInSingleFile.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs index 1f204a9de7..634566a7a9 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs @@ -1,4 +1,3 @@ - // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. From 26e5052db1ed84c91cae7ba713e711a81052e66c Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Wed, 29 Jul 2020 18:06:43 -0700 Subject: [PATCH 06/12] Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amaury Levé --- .../MicrosoftNetCoreAnalyzersResources.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx index ae2d2bc361..8a0b2ec44e 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx @@ -1426,7 +1426,7 @@ Accessing Assembly location or file path is not valid when publishing as a single-file. - Avoid '{0}'`' when publishing as a single-file + Avoid '{0}' when publishing as a single-file Avoid '{0}' when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. From 887f95f35d6577500be4693e84984a137a9cca4d Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Wed, 29 Jul 2020 18:06:51 -0700 Subject: [PATCH 07/12] Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Amaury Levé --- .../Publish/AvoidAssemblyLocationInSingleFile.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs index 634566a7a9..18185bffc5 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs @@ -1,6 +1,5 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - using System; using System.Collections.Generic; using System.Collections.Immutable; From d62018c751047456a3b67c3ddbc61a6eaded7750 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Wed, 29 Jul 2020 20:50:03 -0700 Subject: [PATCH 08/12] Respond to PR comments --- .../Core/AnalyzerReleases.Unshipped.md | 4 +- .../MicrosoftNetCoreAnalyzersResources.resx | 10 +- .../AvoidAssemblyLocationInSingleFile.cs | 92 +++++++++++-------- .../MicrosoftNetCoreAnalyzersResources.cs.xlf | 14 +-- .../MicrosoftNetCoreAnalyzersResources.de.xlf | 14 +-- .../MicrosoftNetCoreAnalyzersResources.es.xlf | 14 +-- .../MicrosoftNetCoreAnalyzersResources.fr.xlf | 14 +-- .../MicrosoftNetCoreAnalyzersResources.it.xlf | 14 +-- .../MicrosoftNetCoreAnalyzersResources.ja.xlf | 14 +-- .../MicrosoftNetCoreAnalyzersResources.ko.xlf | 14 +-- .../MicrosoftNetCoreAnalyzersResources.pl.xlf | 14 +-- ...crosoftNetCoreAnalyzersResources.pt-BR.xlf | 14 +-- .../MicrosoftNetCoreAnalyzersResources.ru.xlf | 14 +-- .../MicrosoftNetCoreAnalyzersResources.tr.xlf | 14 +-- ...osoftNetCoreAnalyzersResources.zh-Hans.xlf | 14 +-- ...osoftNetCoreAnalyzersResources.zh-Hant.xlf | 14 +-- .../AvoidAssemblyLocationInSingleFileTests.cs | 42 ++++----- 17 files changed, 171 insertions(+), 159 deletions(-) diff --git a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md index a15033321b..827c2b8bcc 100644 --- a/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md +++ b/src/NetAnalyzers/Core/AnalyzerReleases.Unshipped.md @@ -38,7 +38,9 @@ CA2353 | Security | Disabled | DataSetDataTableInSerializableTypeAnalyzer, [Docu CA2354 | Security | Disabled | DataSetDataTableInIFormatterSerializableObjectGraphAnalyzer, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca2354) CA2355 | Security | Disabled | DataSetDataTableInSerializableObjectGraphAnalyzer, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca2355) CA2356 | Security | Disabled | DataSetDataTableInWebSerializableObjectGraphAnalyzer, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca2356) -CA3000 | Publish | Warning | DoNotUseAssemblyLocationInSingleFile, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/ca3000) + +IL3000 | Publish | Warning | DoNotUseAssemblyLocationInSingleFile, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/il3000) +IL3001 | Publish | Warning | DoNotUseAssemblyGetFilesInSingleFile, [Documentation](https://docs.microsoft.com/visualstudio/code-quality/il3001) ### Changed Rules Rule ID | New Category | New Severity | Old Category | Old Severity | Notes diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx index 8a0b2ec44e..2cd4e64396 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx @@ -1422,13 +1422,13 @@ Do not use 'OutAttribute' on string parameters for P/Invokes - - Accessing Assembly location or file path is not valid when publishing as a single-file. - - Avoid '{0}' when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file - Avoid '{0}' when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + + + Assemblies embedded in a single-file app cannot have additional files in the manifest. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs index 18185bffc5..87e6521539 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs @@ -17,25 +17,34 @@ namespace Microsoft.NetCore.Analyzers.Publish [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] public sealed class AvoidAssemblyLocationInSingleFile : DiagnosticAnalyzer { - internal const string RuleId = "CA3000"; - - private static readonly LocalizableString s_localizableTitle = new LocalizableResourceString( - nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileTitle), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)); - private static readonly LocalizableString s_localizableMessage = new LocalizableResourceString( - nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileMessage), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)); - private static readonly LocalizableString s_localizableDescription = new LocalizableResourceString( - nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileDescription), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)); - - internal static DiagnosticDescriptor Rule = DiagnosticDescriptorHelper.Create(RuleId, - s_localizableTitle, - s_localizableMessage, - DiagnosticCategory.Publish, - RuleLevel.BuildWarning, - s_localizableDescription, - isPortedFxCopRule: false, - isDataflowRule: false); - - public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(Rule); + public const string IL3000 = nameof(IL3000); + public const string IL3001 = nameof(IL3001); + + internal static DiagnosticDescriptor LocationRule = DiagnosticDescriptorHelper.Create( + IL3000, + new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileTitle), + MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), + new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileMessage), + MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), + DiagnosticCategory.Publish, + RuleLevel.BuildWarning, + description: null, + isPortedFxCopRule: false, + isDataflowRule: false); + + internal static DiagnosticDescriptor GetFilesRule = DiagnosticDescriptorHelper.Create( + IL3001, + new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyGetFilesInSingleFile), + MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), + new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyGetFilesInSingleFile), + MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), + DiagnosticCategory.Publish, + RuleLevel.BuildWarning, + description: null, + isPortedFxCopRule: false, + isDataflowRule: false); + + public override ImmutableArray SupportedDiagnostics => ImmutableArray.Create(LocationRule, GetFilesRule); public override void Initialize(AnalysisContext context) { @@ -58,24 +67,20 @@ public override void Initialize(AnalysisContext context) return; } - HashSet properties = new HashSet(SymbolEqualityComparer.Default); - HashSet methods = new HashSet(SymbolEqualityComparer.Default); + var properties = new List(); + var methods = new List(); - var assemblyType = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssembly); - if (assemblyType is not null) + if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssembly, out var assemblyType)) { // properties addIfNotNull(properties, tryGetSingleSymbol(assemblyType.GetMembers("Location"))); - addIfNotNull(properties, tryGetSingleSymbol(assemblyType.GetMembers("CodeBase"))); - addIfNotNull(properties, tryGetSingleSymbol(assemblyType.GetMembers("EscapedCodeBase"))); // methods - methods.UnionWith(assemblyType.GetMembers("GetFile").OfType()); - methods.UnionWith(assemblyType.GetMembers("GetFiles").OfType()); + methods.AddRange(assemblyType.GetMembers("GetFile").OfType()); + methods.AddRange(assemblyType.GetMembers("GetFiles").OfType()); } - var assemblyNameType = compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssemblyName); - if (assemblyNameType is not null) + if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssemblyName, out var assemblyNameType)) { addIfNotNull(properties, tryGetSingleSymbol(assemblyNameType.GetMembers("CodeBase"))); addIfNotNull(properties, tryGetSingleSymbol(assemblyNameType.GetMembers("EscapedCodeBase"))); @@ -85,34 +90,41 @@ public override void Initialize(AnalysisContext context) { var access = (IPropertyReferenceOperation)operationContext.Operation; var property = access.Property; - if (!properties.Contains(property)) + if (!contains(properties, property, SymbolEqualityComparer.Default)) { return; } - operationContext.ReportDiagnostic(Diagnostic.Create( - Rule, - access.Syntax.GetLocation(), - property)); + operationContext.ReportDiagnostic(access.CreateDiagnostic(LocationRule, property)); }, OperationKind.PropertyReference); context.RegisterOperationAction(operationContext => { var invocation = (IInvocationOperation)operationContext.Operation; var targetMethod = invocation.TargetMethod; - if (!methods.Contains(targetMethod)) + if (!contains(methods, targetMethod, SymbolEqualityComparer.Default)) { return; } - operationContext.ReportDiagnostic(Diagnostic.Create( - Rule, - invocation.Syntax.GetLocation(), - targetMethod)); + operationContext.ReportDiagnostic(invocation.CreateDiagnostic(GetFilesRule, targetMethod)); }, OperationKind.Invocation); return; + static bool contains(List list, T elem, TComp comparer) + where TComp : IEqualityComparer + { + foreach (var e in list) + { + if (comparer.Equals(e, elem)) + { + return true; + } + } + return false; + } + static TSymbol? tryGetSingleSymbol(ImmutableArray members) where TSymbol : class, ISymbol { TSymbol? candidate = null; @@ -133,7 +145,7 @@ public override void Initialize(AnalysisContext context) return candidate; } - static void addIfNotNull(HashSet properties, TSymbol? p) where TSymbol : class, ISymbol + static void addIfNotNull(List properties, TSymbol? p) where TSymbol : class, ISymbol { if (p is not null) { diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf index 54a7230567..52ad056831 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf @@ -47,19 +47,19 @@ Literály řetězců atributů by se měly správně parsovat - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf index c06a16eb37..e4ff5eb32b 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf @@ -47,19 +47,19 @@ Attributzeichenfolgenliterale müssen richtig analysiert werden - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf index 9bbbf7f5c3..f4692bcfe4 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf @@ -47,19 +47,19 @@ Los literales de cadena de atributo se deben analizar correctamente - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf index 41e0ab283b..68b54c3e8b 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf @@ -47,19 +47,19 @@ Les littéraux de chaîne d'attribut doivent être analysés correctement - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf index 0742ed1481..e6605d27ec 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf @@ -47,19 +47,19 @@ I valori letterali stringa dell'attributo devono essere analizzati correttamente - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf index 4da8e75cfe..c09f217b38 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf @@ -47,19 +47,19 @@ 属性文字列リテラルは、正しく解析する必要があります - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf index 2c0d3bdc61..d0b0f0133d 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf @@ -47,19 +47,19 @@ 특성 문자열 리터럴이 올바르게 구문 분석되어야 합니다. - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf index 488e20ab35..a4da71cdd7 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf @@ -47,19 +47,19 @@ Analiza literałów ciągu atrybutu powinna kończyć się powodzeniem - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf index 7e451c3d23..926589ae20 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf @@ -47,19 +47,19 @@ Literais de cadeias de caracteres de atributos devem ser analisados corretamente - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf index db2c0a15f1..c0df1b473d 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf @@ -47,19 +47,19 @@ Синтаксический анализ строковых литералов атрибута должен осуществляться правильно - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf index 35ed142ce7..b63ff7ef8c 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf @@ -47,19 +47,19 @@ Öznitelik dizesinin sabit değerleri doğru ayrıştırılmalıdır - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf index 170c0c3dee..23f53b1196 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf @@ -47,19 +47,19 @@ 特性字符串文本应正确分析 - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf index 8b84e734ce..57689a01e3 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf @@ -47,19 +47,19 @@ 屬性字串常值應正確剖析 - - Accessing Assembly location or file path is not valid when publishing as a single-file. - Accessing Assembly location or file path is not valid when publishing as a single-file. + + Assemblies embedded in a single-file app cannot have additional files in the manifest. + Assemblies embedded in a single-file app cannot have additional files in the manifest. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - Avoid `{0}` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Avoid `{0}` when publishing as a single-file. - Avoid `{0}` when publishing as a single-file. + Avoid using accessing Assembly file path when publishing as a single-file + Avoid using accessing Assembly file path when publishing as a single-file diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs index 0206a13c68..780cb653cc 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFileTests.cs @@ -8,7 +8,7 @@ Microsoft.NetCore.Analyzers.Publish.AvoidAssemblyLocationInSingleFile, Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; using static Analyzer.Utilities.MSBuildPropertyOptionNames; -using System.Collections.Generic; +using static Microsoft.NetCore.Analyzers.Publish.AvoidAssemblyLocationInSingleFile; namespace Microsoft.NetCore.Analyzers.Publish.UnitTests { @@ -45,9 +45,9 @@ class C DiagnosticResult[] diagnostics; if (publish is true && includeContent is not true) { - diagnostics = new[] { -// /0/Test0.cs(5,26): warning CA3000: Avoid `System.Reflection.Assembly.Location` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. -VerifyCS.Diagnostic().WithSpan(5, 26, 5, 66).WithArguments("System.Reflection.Assembly.Location") + diagnostics = new DiagnosticResult[] { + // /0/Test0.cs(5,26): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(5, 26, 5, 66).WithArguments("System.Reflection.Assembly.Location"), }; } else @@ -70,17 +70,14 @@ public void M() { var a = Assembly.GetExecutingAssembly(); _ = a.Location; + // below will be obsolete in 5.0 _ = a.CodeBase; _ = a.EscapedCodeBase; } }"; return VerifyDiagnosticsAsync(src, - // /0/Test0.cs(8,13): warning CA3000: Avoid `System.Reflection.Assembly.Location` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - VerifyCS.Diagnostic().WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location"), - // /0/Test0.cs(9,13): warning CA3000: Avoid `System.Reflection.Assembly.CodeBase` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - VerifyCS.Diagnostic().WithSpan(9, 13, 9, 23).WithArguments("System.Reflection.Assembly.CodeBase"), - // /0/Test0.cs(10,13): warning CA3000: Avoid `System.Reflection.Assembly.EscapedCodeBase` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - VerifyCS.Diagnostic().WithSpan(10, 13, 10, 30).WithArguments("System.Reflection.Assembly.EscapedCodeBase") + // /0/Test0.cs(8,13): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location") ); } @@ -99,10 +96,10 @@ public void M() } }"; return VerifyDiagnosticsAsync(src, - // /0/Test0.cs(8,13): warning CA3000: Avoid `System.Reflection.Assembly.GetFile(string)` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - VerifyCS.Diagnostic().WithSpan(8, 13, 8, 41).WithArguments("System.Reflection.Assembly.GetFile(string)"), - // /0/Test0.cs(9,13): warning CA3000: Avoid `System.Reflection.Assembly.GetFiles()` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - VerifyCS.Diagnostic().WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()") + // /0/Test0.cs(8,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest. + VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3001).WithSpan(8, 13, 8, 41).WithArguments("System.Reflection.Assembly.GetFile(string)"), + // /0/Test0.cs(9,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest. + VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3001).WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()") ); } @@ -121,10 +118,10 @@ public void M() } }"; return VerifyDiagnosticsAsync(src, - // /0/Test0.cs(8,13): warning CA3000: Avoid `System.Reflection.AssemblyName.CodeBase` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - VerifyCS.Diagnostic().WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.AssemblyName.CodeBase"), - // /0/Test0.cs(9,13): warning CA3000: Avoid `System.Reflection.AssemblyName.EscapedCodeBase` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - VerifyCS.Diagnostic().WithSpan(9, 13, 9, 30).WithArguments("System.Reflection.AssemblyName.EscapedCodeBase") + // /0/Test0.cs(8,13): warning IL3000: 'System.Reflection.AssemblyName.CodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.AssemblyName.CodeBase"), + // /0/Test0.cs(9,13): warning IL3000: 'System.Reflection.AssemblyName.EscapedCodeBase' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(9, 13, 9, 30).WithArguments("System.Reflection.AssemblyName.EscapedCodeBase") ); } @@ -145,10 +142,11 @@ public void M() } }"; return VerifyDiagnosticsAsync(src, - // /0/Test0.cs(8,13): warning CA3000: Avoid `System.Reflection.Assembly.Location` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - VerifyCS.Diagnostic().WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location"), - // /0/Test0.cs(9,13): warning CA3000: Avoid `System.Reflection.Assembly.GetFiles()` when publishing as a single-file. Assemblies inside a single-file bundle do not have a file or file path. If the path to the app directory is needed, consider calling System.AppContext.BaseDirectory. - VerifyCS.Diagnostic().WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()")); + // /0/Test0.cs(8,13): warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. + VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3000).WithSpan(8, 13, 8, 23).WithArguments("System.Reflection.Assembly.Location"), + // /0/Test0.cs(9,13): warning IL3001: Assemblies embedded in a single-file app cannot have additional files in the manifest. + VerifyCS.Diagnostic(AvoidAssemblyLocationInSingleFile.IL3001).WithSpan(9, 13, 9, 25).WithArguments("System.Reflection.Assembly.GetFiles()") + ); } private Task VerifyDiagnosticsAsync(string source, params DiagnosticResult[] expected) From 7cbb603ab2133cb1b94ed3184496707beac7c346 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Thu, 30 Jul 2020 13:31:34 -0700 Subject: [PATCH 09/12] Adjust warning message --- .../MicrosoftNetCoreAnalyzersResources.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx index 2cd4e64396..e5646d781d 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx @@ -1429,6 +1429,6 @@ '{0}' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. From 1c1418f156e3610ea54eb72600b462f12df1eae7 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Thu, 30 Jul 2020 15:03:22 -0700 Subject: [PATCH 10/12] Update resources and diagnostic ranges --- .../xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.de.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.es.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.it.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf | 4 ++-- .../xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf | 4 ++-- src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt | 4 ++++ 14 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf index 52ad056831..db60894af6 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.cs.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf index e4ff5eb32b..a05806c05f 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.de.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf index f4692bcfe4..e58fe5b0cd 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.es.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf index 68b54c3e8b..7b8f75cf52 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.fr.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf index e6605d27ec..f358ddafac 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.it.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf index c09f217b38..e6240ce7ed 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ja.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf index d0b0f0133d..5bcfca3378 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ko.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf index a4da71cdd7..b1d29ca496 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pl.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf index 926589ae20..4c425a4d06 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.pt-BR.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf index c0df1b473d..0bf0f847e9 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.ru.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf index b63ff7ef8c..e7bdd64ce1 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.tr.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf index 23f53b1196..2da0537e9d 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hans.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf index 57689a01e3..bdf51c9614 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/xlf/MicrosoftNetCoreAnalyzersResources.zh-Hant.xlf @@ -48,8 +48,8 @@ - Assemblies embedded in a single-file app cannot have additional files in the manifest. - Assemblies embedded in a single-file app cannot have additional files in the manifest. + '{0}' will throw for assemblies embedded in a single-file app. + '{0}' will throw for assemblies embedded in a single-file app. diff --git a/src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt b/src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt index 0215e43aea..25d8d19653 100644 --- a/src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt +++ b/src/Utilities/Compiler/DiagnosticCategoryAndIdRanges.txt @@ -36,3 +36,7 @@ RoslynDiagnosticsMaintainability: RS0000-RS0999 RoslynDiagnosticsPerformance: RS0000-RS0999 RoslynDiagnosticsReliability: RS0000-RS0999 RoslynDiagnosticsUsage: RS0000-RS0999 + +# dotnet publish rules +# These are warnings for single-file publish or the IL trimmer +Publish: IL0000-IL9999 \ No newline at end of file From 6b9ea80cac39166d46c4f5976d1532101985beb2 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Fri, 31 Jul 2020 13:01:52 -0700 Subject: [PATCH 11/12] Update src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs Co-authored-by: Manish Vasani --- .../Publish/AvoidAssemblyLocationInSingleFile.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs index 87e6521539..4f81c698b7 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs @@ -112,7 +112,7 @@ public override void Initialize(AnalysisContext context) return; - static bool contains(List list, T elem, TComp comparer) + static bool Contains(List list, T elem, TComp comparer) where TComp : IEqualityComparer { foreach (var e in list) From be5822b25dfc73e9c7ebbcb3e29fb3909381275b Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Fri, 31 Jul 2020 13:21:07 -0700 Subject: [PATCH 12/12] Respond to PR comments --- .../AvoidAssemblyLocationInSingleFile.cs | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs index 4f81c698b7..1d452f8d7b 100644 --- a/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs +++ b/src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Publish/AvoidAssemblyLocationInSingleFile.cs @@ -12,7 +12,7 @@ namespace Microsoft.NetCore.Analyzers.Publish { /// - /// CA3000: Do not use Assembly.Location in single-file publish + /// IL3000, IL3001: Do not use Assembly file path in single-file publish /// [DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] public sealed class AvoidAssemblyLocationInSingleFile : DiagnosticAnalyzer @@ -34,7 +34,7 @@ public sealed class AvoidAssemblyLocationInSingleFile : DiagnosticAnalyzer internal static DiagnosticDescriptor GetFilesRule = DiagnosticDescriptorHelper.Create( IL3001, - new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyGetFilesInSingleFile), + new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileTitle), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyGetFilesInSingleFile), MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)), @@ -67,30 +67,33 @@ public override void Initialize(AnalysisContext context) return; } - var properties = new List(); - var methods = new List(); + var propertiesBuilder = ImmutableArray.CreateBuilder(); + var methodsBuilder = ImmutableArray.CreateBuilder(); if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssembly, out var assemblyType)) { // properties - addIfNotNull(properties, tryGetSingleSymbol(assemblyType.GetMembers("Location"))); + AddIfNotNull(propertiesBuilder, TryGetSingleSymbol(assemblyType.GetMembers("Location"))); // methods - methods.AddRange(assemblyType.GetMembers("GetFile").OfType()); - methods.AddRange(assemblyType.GetMembers("GetFiles").OfType()); + methodsBuilder.AddRange(assemblyType.GetMembers("GetFile").OfType()); + methodsBuilder.AddRange(assemblyType.GetMembers("GetFiles").OfType()); } if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssemblyName, out var assemblyNameType)) { - addIfNotNull(properties, tryGetSingleSymbol(assemblyNameType.GetMembers("CodeBase"))); - addIfNotNull(properties, tryGetSingleSymbol(assemblyNameType.GetMembers("EscapedCodeBase"))); + AddIfNotNull(propertiesBuilder, TryGetSingleSymbol(assemblyNameType.GetMembers("CodeBase"))); + AddIfNotNull(propertiesBuilder, TryGetSingleSymbol(assemblyNameType.GetMembers("EscapedCodeBase"))); } + var properties = propertiesBuilder.ToImmutable(); + var methods = methodsBuilder.ToImmutable(); + context.RegisterOperationAction(operationContext => { var access = (IPropertyReferenceOperation)operationContext.Operation; var property = access.Property; - if (!contains(properties, property, SymbolEqualityComparer.Default)) + if (!Contains(properties, property, SymbolEqualityComparer.Default)) { return; } @@ -102,7 +105,7 @@ public override void Initialize(AnalysisContext context) { var invocation = (IInvocationOperation)operationContext.Operation; var targetMethod = invocation.TargetMethod; - if (!contains(methods, targetMethod, SymbolEqualityComparer.Default)) + if (!Contains(methods, targetMethod, SymbolEqualityComparer.Default)) { return; } @@ -112,7 +115,7 @@ public override void Initialize(AnalysisContext context) return; - static bool Contains(List list, T elem, TComp comparer) + static bool Contains(ImmutableArray list, T elem, TComp comparer) where TComp : IEqualityComparer { foreach (var e in list) @@ -125,7 +128,7 @@ static bool Contains(List list, T elem, TComp comparer) return false; } - static TSymbol? tryGetSingleSymbol(ImmutableArray members) where TSymbol : class, ISymbol + static TSymbol? TryGetSingleSymbol(ImmutableArray members) where TSymbol : class, ISymbol { TSymbol? candidate = null; foreach (var m in members) @@ -145,7 +148,7 @@ static bool Contains(List list, T elem, TComp comparer) return candidate; } - static void addIfNotNull(List properties, TSymbol? p) where TSymbol : class, ISymbol + static void AddIfNotNull(ImmutableArray.Builder properties, TSymbol? p) where TSymbol : class, ISymbol { if (p is not null) {