From 4f512f8afc5555958c62ab60f4884e1c354c5eb3 Mon Sep 17 00:00:00 2001
From: Hugo van Kemenade <hugovk@users.noreply.github.com>
Date: Thu, 16 Dec 2021 16:28:50 +0200
Subject: [PATCH] Include Python version in pip cache key

---
 __tests__/cache-restore.test.ts          | 6 +-----
 src/cache-distributions/cache-factory.ts | 2 +-
 src/cache-distributions/pip-cache.ts     | 9 ++++++---
 3 files changed, 8 insertions(+), 9 deletions(-)

diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts
index f3c5b01d..d982fd06 100644
--- a/__tests__/cache-restore.test.ts
+++ b/__tests__/cache-restore.test.ts
@@ -92,13 +92,9 @@ describe('restore-cache', () => {
           dependencyFile
         );
         await cacheDistributor.restoreCache();
-        let pythonKey = '';
-        if (packageManager === 'pipenv') {
-          pythonKey = `python-${pythonVersion}-`;
-        }
 
         expect(infoSpy).toHaveBeenCalledWith(
-          `Cache restored from key: setup-python-${process.env['RUNNER_OS']}-${pythonKey}${packageManager}-${fileHash}`
+          `Cache restored from key: setup-python-${process.env['RUNNER_OS']}-python-${pythonVersion}-${packageManager}-${fileHash}`
         );
       }
     );
diff --git a/src/cache-distributions/cache-factory.ts b/src/cache-distributions/cache-factory.ts
index 1a8a055f..3e363a0e 100644
--- a/src/cache-distributions/cache-factory.ts
+++ b/src/cache-distributions/cache-factory.ts
@@ -13,7 +13,7 @@ export function getCacheDistributor(
 ) {
   switch (packageManager) {
     case PackageManagers.Pip:
-      return new PipCache(cacheDependencyPath);
+      return new PipCache(pythonVersion, cacheDependencyPath);
     case PackageManagers.Pipenv:
       return new PipenvCache(pythonVersion, cacheDependencyPath);
     default:
diff --git a/src/cache-distributions/pip-cache.ts b/src/cache-distributions/pip-cache.ts
index 81b875fd..f4d7c242 100644
--- a/src/cache-distributions/pip-cache.ts
+++ b/src/cache-distributions/pip-cache.ts
@@ -8,7 +8,10 @@ import os from 'os';
 import CacheDistributor from './cache-distributor';
 
 class PipCache extends CacheDistributor {
-  constructor(cacheDependencyPath: string = '**/requirements.txt') {
+  constructor(
+    private pythonVersion: string,
+    cacheDependencyPath: string = '**/requirements.txt'
+  ) {
     super('pip', cacheDependencyPath);
   }
 
@@ -36,8 +39,8 @@ class PipCache extends CacheDistributor {
 
   protected async computeKeys() {
     const hash = await glob.hashFiles(this.cacheDependencyPath);
-    const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.packageManager}-${hash}`;
-    const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-${this.packageManager}`;
+    const primaryKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}-${hash}`;
+    const restoreKey = `${this.CACHE_KEY_PREFIX}-${process.env['RUNNER_OS']}-python-${this.pythonVersion}-${this.packageManager}`;
 
     return {
       primaryKey,