Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sebastian-correa opened this issue Jul 10, 2024 · 6 comments

Comments

@sebastian-correa
Copy link
Contributor

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
Copy link
Member

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
Copy link
Contributor Author

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
Copy link
Contributor

zalun commented Jul 14, 2024

#152

@sebastian-correa
Copy link
Contributor Author

sebastian-correa commented Jul 16, 2024

@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

install-poetry/README.md

Lines 483 to 507 in 4e96961

```yaml
name: test
on: pull_request
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v3
with:
path: ~/.local # the path depends on the OS
key: poetry-0 # increment to reset cache
- name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
```

@sondrelg
Copy link
Member

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 added a commit to sebastian-correa/install-poetry that referenced this issue Jul 20, 2024
@sebastian-correa
Copy link
Contributor Author

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.

sebastian-correa added a commit to sebastian-correa/install-poetry that referenced this issue Jul 23, 2024
sondrelg pushed a commit that referenced this issue Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants