Search code examples
visual-studio-2015msbuildenvironment-variablessolution

Environment variables in Visual Studio solution files


I have a Visual Studio solution which has the following entry for one specific project:

Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XXXData",
"%XXX_LIBS_DIR%\XXXData\XXXData\XXXData.csproj", "{AA390915-1F94-459F-A3D8-B2027A90A6AF}"
EndProject

This works fine in Visual Studio but falls over building with MSBUILD, as you get:

E:\path\project.sln.metaproj : error MSB2302: The project file
"E:\path\%XXX_LIBS_DIR%\XXXData\XXXData.csproj" was not found

It looks like %XXX_LIBS_DIR% is not being translated by MSBUILD so it can't find the project file.

I did try replacing %XXX_LIBS_DIR% with $(XXX_LIBS_DIR) however this doesn't work in Visual Studio.

One thought that I considered was to pre process the solution and replace %XXX_LIBS_DIR% with an absolute or relative path and see if that works, I was wondering if there was a better solution than that?


Solution

  • Update

    Usage of environment variables in .sln files through msbuild.exe is fixed on VS2019 MSBuild v16.0 based on the referenced issue.


    Environment variables in Visual Studio solution files

    This is a known issue about MSBuild, see:

    https://github.com/Microsoft/msbuild/issues/120

    And

    https://developercommunity.visualstudio.com/content/problem/248631/msbuild-doesnt-parse-environment-variables-in-sln.html

    "Currently we are using environment variables to specify where to find projects within our VS solution files. Visual Studio seems to handle this fine but when trying to build the solution via MSBuild, we get an error due to it not parsing the environment variable at all."

    MS team marked "Won't Fix" in 2011.

    So, for this issue, I am afraid there is no better solution to solve this issue than give it with an absolute or relative path.

    Hope this helps.