Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/versionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export interface Config {
* `"version"` for Zig, `"--version"` for ZLS
*/
versionArg: string;
mirrorUrls: vscode.Uri[];
getMirrorUrls: () => Promise<vscode.Uri[]>;
canonicalUrl: {
release: vscode.Uri;
nightly: vscode.Uri;
Expand Down Expand Up @@ -93,7 +93,7 @@ async function installGuarded(config: Config, version: semver.SemVer): Promise<s
throw new Error(`Can't install ${config.title} because 'tar' could not be found`);
}

const mirrors = [...config.mirrorUrls]
const mirrors = [...(await config.getMirrorUrls())]
.map((mirror) => ({ mirror, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ mirror }) => mirror);
Expand Down
44 changes: 30 additions & 14 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,33 @@ async function updateStatus(context: vscode.ExtensionContext): Promise<void> {
});
}

async function getMirrors(context: vscode.ExtensionContext): Promise<vscode.Uri[]> {
const key = "zig-mirror-cache";
let cached = context.globalState.get(key, { timestamp: 0, mirrors: "" });

const millisecondsInDay = 24 * 60 * 60 * 1000;
if (new Date().getTime() - cached.timestamp > millisecondsInDay) {
try {
const response = await fetch("https://ziglang.org/download/community-mirrors.txt");
if (response.status !== 200) throw Error("invalid mirrors");
const mirrorList = await response.text();
cached = {
timestamp: new Date().getTime(),
mirrors: mirrorList,
};
context.globalState.update(key, cached);
} catch {
// Cannot fetch mirrors, rely on cache.
}
}

return cached.mirrors
.trim()
.split("\n")
.filter((u) => !!u)
.map((u) => vscode.Uri.parse(u));
}

export async function setupZig(context: vscode.ExtensionContext) {
{
// This check can be removed once enough time has passed so that most users switched to the new value
Expand Down Expand Up @@ -674,19 +701,6 @@ export async function setupZig(context: vscode.ExtensionContext) {
break;
}

let mirrors: vscode.Uri[] = [];
try {
const response = await fetch("https://ziglang.org/download/community-mirrors.txt");
if (response.status !== 200) throw Error("invalid mirrors");
const mirrorList = await response.text();
mirrors = mirrorList
.trim()
.split("\n")
.map((u) => vscode.Uri.parse(u));
} catch {
// Cannot fetch mirrors, attempt downloading from canonical source.
}

versionManagerConfig = {
context: context,
title: "Zig",
Expand All @@ -695,7 +709,9 @@ export async function setupZig(context: vscode.ExtensionContext) {
/** https://ziglang.org/download */
minisignKey: minisign.parseKey("RWSGOq2NVecA2UPNdBUZykf1CCb147pkmdtYxgb3Ti+JO/wCYvhbAb/U"),
versionArg: "version",
mirrorUrls: mirrors,
getMirrorUrls() {
return getMirrors(context);
},
canonicalUrl: {
release: vscode.Uri.parse("https://ziglang.org/download"),
nightly: vscode.Uri.parse("https://ziglang.org/builds"),
Expand Down
4 changes: 3 additions & 1 deletion src/zls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ export async function activate(context: vscode.ExtensionContext) {
/** https://github.com/zigtools/release-worker */
minisignKey: minisign.parseKey("RWR+9B91GBZ0zOjh6Lr17+zKf5BoSuFvrx2xSeDE57uIYvnKBGmMjOex"),
versionArg: "--version",
mirrorUrls: [],
getMirrorUrls() {
return Promise.resolve([]);
},
canonicalUrl: {
release: vscode.Uri.parse("https://builds.zigtools.org"),
nightly: vscode.Uri.parse("https://builds.zigtools.org"),
Expand Down