Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect against syncing and building the wrong project types #772

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* Added rich Source diffs in patch visualizer ([#748])
* Fix PatchTree performance issues ([#755])
* Don't override the initial enabled state for source diffing ([#760])
* Projects can no longer be built with the wrong file extension ([#772])
* Projects can no longer be served if they don't have a DataModel root ([#722])

[#761]: https://github.com/rojo-rbx/rojo/pull/761
[#745]: https://github.com/rojo-rbx/rojo/pull/745
Expand All @@ -50,6 +52,7 @@
[#748]: https://github.com/rojo-rbx/rojo/pull/748
[#755]: https://github.com/rojo-rbx/rojo/pull/755
[#760]: https://github.com/rojo-rbx/rojo/pull/760
[#772]: https://github.com/rojo-rbx/rojo/pull/772

## [7.3.0] - April 22, 2023
* Added `$attributes` to project format. ([#574])
Expand Down
16 changes: 16 additions & 0 deletions src/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const UNKNOWN_OUTPUT_KIND_ERR: &str = "Could not detect what kind of file to bui
const UNKNOWN_PLUGIN_KIND_ERR: &str = "Could not detect what kind of file to build. \
Expected plugin file to end in .rbxm or .rbxmx.";

const PLACE_AS_MODEL_ERR: &str = "Cannot build a place project file as a model file. \
Expected output file to end in .rbxl or .rbxlx.";
const MODEL_AS_PLACE_ERR: &str = "Cannot build a model project file as a place file. \
Expected output file file to end in .rbxm or .rbxmx.";

/// Generates a model or place file from the Rojo project.
#[derive(Debug, Parser)]
pub struct BuildCommand {
Expand Down Expand Up @@ -168,6 +173,17 @@ fn write_model(
let tree = session.tree();
let root_id = tree.get_root_id();

let root_class = tree.get_instance(root_id).unwrap().class_name();

// Bail out of building a model if the internal project is wrong.
if matches!(output_kind, OutputKind::Rbxm | OutputKind::Rbxmx) && root_class == "DataModel" {
bail!(PLACE_AS_MODEL_ERR)
} else if matches!(output_kind, OutputKind::Rbxl | OutputKind::Rbxlx)
&& root_class != "DataModel"
{
bail!(MODEL_AS_PLACE_ERR)
}

log::trace!("Opening output file for write");
let mut file = BufWriter::new(File::create(output)?);

Expand Down
14 changes: 14 additions & 0 deletions src/cli/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ use super::{resolve_path, GlobalOptions};
const DEFAULT_BIND_ADDRESS: Ipv4Addr = Ipv4Addr::new(127, 0, 0, 1);
const DEFAULT_PORT: u16 = 34872;

const SYNCING_MODEL_ERR: &str = "Cannot sync a model project file. \
Projects must have a `DataModel` at their root to be synced.";

Dekkonot marked this conversation as resolved.
Show resolved Hide resolved
/// Expose a Rojo project to the Rojo Studio plugin.
#[derive(Debug, Parser)]
pub struct ServeCommand {
Expand All @@ -41,6 +44,17 @@ impl ServeCommand {

let session = Arc::new(ServeSession::new(vfs, project_path)?);

// `ServeSession.tree()` is locking, so we have to let it fall out
// of scope when we're done.
{
let tree = session.tree();
// This cannot fail because a root cant' ever not exist.
Dekkonot marked this conversation as resolved.
Show resolved Hide resolved
let root = tree.get_instance(tree.get_root_id()).unwrap();
if root.class_name() != "DataModel" {
anyhow::bail!(SYNCING_MODEL_ERR)
}
}

let ip = self
.address
.or_else(|| session.serve_address())
Expand Down
Loading