Prebuild event in Visual Studio replacing $(SolutionDir) with *Undefined*
Asked Answered
A

7

58

I believe the problem is documented here moved here and looks like it might be a bug in visual studio, but I'm wondering if anyone knows of a workaround.

Basically I have the following two lines (among other things) one right after the other in the prebuild event.

"C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "$(SolutionDir)Folder1\Project1.csproj"

"C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "$(SolutionDir)Folder2\Folder3\Project2.csproj" 

The first one succeeds and the other fails saying that The command ""C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "*Undefined*Folder2\Folder3\Project2.csproj"" exited with code 1..

Edit:
Figured out the problem was that one of the other projects with the same line as it's prebuild failed, since MSBuild itself doesn't know about the macros.

Anlace answered 11/3, 2009 at 16:35 Comment(1)
author FYI: link is broken (VS feedback).Flintshire
L
102

I fixed this by replacing all $(SolutionDir) with $(ProjectDir)..\.

It was due to MSBuild running each project independently, and thus not the solution. It worked fine in VS2010, but not on the build server.

Lh answered 17/12, 2013 at 15:29 Comment(9)
I can't believe this is still a problem in 2015Ladonna
hilariously enough, I figured this out too about 2 weeks later than you, but recently forgot I knew the answer and ended up hereLadonna
this is because it's not a problem, you are building the csproj, not the solutionPriscillaprise
This is still a problem in 2017, this answer corrects the problem.Gildagildas
This is still a problem in 2021.Mathias
Guess I'll chime in, still a problem in the year of the lord 2022!Folkestone
You know what... still a problem in 2023 as well. ;)Swett
It's still a problem in 2024. Well, it's not 2024 yet, but I'm just leaving this here pre-emptively.Summertree
The year is 2029. The machines rose from the ashes of the nuclear fire. Their war to exterminate mankind can only be stopped by one program - MSBuild building a single project.Emelda
C
37

Wasted a lot of time to find perfect solution for this problem. Use Directory.Build.props.

In your sln location folder create a file with name Directory.Build.props and put this code inside:

<Project>
 <PropertyGroup>
    <SolutionDir>$(MSBuildThisFileDirectory)</SolutionDir>
 </PropertyGroup>
</Project>

This file will be automagically picked up by all your csproj files and will define (SolutionDir) property.

Conni answered 7/3, 2020 at 14:2 Comment(4)
Worked for me in VS 2022Dorsad
This is a cleaner approach for .NET Core projects. This worked for me in VS 2022Intrepid
This is a good approach, especially if you have one solution with multiple projects that you build separately - I do this kind of thing: RUN dotnet publish --no-restore --configuration Release --output /api src/My.Comany/My.Company.Api.csproj, and then another for other csproj's in the same buildAhmednagar
using .Net 8 and this works for me in command line.Hydrothermal
P
1

Allan's answer is exactly on the right path, but if you want to move to parent directory of the ProjectDir, or the $(ProjectDir)..\ output is going to be a string, you need to perform the 'go to parent directory' operation in msbuild.
Here is how you can achieve this:

<PropertyGroup>
  <TrimmedDir>$([System.IO.Path]::GetDirectoryName($(ProjectDir)))</TrimmedDir>
</PropertyGroup>

This code will set the parent directory path of the ProjectDir to TrimmedDir variable. [System.IO.Path]::GetDirectoryName() performs this operation.

Physicist answered 16/1, 2019 at 16:55 Comment(0)
M
1

Allan's answer had the right idea, but replacing all $(SolutionDir) with $(ProjectDir)..\ did not work for me in VS 2019 16.7.2.

$(ProjectDir)..\ evaluated to the actual ProjectDir with "..\" appended to it, instead of going up one directory.

Instead, I replaced all $(SolutionDir) with $(ProjectDir)\.. and this properly went up one directory.

Mekka answered 9/9, 2021 at 1:9 Comment(0)
W
0

You don't specify if this happens for many projects or just one or two.

If it is only in one or two projects, a temporary workaround might be to replace $(SolutionDir) with the explicit path that Folder2 is located in. For example, something like:

"C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe" /p:configuration=Release;platform=x86 /t:rebuild "C:\AllSolutions\ExampleSolutions\Folder2\Folder3\Project2.csproj"

but with the correct path for your project.

Wan answered 11/3, 2009 at 17:20 Comment(0)
L
0

If you are using only MSBuild and SolutionDir works for some projects but for others not, it's possible that SolutionDir is already defined somewhere in .csproj file of the working projects.

Leptospirosis answered 4/8, 2020 at 14:19 Comment(0)
D
0

I experienced the same problem, in my case the Azure DevOps build pipeline was pointing to the project.csproj file, not the project.sln file.

This resulted in $(SolutionDir) being undefined

Tool a while to figure out; thought I'd share in case others have the same problem.

Dedra answered 18/10, 2023 at 5:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.