Repository: conan-io/conan

Issue:
USERPROFILE in conanbuildinfo.props Visual Studio files
PR https://github.com/conan-io/conan/pull/2936 was reverted due to failing things.

cc/ @pawelkami 


Discussion:

Resolving patch:
diff --git a/conans/client/generators/visualstudio.py b/conans/client/generators/visualstudio.py
--- a/conans/client/generators/visualstudio.py
+++ b/conans/client/generators/visualstudio.py
@@ -1,5 +1,8 @@
+import os
+
 from conans.model import Generator
 from conans.paths import BUILD_INFO_VISUAL_STUDIO
+import re
 
 
 class VisualStudioGenerator(Generator):
@@ -76,4 +79,8 @@ def content(self):
             'exe_flags': " ".join(self._deps_build_info.exelinkflags)
         }
         formatted_template = self.template.format(**fields)
+        userprofile = os.getenv("USERPROFILE")
+        if userprofile:
+            userprofile = userprofile.replace("\\", "/")
+            formatted_template = re.sub(userprofile, "$(USERPROFILE)", formatted_template, flags=re.I)
         return formatted_template


Test patch:
diff --git a/conans/test/generators/visual_studio_test.py b/conans/test/generators/visual_studio_test.py
--- a/conans/test/generators/visual_studio_test.py
+++ b/conans/test/generators/visual_studio_test.py
@@ -7,6 +7,10 @@
 from conans.model.conan_file import ConanFile
 from conans.model.build_info import CppInfo
 from conans.model.ref import ConanFileReference
+from conans.test.utils.test_files import temp_folder
+from conans.util.files import save
+import os
+from conans import tools
 
 
 class VisualStudioGeneratorTest(unittest.TestCase):
@@ -27,3 +31,32 @@ def valid_xml_test(self):
         self.assertIn('<PropertyGroup Label="Conan-RootDirs">', content)
         self.assertIn("<Conan-MyPkg-Root>dummy_root_folder1</Conan-MyPkg-Root>", content)
         self.assertIn("<Conan-My-Fancy-Pkg_2-Root>dummy_root_folder2</Conan-My-Fancy-Pkg_2-Root>", content)
+
+    def user_profile_test(self):
+        conanfile = ConanFile(None, None, Settings({}), None)
+        ref = ConanFileReference.loads("MyPkg/0.1@user/testing")
+        tmp_folder = temp_folder()
+        pkg1 = os.path.join(tmp_folder, "pkg1")
+        cpp_info = CppInfo(pkg1)
+        cpp_info.includedirs = ["include"]
+        save(os.path.join(pkg1, "include/file.h"), "")
+        conanfile.deps_cpp_info.update(cpp_info, ref.name)
+        ref = ConanFileReference.loads("My.Fancy-Pkg_2/0.1@user/testing")
+        pkg2 = os.path.join(tmp_folder, "pkg2")
+        cpp_info = CppInfo(pkg2)
+        cpp_info.includedirs = ["include"]
+        save(os.path.join(pkg2, "include/file.h"), "")
+        conanfile.deps_cpp_info.update(cpp_info, ref.name)
+        generator = VisualStudioGenerator(conanfile)
+
+        with tools.environment_append({"USERPROFILE": tmp_folder}):
+            content = generator.content
+            xml.etree.ElementTree.fromstring(content)
+            self.assertIn("<AdditionalIncludeDirectories>$(USERPROFILE)/pkg1/include;"
+                          "$(USERPROFILE)/pkg2/include;", content)
+
+        with tools.environment_append({"USERPROFILE": tmp_folder.upper()}):
+            content = generator.content
+            xml.etree.ElementTree.fromstring(content)
+            self.assertIn("<AdditionalIncludeDirectories>$(USERPROFILE)/pkg1/include;"
+                          "$(USERPROFILE)/pkg2/include;", content)