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

Disallow orphaned VersionOverrides (opt-in) #180

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,66 @@ public void PackageVersionsAreApplied(string projectFileExtension)
});
}

[Theory]
[InlineData(".csproj")]
[InlineData(".fsproj")]
[InlineData(".vbproj")]
public void VersionOverridesWithoutCentralVersionsAreAllowed(string projectFileExtension)
{
ProjectCreator packagesProps = WritePackagesProps();

ProjectCreator.Templates
.SdkCsproj(
path: Path.Combine(TestRootPath, $"test.{projectFileExtension}"),
projectCollection: new ProjectCollection(new Dictionary<string, string>
{
// EnablePackageVersionOverrideWithoutCentralVersion is true by default
["DisableImplicitFrameworkReferences"] = "true"
}),
projectCreator: creator => creator
.ItemPackageReference(
"Orphan",
metadata: new Dictionary<string, string>
{
["VersionOverride"] = "1.0.0"
})
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeTrue(() => buildOutput.GetConsoleLog());
}

[Theory]
[InlineData(".csproj")]
[InlineData(".fsproj")]
[InlineData(".vbproj")]
public void VersionOverridesWithoutCentralVersionsCanBeDisabled(string projectFileExtension)
{
ProjectCreator packagesProps = WritePackagesProps();

ProjectCreator.Templates
.SdkCsproj(
path: Path.Combine(TestRootPath, $"test.{projectFileExtension}"),
projectCollection: new ProjectCollection(new Dictionary<string, string>
{
["DisableImplicitFrameworkReferences"] = "true",
["EnablePackageVersionOverrideWithoutCentralVersion"] = "false"
}),
projectCreator: creator => creator
.ItemPackageReference(
"Orphan",
metadata: new Dictionary<string, string>
{
["VersionOverride"] = "1.0.0"
})
.Import(Path.Combine(Environment.CurrentDirectory, @"Sdk\Sdk.targets")))
.TryBuild("CheckPackageReferences", out bool result, out BuildOutput buildOutput);

result.ShouldBeFalse(() => buildOutput.GetConsoleLog());

buildOutput.Errors.ShouldBe(new[] { $"The package reference \'Orphan\' must have a version defined in \'{packagesProps.FullPath}\'." });
}

private ProjectCreator WritePackagesProps()
{
return ProjectCreator.Templates
Expand Down
2 changes: 2 additions & 0 deletions src/CentralPackageVersions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ In some cases, you may need to override the version for a particular project. T
</Project>
```

A `PackageReference` does not have to have a corresponding central version if it has a `VersionOverride`. If you want to require a central version for each `PackageReference` in your code base, set `EnablePackageVersionOverrideWithoutCentralVersion` to `false`.

## Global Package References
Some packages should be referenced by all projects in your tree and are development dependencies only. This includes packages that do versioning, extend your build, or do any other function that is needed repository-wide. Global package references are added to the `PackageReference` item group with the following metadata:

Expand Down
20 changes: 19 additions & 1 deletion src/CentralPackageVersions/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@
<PackageReference Include="@(GlobalPackageReference)"
Condition=" '$(EnableGlobalPackageReferences)' != 'false' " />

<!--
Get a list of <PackageReference /> items that specify a VersionOverride but not a Version; that is,
there's no entry for them in the central package management file.

Clear the metadata to save some memory.
-->
<_PackageReferenceWithVersionOverrideButNoCentralVersion Include="@(PackageReference->HasMetadata('VersionOverride')->ClearMetadata())"
Exclude="@(PackageReference->HasMetadata('Version'))"
Condition=" '$(EnablePackageVersionOverride)' != 'false' And '$(EnablePackageVersionOverrideWithoutCentralVersion)' == 'false' " />

<!--
Get a list of <PackageReference /> items that specify the VersionOverride metadata and copy the value
to the Version metadata. All other metadata is also copied.
Expand Down Expand Up @@ -165,12 +175,20 @@
File="$(MSBuildProjectFullPath) "/>

<!--
Generate an error if any explicit PackageReference did not have Version specified in the central package management file.
Generate an error if any explicit PackageReference did not have Version specified in the central package management file or a VersionOverride.
-->
<Error
Text="The package reference '%(PackageReference.Identity)' must have a version defined in '$(CentralPackagesFile)'."
Condition=" '%(PackageReference.IsImplicitlyDefined)' != 'true' And '%(PackageReference.Version)' == '' "
File="$(MSBuildProjectFullPath)" />

<!--
Generate an error if any explicit PackageReference had a VersionOverride but did not have Version specified in the central package management file.
-->
<Error
Text="The package reference '%(_PackageReferenceWithVersionOverrideButNoCentralVersion.Identity)' must have a version defined in '$(CentralPackagesFile)'."
Condition=" @(_PackageReferenceWithVersionOverrideButNoCentralVersion->Count()) > 0 "
File="$(MSBuildProjectFullPath)" />
</Target>

<!--
Expand Down