Ordered evaluation in GlobbingUrlBuilder.BuildUrlList
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
The current `GlobbingUrlBuilder.BuildUrlList` does not evaluate include and exclude patterns in the order they are added, which leads to limitations when trying to achieve more complex file-matching behavior. Specifically, includes added after an exclude do not override the exclude, resulting in unexpected or undesired results.
### Expected Behavior
The API should evaluate include/exclude paths in order.
### Steps To Reproduce
```sh
$ dotnet new webapi -o webapi1
$ cd webapi1
```
Replace Program.cs with:
```c#
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.AspNetCore.Mvc.TagHelpers;
var globBuilder = new GlobbingUrlBuilder(
fileProvider: new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")),
cache: new MemoryCache(new MemoryCacheOptions()),
requestPathBase: "/"
);
var urls = globBuilder.BuildUrlList(
staticUrl: null,
includePattern: "**/*",
excludePattern: "ExcludeMe/**/*"
);
Console.WriteLine("Matched URLs:");
foreach (var url in urls)
{
Console.WriteLine(url);
}
```
make wwwroot structure:
```sh
$ mkdir -p wwwroot/ExcludeMe/ButActuallyIncludeMe
$ echo 'Hello' > wwwroot/helloWorld.txt
$ echo 'Do not show me' > wwwroot/ExcludeMe/notIncluded.txt
$ echo 'Include me!' > wwwroot/ExcludeMe/ButActuallyIncludeMe/hiEarth.txt
```
run:
```sh
$ dotnet run
...
Matched URLs:
/helloWorld.txt
```
Note, it is missing `ExcludeMe/ButActuallyIncludeMe/hiEarth.txt`
### Exceptions (if any)
_No response_
### .NET Version
9.0.203
### Anything else?
Once this API is available https://github.com/dotnet/runtime/issues/109408#issuecomment-2715409854, change
https://github.com/dotnet/aspnetcore/blob/e849f2052624e7fe981c1ab158414014d2339b23/src/Mvc/Mvc.TagHelpers/src/GlobbingUrlBuilder.cs#L127
```diff
- var matcher = MatcherBuilder != null ? MatcherBuilder() : new Matcher();
+ var matcher = MatcherBuilder != null ? MatcherBuilder() : new Matcher(preserveFilterOrder: true);
```
or add `bool preserveFilterOrder = false` argument in `BuildUrlList` as well if existing evaluation order must be kept.
Contributor guide
Assessment
This issue has not been assessed yet.