@exodus/stasis-core 1.0.0-beta.4 → 1.0.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,14 @@
1
1
  // The pure half of the util split: the artifact data model shared by bundle.js, lockfile.js and
2
- // shard.js -- the format universe, flat file keys, strict merges, the executable-set rules and the
3
- // JSON<->Map converters. Everything here is pure computation over caller-supplied values with no
4
- // Node builtin imports at all, so the data-model entry points (`./bundle`, `./lockfile`) load in
5
- // any JS runtime. Byte/name classification, fs observation and CLI parsing live in util.js, which
6
- // re-exports this module so `@exodus/stasis-core/util` keeps serving the full set.
7
-
8
- const sep = '/'
2
+ // shard.js (the format universe, flat file keys, strict merges, the executable-set rules and the
3
+ // JSON<->Map converters). No Node builtin imports, so `./bundle` and `./lockfile` load in any JS
4
+ // runtime. util.js re-exports this module, so `@exodus/stasis-core/util` serves the full set.
9
5
 
10
6
  // KNOWN_FORMATS is the closed universe of `format` strings; parsers reject anything outside it.
11
7
  export const NODE_FORMATS = new Set(['module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript'])
12
8
  export const SOURCE_LANGUAGE_FORMATS = new Set(['solidity', 'php', 'shell', 'rust'])
13
9
  export const NATIVE_BUILD_FORMATS = new Set([
14
10
  'java', 'kotlin', 'gradle', 'objc', 'objcpp', 'swift', 'c', 'cpp', 'c-header', 'cpp-header',
15
- 'ruby', 'cmake', 'podspec', 'podfile', 'podfile-lock', 'template', 'xml', 'env', 'fastlane', 'pbxproj',
11
+ 'ruby', 'python', 'cmake', 'podspec', 'podfile', 'podfile-lock', 'template', 'xml', 'env', 'fastlane', 'pbxproj',
16
12
  ])
17
13
  export const RESOURCE_FORMATS = new Set(['resource', 'resource:base64'])
18
14
  export const STAT_FORMATS = new Set(['stat:file', 'stat:directory'])
@@ -29,15 +25,19 @@ export const KNOWN_FORMATS = new Set([
29
25
  // Payload-free stat records: attest a path's KIND, no content, and yield to a real format.
30
26
  export const isStatFormat = (format) => STAT_FORMATS.has(format)
31
27
 
32
- // Gates reconcileFormat: a stat record only reconciles with a real format of the SAME kind.
28
+ // The post-erasure family of a '-typescript' loader format ('module-typescript' -> 'module'): a
29
+ // transforming preload (`stasis run --import tsx`) resolves/serves a TypeScript file as the JS
30
+ // family it transpiles to, while the attestation keeps the on-disk format. undefined otherwise.
31
+ export const erasedTypeScriptFormat = (format) =>
32
+ format === 'module-typescript' ? 'module' : format === 'commonjs-typescript' ? 'commonjs' : undefined
33
+
33
34
  const formatKind = (format) => (format === 'directory' || format === 'stat:directory' ? 'directory' : 'file')
34
35
 
35
36
  // A weak 'stat:*' yields to a real format of the SAME kind and never displaces one; anything else throws.
36
37
  export function reconcileFormat(format, currentFormat, name) {
37
38
  if (format === currentFormat) return format
38
39
  const stat = isStatFormat(format)
39
- const currentStat = isStatFormat(currentFormat)
40
- if (stat !== currentStat && formatKind(format) === formatKind(currentFormat)) {
40
+ if (stat !== isStatFormat(currentFormat) && formatKind(format) === formatKind(currentFormat)) {
41
41
  return stat ? currentFormat : format
42
42
  }
43
43
  throw new Error(`format conflict for '${name}' ('${currentFormat}' vs '${format}')`)
@@ -50,6 +50,74 @@ export function moduleFileKey(dir, rel) {
50
50
  return dir === '.' ? rel : `${dir}/${rel}`
51
51
  }
52
52
 
53
+ // GitHub `owner/name` (owner 1-39, name 1-100 chars).
54
+ const GITHUB_REPO = /^(?=[A-Za-z0-9-]{1,39}\/)[A-Za-z0-9](?:-?[A-Za-z0-9])*\/(?!\.\.?$)[\w.-]{1,100}$/u
55
+ // Non-empty normalized repo-relative path of URL-safe segments.
56
+ const REPO_DIRECTORY = /^(?!\.\.?(?:\/|$))[\w.~@+-]+(?:\/(?!\.\.?(?:\/|$))[\w.~@+-]+)*$/u
57
+ // Full lowercase SHA-1 or SHA-256.
58
+ const GIT_SHA = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u
59
+ // The fields of a `repo`, the bundle's own or a dependency's, and their checks.
60
+ export const REPO_FIELDS = {
61
+ github: (v) => typeof v === 'string' && GITHUB_REPO.test(v),
62
+ // `''` is the repository's root.
63
+ directory: (v) => typeof v === 'string' && v.length <= 1024 && (v === '' || REPO_DIRECTORY.test(v)),
64
+ commit: (v) => typeof v === 'string' && GIT_SHA.test(v),
65
+ }
66
+
67
+ // Whether two GitHub `owner/name`s are one: GitHub's names are case-insensitive.
68
+ export const sameGithub = (a, b) => a?.toLowerCase() === b?.toLowerCase()
69
+
70
+ // Whether two `repo`s name the same place: every field alike, `github` in any case.
71
+ export const sameRepo = (a, b) => Object.keys(REPO_FIELDS).every((key) => (key === 'github' ? sameGithub(a.github, b.github) : a[key] === b[key]))
72
+
73
+ // Whether two records of one dependency agree on its `repo`: an artifact from before the field records none.
74
+ export const reposAgree = (a, b) => a === undefined || b === undefined || sameRepo(a, b)
75
+
76
+ // Validate a block against its `fields` (each optional: a check, or a nested block's fields), `what`
77
+ // naming it in errors; canonical, frozen, undefined if empty. Messages are built on failure alone: a
78
+ // dependency's `repo` is checked on every parse and write.
79
+ export const normalizeBlock = (block, fields, what) => {
80
+ if (block === undefined) return undefined
81
+ if (!isPlainObject(block)) assert(false, `${what} must be an object`)
82
+ for (const key of Object.keys(block)) if (!Object.hasOwn(fields, key)) assert(false, `unknown ${what} key '${key}'`)
83
+ const entries = Object.entries(fields).map(([key, check]) => {
84
+ if (typeof check === 'object') return [key, normalizeBlock(block[key], check, `${what}.${key}`)]
85
+ if (block[key] !== undefined && !check(block[key])) assert(false, `invalid ${what}.${key}: ${JSON.stringify(block[key])}`)
86
+ return [key, block[key]]
87
+ }).filter(([, value]) => value !== undefined)
88
+ return entries.length === 0 ? undefined : Object.freeze(fromEntries(entries))
89
+ }
90
+
91
+ // Validate a `repo` (all fields optional); canonical key order, undefined if empty.
92
+ export const normalizeRepo = (repo, what = 'bundle repo') => normalizeBlock(repo, REPO_FIELDS, what)
93
+
94
+ // A bucket's ecosystem as a dependency's: its `ecosystem` tag, or npm for an untagged one under
95
+ // node_modules (an artifact from before the tag); undefined for first-party code.
96
+ export const dependencyEcosystem = (dir, ecosystem) => ecosystem ?? (hasNodeModulesSegment(dir) ? 'npm' : undefined)
97
+
98
+ // A dependency's `repo` holds a build's own fields, but never `directory: ''`: only a layout a build
99
+ // reads, its own, places code at a repository's root, never a manifest (packageRepo).
100
+ const DEPENDENCY_REPO_FIELDS = { ...REPO_FIELDS, directory: (v) => v !== '' && REPO_FIELDS.directory(v) }
101
+
102
+ // A dependency's `repo`, the one its own manifest names; first-party code carries none.
103
+ const normalizeModuleRepo = (dir, { ecosystem, repo }, what) => {
104
+ if (repo === undefined) return undefined
105
+ if (dependencyEcosystem(dir, ecosystem) === undefined) assert(false, `${what}: '${dir}' is no dependency's bucket, and carries no repo`)
106
+ return normalizeBlock(repo, DEPENDENCY_REPO_FIELDS, `${what} module '${dir}' repo`)
107
+ }
108
+
109
+ // A module bucket record in canonical key order; `ecosystem` and `repo` are omitted (not undefined) when absent.
110
+ export const moduleInfo = ({ name, version, ecosystem, repo, files }) =>
111
+ ({ name, version, ...(ecosystem === undefined ? {} : { ecosystem }), ...(repo === undefined ? {} : { repo }), files })
112
+
113
+ // A parsed bucket `dir` of a `what` artifact: an absent version has one spelling (a literal null folds
114
+ // into undefined so identity comparisons and JSON round-trips can't split on it), `repo` is validated,
115
+ // and `files` is null-prototype.
116
+ export function normalizeModule({ name, version, ecosystem, repo, files }, dir, what) {
117
+ assert(ecosystem === undefined || typeof ecosystem === 'string')
118
+ return moduleInfo({ name, version: version ?? undefined, ecosystem, repo: normalizeModuleRepo(dir, { ecosystem, repo }, what), files: fromEntries(Object.entries(files)) })
119
+ }
120
+
53
121
  // The keys a module map records -- the set an artifact's `executable` must be a subset of. `scope` MUST
54
122
  // be the artifact's own: a non-full-scope artifact records only its node_modules buckets.
55
123
  export function moduleFileKeys(modules, { scope = 'full' } = {}) {
@@ -64,6 +132,7 @@ export function moduleFileKeys(modules, { scope = 'full' } = {}) {
64
132
  // THE rule an artifact's `executable` entry must satisfy; returns the problem, or null when legal.
65
133
  // parseExecutable (read), assertExecutable (write) and narrowExecutable share it so they cannot drift.
66
134
  function executableEntryProblem(file, { what, files, formats, scope }) {
135
+ if (file.includes('\\')) return "holds a '\\'"
67
136
  if (posixPathEscapes(file)) return 'escapes the root'
68
137
  if (scope !== 'full' && !hasNodeModulesSegment(file)) {
69
138
  return `is outside node_modules, which a '${scope}'-scope ${what} does not record`
@@ -121,7 +190,7 @@ export function assert(condition, msg) {
121
190
  }
122
191
 
123
192
  export function sortPaths(a, b) {
124
- const [al, bl] = [a.split(sep), b.split(sep)]
193
+ const [al, bl] = [a.split('/'), b.split('/')]
125
194
  while (al.length > 0 && al[0] === bl[0]) {
126
195
  al.shift()
127
196
  bl.shift()
@@ -145,26 +214,23 @@ export function sortPaths(a, b) {
145
214
  throw new Error('Unreachable')
146
215
  }
147
216
 
217
+ const byPath = (a, b) => sortPaths(a[0], b[0])
218
+
148
219
  export const isPlainObject = (x) => x && [null, Object.prototype].includes(Object.getPrototypeOf(x))
149
220
 
150
221
  export const fromEntries = (entries) => Object.setPrototypeOf(Object.fromEntries(entries), null)
151
222
 
152
- export const fileSetToObject = (set) => [...set].toSorted((a, b) => sortPaths(a, b))
223
+ export const fileSetToObject = (set) => [...set].toSorted(sortPaths)
153
224
 
154
225
  // `sorted: false` only for a machine-only payload whose reader does not care about ordering (shard.js).
155
226
  export const fileMapToObject = (map, { sorted = true } = {}) => {
156
- if (!sorted) {
157
- const out = Object.create(null)
158
- for (const [k, v] of map) out[k] = v instanceof Map ? fileMapToObject(v, { sorted }) : v
159
- return out
160
- }
161
- return fromEntries(
162
- [...map]
163
- .toSorted((a, b) => sortPaths(a[0], b[0]))
164
- .map(([k, v]) => [k, v instanceof Map ? fileMapToObject(v, { sorted }) : v])
165
- )
227
+ const entries = sorted ? [...map].toSorted(byPath) : [...map]
228
+ return fromEntries(entries.map(([k, v]) => [k, v instanceof Map ? fileMapToObject(v, { sorted }) : v]))
166
229
  }
167
230
 
231
+ // The Map at `map.get(key)`, created on first use.
232
+ export const nestedMap = (map, key) => map.get(key) ?? map.set(key, new Map()).get(key)
233
+
168
234
  export const objectToMaps = (obj) => new Map(
169
235
  Object.entries(obj).map(([k, v]) => [k, isPlainObject(v) ? objectToMaps(v) : v])
170
236
  )
@@ -176,6 +242,8 @@ export const objectToMaps = (obj) => new Map(
176
242
  // a `..` with nothing left to pop is an escape -- normalize would keep it as a leading `..` forever.
177
243
  export function posixPathEscapes(path) {
178
244
  if (path.startsWith('/')) return true
245
+ // The walk can only return true via a literal '..' segment, so the ordinary keys skip the split.
246
+ if (!path.includes('..')) return false
179
247
  let depth = 0
180
248
  for (const segment of path.split('/')) {
181
249
  if (segment === '.' || segment === '') continue
@@ -185,6 +253,122 @@ export function posixPathEscapes(path) {
185
253
  return false
186
254
  }
187
255
 
256
+ // THE rule every path an artifact records must satisfy (a file key's too, see canonicalFileKey): in
257
+ // the root, and free of `\` -- part of a name off Windows, which stasis refuses everywhere rather than
258
+ // take for another path, and on Windows a separator a `..\x` would climb out by.
259
+ export const isRefusedPath = (path) => path.includes('\\') || posixPathEscapes(path)
260
+
261
+ // Throws unless `path`, one of `what`'s, is a path an artifact may record (see isRefusedPath).
262
+ export const assertArtifactPath = (path, what) =>
263
+ assert(!isRefusedPath(path), `${what}: path '${path}' escapes the root or holds a '\\'`)
264
+
265
+ // THE rule an artifact's entry must satisfy, on read (parseEntries) and on write (serializeEntries).
266
+ const assertEntry = (entry, what) =>
267
+ assert(typeof entry === 'string' && entry !== '' && !isRefusedPath(entry), `${what}: invalid entry ${JSON.stringify(entry)}`)
268
+
269
+ // An artifact's `entries` list as a Set: each a non-empty in-root path, listed once (a dupe would
270
+ // collapse in the Set and round-trip to different bytes).
271
+ export function parseEntries(list, what) {
272
+ assert(Array.isArray(list), `${what}: entries must be an array of file paths`)
273
+ const out = new Set()
274
+ for (const entry of list) {
275
+ assertEntry(entry, what)
276
+ assert(!out.has(entry), `${what}: entry '${entry}' is listed twice`)
277
+ out.add(entry)
278
+ }
279
+ return out
280
+ }
281
+
282
+ // The `entries` list an artifact writes: the same rule as on read, so an in-memory construct can't
283
+ // serialize what parse would reject; path-sorted.
284
+ export function serializeEntries(entries, what) {
285
+ for (const entry of entries) assertEntry(entry, what)
286
+ return fileSetToObject(entries)
287
+ }
288
+
289
+ // THE rule an artifact's `formats` entry must satisfy, on read (parseFormats) and on write
290
+ // (serializeFormats): a path an artifact may record, and a known format.
291
+ function assertFormat(file, format) {
292
+ assertArtifactPath(file, 'formats')
293
+ assert(KNOWN_FORMATS.has(format), `unknown format '${format}' for ${file}`)
294
+ }
295
+
296
+ // An artifact's `formats` object as a validated Map. '' and '.' alias to the same key (older
297
+ // artifacts keyed the root listing ''); normalized, failing closed on dupes, and an unknown format
298
+ // is rejected at the schema boundary so a tampered artifact fails closed.
299
+ export function parseFormats(json) {
300
+ assert(isPlainObject(json))
301
+ const formats = new Map()
302
+ for (const [file, format] of Object.entries(json)) {
303
+ assertFormat(file, format)
304
+ const key = file === '' ? '.' : file
305
+ assert(!formats.has(key), `duplicate format key '${key}'`)
306
+ formats.set(key, format)
307
+ }
308
+ return formats
309
+ }
310
+
311
+ // The `formats` an artifact writes: the same rule as on read, so an in-memory construct can't serialize
312
+ // what parse would reject; path-sorted.
313
+ export function serializeFormats(formats) {
314
+ for (const [file, format] of formats) assertFormat(file, format)
315
+ return fileMapToObject(formats)
316
+ }
317
+
318
+ // THE rules an artifact's `imports` (conditions -> parent -> specifier -> target, as nested Maps)
319
+ // must satisfy, on read (parseImports) and on write (serializeImports). Paths escaping the root (incl.
320
+ // mid-path `a/../../x`) or holding a `\` are rejected: getImport resolves against the root at load. A
321
+ // target is a file, or (--metro) a non-empty { platform: file } map.
322
+ function assertImports(imports) {
323
+ for (const [, byParent] of imports) {
324
+ assert(byParent instanceof Map)
325
+ for (const [parent, specifiers] of byParent) {
326
+ assertArtifactPath(parent, 'imports')
327
+ assert(specifiers instanceof Map)
328
+ for (const [, target] of specifiers) {
329
+ if (typeof target === 'string') {
330
+ assertArtifactPath(target, 'imports')
331
+ continue
332
+ }
333
+ assert(target instanceof Map && target.size > 0, 'import target must be a file or a non-empty {platform: file} map')
334
+ for (const [platform, file] of target) {
335
+ assert(typeof platform === 'string' && platform.length > 0 && !platform.includes('/'), `invalid platform key '${platform}'`)
336
+ assert(typeof file === 'string')
337
+ assertArtifactPath(file, 'imports')
338
+ }
339
+ }
340
+ }
341
+ }
342
+ }
343
+
344
+ // An artifact's `imports` object as nested Maps (see assertImports).
345
+ export function parseImports(json) {
346
+ assert(isPlainObject(json))
347
+ const imports = objectToMaps(json)
348
+ assertImports(imports)
349
+ return imports
350
+ }
351
+
352
+ // The `imports` an artifact writes: the same rules as on read (see assertImports); path-sorted.
353
+ export function serializeImports(imports) {
354
+ assertImports(imports)
355
+ return fileMapToObject(imports)
356
+ }
357
+
358
+ // A module map as the serialized `modules` (node_modules buckets) and `sources` (the rest) objects
359
+ // of a `what` artifact, buckets and files path-sorted so the bytes are canonical.
360
+ export function groupModules(modules, { skipEmpty = false, what } = {}) {
361
+ const grouped = { modules: [], sources: [] }
362
+ for (const [dir, info] of modules) {
363
+ if (skipEmpty && Object.keys(info.files).length === 0) continue
364
+ const inNodeModules = hasNodeModulesSegment(dir)
365
+ if (inNodeModules) assert(info.name && info.version && info.files)
366
+ const files = fromEntries(Object.entries(info.files).toSorted(byPath))
367
+ grouped[inNodeModules ? 'modules' : 'sources'].push([dir, moduleInfo({ ...info, repo: normalizeModuleRepo(dir, info, what), files })])
368
+ }
369
+ return { modules: fromEntries(grouped.modules.toSorted(byPath)), sources: fromEntries(grouped.sources.toSorted(byPath)) }
370
+ }
371
+
188
372
  // A target is a resolved-file string, or a { platform: file } Map under --metro.
189
373
  function importTargetsEqual(a, b) {
190
374
  const aMap = a instanceof Map
@@ -202,11 +386,9 @@ export function mergeImportMaps(a, b, label) {
202
386
  const out = new Map()
203
387
  const absorb = (imports) => {
204
388
  for (const [conditions, byParent] of imports) {
205
- let outByParent = out.get(conditions)
206
- if (outByParent === undefined) out.set(conditions, (outByParent = new Map()))
389
+ const outByParent = nestedMap(out, conditions)
207
390
  for (const [parent, specs] of byParent) {
208
- let outSpecs = outByParent.get(parent)
209
- if (outSpecs === undefined) outByParent.set(parent, (outSpecs = new Map()))
391
+ const outSpecs = nestedMap(outByParent, parent)
210
392
  for (const [spec, target] of specs) {
211
393
  if (outSpecs.has(spec)) {
212
394
  assert(importTargetsEqual(outSpecs.get(spec), target),
@@ -254,6 +436,42 @@ export function mergeExecutableSets(a, b, bModules, scope) {
254
436
  return out
255
437
  }
256
438
 
439
+ // An empty, '.' or '..' path segment.
440
+ const NON_CANONICAL_SEGMENT = /(?:^|\/)\.{0,2}(?:\/|$)/u
441
+
442
+ // The flat key of `rel` in bucket `dir`; throws unless canonical ('.' only as the root listing, rel '')
443
+ // and free of `\`, which no artifact path holds: off Windows it is part of a name, refused everywhere.
444
+ export function canonicalFileKey(dir, rel, what) {
445
+ const key = moduleFileKey(dir, rel)
446
+ // Message built only on failure: this runs for every file.
447
+ if ((key !== '.' || rel !== '') && NON_CANONICAL_SEGMENT.test(key)) assert(false, `${what}: non-canonical file key ${JSON.stringify(key)}`)
448
+ if (key.includes('\\')) assert(false, `${what}: file key '${key}' holds a '\\', which no path may`)
449
+ return key
450
+ }
451
+
452
+ // Maps each file's flat key to its bucket; rejects non-canonical keys and refused bucket dirs (an empty
453
+ // bucket's too, which no file key would reach), reports duplicates to onDuplicate.
454
+ export function flatFileKeys(modules, what, onDuplicate) {
455
+ const owners = new Map()
456
+ for (const [dir, { files }] of modules) {
457
+ if (typeof dir !== 'string') assert(false, `${what}: bucket dir ${String(dir)} is not a string`)
458
+ assertArtifactPath(dir, what)
459
+ for (const rel of Object.keys(files)) {
460
+ const key = canonicalFileKey(dir, rel, what)
461
+ const owner = owners.get(key)
462
+ if (owner !== undefined) onDuplicate(key, owner, dir)
463
+ owners.set(key, dir)
464
+ }
465
+ }
466
+ return owners
467
+ }
468
+
469
+ // The flatFileKeys duplicate handler for an artifact's parse/serialize: two bucket splits flattening
470
+ // to one path means module bucketing changed between writes.
471
+ export const duplicateKeyError = (what, regenerate) => (key) => assert(false,
472
+ `duplicate file key '${key}' across ${what} buckets -- module bucketing changed between writes ` +
473
+ `(a workspace package without a version now owns its own bucket); regenerate the ${regenerate}`)
474
+
257
475
  // Result `files` objects are null-prototype, so a `__proto__` file name is a plain own key.
258
476
  export function mergeModuleMaps(a, b, label) {
259
477
  const out = new Map()
@@ -261,20 +479,23 @@ export function mergeModuleMaps(a, b, label) {
261
479
  for (const [dir, info] of modules) {
262
480
  const existing = out.get(dir)
263
481
  if (existing === undefined) {
264
- out.set(dir, {
265
- name: info.name,
266
- version: info.version,
267
- ...(info.ecosystem === undefined ? {} : { ecosystem: info.ecosystem }),
268
- files: Object.assign(Object.create(null), info.files),
269
- })
482
+ out.set(dir, moduleInfo({ ...info, files: Object.assign(Object.create(null), info.files) }))
270
483
  continue
271
484
  }
272
485
  assert(existing.name === info.name,
273
486
  `${label}: module '${dir}' name mismatch ('${existing.name}' vs '${info.name}')`)
487
+ // A one-sided absent version is usually a migration skew: older stasis fabricated '0.0.0'
488
+ // for a workspace package whose package.json has no version, newer stasis records none.
274
489
  assert(existing.version === info.version,
275
- `${label}: module '${dir}' version mismatch ('${existing.version}' vs '${info.version}')`)
490
+ `${label}: module '${dir}' version mismatch ('${existing.version}' vs '${info.version}')` +
491
+ ((existing.version == null) === (info.version == null) ? '' :
492
+ ` -- an artifact from an older stasis may record a placeholder version for a workspace ` +
493
+ `package without one; regenerate it (bundle=replace / lock=replace)`))
276
494
  assert(existing.ecosystem === info.ecosystem,
277
495
  `${label}: module '${dir}' ecosystem mismatch ('${existing.ecosystem ?? '(none)'}' vs '${info.ecosystem ?? '(none)'}')`)
496
+ assert(reposAgree(existing.repo, info.repo),
497
+ `${label}: module '${dir}' repo mismatch (${JSON.stringify(existing.repo)} vs ${JSON.stringify(info.repo)})`)
498
+ if (existing.repo === undefined && info.repo !== undefined) out.set(dir, moduleInfo({ ...existing, repo: info.repo }))
278
499
  for (const [rel, value] of Object.entries(info.files)) {
279
500
  if (Object.hasOwn(existing.files, rel)) {
280
501
  assert(existing.files[rel] === value, `${label}: content mismatch for '${moduleFileKey(dir, rel)}'`)
@@ -286,6 +507,14 @@ export function mergeModuleMaps(a, b, label) {
286
507
  }
287
508
  absorb(a)
288
509
  absorb(b)
510
+ // One project-relative path must live in exactly one bucket. Bucketing can change between
511
+ // releases (a versionless workspace package used to fall through to a parent bucket and now owns
512
+ // its own), and per-dir absorption cannot see that: without this check the merge would WRITE an
513
+ // artifact that then fails its own next parse on the duplicate-file-key guard.
514
+ flatFileKeys(out, label, (key, owner, dir) => assert(false,
515
+ `${label}: file '${key}' is bucketed under both '${owner}' and '${dir}' -- module bucketing ` +
516
+ `changed between the artifacts (a workspace package without a version now owns its own ` +
517
+ `bucket); regenerate the artifact (bundle=replace / lock=replace)`))
289
518
  return out
290
519
  }
291
520