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

SourceGenerators: MSBuild property (and metadata?) values are (potentially) truncated before being passed to Generator #72592

Closed
DoctorVanGogh opened this issue Mar 19, 2024 · 2 comments
Assignees
Labels
Area-Compilers Bug Feature - Source Generators Source Generators Resolution-Duplicate The described behavior is tracked in another issue
Milestone

Comments

@DoctorVanGogh
Copy link

Version Used:
4.9.0-3.24121.1 (a98c90d)

Steps to Reproduce:

  1. Write a source generator which consumes a MSBuild property
      ...
      public class FooGen : IIncrementalGenerator {
          public void Initialize(IncrementalGeneratorInitializationContext context) {
              var pipeline = context.AnalyzerConfigOptionsProvider
                  .Select(
                      (acop, _) => acop.GlobalOptions.TryGetValue("build_property.Foo", out string s) ? s : null
                  );
    
              context.RegisterSourceOutput(
                  pipeline, 
                  (SourceProductionContext ctx, string property) => throw new Exception($"Property value was: '{property}'"));
          }
      }
      ...
    (Using an exception instead of actual source output for brevity)
  2. Consume the source generator in another project
  3. Ensure the property value is visible to the generator
      <ItemGroup>
        <CompilerVisibleProperty Include="Foo" />
      </ItemGroup>
  4. Set the property value and have it contain a ; character
      <PropertyGroup>
          <Foo>Wiz;Bang</Foo>
      </PropertyGroup>
  5. Build the consuming project.

Expected Behavior:
A property value identical to what is available in MSBuild - Here exception from the generator with a message of

... Exception was of type 'Exception' with message 'Property value was: Wiz;Bang'.

Actual Behavior:
A property value that is truncated at the first use of the ;character - Here exception from the generator with a message of

... Exception was of type 'Exception' with message 'Property value was: Wiz'.


It seems as if property values get truncated at ; characters before getting passed to the generator. The Incremental Generators Cookbook does not mention any forbidden characters ;)

I have not tested if the same issue occurs when using MSBuild Metadata (aka build_metadata.FooItem.BarMetedata) but would very much assume so.

This is quite the annoying bug because it is not unreasonable to want to pass ItemGroups to a generator, and setting a property to a simple <Foo>@(SomeGroup)</Foo> serializes the item group with ;as separator.

I'm assuming this is happening because the analyzer gets fed with some key1=...;key2=...;... format, internally using ; as a separator, and the MSBuild to generator transition does not (cannot?) properly escape semicolons before building that format.


Minimal Repo Solution:
AnalyzerMsBuildPropertyIssues.zip

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 19, 2024
@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 21, 2024
@jaredpar jaredpar added this to the 17.10 milestone Mar 21, 2024
@jaredpar jaredpar added the Feature - Source Generators Source Generators label Mar 21, 2024
@jaredpar jaredpar modified the milestones: 17.10, 17.11 Apr 1, 2024
@gtbuchanan
Copy link

I just encountered something similar using the built-in ProjectDir property (i.e. build_property.projectdir). The path was truncated at the first # character. I'm not sure if this is related. I cannot reproduce it in a unit test by passing the prop manually with AnalyzerConfigOptionsProvider. I've adapted the repro solution to work with my use case. You should see the same behavior when you unzip it to a directory with the name C#:

AnalyzerMsBuildPropertyIssues.zip

@sharwell
Copy link
Member

Duplicate of #44596

@sharwell sharwell marked this as a duplicate of #44596 May 31, 2024
@sharwell sharwell closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2024
@sharwell sharwell added the Resolution-Duplicate The described behavior is tracked in another issue label May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Feature - Source Generators Source Generators Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

5 participants