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

Add SG dependencies section #28054

Closed
wants to merge 3 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/csharp/roslyn-sdk/source-generators-overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ In this guide, you'll explore the creation of a source generator using the <xref

:::image type="content" source="media/source-generators/source-generated-program.png" lightbox="media/source-generators/source-generated-program.png" alt-text="Visual Studio: Auto-generated Program.g.cs file.":::

## Dependencies in source generators

If you want to add package dependencies to your source generator, you will need extra work in the *csproj* file:

- Each `PackageReference` should have `GeneratePathProperty="true"` and `PrivateAssets="all"`.
- You need to add the following target:

```xml
<Target Name="GetDependencyTargetPaths">
<ItemGroup>
<!-- Do this for every package you reference. The "PKG" prefix remains the same, while any periods in package name are replaced with underscores -->
<TargetPathWithTargetPlatformMoniker Include="$(PKGThePackageYouReference)\lib\netstandard2.0\ThePackageYouReference.dll" IncludeRuntimeDependency="false" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❗ This won't work due to the way Roslyn's assembly loader works. All assemblies must be located in the same folder on disk in order for them to be usable. Naturally, the easiest location ends up being $(OutputPath), like this:

https://github.com/KathleenDollard/JackFruit/blob/3855504030438acc9ce79e507be6b941d4d36289/JackFruitAppModel.CSharp/JackfruitAppModel.CSharp.fsproj#L37-L46

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sharwell Does this mean the source generator sample I quoted the code from is incorrect?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean the source generator sample I quoted the code from is incorrect?

Likely yes. @genlu where is the issue filed where people noticed the behavior change when we restricted assembly load locations?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the sdk itself uses the directory as the source generator sample

I believe this source generator is special and doesn't run in the same way as others. It might not be impacted.

Copy link
Member Author

@Youssef1313 Youssef1313 Feb 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think our current samples are correct here

@jmarolf The line you linked is almost the same as the sample. Sorry noticed it's totally different. I was initially paying attention to the path only. Any advantage for this approach over this one?

I'll add a note about the netstandard2.0 requirement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sharwell I tested now. The OutputPath approach didn't work while the other worked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean the source generator sample I quoted the code from is incorrect?

Likely yes. @genlu where is the issue filed where people noticed the behavior change when we restricted assembly load locations?

@sharwell In case you'd still like a link to the issue, I believe it's dotnet/roslyn#56442 🙂

</ItemGroup>
</Target>
```

- The `GetTargetPath` target should depend on the new target you added in the previous step, you do that as follows:

```xml
<PropertyGroup>
<GetTargetPathDependsOn>$(GetTargetPathDependsOn);GetDependencyTargetPaths</GetTargetPathDependsOn>
</PropertyGroup>
```

For a complete *csproj* example, see [CSharpSourceGeneratorSamples.csproj](https://github.com/dotnet/roslyn-sdk/blob/5496633deaa473b7fdcf686ac4da3a1c9a58a6f8/samples/CSharp/SourceGenerators/SourceGeneratorSamples/CSharpSourceGeneratorSamples.csproj#L13-L30).

## Next steps

The [Source Generators Cookbook](https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md) goes over some of these examples with some recommended approaches to solving them. Additionally, we have a [set of samples available on GitHub](https://github.com/dotnet/roslyn-sdk/tree/main/samples/CSharp/SourceGenerators) that you can try on your own.
Expand Down