Skip to content

Commit

Permalink
Enforce .NET 8 SDK for new C# modules (#2046)
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser authored Jan 7, 2025
1 parent 2b227fe commit 763ba5e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/benchmarks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
- name: Install .NET toolchain
uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.x"
global-json-file: modules/global.json
env:
DOTNET_INSTALL_DIR: ~/.dotnet

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- uses: dsherret/rust-toolchain-file@v1
- uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.x"
global-json-file: modules/global.json
- name: Start containers
run: docker compose up -d
- name: Run smoketests
Expand Down Expand Up @@ -70,7 +70,7 @@ jobs:

- uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.x"
global-json-file: modules/global.json

- name: Create /stdb dir
run: |
Expand Down Expand Up @@ -114,7 +114,7 @@ jobs:

- uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.x"
global-json-file: modules/global.json

- name: Create /stdb dir
run: |
Expand Down
1 change: 1 addition & 0 deletions crates/cli/src/subcommands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pub async fn exec_init_csharp(args: &ArgMatches) -> anyhow::Result<()> {
(include_str!("project/csharp/StdbModule._csproj"), "StdbModule.csproj"),
(include_str!("project/csharp/Lib._cs"), "Lib.cs"),
(include_str!("project/csharp/_gitignore"), ".gitignore"),
(include_str!("project/csharp/global._json"), "global.json"),
];

// Check all dependencies
Expand Down
6 changes: 6 additions & 0 deletions crates/cli/src/subcommands/project/csharp/global._json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "8.0.400",
"rollForward": "latestMinor"
}
}
35 changes: 21 additions & 14 deletions crates/cli/src/tasks/csharp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Context;
use duct::cmd;
use std::fs;
use std::path::{Path, PathBuf};

Expand All @@ -8,27 +7,37 @@ fn parse_major_version(version: &str) -> Option<u8> {
}

pub(crate) fn build_csharp(project_path: &Path, build_debug: bool) -> anyhow::Result<PathBuf> {
// All `dotnet` commands must execute in the project directory, otherwise
// global.json won't have any effect and wrong .NET SDK might be picked.
macro_rules! dotnet {
($($arg:expr),*) => {
duct::cmd!("dotnet", $($arg),*).dir(project_path)
};
}

// Check if the `wasi-experimental` workload is installed. Unfortunately, we
// have to do this by inspecting the human-readable output. There is a
// hidden `--machine-readable` flag but it also mixes in human-readable
// output as well as unnecessarily updates various unrelated manifests.
match cmd!("dotnet", "workload", "list").read() {
match dotnet!("workload", "list").read() {
Ok(workloads) if workloads.contains("wasi-experimental") => {}
Ok(_) => {
// If wasi-experimental is not found, first check if we're running
// on .NET 8.0. We can't even install that workload on older
// versions, so this helps to provide a nicer message than "Workload
// ID wasi-experimental is not recognized.".
let version = cmd!("dotnet", "--version").read().unwrap_or_default();
if parse_major_version(&version) < Some(8) {
anyhow::bail!(".NET 8.0 is required, but found {version}.");
// on .NET SDK 8.0. We can't even install that workload on older
// versions, and we don't support .NET 9.0 yet, so this helps to
// provide a nicer message than "Workload ID wasi-experimental is not recognized.".
let version = dotnet!("--version").read().unwrap_or_default();
if parse_major_version(&version) != Some(8) {
anyhow::bail!(concat!(
".NET SDK 8.0 is required, but found {version}.\n",
"If you have multiple versions of .NET SDK installed, configure your project using https://learn.microsoft.com/en-us/dotnet/core/tools/global-json."
));
}

// Finally, try to install the workload ourselves. On some systems
// this might require elevated privileges, so print a nice error
// message if it fails.
cmd!(
"dotnet",
dotnet!(
"workload",
"install",
"wasi-experimental",
Expand All @@ -41,7 +50,7 @@ pub(crate) fn build_csharp(project_path: &Path, build_debug: bool) -> anyhow::Re
))?;
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
anyhow::bail!("dotnet not found in PATH. Please install .NET 8.0.")
anyhow::bail!("dotnet not found in PATH. Please install .NET SDK 8.0.")
}
Err(error) => anyhow::bail!("{error}"),
};
Expand All @@ -57,9 +66,7 @@ pub(crate) fn build_csharp(project_path: &Path, build_debug: bool) -> anyhow::Re
})?;

// run dotnet publish using cmd macro
cmd!("dotnet", "publish", "-c", config_name, "-v", "quiet")
.dir(project_path)
.run()?;
dotnet!("publish", "-c", config_name, "-v", "quiet").run()?;

// check if file exists
let subdir = if std::env::var_os("EXPERIMENTAL_WASM_AOT").map_or(false, |v| v == "1") {
Expand Down
6 changes: 6 additions & 0 deletions modules/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"sdk": {
"version": "8.0.400",
"rollForward": "latestMinor"
}
}

1 comment on commit 763ba5e

@github-actions
Copy link

@github-actions github-actions bot commented on 763ba5e Jan 7, 2025

Choose a reason for hiding this comment

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

Criterion benchmark results

Error when comparing benchmarks: Couldn't find AWS credentials in environment, credentials file, or IAM role.

Caused by:
Couldn't find AWS credentials in environment, credentials file, or IAM role.

Please sign in to comment.