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

Blocks unwanted dependencies in project or solution #37349

Open
MorWalPiz opened this issue Dec 6, 2023 · 3 comments
Open

Blocks unwanted dependencies in project or solution #37349

MorWalPiz opened this issue Dec 6, 2023 · 3 comments
Assignees
Labels
Area-NetSDK needs team triage Requires a full team discussion

Comments

@MorWalPiz
Copy link

I aim to exclude specific references to a library or any transitive dependencies during the solution build, with the desired outcome being a build error.

This code is designed to function with explicit dependencies.

<!-- directory.build.target -->
<Project>
    <Target Name="RemoveDeps" AfterTargets="ResolvePackageDependenciesForBuild">
        <Error Code="420" Text="Blocked package dependency detected: %(PackageReference.Identity):%(PackageReference.Version)" 
            Condition="'%(PackageReference.Identity)' == 'Microsoft.AspNetCore.Mvc.Abstractions' and '%(PackageReference.Version)' == '2.2.0'" />
    </Target>
</Project>

If I run the build it succesfully prompt that I cannot use Microsoft.AspNetCore.Mvc.Abstractions:2.2.0.

The current process fails when attempting to check for transitive dependencies. To address this, I need to switch from using %(PackageReference) to %(PackageDependencies). However, the latter does not include any Version metadata; its value is consistently empty.

<!-- directory.build.target -->
<Project>
    <Target Name="RemoveDeps" AfterTargets="ResolvePackageDependenciesForBuild">
        <Error Code="420" Text="Blocked package dependency detected: %(PackageDependencies.Identity):%(PackageDependencies.Version)" 
            Condition="'%(PackageDependencies.Identity)' == 'Microsoft.AspNetCore.Mvc.Abstractions' and '%(PackageDependencies.Version)' == '2.2.0'" />
    </Target>
</Project>
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Dec 6, 2023
@KalleOlaviNiemitalo
Copy link
Contributor

I'd try to implement the check by parsing the output of dotnet list package --include-transitive --format=json. Or if I had to support an older version of .NET SDK that does not implement --format=json, then I'd try parsing project.assets.json even if its structure is not documented. PowerShell would be able to parse the JSON and compare versions pretty easily.

@MorWalPiz
Copy link
Author

That works, and that's how I know which package I should provide to run tests. But it's not my goal: my intention is to automate the checking process during the application build.

@KalleOlaviNiemitalo
Copy link
Contributor

IIRC some item type with "Reference" in its name lists DLL files and has metadata about NuGet package names and versions if the DLLs came from a package. But this would not let you detect packages that do not contain any DLLs.

@MiYanni MiYanni added needs team triage Requires a full team discussion and removed untriaged Request triage from a team member type-investigation labels Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-NetSDK needs team triage Requires a full team discussion
Projects
None yet
Development

No branches or pull requests

3 participants