From be775566d415d0c8252ad1602eb01ab091ce3fee Mon Sep 17 00:00:00 2001
From: Anton Troshin <anton@diagrid.io>
Date: Tue, 19 Nov 2024 21:15:12 -0600
Subject: [PATCH] try different approach by overriding go ENV GOCACHE and
 GOMODCACHE vars for Windows

Signed-off-by: Anton Troshin <anton@diagrid.io>
---
 dist/setup/index.js  | 27 ++++++++++++++++++++++++++-
 src/cache-restore.ts | 33 +++++++++++++++++++++++++++++++--
 src/main.ts          |  3 ++-
 3 files changed, 59 insertions(+), 4 deletions(-)

diff --git a/dist/setup/index.js b/dist/setup/index.js
index eea6899..1c59f69 100644
--- a/dist/setup/index.js
+++ b/dist/setup/index.js
@@ -88034,7 +88034,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
     return (mod && mod.__esModule) ? mod : { "default": mod };
 };
 Object.defineProperty(exports, "__esModule", ({ value: true }));
-exports.restoreCache = void 0;
+exports.setWindowsCacheDirectories = exports.restoreCache = void 0;
 const cache = __importStar(__nccwpck_require__(7799));
 const core = __importStar(__nccwpck_require__(2186));
 const glob = __importStar(__nccwpck_require__(8090));
@@ -88042,6 +88042,7 @@ const path_1 = __importDefault(__nccwpck_require__(1017));
 const fs_1 = __importDefault(__nccwpck_require__(7147));
 const constants_1 = __nccwpck_require__(9042);
 const cache_utils_1 = __nccwpck_require__(1678);
+const os_1 = __importDefault(__nccwpck_require__(2037));
 const restoreCache = (versionSpec, packageManager, cacheDependencyPath) => __awaiter(void 0, void 0, void 0, function* () {
     const packageManagerInfo = yield (0, cache_utils_1.getPackageManagerInfo)(packageManager);
     const platform = process.env.RUNNER_OS;
@@ -88069,6 +88070,29 @@ const restoreCache = (versionSpec, packageManager, cacheDependencyPath) => __awa
     core.info(`Cache restored from key: ${cacheKey}`);
 });
 exports.restoreCache = restoreCache;
+const setWindowsCacheDirectories = () => __awaiter(void 0, void 0, void 0, function* () {
+    if (os_1.default.platform() !== 'win32')
+        return;
+    let goCache = yield (0, cache_utils_1.getCommandOutput)(`go env GOCACHE`);
+    core.info(`GOCACHE: ${goCache}`);
+    goCache = goCache.replace('C:', 'D:').replace('c:', 'd:');
+    if (!fs_1.default.existsSync(goCache)) {
+        core.info(`${goCache} does not exist. Creating`);
+        fs_1.default.mkdirSync(goCache, { recursive: true });
+    }
+    const setOutput = yield (0, cache_utils_1.getCommandOutput)(`go env -w GOCACHE=${goCache}`);
+    core.info(`go env -w GOCACHE output: ${setOutput}`);
+    let goModCache = yield (0, cache_utils_1.getCommandOutput)(`go env GOMODCACHE`);
+    core.info(`GOMODCACHE: ${goModCache}`);
+    goModCache = goModCache.replace('C:', 'D:').replace('c:', 'd:');
+    if (!fs_1.default.existsSync(goModCache)) {
+        core.info(`${goModCache} does not exist. Creating`);
+        fs_1.default.mkdirSync(goModCache, { recursive: true });
+    }
+    const setModOutput = yield (0, cache_utils_1.getCommandOutput)(`go env -w GOMODCACHE=${goModCache}`);
+    core.info(`go env -w GOMODCACHE output: ${setModOutput}`);
+});
+exports.setWindowsCacheDirectories = setWindowsCacheDirectories;
 const findDependencyFile = (packageManager) => {
     const dependencyFile = packageManager.dependencyFilePattern;
     const workspace = process.env.GITHUB_WORKSPACE;
@@ -88701,6 +88725,7 @@ const os_1 = __importDefault(__nccwpck_require__(2037));
 function run() {
     return __awaiter(this, void 0, void 0, function* () {
         try {
+            yield (0, cache_restore_1.setWindowsCacheDirectories)();
             //
             // versionSpec is optional.  If supplied, install / use from the tool cache
             // If not supplied then problem matchers will still be setup.  Useful for self-hosted.
diff --git a/src/cache-restore.ts b/src/cache-restore.ts
index 18d930b..47898ad 100644
--- a/src/cache-restore.ts
+++ b/src/cache-restore.ts
@@ -4,9 +4,10 @@ import * as glob from '@actions/glob';
 import path from 'path';
 import fs from 'fs';
 
-import {State, Outputs} from './constants';
+import {Outputs, State} from './constants';
 import {PackageManagerInfo} from './package-managers';
-import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils';
+import {getCacheDirectoryPath, getCommandOutput, getPackageManagerInfo} from './cache-utils';
+import os from "os";
 
 export const restoreCache = async (
   versionSpec: string,
@@ -50,6 +51,34 @@ export const restoreCache = async (
   core.info(`Cache restored from key: ${cacheKey}`);
 };
 
+export const setWindowsCacheDirectories = async () => {
+  if (os.platform() !== 'win32') return;
+
+  let goCache = await getCommandOutput(`go env GOCACHE`);
+  core.info(`GOCACHE: ${goCache}`);
+  goCache = goCache.replace('C:', 'D:').replace('c:', 'd:');
+
+  if (!fs.existsSync(goCache)) {
+    core.info(`${goCache} does not exist. Creating`);
+    fs.mkdirSync(goCache, {recursive: true});
+  }
+
+  const setOutput = await getCommandOutput(`go env -w GOCACHE=${goCache}`);
+  core.info(`go env -w GOCACHE output: ${setOutput}`);
+
+  let goModCache = await getCommandOutput(`go env GOMODCACHE`);
+  core.info(`GOMODCACHE: ${goModCache}`);
+  goModCache = goModCache.replace('C:', 'D:').replace('c:', 'd:');
+
+  if (!fs.existsSync(goModCache)) {
+    core.info(`${goModCache} does not exist. Creating`);
+    fs.mkdirSync(goModCache, {recursive: true});
+  }
+
+  const setModOutput = await getCommandOutput(`go env -w GOMODCACHE=${goModCache}`);
+  core.info(`go env -w GOMODCACHE output: ${setModOutput}`);
+};
+
 const findDependencyFile = (packageManager: PackageManagerInfo) => {
   const dependencyFile = packageManager.dependencyFilePattern;
   const workspace = process.env.GITHUB_WORKSPACE!;
diff --git a/src/main.ts b/src/main.ts
index 690d277..f4de9ae 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -3,7 +3,7 @@ import * as io from '@actions/io';
 import * as installer from './installer';
 import * as semver from 'semver';
 import path from 'path';
-import {restoreCache} from './cache-restore';
+import {restoreCache, setWindowsCacheDirectories} from './cache-restore';
 import {isCacheFeatureAvailable} from './cache-utils';
 import cp from 'child_process';
 import fs from 'fs';
@@ -11,6 +11,7 @@ import os from 'os';
 
 export async function run() {
   try {
+    await setWindowsCacheDirectories();
     //
     // versionSpec is optional.  If supplied, install / use from the tool cache
     // If not supplied then problem matchers will still be setup.  Useful for self-hosted.