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

Source generators don't run when a file in AdditionalFiles is deleted #60455

Closed
Eli-Black-Work opened this issue Mar 29, 2022 · 5 comments
Closed
Assignees
Milestone

Comments

@Eli-Black-Work
Copy link

Eli-Black-Work commented Mar 29, 2022

Version Used:
VS 17.2.0 Preview 2.0

Overview
If a source generator transforms an input file that's given to it via AdditionalFiles and then the input file is deleted, the generated source remains.

Steps to Reproduce:

Set up a project, like so:

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>
</Project>

Set up a generator, like so:

MyGenerator

[Generator]
public class ControllerGenerator : ISourceGenerator
{
    public void Execute(GeneratorExecutionContext context)
    {
        foreach (var file in context.AdditionalFiles)
        {
            string inputFilename = Path.GetFileNameWithoutExtension(file.Path);
            string outputFilename = inputFilename + ".g.cs";

            context.AddSource(outputFilename, $"class {inputFilename}");
        }
    }
}
  1. Place two files in examplesubfolder\: one.json and two.json.
  2. Compile MyProject.
  3. VS shows two generated files: one.g.cs and two.g.cs
  4. Delete two.json from examplesubfolder\
  5. VS still shows two generated files: one.g.cs and two.g.cs, even though two.json has been deleted. The only way to fix this is to restart VS. Rebuilding MyProject or doing a clean and then build doesn't work.

Expected Behavior:
When two.json is deleted, its corresponding two.g.cs file should disappear from within VS.

Actual Behavior:
When two.json is deleted, the corresponding two.g.cs file remains visible in VS.

@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 29, 2022
@Eli-Black-Work Eli-Black-Work changed the title Source generators don't hide generated C# when source file is deleted Source generators don't run when a file in AdditionalFiles is deleted Mar 29, 2022
@vatsalyaagrawal vatsalyaagrawal removed the untriaged Issues and PRs which have not yet been triaged by a lead label May 13, 2022
@vatsalyaagrawal vatsalyaagrawal added this to the 17.3 milestone May 13, 2022
@jasonmalinowski
Copy link
Member

@Bosch-Eli-Black trying this in 17.3 Preview 1.1 I do see the file disappear under the Solution Explorer and the generated content does disappear from intellisense as expected. I don't think we made any recent fixes here that would have fixed this. You're still seeing this as best you know?

@jasonmalinowski
Copy link
Member

And oh I should also say @Bosch-Eli-Black the delay for updating the solution explorer list is intentionally very slow, so it might take a few seconds after everything else has settled down.

@Eli-Black-Work
Copy link
Author

@jasonmalinowski Seems to be working for me on 17.3 Preview 1.1, too 🙂 The file disappears after 4-5 seconds.

Not sure what changed, if anything, but thanks for looking into this 🙂

@Eli-Black-Work Eli-Black-Work closed this as not planned Won't fix, can't repro, duplicate, stale May 27, 2022
@jasonmalinowski
Copy link
Member

@Bosch-Eli-Black Thanks for the follow-up!

@Eli-Black-Work
Copy link
Author

@jasonmalinowski No problem! 🙂

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

5 participants