Skip to content

Commit 4b051f0

Browse files
committed
fix(action): copy pesde home into temp dir on windows before cache
1 parent 15a0475 commit 4b051f0

3 files changed

Lines changed: 42 additions & 28 deletions

File tree

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ A usage example as well as the default values for the accepted inputs is provide
3434
# undefined, defaults to either `$PESDE_HOME` or `~/.pesde`, where the tilde corresponds
3535
# to the home directory.
3636
#
37-
# ****IMPORTANT****
38-
# There is a caveat though: the path must be relative to `${{ github.workspace }}`,
39-
# especially for Windows, due to an internal actions limitation. This applies to
40-
# `$PESDE_HOME` as well. The tilde (`~`) and references to `$HOME` are automatically
41-
# expanded to their respective values relative to the workspace for each of the above
42-
# in order to simplify this.
37+
# The tilde (`~`) and references to `$HOME` are automatically expanded to the home
38+
# directory.
4339
home: "~/.pesde"
4440

4541
# The base directory containing the pesde project. This is utilized to figure out the

dist/index.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import path, { join, basename as basename$1, dirname } from 'node:path';
2-
import process$1, { chdir, exit } from 'node:process';
2+
import process$1, { env, chdir, exit } from 'node:process';
33
import require$$1$6, { stripVTControlCharacters, promisify, isDeepStrictEqual } from 'node:util';
44
import { mkdir as mkdir$1, mkdtemp, rm, readFile as readFile$1, access } from 'node:fs/promises';
5-
import require$$1$8, { tmpdir } from 'node:os';
5+
import require$$1$8, { tmpdir, homedir } from 'node:os';
66
import fs$1, { existsSync, appendFileSync } from 'node:fs';
77
import require$$0$4 from 'util';
88
import require$$0$5 from 'stream';
@@ -114628,22 +114628,23 @@ function requireCache () {
114628114628

114629114629
var cacheExports = requireCache();
114630114630

114631+
var ioExports = requireIo();
114632+
114631114633
const tools = {
114632114634
pesde: { owner: "pesde-pkg", repo: "pesde" },
114633114635
lune: { owner: "lune-org", repo: "lune" }
114634114636
};
114635114637
const parentLogger = logging.child({ scope: "actions" });
114636114638
parentLogger.exitOnError = true;
114637-
const PESDE_HOME = expandRelativeToWorkspace(coreExports.getInput("home") || process.env.PESDE_HOME || "~/.pesde");
114638-
await ensureExists(PESDE_HOME);
114639+
const PESDE_HOME = expandRelativeToWorkspace(coreExports.getInput("home") || env.PESDE_HOME || "~/.pesde");
114639114640
coreExports.exportVariable("PESDE_HOME", PESDE_HOME);
114640114641
parentLogger.info(`Discovered pesde home directory: ${PESDE_HOME}`);
114641114642
function expandRelativeToWorkspace(path) {
114642-
const workspaceRoot = dirname(process.env.GITHUB_WORKSPACE);
114643+
const homeDir = homedir();
114643114644
if (path === "~" || path.startsWith("~/")) {
114644-
path = path.replace(/^~(?=$|\/|\\)/, workspaceRoot);
114645+
path = path.replace(/^~(?=$|\/|\\)/, homeDir);
114645114646
}
114646-
path = path.replace(/\$HOME/g, path).replace(/\$\{HOME\}/g, path);
114647+
path = path.replace(/\$HOME/g, homeDir).replace(/\$\{HOME\}/g, homeDir);
114647114648
return path;
114648114649
}
114649114650
chdir(coreExports.getInput("cwd"));
@@ -114662,18 +114663,21 @@ async function setupTool(repo, version) {
114662114663
coreExports.addPath(toolPath);
114663114664
}
114664114665
const cacheLogger = parentLogger.child({ scope: "actions.cache" });
114666+
const pesdeHome = process.platform === "win32" ? join(dirname(env.GITHUB_WORKSPACE), ".pesde-tmp") : PESDE_HOME;
114667+
const cacheDirs = [...PESDE_PACKAGE_DIRS, pesdeHome];
114665114668
if (coreExports.getState("post") === "true") {
114666114669
if (coreExports.getState("needsCache") === "true") {
114667-
const toCache = [...PESDE_PACKAGE_DIRS, PESDE_HOME];
114670+
await ioExports.mv(PESDE_HOME, pesdeHome);
114668114671
const cacheableDirs = await Promise.all(
114669114672
// filter out dirs which do not exist and cannot be cached
114670-
toCache.map(async (p) => {
114673+
cacheDirs.map(async (p) => {
114671114674
return await access(p).then(() => p).catch(() => null);
114672114675
})
114673114676
).then((results) => results.filter((p) => p != null));
114674114677
if (cacheableDirs.length != 0) {
114675114678
const cacheId = await cacheExports.saveCache(cacheableDirs, await cacheKey());
114676114679
coreExports.saveState("needsCache", false);
114680+
if (process.platform === "win32") await ioExports.rmRF(pesdeHome);
114677114681
cacheLogger.info(`Successfully cached to ${cacheId}, exiting`);
114678114682
}
114679114683
} else {
@@ -114687,12 +114691,13 @@ if (luneVersion !== "") await setupTool(tools.lune, luneVersion);
114687114691
await setupTool(tools.pesde, coreExports.getInput("version") || "latest");
114688114692
coreExports.saveState("post", true);
114689114693
if (coreExports.getBooleanInput("cache")) {
114690-
await cacheExports.restoreCache(PESDE_PACKAGE_DIRS, await cacheKey()).then((hit) => {
114694+
await cacheExports.restoreCache(cacheDirs, await cacheKey()).then(async (hit) => {
114691114695
if (!hit) {
114692114696
cacheLogger.warn("Cache miss, dispatching future post-run to save cache");
114693114697
coreExports.saveState("needsCache", true);
114694114698
return;
114695114699
}
114700+
await ioExports.mv(pesdeHome, PESDE_HOME);
114696114701
cacheLogger.info(`Restored cache key ${hit} successfully`);
114697114702
});
114698114703
}

src/index.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { dirname } from "node:path";
2-
import { chdir, exit } from "node:process";
1+
import { dirname, join } from "node:path";
2+
import { chdir, env, exit } from "node:process";
33
import { isDeepStrictEqual } from "node:util";
44
import { access } from "node:fs/promises";
5+
import { homedir } from "node:os";
56

67
import { DownloadProvider } from "@/index.js";
78
import logging from "@/logging/index.js";
@@ -11,7 +12,7 @@ import { cacheKey, PESDE_PACKAGE_DIRS } from "./cache.js";
1112
import { cacheDir, find } from "@actions/tool-cache";
1213
import * as core from "@actions/core";
1314
import * as cache from "@actions/cache";
14-
import { ensureExists } from "@/util.js";
15+
import { mv, rmRF } from "@actions/io";
1516

1617
export type Tool = "pesde" | "lune";
1718
export type Repo = { owner: string; repo: string };
@@ -23,23 +24,21 @@ const tools: Record<Tool, Repo> = {
2324
const parentLogger = logging.child({ scope: "actions" });
2425
parentLogger.exitOnError = true;
2526

26-
const PESDE_HOME = expandRelativeToWorkspace(core.getInput("home") || process.env.PESDE_HOME || "~/.pesde");
27-
28-
await ensureExists(PESDE_HOME);
27+
const PESDE_HOME = expandRelativeToWorkspace(core.getInput("home") || env.PESDE_HOME || "~/.pesde");
2928
core.exportVariable("PESDE_HOME", PESDE_HOME);
3029

3130
parentLogger.info(`Discovered pesde home directory: ${PESDE_HOME}`);
3231

3332
function expandRelativeToWorkspace(path: string) {
34-
const workspaceRoot = dirname(process.env.GITHUB_WORKSPACE!);
33+
const homeDir = homedir();
3534

3635
// Expand tilde
3736
if (path === "~" || path.startsWith("~/")) {
38-
path = path.replace(/^~(?=$|\/|\\)/, workspaceRoot);
37+
path = path.replace(/^~(?=$|\/|\\)/, homeDir);
3938
}
4039

4140
// Expand $HOME and ${HOME}
42-
path = path.replace(/\$HOME/g, path).replace(/\$\{HOME\}/g, path);
41+
path = path.replace(/\$HOME/g, homeDir).replace(/\$\{HOME\}/g, homeDir);
4342

4443
return path;
4544
}
@@ -79,14 +78,22 @@ async function setupTool(repo: Repo, version: string) {
7978

8079
const cacheLogger = parentLogger.child({ scope: "actions.cache" });
8180

81+
// on windows, it is impossible for `saveCache` to cache unless the pesde home dir can
82+
// be represented relatively to the github workspace. so, we create a tempdir which can
83+
// be cached, and transform it during restore. on all other platforms, we just use the
84+
// regular pesde home
85+
const pesdeHome = process.platform === "win32" ? join(dirname(env.GITHUB_WORKSPACE!), ".pesde-tmp") : PESDE_HOME;
86+
const cacheDirs = [...PESDE_PACKAGE_DIRS, pesdeHome];
87+
8288
if (core.getState("post") === "true") {
8389
// post-run invocation, just cache or exit
8490

8591
if (core.getState("needsCache") === "true") {
86-
const toCache = [...PESDE_PACKAGE_DIRS, PESDE_HOME];
92+
await mv(PESDE_HOME, pesdeHome); // both paths are same everywhere except on windows
93+
8794
const cacheableDirs = await Promise.all(
8895
// filter out dirs which do not exist and cannot be cached
89-
toCache.map(async (p) => {
96+
cacheDirs.map(async (p) => {
9097
return await access(p)
9198
.then(() => p)
9299
.catch(() => null);
@@ -97,6 +104,9 @@ if (core.getState("post") === "true") {
97104
const cacheId = await cache.saveCache(cacheableDirs, await cacheKey());
98105
core.saveState("needsCache", false); // notify future runs caching isn't required
99106

107+
// remove only for windows, where it is a temp dir
108+
if (process.platform === "win32") await rmRF(pesdeHome);
109+
100110
cacheLogger.info(`Successfully cached to ${cacheId}, exiting`);
101111
}
102112
} else {
@@ -117,13 +127,16 @@ await setupTool(tools.pesde, core.getInput("version") || "latest");
117127
core.saveState("post", true);
118128

119129
if (core.getBooleanInput("cache")) {
120-
await cache.restoreCache(PESDE_PACKAGE_DIRS, await cacheKey()).then((hit) => {
130+
await cache.restoreCache(cacheDirs, await cacheKey()).then(async (hit) => {
121131
if (!hit) {
122132
cacheLogger.warn("Cache miss, dispatching future post-run to save cache");
123133
core.saveState("needsCache", true); // notify future runs that they need to cache
124134
return;
125135
}
126136

137+
// move the temporary pesde home onto the expected path (windows only)
138+
await mv(pesdeHome, PESDE_HOME);
139+
127140
cacheLogger.info(`Restored cache key ${hit} successfully`);
128141
});
129142
}

0 commit comments

Comments
 (0)