Alireza Hosseinitabar
Alireza Hosseinitabar

Reputation: 118

Why $(SolutionDir) is undefined in dotnet CLI?

I'm trying to build my project from dotnet CLI.

I'm using dotnet build and it fails.

How to reproduct:

  1. Create a simple console application using Visual Studio template

  2. Add a file named Colors.txt to the solution directory

  3. Add a file named Names.txt to the project directory

  4. Modify csproj file to include a post-build event

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
    </PropertyGroup>
    
    <Target Name="CopySettingsToOutput" AfterTargets="PostBuildEvent">
        <Exec Command="copy $(ProjectDir)\Names.txt $(TargetDir)\Names.txt" />
        <Exec Command="copy $(SolutionDir)\Colors.txt $(TargetDir)\Colors.txt" />
    </Target>
    
  5. Now build using Visual Studio (it gets built)

  6. Now build using dotnet build command and it fails. It says:

error MSB3073: The command "copy Undefined\Colors.txt C:\---path---\Colors.txt" exited with code 1.

Why $(SolutionDir) is undefined when using dotnet build?

Upvotes: 7

Views: 3164

Answers (1)

KondzioSSJ4
KondzioSSJ4

Reputation: 260

the walkaround is provide that property in dotnet command like:

$slnPath = 'directory where is solution'
dotnet build -property:SolutionDir=$slnPath

That can be usefull when you use dotnet in any build pipelines like Azure DevOps

Upvotes: 7

Related Questions