Skip to content

Commit 77a96a3

Browse files
committed
automatically download appropriate Go version when necessary
There are three scenarios where we might need to do this: - Go is not installed at all - Go is installed, but the version is too old - Go is installed and the version is new enough, but the target world uses async features and the installed Go lacks [the required patch](golang/go#76775) In any of those cases, we'll download a suitable version, install it in the current user's cache directory, and run it from there. This also adds a new `--export-pkg-name` parameter corresponding to bytecodealliance/wit-bindgen#1572.
1 parent 303f6f3 commit 77a96a3

File tree

9 files changed

+388
-177
lines changed

9 files changed

+388
-177
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ members = ["./tests"]
1313

1414
[workspace.dependencies]
1515
anyhow = "1.0.102"
16+
bzip2 = "0.6.1"
17+
once_cell = "1.21.3"
18+
reqwest = { version = "0.13.1", features = ["blocking"] }
19+
tar = "0.4"
1620

1721
[workspace.package]
1822
version = "0.2.0"
@@ -34,11 +38,19 @@ unnecessary_cast = 'warn'
3438
allow_attributes_without_reason = 'warn'
3539

3640
[dependencies]
37-
anyhow = { workspace = true}
41+
anyhow = { workspace = true }
42+
bzip2 = { workspace = true }
43+
reqwest = { workspace = true }
44+
tar = { workspace = true }
3845
clap = { version = "4.5.60", features = ["derive"] }
3946
regex = "1.12.3"
4047
serde = { version = "1.0.228", features = ["derive"] }
4148
toml = "1.1.0"
42-
wit-bindgen-go = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "3ee9fe20a5bce398360d5d291e81a4224a6d7c76" }
49+
# TODO: Switch back to upstream once
50+
# https://github.com/bytecodealliance/wit-bindgen/pull/1572 is merged:
51+
wit-bindgen-go = { git = "https://github.com/dicej/wit-bindgen", rev = "b163b3b5" }
4352
wit-component = "0.245.1"
4453
wit-parser = "0.245.1"
54+
which = "8.0.2"
55+
dirs = "6.0.0"
56+

src/cmd_bindings.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
use crate::utils::{make_path_absolute, parse_wit};
1+
use crate::utils::make_path_absolute;
22
use anyhow::Result;
33
use std::path::{Path, PathBuf};
4+
use wit_parser::{Resolve, WorldId};
45

56
#[allow(clippy::too_many_arguments)]
67
pub fn generate_bindings(
7-
paths: &[impl AsRef<Path>],
8-
worlds: &[String],
9-
ignore_toml_files: bool,
10-
features: &[String],
11-
all_features: bool,
8+
resolve: &mut Resolve,
9+
world: WorldId,
1210
generate_stubs: bool,
1311
should_format: bool,
1412
output: Option<&Path>,
1513
pkg_name: Option<String>,
14+
export_pkg_name: Option<String>,
1615
) -> Result<()> {
17-
let (mut resolve, world) = parse_wit(paths, worlds, ignore_toml_files, features, all_features)?;
1816
let mut files = Default::default();
1917

2018
let format = if should_format {
@@ -37,13 +35,14 @@ pub fn generate_bindings(
3735
generate_stubs,
3836
format,
3937
pkg_name,
38+
export_pkg_name,
4039
..Default::default()
4140
}
4241
.build()
43-
.generate(&mut resolve, world, &mut files)?;
42+
.generate(resolve, world, &mut files)?;
4443

4544
let output_path = match output {
46-
Some(p) => make_path_absolute(&p.to_path_buf())?,
45+
Some(p) => make_path_absolute(p)?,
4746
None => PathBuf::from("."),
4847
};
4948

src/cmd_build.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
use crate::utils::{check_go_version, make_path_absolute};
22
use anyhow::{Result, anyhow};
3-
use std::{path::PathBuf, process::Command};
3+
use std::{
4+
path::{Path, PathBuf},
5+
process::Command,
6+
};
47

58
/// Compiles a Go application to a wasm module with `go build`.
69
///
710
/// If the module is not going to be adapted to the component model,
811
/// set the `only_wasip1` arg to true.
9-
pub fn build_module(
10-
out: Option<&PathBuf>,
11-
go_path: Option<&PathBuf>,
12-
only_wasip1: bool,
13-
) -> Result<PathBuf> {
14-
let go = match &go_path {
15-
Some(p) => make_path_absolute(p)?,
16-
None => PathBuf::from("go"),
17-
};
18-
19-
check_go_version(&go)?;
12+
pub fn build_module(out: Option<&PathBuf>, go: &Path, only_wasip1: bool) -> Result<PathBuf> {
13+
check_go_version(go)?;
2014

2115
let out_path_buf = match &out {
2216
Some(p) => make_path_absolute(p)?,
@@ -53,13 +47,13 @@ pub fn build_module(
5347
];
5448

5549
let output = if only_wasip1 {
56-
Command::new(&go)
50+
Command::new(go)
5751
.args(module_args)
5852
.env("GOOS", "wasip1")
5953
.env("GOARCH", "wasm")
6054
.output()?
6155
} else {
62-
Command::new(&go)
56+
Command::new(go)
6357
.args(component_args)
6458
.env("GOOS", "wasip1")
6559
.env("GOARCH", "wasm")

src/cmd_test.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@ use std::{
1212
pub fn build_test_module(
1313
path: &Path,
1414
output_dir: Option<&PathBuf>,
15-
go_path: Option<&PathBuf>,
15+
go: &Path,
1616
only_wasip1: bool,
1717
) -> Result<PathBuf> {
18-
let go = match &go_path {
19-
Some(p) => make_path_absolute(p)?,
20-
None => PathBuf::from("go"),
21-
};
22-
23-
check_go_version(&go)?;
18+
check_go_version(go)?;
2419

2520
let test_wasm_path = {
2621
// The directory in which the test component will be placed
@@ -68,7 +63,7 @@ pub fn build_test_module(
6863
];
6964

7065
let output = if only_wasip1 {
71-
Command::new(&go)
66+
Command::new(go)
7267
.args(module_args)
7368
.env("GOOS", "wasip1")
7469
.env("GOARCH", "wasm")
@@ -80,7 +75,7 @@ pub fn build_test_module(
8075

8176
// TODO: for when we figure out how wasip2 tests are to be run
8277
#[allow(unreachable_code)]
83-
Command::new(&go)
78+
Command::new(go)
8479
.args(component_args)
8580
.env("GOOS", "wasip1")
8681
.env("GOARCH", "wasm")

0 commit comments

Comments
 (0)