Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain

###############################################################################
# The target filter parser is generated (Source/TargetFilter/regenerate.sh).
# The grammar and the scripts are read as checked out, so every machine reads
# the same text; the generated folder is kept byte for byte as the generator
# writes it (CRLF), and its rule comes last so that no rule above converts it.
###############################################################################
*.gram text eol=lf
*.sh text eol=lf
Source/TargetFilter/dotgram.pin text eol=lf
Source/TargetFilter/Generated/** -text
82 changes: 80 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
workflow_dispatch:

jobs:
build:
Expand Down Expand Up @@ -119,18 +120,38 @@ jobs:
public static void OnBeforeCall(InterceptInfo info) { Calls++; }
}

// Applied to a type with a TargetFilter, so that a filter is selected by the packed generator on the oldest toolchain.
// Filters are parsed by the hand-written parser for now; this covers the generated one once filters are parsed by it.
[Aspect(OnBeforeCall = nameof(OnBeforeCall))]
[AttributeUsage(AttributeTargets.Class)]
sealed class FilteredAttribute : Attribute
{
public static int Calls;
public string? TargetFilter { get; set; }
public static void OnBeforeCall(InterceptInfo info) { Calls++; }
}

static class Service
{
[Count] public static int Twice(int x) { return x * 2; }
}

[Filtered(TargetFilter = "contains: Selected")]
static class FilteredService
{
public static int Selected(int x) { return x + 1; }
public static int Other(int x) { return x + 2; }
}

static class Program
{
static int Main()
{
var result = Service.Twice(21);
Console.WriteLine($"result={result} calls={CountAttribute.Calls}");
return result == 42 && CountAttribute.Calls == 1 ? 0 : 1;
var filtered = FilteredService.Selected(1) + FilteredService.Other(1);
Console.WriteLine($"result={result} calls={CountAttribute.Calls} filtered={filtered} filteredCalls={FilteredAttribute.Calls}");
// Only the call the filter selects is intercepted.
return result == 42 && CountAttribute.Calls == 1 && filtered == 5 && FilteredAttribute.Calls == 1 ? 0 : 1;
}
}
EOF
Expand All @@ -139,3 +160,60 @@ jobs:
cmp "$RUNNER_TEMP/feed/AspectGenerator.$ASPECT_GENERATOR_VERSION.nupkg" "$dir/packages/aspectgenerator/${ASPECT_GENERATOR_VERSION,,}/aspectgenerator.${ASPECT_GENERATOR_VERSION,,}.nupkg"
(cd "$dir" && dotnet build --configuration Release --no-restore && dotnet run --configuration Release --no-build)
done

# The target filter parser is generated ahead of the build (Source/TargetFilter/regenerate.sh, at the DotGram commit and
# SDK in Source/TargetFilter/dotgram.pin) and committed. This job regenerates it exactly as a developer does and fails
# when the committed folder differs: a grammar edited without regenerating, a stale or hand-edited file, a leftover file.
# Windows checks as well that the checkout's line-ending conversion leaves the generated folder byte for byte as written.
filter-parser:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]

runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4
- name: Read the pin
id: pin
shell: bash
run: |
set -euo pipefail
sdk="$(sed -n 's/^sdk=//p' Source/TargetFilter/dotgram.pin | tr -d '\r')"
[ -n "$sdk" ] || { echo "Source/TargetFilter/dotgram.pin has no sdk=" >&2; exit 1; }
echo "sdk=$sdk" >> "$GITHUB_OUTPUT"
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ steps.pin.outputs.sdk }}
- name: Select the pinned SDK
shell: bash
# The runner image may have a newer SDK installed; the script refuses anything but the pinned one.
run: dotnet new globaljson --sdk-version "${{ steps.pin.outputs.sdk }}" --roll-forward disable --force
- name: Regenerate the parser
shell: bash
run: Source/TargetFilter/regenerate.sh
- name: The committed parser is current
shell: bash
run: |
status="$(git status --porcelain --untracked-files=all -- Source/TargetFilter)"
if [ -n "$status" ]; then
echo "Source/TargetFilter differs after regeneration; run Source/TargetFilter/regenerate.sh and commit the result:" >&2
echo "$status" >&2
git --no-pager diff --stat -- Source/TargetFilter >&2
exit 1
fi
- name: The local DotGram mode refuses to pack
if: runner.os == 'Linux'
shell: bash
# -p:DotGramLocal=true builds an unpinned parser, so a package must never come out of it. The scratch checkout the
# script just made is the DotGram checkout.
run: |
set -euo pipefail
commit="$(sed -n 's/^commit=//p' Source/TargetFilter/dotgram.pin | tr -d '\r')"
root="${TMPDIR:-/tmp}/aspectgenerator-dotgram-${commit:0:12}/dotgram"
if dotnet pack Source/AspectGenerator.csproj --configuration Release -p:DotGramLocal=true -p:DotGramRoot="$root" -nodeReuse:false; then
echo "Packing succeeded with DotGramLocal=true; it must fail." >&2
exit 1
fi
11 changes: 11 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@ Current notes:
- Preserve CRLF line endings for touched repository files.
- Do not add new NuGet packages, template engines, formatters, source-generation helper libraries, or build tools unless the maintainer explicitly asks for that dependency.
- Do not replace internal code generation with Scriban, Handlebars.Net, CSharpier, or other external templating/formatting tools as an automatic refactoring step.

## Target filter parser

The parser of the target filter language is written in DotGram (`Source/TargetFilter/TargetFilter.gram`) and generated ahead of the build. The generated C# in `Source/TargetFilter/Generated` is committed and compiled like any other source: the build and the package have no DotGram dependency, and nothing is generated at build time.

What is proven today: the generated parser compiles against Roslyn 4.12 on `netstandard2.0` inside the generator assembly, and the unit tests (`TargetFilterParserSmokeTests`) exercise it. Production target filters are still parsed by the hand-written parser, so the `minimum-sdk` consumer's `TargetFilter` does not run the generated parser yet; that check covers it once filters are parsed by it.

- After any change to `TargetFilter.gram` (or to a type it builds), run `Source/TargetFilter/regenerate.sh` and commit the changed files with it. Never edit `Generated` by hand; the next run overwrites it. The script works in Git Bash on Windows too.
- `Source/TargetFilter/dotgram.pin` names the DotGram commit and the exact .NET SDK the generation uses. The script builds `DotGram.Cli` at that commit into a scratch folder (`DOTGRAM_SCRATCH`, `DOTGRAM_REPO` to fetch from a local clone) and refuses to run under another SDK: select the pinned one first, for instance with an untracked `global.json` (the refusal message prints it). Move the pin only to a green commit of the DotGram repository, in a commit of its own with the regenerated folder.
- The `filter-parser` CI job runs the same script and fails when `git status` shows any difference in `Source/TargetFilter`. The script itself fails when `Generated` holds a file the manifest (`.dotgram.json`) does not list, which the generator would leave in place. `.gitattributes` keeps `*.gram` and scripts at LF and the generated folder byte for byte (`-text`, last rule).
- `-p:DotGramLocal=true -p:DotGramRoot=<a DotGram checkout>` builds the parser from the grammar with that checkout as the generator and leaves the committed folder out (default `DotGramRoot` is `..\dotgram`, next to this repository). It is for trying a DotGram change; the committed folder is what ships. In this mode automatic packaging is off and an explicit `dotnet pack` fails, so a package never carries an unpinned parser.
6 changes: 6 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>

<!-- Off by default: the target filter parser is generated ahead of the build and committed. See Source/AspectGenerator.csproj. -->
<PropertyGroup>
<DotGramLocal Condition="'$(DotGramLocal)' == ''">false</DotGramLocal>
<DotGramRoot Condition="'$(DotGramRoot)' == ''">$(MSBuildThisFileDirectory)..\dotgram</DotGramRoot>
</PropertyGroup>

<ItemGroup>
<CompilerVisibleProperty Include="AspectGeneratorGenerateApi" />
<CompilerVisibleProperty Include="AspectGeneratorPublicApi" />
Expand Down
29 changes: 29 additions & 0 deletions Source/AspectGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,35 @@
<IsRoslynComponent>true</IsRoslynComponent>
</PropertyGroup>

<!--
The target filter parser is written in DotGram and generated ahead of the build: Source/TargetFilter/regenerate.sh
writes Source/TargetFilter/Generated, which is committed and compiled like any other source, so the build needs no
DotGram and the package gains no dependency. The grammar is an additional file so that the generation sees it.

-p:DotGramLocal=true builds the parser from the grammar instead, with the DotGram sources in $(DotGramRoot) as the
generator, and leaves the committed folder out; compare its output with the folder to try a DotGram change.
That output is not pinned, so this mode never packs: automatic packaging is off and an explicit pack fails.
-->
<ItemGroup>
<AdditionalFiles Include="TargetFilter\TargetFilter.gram" />
</ItemGroup>

<PropertyGroup Condition="'$(DotGramLocal)' == 'true'">
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>

<Target Name="DotGramLocalNeverPacks" BeforeTargets="GenerateNuspec;_IntermediatePack;Pack" Condition="'$(DotGramLocal)' == 'true'">
<Error Text="DotGramLocal=true generates the target filter parser from the grammar with an unpinned DotGram, so the package must not be packed in this mode. Pack without it, from the committed parser." />
</Target>

<ItemGroup Condition="'$(DotGramLocal)' == 'true'">
<Compile Remove="TargetFilter\Generated\**" />
<ProjectReference Include="$(DotGramRoot)\src\DotGram\DotGram.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

<!-- A project reference does not bring the package's build/DotGram.targets: take what it adds, as the package would. -->
<Import Project="$(DotGramRoot)\src\DotGram\build\DotGram.targets" Condition="'$(DotGramLocal)' == 'true'" />

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" PrivateAssets="all" />
Expand Down
10 changes: 10 additions & 0 deletions Source/TargetFilter/Generated/.dotgram.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"format": 1,
"tool": "0.3.0",
"generator": "0.3.0",
"files": [
{ "name": "AspectGenerator.TargetFilterParser.g.cs" },
{ "name": "DotGram.Attributes.g.cs" },
{ "name": "Microsoft.CodeAnalysis.EmbeddedAttribute.cs" }
]
}
Loading
Loading