snok / install-poetry

Github action for installing and configuring Poetry
MIT License
566 stars 53 forks source link

The cached poetry installation doesn't recover settings like `virtualenvs-in-project` so caching venvs breaks #150

Closed sebastian-correa closed 1 month ago

sebastian-correa commented 1 month ago

Hi! I'm using this action to install poetry like this:

name: Syle check

on: push

jobs:
  style-check:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4

      - name: Setup Python
        id: setup-python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"

      - name: Load cached Poetry installation
        id: cached-poetry
        uses: actions/cache@v4
        with:
          path: ~/.local
          key: poetry-0-${{ runner.os }}

      - name: Install Poetry
        if: steps.cached-poetry.outputs.cache-hit != 'true'
        uses: snok/install-poetry@v1
        with:
          virtualenvs-create: true
          virtualenvs-in-project: true

      - name: Load cached venv if it exists
        id: venv-cache
        uses: actions/cache@v4
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}-${{ github.workflow }}

      - name: Install dependencies
        if: steps.venv-cache.outputs.cache-hit != 'true'
        run: poetry install --no-interaction --only dev

      - name: Lint codebase
        run: poetry run ruff check

      - name: Format codebase
        run: poetry run ruff format

As you can see, I'm caching both the poetry install and the venv creation. However, any runs after the one that creates the cache entry for the poetry install don't correctly set virtualenvs-in-project, so if I change my lock file, the venv install step fails to cache (because its using a cached poetry installation that hasn't been configured to install the venv in .venv and installs it to the default location).

How can this be fixed? For now it seems I have to disable the caching of installing poetry, or add a new intermediate step that runs the correct poetry config steps. Maybe we also need to cache the output of poetry config --list --local?

sondrelg commented 1 month ago

One fix would be to run:

    run: # set your config values here
    if: steps.cached-poetry.outputs.cache-hit != 'true'

but I guess the better fix would be to figure out where the config is stored, and cache that 👍

sebastian-correa commented 1 month ago

Hi @sondrelg, do you mean that the user should figure this out and cache it, or do you mean that the action itself should be aware of this?

According to Poetry's docs, the config is stored in a different place according to the OS. In my local machine (a Mac), poetry has no configs where they say they store them though.

I tried setting the env var POETRY_CONFIG_DIR to ~/.local in my install-poetry step to see if it'd maybe work but, upon a rerun, the issue was still there. Maybe it's not enough to set POETRY_CONFIG_DIR="$INSTALL_PATH" in this repo's main.sh.

zalun commented 1 month ago

https://github.com/snok/install-poetry/pull/152

sebastian-correa commented 1 month ago

@zalun adding that would patch the issue (just as @sondrelg's suggestion would).

I think handling this in the action isn't trivial (if my suggestion above doesn't work, which I can't really test) and maybe changing the cache instructions would suffice. If @sondrelg agrees, I can open a PR adding his suggestion here

https://github.com/snok/install-poetry/blob/4e9696153ce641c9ff9ee445ffb5eb429c8f69f7/README.md?plain=1#L483-L507

sondrelg commented 1 month ago

I think for the purposes of the docs here, @zalun's fix addresses this issue, so I'll close this for now 👍 Not sure if you agree with this @sebastian-correa - if not, could you elaborate a bit on what you'd want done?

sebastian-correa commented 1 month ago

Come to think of it, @zalun's suggestion wouldn't work in the case I proposed, would it? It's the same thing, the Action runs a poetry configure ... to set the value of the virtualenv path, so it'd configure it only the first time it runs. However, upon needing to recreate the venv, poetry install will install the venv in the default location.

To fix this at the action's level, I would figure out how to tell Poetry to store its configurations in a given directory (the same directory where the action installs Poetry itself) so that caching also brings over configs. Can you guide me through making changes and testing the action myself to see if I can figure out (and contribute) some fix?

In the meantime, I opened #154 with the changes you mentioned here, which seem to work in my case.