diff --git a/Cargo.lock b/Cargo.lock index 9e31c4bdf3f88..ac47fb91e2c56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2259,6 +2259,7 @@ dependencies = [ "insta", "itertools 0.12.0", "once_cell", + "rand", "ruff_diagnostics", "ruff_source_file", "ruff_text_size", diff --git a/crates/ruff_notebook/Cargo.toml b/crates/ruff_notebook/Cargo.toml index 517a2334553c7..524cfb4eb97f3 100644 --- a/crates/ruff_notebook/Cargo.toml +++ b/crates/ruff_notebook/Cargo.toml @@ -25,6 +25,7 @@ serde_json = { workspace = true } serde_with = { workspace = true, default-features = false, features = ["macros"] } thiserror = { workspace = true } uuid = { workspace = true } +rand = { workspace = true } [dev-dependencies] insta = { workspace = true } diff --git a/crates/ruff_notebook/src/notebook.rs b/crates/ruff_notebook/src/notebook.rs index 84d2b43ab6e7a..6e99e7e501cc8 100644 --- a/crates/ruff_notebook/src/notebook.rs +++ b/crates/ruff_notebook/src/notebook.rs @@ -7,6 +7,7 @@ use std::{io, iter}; use itertools::Itertools; use once_cell::sync::OnceCell; +use rand::Rng; use serde::Serialize; use serde_json::error::Category; use thiserror::Error; @@ -145,8 +146,10 @@ impl Notebook { // Add cell ids to 4.5+ notebooks if they are missing // https://github.com/astral-sh/ruff/issues/6834 // https://github.com/jupyter/enhancement-proposals/blob/master/62-cell-id/cell-id.md#required-field + // https://github.com/jupyter/enhancement-proposals/blob/master/62-cell-id/cell-id.md#questions if raw_notebook.nbformat == 4 && raw_notebook.nbformat_minor >= 5 { - let mut id_index: u128 = 0; + // We use a mock random number generator to generate deterministic uuids + let mut rng = rand::rngs::mock::StepRng::new(0, 1); let mut existing_ids = HashSet::new(); for cell in &raw_notebook.cells { @@ -168,15 +171,11 @@ impl Notebook { }; if id.is_none() { loop { - // https://github.com/jupyter/enhancement-proposals/blob/master/62-cell-id/cell-id.md#questions - let new_id = uuid::Builder::from_u128(id_index) + let new_id = uuid::Builder::from_random_bytes(rng.gen()) .into_uuid() .as_hyphenated() .to_string(); - // Increment the index - id_index += 1; - if !existing_ids.contains(&new_id) { existing_ids.insert(new_id.clone()); *id = Some(new_id);