When adding a new file to a solution from within a codefix provider using the Solution.AddAdditionalDocument
method, the file gets added with an item type of None
where an item type of AdditionalFiles
is expected.
This issue was observed using VS2015 RTM with roslyn 1.0.0
@vweijsters, can you clarify what your scenario is? What type of file is the file you're trying to add? I ask only because there's a few other bugs floating around in this area that _might_ fix your underlying need and I'd like to confirm.
I'm adding a default JSON settings file that will (in the future) be parsed by analyzers as means of configuration. The important part for me is that the added file will show up in the AnalyzerOptions.AdditionalFiles
collection.
A first version of the code is available DotNetAnalyzers/StyleCopAnalyzers#1250
The codefix provider can be found here
:+1:
@vweijsters: thanks. In your case, the settings file name is always known, right? It's always stylecop.json?
@jasonmalinowski That's correct. It opens some possibilities regarding the inclusion of a .targets file in our NuGet package, but I was trying to avoid that.
@jasonmalinowski in my case I am adding a JSON file with a different name and still expect it to be an "AdditionalFile". I am adding it in a non-PCL project but want to read it in an Analyzer/Diagnostic using the Projects AdditionalFiles method which right now returns Nothing unless I hand edit the project file.
"Projects AdditionFiles method" above should read "AnalyzerOptions.AdditionalFiles collection". Can't correct on phone.
I'm encountering this as well (same scenario as @vweijsters). Any updates?
No updates, sorry.
@twsouthwick Take a look at how we are now handling this in DotNetAnalyzers/StyleCopAnalyzers#1790. Obviously it's not ideal, but it's enough to make sure that users aren't left with completely broken functionality :smile:
Thanks @sharwell . We're already doing something similar to that. I was trying to add a codefix that would add a default config file so the VSIX version could be useful without requiring the user to open up the proj file.
Rather than try to change the nupkg so that AdditionalItems
is used instead of None
, why not just change how _stylecop.json_ is found like some other tools (including NuGet) that search up from the project directory looking for it? That way, we can put a single _stylecop.json_ in the solution root and all projects will use it automatically. If a project wants to override, have a project-local copy with changes. Lots of tools work this way, like git with _.gitignore_.
@sharwell Can you please consider @heaths 's suggestion?
@heaths @scott-xu You are assuming that the project being analyzed exists on disk, and that the file system can be used to locate a file relative to the project. Neither of these are valid assumptions in a Roslyn analyzer.
The obvious counterexample is MetadataAsSourceWorkspace
, but other useful cases can easily be described, such as analyzing the code in a specific Git commit without actually checking out that commit. The APIs that would allow analyzers to locate files within these virtual projects are not exposed to analyzers except as requested in this bug.
I was having this problem with the example CodeAnalyzer project and installing the analyzers I had made via nuget package. After wading through quite a few other project template related difficulties, I was able to get this to work by updating the nuget install.ps1 (that project template needs cleanup) to include the following at the end...
$item = $project.ProjectItems.Item("ForbiddenReferences.xml")
$item.Properties.Item("BuildAction").Value = 4
4 is the magic BuildAction for "AdditionalFiles", and it didnt require a project reload.
I'm not sure if this is useful to anyone, but going through this thread helped me get to this solution.
The workaround of this issue for what I am understanding (the above mentioned), depends on knowing the file name from the beginning. Has someone found another way? I guess this is low in the priority list.
I'm blocked on this too. I can't just update via NuGet tools because the path needs to be customized to the individual repository.
Any news from this? Really would like to add style cop to project
Any updates on this one?
This was started back in 2015 and it's still not closed? What is the progress of this item?
Any progress or update?
Bumped into the same issue. Any news on that?
Just to add a note, I thought I was running into this issue too but it turns out stylecop.json was in my .gitignore so it didn't get read by visual studio.
5 years passed, any updates? This issue is critical if you want to add your own configuration files from the Roslyn-based extension.
Most helpful comment
Rather than try to change the nupkg so that
AdditionalItems
is used instead ofNone
, why not just change how _stylecop.json_ is found like some other tools (including NuGet) that search up from the project directory looking for it? That way, we can put a single _stylecop.json_ in the solution root and all projects will use it automatically. If a project wants to override, have a project-local copy with changes. Lots of tools work this way, like git with _.gitignore_.