Skip to content

Commit ea78214

Browse files
authored
fix: paths in OCI & WIT subcommands (#286)
* fix: paths in OCI & WIT commands Signed-off-by: Lucas Fontes <lucas@cosmonic.com> * handling dir selection a bit more gracefully Signed-off-by: Lucas Fontes <lucas@cosmonic.com> --------- Signed-off-by: Lucas Fontes <lucas@cosmonic.com>
1 parent 11c4cd8 commit ea78214

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

crates/wash/src/cli/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ pub struct CliContext {
306306
config: Option<PathBuf>,
307307
// path to project dir. Usually current working dir
308308
project_dir: PathBuf,
309+
// the original working directory when the CLI was invoked, used for resolving relative paths in commands
310+
original_working_dir: PathBuf,
309311
}
310312

311313
impl Deref for CliContext {
@@ -406,9 +408,14 @@ impl CliContextBuilder {
406408
.start()
407409
.await?;
408410

411+
let original_working_dir = std::env::current_dir()
412+
.ok()
413+
.or_else(|| self.project_dir.clone())
414+
.context("failed to get current working directory and no project dir specified")?;
415+
409416
let project_dir = match &self.project_dir {
410417
Some(dir) => dir.clone(),
411-
None => std::env::current_dir()?,
418+
None => original_working_dir.clone(),
412419
};
413420

414421
// Change working directory to project path
@@ -418,6 +425,7 @@ impl CliContextBuilder {
418425
app_strategy,
419426
host,
420427
project_dir,
428+
original_working_dir,
421429
config: self.config,
422430
plugin_manager: plugin_manager.clone(),
423431
};
@@ -506,6 +514,10 @@ impl CliContext {
506514
}
507515
}
508516

517+
pub fn original_working_dir(&self) -> &PathBuf {
518+
&self.original_working_dir
519+
}
520+
509521
pub fn user_config_path(&self) -> std::path::PathBuf {
510522
if let Some(config) = &self.config {
511523
return config.clone();

crates/wash/src/cli/oci.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl PullCommand {
8585
let component_path = if self.component_path.is_absolute() {
8686
self.component_path.clone()
8787
} else {
88-
ctx.project_dir().join(&self.component_path)
88+
ctx.original_working_dir().join(&self.component_path)
8989
};
9090

9191
// Write the component to the specified output path
@@ -133,7 +133,7 @@ impl PushCommand {
133133
let component_path = if self.component_path.is_absolute() {
134134
self.component_path.clone()
135135
} else {
136-
ctx.project_dir().join(&self.component_path)
136+
ctx.original_working_dir().join(&self.component_path)
137137
};
138138

139139
let component = tokio::fs::read(&component_path)

crates/wash/src/cli/wit.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,11 @@ async fn handle_build(
792792

793793
// Determine output path
794794
let output_path = if let Some(output) = output_override {
795-
output.to_path_buf()
795+
if output.is_absolute() {
796+
output.to_path_buf()
797+
} else {
798+
ctx.original_working_dir().join(output)
799+
}
796800
} else {
797801
// Default to project root: <package-name>-<version>.wasm or <package-name>.wasm
798802
let filename = if let Some(ver) = &version {

0 commit comments

Comments
 (0)