From a411cab939d764da8dba284223d2756ef30b1afb Mon Sep 17 00:00:00 2001 From: Alitzel Mendez <6895254+AlitzelMendez@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:02:49 -0800 Subject: [PATCH] No matching versions found (#1838) issue: https://github.com/Azure/typespec-azure/issues/1647 --- ...orest-version-error-2024-10-14-17-31-30.md | 7 +++++ packages/typespec-autorest/src/emit.ts | 6 +++- packages/typespec-autorest/src/lib.ts | 7 +++++ .../typespec-autorest/test/versioning.test.ts | 28 ++++++++++++++++++- 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 .chronus/changes/fix-autorest-version-error-2024-10-14-17-31-30.md diff --git a/.chronus/changes/fix-autorest-version-error-2024-10-14-17-31-30.md b/.chronus/changes/fix-autorest-version-error-2024-10-14-17-31-30.md new file mode 100644 index 0000000000..d606e76c98 --- /dev/null +++ b/.chronus/changes/fix-autorest-version-error-2024-10-14-17-31-30.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@azure-tools/typespec-autorest" +--- + +Emit error `@azure-tools/typespec-autorest/no-matching-version-found` when the version option is used and does not match any versions of the service. diff --git a/packages/typespec-autorest/src/emit.ts b/packages/typespec-autorest/src/emit.ts index a6877fada4..372f4a5af4 100644 --- a/packages/typespec-autorest/src/emit.ts +++ b/packages/typespec-autorest/src/emit.ts @@ -17,7 +17,7 @@ import { } from "@typespec/compiler"; import { resolveInfo } from "@typespec/openapi"; import { buildVersionProjections } from "@typespec/versioning"; -import { AutorestEmitterOptions, getTracer } from "./lib.js"; +import { AutorestEmitterOptions, getTracer, reportDiagnostic } from "./lib.js"; import { AutorestDocumentEmitterOptions, getOpenAPIForService, @@ -128,6 +128,10 @@ export async function getAllServicesAtAllVersions( (v) => !options.version || options.version === v.version, ); + if (versions.length === 0) { + reportDiagnostic(program, { code: "no-matching-version-found", target: service.type }); + } + if (versions.length === 1 && versions[0].version === undefined) { let projectedProgram; if (versions[0].projections.length > 0) { diff --git a/packages/typespec-autorest/src/lib.ts b/packages/typespec-autorest/src/lib.ts index 30e1e7170f..aa56def8b2 100644 --- a/packages/typespec-autorest/src/lib.ts +++ b/packages/typespec-autorest/src/lib.ts @@ -353,6 +353,13 @@ export const $lib = createTypeSpecLibrary({ default: paramMessage`Authentication "${"authType"}" is not a known authentication by the openapi3 emitter, it will be ignored.`, }, }, + "no-matching-version-found": { + severity: "error", + messages: { + default: + "The emitter did not emit any files because the specified version option does not match any versions of the service.", + }, + }, }, emitter: { options: EmitterOptionsSchema as JSONSchemaType, diff --git a/packages/typespec-autorest/test/versioning.test.ts b/packages/typespec-autorest/test/versioning.test.ts index b1a5b341ac..18be51171b 100644 --- a/packages/typespec-autorest/test/versioning.test.ts +++ b/packages/typespec-autorest/test/versioning.test.ts @@ -1,7 +1,8 @@ +import { expectDiagnostics } from "@typespec/compiler/testing"; import { deepStrictEqual, strictEqual } from "assert"; import { describe, expect, it } from "vitest"; import { OpenAPI2Document } from "../src/openapi2-document.js"; -import { openApiFor } from "./test-host.js"; +import { diagnoseOpenApiFor, openApiFor } from "./test-host.js"; describe("typespec-autorest: versioning", () => { it("if version enum is referenced only include current member and mark it with modelAsString: true", async () => { @@ -269,4 +270,29 @@ describe("typespec-autorest: versioning", () => { required: ["jsonProp1", "jsonProp2", "prop3"], }); }); + + it("emit diagnostics when version option is used an doesn't match the versions of the service", async () => { + const diagnostics = await diagnoseOpenApiFor( + ` +@versioned(Versions) +@service +namespace DemoService; + +enum Versions { + v1, + v2, +} + +model A {} + `, + { version: "v3" }, + ); + expectDiagnostics(diagnostics, [ + { + code: "@azure-tools/typespec-autorest/no-matching-version-found", + message: + "The emitter did not emit any files because the specified version option does not match any versions of the service.", + }, + ]); + }); });