dfinity / ic-repl

Apache License 2.0
70 stars 10 forks source link

thread 'main' panicked at 'byte index 4218 is out of bounds #51

Closed paulyoung closed 1 year ago

paulyoung commented 1 year ago

I've been trying to debug the following error:

thread 'main' panicked at 'byte index 4218 is out of bounds of `import codebase = "${CODEBASE_BACKEND_CANISTER_ID:-rwlgt-iiaaa-aaaaa-aaaaa-cai}" as "${CODEBASE_DID:-candid/codebase.did}";
identity default "~/.config/dfx/identity/default/identity.pem";
// Fixtures
let user_id = 1;
let name = "Test";
let handle = "te`[...]', library/core/src/str/mod.rs:107:9
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::str::slice_error_fail_rt
   3: core::ops::function::FnOnce::call_once
   4: core::intrinsics::const_eval_select
   5: core::str::slice_error_fail
   6: ic_repl::command::Command::run
   7: ic_repl::main

I had started at the beginning of the file because the error mentioned "import" but it seems to be just printing the truncated contents of the file.

After much investigating I've narrowed it down to these lines of my test which happen to be the last thing in the file.

let _ = call codebase.getCanisterMetrics(
  record
    { dateFromMillis = 0
    ; dateToMillis = 0
    ; granularity = variant { daily }
    }
);

If I comment out these lines then everything works fine.

Putting it all on one line like this makes no difference:

let _ = call codebase.getCanisterMetrics(record { dateFromMillis = 0; dateToMillis = 0; granularity = variant { daily } });


Neither does reducing the file to this:

import codebase = "${CODEBASE_BACKEND_CANISTER_ID:-rwlgt-iiaaa-aaaaa-aaaaa-cai}" as "${CODEBASE_DID:-candid/codebase.did}";
let _ = call codebase.getCanisterMetrics(record { dateFromMillis = 0; dateToMillis = 0; granularity = variant { daily } });
> import codebase = "${CODEBASE_BACKEND_CANISTER_ID:-rwlgt-iiaaa-aaaaa-aaaaa-cai}" as "${CODEBASE_DID:-candid/codebase.did}";
let _ = c
thread 'main' panicked at 'byte index 257 is out of bounds of `import codebase = "${CODEBASE_BACKEND_CANISTER_ID:-rwlgt-iiaaa-aaaaa-aaaaa-cai}" as "${CODEBASE_DID:-candid/codebase.did}";

Other methods work fine:

import codebase = "${CODEBASE_BACKEND_CANISTER_ID:-rwlgt-iiaaa-aaaaa-aaaaa-cai}" as "${CODEBASE_DID:-candid/codebase.did}";

identity default "~/.config/dfx/identity/default/identity.pem";

// Canistergeek: minimal tests that verify that the methods are available

let _ = call codebase.collectCanisterMetrics();

let _ = call codebase.getCanisterLog(opt variant { getMessagesInfo });

// let _ = call codebase.getCanisterMetrics(
//   record
//     { dateFromMillis = 0
//     ; dateToMillis = 0
//     ; granularity = variant { daily }
//     }
// );

Placing something else at the end of the file made no difference either.

paulyoung commented 1 year ago

Confirming that this works using dfx:

$ dfx canister call codebase_backend getCanisterMetrics '(record { dateFromMillis = 0; dateToMillis = 0; granularity = variant { daily }; })'
(opt record { data = variant { daily = vec {} } })
chenyan2002 commented 1 year ago

This looks strange. Not sure where the out-of-bound error come from...Can you try not using ${env_var}, but just hard code the content? I suspect the error comes from some other crates that use unsafe? Would be nice to have a smaller repo.