Skip to content
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

== DFX

=== feat: Better build scripts for type:custom

Build scripts now always receive a CWD of the DFX project root, instead of wherever `+dfx+` was invoked from, and a bare script `+script.sh+` can be specified without needing to prefix with `+./+`.

=== feat: rust, custom, and asset canisters now include candid:service metadata

Motoko canisters already included this metadata.
Expand Down
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions e2e/assets/custom_canister/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
echo CUSTOM_CANISTER2_BUILD_DONE
6 changes: 6 additions & 0 deletions e2e/assets/custom_canister/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"candid": "main.did",
"wasm": "main.wasm",
"build": "echo CUSTOM_CANISTER_BUILD_DONE"
},
"custom2": {
"type": "custom",
"candid": "main.did",
"wasm": "main.wasm",
"build": "build.sh"
}
},
"defaults": {
Expand Down
17 changes: 16 additions & 1 deletion e2e/tests-dfx/build.bash
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,26 @@ teardown() {
install_asset custom_canister
dfx_start
dfx canister create --all
assert_command dfx build
assert_command dfx build custom
assert_match "CUSTOM_CANISTER_BUILD_DONE"
assert_command dfx build custom2
assert_match "CUSTOM_CANISTER2_BUILD_DONE"

dfx canister install --all
assert_command dfx canister call custom fromQuery
assert_command dfx canister call custom2 fromQuery
}

@test "custom canister build script picks local executable first" {
install_asset custom_canister
dfx_start
dfx canister create custom2
#shellcheck disable=SC2094
cat <<<"$(jq '.canisters.custom2.build="ln"' dfx.json)" >dfx.json
mv ./build.sh ./ln

assert_command dfx build custom2
assert_match CUSTOM_CANISTER2_BUILD_DONE
}

@test "build succeeds with network parameter" {
Expand Down
2 changes: 1 addition & 1 deletion e2e/tests-dfx/error_context.bash
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,5 @@ teardown() {
# expect to see the name of the canister
assert_match "custom_bad_build_step"
# expect to see the underlying cause
assert_match "No such file or directory"
assert_match "Cannot find command or file"
}
1 change: 1 addition & 0 deletions src/dfx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ url = "2.1.0"
walkdir = "2.2.9"
walrus = "0.19.0"
wasmparser = "0.86.0"
which = "4.2.5"

[dependencies.ic-agent]
version = "0.17.0"
Expand Down
20 changes: 13 additions & 7 deletions src/dfx/src/lib/builders/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use ic_types::principal::Principal as CanisterId;
use serde::Deserialize;
use slog::info;
use slog::Logger;
use std::path::PathBuf;
use std::process::Stdio;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

/// Set of extras that can be specified in the dfx.json.
struct CustomBuilderExtra {
Expand Down Expand Up @@ -139,7 +139,8 @@ impl CanisterBuilder for CustomBuilder {
.with_context(|| format!("Cannot parse command '{}'.", command))?;
// No commands, noop.
if !args.is_empty() {
run_command(args, &vars).with_context(|| format!("Failed to run {}.", command))?;
run_command(args, &vars, info.get_workspace_root())
.with_context(|| format!("Failed to run {}.", command))?;
}
}

Expand Down Expand Up @@ -190,12 +191,17 @@ impl CanisterBuilder for CustomBuilder {
}
}

fn run_command(args: Vec<String>, vars: &[super::Env<'_>]) -> DfxResult<()> {
fn run_command(args: Vec<String>, vars: &[super::Env<'_>], cwd: &Path) -> DfxResult<()> {
let (command_name, arguments) = args.split_first().unwrap();

let mut cmd = std::process::Command::new(command_name);
let canonicalized = cwd
.join(command_name)
.canonicalize()
.or_else(|_| which::which(command_name))
.map_err(|_| anyhow!("Cannot find command or file {command_name}"))?;
let mut cmd = Command::new(&canonicalized);

cmd.args(arguments)
.current_dir(cwd)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());

Expand All @@ -205,7 +211,7 @@ fn run_command(args: Vec<String>, vars: &[super::Env<'_>]) -> DfxResult<()> {

let output = cmd
.output()
.with_context(|| format!("Error executing custom build step {:#?}", cmd))?;
.with_context(|| format!("Error executing custom build step {cmd:#?}"))?;
if output.status.success() {
Ok(())
} else {
Expand Down