Skip to content

Commit

Permalink
feat: Introduce DFX_CACHE_ROOT environment variable (#2112)
Browse files Browse the repository at this point in the history
Fixes #2106
  • Loading branch information
paulyoung authored Apr 1, 2022
1 parent 4eb6e23 commit f4e24bf
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

== DFX

=== feat: Introduce DFX_CACHE_ROOT environment variable

A new environment variable, `DFX_CACHE_ROOT`, has been introduced to allow setting the cache root directory to a different location than the configuration root directory. Previously `DFX_CONFIG_ROOT` was repurposed for this which only allowed one location to be set for both the cache and configuration root directories.

This is a breaking change since setting `DFX_CONFIG_ROOT` will no longer set the cache root directory to that location.

=== fix: Error if nonzero cycles are passed without a wallet proxy

Previously, `dfx canister call --with-cycles 1` would silently ignore the `--with-cycles` argument as the DFX principal has no way to pass cycles and the call must be forwarded through the wallet. Now it will error instead of silently ignoring it. To forward a call through the wallet, use `--wallet $(dfx identity get-wallet)`, or `--wallet $(dfx identity --network ic get-wallet)` for mainnet.
Expand Down
2 changes: 1 addition & 1 deletion check-binaries.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pkgs.runCommand "check-binaries" {
++ lib.optional stdenv.isLinux [ glibc.bin patchelf ];
} ''
mkdir -p $out
export DFX_CONFIG_ROOT="$out"
export DFX_CACHE_ROOT="$out"
cp ${dfx.standalone}/bin/dfx dfx
${pkgs.lib.optionalString pkgs.stdenv.isLinux ''
Expand Down
6 changes: 3 additions & 3 deletions e2e/tests-dfx/dfx_install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ teardown() {
@test "dfx cache show does not install the dfx version into the cache" {
[ "$USE_IC_REF" ] && skip "skipped for ic-ref"

test -z "$(ls -A "$DFX_CONFIG_ROOT")"
test -z "$(ls -A "$DFX_CACHE_ROOT")"

assert_command dfx cache show

# does not populate the cache with this version
test ! -e "$(dfx cache show)"

# it does create the empty versions directory though
test -d "$DFX_CONFIG_ROOT/.cache/dfinity/versions"
test -z "$(ls -A "$DFX_CONFIG_ROOT/.cache/dfinity/versions"``)"
test -d "$DFX_CACHE_ROOT/.cache/dfinity/versions"
test -z "$(ls -A "$DFX_CACHE_ROOT/.cache/dfinity/versions"``)"
}

@test "non-forced install populates an empty cache" {
Expand Down
2 changes: 2 additions & 0 deletions e2e/tests-dfx/mode_reinstall.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load ../utils/_
setup() {
# We want to work from a temporary directory, different for every test.
x=$(mktemp -d -t dfx-e2e-XXXXXXXX)
export DFX_CACHE_ROOT="$x"
export DFX_CONFIG_ROOT="$x"
cd "$x" || exit
export RUST_BACKTRACE=1
Expand All @@ -14,6 +15,7 @@ setup() {

teardown() {
dfx_stop
rm -rf "$DFX_CACHE_ROOT"
rm -rf "$DFX_CONFIG_ROOT"
}

Expand Down
2 changes: 2 additions & 0 deletions e2e/tests-dfx/upgrade_check.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ load ../utils/_
setup() {
# We want to work from a temporary directory, different for every test.
x=$(mktemp -d -t dfx-e2e-XXXXXXXX)
export DFX_CACHE_ROOT="$x"
export DFX_CONFIG_ROOT="$x"
cd "$x" || exit
export RUST_BACKTRACE=1
Expand All @@ -14,6 +15,7 @@ setup() {

teardown() {
dfx_stop
rm -rf "$DFX_CACHE_ROOT"
rm -rf "$DFX_CONFIG_ROOT"
}

Expand Down
5 changes: 3 additions & 2 deletions e2e/tests-dfx/usage_env.bash
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ teardown() {

#cache
# create a new project to install dfx cache
assert_command_fail ls "$DFX_CONFIG_ROOT/.cache/dfinity/versions"
assert_command_fail ls "$DFX_CACHE_ROOT/.cache/dfinity/versions"
dfx new hello
assert_command ls "$DFX_CONFIG_ROOT/.cache/dfinity/versions"
assert_command ls "$DFX_CACHE_ROOT/.cache/dfinity/versions"
assert_command_fail ls "$HOME/.cache/dfinity/versions"
rm -rf hello

(
# use subshell to retain $DFX_CONFIG_ROOT for teardown
# remove configured variable, should use $HOME now
unset DFX_CACHE_ROOT
unset DFX_CONFIG_ROOT

dfx identity new --disable-encryption bob
Expand Down
2 changes: 2 additions & 0 deletions e2e/utils/_.bash
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ standard_setup() {
export DFX_E2E_TEMP_DIR="$x"

mkdir "$x/working-dir"
mkdir "$x/cache-root"
mkdir "$x/config-root"
mkdir "$x/home-dir"

cd "$x/working-dir" || exit

export HOME="$x/home-dir"
export DFX_CACHE_ROOT="$x/cache-root"
export DFX_CONFIG_ROOT="$x/config-root"
export RUST_BACKTRACE=1
}
Expand Down
4 changes: 2 additions & 2 deletions src/dfx/src/config/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ impl Cache for DiskBasedCache {
}

pub fn get_cache_root() -> DfxResult<PathBuf> {
let config_root = std::env::var("DFX_CONFIG_ROOT").ok();
let cache_root = std::env::var("DFX_CACHE_ROOT").ok();
let home =
std::env::var("HOME").map_err(|_| DfxError::new(CacheError::CannotFindHomeDirectory()))?;
let root = config_root.unwrap_or(home);
let root = cache_root.unwrap_or(home);
let p = PathBuf::from(root).join(".cache").join("dfinity");
if !p.exists() {
if let Err(_e) = std::fs::create_dir_all(&p) {
Expand Down
10 changes: 5 additions & 5 deletions src/dfx/src/lib/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ mod tests {

#[test]
fn test_passwords() {
let cfg_root = TempDir::new().unwrap();
let old_var = env::var_os("DFX_CONFIG_ROOT");
env::set_var("DFX_CONFIG_ROOT", cfg_root.path());
let cache_root = TempDir::new().unwrap();
let old_var = env::var_os("DFX_CACHE_ROOT");
env::set_var("DFX_CACHE_ROOT", cache_root.path());
let log = Logger::root(
FullFormat::new(PlainSyncDecorator::new(io::stderr()))
.build()
Expand All @@ -503,9 +503,9 @@ mod tests {
assert_eq!(user, "default");
assert_eq!(pass, "hunter2:");
if let Some(old_var) = old_var {
env::set_var("DFX_CONFIG_ROOT", old_var);
env::set_var("DFX_CACHE_ROOT", old_var);
} else {
env::remove_var("DFX_CONFIG_ROOT");
env::remove_var("DFX_CACHE_ROOT");
}
}
}

0 comments on commit f4e24bf

Please sign in to comment.