-
How do I reference a local project from a Source Generator project. The project is also used from normal .Net projects so cannot be marked as an Analyzer. I tried adding a normal project reference:
But I get the following error when trying to use the Source Generator:
I tried marking it's output type as Analyzer in the project reference OutputItemType as follows:
But this had exactly the same problem. All the projects have:
The anaylizer is:
and the referenced project is:
|
Beta Was this translation helpful? Give feedback.
Replies: 15 comments 59 replies
-
From the discussions on gitter both @canton7 and @YairHalberstadt have had this problem. Both @canton7 and I are currently working around this by linking in the .cs files we need, but this is a far from ideal solution. |
Beta Was this translation helpful? Give feedback.
-
I'm not sure if this restriction applies to package references as well, if so that means you can't use code from a nuget package unless that package was specifically built as an Analyzer |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
@trampster are you able to see the source generator dependency in the Analyzers node in solution explorer when you add a NuGet package reference to your source generator NuGet package? |
Beta Was this translation helpful? Give feedback.
-
That is correct, analyzers and source generator can only dependency that is itself marked as an analyzer. This is required to ensure proper execution in presence of shadow copying. We had considered a special AnalyzerDependecy item type, but that did not give us much compared to just reusing Analyzer item type for dependencies. |
Beta Was this translation helpful? Give feedback.
-
You can add a dependency to a source generator in the same solution through three steps:
You can see an example of these steps here:
Note that the source generator must target netstandard2.0, and the dependencies must target netstandard 2.0 or netstandard1.x. See #47087. |
Beta Was this translation helpful? Give feedback.
-
I had a situation alike in some regards. I have a source generator with a project reference to a utility lib. The utility lib is used in a VSIX extenstion and in a source generator in webapi project. To make this work i had to reference both the utility project and the source generator project as analyser references in the webapi project, and everything works. Now the source generator project picks up the util project without errors.
|
Beta Was this translation helpful? Give feedback.
-
with System.Text.Json 6.0.0
when running this
this never fail when doing dotnet build but visual studio 2022 fail to build... |
Beta Was this translation helpful? Give feedback.
-
I've just started experimenting with source generators, and as soon as I've began doing interesting stuff, that is, referencing useful packages, everything fell down and I wasted several hours until I found this discussion. I understand what @mavasani said here, but I agree with @thomaslevesque here. Developers will expect similar behavior compared to other projects, and not being able to reference projects and packages seamlessly severely waters down the usefulness of source generators. Furthermore, this was probably known by microsoft since the very beginning, and it should have been explained and pointed in all the introductions I've read, to prevent countless developers wasting their time I was planning to do some stuff with source generators based on the expectations I had... but seeing the state of things, to me it's clearly a no go. Maybe next time. |
Beta Was this translation helpful? Give feedback.
-
I've found this useful link about writing practical source generators, which also covers the transient project dependencies in full. I'm still not touching source generators until dotnet provides a simple way of adding the dependencies, but I provide the link for those that might find it useful: https://turnerj.com/blog/the-pain-points-of-csharp-source-generators |
Beta Was this translation helpful? Give feedback.
-
The only thing that worked for me was this: <PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup> |
Beta Was this translation helpful? Give feedback.
-
I know this thread already has an answer for package references, but for some scenarios, you would still want to add a reference to a local project with ProjectReference.
This is enough if you add your Source Generator as a NuGet package. But if you are going to reference your Source Gerantor as a project reference we should add these too:
And:
After these steps, you don't need to add anything else to the target project except the Source Generator itself |
Beta Was this translation helpful? Give feedback.
-
Nothing from this thread worked for me. Interestingly enough only incremental generator failed work. Only way I found to fix it is adding missing dll in consuming project as ANALYZER:
|
Beta Was this translation helpful? Give feedback.
-
May anyone tell me whether there is any pattern that this PKG variable has to be set with ? $(PKGNewtonsoft_Json) What value shall be set e.g for following:
I've tried with following: $(PKGSwashbuckle_AspNetCore_Annotations) (complete example below)
does it even matter? |
Beta Was this translation helpful? Give feedback.
-
Any hope of getting a proper fix for this? The solutions suggested here haven't worked for me and just seem like workarounds. Ideally, adding dependencies into a source generator would "just work". |
Beta Was this translation helpful? Give feedback.
You can add a dependency to a source generator in the same solution through three steps:
<PackageReference>
, making sure to set bothGeneratePathProperty="true"
andPrivateAssets="all"
<TargetPathWithTargetPlatformMoniker>
elements as part ofGetTargetPath
, and add all the required dependency assemblies inside this target, making sure to setIncludeRuntimeDependency="false"
<GetTargetPathDependsOn>
to ensure the target from the previous step is usedYou can see an example of these steps here:
https://github.com/dotnet/roslyn-sdk/blob/0313c80ed950ac4f4eef11bb2e1c6d1009b328c4/samples/CSharp/SourceGenerators/SourceGeneratorSamples/SourceGeneratorSampl…