Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent handling of file-local types in generic constraints causes path exception in Source Generator #7572

Open
KARPED1EM opened this issue Feb 10, 2025 · 0 comments

Comments

@KARPED1EM
Copy link

KARPED1EM commented Feb 10, 2025

Issue Description

When using file-local types as generic type parameters in Source Generator implementation, Roslyn fails to properly validate the usage and results in runtime path exception, despite the code being syntactically valid.

Reproduction Code

Basic structure showing the issue:

public abstract class SyntaxGeneratorCollectionBase<T> : IIncrementalGenerator
{
    protected abstract T? ExtractDataFromContext(GeneratorSyntaxContext context, CancellationToken _);
    protected abstract void GenerateFromData(SourceProductionContext spc, ImmutableArray<T?> dataCollection);
}

[Generator(LanguageNames.CSharp)]
file sealed class ServiceExtensionsGenerator : SyntaxGeneratorCollectionBase<ViewData>
{
    protected override ViewData? ExtractDataFromContext(GeneratorSyntaxContext context, CancellationToken _) 
        => /* implementation */;

    protected override void GenerateFromData(SourceProductionContext spc, ImmutableArray<ViewData?> dataCollection) 
        => /* implementation */;
}

file sealed record ViewData(string Name);

Current Behavior

  1. Code compiles successfully despite potential invalidity of file-local type usage
  2. At runtime, source generator fails with:
System.ArgumentException: Illegal characters in path.
   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
   at Microsoft.CodeAnalysis.GeneratorDriver.ParseAdditionalSources(...)

Expected Behavior

Roslyn should either:

  • Properly validate and reject invalid file-local type usage in generic constraints at compile time
  • Or handle file-local types correctly without throwing path exceptions

Environment

  • Latest Roslyn/MSBuild toolchain
  • Reproducible in both Visual Studio and Rider
  • Source Generator compilation context
@jaredpar jaredpar transferred this issue from dotnet/roslyn Feb 12, 2025
@KARPED1EM KARPED1EM changed the title [Bug] Inconsistent handling of file-local types as generic types in source generator causes invalid path exception at compile time Inconsistent handling of file-local types in generic constraints causes path exception in Source Generator Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant