Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report nice error message when dotnet is not installed #5477

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions packages/http-client-csharp/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getDirectoryPath,
joinPaths,
logDiagnostics,
NoTarget,
Program,
resolvePath,
} from "@typespec/compiler";
Expand All @@ -18,6 +19,7 @@ import { dirname } from "path";
import { fileURLToPath } from "url";
import { configurationFileName, tspOutputFileName } from "./constants.js";
import { createModel } from "./lib/client-model-builder.js";
import { reportDiagnostic } from "./lib/lib.js";
import { LoggerLevel } from "./lib/log-level.js";
import { Logger } from "./lib/logger.js";
import { NetEmitterOptions, resolveOptions, resolveOutputFolder } from "./options.js";
Expand Down Expand Up @@ -150,25 +152,40 @@ export async function $onEmit(context: EmitContext<NetEmitterOptions>) {
const command = `dotnet --roll-forward Major ${generatorPath} ${outputFolder} -p ${options["plugin-name"]}${constructCommandArg(newProjectOption)}${constructCommandArg(existingProjectOption)}${constructCommandArg(debugFlag)}`;
Logger.getInstance().info(command);

const result = await execAsync(
"dotnet",
[
"--roll-forward",
"Major",
generatorPath,
outputFolder,
"-p",
options["plugin-name"],
newProjectOption,
existingProjectOption,
debugFlag,
],
{ stdio: "inherit" },
);
if (result.exitCode !== 0) {
if (result.stderr) Logger.getInstance().error(result.stderr);
if (result.stdout) Logger.getInstance().verbose(result.stdout);
throw new Error(`Failed to generate SDK. Exit code: ${result.exitCode}`);
try {
const result = await execAsync(
"dotnet",
[
"--roll-forward",
"Major",
generatorPath,
outputFolder,
"-p",
options["plugin-name"],
newProjectOption,
existingProjectOption,
debugFlag,
],
{ stdio: "inherit" },
);
if (result.exitCode !== 0) {
if (result.stderr) Logger.getInstance().error(result.stderr);
if (result.stdout) Logger.getInstance().verbose(result.stdout);
throw new Error(`Failed to generate SDK. Exit code: ${result.exitCode}`);
}
} catch (error: any) {
if (error && "code" in (error as {}) && error["code"] === "ENOENT") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this too general of a condition? Is there a way to make it specific to dotnet being missing?

Copy link
Contributor

@JoshLove-msft JoshLove-msft Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, don't we need to ensure that .NET 8 is installed? What would happen with the current approach if .NET 6 is installed.
dotnet --list-sdks will output the list of installed SDKs. We should check that they have greater than or equal to the version we use in the global.json.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello @JoshLove-msft as discussed before, we will not do runtime time check every time when we generate sdk, we just check the error if it is failed because of missing .NET and give out a nice error message.
And we ask .NET 8 in the message to consistent with the requirement we write in the Readme.md of the library.

Copy link
Contributor Author

@chunyu3 chunyu3 Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed in the meeting, we will do runtime check when generation fail, and identify if is because of dotnet missing. I will update it. Thanks

reportDiagnostic(sdkContext.program, {
code: "runtime-dependency-missing",
format: {
dotnetMajorVersion: "8",
downloadUrl: "https://dotnet.microsoft.com/en-us/download",
Copy link
Contributor

@JoshLove-msft JoshLove-msft Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
downloadUrl: "https://dotnet.microsoft.com/en-us/download",
downloadUrl: "https://dotnet.microsoft.com/download/dotnet/8.0",

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to confirm with .NET folks that latest patch from major version is fine (as opposed to matching the version we use in global.json exactly).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @JoshLove-msft We just need user to install .NET 8 or above, (consistent with the requirement written in readme.md), so we provide a url to download .NET , not a dotnet 8 specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussion, we decide to suggest user to use the exact version in global.json, and provide the dotnet 8 download uri. Thanks.

},
target: NoTarget,
});
} else {
throw new Error(error);
}
}
if (!options["save-inputs"]) {
// delete
Expand Down
6 changes: 6 additions & 0 deletions packages/http-client-csharp/emitter/src/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ const $lib = createTypeSpecLibrary({
default: paramMessage`${"message"}`,
},
},
"runtime-dependency-missing": {
severity: "error",
messages: {
default: paramMessage`Dotnet is not found in PATH. Please install DotNet ${"dotnetMajorVersion"} or above. Dotnet can be downloaded from ${"downloadUrl"}"`,
},
},
"no-root-client": {
severity: "error",
messages: {
Expand Down
Loading