Merge pull request #917 from Demonthos/fix-more-complex-workspace-hot-reload

Fix more complex workspaces with hot reloading
This commit is contained in:
Jon Kelley authored and GitHub committed 2023-03-28 16:17:49 -07:00
commit adade2d5c5
1 file changed
+8 -10
@@ -29,7 +29,7 @@ pub struct FileMapBuildResult<Ctx: HotReloadingContext> {
pub struct FileMap<Ctx: HotReloadingContext> { pub struct FileMap<Ctx: HotReloadingContext> {
pub map: HashMap<PathBuf, (String, Option<Template<'static>>)>, pub map: HashMap<PathBuf, (String, Option<Template<'static>>)>,
in_workspace: HashMap<PathBuf, bool>, in_workspace: HashMap<PathBuf, Option<PathBuf>>,
phantom: std::marker::PhantomData<Ctx>, phantom: std::marker::PhantomData<Ctx>,
} }
@@ -112,11 +112,8 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
) { ) {
// if the file!() macro is invoked in a workspace, the path is relative to the workspace root, otherwise it's relative to the crate root // if the file!() macro is invoked in a workspace, the path is relative to the workspace root, otherwise it's relative to the crate root
// we need to check if the file is in a workspace or not and strip the prefix accordingly // we need to check if the file is in a workspace or not and strip the prefix accordingly
let prefix = if in_workspace { let prefix = if let Some(workspace) = &in_workspace {
crate_dir.parent().ok_or(io::Error::new( workspace
io::ErrorKind::Other,
"Could not load workspace",
))?
} else { } else {
crate_dir crate_dir
}; };
@@ -173,9 +170,9 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
Ok(UpdateResult::NeedsRebuild) Ok(UpdateResult::NeedsRebuild)
} }
fn child_in_workspace(&mut self, crate_dir: &Path) -> io::Result<bool> { fn child_in_workspace(&mut self, crate_dir: &Path) -> io::Result<Option<PathBuf>> {
if let Some(in_workspace) = self.in_workspace.get(crate_dir) { if let Some(in_workspace) = self.in_workspace.get(crate_dir) {
Ok(*in_workspace) Ok(in_workspace.clone())
} else { } else {
let mut cmd = Cmd::new(); let mut cmd = Cmd::new();
let manafest_path = crate_dir.join("Cargo.toml"); let manafest_path = crate_dir.join("Cargo.toml");
@@ -186,9 +183,10 @@ impl<Ctx: HotReloadingContext> FileMap<Ctx> {
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?; .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
let in_workspace = metadata.workspace_root != crate_dir; let in_workspace = metadata.workspace_root != crate_dir;
let workspace_path = in_workspace.then(|| metadata.workspace_root.into());
self.in_workspace self.in_workspace
.insert(crate_dir.to_path_buf(), in_workspace); .insert(crate_dir.to_path_buf(), workspace_path.clone());
Ok(in_workspace) Ok(workspace_path)
} }
} }
} }