Skip to content

Commit b46f966

Browse files
authored
Fix some issues with -o and module output (#41)
* Require that `-o` is passed because this will panic otherwise if it's not. * Don't use `NamedTempFile` because the file name ends up in a module's output from LLD, so make sure it's got the same file name.
1 parent fd6ea3f commit b46f966

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/lib.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ struct ComponentLdArgs {
250250

251251
/// Where to place the component output.
252252
#[clap(short, long)]
253-
output: Option<PathBuf>,
253+
output: PathBuf,
254254

255255
/// Print verbose output.
256256
#[clap(long)]
@@ -522,17 +522,29 @@ impl App {
522522
let mut cmd = self.lld();
523523
let linker = cmd.get_program().to_owned();
524524

525-
let lld_output =
526-
tempfile::NamedTempFile::new().context("failed to create temp output file")?;
525+
// If a temporary output is needed make sure it has the same file name
526+
// as the output of our command itself since LLD will embed this file
527+
// name in the name section of the output.
528+
let temp_dir = match self.component.output.parent() {
529+
Some(parent) => tempfile::TempDir::new_in(parent)?,
530+
None => tempfile::TempDir::new()?,
531+
};
532+
let temp_output = match self.component.output.file_name() {
533+
Some(name) => temp_dir.path().join(name),
534+
None => bail!(
535+
"output of {:?} does not have a file name",
536+
self.component.output
537+
),
538+
};
527539

528540
// Shared libraries don't get wit-component run below so place the
529541
// output directly at the desired output location. Otherwise output to a
530542
// temporary location for wit-component to read and then the real output
531543
// is created after wit-component runs.
532544
if self.skip_wit_component() {
533-
cmd.arg("-o").arg(self.component.output.as_ref().unwrap());
545+
cmd.arg("-o").arg(&self.component.output);
534546
} else {
535-
cmd.arg("-o").arg(lld_output.path());
547+
cmd.arg("-o").arg(&temp_output);
536548
}
537549

538550
if self.component.verbose {
@@ -555,8 +567,8 @@ impl App {
555567
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_COMMAND_ADAPTER;
556568
let proxy_adapter =
557569
wasi_preview1_component_adapter_provider::WASI_SNAPSHOT_PREVIEW1_PROXY_ADAPTER;
558-
let mut core_module = std::fs::read(lld_output.path())
559-
.with_context(|| format!("failed to read {linker:?} output"))?;
570+
let mut core_module = std::fs::read(&temp_output)
571+
.with_context(|| format!("failed to read {linker:?} output: {temp_output:?}"))?;
560572

561573
// Inspect the output module to see if it's a command or reactor.
562574
let mut exports_start = false;
@@ -636,8 +648,10 @@ impl App {
636648

637649
let component = encoder.encode().context("failed to encode component")?;
638650

639-
std::fs::write(self.component.output.as_ref().unwrap(), &component)
640-
.context("failed to write output file")?;
651+
std::fs::write(&self.component.output, &component).context(format!(
652+
"failed to write output file: {:?}",
653+
self.component.output
654+
))?;
641655

642656
Ok(())
643657
}

0 commit comments

Comments
 (0)