From fdd38598b9245034e4bcecd29e2d8d7cb5bbdcc4 Mon Sep 17 00:00:00 2001
From: Anton Troshin <anton@diagrid.io>
Date: Fri, 7 Mar 2025 13:35:32 -0600
Subject: [PATCH] add GOTMPDIR override for Windows workflow add validation for
 GOCACHE, GOMODCACHE, and GOTMPDIR on Windows

Signed-off-by: Anton Troshin <anton@diagrid.io>
---
 .github/workflows/windows-validation.yml | 37 ++++++++++++++++++++++++
 dist/setup/index.js                      | 12 ++++++++
 src/cache-restore.ts                     | 29 +++++++++++++++++--
 3 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/.github/workflows/windows-validation.yml b/.github/workflows/windows-validation.yml
index ed05b0d..041069c 100644
--- a/.github/workflows/windows-validation.yml
+++ b/.github/workflows/windows-validation.yml
@@ -134,3 +134,40 @@ jobs:
         with:
           go-version: ${{ matrix.go }}
           cache: ${{ matrix.cache }}
+
+  go-mod-cache-and-tmp-location:
+    name: 'Validate if GOCACHE, GOMODCACHE, GOTMPDIR is set to drive D:'
+    runs-on: windows-latest
+    strategy:
+      matrix:
+        cache: [false]
+        go: [1.20.1]
+    steps:
+      - uses: actions/checkout@v4
+
+      - name: 'Setup ${{ matrix.go }}, cache: ${{ matrix.cache }}'
+        uses: ./
+        with:
+          go-version: ${{ matrix.go }}
+          cache: ${{ matrix.cache }}
+
+      - name: 'Check if go mod cache and tmp location is set correctly'
+        run: |
+          go env GOCACHE
+          go env GOMODCACHE
+          go env GOTMPDIR
+
+          if [ $(go env GOCACHE) != 'D:\Users\runneradmin\AppData\Local\go-build' ];then
+            echo 'go env GOCACHE should return "D:\Users\runneradmin\AppData\Local\go-build"'
+            exit 1
+          fi
+          if [ $(go env GOMODCACHE) != 'D:\Users\runneradmin\go\pkg\mod' ];then
+            echo 'go env GOMODCACHE should return "D:\Users\runneradmin\go\pkg\mod"'
+            exit 1
+          fi
+          if [ $(go env GOTMPDIR) != 'D:\gotmp' ];then
+            echo 'go env GOTMPDIR should return "D:\gotmp"'
+            exit 1
+          fi
+
+        shell: bash
diff --git a/dist/setup/index.js b/dist/setup/index.js
index 490c489..7afbd55 100644
--- a/dist/setup/index.js
+++ b/dist/setup/index.js
@@ -88091,6 +88091,18 @@ const setWindowsCacheDirectories = () => __awaiter(void 0, void 0, void 0, funct
     }
     const setModOutput = yield (0, cache_utils_1.getCommandOutput)(`go env -w GOMODCACHE=${goModCache}`);
     core.info(`go env -w GOMODCACHE output: ${setModOutput}`);
+    let goTmpDir = yield (0, cache_utils_1.getCommandOutput)(`go env GOTMPDIR`);
+    core.info(`GOTMPDIR: ${goTmpDir}`);
+    if (!goTmpDir || goTmpDir === '') {
+        goTmpDir = 'D:\\gotmp';
+    }
+    goTmpDir = goTmpDir.replace('C:', 'D:').replace('c:', 'd:');
+    if (!fs_1.default.existsSync(goTmpDir)) {
+        core.info(`${goTmpDir} does not exist. Creating`);
+        fs_1.default.mkdirSync(goTmpDir, { recursive: true });
+    }
+    const setGoTmpOutput = yield (0, cache_utils_1.getCommandOutput)(`go env -w GOTMPDIR=${goTmpDir}`);
+    core.info(`go env -w GOTMPDIR output: ${setGoTmpOutput}`);
 });
 exports.setWindowsCacheDirectories = setWindowsCacheDirectories;
 const findDependencyFile = (packageManager) => {
diff --git a/src/cache-restore.ts b/src/cache-restore.ts
index 47898ad..a170a2f 100644
--- a/src/cache-restore.ts
+++ b/src/cache-restore.ts
@@ -6,8 +6,12 @@ import fs from 'fs';
 
 import {Outputs, State} from './constants';
 import {PackageManagerInfo} from './package-managers';
-import {getCacheDirectoryPath, getCommandOutput, getPackageManagerInfo} from './cache-utils';
-import os from "os";
+import {
+  getCacheDirectoryPath,
+  getCommandOutput,
+  getPackageManagerInfo
+} from './cache-utils';
+import os from 'os';
 
 export const restoreCache = async (
   versionSpec: string,
@@ -75,8 +79,27 @@ export const setWindowsCacheDirectories = async () => {
     fs.mkdirSync(goModCache, {recursive: true});
   }
 
-  const setModOutput = await getCommandOutput(`go env -w GOMODCACHE=${goModCache}`);
+  const setModOutput = await getCommandOutput(
+    `go env -w GOMODCACHE=${goModCache}`
+  );
   core.info(`go env -w GOMODCACHE output: ${setModOutput}`);
+
+  let goTmpDir = await getCommandOutput(`go env GOTMPDIR`);
+  core.info(`GOTMPDIR: ${goTmpDir}`);
+  if (!goTmpDir || goTmpDir === '') {
+    goTmpDir = 'D:\\gotmp';
+  }
+  goTmpDir = goTmpDir.replace('C:', 'D:').replace('c:', 'd:');
+
+  if (!fs.existsSync(goTmpDir)) {
+    core.info(`${goTmpDir} does not exist. Creating`);
+    fs.mkdirSync(goTmpDir, {recursive: true});
+  }
+
+  const setGoTmpOutput = await getCommandOutput(
+    `go env -w GOTMPDIR=${goTmpDir}`
+  );
+  core.info(`go env -w GOTMPDIR output: ${setGoTmpOutput}`);
 };
 
 const findDependencyFile = (packageManager: PackageManagerInfo) => {