Detect indent width in pnpm-lock.yaml and shard.lock parsers - #109
Conversation
andrew
left a comment
There was a problem hiding this comment.
Both parsers derive indentation from comment-only lines. Adding a four-space-indented # Dependencies immediately after packages: or shards: makes the existing two-space fixtures return zero dependencies. I reproduced this through manifests.Parse and verified that the YAML values are unchanged: pnpm dependencies fall from 9 to 0, and shards from 7 to 0. The same test passes on main.
Please skip comment-only lines before detecting indentation in both parsers and add regression tests through manifests.Parse.
# Conflicts: # internal/npm/npm_test.go
|
Thanks. I added |
andrew
left a comment
There was a problem hiding this comment.
The comment-indentation issue is fixed in both parsers, with regression coverage through manifests.Parse.
Fixes #108
Problem
The
pnpm-lock.yamlandshard.lockparsers scan line by line and compare indentation literally.extractPnpmPackageKeyandextractShardNameonly accept a key indented by exactly two spaces. The shard.lock parser also matchesversion:andcommit:with four literal spaces. A lockfile reformatted with a different indent width (for example by a YAML formatter) parses to zero dependencies with no error.Fix
Both parsers now take the indent width from the first indented line inside
packages:/shards:and compare against it instead of hard-coded widths:peerDependenciesMetaare still skipped (Bug in parsing pnpm-lock files #32).version:andcommit:are matched at any indent deeper than the shard name.core.LeadingSpaceshelper is shared by both parsers.The line scanners stay as they are, so there is no added memory cost on large pnpm lockfiles.
Tests
TestPnpmLockIndentWidthparses the legacy, v5, v6 and v9 pnpm fixtures with doubled indentation. It asserts the dependencies (name, version, integrity, scope) match the originals exactly.TestShardLockIndentWidthdoes the same fortestdata/crystal/shard.lock. It asserts all 7 shards with their versions.TestExtractPnpmPackageKeynow covers 4-space keys, nested keys at 4-space width and the undetected (zero) indent case.Before and after, parsing through
manifests.Parsewith indentation doubled:testdata/npm/pnpm-lock.yamltestdata/crystal/shard.lockgo build ./...,go vet ./...,go test -race ./...andgolangci-lint runall pass.