In a .NET library's project file, how do PackageLicenseExpression, PackageReadmeFile and PackageIcon get into the .nupkg, and why do NU5039 and NU5046 appear?
answer
- SPDX expression, or a packed license file
- PackageLicenseUrl and PackageIconUrl are deprecated
- None Include, Pack=true, PackagePath
- readme: Markdown only
- icon: PNG or JPEG, 1 MB
basics
~20 sPackageLicenseExpression writes an SPDX licence into the nuspec. PackageReadmeFile and PackageIcon only name files inside the package, so each file must also be packed with a None item marked Pack="true"; forgetting that gives NU5039 for the readme and NU5046 for the icon.
solid answer
~40 sLicence: `PackageLicenseExpression` takes an SPDX expression such as `MIT` or `Apache-2.0`; a custom licence uses `PackageLicenseFile` pointing at a file inside the package; only one of expression, file and the deprecated `PackageLicenseUrl` may be set. `PackageReadmeFile` and `PackageIcon` are **paths inside the package**, not on disk: you also add `<None Include="docs/README.md" Pack="true" PackagePath="\" />` (and the same for the icon). If the file is not packed, pack fails with **NU5039** (readme) or **NU5046** (icon) — NU5030 is the licence-file equivalent. The readme must be Markdown; the icon PNG or JPEG, at most 1 MB. `PackageIconUrl` is deprecated (NU5048 when it is the only icon) and `PackageLicenseUrl` warns NU5125.
code
xml · 10 lines<PropertyGroup>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>icon.png</PackageIcon>
<PackageTags>validation;rules</PackageTags>
</PropertyGroup>
<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\" />
<None Include="..\assets\icon.png" Pack="true" PackagePath="\" />
</ItemGroup>go deeper
Recall that licence, readme and icon are MSBuild properties, and that readme and icon files must be packed with Pack="true".
Explain expression versus file licences, why readme and icon are package paths, the NU5039, NU5046 and NU5030 errors, and the deprecated URL properties.
Standardise package metadata across many libraries with Directory.Build.props, choose licence forms deliberately, and inspect the nuspec before publishing.
Set the organisation's rules for licence declarations and package presentation so consumers can rely on metadata when approving dependencies.
## Why the metadata matters A package page shows a licence, a readme and an icon, and tooling reads the licence to decide whether a package may be used at all. For the shared validation library, the platform team wants every consuming team to see what the rules are, how to call them and under which licence the code may be reused — so all three must be in the package itself, not on some wiki. ## Licence: expression or file NuGet supports exactly one of three licence forms: | Property | nuspec | When | |---|---|---| | `PackageLicenseExpression` | `<license type="expression">` | a standard SPDX identifier or expression, e.g. `MIT`, `Apache-2.0` | | `PackageLicenseFile` | `<license type="file">` | a custom or proprietary licence text shipped in the package | | `PackageLicenseUrl` | `<licenseUrl>` | **deprecated**; pack warns NU5125 | Setting more than one is not allowed. A licence file must itself be packed, exactly like a readme; if it is missing from the package, pack reports **NU5030**. An internal library that is not open source often uses `PackageLicenseFile` with the company's internal terms. ## Readme and icon are package paths The most common mistake is to read `PackageReadmeFile` as "the path to my readme on disk". It is the path **inside the package**, relative to its root. MSBuild does not add the file for you, so two lines are always needed: ```xml <PropertyGroup> <PackageReadmeFile>README.md</PackageReadmeFile> <PackageIcon>icon.png</PackageIcon> </PropertyGroup> <ItemGroup> <None Include="docs/README.md" Pack="true" PackagePath="\" /> <None Include="assets/icon.png" Pack="true" PackagePath="\" /> </ItemGroup> ``` `PackagePath="\"` puts the file at the package root, matching the property values. When the property names a file that was never packed, pack fails: - **NU5039** — "The readme file 'README.md' does not exist in the package." - **NU5046** — "The icon file 'icon.png' does not exist in the package." - **NU5030** — the same for `PackageLicenseFile`. ## Format rules - The readme must be **Markdown** (`.md`); it is the text shown on the package page. - The icon must be **JPEG or PNG**, at most **1 MB**; 128x128 is the recommended size. - `PackageIconUrl` is deprecated in favour of `PackageIcon`. Pack warns **NU5048** when the URL is the only icon; the docs still suggest setting both for clients that do not understand embedded icons. ## The rest of the package page A few more properties complete the listing and are cheap to set: 1. `Description` (or `PackageDescription`) — otherwise the page says "Package Description". 2. `PackageTags` — semicolon-separated search tags. 3. `PackageProjectUrl` — the documentation or home page. 4. `PackageReleaseNotes` — what changed in this version. 5. `PublishRepositoryUrl` set to `true` — lets Source Link publish the repository URL into the nuspec's `<repository>` element. Putting the shared ones in a `Directory.Build.props` keeps every library in the repository consistent, while each project sets its own `Description` and readme. ## Sharing metadata across a repository A repository with several libraries keeps the common fields in one place: - `Directory.Build.props` at the root sets `Authors`, `PackageLicenseExpression` (or `PackageLicenseFile`), `PackageIcon`, `PackageProjectUrl`, `PublishRepositoryUrl` and the `None` item that packs the shared icon. - Each project sets its own `Description`, `PackageTags` and `PackageReadmeFile`, with its own readme packed beside it, because a readme that describes the whole repository rarely helps someone choosing one package. - Non-library projects in the same tree set `IsPackable` to `false`, so the shared metadata never turns a sample or a tool into an accidental package. When the same icon `None` item lives in the shared props file, its `Include` path should be built from `$(MSBuildThisFileDirectory)` so it resolves from every project folder. ## Checking the result A `.nupkg` is a zip file. Opening it shows the `.nuspec` at the root with `<license>`, `<readme>` and `<icon>` elements, and the files themselves beside it. That quick look catches the classic error of a readme that pack silently placed under `content/` because `PackagePath` was omitted — in which case the `<readme>` path no longer matches and NU5039 fires.
- When would you choose PackageLicenseFile over PackageLicenseExpression?When the licence has no SPDX identifier — proprietary or custom terms, typical for an internal library. You set `PackageLicenseFile` to the file's path inside the package and pack the file with `Pack="true"`; pack reports NU5030 if it is missing. An expression and a file cannot be combined.
- Why does the docs page still suggest setting PackageIconUrl alongside PackageIcon?For downlevel clients and sources that do not support embedded icons. Pack only warns NU5048 when the URL is the sole icon; with `PackageIcon` also set, the embedded image is the one modern clients show.
- PackageReadmeFile is a path on disk, so the readme is packed automatically.
- PackageLicenseUrl remains the recommended way to declare a licence.
- You can set PackageLicenseExpression and PackageLicenseFile together for clarity.
- PackageIcon accepts any image format, including SVG, of any size.
- PackageIconUrl alone produces no warning in current NuGet.