Repository: conda/conda

Issue:
unexpected python update with 4.6.0rc1
```
[root@847ac3bf50f9 build_scripts]# conda install imagesize
Collecting package metadata: done
Solving environment: done

## Package Plan ##

  environment location: /opt/conda

  added / updated specs:
    - imagesize


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    imagesize-1.1.0            |           py37_0           9 KB  defaults
    python-3.7.1               |       h0371630_7        36.4 MB  defaults
    ------------------------------------------------------------
                                           Total:        36.4 MB

The following NEW packages will be INSTALLED:

  imagesize          pkgs/main/linux-64::imagesize-1.1.0-py37_0

The following packages will be UPDATED:

  python                                  2.7.15-h9bab390_6 --> 3.7.1-h0371630_7


Proceed ([y]/n)? n
```

```
[root@847ac3bf50f9 build_scripts]# conda info

     active environment : None
       user config file : /root/.condarc
 populated config files : /root/.condarc
          conda version : 4.6.0rc1
    conda-build version : 3.17.6
         python version : 2.7.15.final.0
       base environment : /opt/conda  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/linux-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /opt/conda/pkgs
                          /root/.conda/pkgs
       envs directories : /opt/conda/envs
                          /root/.conda/envs
               platform : linux-64
             user-agent : conda/4.6.0rc1 requests/2.21.0 CPython/2.7.15 Linux/4.9.125-linuxkit centos/6.9 glibc/2.12
                UID:GID : 0:0
             netrc file : None
           offline mode : False
```

Reproducible by:
* ``docker run -ti conda/c3i-linux-64 bash``
* ``conda install python=2.7``
* ``conda update -c conda-canary conda``
* ``conda install imagesize``


Discussion:
I think this is a special behavior of the base environment.  Running the same workflow but creating and activating a Python 2.7 environment does not upgrade python when `conda install imagesize` is run.
Perhaps more concerning is that python in the base environment can be downgrade without updating other installed packages:

```
[root@06d038b9f267 build_scripts]# conda list conda
# packages in environment at /opt/conda:
#
# Name                    Version                   Build  Channel
anaconda-client           1.6.14                   py36_0    defaults
conda                     4.6.0rc1                 py36_0    conda-canary
conda-build               3.10.4                   py36_0    defaults
conda-concourse-ci        0.1.0                    pypi_0    pypi
conda-env                 2.6.0                h36134e3_1    defaults
conda-verify              2.0.0            py36h98955d8_0    defaults
[root@06d038b9f267 build_scripts]# conda install -c conda-canary python=2.7
Collecting package metadata: done
Solving environment: done

## Package Plan ##

  environment location: /opt/conda

  added / updated specs:
    - python=2.7


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    python-2.7.15              |       h9bab390_6        12.8 MB  defaults
    readline-7.0               |       h7b6447c_5         392 KB  defaults
    zlib-1.2.11                |       h7b6447c_3         120 KB  defaults
    ------------------------------------------------------------
                                           Total:        13.3 MB

The following packages will be UPDATED:

  readline                                   7.0-ha6073c6_4 --> 7.0-h7b6447c_5
  zlib                                    1.2.11-ha838bed_2 --> 1.2.11-h7b6447c_3

The following packages will be DOWNGRADED:

  python                                   3.6.8-h0371630_0 --> 2.7.15-h9bab390_6


Proceed ([y]/n)? 
```

```
[root@06d038b9f267 build_scripts]# conda info

     active environment : None
       user config file : /root/.condarc
 populated config files : /root/.condarc
          conda version : 4.6.0rc1
    conda-build version : 3.10.4
         python version : 3.6.8.final.0
       base environment : /opt/conda  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/linux-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /opt/conda/pkgs
                          /root/.conda/pkgs
       envs directories : /opt/conda/envs
                          /root/.conda/envs
               platform : linux-64
             user-agent : conda/4.6.0rc1 requests/2.18.4 CPython/3.6.8 Linux/4.13.0-45-generic centos/6.9 glibc/2.12
                UID:GID : 0:0
             netrc file : None
           offline mode : False
```



Reproducible by:
* docker run -ti conda/c3i-linux-64 bash
* conda update -c conda-canary conda
* conda install -c conda-canary python=2.7 

Hmmmm, @jjhelmus I run into a different issue when I try to do that with the continuum/miniconda3 docker image. 

```
# conda info

     active environment : base
    active env location : /opt/conda
            shell level : 1
       user config file : /root/.condarc
 populated config files :
          conda version : 4.6.0rc1
    conda-build version : not installed
         python version : 3.7.2.final.0
       base environment : /opt/conda  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/linux-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /opt/conda/pkgs
                          /root/.conda/pkgs
       envs directories : /opt/conda/envs
                          /root/.conda/envs
               platform : linux-64
             user-agent : conda/4.6.0rc1 requests/2.21.0 CPython/3.7.2 Linux/4.9.125-linuxkit debian/9 glibc/2.24
                UID:GID : 0:0
             netrc file : None
           offline mode : False
```

```
# conda list
# packages in environment at /opt/conda:
#
# Name                    Version                   Build  Channel
asn1crypto                0.24.0                   py37_0
ca-certificates           2018.03.07                    0
certifi                   2018.11.29               py37_0
cffi                      1.11.5           py37he75722e_1
chardet                   3.0.4                    py37_1
conda                     4.6.0rc1                 py37_0    conda-canary
conda-env                 2.6.0                         1
cryptography              2.4.2            py37h1ba5d50_0
idna                      2.8                      py37_0
imagesize                 1.1.0                    py37_0
libedit                   3.1.20170329         h6b74fdf_2
libffi                    3.2.1                hd88cf55_4
libgcc-ng                 8.2.0                hdf63c60_1
libstdcxx-ng              8.2.0                hdf63c60_1
ncurses                   6.1                  he6710b0_1
openssl                   1.1.1a               h7b6447c_0
pip                       18.1                     py37_0
pycosat                   0.6.3            py37h14c3975_0
pycparser                 2.19                     py37_0
pyopenssl                 18.0.0                   py37_0
pysocks                   1.6.8                    py37_0
python                    3.7.2                h0371630_0
readline                  7.0                  h7b6447c_5
requests                  2.21.0                   py37_0
ruamel_yaml               0.15.46          py37h14c3975_0
setuptools                40.6.3                   py37_0
six                       1.12.0                   py37_0
sqlite                    3.26.0               h7b6447c_0
tk                        8.6.8                hbc83047_0
urllib3                   1.24.1                   py37_0
wheel                     0.32.3                   py37_0
xz                        5.2.4                h14c3975_4
yaml                      0.1.7                had09818_2
zlib                      1.2.11               h7b6447c_3
(base) root@14700af21dc1:/# conda install python=2.7 -c conda-canary
Collecting package metadata: done
Solving environment: done

## Package Plan ##

  environment location: /opt/conda

  added / updated specs:
    - python=2.7


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2018.11.29         |           py27_0         146 KB
    pip-18.1                   |           py27_0         1.8 MB
    python-2.7.15              |       h9bab390_6        12.8 MB
    setuptools-40.6.3          |           py27_0         627 KB
    wheel-0.32.3               |           py27_0          34 KB
    ------------------------------------------------------------
                                           Total:        15.3 MB

The following packages will be DOWNGRADED:

  certifi                                 2018.11.29-py37_0 --> 2018.11.29-py27_0
  pip                                           18.1-py37_0 --> 18.1-py27_0
  python                                   3.7.2-h0371630_0 --> 2.7.15-h9bab390_6
  setuptools                                  40.6.3-py37_0 --> 40.6.3-py27_0
  wheel                                       0.32.3-py37_0 --> 0.32.3-py27_0


Proceed ([y]/n)? y


Downloading and Extracting Packages
python-2.7.15        | 12.8 MB   | ################################################################################################################ | 100%
certifi-2018.11.29   | 146 KB    | ################################################################################################################ | 100%
pip-18.1             | 1.8 MB    | ################################################################################################################ | 100%
setuptools-40.6.3    | 627 KB    | ################################################################################################################ | 100%
wheel-0.32.3         | 34 KB     | ################################################################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Traceback (most recent call last):
  File "/opt/conda/bin/conda", line 12, in <module>
    from conda.cli import main
ImportError: No module named conda.cli
```

Reproducible by:
* docker run -it continuumio/miniconda3 bash
* conda update -c conda-canary conda
* conda install python=2.7 -c conda-canary

It seems as if conda 4.6.0rc1 believes the environment is in an inconsistent state and therefore removes the inconsistent packages from the SAT solve:

```
[root@06d038b9f267 build_scripts]# CONDA_DEBUG=1 conda install -c conda-canary python=2.7
...
...
DEBUG conda.core.solve:_find_inconsistent_packages(345): inconsistent precs: 
  - defaults/linux-64::git==2.19.1=pl526hacde149_0
  - defaults/linux-64::cryptography==2.4.2=py36h1ba5d50_0
  - defaults/linux-64::krb5==1.16.1=h173b8e3_7
...
...
DEBUG conda.resolve:get_reduced_index(324): Retrieving packages for: 
  - binutils_impl_linux-64
  - binutils_linux-64
  - ca-certificates
  - conda-env
  - expat
  - gcc_impl_linux-64
  - gcc_linux-64
  - gxx_impl_linux-64
  - gxx_linux-64
  - libedit
  - libgcc-ng
  - libssh2
  - libstdcxx-ng
  - ncurses
  - openssl
  - patchelf
  - perl
  - python=2.7
  - sqlite
  - xz
...
```
> Hmmmm, @jjhelmus I run into a different issue when I try to do that with the continuum/miniconda3 docker image.

Different issue but looks to be the same cause, conda thinks the environment is inconsistent:

```
DEBUG conda.core.solve:_find_inconsistent_packages(345): inconsistent precs: 
  - conda-canary/linux-64::conda==4.6.0rc1=py37_0
  - defaults/linux-64::pycparser==2.19=py37_0
```
The environment in question ARE inconsistent when the information in conda-meta is used because the libffi package has incorrect depends information:

```
# cat /opt/conda/conda-meta/libffi-3.2.1-hd88cf55_4.json 
{
  "arch": "x86_64",
  "build": "hd88cf55_4",
  "build_number": 4,
  "depends": [
    "libgcc-ng 7.2.0.*",
    "libstdcxx-ng 7.2.0.*"
  ],
```

The dependencies should be `libgcc-ng >= 7.2.0` and `libstdcxx >=7.2.0`. 

I'm not sure why conda 4.5.12 ignores this dependency.

Resolving patch:
diff --git a/conda/core/solve.py b/conda/core/solve.py
--- a/conda/core/solve.py
+++ b/conda/core/solve.py
@@ -340,6 +340,14 @@ def _find_inconsistent_packages(self, ssc):
         # we remove now from consideration the set of packages causing inconsistencies,
         # and then we add them back in following the main SAT call.
         _, inconsistent_precs = ssc.r.bad_installed(ssc.solution_precs, ())
+        if inconsistent_precs:
+            # It is possible that the package metadata is incorrect, for example when
+            # un-patched metadata from the Miniconda or Anaconda installer is present, see:
+            # https://github.com/conda/conda/issues/8076
+            # Update the metadata with information from the index and see if that makes the
+            # environment consistent.
+            ssc.solution_precs = tuple(ssc.index.get(k, k) for k in ssc.solution_precs)
+            _, inconsistent_precs = ssc.r.bad_installed(ssc.solution_precs, ())
         if log.isEnabledFor(DEBUG):
             log.debug("inconsistent precs: %s",
                       dashlist(inconsistent_precs) if inconsistent_precs else 'None')


Test patch: