Skip to content

Commit

Permalink
run clean-all before switching pants python version runner scripts (#…
Browse files Browse the repository at this point in the history
…7151)

### Problem

When running `./pants` vs `./pants3`, or changing python interpreter version via `PY=python3.6 ./pants` to `PY=python3.7 ./pants`, it is currently necessary to run `clean-all` first before switching which python interpreter is used to run pants. This is unfortunate, but temporary. Anyone testing with different python interpreter versions has to remember to run `clean-all` in between invocations. I find this difficult to remember.

### Solution

- Make `./pants` write the python version string to a sentinel file `.pants-python-version` at the repo root containing the python version we're running with before doing anything else, and if the version differs from the python version we are currently running pants under, run a `clean-all`.
- Add `ONLY_USING_SINGLE_PYTHON_VERSION` env var to CI to avoid running `clean-all` or needing to generate this sentinel file.

### Result

Many weird errors no longer occur when running `./pants3`.

### Followup Issues
- #7355 -- fix reporting not picking up a run if `clean-all` is one of the goals, which would let us avoid having to make two separate pants invocations here.
  • Loading branch information
cosmicexplorer authored Mar 18, 2019
1 parent 16cefff commit 0672383
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ GRTAGS
GSYMS
GTAGS
.mypy_cache/
# Stores the version of python last used to run pants. See ./pants.
/.python-interpreter-constraints
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ env:
- PANTS_CONFIG_FILES="${TRAVIS_BUILD_DIR}/pants.travis-ci.ini"
- PYTEST_PASSTHRU_ARGS="-v --duration=3"
- LC_ALL="en_US.UTF-8"
# This tells the ./pants runner script to avoid trying to clean the workspace when changing
# python versions. CI starts off without the .python-interpreter-constraints file, so it would otherwise
# run a clean-all without this env var.
- ONLY_USING_SINGLE_PYTHON_VERSION='true'
- BOOTSTRAPPED_PEX_BUCKET=ci-public.pantsbuild.org
- BOOTSTRAPPED_PEX_KEY_PREFIX=${TRAVIS_BUILD_NUMBER}/${TRAVIS_BUILD_ID}/pants.pex
- BOOTSTRAPPED_PEX_URL_PREFIX=s3://${BOOTSTRAPPED_PEX_BUCKET}/${BOOTSTRAPPED_PEX_KEY_PREFIX}
Expand Down
5 changes: 5 additions & 0 deletions build-support/docker/travis_ci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ USER ${TRAVIS_USER}:${TRAVIS_GROUP}
# Our newly created user is unlikely to have a sane environment: set a locale at least.
ENV LC_ALL="en_US.UTF-8"

# This tells the ./pants runner script to avoid trying to clean the workspace when changing python
# versions. CI starts off without the .python-interpreter-constraints file, so it would otherwise run a
# clean-all without this env var.
ENV ONLY_USING_SINGLE_PYTHON_VERSION 'true'

WORKDIR /travis/workdir
5 changes: 5 additions & 0 deletions build-support/docker/travis_ci_py27_ucs2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ USER ${TRAVIS_USER}:${TRAVIS_GROUP}
# Our newly created user is unlikely to have a sane environment: set a locale at least.
ENV LC_ALL="en_US.UTF-8"

# This tells the ./pants runner script to avoid trying to clean the workspace when changing python
# versions. CI starts off without the .python-interpreter-constraints file, so it would otherwise run a
# clean-all without this env var.
ENV ONLY_USING_SINGLE_PYTHON_VERSION 'true'

WORKDIR /travis/workdir
5 changes: 5 additions & 0 deletions build-support/docker/travis_ci_py36/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ USER ${TRAVIS_USER}:${TRAVIS_GROUP}
# Our newly created user is unlikely to have a sane environment: set a locale at least.
ENV LC_ALL="en_US.UTF-8"

# This tells the ./pants runner script to avoid trying to clean the workspace when changing python
# versions. CI starts off without the .python-interpreter-constraints file, so it would otherwise run a
# clean-all without this env var.
ENV ONLY_USING_SINGLE_PYTHON_VERSION 'true'

WORKDIR /travis/workdir
4 changes: 4 additions & 0 deletions build-support/travis/travis.yml.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ env:
- PANTS_CONFIG_FILES="${TRAVIS_BUILD_DIR}/pants.travis-ci.ini"
- PYTEST_PASSTHRU_ARGS="-v --duration=3"
- LC_ALL="en_US.UTF-8"
# This tells the ./pants runner script to avoid trying to clean the workspace when changing
# python versions. CI starts off without the .python-interpreter-constraints file, so it would otherwise
# run a clean-all without this env var.
- ONLY_USING_SINGLE_PYTHON_VERSION='true'
- BOOTSTRAPPED_PEX_BUCKET=ci-public.pantsbuild.org
- BOOTSTRAPPED_PEX_KEY_PREFIX=${TRAVIS_BUILD_NUMBER}/${TRAVIS_BUILD_ID}/pants.pex
- BOOTSTRAPPED_PEX_URL_PREFIX=s3://${BOOTSTRAPPED_PEX_BUCKET}/${BOOTSTRAPPED_PEX_KEY_PREFIX}
Expand Down
41 changes: 41 additions & 0 deletions pants
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@
# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

REPO_ROOT=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)

interpreter_constraints_sentinel_file_path='.python-interpreter-constraints'

function clean_if_interpreter_constraints_changed() {
new_interpreter_constraints="$1"

if [[ -n "${recursive_pants_shell_script_invocation:-}" ]]; then
return 0
fi

interpreter_constraints_file="${REPO_ROOT}/${interpreter_constraints_sentinel_file_path}"

if [[ -f "${interpreter_constraints_file}" ]]; then
previous_interpreter_constraints="$(cat "${interpreter_constraints_file}")"
if [[ "${new_interpreter_constraints}" == "${previous_interpreter_constraints}" ]]; then
return 0
fi
else
cat >&2 <<EOF
${interpreter_constraints_sentinel_file_path} not found in the buildroot.
Forcing an initial clean-all.
EOF
fi

export recursive_pants_shell_script_invocation='y'
cat >&2 <<EOF
Different interpreter constraints were set than were used in the previous Pants run.
Clearing the cache and preparing a Python environment with these interpreter constraints:
${new_interpreter_constraints}
EOF
./pants clean-all
echo "${new_interpreter_constraints}" > "${interpreter_constraints_file}"
unset recursive_pants_shell_script_invocation
}

# This bootstrap script runs pants from the live sources in this repo.
#
# Further support is added for projects wrapping pants with custom external extensions. In the
Expand Down Expand Up @@ -67,6 +103,11 @@ export PY="${PY:-python3}"
py_major_minor_patch=$(${PY} -c 'import sys; print(".".join(map(str, sys.version_info[0:3])))')
export PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS="${PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS:-['CPython==${py_major_minor_patch}']}"

# We can currently assume the CI environment does not switch python versions within a shard.
if [[ "${ONLY_USING_SINGLE_PYTHON_VERSION:-false}" != 'true' ]]; then
clean_if_interpreter_constraints_changed "${PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS}"
fi

PANTS_EXE="${HERE}/src/python/pants/bin/pants_loader.py"

if [[ ! -z "${WRAPPER_REQUIREMENTS}" ]]; then
Expand Down
4 changes: 4 additions & 0 deletions tests/python/pants_test/pants_run_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ def hermetic_env_whitelist(cls):
# Ensure that the underlying ./pants invocation doesn't run from sources
# (and therefore bootstrap) if we don't want it to.
'RUN_PANTS_FROM_PEX',
# This tells the ./pants runner script to avoid trying to clean the workspace when changing
# python versions. CI starts off without the .python-interpreter-constraints file, so it
# would otherwise run a clean-all without this env var.
'ONLY_USING_SINGLE_PYTHON_VERSION',
]

def setUp(self):
Expand Down

0 comments on commit 0672383

Please sign in to comment.