Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

raea play on Windows: plain paths for home() and the bundler's folders, a junction for the work folder's node_modules

tobitegec88adb4695db
diff --git a/Cargo.lock b/Cargo.lock
index bfe9a954..021359f1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -14111,6 +14111,7 @@ name = "raea-runtime-bundle"
version = "0.1.0"
dependencies = [
"dunce",
+ "junction",
"libloading",
"raea-core-scene",
"raea-io-home",
diff --git a/engine/bundle/src/edit.rs b/engine/bundle/src/edit.rs
index 5063517b..d20364dc 100644
--- a/engine/bundle/src/edit.rs
+++ b/engine/bundle/src/edit.rs
@@ -24,11 +24,39 @@ pub struct EditResolve(Edit);
impl EditResolve {
/// its folders as the resolver names modules: symlinks resolved (the Mac's temp folder is /var, a link to /private/var)
pub fn new(e: Edit) -> Self {
- let real = |p: PathBuf| p.canonicalize().unwrap_or(p);
+ let real = |p: PathBuf| p.canonicalize().map(plain).unwrap_or(p);
EditResolve(Edit { dir: real(e.dir), js: real(e.js), work: real(e.work) })
}
}
+/// A path without the verbatim `\\?\` prefix Windows' canonicalize() puts before a drive path. The resolver names
+/// modules without it, so with it no importer was ever under the work folder: a live game's own `import "react"`
+/// went to the engine's React instead of the live runtime its tsconfig paths name, and `raea play` threw "not a
+/// function" before the first frame. Any other path, a `\\?\UNC\` one included, is kept as it is.
+fn plain(p: PathBuf) -> PathBuf {
+ match p.to_str().and_then(|s| s.strip_prefix(r"\\?\")).filter(|r| r.as_bytes().get(1) == Some(&b':')) {
+ Some(r) => r.into(),
+ None => p,
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn its_folders_are_named_as_the_resolver_names_modules() {
+ assert_eq!(plain(PathBuf::from(r"\\?\C:\Temp\raea-cli-1")), PathBuf::from(r"C:\Temp\raea-cli-1"));
+ assert_eq!(plain(PathBuf::from(r"\\?\UNC\server\share")), PathBuf::from(r"\\?\UNC\server\share"));
+ assert_eq!(plain(PathBuf::from("/private/var/x")), PathBuf::from("/private/var/x"));
+ // a real folder: canonicalized on Windows that was \\?\C:\..., which no importer starts with
+ let dir = std::env::temp_dir();
+ let r = EditResolve::new(Edit { dir: dir.clone(), js: dir.clone(), work: dir.clone() });
+ assert!(!r.0.work.display().to_string().starts_with(r"\\?\"), "{}", r.0.work.display());
+ assert!(dir.join("entry.tsx").starts_with(&r.0.work) || cfg!(unix), "{} under {}", dir.display(), r.0.work.display());
+ }
+}
+
/// `spec` resolved by Rolldown's resolver as if imported from a file in `dir`
async fn resolve_from(ctx: &PluginContext, spec: &str, dir: &Path) -> anyhow::Result<Option<HookResolveIdOutput>> {
let importer = dir.join("_raea.js");
diff --git a/io/home/src/lib.rs b/io/home/src/lib.rs
index c5851281..ee9917a7 100644
--- a/io/home/src/lib.rs
+++ b/io/home/src/lib.rs
@@ -13,7 +13,7 @@ pub fn home() -> std::path::PathBuf {
if let Some(h) = std::env::var_os("RAEA_HOME").filter(|v| !v.is_empty()) {
return std::path::PathBuf::from(h);
}
- let installed = std::env::current_exe().ok().and_then(|e| e.canonicalize().ok()).and_then(|e| e.parent()?.parent().map(|p| p.join("share/raea")));
+ let installed = std::env::current_exe().ok().and_then(|e| e.canonicalize().ok()).map(plain).and_then(|e| e.parent()?.parent().map(|p| p.join("share/raea")));
if let Some(d) = installed.filter(|d| d.join("sdk/raea/src").is_dir()) {
return d;
}
@@ -22,11 +22,21 @@ pub fn home() -> std::path::PathBuf {
return std::path::PathBuf::from("/nonexistent/raea");
}
let tree = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
- tree.canonicalize().unwrap_or(tree)
+ tree.canonicalize().map(plain).unwrap_or(tree)
})
.clone()
}
+/// A path without the verbatim `\\?\` prefix Windows' canonicalize() puts before a drive path: Rolldown's resolver
+/// can't open an import that starts with it, so `raea play` couldn't bundle a game (the lite entry imports bevy-react's
+/// bridge.ts by its path under home()). Any other path, a `\\?\UNC\` one included, is kept as it is.
+fn plain(p: std::path::PathBuf) -> std::path::PathBuf {
+ match p.to_str().and_then(|s| s.strip_prefix(r"\\?\")).filter(|r| r.as_bytes().get(1) == Some(&b':')) {
+ Some(r) => r.into(),
+ None => p,
+ }
+}
+
/// raea's per-user cache folder: ~/Library/Caches/raea on macOS, $XDG_CACHE_HOME/raea (~/.cache/raea by default)
/// elsewhere on Unix, %LOCALAPPDATA%\raea\cache on Windows. None when that platform's home isn't set.
pub fn caches() -> Option<std::path::PathBuf> {
@@ -100,6 +110,19 @@ mod tests {
assert_eq!(super::src_on("assets/m.png", true), "assets/m.png");
}
+ #[test]
+ fn home_is_no_verbatim_path() {
+ use std::path::PathBuf;
+ assert_eq!(super::plain(PathBuf::from(r"\\?\C:\github\raea")), PathBuf::from(r"C:\github\raea"));
+ assert_eq!(super::plain(PathBuf::from(r"\\?\UNC\server\share")), PathBuf::from(r"\\?\UNC\server\share"));
+ assert_eq!(super::plain(PathBuf::from("/Users/a/raea")), PathBuf::from("/Users/a/raea"));
+ // the tree a test build came from, canonicalized: on Windows that added \\?\ before
+ if std::env::var_os("RAEA_HOME").is_none() {
+ let home = super::home().display().to_string();
+ assert!(!home.starts_with(r"\\?\"), "{home}");
+ }
+ }
+
#[test]
fn caches_and_logs_follow_the_platform() {
// one test, so nothing else reads these variables while it sets them
diff --git a/runtime/bundle/Cargo.toml b/runtime/bundle/Cargo.toml
index 4991a524..26fa3be0 100644
--- a/runtime/bundle/Cargo.toml
+++ b/runtime/bundle/Cargo.toml
@@ -27,6 +27,10 @@ regex = "1"
# (f60e0b7: every JSON map an IndexMap, every number its text, pong's apply 87 -> 159 allocations)
libloading = "0.8"
+# the work folder's node_modules link on Windows (fresh.rs link_node_modules), as raea-io-npm makes its links
+[target.'cfg(windows)'.dependencies]
+junction = "1"
+
[dev-dependencies]
# tests/mezz.rs writes its long-GOP clip with the soft backend's H.264
raea-io-media = { path = "../../io/media", default-features = false, features = ["apple", "soft"] }
diff --git a/runtime/bundle/src/fresh.rs b/runtime/bundle/src/fresh.rs
index 6ba4dab0..c4c28a4b 100644
--- a/runtime/bundle/src/fresh.rs
+++ b/runtime/bundle/src/fresh.rs
@@ -180,7 +180,7 @@ pub fn bundle_fresh(tsx: &Path, out_js: &Path, o: &CompOpts, portable: Option<&P
static ONE: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _one = ONE.lock().unwrap_or_else(|e| e.into_inner());
let work = std::env::temp_dir().join(format!("raea-cli-b{}", std::process::id()));
- let _ = std::fs::remove_dir_all(&work);
+ remove_work(&work);
copy_dir(&dir, &work.join("src"), 0)?;
let motion_src = raea_io_home::home().join("sdk/raea/src").canonicalize().map_err(|e| format!("raea/motion/src: {e}"))?;
copy_dir(&motion_src, &work.join("motion"), 0)?;
@@ -243,8 +243,7 @@ pub fn bundle_fresh(tsx: &Path, out_js: &Path, o: &CompOpts, portable: Option<&P
}
}
let js = raea_io_home::home().join("sdk/react/js");
- #[cfg(unix)]
- std::os::unix::fs::symlink(js.join("node_modules"), work.join("node_modules")).map_err(|e| format!("node_modules: {e}"))?;
+ link_node_modules(&js, &work)?;
if o.lite {
// the game's code and the SDK's resolve "react" to the live view runtime (tsconfig paths cover the files under
// the work folder; packages in node_modules keep their own React)
@@ -267,6 +266,45 @@ pub fn bundle_fresh(tsx: &Path, out_js: &Path, o: &CompOpts, portable: Option<&P
eprintln!("raea bundle: {} in {:.1} ms: the work folder {:.1} ms, Rolldown {:.1} ms", tsx.display(), t0.elapsed().as_secs_f64() * 1e3, t_work.as_secs_f64() * 1e3, (t0.elapsed() - t_work).as_secs_f64() * 1e3);
}
std::fs::write(out_js, code).map_err(|e| format!("{}: {e}", out_js.display()))?;
- let _ = std::fs::remove_dir_all(&work);
+ remove_work(&work);
+ Ok(())
+}
+
+/// The work folder's node_modules: the engine's (sdk/react/js's), linked. On Windows a junction, which needs none of the
+/// rights a symlink does (Developer Mode or an admin), as raea-io-npm links packages. Windows had no link at all, so the
+/// live runtime's `../../node_modules/bevy-react/src/bridge` resolved nothing and `raea play` couldn't bundle a game.
+fn link_node_modules(js: &Path, work: &Path) -> R<()> {
+ let (to, at) = (js.join("node_modules"), work.join("node_modules"));
+ #[cfg(unix)]
+ std::os::unix::fs::symlink(&to, &at).map_err(|e| format!("node_modules: {e}"))?;
+ #[cfg(windows)]
+ junction::create(&to, &at).map_err(|e| format!("node_modules: {e}"))?;
Ok(())
}
+
+/// The work folder removed: on Windows its node_modules junction first, by remove_dir, which takes the link and never
+/// the engine's packages it points at
+fn remove_work(work: &Path) {
+ #[cfg(windows)]
+ let _ = std::fs::remove_dir(work.join("node_modules"));
+ let _ = std::fs::remove_dir_all(work);
+}
+
+#[cfg(test)]
+mod link_tests {
+ #[test]
+ fn the_work_folder_reads_the_engines_packages_and_its_removal_keeps_them() {
+ let base = std::env::temp_dir().join(format!("raea-link-test-{}", std::process::id()));
+ let (js, work) = (base.join("js"), base.join("work"));
+ std::fs::create_dir_all(js.join("node_modules/bevy-react/src")).unwrap();
+ std::fs::write(js.join("node_modules/bevy-react/src/bridge.ts"), "export {};").unwrap();
+ std::fs::create_dir_all(work.join("lite/parts")).unwrap();
+ super::link_node_modules(&js, &work).unwrap();
+ // what lite/parts/root.ts imports, read through the link
+ assert_eq!(std::fs::read_to_string(work.join("lite/parts/../../node_modules/bevy-react/src/bridge.ts")).unwrap(), "export {};");
+ super::remove_work(&work);
+ assert!(!work.exists(), "the work folder is gone");
+ assert!(js.join("node_modules/bevy-react/src/bridge.ts").exists(), "the engine's packages are kept");
+ let _ = std::fs::remove_dir_all(&base);
+ }
+}