Skip to content

Commit

Permalink
don't consider tagged releases for old development builds
Browse files Browse the repository at this point in the history
This should report an error when querying with Zig `0.13.0-dev.1+aaaaaaa` as an example. This would previously return `0.12.0`.
  • Loading branch information
Techatrix committed Sep 15, 2024
1 parent d356add commit 5f6b66a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 37 deletions.
79 changes: 44 additions & 35 deletions src/select-zls-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,6 @@ 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 @@ -163,11 +151,14 @@ export async function handleZLSIndex(
return failure(500, "Internal Server Error"); // Internal Server Error
}

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

const response: ZLSIndexResponse = {};

for (const entry of releases) {
for (const entry of releases.results) {
const jsonData = JSON.parse(entry.JsonData) as D2JsonData;
response[jsonData.zlsVersion] = {
date: new Date(jsonData.date).toISOString().slice(0, 10),
Expand Down Expand Up @@ -266,8 +257,8 @@ async function selectOnTaggedRelease(
): Promise<D2JsonData | SelectVersionFailureCode> {
assert(zigVersion.isRelease);

// update the "explain query plan when searching on tagged release (sorted)" test when modifying the query
const selectedRelease = await env.ZIGTOOLS_DB.prepare(
// update the "explain query plan when searching on tagged release (sorted)" test when modifying the query
"SELECT JsonData FROM ZLSReleases WHERE IsRelease = 1 AND ZLSVersionMajor = ?1 AND ZLSVersionMinor = ?2 ORDER BY ZLSVersionPatch DESC",
)
.bind(zigVersion.major, zigVersion.minor)
Expand All @@ -278,10 +269,13 @@ async function selectOnTaggedRelease(
}

// If the version is older than the oldest available tagged release then the version is declared unsupported.
const releases = await queryAllTaggedReleases(env);
const oldestRelease = await env.ZIGTOOLS_DB.prepare(
// update the "explain query plan when searching all tagged releases" test when modifying the query
"SELECT JsonData FROM ZLSReleases WHERE IsRelease = 1 ORDER BY ZLSVersionMajor ASC, ZLSVersionMinor ASC, ZLSVersionPatch ASC",
).first<{ JsonData: string }>();

if (releases.length !== 0) {
const oldest = JSON.parse(releases[0].JsonData) as D2JsonData;
if (oldestRelease != null) {
const oldest = JSON.parse(oldestRelease.JsonData) as D2JsonData;
const oldestMinRuntimeZigVersion = SemanticVersion.parse(
oldest.minimumRuntimeZigVersion,
);
Expand Down Expand Up @@ -369,23 +363,34 @@ async function selectOnDevelopmentBuild(
): Promise<D2JsonData | SelectVersionFailureCode> {
assert(!zigVersion.isRelease);

const [developmentReleases, taggedReleases] = await env.ZIGTOOLS_DB.batch<{
JsonData: string;
}>([
const developmentReleases = await env.ZIGTOOLS_DB.prepare(
// update the "explain query plan when searching on development built" test when modifying the query
env.ZIGTOOLS_DB.prepare(
"SELECT JsonData FROM ZLSReleases WHERE IsRelease = 0 AND ZLSVersionMajor = ?1 AND ZLSVersionMinor = ?2 ORDER BY ZLSVersionBuildID ASC",
).bind(zigVersion.major, zigVersion.minor),
// update the "explain query plan when searching on tagged release" test when modifying the query
env.ZIGTOOLS_DB.prepare(
"SELECT JsonData FROM ZLSReleases WHERE IsRelease = 1 AND ZLSVersionMajor = ?1 AND ZLSVersionMinor = ?2",
).bind(zigVersion.major, zigVersion.minor - 1),
]);

// tagged releases come first so that development builts come first
const releases = taggedReleases.results.concat(developmentReleases.results);

if (releases.length === 0) {
"SELECT JsonData FROM ZLSReleases WHERE IsRelease = 0 AND ZLSVersionMajor = ?1 AND ZLSVersionMinor = ?2 ORDER BY ZLSVersionBuildID ASC",
).bind(zigVersion.major, zigVersion.minor).all<{ JsonData: string; }>();

let releases: { JsonData: string; }[] = [];
if (developmentReleases.results.length !== 0) {
releases = developmentReleases.results;
} else {
// This is to handle the following situtation:
// 1. Zig has tagged a new release (e.g `0.13.0`)
// 2. new Zig development builds have come out (e.g 0.13.0-dev.1+aaaaaaa)
// 3. ZLS has tagged a new release (e.g `0.13.0`)
// 4. but no ZLS development builds have come out!
//
// Querying with `0.13.0-dev.1+aaaaaaa` should return `0.13.0`. This
// should only happen for the latest tagged release while previous
// releases will report 'unsupported'.
//
// This is why only the latest tagged release is selected.
const latestTaggedRelease = await env.ZIGTOOLS_DB.prepare(
// update the "explain query plan when searching all tagged releases" test when modifying the query
"SELECT JsonData FROM ZLSReleases WHERE IsRelease = 1 ORDER BY ZLSVersionMajor DESC, ZLSVersionMinor DESC, ZLSVersionPatch DESC",
).first<{ JsonData: string; }>();
releases = latestTaggedRelease ? [latestTaggedRelease] : [];

Check failure on line 390 in src/select-zls-version.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected nullable object value in conditional. An explicit null check is required
}

if (releases.length == 0) {
return SelectVersionFailureCode.DevelopmentBuildUnsupported;
}

Expand All @@ -396,7 +401,11 @@ async function selectOnDevelopmentBuild(
);

if (SemanticVersion.order(zigVersion, oldestMinZigVersion) == Order.lt) {
return SelectVersionFailureCode.Unsupported;
if (developmentReleases.results.length == 0) {
return SelectVersionFailureCode.DevelopmentBuildUnsupported;
} else {
return SelectVersionFailureCode.Unsupported;
}
}

// The following algorithm assumes that the Zig version and tested Zig
Expand Down
22 changes: 20 additions & 2 deletions test/select-zls-version.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ describe("/v1/zls/select-version", () => {
},
);

test("search on empty database with Zig version", async () => {
test("search on empty database with tagged Zig version", async () => {
const response = await SELF.fetch(
"https://example.com/v1/zls/select-version?zig_version=0.11.0&compatibility=full",
);
Expand All @@ -409,6 +409,17 @@ describe("/v1/zls/select-version", () => {
expect(response.status).toBe(200);
});

test("search on empty database with development Zig version", async () => {
const response = await SELF.fetch(
`https://example.com/v1/zls/select-version?zig_version=${encodeURIComponent("0.11.0-dev.1+aaaaaaaaa")}&compatibility=full`,
);
expect(await response.json()).toStrictEqual<SelectVersionFailureResponse>({
code: SelectVersionFailureCode.DevelopmentBuildUnsupported,
message: "No builds for the 0.11 release cycle are currently available",
});
expect(response.status).toBe(200);
});

describe("test on sample database", () => {
beforeEach(populateDatabase);

Expand Down Expand Up @@ -472,8 +483,10 @@ describe("/v1/zls/select-version", () => {
["0.12.0-dev.18+aaaaaaaaa", "Full", "0.12.0-dev.3+aaaaaaaaa"],
["0.12.0", "Full", "0.12.1"],
["0.12.1", "Full", "0.12.1"],
["0.13.0-dev.1+aaaaaaaaa", "Full", "0.12.1"],
["0.13.0-dev.1+aaaaaaaaa", "OnlyRuntime", null],
["0.13.0", "Full", "0.13.0"],
["0.14.0-dev.3+aaaaaaaaa", "Full", "0.13.0"],
["0.14.0-dev.4+aaaaaaaaa", "Full", null],
["0.14.0", "Full", null],
["0.15.0", "Full", null],
])(
Expand Down Expand Up @@ -540,6 +553,11 @@ describe("/v1/zls/select-version", () => {
SelectVersionFailureCode.DevelopmentBuildIncompatible,
"Zig 0.12.0-dev.13+aaaaaaaaa has no compatible ZLS build (yet)",
],
[
"0.14.0-dev.10+aaaaaaaaa",
SelectVersionFailureCode.DevelopmentBuildIncompatible,
"Zig 0.14.0-dev.10+aaaaaaaaa has no compatible ZLS build (yet)",
],
])(
"Zig %s should error with '%s'",
async (zigVersion, expectedCode, expectedError) => {
Expand Down

0 comments on commit 5f6b66a

Please sign in to comment.