Msbuild: $(UserProfile) does not work on macOS

Created on 3 Nov 2018  路  4Comments  路  Source: dotnet/msbuild

Overview

$(UserProfile) is often used to access the local NuGet cache (example). However on macOS, $(UserProfile) does not appear to be defined.

Steps to reproduce

Project file: test.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
    <Target Name="DisplayMessages">
     <Message Text="UserProfile: $(UserProfile)" Importance="High" />
    </Target>
</Project>

Directory contents:

/
- test.csproj

Command line

msbuild /t:DisplayMessages

Expected behavior

Prints out something like:

...
DisplayMessages:
  UserProfile: /Users/ztl8702/
...

(using my username as an example)

Actual behavior

Project "/Users/ztl8702/dev/test/test.csproj" on node 1 (DisplayMessages target(s)).
DisplayMessages:
  UserProfile: 
Done Building Project "/Users/ztl8702/dev/test/test.csproj" (DisplayMessages target(s)).

Environment data

msbuild /version output:

Microsoft (R) Build Engine version 15.7.224.30163 (xplat-master/d375bb6e Thu Sep 27 07:16:13 EDT 2018) for Mono
Copyright (C) Microsoft Corporation. All rights reserved.

15.7.224.30163

OS info:

  • macOS 10.14

If applicable, version of the tool that invokes MSBuild (Visual Studio, dotnet CLI, etc):

Most helpful comment

Another option, more precise but less understandable:

$([System.Environment]::GetFolderPath(SpecialFolder.UserProfile))

Or any of the other values for that enum: https://docs.microsoft.com/en-us/dotnet/api/system.environment.specialfolder.

All 4 comments

I just realised that on non-Windows machines there is a $(Home) which achieves the same effect. 馃榾

Is there a universal variable for the user directory that works cross-platform?

(Otherwise, feel free to close this issue)

The way this currently works is because MSBuild considers all environment variables to be global properties, so you can access them via $().

In the meantime, you could do something like:

<PropertyGroup>
  <HomeDir>$(UserProfile)</HomeDir>
  <HomeDir Condition="'$(HomeDir)' == ''">$(Home)</HomeDir>
</PropertyGroup>

and then use $(HomeDir) where necessary.
or use conditions based on the OS (`'$(OS)' == 'Windows_NT')

Another option, more precise but less understandable:

$([System.Environment]::GetFolderPath(SpecialFolder.UserProfile))

Or any of the other values for that enum: https://docs.microsoft.com/en-us/dotnet/api/system.environment.specialfolder.

Closing as the question here seems to have been answered.

Was this page helpful?
0 / 5 - 0 ratings