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

Custom conditional dotnet add package #8741

Open
TangMonk opened this issue Oct 24, 2019 · 2 comments
Open

Custom conditional dotnet add package #8741

TangMonk opened this issue Oct 24, 2019 · 2 comments
Labels
Priority:3 Issues under consideration. With enough upvotes, will be reconsidered to be added to the backlog. Product:dotnet.exe Style:PackageReference Type:Feature
Milestone

Comments

@TangMonk
Copy link

TangMonk commented Oct 24, 2019

for instance:

$ dotnet add package LigerShark.WebOptimizer.TypeScript  --dev
<ItemGroup Env="Development">
    <PackageReference Include="LigerShark.WebOptimizer.Core" Version="3.0.250" />
    <PackageReference Include="LigerShark.WebOptimizer.Sass" Version="3.0.40-beta" />
    <PackageReference Include="LigerShark.WebOptimizer.TypeScript" Version="2.5.10-beta" />
</ItemGroup>

<ItemGroup Env="Production">
 <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.0.0" />
</ItemGroup>

This will avoid binary size when publish project and will avoid environment condition in Startup.cs

@TangMonk TangMonk changed the title Nuget should support load dependency for different environment Nuget should load dependency for different environment Oct 24, 2019
@dominoFire
Copy link
Contributor

dominoFire commented Oct 29, 2019

You can achieve a similar effect using the Condition attribute for MSBuild ItemGroup elements. For example:

  <PropertyGroup>
    <Env Condition="'$(Env)' == ''">Development</Env>
  </PropertyGroup>

  <ItemGroup Condition="'$(Env)' == 'Production'">
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.0.0" />
  </ItemGroup>

  <ItemGroup Condition="'$(Env)' == 'Development'">
    <PackageReference Include="LigerShark.WebOptimizer.Core" Version="3.0.250" />
    <PackageReference Include="LigerShark.WebOptimizer.Sass" Version="3.0.40-beta" />
    <PackageReference Include="LigerShark.WebOptimizer.TypeScript" Version="2.5.10-beta" />
  </ItemGroup>

When you add packages via dotnet.exe, for example:

dotnet add package NuGet.Protocol

It will add the package to the ItemGroup conditioned for the 'Development' ItemGroup, since it is the default value for the Env property, as defined in the PropertyGroup:

  <PropertyGroup>
    <Env Condition="'$(Env)' == ''">Development</Env>
  </PropertyGroup>

  <ItemGroup Condition="'$(Env)' == 'Production'">
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.0.0" />
  </ItemGroup>

  <ItemGroup Condition="'$(Env)' == 'Development'">
    <PackageReference Include="LigerShark.WebOptimizer.Core" Version="3.0.250" />
    <PackageReference Include="LigerShark.WebOptimizer.Sass" Version="3.0.40-beta" />
    <PackageReference Include="LigerShark.WebOptimizer.TypeScript" Version="2.5.10-beta" />
    <PackageReference Include="NuGet.Protocol" Version="5.3.1" />
  </ItemGroup>

In that case, you will need to manually edit the .csproj file for adding the PackageReference for different ItemGroup's

@dominoFire dominoFire added this to the Backlog milestone Oct 29, 2019
@dominoFire dominoFire changed the title Nuget should load dependency for different environment Conditional dotnet add package Oct 29, 2019
@nkolev92 nkolev92 added the Priority:3 Issues under consideration. With enough upvotes, will be reconsidered to be added to the backlog. label May 11, 2020
@Dimigergo
Copy link

Hello!

I have the same problem, but the workaround doesn't work when the nuget package is already added, and only need to update the package.

When I try to use an another workaround with choose condition the problem is the same.

    <Choose>
        <When Condition=" '$(Configuration)' == 'Debug'">
            <ItemGroup>
                <ProjectReference Include="..\MLR.Components\MLR.Components.csproj" />
            </ItemGroup>
        </When>
        <Otherwise>
            <ItemGroup>
                <PackageReference Include="MLR.Components" Version="3.0.13655" />
            </ItemGroup>
        </Otherwise>
    </Choose>

The dotnet list command simply ignore the otherwise too.
Of course the dotnet add package returns the original problem: error: Value cannot be null. (Parameter 'path1')

We use internal (company) nuget packages and build with Azure Devops Server and only can use dotnet cli. :(
Will it be a bug fix?

@nkolev92 nkolev92 changed the title Conditional dotnet add package Custom conditional dotnet add package Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority:3 Issues under consideration. With enough upvotes, will be reconsidered to be added to the backlog. Product:dotnet.exe Style:PackageReference Type:Feature
Projects
None yet
Development

No branches or pull requests

6 participants