After c2h5oh's explicit-key fold in #155077, section_keys still split quoted file: keys at the first embedded ': '. This truncates the package name/version in semantic lockfile reports for a legal POSIX file path containing ': ' and long enough to trigger js-yaml's pair. Reuse the quote-aware _ENTRY parser at the keys-only call site and pin the real 1024-character threshold without changing pnpm defaults. Follow-up to c2h5oh's #155077 and BearHuddleston's #150296 review. (cherry picked from commit 0d8141d97fb09e3439b76b89f6da090134e049df)
133 lines
5.7 KiB
Python
133 lines
5.7 KiB
Python
"""Read pnpm-lock.yaml without a YAML parser.
|
|
|
|
CI runs these scripts on the runner's bare Python, before any dependency is
|
|
installed. pnpm writes this file itself in a fixed shape (two-space indent, one
|
|
key per line); anything that does not fit that shape is an error, never a guess.
|
|
scripts/build/pnpm-lock.mjs is the Node twin of this reader.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import re
|
|
|
|
# A key (quoted, or plain up to the first ``: ``) and its inline value, if any.
|
|
_ENTRY = re.compile(r"""^('(?:[^']|'')*'|"(?:[^"\\]|\\.)*"|[^'"\s].*?):(?: (.*))?$""")
|
|
# Any top-level key (plain, or quoted as pnpm's emitter quotes ambiguous keys) ends
|
|
# a section: pnpm keeps root keys it does not define (Bit's ``bit:`` block) and adds
|
|
# new ones over time. Any other column-0 line (a comment, a conflict marker, a
|
|
# tab-indented field) fails instead of silently truncating the section.
|
|
_TOP_LEVEL = re.compile(r"""^(?:\w.*?|'(?:[^']|'')*'|"(?:[^"\\]|\\.)*"):(?: .*)?$""")
|
|
|
|
|
|
def _unquote(key: str) -> str:
|
|
if key.startswith("'") and key.endswith("'"):
|
|
return key[1:-1].replace("''", "'")
|
|
if key.startswith('"') and key.endswith('"'):
|
|
return json.loads(key)
|
|
return key
|
|
|
|
|
|
def _document_lines(text: str) -> list[str]:
|
|
"""The lines of the lock's only YAML document, after checking its format version.
|
|
|
|
The repository lock is one YAML document (``pmOnFail: ignore`` keeps pnpm from
|
|
prepending a ``packageManagerDependencies`` document); several documents are
|
|
refused, because lockfile scanners read only the first one.
|
|
"""
|
|
documents = [part for part in re.split(r"^---\r?\n", text, flags=re.MULTILINE) if part.strip()]
|
|
if len(documents) != 1:
|
|
raise ValueError(f"pnpm-lock.yaml: expected one YAML document, found {len(documents)}")
|
|
lines = documents[0].splitlines()
|
|
version = next((line for line in lines if line.startswith("lockfileVersion:")), None)
|
|
if version != "lockfileVersion: '9.0'":
|
|
raise ValueError(f"pnpm-lock.yaml: unsupported {version or 'lockfileVersion <missing>'}")
|
|
return lines
|
|
|
|
|
|
def _section_lines(text: str, name: str) -> list[str]:
|
|
"""The lines of top-level section ``name`` (empty when the lock has none).
|
|
|
|
The section runs to the next top-level key or the end of the file; any other
|
|
line that is not indented by spaces is refused. pnpm writes a key longer than
|
|
1024 characters (a long peer suffix) as an explicit ``? key`` / ``: value``
|
|
pair, which is folded back into the usual ``key:`` lines.
|
|
"""
|
|
lines = _document_lines(text)
|
|
if f"{name}:" not in lines:
|
|
return []
|
|
body = []
|
|
for line in lines[lines.index(f"{name}:") + 1:]:
|
|
if body and body[-1].startswith(" ? ") and line.startswith(" : "):
|
|
key, value = body.pop()[4:], line[4:]
|
|
body += [f" {key}: {{}}"] if value == "{}" else [f" {key}:", f" {value}"]
|
|
elif line.startswith(" ") or not line.strip():
|
|
body.append(line)
|
|
elif _TOP_LEVEL.match(line):
|
|
break
|
|
else:
|
|
raise ValueError(f"pnpm-lock.yaml: unreadable {name} line: {line}")
|
|
return body
|
|
|
|
|
|
def _scalar(value: str) -> dict | str:
|
|
"""An inline value: ``{}`` (an entry without fields) or a (maybe quoted) string.
|
|
|
|
Other flow values (``resolution: {integrity: ...}``, ``cpu: [x64]``) stay raw text.
|
|
"""
|
|
return {} if value == "{}" else _unquote(value)
|
|
|
|
|
|
def section(text: str, name: str) -> dict:
|
|
"""A top-level map (``importers``, ``packages``, ``snapshots``) as nested dicts.
|
|
|
|
Block maps become dicts, ``- item`` blocks lists, everything else strings. A
|
|
lockfile without dependencies has no ``packages``/``snapshots`` section at all.
|
|
"""
|
|
root: dict = {}
|
|
# (indent of the lines this frame holds, container, its parent, its key in the parent)
|
|
stack: list[tuple[int, dict | list, dict | None, str | None]] = [(2, root, None, None)]
|
|
for line in _section_lines(text, name):
|
|
if not line.strip():
|
|
continue
|
|
indent = len(line) - len(line.lstrip(" "))
|
|
while len(stack) > 1 and indent < stack[-1][0]:
|
|
stack.pop()
|
|
level, container, parent, key = stack[-1]
|
|
if indent != level:
|
|
raise ValueError(f"pnpm-lock.yaml: unreadable {name} line: {line}")
|
|
body = line[indent:]
|
|
if body.startswith("- "):
|
|
if container == {} and parent is not None:
|
|
container = parent[key] = [] # the first child shows the block is a list
|
|
stack[-1] = (level, container, parent, key)
|
|
if not isinstance(container, list):
|
|
raise ValueError(f"pnpm-lock.yaml: unreadable {name} line: {line}")
|
|
container.append(_unquote(body[2:]))
|
|
continue
|
|
match = _ENTRY.match(body)
|
|
if not match or not isinstance(container, dict):
|
|
raise ValueError(f"pnpm-lock.yaml: unreadable {name} line: {line}")
|
|
entry = _unquote(match[1])
|
|
if match[2] is None:
|
|
container[entry] = {}
|
|
stack.append((indent + 2, container[entry], container, entry))
|
|
else:
|
|
container[entry] = _scalar(match[2])
|
|
return root
|
|
|
|
|
|
def section_keys(text: str, name: str) -> list[str]:
|
|
"""The keys of a top-level map (``importers``, ``packages``) of the lock.
|
|
|
|
Reads only the keys, so it does not depend on the shape of each entry's fields.
|
|
"""
|
|
keys = []
|
|
for line in _section_lines(text, name):
|
|
if not line.strip() or line.startswith(" "):
|
|
continue # blank separators and each entry's own fields
|
|
match = _ENTRY.match(line[2:]) if line.startswith(" ") else None
|
|
if not match:
|
|
raise ValueError(f"pnpm-lock.yaml: unreadable {name} line: {line}")
|
|
keys.append(_unquote(match[1]))
|
|
return keys
|