GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix Poetry/PDM lock splice drift (#694, #695) (#703) · SocketDev/socket-patch@792e836

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • crates/socket-patch-core/src/utils
    • lock_fragments.rs
    • mod.rs
    • pdm_lock.rs
    • poetry_lock.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎crates/socket-patch-core/src/utils/lock_fragments.rs‎

Copy file name to clipboard
+344Lines changed: 344 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,344 @@
1+
//! The fragment-splice engine shared by the `poetry.lock` and `pdm.lock`
2+
//! rewriters.
3+
//!
4+
//! Both rewriters mutate a parsed `toml_edit` document, take the package's
5+
//! verbatim text fragments from the original and from the rendering, pair
6+
//! them, and splice the changed ones into the ORIGINAL text, so untouched
7+
//! bytes survive and rollback can replay the recorded fragments in reverse.
8+
//! What differs per format (which fragments a package owns, what the rewrite
9+
//! refuses) stays in `poetry_lock` / `pdm_lock`; everything around it lives
10+
//! here, once.
11+
12+
use toml_edit::Table;
13+
14+
use crate::utils::line_endings::majority_terminator;
15+
16+
/// Takes `name`'s fragments of `text` from its (spanned) parse.
17+
pub(crate) type FragmentsIn =
18+
fn(&toml_edit::Document<String>, &str, &str) -> Result<Vec<String>, String>;
19+
20+
/// The parse of the lock text last seen or produced, carried between calls
21+
/// so a caller working through one lock dep by dep parses each state once:
22+
/// [`Self::parsed`] reuses it for byte-identical text, and a rewrite hands on
23+
/// the parse of its own output (which it takes anyway, for the output's
24+
/// fragments) when the splice reproduced that output byte for byte.
25+
/// `DocumentMut`'s own parser is exactly `Document::parse(..).into_mut()`, so
26+
/// every result is the fresh parse's.
27+
#[derive(Default)]
28+
pub struct LockParse {
29+
doc: Option<toml_edit::Document<String>>,
30+
}
31+
32+
impl LockParse {
33+
/// The parse of `text`, reusing the held one when it is of these bytes.
34+
pub fn parsed(&mut self, text: &str) -> Result<&Table, toml_edit::TomlError> {
35+
if self.doc.as_ref().is_none_or(|doc| doc.raw() != text) {
36+
self.doc = None;
37+
self.doc = Some(toml_edit::Document::parse(text.to_owned())?);
38+
}
39+
Ok(self.doc.as_ref().expect("parsed just above").as_table())
40+
}
41+
42+
/// The held parse of `text` (taken out), or a fresh one; `what` names the
43+
/// lock in the parse error.
44+
pub(crate) fn take(
45+
&mut self,
46+
text: &str,
47+
what: &str,
48+
) -> Result<toml_edit::Document<String>, String> {
49+
match self.doc.take() {
50+
Some(doc) if doc.raw() == text => Ok(doc),
51+
_ => toml_edit::Document::parse(text.to_owned())
52+
.map_err(|e| format!("invalid {what} lock: {e}")),
53+
}
54+
}
55+
56+
/// Hand back a parse of the text it was taken for, unchanged (a refusal
57+
/// before any mutation).
58+
pub(crate) fn restore(&mut self, doc: toml_edit::Document<String>) {
59+
self.doc = Some(doc);
60+
}
61+
}
62+
63+
/// A successful fragment-spliced lock rewrite.
64+
pub struct FragmentRewrite<'a> {
65+
/// The rewritten lock text.
66+
pub text: String,
67+
pub(crate) original: &'a str,
68+
pub(crate) name: &'a str,
69+
/// The format's lock name, for error messages.
70+
pub(crate) what: &'static str,
71+
pub(crate) fragments_in: FragmentsIn,
72+
/// The original's fragments, already taken to build `text`.
73+
pub(crate) before: Vec<String>,
74+
/// The fragment edits, when `text` is byte-identical to the rendered
75+
/// document they were derived against (the common case: toml_edit
76+
/// round-trips the untouched bytes) — then they are also the edits
77+
/// against `text`.
78+
pub(crate) known_edits: Option<Vec<(String, String)>>,
79+
}
80+
81+
impl FragmentRewrite<'_> {
82+
/// Exactly the format's `*_lock_edits(original, &self.text, name)`,
83+
/// without re-deriving what the rewrite already did.
84+
pub fn edits(&self) -> Result<Vec<(String, String)>, String> {
85+
if let Some(edits) = &self.known_edits {
86+
return Ok(edits.clone());
87+
}
88+
let after = fragments_of(&self.text, self.name, self.fragments_in)?;
89+
pair_fragments(self.what, self.original, &self.before, &self.text, after)
90+
}
91+
}
92+
93+
/// `name`'s fragments of `text`, parsing it first.
94+
pub(crate) fn fragments_of(
95+
text: &str,
96+
name: &str,
97+
fragments_in: FragmentsIn,
98+
) -> Result<Vec<String>, String> {
99+
let lock = toml_edit::Document::parse(text.to_owned()).map_err(|e| e.to_string())?;
100+
fragments_in(&lock, text, name)
101+
}
102+
103+
/// The verbatim `(original, replacement)` fragment pairs that differ between
104+
/// two sides. Each fragment must occur exactly once on its side, so a
105+
/// textual splice (and its rollback) can never hit the wrong place, and both
106+
/// sides must have the same shape.
107+
pub(crate) fn pair_fragments(
108+
what: &str,
109+
original: &str,
110+
before: &[String],
111+
rewritten: &str,
112+
after: Vec<String>,
113+
) -> Result<Vec<(String, String)>, String> {
114+
if before.len() != after.len() {
115+
return Err(format!("{what} package fragments changed shape"));
116+
}
117+
let mut edits = Vec::new();
118+
for (old, new) in before.iter().zip(after) {
119+
if *old == new {
120+
continue;
121+
}
122+
if original.matches(old.as_str()).count() != 1 || rewritten.matches(&new).count() != 1 {
123+
return Err(format!("ambiguous {what} rollback fragment"));
124+
}
125+
edits.push((old.clone(), new));
126+
}
127+
Ok(edits)
128+
}
129+
130+
/// Widen `span` over `table`'s own span and every key, value and sub-table
131+
/// inside it.
132+
pub(crate) fn extend_span(table: &Table, span: &mut std::ops::Range<usize>) {
133+
if let Some(own) = table.span() {
134+
span.start = span.start.min(own.start);
135+
span.end = span.end.max(own.end);
136+
}
137+
for (_, item) in table.iter() {
138+
if let Some(own) = item.span() {
139+
span.start = span.start.min(own.start);
140+
span.end = span.end.max(own.end);
141+
}
142+
if let Some(child) = item.as_table() {
143+
extend_span(child, span);
144+
}
145+
}
146+
}
147+
148+
/// End (exclusive, before its line break) of the first top-level TOML header
149+
/// line at or after `from`, skipping blank lines and comments; `text.len()`
150+
/// at EOF; `from` itself when the next non-blank line is not a header (a
151+
/// shape neither Poetry nor PDM writes — the fragment is then not extended).
152+
pub(crate) fn next_header_end(text: &str, from: usize) -> usize {
153+
let mut pos = from;
154+
for line in text[from..].split_inclusive('\n') {
155+
let content = line.trim_end_matches(['\r', '\n']);
156+
// Blank lines and comments sit between units (toml_edit clones carry
157+
// the file's leading comment as decor); they belong to the boundary.
158+
if content.trim().is_empty() || content.trim_start().starts_with('#') {
159+
pos += line.len();
160+
continue;
161+
}
162+
if content.starts_with('[') {
163+
return pos + content.len();
164+
}
165+
return from;
166+
}
167+
text.len()
168+
}
169+
170+
/// `new` (the rendering's fragment) with every line break spelled the way
171+
/// most of `old`'s (the original fragment it replaces) are, or
172+
/// `file_terminator` when `old` has none.
173+
///
174+
/// A fragment anchored at the line break before it (Poetry's legacy
175+
/// integrity entry) starts with that break's `\n`, whose `\r`, if any, lies
176+
/// outside the fragment: that leading `\n` is kept as is on both sides.
177+
fn respell(old: &str, new: &str, file_terminator: &str) -> String {
178+
let (lead, old, new) = match (old.strip_prefix('\n'), new.strip_prefix('\n')) {
179+
(Some(old), Some(new)) => ("\n", old, new),
180+
_ => ("", old, new),
181+
};
182+
let terminator = if old.contains('\n') {
183+
majority_terminator(old)
184+
} else {
185+
file_terminator
186+
};
187+
let lf = new.replace("\r\n", "\n");
188+
let body = if terminator == "\n" {
189+
lf
190+
} else {
191+
lf.replace('\n', terminator)
192+
};
193+
format!("{lead}{body}")
194+
}
195+
196+
/// Finish a rewrite whose mutated document rendered to `rendered`: take the
197+
/// rendering's fragments, give each the line ending that dominates the
198+
/// original fragment it replaces (toml_edit renders every break as LF), and
199+
/// splice the changed ones into `text`.
200+
///
201+
/// The ending rule is per fragment, so no line outside the spliced bytes
202+
/// changes and the replaced unit keeps its own style: an LF-only or
203+
/// CRLF-only lock comes back in that style, and a mixed-ending lock's unit
204+
/// gets the ending most of its own lines had (the file's majority when the
205+
/// original fragment has no break at all).
206+
pub(crate) fn finish<'a>(
207+
what: &'static str,
208+
parse: &mut LockParse,
209+
text: &'a str,
210+
name: &'a str,
211+
rendered: String,
212+
before: Result<Vec<String>, String>,
213+
fragments_in: FragmentsIn,
214+
) -> Result<FragmentRewrite<'a>, String> {
215+
let before = before?;
216+
let rendered = crate::utils::python_lock::preserve_line_endings(text, rendered);
217+
let after_doc = toml_edit::Document::parse(rendered).map_err(|e| e.to_string())?;
218+
let rendered = after_doc.raw();
219+
let after = fragments_in(&after_doc, rendered, name)?;
220+
if before.len() != after.len() {
221+
return Err(format!("{what} package fragments changed shape"));
222+
}
223+
let file_terminator = majority_terminator(text);
224+
let after: Vec<String> = before
225+
.iter()
226+
.zip(after)
227+
.map(|(old, new)| respell(old, &new, file_terminator))
228+
.collect();
229+
let mut edits = Vec::new();
230+
for (old, new) in before.iter().zip(after) {
231+
if *old == new {
232+
continue;
233+
}
234+
if text.matches(old.as_str()).count() != 1 {
235+
return Err(format!("ambiguous {what} rollback fragment"));
236+
}
237+
edits.push((old.clone(), new));
238+
}
239+
let mut result = text.to_string();
240+
for (old, new) in &edits {
241+
result = result.replacen(old, new, 1);
242+
}
243+
if edits
244+
.iter()
245+
.any(|(_, new)| result.matches(new.as_str()).count() != 1)
246+
{
247+
return Err(format!("ambiguous {what} rollback fragment"));
248+
}
249+
let known_edits = (result == rendered).then_some(edits);
250+
if known_edits.is_some() {
251+
// The output IS the rendered text just parsed: the next dep's input.
252+
parse.restore(after_doc);
253+
}
254+
Ok(FragmentRewrite {
255+
text: result,
256+
original: text,
257+
name,
258+
what,
259+
fragments_in,
260+
before,
261+
known_edits,
262+
})
263+
}
264+
265+
#[cfg(test)]
266+
mod tests {
267+
use super::*;
268+
269+
fn whole(
270+
lock: &toml_edit::Document<String>,
271+
text: &str,
272+
_name: &str,
273+
) -> Result<Vec<String>, String> {
274+
let span = lock
275+
.get("unit")
276+
.and_then(|item| item.as_table())
277+
.and_then(Table::span)
278+
.ok_or("missing unit")?;
279+
let mut span = span;
280+
extend_span(lock["unit"].as_table().unwrap(), &mut span);
281+
Ok(vec![text[span].to_string()])
282+
}
283+
284+
#[test]
285+
fn pair_fragments_refuses_a_shape_change() {
286+
for what in ["Poetry", "PDM"] {
287+
let error = pair_fragments(what, "a", &["a".into()], "b", vec![]).unwrap_err();
288+
assert_eq!(error, format!("{what} package fragments changed shape"));
289+
let error = pair_fragments(what, "a", &[], "b", vec!["b".into()]).unwrap_err();
290+
assert_eq!(error, format!("{what} package fragments changed shape"));
291+
}
292+
}
293+
294+
#[test]
295+
fn finish_spells_each_fragment_like_the_one_it_replaces() {
296+
// A CRLF file whose unit is mostly LF, and an LF file whose unit is
297+
// mostly CRLF: the unit keeps its own majority, the rest is untouched.
298+
for (text, unit_break) in [
299+
(
300+
"[head]\r\na = 1\r\n\r\n[unit]\nb = 2\nc = 3\r\ne = 5\n",
301+
"\n",
302+
),
303+
(
304+
"[head]\na = 1\n\n[unit]\r\nb = 2\r\nc = 3\ne = 5\r\n",
305+
"\r\n",
306+
),
307+
] {
308+
let mut doc = toml_edit::Document::parse(text.to_owned()).unwrap();
309+
let before = whole(&doc, text, "");
310+
let mut mutable =
311+
std::mem::replace(&mut doc, toml_edit::Document::parse(String::new()).unwrap())
312+
.into_mut();
313+
mutable["unit"]["d"] = toml_edit::value(4);
314+
let rewrite = finish(
315+
"Test",
316+
&mut LockParse::default(),
317+
text,
318+
"",
319+
mutable.to_string(),
320+
before,
321+
whole,
322+
)
323+
.unwrap();
324+
let head = text.split("[unit]").next().unwrap();
325+
assert!(rewrite.text.starts_with(head), "{:?}", rewrite.text);
326+
let unit = &rewrite.text[head.len()..];
327+
assert!(unit.contains("d = 4"), "{unit:?}");
328+
let crlf = unit.matches("\r\n").count();
329+
let lf = unit.matches('\n').count() - crlf;
330+
if unit_break == "\n" {
331+
assert_eq!(crlf, 0, "{unit:?}");
332+
} else {
333+
assert_eq!(lf, 0, "{unit:?}");
334+
}
335+
// The recorded edit replays the original back byte for byte.
336+
let edits = rewrite.edits().unwrap();
337+
let mut back = rewrite.text.clone();
338+
for (old, new) in &edits {
339+
back = back.replacen(new.as_str(), old, 1);
340+
}
341+
assert_eq!(back, text);
342+
}
343+
}
344+
}
Collapse file

‎crates/socket-patch-core/src/utils/mod.rs‎

Copy file name to clipboardExpand all lines: crates/socket-patch-core/src/utils/mod.rs
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ pub mod env_compat;
77
pub mod failpoint;
88
pub mod fs;
99
pub mod group_commit;
10-
pub mod notice;
1110
pub(crate) mod http;
1211
pub(crate) mod line_endings;
12+
pub mod lock_fragments;
13+
pub mod notice;
1314
pub mod pdm_lock;
1415
pub(crate) mod pep440;
1516
pub mod pipenv;

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.