Repository: conan-io/conan-center-index

Issue:
[package] msys2/20190524: PKG_CONFIG_PATH environment variable is not passed
The `PKG_CONFIG_PATH` environment variable is not passed tot the msys2 environment.
This causes the `pkg_config` generator not to work.

The `PKG_CONFIG_PATH` environment variable is always  `/mingw64/lib/pkgconfig:/mingw64/share/pkgconfig`

Is this a bug or am I missing something?

### Package and Environment Details (include every applicable attribute)
  * Package Name/Version: **msys2/20190524**
  * Operating System+version: **Windows 10**
  * Conan version: **conan 1.21.0**
  * Python version: **Python 3.8.0**

### Steps to reproduce (Include if Applicable)
In Windows 10, build the following recipe:
```
from conans import ConanFile, tools
import os


class DummyConan(ConanFile):
    name = "dummy"
    version = "0.1"

    requires = ""

    def build_requirements(self):
        if tools.os_info.is_windows and not "CONAN_BASH_PATH" in os.environ:
            self.build_requires("msys2/20190524")
            # self.build_requires("msys2/20161025")

    def build(self):
        env = {
            "PKG_CONFIG_PATH": "PKG_CONFIG_PATH from conan",
            "DUMMY_ENV": "DUMMY_ENV from conan",
        }
        with tools.environment_append(env):
            self.run("echo $PKG_CONFIG_PATH", win_bash=tools.os_info.is_windows)
            self.run("echo $DUMMY_ENV", win_bash=tools.os_info.is_windows)
```
(the behavior is the same for `msys2/20161025`)

This prints ` /mingw64/lib/pkgconfig:/mingw64/share/pkgconfig` for `PKG_CONFIG_PATH`.
And `DUMMY_ENV from conan` for `DUMMY_ENV`.


### Logs (Include/Attach if Applicable)
<details><summary>Click to expand log</summary>

```
dummy/0.1: Calling build()
dummy/0.1: run_in_windows_bash: C:\.conan\1982c6\1\bin\usr\bin\bash.exe --login -c ^"cd \^"/c/users/maarten/.conan/data/dummy/0.1/_/_/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\^" ^&^& PATH=\^"/c/.conan/1982c6/1/bin/usr/bin:$PATH\^" ^&^& echo $PKG_CONFIG_PATH ^"
dummy/0.1:
----Running------
> C:\.conan\1982c6\1\bin\usr\bin\bash.exe --login -c ^"cd \^"/c/users/maarten/.conan/data/dummy/0.1/_/_/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\^" ^&^& PATH=\^"/c/.conan/1982c6/1/bin/usr/bin:$PATH\^" ^&^& echo $PKG_CONFIG_PATH ^"
-----------------
dummy/0.1: /mingw64/lib/pkgconfig:/mingw64/share/pkgconfig
dummy/0.1: run_in_windows_bash: C:\.conan\1982c6\1\bin\usr\bin\bash.exe --login -c ^"cd \^"/c/users/maarten/.conan/data/dummy/0.1/_/_/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\^" ^&^& PATH=\^"/c/.conan/1982c6/1/bin/usr/bin:$PATH\^" ^&^& echo $DUMMY_ENV ^"
dummy/0.1:
----Running------
> C:\.conan\1982c6\1\bin\usr\bin\bash.exe --login -c ^"cd \^"/c/users/maarten/.conan/data/dummy/0.1/_/_/build/5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\^" ^&^& PATH=\^"/c/.conan/1982c6/1/bin/usr/bin:$PATH\^" ^&^& echo $DUMMY_ENV ^"
-----------------
dummy/0.1: DUMMY_ENV from conan
dummy/0.1: Package '5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9' built
dummy/0.1: Build folder C:\Users\maarten\.conan\data\dummy\0.1\_\_\build\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9
dummy/0.1: Generated conaninfo.txt
dummy/0.1: Generated conanbuildinfo.txt
dummy/0.1: Generating the package
dummy/0.1: Package folder C:\Users\maarten\.conan\data\dummy\0.1\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9
```

</details>



Discussion:
I can fix this by commenting out the `PKG_CONFIG_PATH` setting in `/bin/etc/profile`

Resolving patch:
diff --git a/recipes/msys2/all/conanfile.py b/recipes/msys2/all/conanfile.py
--- a/recipes/msys2/all/conanfile.py
+++ b/recipes/msys2/all/conanfile.py
@@ -37,6 +37,10 @@ def _download(self, url, sha256):
         tools.check_sha256(filename, sha256)
         return filename
 
+    @property
+    def _msys_dir(self):
+        return "msys64" if self.settings.arch_build == "x86_64" else "msys32"
+
     def build(self):
         arch = 0 if self.settings.arch_build == "x86" else 1  # index in the sources list
         url = self.conan_data["sources"][self.version][arch]["url"]
@@ -48,33 +52,34 @@ def build(self):
         os.unlink(filename)
         os.unlink(tar_name)
 
-        msys_dir = "msys64" if self.settings.arch_build == "x86_64" else "msys32"
-
         packages = []
         if self.options.packages:
             packages.extend(str(self.options.packages).split(","))
         if self.options.additional_packages:
             packages.extend(str(self.options.additional_packages).split(","))
 
-        with tools.chdir(os.path.join(msys_dir, "usr", "bin")):
+        with tools.chdir(os.path.join(self._msys_dir, "usr", "bin")):
             for package in packages:
                 self.run('bash -l -c "pacman -S %s --noconfirm"' % package)
 
         # create /tmp dir in order to avoid
         # bash.exe: warning: could not find /tmp, please create!
-        tmp_dir = os.path.join(msys_dir, 'tmp')
+        tmp_dir = os.path.join(self._msys_dir, 'tmp')
         if not os.path.isdir(tmp_dir):
             os.makedirs(tmp_dir)
         tmp_name = os.path.join(tmp_dir, 'dummy')
         with open(tmp_name, 'a'):
             os.utime(tmp_name, None)
 
+        # Prepend the PKG_CONFIG_PATH environment variable with an eventual PKG_CONFIG_PATH environment variable
+        tools.replace_in_file(os.path.join(self._msys_dir, "etc", "profile"),
+                              'PKG_CONFIG_PATH="', 'PKG_CONFIG_PATH="$PKG_CONFIG_PATH:')
+
     def package(self):
-        msys_dir = "msys64" if self.settings.arch_build == "x86_64" else "msys32"
         excludes = None
         if self.options.exclude_files:
             excludes = tuple(str(self.options.exclude_files).split(","))
-        self.copy("*", dst="bin", src=msys_dir, excludes=excludes)
+        self.copy("*", dst="bin", src=self._msys_dir, excludes=excludes)
         shutil.copytree(os.path.join(self.package_folder, "bin", "usr", "share", "licenses"),
                         os.path.join(self.package_folder, "licenses"))
 


Test patch:
diff --git a/recipes/msys2/all/test_package/conanfile.py b/recipes/msys2/all/test_package/conanfile.py
--- a/recipes/msys2/all/test_package/conanfile.py
+++ b/recipes/msys2/all/test_package/conanfile.py
@@ -1,9 +1,6 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
 from conans import ConanFile, tools
-import os
-
+from conans.errors import ConanException
+from io import StringIO
 
 class TestPackage(ConanFile):
         
@@ -18,3 +15,10 @@ def test(self):
         self.run('bash.exe -c ^"make --version^"')
         self.run('bash.exe -c ^"! test -f /bin/link^"')
         self.run('bash.exe -c ^"! test -f /usr/bin/link^"')
+
+        secret_value = "SECRET_CONAN_PKG_VARIABLE"
+        with tools.environment_append({"PKG_CONFIG_PATH": secret_value}):
+            output = StringIO()
+            self.run('bash.exe -c "echo $PKG_CONFIG_PATH"', output=output)
+            print(output.getvalue())
+            assert secret_value in output.getvalue()