Skip to content

Commit 30747ac

Browse files
committed
Fix incorrect watch feature handling in build
1 parent c4613d7 commit 30747ac

File tree

2 files changed

+37
-27
lines changed

2 files changed

+37
-27
lines changed

crates/cargo-gpu/src/build.rs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,14 @@
22
#![allow(clippy::unwrap_used, reason = "this is basically a test")]
33
//! `cargo gpu build`, analogous to `cargo build`
44
5-
use spirv_builder::SpirvBuilder;
6-
use std::path::PathBuf;
7-
8-
#[cfg(feature = "watch")]
95
use anyhow::Context as _;
10-
#[cfg(feature = "watch")]
11-
use spirv_builder::{CompileResult, ModuleResult};
12-
#[cfg(feature = "watch")]
6+
use spirv_builder::{CompileResult, ModuleResult, SpirvBuilder};
137
use std::io::Write as _;
8+
use std::path::PathBuf;
149

1510
use crate::install::Install;
16-
use crate::lockfile::LockfileMismatchHandler;
17-
18-
#[cfg(feature = "watch")]
1911
use crate::linkage::Linkage;
12+
use crate::lockfile::LockfileMismatchHandler;
2013

2114
/// Args for just a build
2215
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
@@ -102,32 +95,51 @@ impl Build {
10295
);
10396

10497
#[cfg(feature = "watch")]
105-
if self.build.watch {
98+
let watching = self.build.watch;
99+
#[cfg(not(feature = "watch"))]
100+
let watching = false;
101+
if watching {
102+
return self.watch();
103+
}
104+
105+
self.build()
106+
}
107+
108+
/// Builds shader crate using [`SpirvBuilder`].
109+
fn build(&self) -> anyhow::Result<()> {
110+
crate::user_output!(
111+
"Compiling shaders at {}...\n",
112+
self.install.shader_crate.display()
113+
);
114+
let result = self.build.spirv_builder.build()?;
115+
self.parse_compilation_result(&result)?;
116+
Ok(())
117+
}
118+
119+
/// Watches shader crate for changes using [`SpirvBuilder`]
120+
/// or returns an error depending on presence of `watch` feature.
121+
fn watch(&self) -> anyhow::Result<()> {
122+
#[cfg(feature = "watch")]
123+
{
106124
let this = self.clone();
107125
self.build
108126
.spirv_builder
109127
.watch(move |result, accept| {
110-
let result1 = this.parse_compilation_result(&result);
128+
let parse_result = this.parse_compilation_result(&result);
111129
if let Some(accept) = accept {
112-
accept.submit(result1);
130+
accept.submit(parse_result);
113131
}
114132
})?
115-
.context("unreachable")??;
116-
std::thread::park();
117-
} else {
118-
crate::user_output!(
119-
"Compiling shaders at {}...\n",
120-
self.install.shader_crate.display()
121-
);
122-
let result = self.build.spirv_builder.build()?;
123-
self.parse_compilation_result(&result)?;
133+
.context("should always return the first compile result")
134+
.flatten()?;
135+
anyhow::bail!("unexpected end of watch")
124136
}
125137

126-
Ok(())
138+
#[cfg(not(feature = "watch"))]
139+
anyhow::bail!("cannot watch for changes without the `watch` feature")
127140
}
128141

129-
/// Parses compilation result from `SpirvBuilder` and writes it out to a file
130-
#[cfg(feature = "watch")]
142+
/// Parses compilation result from [`SpirvBuilder`] and writes it out to a file
131143
fn parse_compilation_result(&self, result: &CompileResult) -> anyhow::Result<()> {
132144
let shaders = match &result.module {
133145
ModuleResult::MultiModule(modules) => {

crates/cargo-gpu/src/linkage.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Mainly for the Linkage struct, which is written to a json file.
22
3-
#![cfg(feature = "watch")]
4-
53
/// Shader source and entry point that can be used to create shader linkage.
64
#[derive(serde::Serialize, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
75
pub struct Linkage {

0 commit comments

Comments
 (0)