-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
[Questions] Several questions about source generators #60256
Comments
I suspect that you may have already found the answers to these questions, but in case not:
public void Initialize(IncrementalGeneratorInitializationContext initializationContent)
{
// Some of this code is from https://github.com/dotnet/roslyn/blob/main/docs/features/incremental-generators.md
var files = initializationContent.AdditionalTextsProvider.Where(/*static*/ file => file.Path.EndsWith(".json"));
var namesAndContents = files.Select((file, cancellationToken) => (Name: Path.GetFileNameWithoutExtension(file.Path), Content: file.GetText(cancellationToken).ToString(), Path: file.Path));
initializationContent.RegisterSourceOutput(namesAndContents, AddSource);
}
private void AddSource(SourceProductionContext context, (string Name, string Content, string Path) file)
{
string fileName = $"{file.Name}.g.cs";
try
{
if (file.Content == null)
throw new Exception("Failed to read file \"" + file.Path + "\"");
string sourceCode = .....
context.AddSource(fileName, sourceCode);
}
catch (Exception e)
{
string errorMessage = $"Error: {e.Message}\n\nStrack trace: {e.StackTrace}";
context.AddSource(fileName, sourceCode);
}
}
|
@Bosch-Eli-Black Thanks for your answers. And one more question appears when I start working with incremental generator: Does updates in Additional files will be immediately reflected in What I want to achieve is when content one of additional files is changed or if file was added, I want my generator to run and update sources correspondinly. Currently I can update source only after the whole build, which is not what I want, unfortunately |
@QuantumDeveloper No problem! 🙂 We're actually trying to accomplish the exact same thing as you, I think 🙂 We have some In our project, we specify <AdditionalFiles Include="$(ProjectDir)examplesubfolder\*.json" />
Note that we're using In case it helps, here's what our project files look like: MyProject.csproj <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\MyGenerator\MyGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" SetTargetFramework="TargetFramework=netstandard2.0" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="$(ProjectDir)examplesubfolder\*.json" />
</ItemGroup>
<PropertyGroup>
<Example_Variable>in_case_you_need_to_pass_variables_to_your_generator</Example_Variable>
</PropertyGroup>
<ItemGroup>
<CompilerVisibleProperty Include="Example_Variable" />
</ItemGroup>
</Project> MyGenerator.csproj <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<!-- Setting `IsRoslynComponent` to `true` allows us to debug the source generator.
See https://dominikjeske.github.io/source-generators/#comment-5804484397 for debugging instructions. -->
<IsRoslynComponent>true</IsRoslynComponent>
</PropertyGroup>
</Project> |
I've filed an issue for the bug where deleting an input file doesn't cause the analyzer to be run again: #60455 |
Ah, I forgot to reply to your original question! 🙂
Do you mean that you have |
No, I mean that I have |
I tried to replicate described behavior in my demo project. I even use SourceGenerator instead Incremental and it still does not work as I expect. It creates sources only if I rebuild main project and not when I change Additional files. I will attach my small project. Maybe you can find want I am doing wrong here. |
@QuantumDeveloper Sorry, forgot to reply! I downloaded your project and gave it a try, and it seems to work on my computer 🙂 Here's what I did:
//Auto-generated code
namespace CodeGenerationDemo.Window;
public partial class MainWindow
{
public string text = "<Window59></Window59>";
}
Is that the same as what you're seeing? If not, I'm running Visual Studio 17.2.0 Preview 2.1; maybe something got fixed in the newer preview versions 🙂 |
If |
Ok, I was talking just about that :) |
No, thats the issue. You must see |
@QuantumDeveloper Whoops, sorry, I mistyped! Here's what I did:
//Auto-generated code
namespace CodeGenerationDemo.Window;
public partial class MainWindow
{
public string text = "<Window59>testing</Window59>";
} I didn't need to rebuild; as soon as I changed the Which version of Visual Studio are you using? |
@Bosch-Eli-Black I am using version 17.1.3 |
@QuantumDeveloper If you have a chance, you could try with 17.2.0 Preview 3.0, which is what I'm using 🙂 |
@QuantumDeveloper Sorry for the late reply! Just got back from vacation 🙂 Here's a GIF of what I'm seeing: Is the behaviour that you're seeing different, or are we doing different things? |
P.S. I used Screen to Gif for this 🙂 |
@Bosch-Eli-Black Sorry for long delay. When you showed me where to find generated code, I realized that my generator also regenerates code just after I change my xml, but it does not update cs file on disk until I build assembly. Bad news is that seems generators have issues with referencing another projects. Then I decided to attach all code from the refeneced project to generator and seems that it works perfectly. At least I didnt faced with described issue. Do you know how to workaround such issue? |
@QuantumDeveloper Np! 🙂 Does this help with the dependency issue: #47517 (comment) ? |
@Bosch-Eli-Black didnt try that. I am wondering does local project can be referenced via |
@QuantumDeveloper I would read through the replies to Sharwell's comment; there's some good information there about how to link to NuGet packages from an analyzer, how to link to transient NuGet packages from an analyzer, and how to link to other projects from within an analyzer. I think that specifically what you're looking for is this comment: #47517 (reply in thread) 🙂 |
@Bosch-Eli-Black After whole day of testing seems one of comments helped. My generator now working quite stable. But anyway I am still thinking that such tricky and not obvious way of linking local project to generator is a bad design and should be improved and simplified. |
@QuantumDeveloper Ya, I've been wishing for Microsoft to simplify this, too 🙂 |
@Bosch-Eli-Black Hi. Is there a way to make generator work with such assemblies? |
@QuantumDeveloper I'm afraid I don't have any experience there 🙂 I'd recommend filing a new bug. |
Sorry to revive this old question. But since this issue is still open, I just took the liberty. What if I want to generate code in |
You will have to base it purely on the types and members exported from that library. You will not be able to see the code in it. This is how compilation works. Project B is compiled into a dll, and the dll is referenced from Project A. So you won't see the code in it @ThomasHeijtink |
@CyrusNajmabadi Also, I can't imagine I'm the only one with this challenge. Are there talks in the community or issues referring to a feature request to have better support for this scenario? |
Yes. Include whatever data the generator needs to generate from into all projects that need it.
You cannot pass any data between generators. Any attempt to do so is considered undefined behavior. We are also likely to break any attempts to do that as we work on sandboxing extensions.
There are no talks on this. The advice above is the way to do it. Alternatively, since the generator doesn't need the data from the project it is in, but data from another project, don't use a Roslyn generator. Just write your own tool that runs before that project and emits what is necessary. Roslyn generators are specifically about solving the problem where the generator needs to read and change the specific project it is compiling against. |
Sorry, but I didnt find any other suitable place for asking this questions.
I have several question, which I cannot find answer for this time:
AdditionalFiles
changes? I mean can I detect that text in any of additional file was changed and automatically invoke generator to update sources?AdditionalFiles
fromIncrementSourceGenerator
? I didnt find such property there.The text was updated successfully, but these errors were encountered: