1
1
use anyhow:: Context ;
2
- use duct:: cmd;
3
2
use std:: fs;
4
3
use std:: path:: { Path , PathBuf } ;
5
4
@@ -8,27 +7,37 @@ fn parse_major_version(version: &str) -> Option<u8> {
8
7
}
9
8
10
9
pub ( crate ) fn build_csharp ( project_path : & Path , build_debug : bool ) -> anyhow:: Result < PathBuf > {
10
+ // All `dotnet` commands must execute in the project directory, otherwise
11
+ // global.json won't have any effect and wrong .NET SDK might be picked.
12
+ macro_rules! dotnet {
13
+ ( $( $arg: expr) ,* ) => {
14
+ duct:: cmd!( "dotnet" , $( $arg) ,* ) . dir( project_path)
15
+ } ;
16
+ }
17
+
11
18
// Check if the `wasi-experimental` workload is installed. Unfortunately, we
12
19
// have to do this by inspecting the human-readable output. There is a
13
20
// hidden `--machine-readable` flag but it also mixes in human-readable
14
21
// output as well as unnecessarily updates various unrelated manifests.
15
- match cmd ! ( "dotnet" , "workload" , "list" ) . read ( ) {
22
+ match dotnet ! ( "workload" , "list" ) . read ( ) {
16
23
Ok ( workloads) if workloads. contains ( "wasi-experimental" ) => { }
17
24
Ok ( _) => {
18
25
// If wasi-experimental is not found, first check if we're running
19
- // on .NET 8.0. We can't even install that workload on older
20
- // versions, so this helps to provide a nicer message than "Workload
21
- // ID wasi-experimental is not recognized.".
22
- let version = cmd ! ( "dotnet" , "--version" ) . read ( ) . unwrap_or_default ( ) ;
23
- if parse_major_version ( & version) < Some ( 8 ) {
24
- anyhow:: bail!( ".NET 8.0 is required, but found {version}." ) ;
26
+ // on .NET SDK 8.0. We can't even install that workload on older
27
+ // versions, and we don't support .NET 9.0 yet, so this helps to
28
+ // provide a nicer message than "Workload ID wasi-experimental is not recognized.".
29
+ let version = dotnet ! ( "--version" ) . read ( ) . unwrap_or_default ( ) ;
30
+ if parse_major_version ( & version) != Some ( 8 ) {
31
+ anyhow:: bail!( concat!(
32
+ ".NET SDK 8.0 is required, but found {version}.\n " ,
33
+ "If you have multiple versions of .NET SDK installed, configure your project using https://learn.microsoft.com/en-us/dotnet/core/tools/global-json."
34
+ ) ) ;
25
35
}
26
36
27
37
// Finally, try to install the workload ourselves. On some systems
28
38
// this might require elevated privileges, so print a nice error
29
39
// message if it fails.
30
- cmd ! (
31
- "dotnet" ,
40
+ dotnet ! (
32
41
"workload" ,
33
42
"install" ,
34
43
"wasi-experimental" ,
@@ -41,7 +50,7 @@ pub(crate) fn build_csharp(project_path: &Path, build_debug: bool) -> anyhow::Re
41
50
) ) ?;
42
51
}
43
52
Err ( error) if error. kind ( ) == std:: io:: ErrorKind :: NotFound => {
44
- anyhow:: bail!( "dotnet not found in PATH. Please install .NET 8.0." )
53
+ anyhow:: bail!( "dotnet not found in PATH. Please install .NET SDK 8.0." )
45
54
}
46
55
Err ( error) => anyhow:: bail!( "{error}" ) ,
47
56
} ;
@@ -57,9 +66,7 @@ pub(crate) fn build_csharp(project_path: &Path, build_debug: bool) -> anyhow::Re
57
66
} ) ?;
58
67
59
68
// run dotnet publish using cmd macro
60
- cmd ! ( "dotnet" , "publish" , "-c" , config_name, "-v" , "quiet" )
61
- . dir ( project_path)
62
- . run ( ) ?;
69
+ dotnet ! ( "publish" , "-c" , config_name, "-v" , "quiet" ) . run ( ) ?;
63
70
64
71
// check if file exists
65
72
let subdir = if std:: env:: var_os ( "EXPERIMENTAL_WASM_AOT" ) . map_or ( false , |v| v == "1" ) {
0 commit comments