Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: canonicalize load paths in load_from functions #1462

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
04a7eaf
refactor(fuels-core): allow trailing commas in `error!` macro
Br1ght0ne Jul 18, 2024
e261d09
refactor: canonicalize load paths in load_from fns
Br1ght0ne Jul 18, 2024
1b88f36
fix: add missing references
Br1ght0ne Jul 18, 2024
863ace7
fix: tests
Br1ght0ne Jul 18, 2024
567f181
fix: tests
Br1ght0ne Jul 18, 2024
08d26a6
fix: tests (now for real)
Br1ght0ne Jul 18, 2024
529abaf
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Jul 19, 2024
174470a
feat: use path-clean crate
Br1ght0ne Jul 29, 2024
c3ed578
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Jul 29, 2024
cadc90d
update test
Br1ght0ne Jul 29, 2024
8cf410d
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Jul 29, 2024
d6bafe2
update storage tests
Br1ght0ne Jul 29, 2024
f9d9f33
Merge branch 'master' into oleksii/realpath_load_from
hal3e Jul 31, 2024
e63755a
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Aug 5, 2024
7ced3cd
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Aug 19, 2024
27ec921
Fix merge
Br1ght0ne Aug 19, 2024
6e27a89
fix error messages
Br1ght0ne Aug 19, 2024
7ceea6c
absolute, then clean
Br1ght0ne Aug 19, 2024
7264c7a
appease clippy-sama
Br1ght0ne Aug 19, 2024
6b0dea8
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Aug 20, 2024
b1c4af2
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Aug 24, 2024
6468c78
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Aug 28, 2024
2a16fea
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Sep 2, 2024
2e5b36a
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Sep 9, 2024
7f2360a
Merge branch 'master' into oleksii/realpath_load_from
hal3e Sep 12, 2024
5e5c218
Read absolute/uncleaned, report cleaned
Br1ght0ne Sep 30, 2024
cb4a5cb
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Sep 30, 2024
5d6922f
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Sep 30, 2024
200140f
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Oct 1, 2024
0a25bc7
Address review comments
Br1ght0ne Oct 21, 2024
09300d5
Merge branch 'master' into oleksii/realpath_load_from
Br1ght0ne Oct 21, 2024
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fuel-abi-types = "0.7.0"
futures = "0.3.29"
hex = { version = "0.4.3", default-features = false }
itertools = "0.12.0"
path-clean = "1.0.1"
Br1ght0ne marked this conversation as resolved.
Show resolved Hide resolved
portpicker = "0.1.1"
pretty_assertions = { version = "1.4", default-features = false }
proc-macro2 = "1.0.70"
Expand Down
15 changes: 11 additions & 4 deletions e2e/tests/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fs;
use std::time::Duration;

use fuel_tx::{
Expand Down Expand Up @@ -1089,17 +1090,23 @@ async fn test_add_custom_assets() -> Result<()> {
#[tokio::test]
async fn contract_load_error_messages() {
{
let binary_path = "sway/contracts/contract_test/out/release/no_file_on_path.bin";
let expected_error = format!("io: file \"{binary_path}\" does not exist");
let binary_path = "sway/../sway/contracts/contract_test/out/release/no_file_on_path.bin";
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let expected_error = format!("io: file \"{manifest_dir}/sway/contracts/contract_test/out/release/no_file_on_path.bin\" does not exist");

let error = Contract::load_from(binary_path, LoadConfiguration::default())
.expect_err("should have failed");

assert_eq!(error.to_string(), expected_error);
}
{
let binary_path = "sway/contracts/contract_test/out/release/contract_test-abi.json";
let expected_error = format!("expected \"{binary_path}\" to have '.bin' extension");
let binary_path =
fs::canonicalize("sway/contracts/contract_test/out/release/contract_test-abi.json")
.unwrap();
let expected_error = format!(
"expected \"{}\" to have '.bin' extension",
binary_path.display()
);

let error = Contract::load_from(binary_path, LoadConfiguration::default())
.expect_err("should have failed");
Expand Down
10 changes: 6 additions & 4 deletions e2e/tests/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ async fn test_init_storage_automatically() -> Result<()> {
#[tokio::test]
async fn storage_load_error_messages() {
{
let json_path = "sway/contracts/storage/out/release/no_file_on_path.json";
let expected_error = format!("io: file \"{json_path}\" does not exist");
let json_path = "sway/../sway/contracts/storage/out/release/no_file_on_path.json";
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let expected_error = format!("io: file \"{manifest_dir}/sway/contracts/storage/out/release/no_file_on_path.json\" does not exist");

let error = StorageConfiguration::default()
.add_slot_overrides_from_file(json_path)
Expand All @@ -97,8 +98,9 @@ async fn storage_load_error_messages() {
assert_eq!(error.to_string(), expected_error);
}
{
let json_path = "sway/contracts/storage/out/release/storage.bin";
let expected_error = format!("expected \"{json_path}\" to have '.json' extension");
let json_path = "sway/../sway/contracts/storage/out/release/storage.bin";
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let expected_error = format!("expected \"{manifest_dir}/sway/contracts/storage/out/release/storage.bin\" to have '.json' extension");

let error = StorageConfiguration::default()
.add_slot_overrides_from_file(json_path)
Expand Down
1 change: 1 addition & 0 deletions packages/fuels-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fuel-tx = { workspace = true }
fuel-types = { workspace = true, features = ["random"] }
fuels-core = { workspace = true, default-features = false }
itertools = { workspace = true }
path-clean = { workspace = true }
rand = { workspace = true, default-features = false }
semver = { workspace = true }
tai64 = { workspace = true, features = ["serde"] }
Expand Down
16 changes: 11 additions & 5 deletions packages/fuels-accounts/src/predicate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Debug, fs};
use std::{fmt::Debug, fs, path};

#[cfg(feature = "std")]
use fuels_core::types::{coin_type_id::CoinTypeId, input::Input, AssetId};
Expand Down Expand Up @@ -41,10 +41,16 @@ impl Predicate {

pub fn load_from(file_path: &str) -> Result<Self> {
let code = fs::read(file_path).map_err(|e| {
error!(
IO,
"could not read predicate binary {file_path:?}. Reason: {e}"
)
match path::absolute(file_path).map(path_clean::clean) {
Ok(clean_file_path) => error!(
IO,
"could not read predicate binary {clean_file_path:?}. Reason: {e}",
),
Err(e) => error!(
IO,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be ideal IMO is the following:
read the original path - success - > all good
read the original path -> fails -> clean it up - cleanup successful -> report nice err message
read the original path -> fails -> clean it up -> cleanup fails -> report original err message without mention of the cleanup failing

"failed to make path absolute: {file_path:?}. Reason: {e}"
),
}
})?;
Ok(Self::from_code(code))
}
Expand Down
2 changes: 1 addition & 1 deletion packages/fuels-core/src/types/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub type Result<T> = std::result::Result<T, Error>;
/// Those are: `IO`, `Codec`, `Provider`, `Other`.
#[macro_export]
macro_rules! error {
($err_variant:ident, $fmt_str: literal $(,$arg: expr)*) => {
($err_variant:ident, $fmt_str: literal $(,$arg: expr)* $(,)?) => {
$crate::types::errors::Error::$err_variant(format!($fmt_str,$($arg),*))
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/fuels-programs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fuel-types = { workspace = true, features = ["default"] }
fuels-accounts = { workspace = true }
fuels-core = { workspace = true }
itertools = { workspace = true }
path-clean = { workspace = true }
rand = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
Expand Down
19 changes: 13 additions & 6 deletions packages/fuels-programs/src/contract/regular.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{default::Default, fmt::Debug, path::Path};
use std::{
default::Default,
fmt::Debug,
path::{self, Path},
};

use fuel_tx::{Bytes32, ContractId, Salt, StorageSlot};
use fuels_accounts::Account;
Expand Down Expand Up @@ -99,17 +103,20 @@ impl Contract<Regular> {
binary_filepath: impl AsRef<Path>,
config: LoadConfiguration,
) -> Result<Contract<Regular>> {
let binary_filepath = binary_filepath.as_ref();
validate_path_and_extension(binary_filepath, "bin")?;
let clean_file_path = path::absolute(&binary_filepath)
.map(path_clean::clean)
.unwrap_or_else(|_| binary_filepath.as_ref().to_path_buf());
validate_path_and_extension(&clean_file_path, "bin")?;

let binary = std::fs::read(binary_filepath).map_err(|e| {
let binary = std::fs::read(&binary_filepath).map_err(|e| {
std::io::Error::new(
e.kind(),
format!("failed to read binary: {binary_filepath:?}: {e}"),
format!("failed to read binary: {clean_file_path:?}: {e}"),
)
})?;

let storage_slots = super::determine_storage_slots(config.storage, binary_filepath)?;
let storage_slots =
super::determine_storage_slots(config.storage, binary_filepath.as_ref())?;

Ok(Contract {
code: Regular::new(binary, config.configurables),
Expand Down
12 changes: 7 additions & 5 deletions packages/fuels-programs/src/contract/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
default::Default,
fmt::Debug,
io,
path::{Path, PathBuf},
path::{self, Path, PathBuf},
};

use fuel_tx::{Bytes32, StorageSlot};
Expand Down Expand Up @@ -94,13 +94,15 @@ impl StorageSlots {
}

pub(crate) fn load_from_file(storage_path: impl AsRef<Path>) -> Result<Self> {
let storage_path = storage_path.as_ref();
validate_path_and_extension(storage_path, "json")?;
let clean_storage_path = path::absolute(&storage_path)
.map(path_clean::clean)
.unwrap_or_else(|_| storage_path.as_ref().to_path_buf());
validate_path_and_extension(&clean_storage_path, "json")?;

let storage_json_string = std::fs::read_to_string(storage_path).map_err(|e| {
let storage_json_string = std::fs::read_to_string(&storage_path).map_err(|e| {
io::Error::new(
e.kind(),
format!("failed to read storage slots from: {storage_path:?}: {e}"),
format!("failed to read storage slots from: {clean_storage_path:?}: {e}"),
)
})?;

Expand Down
Loading