Skip to content

Commit

Permalink
report old tagged releases as unsupported instead of incompatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Aug 10, 2024
1 parent 4c61e0f commit 9f4364d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 25 deletions.
57 changes: 41 additions & 16 deletions src/select-zls-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ function failure(status: number, message: string): Response {
);
}

/** The versions are sorted in ascending order */
async function queryAllTaggedReleases(
env: Env,
): Promise<{ JsonData: string }[]> {
// update the "explain query plan when searching all tagged releases" test when modifying the query
return (
await env.ZIGTOOLS_DB.prepare(
"SELECT JsonData FROM ZLSReleases WHERE IsRelease = 1 ORDER BY ZLSVersionMajor DESC, ZLSVersionMinor DESC, ZLSVersionPatch DESC",
).all<{ JsonData: string }>()
).results;
}

/** `${ENDPOINT}/zls/index.json` */
export async function handleZLSIndex(
request: Request,
Expand All @@ -151,14 +163,11 @@ export async function handleZLSIndex(
return failure(500, "Internal Server Error"); // Internal Server Error
}

// update the "explain query plan when searching all tagged releases" test when modifying the query
const result = await env.ZIGTOOLS_DB.prepare(
"SELECT JsonData FROM ZLSReleases WHERE IsRelease = 1 ORDER BY ZLSVersionMajor DESC, ZLSVersionMinor DESC, ZLSVersionPatch DESC",
).all<{ JsonData: string }>();
const releases = await queryAllTaggedReleases(env);

const response: ZLSIndexResponse = {};

for (const entry of result.results) {
for (const entry of releases) {
const jsonData = JSON.parse(entry.JsonData) as D2JsonData;
response[jsonData.zlsVersion] = {
date: new Date(jsonData.date).toISOString().slice(0, 10),
Expand Down Expand Up @@ -264,10 +273,28 @@ async function selectOnTaggedRelease(
.bind(zigVersion.major, zigVersion.minor)
.first<{ JsonData: string }>();

if (selectedRelease === null)
return SelectVersionFailureCode.TaggedReleaseIncompatible;
if (selectedRelease !== null) {
return JSON.parse(selectedRelease.JsonData) as D2JsonData;
}

// If the version is older than the oldest available tagged release then the version is declared unsupported.
const releases = await queryAllTaggedReleases(env);

if (releases.length !== 0) {
const oldest = JSON.parse(releases[0].JsonData) as D2JsonData;
const oldestMinRuntimeZigVersion = SemanticVersion.parse(
oldest.minimumRuntimeZigVersion,
);
assert(oldestMinRuntimeZigVersion !== null);

if (
SemanticVersion.order(zigVersion, oldestMinRuntimeZigVersion) == Order.lt
) {
return SelectVersionFailureCode.Unsupported;
}
}

return JSON.parse(selectedRelease.JsonData) as D2JsonData;
return SelectVersionFailureCode.TaggedReleaseIncompatible;
}

function isVersionEnclosedInFailure(
Expand Down Expand Up @@ -362,16 +389,14 @@ async function selectOnDevelopmentBuild(
return SelectVersionFailureCode.DevelopmentBuildUnsupported;
}

const oldestRelease = releases[0];
const oldestReleaseData = JSON.parse(oldestRelease.JsonData) as D2JsonData;
const oldestReleaseMinimumRuntimeZigVersion = SemanticVersion.parse(
oldestReleaseData.minimumRuntimeZigVersion,
const oldestRelease = JSON.parse(releases[0].JsonData) as D2JsonData;
const oldestMinRuntimeZigVersion = SemanticVersion.parse(
oldestRelease.minimumRuntimeZigVersion,
);
assert(oldestReleaseMinimumRuntimeZigVersion !== null);
assert(oldestMinRuntimeZigVersion !== null);

if (
SemanticVersion.order(zigVersion, oldestReleaseMinimumRuntimeZigVersion) ==
Order.lt
SemanticVersion.order(zigVersion, oldestMinRuntimeZigVersion) == Order.lt
) {
return SelectVersionFailureCode.Unsupported;
}
Expand All @@ -380,7 +405,7 @@ async function selectOnDevelopmentBuild(
// versions are monotonically increasing when iterating over ordered ZLS
// versions.

let selectedEntry: D2JsonData = oldestReleaseData;
let selectedEntry: D2JsonData = oldestRelease;

for (const entry of releases) {
const data = JSON.parse(entry.JsonData) as D2JsonData;
Expand Down
13 changes: 4 additions & 9 deletions test/select-zls-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,24 +499,19 @@ describe("/v1/zls/select-version", () => {
test.each<[string, SelectVersionFailureCode, string]>([
[
"0.10.0",
SelectVersionFailureCode.TaggedReleaseIncompatible,
"ZLS 0.10.* does not exist (yet)",
SelectVersionFailureCode.Unsupported,
"Zig 0.10.0 is not supported by ZLS",
],
[
"0.10.1",
SelectVersionFailureCode.TaggedReleaseIncompatible,
"ZLS 0.10.* does not exist (yet)",
SelectVersionFailureCode.Unsupported,
"Zig 0.10.1 is not supported by ZLS",
],
[
"0.15.0",
SelectVersionFailureCode.TaggedReleaseIncompatible,
"ZLS 0.15.* does not exist (yet)",
],
[
"0.10.0",
SelectVersionFailureCode.TaggedReleaseIncompatible,
"ZLS 0.10.* does not exist (yet)",
],
[
"0.10.0-dev.5+aaaaaaaaa",
SelectVersionFailureCode.DevelopmentBuildUnsupported,
Expand Down

0 comments on commit 9f4364d

Please sign in to comment.