Skip to content

Commit

Permalink
Use InterceptorsNamespaces feature name instead of InterceptorsPrevie…
Browse files Browse the repository at this point in the history
…wNamespaces (#74865)
  • Loading branch information
RikkiGibson authored Aug 27, 2024
1 parent 7c7a412 commit 5758964
Show file tree
Hide file tree
Showing 27 changed files with 166 additions and 114 deletions.
8 changes: 5 additions & 3 deletions docs/features/interceptors.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,14 @@ There is an experimental public API `GetInterceptorMethod(this SemanticModel, In
### User opt-in

To use interceptors, the user project must specify the property `<InterceptorsPreviewNamespaces>`. This is a list of namespaces which are allowed to contain interceptors.
To use interceptors, the user project must specify the property `<InterceptorsNamespaces>`. This is a list of namespaces which are allowed to contain interceptors.
```xml
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.AspNetCore.Http.Generated;MyLibrary.Generated</InterceptorsPreviewNamespaces>
<InterceptorsNamespaces>$(InterceptorsNamespaces);Microsoft.AspNetCore.Http.Generated;MyLibrary.Generated</InterceptorsNamespaces>
```

It's expected that each entry in the `InterceptorsPreviewNamespaces` list roughly corresponds to one source generator. Well-behaved components are expected to not insert interceptors into namespaces they do not own.
It's expected that each entry in the `InterceptorsNamespaces` list roughly corresponds to one source generator. Well-behaved components are expected to not insert interceptors into namespaces they do not own.

For compatibility, the property `<InterceptorsPreviewNamespaces>` can be used as an alias for `<InterceptorsNamespaces>`. If both properties have non-empty values, they will be concatenated together in the order `$(InterceptorsNamespaces);$(InterceptorsPreviewNamespaces)` when passed to the compiler.

### Implementation strategy

Expand Down
12 changes: 6 additions & 6 deletions src/Compilers/CSharp/Portable/CSharpParseOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class CSharpParseOptions : ParseOptions, IEquatable<CSharpParseOpt
public static CSharpParseOptions Default { get; } = new CSharpParseOptions();

private ImmutableDictionary<string, string> _features;
private ImmutableArray<ImmutableArray<string>> _interceptorsPreviewNamespaces;
private ImmutableArray<ImmutableArray<string>> _interceptorsNamespaces;

/// <summary>
/// Gets the effective language version, which the compiler uses to select the
Expand Down Expand Up @@ -177,21 +177,21 @@ public override IReadOnlyDictionary<string, string> Features
}
}

internal ImmutableArray<ImmutableArray<string>> InterceptorsPreviewNamespaces
internal ImmutableArray<ImmutableArray<string>> InterceptorsNamespaces
{
get
{
if (!_interceptorsPreviewNamespaces.IsDefault)
if (!_interceptorsNamespaces.IsDefault)
{
return _interceptorsPreviewNamespaces;
return _interceptorsNamespaces;
}

// e.g. [["System", "Threading"], ["System", "Collections"]]
ImmutableArray<ImmutableArray<string>> previewNamespaces = Features.TryGetValue("InterceptorsPreviewNamespaces", out var namespaces) && namespaces.Length > 0
ImmutableArray<ImmutableArray<string>> previewNamespaces = Features.TryGetValue("InterceptorsNamespaces", out var namespaces) && namespaces.Length > 0
? makeNamespaces(namespaces)
: ImmutableArray<ImmutableArray<string>>.Empty;

ImmutableInterlocked.InterlockedInitialize(ref _interceptorsPreviewNamespaces, previewNamespaces);
ImmutableInterlocked.InterlockedInitialize(ref _interceptorsNamespaces, previewNamespaces);
return previewNamespaces;

static ImmutableArray<ImmutableArray<string>> makeNamespaces(string namespaces)
Expand Down
2 changes: 1 addition & 1 deletion src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7609,7 +7609,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<value>A switch expression arm does not begin with a 'case' keyword.</value>
</data>
<data name="ERR_InterceptorsFeatureNotEnabled" xml:space="preserve">
<value>The 'interceptors' experimental feature is not enabled in this namespace. Add '{0}' to your project.</value>
<value>The 'interceptors' feature is not enabled in this namespace. Add '{0}' to your project.</value>
</data>
<data name="ERR_InterceptorGlobalNamespace" xml:space="preserve">
<value>An interceptor cannot be declared in the global namespace.</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ internal sealed override CommandLineArguments CommonParse(IEnumerable<string> ar
}
else
{
// When a features value like "InterceptorsPreviewNamespaces=NS1;NS2" is provided,
// When a features value like "InterceptorsNamespaces=NS1;NS2" is provided,
// the build system will quote it so that splitting doesn't occur in the wrong layer.
// We need to unquote here so that subsequent layers can properly identify the feature name and value.
features.Add(value.Unquote());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ private void DecodeInterceptsLocationChecksumBased(DecodeWellKnownAttributeArgum
return;
}

var interceptorsNamespaces = ((CSharpParseOptions)attributeNameSyntax.SyntaxTree.Options).InterceptorsPreviewNamespaces;
var interceptorsNamespaces = ((CSharpParseOptions)attributeNameSyntax.SyntaxTree.Options).InterceptorsNamespaces;
var thisNamespaceNames = getNamespaceNames(this);
var foundAnyMatch = interceptorsNamespaces.Any(static (ns, thisNamespaceNames) => isDeclaredInNamespace(thisNamespaceNames, ns), thisNamespaceNames);
if (!foundAnyMatch)
Expand Down Expand Up @@ -1149,7 +1149,7 @@ static void reportFeatureNotEnabled(BindingDiagnosticBag diagnostics, Location a
}
else
{
var recommendedProperty = $"<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);{string.Join(".", namespaceNames)}</InterceptorsPreviewNamespaces>";
var recommendedProperty = $"<InterceptorsNamespaces>$(InterceptorsNamespaces);{string.Join(".", namespaceNames)}</InterceptorsNamespaces>";
diagnostics.Add(ErrorCode.ERR_InterceptorsFeatureNotEnabled, attributeLocation, recommendedProperty);
}
}
Expand All @@ -1170,7 +1170,7 @@ private void DecodeInterceptsLocationAttributeExperimentalCompat(
const int lineNumberParameterIndex = 1;
const int characterNumberParameterIndex = 2;

var interceptorsNamespaces = ((CSharpParseOptions)attributeSyntax.SyntaxTree.Options).InterceptorsPreviewNamespaces;
var interceptorsNamespaces = ((CSharpParseOptions)attributeSyntax.SyntaxTree.Options).InterceptorsNamespaces;
var thisNamespaceNames = getNamespaceNames();
var foundAnyMatch = interceptorsNamespaces.Any(ns => isDeclaredInNamespace(thisNamespaceNames, ns));
if (!foundAnyMatch)
Expand Down Expand Up @@ -1362,7 +1362,7 @@ static void reportFeatureNotEnabled(BindingDiagnosticBag diagnostics, AttributeS
}
else
{
var recommendedProperty = $"<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);{string.Join(".", namespaceNames)}</InterceptorsPreviewNamespaces>";
var recommendedProperty = $"<InterceptorsNamespaces>$(InterceptorsNamespaces);{string.Join(".", namespaceNames)}</InterceptorsNamespaces>";
diagnostics.Add(ErrorCode.ERR_InterceptorsFeatureNotEnabled, attributeSyntax, recommendedProperty);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void discoverInterceptors()
var toVisit = ArrayBuilder<NamespaceOrTypeSymbol>.GetInstance();

// Search the namespaces which were indicated to contain interceptors.
ImmutableArray<ImmutableArray<string>> interceptorsNamespaces = ((CSharpParseOptions)location.SourceTree.Options).InterceptorsPreviewNamespaces;
ImmutableArray<ImmutableArray<string>> interceptorsNamespaces = ((CSharpParseOptions)location.SourceTree.Options).InterceptorsNamespaces;
foreach (ImmutableArray<string> namespaceParts in interceptorsNamespaces)
{
if (namespaceParts is ["global"])
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5758964

Please sign in to comment.