Skip to content

Commit 6d74baa

Browse files
committed
fix(cli): support relative paths for IDL commands
Convert relative file paths to absolute paths using fs::canonicalize() before reading IDL files. This fixes the 'No such file or directory' error when running IDL commands from directories other than the project root. Affected functions: - idl_init - idl_upgrade - idl_convert - idl_type Fixes #2993
1 parent 95c26f5 commit 6d74baa

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

cli/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,11 @@ fn idl_init(
24792479
non_canonical: bool,
24802480
allow_localnet: bool,
24812481
) -> Result<()> {
2482+
// Convert relative path to absolute path
2483+
let idl_filepath = fs::canonicalize(&idl_filepath)
2484+
.map(|p| p.to_string_lossy().to_string())
2485+
.unwrap_or(idl_filepath);
2486+
24822487
// Get cluster URL and wallet path from Anchor config
24832488
let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;
24842489

@@ -2529,6 +2534,11 @@ fn idl_upgrade(
25292534
priority_fee: Option<u64>,
25302535
allow_localnet: bool,
25312536
) -> Result<()> {
2537+
// Convert relative path to absolute path
2538+
let idl_filepath = fs::canonicalize(&idl_filepath)
2539+
.map(|p| p.to_string_lossy().to_string())
2540+
.unwrap_or(idl_filepath);
2541+
25322542
// Get cluster URL and wallet path from Anchor config
25332543
let (cluster_url, wallet_path) = get_cluster_and_wallet(cfg_override)?;
25342544

@@ -2652,6 +2662,10 @@ fn idl_fetch(
26522662
}
26532663

26542664
fn idl_convert(path: String, out: Option<String>, program_id: Option<Pubkey>) -> Result<()> {
2665+
// Convert relative path to absolute path
2666+
let path = fs::canonicalize(&path)
2667+
.map(|p| p.to_string_lossy().to_string())
2668+
.unwrap_or(path);
26552669
let idl = fs::read(path)?;
26562670

26572671
// Set the `metadata.address` field based on the given `program_id`
@@ -2678,6 +2692,10 @@ fn idl_convert(path: String, out: Option<String>, program_id: Option<Pubkey>) ->
26782692
}
26792693

26802694
fn idl_type(path: String, out: Option<String>) -> Result<()> {
2695+
// Convert relative path to absolute path
2696+
let path = fs::canonicalize(&path)
2697+
.map(|p| p.to_string_lossy().to_string())
2698+
.unwrap_or(path);
26812699
let idl = fs::read(path)?;
26822700
let idl = convert_idl(&idl)?;
26832701
let types = idl_ts(&idl)?;

0 commit comments

Comments
 (0)