Skip to content

Commit a2af549

Browse files
committed
Add fallback if the cached Shards fails to run
1 parent 3d8be40 commit a2af549

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

index.js

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ async function installCrystalForLinux({crystal, shards, arch = getArch(), path})
108108
Core.info("Setting up environment for Crystal");
109109
Core.addPath(Path.join(path, "bin"));
110110
await FS.symlink("share/crystal/src", Path.join(path, "src"));
111-
if (shards === None) {
111+
if (shards !== Any) {
112112
try {
113113
await FS.unlink(Path.join(path, "bin", "shards"));
114114
} catch (e) {}
@@ -124,7 +124,7 @@ async function installCrystalForMac({crystal, shards, arch = "x86_64", path}) {
124124
Core.info("Setting up environment for Crystal");
125125
Core.addPath(Path.join(path, "embedded", "bin"));
126126
Core.addPath(Path.join(path, "bin"));
127-
if (shards === None) {
127+
if (shards !== Any) {
128128
try {
129129
await FS.unlink(Path.join(path, "embedded", "bin", "shards"));
130130
} catch (e) {}
@@ -162,17 +162,31 @@ async function installBinaryRelease({crystal, suffix, path}) {
162162
}
163163
}
164164

165-
async function maybeInstallShards({shards, path}, crystalPromise) {
165+
async function maybeInstallShards({shards, path, allowCache = true}, crystalPromise) {
166166
const allowed = [Latest, Nightly, NumericVersion, Any, None];
167+
let cached = false;
167168
checkVersion(shards, allowed);
168169
if (![Any, None].includes(shards)) {
169-
await installShards({shards, path}, crystalPromise);
170+
cached = await installShards({shards, path, allowCache}, crystalPromise);
170171
}
171172
if (shards !== None) {
172173
if (shards === Any) {
173174
await crystalPromise;
174175
}
175-
const {stdout} = await subprocess(["shards", "--version"]);
176+
let result = null;
177+
try {
178+
result = await subprocess(["shards", "--version"]);
179+
} catch (error) {
180+
if (!cached) {
181+
throw error;
182+
}
183+
Core.warning(error);
184+
Core.info("Will try to rebuild");
185+
await crystalPromise;
186+
await rebuildShards({path});
187+
result = await subprocess(["shards", "--version"]);
188+
}
189+
const {stdout} = result;
176190
const [ver] = stdout.match(/\d[^ ]+/);
177191
if (shards === Any && ver) {
178192
Core.setOutput("shards", "v" + ver);
@@ -201,12 +215,7 @@ async function installShards({shards, path}, crystalPromise) {
201215
const fetchSrcTask = downloadSource({name: "Shards", apiBase: GitHubApiBaseShards, ref});
202216
await IO.mv(await fetchSrcTask, path);
203217
await crystalPromise;
204-
205-
Core.info("Building Shards");
206-
const {stdout} = await subprocess(["make"], {cwd: path});
207-
Core.startGroup("Finished building Shards");
208-
Core.info(stdout);
209-
Core.endGroup();
218+
await rebuildShards({path});
210219
}
211220
if (restored !== cacheKey) {
212221
Core.info(`Saving cache: '${cacheKey}'`);
@@ -217,9 +226,18 @@ async function installShards({shards, path}, crystalPromise) {
217226
}
218227
}
219228

220-
await crystalPromise;
221229
Core.info("Setting up environment for Shards");
222230
Core.addPath(Path.join(path, "bin"));
231+
return !!restored;
232+
}
233+
234+
async function rebuildShards({path}) {
235+
Core.info("Building Shards");
236+
await subprocess(["make", "clean"], {cwd: path});
237+
const {stdout} = await subprocess(["make"], {cwd: path});
238+
Core.startGroup("Finished building Shards");
239+
Core.info(stdout);
240+
Core.endGroup();
223241
}
224242

225243
const GitHubApiBase = "/repos/crystal-lang/crystal";

0 commit comments

Comments
 (0)