Skip to content

Commit

Permalink
Migrate lockfile to add missing id when only name is defined in t…
Browse files Browse the repository at this point in the history
…he deps lockfile (#77)

Try to auto-migrate lockfile to v3.
  • Loading branch information
manolisliolios authored Dec 9, 2024
1 parent 6cdf1fb commit 3d9ac62
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
70 changes: 65 additions & 5 deletions mvr-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const ADDRESSES_KEY: &str = "addresses";
const LOCK_MOVE_KEY: &str = "move";
const LOCK_PACKAGE_KEY: &str = "package";
const LOCK_PACKAGE_NAME_KEY: &str = "name";
const LOCK_PACKAGE_ID_KEY: &str = "id";
const LOCK_PACKAGE_VERSION_KEY: &str = "version";

const APP_REGISTRY_TABLE_ID: &str =
"0xe8417c530cde59eddf6dfb760e8a0e3e2c6f17c69ddaab5a73dd6a6e65fc463b";
Expand Down Expand Up @@ -598,7 +600,7 @@ pub async fn published_ids(
// set in the Move.toml (if any).
None => {
let package_name = package_table
.get("name")
.get(LOCK_PACKAGE_NAME_KEY)
.and_then(|v| v.as_str())
.map(|s| s.to_lowercase());
let addresses_id = package_name.clone().and_then(|name| {
Expand Down Expand Up @@ -908,29 +910,42 @@ fn insert_root_dependency(
)
})?;

let package_version = move_section
.get(LOCK_PACKAGE_VERSION_KEY)
.unwrap_or(&value(1))
.as_integer()
.ok_or_else(|| anyhow!("Invalid version in lock file".red()))?;

// Save the top-level `dependencies`, which will become the dependencies of the new root package.
let original_deps = move_section.get("dependencies").cloned();

// Make the top-level `dependencies` point to the new root package.
let new_dep = {
let mut table = InlineTable::new();
table.insert("name", Value::String(Formatted::new(root_name.to_string())));
table.insert("id", Value::String(Formatted::new(root_name.to_string())));
table.insert(
LOCK_PACKAGE_NAME_KEY,
Value::String(Formatted::new(root_name.to_string())),
);
table.insert(
LOCK_PACKAGE_ID_KEY,
Value::String(Formatted::new(root_name.to_string())),
);
Value::InlineTable(table)
};

let mut new_deps = Array::new();
new_deps.push(new_dep);
move_section["dependencies"] = value(new_deps);

// Create a new root package entry, set its dependencies to the original top-level dependencies, and persist.
let mut new_package = Table::new();
new_package.insert("id", value(root_name));
new_package.insert(LOCK_PACKAGE_ID_KEY, value(root_name));

let mut source = Table::new();
source.insert("git", value(&git_info.repository));
source.insert("rev", value(&git_info.tag));
source.insert("subdir", value(&git_info.path));
new_package.insert("source", Item::Table(source));
new_package.insert("source", value(source.into_inline_table()));

if let Some(deps) = original_deps {
new_package.insert("dependencies", deps);
Expand All @@ -941,7 +956,16 @@ fn insert_root_dependency(
.or_insert(Item::ArrayOfTables(ArrayOfTables::new()))
.as_array_of_tables_mut()
.ok_or_else(|| anyhow!("Failed to get or create package array in lock file".red()))?;

packages.push(new_package);

// If the lockfile is version 2, migrate to version 3.
// Migration to version 3 requires an `id` field in the lockfile.
if package_version < 3 {
migrate_to_version_three(packages)?;
move_section.insert(LOCK_PACKAGE_VERSION_KEY, value(3));
}

Ok(doc.to_string())
}

Expand Down Expand Up @@ -1226,3 +1250,39 @@ pub fn sui_config_dir() -> Result<PathBuf, anyhow::Error> {
Ok(dir)
})
}

/// Migrates the lockfile to version 3 from older versions, if necessary.
fn migrate_to_version_three(packages: &mut ArrayOfTables) -> Result<()> {
for package in packages.iter_mut() {
// if ID is missing from the core dependency, add it.
if !package.contains_key(LOCK_PACKAGE_ID_KEY) {
let name = package
.get(LOCK_PACKAGE_NAME_KEY)
.ok_or_else(|| anyhow!("Failed to get name for dependency"))?;
package.insert(LOCK_PACKAGE_ID_KEY, name.clone());
}

// Skip if there are no dependencies or if dependencies are not an array.
let dependencies = match package
.get_mut(DEPENDENCIES_KEY)
.and_then(|v| v.as_array_mut())
{
Some(deps) => deps,
None => continue,
};

for dep in dependencies.iter_mut() {
if let Some(val) = dep.as_inline_table_mut() {
// Get name, skipping if not found
let name = val
.get(LOCK_PACKAGE_NAME_KEY)
.ok_or_else(|| anyhow!("Failed to get name"))?;

if !val.contains_key(LOCK_PACKAGE_ID_KEY) {
val.insert(LOCK_PACKAGE_ID_KEY, name.clone());
}
}
}
}
Ok(())
}
6 changes: 1 addition & 5 deletions mvr-cli/src/tests/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,10 @@ dependencies = [
[[move.package]]
id = "demo"
source = { git = "https://github.com/example/demo.git", rev = "v1.0.0", subdir = "packages/demo" }
dependencies = [
{ id = "Sui", name = "Sui" },
]
[move.package.source]
git = "https://github.com/example/demo.git"
rev = "v1.0.0"
subdir = "packages/demo"
"##]]
.assert_eq(&result[0]);
Ok(())
Expand Down

0 comments on commit 3d9ac62

Please sign in to comment.