Skip to content

Commit

Permalink
Merge branch 'main' into implement-namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang authored Jan 10, 2025
2 parents 781bd0b + 5e26257 commit f1709bc
Show file tree
Hide file tree
Showing 91 changed files with 1,443 additions and 808 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- typespec-vscode
---

improve console output when tsp-server not found
7 changes: 6 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Catch all
* @bterlson @markcowl @allenjzhang @timotheeguerin
* @bterlson @markcowl @allenjzhang @timotheeguerin @witemple-msft

######################
# CSharp
Expand All @@ -21,6 +21,11 @@
######################
/eng/emitters/ @m-nash

######################
# JS - Server
######################
/packages/http-server-javascript @witemple-msft

######################
# IDE
######################
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,4 @@ BenchmarkDotnet.Artifacts/
packages/http-client-python/generator/test/**/generated/
packages/http-client-python/generator/test/**/cadl-ranch-coverage.json
!packages/http-client-python/package-lock.json
packages/http-client-python/micropip.lock
4 changes: 2 additions & 2 deletions packages/astro-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
},
"devDependencies": {
"@types/react": "~18.3.11",
"astro": "^4.16.10"
"astro": "^4.16.18"
},
"peerDependencies": {
"astro": "^4.16.10"
"astro": "^4.16.18"
},
"dependencies": {
"@astrojs/check": "^0.9.4",
Expand Down
11 changes: 6 additions & 5 deletions packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ Refresh-Build

$specsDirectory = Join-Path $packageRoot 'node_modules' '@typespec' 'http-specs' 'specs'
$azureSpecsDirectory = Join-Path $packageRoot 'node_modules' '@azure-tools' 'azure-http-specs' 'specs'
$cadlRanchRoot = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch' 'http'
$directories = Get-ChildItem -Path "$cadlRanchRoot" -Directory -Recurse
$cadlRanchRoot = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch'
$cadlRanchRootHttp = Join-Path $cadlRanchRoot 'http'
$directories = Get-ChildItem -Path "$cadlRanchRootHttp" -Directory -Recurse
$cadlRanchCsproj = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch.Tests' 'TestProjects.CadlRanch.Tests.csproj'

$coverageDir = Join-Path $packageRoot 'generator' 'artifacts' 'coverage'
Expand All @@ -27,15 +28,15 @@ foreach ($directory in $directories) {
}

$outputDir = $directory.FullName.Substring(0, $directory.FullName.IndexOf("src") - 1)
$subPath = $outputDir.Substring($cadlRanchRoot.Length + 1)
$subPath = $outputDir.Substring($cadlRanchRootHttp.Length + 1)
$folders = $subPath.Split([System.IO.Path]::DirectorySeparatorChar)

if (-not (Compare-Paths $subPath $filter)) {
continue
}

$testPath = "$cadlRanchRoot.Tests"
$testFilter = "TestProjects.CadlRanch.Tests"
$testPath = Join-Path "$cadlRanchRoot.Tests" "Http"
$testFilter = "TestProjects.CadlRanch.Tests.Http"
foreach ($folder in $folders) {
$segment = "$(Get-Namespace $folder)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private ValueExpression GetResultConversion(ClientResponseApi result, HttpRespon
return response.Content().ToObjectFromJson(responseBodyType);
}
}
if (responseBodyType.Equals(typeof(string)) && Operation.RequestMediaTypes?.Contains("text/plain") == true)
if (responseBodyType.Equals(typeof(string)) && Operation.Responses.Any(r => r.IsErrorResponse is false && r.ContentTypes.Contains("text/plain")))
{
return response.Content().InvokeToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public Task SendAsText() => Test(async (host) =>
});

[CadlRanchTest]
[Ignore("https://github.com/microsoft/typespec/issues/4208")]
public Task GetAsText() => Test(async (host) =>
{
var response2 = await new MediaTypeClient(host, null).GetStringBodyClient().GetAsTextAsync();
Expand Down
15 changes: 4 additions & 11 deletions packages/http-client-java/emitter/src/code-model-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ export class CodeModelBuilder {
private emitterContext: EmitContext<EmitterOptions>;
private serviceNamespace: Namespace;

private loggingEnabled: boolean = false;

readonly schemaCache = new ProcessingCache((type: SdkType, name: string) =>
this.processSchemaImpl(type, name),
);
Expand All @@ -186,9 +184,6 @@ export class CodeModelBuilder {
this.options = context.options;
this.program = program1;
this.emitterContext = context;
if (this.options["dev-options"]?.loglevel) {
this.loggingEnabled = true;
}

if (this.options["skip-special-headers"]) {
this.options["skip-special-headers"].forEach((it) =>
Expand Down Expand Up @@ -587,7 +582,7 @@ export class CodeModelBuilder {
}
}
} else if (initializationProperty.type.variantTypes.length > 2) {
this.logError("Multiple server url defined for one client is not supported yet.");
this.logError("Multiple server URL defined for one client is not supported yet.");
}
} else if (initializationProperty.type.kind === "endpoint") {
sdkPathParameters = initializationProperty.type.templateArguments;
Expand Down Expand Up @@ -2321,7 +2316,7 @@ export class CodeModelBuilder {

private getUnionVariantName(type: Type | undefined, option: any): string {
if (type === undefined) {
this.logError("type is undefined.");
this.logWarning("Union variant type is undefined.");
return "UnionVariant";
}
switch (type.kind) {
Expand Down Expand Up @@ -2374,7 +2369,7 @@ export class CodeModelBuilder {
case "UnionVariant":
return (typeof type.name === "string" ? type.name : undefined) ?? "UnionVariant";
default:
this.logError(`Unrecognized type for union variable: '${type.kind}'.`);
this.logWarning(`Unrecognized type for union variable: '${type.kind}'.`);
return "UnionVariant";
}
}
Expand Down Expand Up @@ -2584,9 +2579,7 @@ export class CodeModelBuilder {
}

private logWarning(msg: string) {
if (this.loggingEnabled) {
logWarning(this.program, msg);
}
logWarning(this.program, msg);
}

private trace(msg: string) {
Expand Down
17 changes: 13 additions & 4 deletions packages/http-client-java/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { dirname } from "path";
import { fileURLToPath } from "url";
import { CodeModelBuilder } from "./code-model-builder.js";
import { CodeModel } from "./common/code-model.js";
import { logError, spawnAsync } from "./utils.js";
import { logError, spawnAsync, SpawnError } from "./utils.js";
import { JDK_NOT_FOUND_MESSAGE, validateDependencies } from "./validate.js";

export interface EmitterOptions {
Expand Down Expand Up @@ -117,7 +117,9 @@ export const $lib = createTypeSpecLibrary({

export async function $onEmit(context: EmitContext<EmitterOptions>) {
const program = context.program;
await validateDependencies(program, true);
if (!program.compilerOptions.noEmit) {
await validateDependencies(program, true);
}

if (!program.hasError()) {
const options = context.options;
Expand Down Expand Up @@ -187,12 +189,19 @@ export async function $onEmit(context: EmitContext<EmitterOptions>) {
javaArgs.push(jarFileName);
javaArgs.push(codeModelFileName);
try {
await spawnAsync("java", javaArgs, { stdio: "inherit" });
const result = await spawnAsync("java", javaArgs, { stdio: "pipe" });
program.trace("http-client-java", `Code generation log: ${result.stdout}`);
} catch (error: any) {
if (error && "code" in error && error["code"] === "ENOENT") {
logError(program, JDK_NOT_FOUND_MESSAGE);
} else {
logError(program, error.message);
logError(
program,
'The emitter was unable to generate client code from this TypeSpec, please run this command again with "--trace http-client-java" to get diagnostic information, and open an issue on https://github.com/microsoft/typespec',
);
if (error instanceof SpawnError) {
program.trace("http-client-java", `Code generation error: ${error.stdout}`);
}
}
}

Expand Down
19 changes: 16 additions & 3 deletions packages/http-client-java/emitter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { NoTarget, Program, Type } from "@typespec/compiler";
import { spawn, SpawnOptions } from "child_process";

export function logError(program: Program, msg: string) {
trace(program, msg);
program.reportDiagnostic({
code: "http-client-java",
severity: "error",
Expand All @@ -12,7 +11,6 @@ export function logError(program: Program, msg: string) {
}

export function logWarning(program: Program, msg: string) {
trace(program, msg);
program.reportDiagnostic({
code: "http-client-java",
severity: "warning",
Expand Down Expand Up @@ -70,6 +68,17 @@ export type SpawnReturns = {
stderr: string;
};

export class SpawnError extends Error {
stdout: string;
stderr: string;

constructor(message: string, stdout: string, stderr: string) {
super(message);
this.stdout = stdout;
this.stderr = stderr;
}
}

export async function spawnAsync(
command: string,
args: readonly string[],
Expand Down Expand Up @@ -103,7 +112,11 @@ export async function spawnAsync(
childProcess.on("exit", (code, signal) => {
if (code !== 0) {
if (code) {
error = new Error(`${command} ended with code '${code}'.`);
error = new SpawnError(
`${command} ended with code '${code}'.`,
stdout.join(""),
stderr.join(""),
);
} else {
error = new Error(`${command} terminated by signal '${signal}'.`);
}
Expand Down
38 changes: 32 additions & 6 deletions packages/http-client-java/emitter/src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Program } from "@typespec/compiler";
import { logError, spawnAsync } from "./utils.js";
import { logError, spawnAsync, trace } from "./utils.js";

export const JDK_NOT_FOUND_MESSAGE =
"Java Development Kit (JDK) is not found in PATH. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download";
Expand All @@ -13,7 +13,11 @@ export async function validateDependencies(
const result = await spawnAsync("javac", ["-version"], { stdio: "pipe" });
const javaVersion = findJavaVersion(result.stdout) ?? findJavaVersion(result.stderr);
if (javaVersion) {
if (javaVersion < 11) {
if (program && logDiagnostic) {
trace(program, `Java Development Kit (JDK) in PATH is version ${javaVersion}.`);
}
const javaMajorVersion = getJavaMajorVersion(javaVersion);
if (javaMajorVersion < 11) {
// the message is JDK 17, because clientcore depends on JDK 17
// emitter only require JDK 11
const message = `Java Development Kit (JDK) in PATH is version ${javaVersion}. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download`;
Expand All @@ -40,7 +44,13 @@ export async function validateDependencies(
// nodejs does not allow spawn of .cmd on win32
const shell = process.platform === "win32";
try {
await spawnAsync("mvn", ["-v"], { stdio: "pipe", shell: shell });
const result = await spawnAsync("mvn", ["-v"], { stdio: "pipe", shell: shell });
const mavenVersion = findMavenVersion(result.stdout) ?? findMavenVersion(result.stderr);
if (mavenVersion) {
if (program && logDiagnostic) {
trace(program, `Apache Maven in PATH is version ${mavenVersion}.`);
}
}
} catch (error: any) {
let message = error.message;
if (shell || (error && "code" in error && error["code"] === "ENOENT")) {
Expand All @@ -55,9 +65,16 @@ export async function validateDependencies(
}
}

function findJavaVersion(output: string): number | undefined {
const regex = /javac (\d+)\.(\d+)\..*/;
const matches = output.match(regex);
function findJavaVersion(output: string): string | undefined {
const matches = output.match(/javac ([\d.]+).*/);
if (matches && matches.length > 1) {
return matches[1];
}
return undefined;
}

function getJavaMajorVersion(version: string): number {
const matches = version.match(/(\d+)\.(\d+)\..*/);
if (matches && matches.length > 2) {
if (matches[1] === "1") {
// "javac 1.8.0_422" -> 8
Expand All @@ -67,5 +84,14 @@ function findJavaVersion(output: string): number | undefined {
return +matches[1];
}
}
return 0;
}

function findMavenVersion(output: string): string | undefined {
// there is control characters in the output
const matches = output.match(/.*Apache Maven ([\d.]+).*/);
if (matches && matches.length > 1) {
return matches[1];
}
return undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public static void getAsyncSyncClients(Client client, ServiceClient serviceClien
}
}

final int count = serviceClient.getMethodGroupClients().size() + asyncClients.size();
final int count
= serviceClient.getMethodGroupClients().size() + Math.max(asyncClients.size(), syncClients.size());
for (MethodGroupClient methodGroupClient : serviceClient.getMethodGroupClients()) {
AsyncSyncClient.Builder builder = new AsyncSyncClient.Builder().packageName(packageName)
.serviceClient(serviceClient)
Expand Down
17 changes: 17 additions & 0 deletions packages/http-client-python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Change Log - @typespec/http-client-python

## 0.6.1

### Bug Fixes

- Only add linting disables for a file with too many lines if the file doesn't already disable this linter rule
- Generate `__init__` for internal models to allow for discriminator needs

## 0.6.0

### Features

- Add support for typespec namespace

### Bug Fixes

- Only add linting disables for a file with too many lines if the file doesn't already disable this linter rule

## 0.5.1

### Bug Fixes
Expand Down
Loading

0 comments on commit f1709bc

Please sign in to comment.