Skip to content

Commit

Permalink
move: build writes toolchain version to Move.lock (#15166)
Browse files Browse the repository at this point in the history
## Description 

As in title, see inline for more.

## Test Plan 

Updated tests.

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] protocol change
- [x] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes

- The `Move.lock` file will update with `[move.toolchain-version]` fields when building a package (e.g., with `sui move build`). Please check in and commit changes to the `Move.lock` file as usual.
  • Loading branch information
rvantonder authored and gdanezis committed Dec 15, 2023
1 parent 2a10bf6 commit f848b23
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ members = [
]

[workspace.package]
# This version string will be inherited by sui-core, sui-faucet, sui-node, sui-tools, sui-sdk, and sui crates.
# This version string will be inherited by sui-core, sui-faucet, sui-node, sui-tools, sui-sdk, sui-move-build, and sui crates.
version = "1.16.0"

[profile.release]
Expand Down
13 changes: 12 additions & 1 deletion crates/sui-core/src/unit_tests/move_package_publish_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,14 @@ async fn test_generate_lock_file() {
let mut build_config = BuildConfig::new_for_testing();
build_config.config.lock_file = Some(lock_file_path.clone());
build_config
.build(path)
.clone()
.build(path.clone())
.expect("Move package did not build");
// Update the lock file with placeholder compiler version so this isn't bumped every release.
build_config
.config
.update_lock_file_toolchain_version(&path, "0.0.1".into())
.expect("Could not update lock file");

let mut lock_file_contents = String::new();
File::open(lock_file_path)
Expand Down Expand Up @@ -240,6 +246,11 @@ async fn test_generate_lock_file() {
dependencies = [
{ name = "MoveStdlib" },
]
[move.toolchain-version]
compiler-version = "0.0.1"
edition = "legacy"
flavor = "sui"
"#]];
expected.assert_eq(lock_file_contents.as_str());
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-move-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sui-move-build"
version = "0.0.0"
version.workspace = true
edition = "2021"
authors = ["Mysten Labs <eng@mystenlabs.com>"]
description = "Logic for building Sui Move Packages"
Expand Down
17 changes: 14 additions & 3 deletions crates/sui-move-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,23 @@ impl BuildConfig {
let print_diags_to_stderr = self.print_diags_to_stderr;
let run_bytecode_verifier = self.run_bytecode_verifier;
let resolution_graph = self.resolution_graph(&path)?;
build_from_resolution_graph(
path,
let result = build_from_resolution_graph(
path.clone(),
resolution_graph,
run_bytecode_verifier,
print_diags_to_stderr,
)
);
if let Ok(ref compiled) = result {
compiled
.package
.compiled_package_info
.build_flags
.update_lock_file_toolchain_version(&path, env!("CARGO_PKG_VERSION").into())
.map_err(|e| SuiError::ModuleBuildFailure {
error: format!("Failed to update Move.lock toolchain version: {e}"),
})?;
}
result
}

pub fn resolution_graph(mut self, path: &Path) -> SuiResult<ResolvedGraph> {
Expand Down
16 changes: 10 additions & 6 deletions external-crates/move/crates/move-package/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ impl BuildConfig {
let lock_string = std::fs::read_to_string(path.join(SourcePackageLayout::Lock.path())).ok();
let _mutx = PackageLock::lock(); // held until function returns

let install_dir_set = self.install_dir.is_some();
let install_dir = self.install_dir.as_ref().unwrap_or(&path).to_owned();

let mut dep_graph_builder = DependencyGraphBuilder::new(
Expand All @@ -190,7 +191,9 @@ impl BuildConfig {
lock_string,
)?;

if modified {
if modified || install_dir_set {
// (1) Write the Move.lock file if the existing one is `modified`, or
// (2) `install_dir` is set explicitly, which may be a different directory, and where a Move.lock does not exist yet.
let lock = dependency_graph.write_to_lock(install_dir)?;
if let Some(lock_path) = &self.lock_file {
lock.commit(lock_path)?;
Expand Down Expand Up @@ -222,14 +225,15 @@ impl BuildConfig {
.set_silence_warnings(self.silence_warnings)
}

pub fn update_lock_file_toolchain_version(&self, compiler_version: String) -> Result<()> {
pub fn update_lock_file_toolchain_version(
&self,
path: &PathBuf,
compiler_version: String,
) -> Result<()> {
let Some(lock_file) = self.lock_file.as_ref() else {
return Ok(());
};
let install_dir = self
.install_dir
.clone()
.unwrap_or_else(|| PathBuf::from("."));
let install_dir = self.install_dir.as_ref().unwrap_or(path).to_owned();
let mut lock = LockFile::from(install_dir, lock_file)?;
lock.seek(SeekFrom::Start(0))?;
update_compiler_toolchain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! [move] table). This module does not support serialization because of limitations in the `toml`
//! crate related to serializing types as inline tables.

use std::io::{Read, Seek, SeekFrom, Write};
use std::io::{Read, Seek, Write};

use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -182,7 +182,6 @@ pub fn update_compiler_toolchain(
) -> Result<()> {
let mut toml_string = String::new();
file.read_to_string(&mut toml_string)?;
file.seek(SeekFrom::Start(0))?;
let mut toml = toml_string.parse::<toml_edit::Document>()?;
let move_table = toml["move"].as_table_mut().ok_or(std::fmt::Error)?;
let toolchain_version = toml::Value::try_from(ToolchainVersion {
Expand All @@ -191,6 +190,8 @@ pub fn update_compiler_toolchain(
flavor,
})?;
move_table["toolchain-version"] = to_toml_edit_value(&toolchain_version);
file.set_len(0)?;
file.rewind()?;
write!(file, "{}", toml)?;
file.flush()?;
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ fn update_lock_file_toolchain_version() {
build_config.default_flavor = Some(Flavor::Sui);
build_config.default_edition = Some(Edition::E2024_ALPHA);
build_config.lock_file = Some(lock_path.clone());
let _ = build_config.update_lock_file_toolchain_version("0.0.1".into());
let _ =
build_config.update_lock_file_toolchain_version(&pkg.path().to_path_buf(), "0.0.1".into());

let mut lock_file = File::open(lock_path).unwrap();
let toolchain_version =
Expand Down

0 comments on commit f848b23

Please sign in to comment.