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

Fix dev dependency link generation #131

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/installation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,30 @@ impl InstallationContext {
// package links for its dependencies.
if package_id == &root_package_id {
if let Some(deps) = shared_deps {
self.write_root_package_links(Realm::Shared, deps, Realm::Shared)?;
self.write_root_package_links(Realm::Shared, deps, resolved)?;
}

if let Some(deps) = server_deps {
self.write_root_package_links(Realm::Server, deps, Realm::Server)?;
self.write_root_package_links(Realm::Server, deps, resolved)?;
}

if let Some(deps) = dev_deps {
self.write_root_package_links(Realm::Dev, deps, Realm::Dev)?;
self.write_root_package_links(Realm::Dev, deps, resolved)?;
}
} else {
let metadata = resolved.metadata.get(package_id).unwrap();

let package_realm = metadata.origin_realm;

if let Some(deps) = shared_deps {
self.write_package_links(package_id, package_realm, deps, Realm::Shared)?;
self.write_package_links(package_id, package_realm, deps, resolved)?;
}

if let Some(deps) = server_deps {
self.write_package_links(package_id, package_realm, deps, Realm::Server)?;
self.write_package_links(package_id, package_realm, deps, resolved)?;
}

if let Some(deps) = dev_deps {
self.write_package_links(package_id, package_realm, deps, resolved)?;
}

let source_registry = &resolved.metadata[package_id].source_registry;
Expand Down Expand Up @@ -185,10 +188,6 @@ impl InstallationContext {
})?;

let contents = formatdoc! {r#"
if not game:GetService("RunService"):IsServer() then
error("{full_name} is a server-only package.", 2)
end

return require({packages}._Index["{full_name}"]["{short_name}"])
"#,
packages = server_path,
Expand All @@ -203,7 +202,7 @@ impl InstallationContext {
&self,
root_realm: Realm,
dependencies: impl IntoIterator<Item = (K, &'a PackageId)>,
dependencies_realm: Realm,
resolved: &Resolve,
) -> anyhow::Result<()> {
log::debug!("Writing root package links");

Expand All @@ -217,6 +216,7 @@ impl InstallationContext {
fs::create_dir_all(base_path)?;

for (dep_name, dep_package_id) in dependencies {
let dependencies_realm = resolved.metadata.get(dep_package_id).unwrap().origin_realm;
let path = base_path.join(format!("{}.lua", dep_name));

let contents = match (root_realm, dependencies_realm) {
Expand All @@ -240,7 +240,7 @@ impl InstallationContext {
package_id: &PackageId,
package_realm: Realm,
dependencies: impl IntoIterator<Item = (K, &'a PackageId)>,
dependencies_realm: Realm,
resolved: &Resolve,
) -> anyhow::Result<()> {
log::debug!("Writing package links for {}", package_id);

Expand All @@ -256,6 +256,7 @@ impl InstallationContext {
fs::create_dir_all(&base_path)?;

for (dep_name, dep_package_id) in dependencies {
let dependencies_realm = resolved.metadata.get(dep_package_id).unwrap().origin_realm;
let path = base_path.join(format!("{}.lua", dep_name));

let contents = match (package_realm, dependencies_realm) {
Expand Down
10 changes: 8 additions & 2 deletions src/resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ impl Resolve {
}

/// A single node in the package resolution graph.
/// Origin realm is the "most restrictive" realm the package can still be dependended
/// upon. It is where the package gets placed during install.
/// See [ origin_realm clarification ]. In the resolve function for more info.
#[derive(Debug, Serialize)]
pub struct ResolvePackageMetadata {
pub realm: Realm,
Expand Down Expand Up @@ -132,11 +135,14 @@ pub fn resolve(
.get_mut(package_id)
.expect("activated package was missing metadata");

// We want to set the origin to the least restrictive origin possible.
// [ origin_realm clarification ]
// We want to set the origin to the most restrictive origin possible.
// For example we want to keep packages in the dev realm unless a dependency
// with a shared/server origin requires it. This way server/shared dependencies
// which only originate from dev dependencies get put into the dev folder even
// if they usually belong to another realm.
// if they usually belong to another realm. Likewise we want to keep shared
// dependencies in the server realm unless they are explicitly required as a
// shared dependency.
let realm_match = match (metadata.origin_realm, dependency_request.origin_realm) {
(_, Realm::Shared) => Realm::Shared,
(Realm::Shared, _) => Realm::Shared,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "dev-dependency",
"tree": {
"$path": "src"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local Minimal = require(script.Parent.Minimal)

return function()
print(Minimal)
end
15 changes: 15 additions & 0 deletions test-projects/dev-dependency-also-required-as-non-dev/wally.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "biff/dev-dependency-also-required-as-non-dev"
version = "0.1.0"
license = "MIT"
realm = "server"
registry = "test-registries/primary-registry"

[place]
server-packages = "game.ServerScriptStorage.Packages"

[server-dependencies]
Transitive = "biff/transitive-dependency@0.1.0"

[dev-dependencies]
Minimal = "biff/minimal@0.1.0"
4 changes: 0 additions & 4 deletions test-projects/dev-dependency/wally.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@ registry = "test-registries/primary-registry"
[place]
server-packages = "game.ServerScriptStorage.Packages"

[server-dependencies]
Minimal = "biff/minimal@0.1.0"
One = "biff/one-dependency@0.1.0"

[dev-dependencies]
Transitive = "biff/transitive-dependency@0.1.0"
5 changes: 5 additions & 0 deletions tests/integration/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ fn dev_dependency() {
run_test("dev-dependency");
}

#[test]
fn dev_dependency_also_required_as_non_dev() {
run_test("dev-dependency-also-required-as-non-dev");
}

#[test]
fn cross_realm_dependency() {
run_test("cross-realm-dependency");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@ expression: result
---
DevPackages:
Transitive.lua: "return require(script.Parent._Index[\"biff_transitive-dependency@0.1.0\"][\"transitive-dependency\"])\n"
_Index:
biff_transitive-dependency@0.1.0:
OneDependency.lua: "if not game:GetService(\"RunService\"):IsServer() then\n error(\"biff_one-dependency@0.1.0 is a server-only package.\", 2)\nend\n\nreturn require(game.ServerScriptStorage.Packages._Index[\"biff_one-dependency@0.1.0\"][\"one-dependency\"])\n"
transitive-dependency:
init.lua: "return \"hey\""
ServerPackages:
Minimal.lua: "return require(script.Parent._Index[\"biff_minimal@0.1.0\"][\"minimal\"])\n"
One.lua: "return require(script.Parent._Index[\"biff_one-dependency@0.1.0\"][\"one-dependency\"])\n"
_Index:
biff_minimal@0.1.0:
minimal:
Expand All @@ -20,9 +12,13 @@ ServerPackages:
Minimal.lua: "return require(script.Parent.Parent[\"biff_minimal@0.1.0\"][\"minimal\"])\n"
one-dependency:
init.lua: "return \"hey\""
biff_transitive-dependency@0.1.0:
OneDependency.lua: "return require(script.Parent.Parent[\"biff_one-dependency@0.1.0\"][\"one-dependency\"])\n"
transitive-dependency:
init.lua: "return \"hey\""
default.project.json: "{\n\t\"name\": \"dev-dependency\",\n\t\"tree\": {\n\t\t\"$path\": \"src\"\n\t}\n}"
src:
init.lua: "local Minimal = require(script.Parent.Minimal)\n\nreturn function()\n\tprint(Minimal)\nend"
wally.lock: "# This file is automatically @generated by Wally.\n# It is not intended for manual editing.\nregistry = \"test\"\n\n[[package]]\nname = \"biff/dev-dependency\"\nversion = \"0.1.0\"\ndependencies = [[\"Minimal\", \"biff/minimal@0.1.0\"], [\"One\", \"biff/one-dependency@0.1.0\"], [\"Transitive\", \"biff/transitive-dependency@0.1.0\"]]\n\n[[package]]\nname = \"biff/minimal\"\nversion = \"0.1.0\"\ndependencies = []\n\n[[package]]\nname = \"biff/one-dependency\"\nversion = \"0.1.0\"\ndependencies = [[\"Minimal\", \"biff/minimal@0.1.0\"]]\n\n[[package]]\nname = \"biff/transitive-dependency\"\nversion = \"0.1.0\"\ndependencies = [[\"OneDependency\", \"biff/one-dependency@0.1.0\"]]\n"
wally.toml: "[package]\nname = \"biff/dev-dependency\"\nversion = \"0.1.0\"\nlicense = \"MIT\"\nrealm = \"server\"\nregistry = \"test-registries/primary-registry\"\n\n[place]\nserver-packages = \"game.ServerScriptStorage.Packages\"\n\n[server-dependencies]\nMinimal = \"biff/minimal@0.1.0\"\nOne = \"biff/one-dependency@0.1.0\"\n\n[dev-dependencies]\nTransitive = \"biff/transitive-dependency@0.1.0\"\n"
wally.lock: "# This file is automatically @generated by Wally.\n# It is not intended for manual editing.\nregistry = \"test\"\n\n[[package]]\nname = \"biff/dev-dependency\"\nversion = \"0.1.0\"\ndependencies = [[\"Transitive\", \"biff/transitive-dependency@0.1.0\"]]\n\n[[package]]\nname = \"biff/minimal\"\nversion = \"0.1.0\"\ndependencies = []\n\n[[package]]\nname = \"biff/one-dependency\"\nversion = \"0.1.0\"\ndependencies = [[\"Minimal\", \"biff/minimal@0.1.0\"]]\n\n[[package]]\nname = \"biff/transitive-dependency\"\nversion = \"0.1.0\"\ndependencies = [[\"OneDependency\", \"biff/one-dependency@0.1.0\"]]\n"
wally.toml: "[package]\nname = \"biff/dev-dependency\"\nversion = \"0.1.0\"\nlicense = \"MIT\"\nrealm = \"server\"\nregistry = \"test-registries/primary-registry\"\n\n[place]\nserver-packages = \"game.ServerScriptStorage.Packages\"\n\n[dev-dependencies]\nTransitive = \"biff/transitive-dependency@0.1.0\"\n"

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
source: tests/integration/install.rs
expression: result
---
DevPackages:
Minimal.lua: "return require(game.ServerScriptStorage.Packages._Index[\"biff_minimal@0.1.0\"][\"minimal\"])\n"
ServerPackages:
Transitive.lua: "return require(script.Parent._Index[\"biff_transitive-dependency@0.1.0\"][\"transitive-dependency\"])\n"
_Index:
biff_minimal@0.1.0:
minimal:
init.lua: "return \"hey\""
biff_one-dependency@0.1.0:
Minimal.lua: "return require(script.Parent.Parent[\"biff_minimal@0.1.0\"][\"minimal\"])\n"
one-dependency:
init.lua: "return \"hey\""
biff_transitive-dependency@0.1.0:
OneDependency.lua: "return require(script.Parent.Parent[\"biff_one-dependency@0.1.0\"][\"one-dependency\"])\n"
transitive-dependency:
init.lua: "return \"hey\""
default.project.json: "{\n\t\"name\": \"dev-dependency\",\n\t\"tree\": {\n\t\t\"$path\": \"src\"\n\t}\n}"
src:
init.lua: "local Minimal = require(script.Parent.Minimal)\n\nreturn function()\n\tprint(Minimal)\nend"
wally.lock: "# This file is automatically @generated by Wally.\n# It is not intended for manual editing.\nregistry = \"test\"\n\n[[package]]\nname = \"biff/dev-dependency-also-required-as-non-dev\"\nversion = \"0.1.0\"\ndependencies = [[\"Transitive\", \"biff/transitive-dependency@0.1.0\"], [\"Minimal\", \"biff/minimal@0.1.0\"]]\n\n[[package]]\nname = \"biff/minimal\"\nversion = \"0.1.0\"\ndependencies = []\n\n[[package]]\nname = \"biff/one-dependency\"\nversion = \"0.1.0\"\ndependencies = [[\"Minimal\", \"biff/minimal@0.1.0\"]]\n\n[[package]]\nname = \"biff/transitive-dependency\"\nversion = \"0.1.0\"\ndependencies = [[\"OneDependency\", \"biff/one-dependency@0.1.0\"]]\n"
wally.toml: "[package]\nname = \"biff/dev-dependency-also-required-as-non-dev\"\nversion = \"0.1.0\"\nlicense = \"MIT\"\nrealm = \"server\"\nregistry = \"test-registries/primary-registry\"\n\n[place]\nserver-packages = \"game.ServerScriptStorage.Packages\"\n\n[server-dependencies]\nTransitive = \"biff/transitive-dependency@0.1.0\"\n\n[dev-dependencies]\nMinimal = \"biff/minimal@0.1.0\"\n"