Opinionated MSBuild package for authoring source NuGet packages using standard SDK dotnet pack.
- Ships
.csfiles undercontentFiles/cs/{tfm}/...withBuildAction=Compile - Optional
build/{tfm}/*.propsand*.targetsviaSourcePackFile - Optional
SourcePackBundlefor meta-packages that re-export dependency source/build assets - Per-target-framework dependencies from plain
PackageReferenceitems - No
lib/output (IncludeBuildOutput=falseby default) - Works with class libraries, console apps, and Azure Functions source projects
Add a development dependency to the project you want to pack:
<PackageReference Include="Devlead.SourcePack" PrivateAssets="all" /><PropertyGroup>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<SourcePackRoot>Devlead/MyPackage</SourcePackRoot>
<IsPackable>true</IsPackable>
</PropertyGroup>
<PackageReference Include="Devlead.SourcePack" PrivateAssets="all" />
<SourcePackFile Include="MyPackage.props" Kind="Build" />
<SourcePackFile Include="MyPackage.targets" Kind="Build" />
<SourcePackFile Include="../../README.md" Kind="Metadata" />Pack in Release:
dotnet pack -c ReleaseFor multi-target projects (TargetFrameworks), pack single-threaded to avoid duplicate contentFiles entries:
dotnet pack -c Release -- /m:1Devlead.SourcePack sets BuildInParallel=false as an additional safeguard.
| Property | Default | Description |
|---|---|---|
EnableSourcePack |
true |
Opt out by setting false |
SourcePackRoot |
(required for auto sources) | Root folder under contentFiles/cs/{tfm}/ |
SourcePackPackConfigurations |
Release |
Configurations that pack sources |
SourcePackIncludeGeneratedUsings |
true |
Pack *.GlobalUsings.g.cs from obj/ |
SourcePackFlowBuildAssets |
true |
Before pack, sets suppressParent=None on packed dependencies so the nuspec does not get exclude="Build,Analyzers". Set false to keep SDK pack defaults. |
Re-export source and build assets from a restored dependency package into your nupkg (meta-packages / vendoring).
Add a matching PackageReference (with GeneratePathProperty="true" recommended). Bundle paths are resolved from project.assets.json, so central package management works without Version on the reference:
<PackageReference Include="Devlead.SourcePack.Sample"
PrivateAssets="all"
GeneratePathProperty="true"
Pack="false" />
<SourcePackBundle Include="Devlead.SourcePack.Sample"
PackagePathPrefix="Devlead/Bundled" />| Metadata | Default | Description |
|---|---|---|
PackagePathPrefix |
(empty) | Prefix under contentFiles/cs/{tfm}/ for bundled .cs files |
IncludeBuildAssets |
true |
Copy build/{tfm}/*.props and *.targets from the dependency |
PackagePathProperty |
Pkg{Id} |
Override the Pkg... MSBuild property name (rarely needed) |
Example output paths when bundling Devlead.SourcePack.Sample with prefix Devlead/Bundled:
contentFiles/cs/net8.0/Devlead/Bundled/Devlead/Sample/SampleService.csbuild/net8.0/Devlead.SourcePack.Sample.props
| Kind | Package path |
|---|---|
Source |
contentFiles/cs/{tfm}/{SourcePackRoot}/... |
Build |
build/{tfm}/{filename} |
Metadata |
package root (README, icon, etc.) |
dotnet run --file cake.csMIT