Search code examples
makefilemingwgnu-makemingw-w64

Makefile not exporting env var to submake unless defined in the same recipe


Please see the below Makefile:

SHELL=/bin/bash -euo pipefail

REPO_ROOT = $(shell pwd)
export VIRTUAL_ENV := ${REPO_ROOT}/venv
# bin = POSIX, Scripts = Windows
export PATH := ${VIRTUAL_ENV}/bin:${VIRTUAL_ENV}/Scripts:${PATH}

show-python:    ## Show path to python and version
    @echo -n "python location: "
    @python -c "import sys; print(sys.executable, end='')"
    @echo -n ", version: "
    @python -c "import platform; print(platform.python_version())"

install: show-python
install:    ## Install all dev dependencies into a local virtual environment.
    export VIRTUAL_ENV="${VIRTUAL_ENV}"; \
    python -m pip install -r requirements-dev.txt --progress-bar off

The install recipe only works if I manually export the environment variable VIRTUAL_ENV inside the recipe with ; chaining it into the next command. In other words, the first line of the install recipe is currently "doing something special" that I can't figure out.

What is going on here with this export being required twice?


Solution

  • (I am the OP).

    MINGW64: Didn't Work

    Per the comments, I updated the Makefile recipe:

    install: show-python
    install:    ## Install all dev dependencies into a local virtual environment.
        echo $$VIRTUAL_ENV; echo $$PATH; echo $$SHELL; echo $$BASH_VERSION
        python -m pip install -r requirements-dev.txt --progress-bar off \
            --extra-index-url $(PIP_EXTRA_INDEX_URL)
    

    It prints the below, where everything looks correct:

    echo $VIRTUAL_ENV; echo $PATH; echo $SHELL; echo $BASH_VERSION
    /c/path/to/repo/venv
    /c/path/to/repo/venv/bin:/c/path/to/repo/venv/Scripts:/c/Users/user/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/usr/bin:/mingw64/bin:/usr/bin:/c/Users/user/bin:/c/Users/user/.pyenv/pyenv-win/bin:/c/Users/user/.pyenv/pyenv-win/shims:/c/Python38/Scripts:/c/Python38:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/c/ProgramData/chocolatey/bin:/c/Program Files/dotnet:/cmd:/c/Users/user/.pyenv/pyenv-win/bin:/c/Users/user/.pyenv/pyenv-win/shims:/c/Users/user/AppData/Local/Microsoft/WindowsApps:/usr/bin/vendor_perl:/usr/bin/core_perl
    /usr/bin/bash.exe
    5.1.16(1)-release
    

    Note that I am using Git Bash from Git 2.38.1.windows.1 (Git for Windows) with MINGW64 (installed via msys2-runtime 3.3.6-1).

    Cygwin: Works

    Next, I was like maybe it's a MINGW64 issue, so let me try Cygwin (version 3.3.6-341.x86_64 which uses bash version 4.4.12(3)-release).

    Sure enough, it works out the box, no need for the pass-through export VIRTUAL_ENV="${VIRTUAL_ENV}"; \.


    Conclusions

    1. Use Cygwin instead of Git Bash
    2. I opened an issue with MINGW64: https://github.com/mingw-w64/mingw-w64/issues/16