Skip to content

Commit

Permalink
Add check to ensure plugin version matches cargo version (rojo-rbx#794)
Browse files Browse the repository at this point in the history
This modifies Rojo's build script to throw a fit if we're building a
plugin with a semver incompatible version. In the process, it moves the
version of the plugin to a file named `Version.txt` that's parsed at
runtime. This should be minimally invasive but it's technically worse
for performance than the hardcoded table and string we had before.

This feels better than a CI check or just manually verifying because it
makes it physically impossible for us to forget since Rojo won't build
with it being wrong.
  • Loading branch information
Dekkonot committed Oct 3, 2023
1 parent eab7c60 commit 010e50a
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ anyhow = "1.0.44"
bincode = "1.3.3"
fs-err = "2.6.0"
maplit = "1.0.2"
semver = "1.0.19"

[dev-dependencies]
rojo-insta-ext = { path = "crates/rojo-insta-ext" }
Expand Down
15 changes: 15 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use fs_err as fs;
use fs_err::File;
use maplit::hashmap;
use memofs::VfsSnapshot;
use semver::Version;

fn snapshot_from_fs_path(path: &Path) -> io::Result<VfsSnapshot> {
println!("cargo:rerun-if-changed={}", path.display());
Expand Down Expand Up @@ -43,6 +44,19 @@ fn main() -> Result<(), anyhow::Error> {
let root_dir = env::var_os("CARGO_MANIFEST_DIR").unwrap();
let plugin_root = PathBuf::from(root_dir).join("plugin");

let our_version = Version::parse(env::var_os("CARGO_PKG_VERSION").unwrap().to_str().unwrap())?;
let plugin_version =
Version::parse(fs::read_to_string(&plugin_root.join("Version.txt"))?.trim())?;

assert!(
our_version.major == plugin_version.major,
"plugin version does not match Cargo version"
);
assert!(
our_version.minor == plugin_version.minor,
"plugin version does not match Cargo version"
);

let snapshot = VfsSnapshot::dir(hashmap! {
"default.project.json" => snapshot_from_fs_path(&plugin_root.join("default.project.json"))?,
"fmt" => snapshot_from_fs_path(&plugin_root.join("fmt"))?,
Expand All @@ -51,6 +65,7 @@ fn main() -> Result<(), anyhow::Error> {
"rbx_dom_lua" => snapshot_from_fs_path(&plugin_root.join("rbx_dom_lua"))?,
"src" => snapshot_from_fs_path(&plugin_root.join("src"))?,
"Packages" => snapshot_from_fs_path(&plugin_root.join("Packages"))?,
"Version.txt" => snapshot_from_fs_path(&plugin_root.join("Version.txt"))?,
});

let out_path = Path::new(&out_dir).join("plugin.bincode");
Expand Down
1 change: 1 addition & 0 deletions plugin/Version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.3.0
50 changes: 26 additions & 24 deletions plugin/default.project.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
{
"name": "Rojo",
"tree": {
"$className": "Folder",
"Plugin": {
"$path": "src"
},
"Packages": {
"$path": "Packages",

"Log": {
"$path": "log"
},
"Http": {
"$path": "http"
},
"Fmt": {
"$path": "fmt"
},
"RbxDom": {
"$path": "rbx_dom_lua"
}
}
}
}
"name": "Rojo",
"tree": {
"$className": "Folder",
"Plugin": {
"$path": "src"
},
"Packages": {
"$path": "Packages",
"Log": {
"$path": "log"
},
"Http": {
"$path": "http"
},
"Fmt": {
"$path": "fmt"
},
"RbxDom": {
"$path": "rbx_dom_lua"
}
},
"Version": {
"$path": "Version.txt"
}
}
}
16 changes: 14 additions & 2 deletions plugin/src/Config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ local strict = require(script.Parent.strict)

local isDevBuild = script.Parent.Parent:FindFirstChild("ROJO_DEV_BUILD") ~= nil

local Version = script.Parent.Parent.Version
local realVersion = Version.Value:split(".")

for i = 1, 3 do
local num = tonumber(realVersion[i])
if num then
realVersion[i] = num
else
error(("invalid version `%s` (field %d)"):format(realVersion[i], i))
end
end

return strict("Config", {
isDevBuild = isDevBuild,
codename = "Epiphany",
version = { 7, 3, 0 },
expectedServerVersionString = "7.2 or newer",
version = realVersion,
expectedServerVersionString = ("%d.%d or newer"):format(realVersion[1], realVersion[2]),
protocolVersion = 4,
defaultHost = "localhost",
defaultPort = "34872",
Expand Down

0 comments on commit 010e50a

Please sign in to comment.