.netdeploymentdotnet-publish

dotnet publish specific project with SolutionDir reference


I'm trying to publish a specific project which is part of a solution. Many of the projects in the solution have settings which depend on the $(SolutionDir) value, but this is not available when the command line only points to the .csproj file instead of the .sln

Is there a way to inject a value to be used as $(SolutionDir) in this case?


Solution

  • short answer

    yes it's possible with a property argument -property:SolutionDir

    dotnet test Tests/Tests.csproj --logger trx --configuration Release -property:SolutionDir="C:\path_to_solution\"
    

    note the \ at the end of the path. Will save you some time :)

    note: for the azure devops the argument line looks like (in my scenario)

    -property:SolutionDir=$(Build.SourcesDirectory)/'

    long answer

    I have had problem when running tests from the CLI - azure devops would throw exceptions that files do not exist for the project.. No such exception is thrown when working from VS2022. The project basically has following config:

      <ItemGroup>
        <Content Include="$(SolutionDir)init.sql" CopyToOutputDirectory="Always" LinkBase="\" />
      </ItemGroup>
    

    so then if you just run the

    dotnet test Tests/Tests.csproj --logger trx --configuration Release
    

    error will pop-up

    Error: System.IO.FileNotFoundException: Could not find file 'C:\path_to_solution\Tests\bin\Release\net7.0\init.sql'

    see the solution in the short answer :)