Repository: conan-io/conan

Issue:
[Bug] conan export-pkg has seemingly all values from profile propagated with the exception of CONAN_CMAKE_GENERATOR
When I want to export to local cache from a local source dir, I first run the setup commands and build my project
```
conan install . -if=build -pr=myprofile
conan build . -sf=. -bf=build
```
then when I run *either* of these commands the wrong generator is used
```
conan export-pkg . user/channel -sf=. -bf=build -f
conan export-pkg . user/channel -sf=. -bf=build -pr=myprofile -f
```
Looking at the `command` variable at line 195 in the cmake.py cmake helper class, the command given to cmake is exactly the same for both versions of `export-pkg` above. The cmake command value is how I expect it to be with the exception of that the generator given in the profile is not respected, but uses the default given in conan.conf. If no default in conan.conf is given it defaults back to MinGW Makefiles.  

A workaround is to set the correct generator as default in conan.conf. But this can only be temporary as projects sometimes includes C# so a mix of generators is to be used.


Discussion:
hi @trondhe,

Could you please share here the code in the ``package()`` method of your recipe?

I do not understand why the export-pkg is conditional to `CONAN_CMAKE_GENERATOR`. Probably you are missing the ``cmake.configure()`` before a ``cmake.install()`` or something similar.

Thanks!
The setup is identical with another issue I had, and this error appeared when I solved it, as you said by having `cmake.configure()` in the package method: https://github.com/conan-io/conan/issues/4710

It was maybe a bit badly explained, but the issue appears when executing `conan export-pkg...` which then runs the package method. Then it will run the given cmake.configure() method which sets the variable `command` which is used to run cmake. This line is set at line 195 in cmake.py. This variable contains the default generator as `-G <generator`, and not the one specified in the profile given by the `conan install..`. It has also no regard for if the `conan export-pkg...` command is given a profile argument or not.
Will try to reproduce the error. Thanks for the details!

Resolving patch:
diff --git a/conans/client/cmd/export_pkg.py b/conans/client/cmd/export_pkg.py
--- a/conans/client/cmd/export_pkg.py
+++ b/conans/client/cmd/export_pkg.py
@@ -6,6 +6,7 @@
 from conans.model.manifest import FileTreeManifest
 from conans.model.ref import PackageReference
 from conans.util.files import rmdir
+from conans.model.conan_file import get_env_context_manager
 
 
 def export_pkg(cache, graph_manager, hook_manager, recorder, output,
@@ -47,9 +48,10 @@ def export_pkg(cache, graph_manager, hook_manager, recorder, output,
         packager.export_pkg(conanfile, package_id, package_folder, dest_package_folder,
                             hook_manager, conan_file_path, ref)
     else:
-        packager.create_package(conanfile, package_id, source_folder, build_folder,
-                                dest_package_folder, install_folder, hook_manager, conan_file_path,
-                                ref, local=True)
+        with get_env_context_manager(conanfile):
+            packager.create_package(conanfile, package_id, source_folder, build_folder,
+                                    dest_package_folder, install_folder, hook_manager,
+                                    conan_file_path, ref, local=True)
     with cache.package_layout(ref).update_metadata() as metadata:
         readed_manifest = FileTreeManifest.load(dest_package_folder)
         metadata.packages[package_id].revision = readed_manifest.summary_hash


Test patch:
diff --git a/conans/test/functional/command/export_pkg_test.py b/conans/test/functional/command/export_pkg_test.py
--- a/conans/test/functional/command/export_pkg_test.py
+++ b/conans/test/functional/command/export_pkg_test.py
@@ -11,6 +11,7 @@
 from conans.test.utils.tools import NO_SETTINGS_PACKAGE_ID, TestClient, TestServer
 from conans.util.env_reader import get_env
 from conans.util.files import load, mkdir
+from textwrap import dedent
 
 
 class ExportPkgTest(unittest.TestCase):
@@ -151,6 +152,25 @@ class HelloPythonConan(ConanFile):
         self.assertIn("optionOne: False", client.out)
         self.assertIn("optionOne: 123", client.out)
 
+    def test_profile_environment(self):
+        # https://github.com/conan-io/conan/issues/4832
+        conanfile = dedent("""
+            import os
+            from conans import ConanFile
+            class HelloPythonConan(ConanFile):
+                def package(self):
+                    self.output.info("ENV-VALUE: %s!!!" % os.getenv("MYCUSTOMVAR"))
+            """)
+        profile = dedent("""
+            [env]
+            MYCUSTOMVAR=MYCUSTOMVALUE
+            """)
+        client = TestClient()
+        client.save({CONANFILE: conanfile,
+                     "myprofile": profile})
+        client.run("export-pkg . Hello/0.1@lasote/stable -pr=myprofile")
+        self.assertIn("Hello/0.1@lasote/stable: ENV-VALUE: MYCUSTOMVALUE!!!", client.out)
+
     def test_options_install(self):
         # https://github.com/conan-io/conan/issues/2242
         conanfile = """from conans import ConanFile
diff --git a/conans/test/functional/generators/cmake_paths_test.py b/conans/test/functional/generators/cmake_paths_test.py
--- a/conans/test/functional/generators/cmake_paths_test.py
+++ b/conans/test/functional/generators/cmake_paths_test.py
@@ -28,7 +28,7 @@ def cmake_paths_contents_test(self):
                    '${{CMAKE_PREFIX_PATH}} ${{CMAKE_CURRENT_LIST_DIR}})'
         if platform.system() != "Windows":
             expected = expected.replace("\r", "")
-        self.assertEquals(expected.format(pfolder1=pfolder1, pfolder2=pfolder2), contents)
+        self.assertEqual(expected.format(pfolder1=pfolder1, pfolder2=pfolder2), contents)
 
     def cmake_paths_integration_test(self):
         """First package with own findHello0.cmake file"""