Repository: conan-io/conan

Issue:
package command does not use modified environment
`conan build` command runs in a modified environment that among other things has a `PATH` variable that includes dependencies' binary directories. This functionality supports using tools as build requirements, for example having a build system as a build requirement.

The `conan package` command seems not to run in an environment modified in a similar way, as the command fails if the necessary tool is installed  via Conan and not through external methods.

This can be reproduced using a very simple example:

```python
# conanfile.py
from conans import ConanFile, CMake

class MyConan(ConanFile):
    build_requires = "cmake_installer/3.13.0@conan/stable"

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()
```

```cmake
# CMakeLists.txt
cmake_minimum_required(VERSION 3.12.0)

project(ConanTest)

install(CODE "message(\"installing...\")")
```

Inside the project directory run
```shell
$ conan install . --build mssing
$ conan build .
$ conan package .
```

The `conan build` command will succeed, but `conan package` will fail, unless there is a `cmake` executable installed in system.

I've tested this on Conan 1.11.1

- [x] I've read the [CONTRIBUTING guide](https://github.com/conan-io/conan/blob/develop/.github/CONTRIBUTING.md).
- [x] I've specified the Conan version, operating system version and any tool that can be relevant.
- [x] I've explained the steps to reproduce the error or the motivation/use case of the question/suggestion.


Discussion:

Resolving patch:
diff --git a/conans/client/conan_api.py b/conans/client/conan_api.py
--- a/conans/client/conan_api.py
+++ b/conans/client/conan_api.py
@@ -45,6 +45,7 @@
 from conans.client.store.localdb import LocalDB
 from conans.client.userio import UserIO
 from conans.errors import ConanException
+from conans.model.conan_file import get_env_context_manager
 from conans.model.graph_info import GraphInfo, GRAPH_INFO_FILE
 from conans.model.ref import ConanFileReference, PackageReference, check_valid_ref
 from conans.model.version import Version
@@ -654,9 +655,10 @@ def package(self, path, build_folder, package_folder, source_folder=None, instal
                                  "--build-folder and package folder can't be the same")
         conanfile = self._graph_manager.load_consumer_conanfile(conanfile_path, install_folder,
                                                                 deps_info_required=True)
-        packager.create_package(conanfile, None, source_folder, build_folder, package_folder,
-                                install_folder, self._hook_manager, conanfile_path, None,
-                                local=True, copy_info=True)
+        with get_env_context_manager(conanfile):
+            packager.create_package(conanfile, None, source_folder, build_folder, package_folder,
+                                    install_folder, self._hook_manager, conanfile_path, None,
+                                    local=True, copy_info=True)
 
     @api_method
     def source(self, path, source_folder=None, info_folder=None, cwd=None):


Test patch:
diff --git a/conans/test/functional/conan_api/package_command_test.py b/conans/test/functional/conan_api/package_command_test.py
new file mode 100644
--- /dev/null
+++ b/conans/test/functional/conan_api/package_command_test.py
@@ -0,0 +1,30 @@
+import os
+import unittest
+
+from conans.client import tools
+from conans.client.conan_api import ConanAPIV1
+from conans.test.utils.test_files import temp_folder
+
+
+class PackageCommandTest(unittest.TestCase):
+
+    def uses_recipe_env_test(self):
+        tmp_folder = temp_folder()
+        contents = conanfile.format(test_param=tmp_folder)
+        tools.save(os.path.join(tmp_folder, "conanfile.py"), contents)
+        api, _, _ = ConanAPIV1.factory()
+        api.install(tmp_folder, install_folder=tmp_folder, env=["test_param=" + tmp_folder])
+        # package will fail if recipe's environment is ignored
+        api.package(tmp_folder, tmp_folder, os.path.join(tmp_folder, "package"))
+
+
+conanfile = """\
+from conans import ConanFile, tools
+
+class Pkg(ConanFile):
+    name = "lib"
+    version = "1.0"
+
+    def package(self):
+        assert(tools.get_env("test_param") == r"{test_param}")
+"""