Skip to content

Commit 3e90e19

Browse files
[tsp-client] Fix tsp compile diagnostics logging (#8529)
* fix tsp compile diagnostics logging * update changelog and package version * use typespec/compiler format method * add test * update changelog date * changelog * nit fix changelog wording --------- Co-authored-by: Catalina Peralta <caperal@microsoft.com>
1 parent 8835340 commit 3e90e19

7 files changed

Lines changed: 146 additions & 17 deletions

File tree

tools/tsp-client/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Release
22

3+
## 2024-07-02 - 0.9.1
4+
5+
- Fix error logging after the `compile()` call and exit if diagnostics are encountered.
6+
- Use `formatDiagnostic()` from "@typespec/compiler" to report diagnostics after compiling.
7+
38
## 2024-06-21 - 0.9.0
49

510
- Prefer the `service-dir` parameter in the emitter configurations in tspconfig.yaml if specified.

tools/tsp-client/package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/tsp-client/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@azure-tools/typespec-client-generator-cli",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "A tool to generate Azure SDKs from TypeSpec",
55
"main": "dist/index.js",
66
"homepage": "https://github.com/Azure/azure-sdk-tools/tree/main/tools/tsp-client#readme",
@@ -17,7 +17,7 @@
1717
"clean": "rimraf ./dist ./types",
1818
"example": "npx ts-node src/index.ts update",
1919
"prepack": "npm run build",
20-
"test": "mocha"
20+
"test": "mocha --exclude **/examples/**"
2121
},
2222
"author": "Microsoft Corporation",
2323
"license": "MIT",
@@ -39,7 +39,7 @@
3939
"@types/prompt-sync": "^4.2.1",
4040
"@typespec/compiler": "0.50.0",
4141
"chai": "^4.3.7",
42-
"mocha": "^10.2.0",
42+
"mocha": "^10.5.2",
4343
"prettier": "^3.0.1",
4444
"rimraf": "^5.0.1",
4545
"ts-node": "^10.9.1",

tools/tsp-client/src/typespec.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolvePath, getDirectoryPath, ResolveCompilerOptionsOptions } from "@typespec/compiler";
1+
import { resolvePath, getDirectoryPath, ResolveCompilerOptionsOptions, formatDiagnostic } from "@typespec/compiler";
22
import { ModuleResolutionResult, resolveModule, ResolveModuleHost } from "@typespec/compiler/module-resolver";
33
import { Logger } from "./log.js";
44
import { readFile, readdir, realpath, stat } from "fs/promises";
@@ -66,7 +66,7 @@ export async function compileTsp({
6666
saveInputs?: boolean;
6767
}) {
6868
const parsedEntrypoint = getDirectoryPath(resolvedMainFilePath);
69-
const { compile, NodeHost, getSourceLocation, resolveCompilerOptions } = await importTsp(parsedEntrypoint);
69+
const { compile, NodeHost, resolveCompilerOptions } = await importTsp(parsedEntrypoint);
7070

7171
const outputDir = resolvePath(outputPath);
7272
const overrideOptions: Record<string, Record<string, string>> = {
@@ -107,12 +107,9 @@ export async function compileTsp({
107107

108108
if (program.diagnostics.length > 0) {
109109
for (const diagnostic of program.diagnostics) {
110-
const location = getSourceLocation(diagnostic.target);
111-
const source = location ? location.file.path : "unknown";
112-
console.error(
113-
`${diagnostic.severity}: ${diagnostic.code} - ${diagnostic.message} @ ${source}`,
114-
);
110+
Logger.error(formatDiagnostic(diagnostic));
115111
}
112+
process.exit(1);
116113
} else {
117114
Logger.success("generation complete");
118115
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// DONT specify @useDependency to get a diagnostic reported
2+
3+
import "@typespec/http";
4+
import "@typespec/rest";
5+
import "@typespec/versioning";
6+
import "@azure-tools/typespec-azure-core";
7+
8+
using TypeSpec.Http;
9+
using TypeSpec.Rest;
10+
using TypeSpec.Versioning;
11+
using Azure.Core;
12+
using Azure.Core.Traits;
13+
14+
@useAuth(
15+
ApiKeyAuth<ApiKeyLocation.header, "api-key"> | OAuth2Auth<[
16+
{
17+
type: OAuth2FlowType.implicit,
18+
authorizationUrl: "https://login.contoso.com/common/oauth2/v2.0/authorize",
19+
scopes: ["https://widget.contoso.com/.default"],
20+
}
21+
]>
22+
)
23+
@service({
24+
title: "Contoso Widget Manager",
25+
})
26+
@server(
27+
"{endpoint}/widget",
28+
"Contoso Widget APIs",
29+
{
30+
/**
31+
Supported Widget Services endpoints (protocol and hostname, for example:
32+
https://westus.api.widget.contoso.com).
33+
*/
34+
endpoint: string,
35+
}
36+
)
37+
@versioned(Contoso.WidgetManager.Versions)
38+
namespace Contoso.WidgetManager;
39+
40+
/** The Contoso Widget Manager service version. */
41+
enum Versions {
42+
/** Version 2022-08-31 */
43+
// @useDependency(Azure.Core.Versions.v1_0_Preview_2)
44+
`2022-08-30`,
45+
}
46+
47+
/** A widget. */
48+
@resource("widgets")
49+
model Widget {
50+
/** The widget name. */
51+
@key("widgetName")
52+
@visibility("read")
53+
name: string;
54+
55+
/** The widget color. */
56+
color: string;
57+
58+
/** The ID of the widget's manufacturer. */
59+
manufacturerId: string;
60+
}
61+
62+
alias ServiceTraits = SupportsRepeatableRequests &
63+
SupportsConditionalRequests &
64+
SupportsClientRequestId;
65+
66+
alias Operations = Azure.Core.ResourceOperations<ServiceTraits>;
67+
68+
interface Widgets {
69+
getWidget is Operations.ResourceRead<Widget>;
70+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
parameters:
2+
"service-dir":
3+
default: "sdk/contosowidgetmanager"
4+
emit:
5+
- "@azure-tools/typespec-autorest"
6+
linter:
7+
extends:
8+
- "@azure-tools/typespec-azure-rulesets/data-plane"
9+
options:
10+
"@azure-tools/typespec-autorest":
11+
azure-resource-provider-folder: "data-plane"
12+
emit-lro-options: "none"
13+
emitter-output-dir: "{project-root}/.."
14+
examples-directory: "examples"
15+
output-file: "{azure-resource-provider-folder}/{service-name}/{version-status}/{version}/widgets.json"
16+
"@azure-tools/typespec-python":
17+
package-dir: "azure-contoso-widgetmanager"
18+
package-name: "{package-dir}"
19+
flavor: azure
20+
"@azure-tools/typespec-csharp":
21+
package-dir: "Azure.Template.Contoso"
22+
clear-output-folder: true
23+
model-namespace: false
24+
namespace: "{package-dir}"
25+
flavor: azure
26+
"@azure-tools/typespec-ts":
27+
package-dir: "contosowidgetmanager-rest"
28+
packageDetails:
29+
name: "@azure-rest/contoso-widgetmanager-rest"
30+
flavor: azure
31+
"@azure-tools/typespec-java":
32+
package-dir: "azure-contoso-widgetmanager"
33+
namespace: com.azure.contoso.widgetmanager
34+
flavor: azure
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { assert } from "chai";
2+
import { compileTsp } from "../src/typespec.js";
3+
import { describe, it } from "node:test";
4+
import { joinPaths } from "@typespec/compiler";
5+
6+
describe("Check diagnostic reporting", function () {
7+
it("Check diagnostic format", async function () {
8+
compileTsp({
9+
emitterPackage: "@azure-tools/typespec-ts",
10+
outputPath: joinPaths(process.cwd(), "examples"),
11+
resolvedMainFilePath: joinPaths(process.cwd(), "examples", "specification", "diagnostics", "main.tsp"),
12+
additionalEmitterOptions: "",
13+
saveInputs: false
14+
}).then(
15+
() => {
16+
assert.fail("Expected error but got none");
17+
},
18+
(e) => {
19+
assert.ok(e.message.includes("test/examples/specification/diagnostics/main.tsp:38:19 - error @typespec/versioning/using-versioned-library: Namespace 'Contoso.WidgetManager' is referencing types from versioned namespace 'Azure.Core' but didn't specify which versions with @useDependency."));
20+
}
21+
);
22+
});
23+
});

0 commit comments

Comments
 (0)