@exodus/stasis 1.0.0-rc.1 → 1.0.0-rc.3

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.
package/bin/stasis.js CHANGED
@@ -1,9 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { fileURLToPath } from 'node:url'
4
- import { basename, dirname, isAbsolute, join, resolve } from 'node:path'
4
+ import { basename, dirname, resolve } from 'node:path'
5
5
  import { existsSync, realpathSync } from 'node:fs'
6
- import { homedir } from 'node:os'
7
6
  import assert from 'node:assert/strict'
8
7
  import { parseLeadingOptions, valueFlagsOf } from '@exodus/stasis-core/util'
9
8
  import { addCli, brotliQualityOption, extractCli, parseRunOptions, pruneCli, runProgram, setEnv, setRunEnv } from '@exodus/stasis-core/cli'
@@ -68,12 +67,14 @@ async function warningsOnFailure(build) {
68
67
  }
69
68
  }
70
69
 
71
- // The per-user cache directory, as each platform names it (XDG ignores a relative path).
72
- function userCacheDir() {
73
- if (isAbsolute(process.env.XDG_CACHE_HOME ?? '')) return join(process.env.XDG_CACHE_HOME, 'stasis')
74
- if (process.platform === 'darwin') return join(homedir(), 'Library', 'Caches', 'stasis')
75
- if (process.platform === 'win32') return join(process.env.LOCALAPPDATA || join(homedir(), 'AppData', 'Local'), 'stasis', 'Cache')
76
- return join(homedir(), '.cache', 'stasis')
70
+ // The cache directory (STASIS_CACHE_DIR, else the platform's), refused where it is relative or there is none.
71
+ async function cacheDir() {
72
+ const { userCacheDir } = await import('../src/cmd/cache-dir.js')
73
+ try {
74
+ return userCacheDir()
75
+ } catch (cause) {
76
+ fail(`Error: ${cause.message}`)
77
+ }
77
78
  }
78
79
 
79
80
  const command = argv.shift()
@@ -147,6 +148,10 @@ if (command === '-v' || command === '--version') {
147
148
  } catch (cause) {
148
149
  fail(`Error: ${cause.message}`)
149
150
  }
151
+ if (plan.deptree) {
152
+ const { setCacheDir } = await import('../src/vfs-bundle.js')
153
+ setCacheDir(await cacheDir())
154
+ }
150
155
  const { bundleCommand } = await import('../src/cmd/bundle.js')
151
156
  await bundleCommand({ cwd, ...plan })
152
157
  } else if (command === 'github-bundle') {
@@ -219,7 +224,7 @@ if (command === '-v' || command === '--version') {
219
224
  }
220
225
  // Tarballs, zips and GitHub trees are cached where `stasis audit` caches.
221
226
  const { setCacheDir } = await import('../src/vfs-bundle.js')
222
- setCacheDir(userCacheDir())
227
+ setCacheDir(await cacheDir())
223
228
  const { githubBundleCommand } = await import('../src/cmd/github-bundle.js')
224
229
  await githubBundleCommand({
225
230
  github,
@@ -384,7 +389,7 @@ if (command === '-v' || command === '--version') {
384
389
  // Upstream caches what it looks up (a package's GitHub repo, a month each) only where told to, and
385
390
  // answers from it without asking: a per-user directory, never one shared with less trusted jobs.
386
391
  const { setCacheDir } = await import('@preventive/upstream/npm.js')
387
- setCacheDir(userCacheDir())
392
+ setCacheDir(await cacheDir())
388
393
  const { AuditError, audit, printAuditReport } = await import('../src/audit.js')
389
394
  // With no file, the package manager's lockfile installing the project too, or each JS one its
390
395
  // stasis.config.json names in its `lockfiles`: their packages the bundle doesn't hold checked for
@@ -441,7 +446,7 @@ if (command === '-v' || command === '--version') {
441
446
  // `stasis audit` caches, each held to its id or integrity as it is read.
442
447
  const { createClient } = await import('@preventive/upstream/github.js')
443
448
  const { setCacheDir } = await import('@preventive/upstream/npm.js')
444
- setCacheDir(userCacheDir())
449
+ setCacheDir(await cacheDir())
445
450
  const { githubToken, withNpmToken } = await import('../src/cmd/tokens.js')
446
451
  const { ValidateError, exitCodeOf, validate, printValidationReport } = await import('../src/validate.js')
447
452
  let report
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis",
3
- "version": "1.0.0-rc.1",
3
+ "version": "1.0.0-rc.3",
4
4
  "description": "File-level source bundles and lockfiles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -43,6 +43,7 @@
43
43
  "src/bundle-reader.js",
44
44
  "src/cmd/bundle.js",
45
45
  "src/cmd/bundle-options.js",
46
+ "src/cmd/cache-dir.js",
46
47
  "src/cmd/build.js",
47
48
  "src/cmd/diff.js",
48
49
  "src/cmd/github-bundle.js",
@@ -117,16 +118,18 @@
117
118
  },
118
119
  "homepage": "https://github.com/PreventiveMeasures/stasis#readme",
119
120
  "dependencies": {
121
+ "@babel/generator": "^7.29.7",
120
122
  "@exodus/bytes": "^1.16.0",
121
- "@exodus/stasis-core": "1.0.0-rc.1",
122
- "@exodus/stasis-plugins": "1.0.0-rc.1",
123
- "@preventive/archive": "^1.4.0",
124
- "@preventive/deptree": "^1.5.0",
123
+ "@exodus/stasis-core": "1.0.0-rc.3",
124
+ "@exodus/stasis-plugins": "1.0.0-rc.3",
125
+ "@preventive/archive": "^1.4.1",
126
+ "@preventive/deptree": "^1.5.2",
125
127
  "@preventive/lockfile": "^1.3.0",
126
- "@preventive/rn": "1.0.0-alpha.6",
127
- "@preventive/upstream": "^1.6.0",
128
+ "@preventive/rn": "1.0.0-alpha.8",
129
+ "@preventive/upstream": "^1.8.0",
128
130
  "@preventive/vfs": "^1.1.0",
129
131
  "flow-remove-types": "^2.322.0",
132
+ "hermes-parser": "^0.36.1",
130
133
  "oxc-parser": "^0.152.0"
131
134
  },
132
135
  "peerDependencies": {
package/src/cmd/build.js CHANGED
@@ -14,16 +14,17 @@ import { assetData, assetModule } from '@preventive/rn/metro-assets.js'
14
14
  import { HERMES_UNSUPPORTED, createBabelConfigTransform, createHermesTransform } from '../babel.js'
15
15
  import { bundleFromLockfile } from '../bundle-from-lockfile.js'
16
16
  import { METRO_RELEASE_DEFINE } from '../define.js'
17
- import { FLOW_EXTS, hasFlowPragma, stripFlowTypes } from '../flow.js'
17
+ import { FLOW_EXTS, hasFlowPragma, stripFlow } from '../flow.js'
18
18
  import { ASSET_REGISTRY, assetRegistryResolver } from '../metro-assets.js'
19
19
  import { parseFileWithKind } from '../parse.js'
20
+ import { getParser, syntaxErrors } from '../scan.js'
20
21
  import { writeFile } from './output.js'
21
22
 
22
23
  export { bundleFromLockfile }
23
24
 
24
25
  // An allowlist, so that a bundle format added later fails closed here rather than in a cryptic esbuild error.
25
26
  const BUILDABLE_FORMATS = new Set([
26
- 'module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript',
27
+ 'module', 'commonjs', 'json', 'module-typescript', 'commonjs-typescript', 'generic-javascript', 'generic-typescript',
27
28
  'resource', 'resource:base64', 'directory', 'stat:file', 'stat:directory',
28
29
  ])
29
30
 
@@ -169,11 +170,11 @@ function createFlowTransform(esbuild) {
169
170
  return undefined
170
171
  } catch { /* stripped below */ }
171
172
  }
172
- try {
173
- return { contents: stripFlowTypes(text, { removeEmptyImports: true }), loader }
174
- } catch {
175
- return undefined
176
- }
173
+ // oxc, as the scan checks it, so the build lowers what the bundle's scan lowered.
174
+ const lang = loader === 'jsx' ? 'jsx' : 'js'
175
+ const parses = (code) => syntaxErrors(getParser().parseSync(path, code, { sourceType: 'unambiguous', lang })).length === 0
176
+ const { stripped } = stripFlow(text, path, parses, { removeEmptyImports: true })
177
+ return stripped === undefined ? undefined : { contents: stripped, loader }
177
178
  }
178
179
  }
179
180
 
package/src/cmd/bundle.js CHANGED
@@ -20,7 +20,7 @@ import { installedRecords, pinInstalled } from '../installed.js'
20
20
  import { State } from '@exodus/stasis-core/state'
21
21
  import { bundledIntegrity, readFileSyncMaybe } from '@exodus/stasis-core/state-util'
22
22
  import { checkoutCommit, detectRepo, normalizeEntries, packageLookup, packageJSONStat, packageType, parseGithubRepository, readJson, readModuleManifest, readPackageJson, readRegularFileOrNull, scopeManifestOf } from '@exodus/stasis-core/bundle-util'
23
- import { attestsAny, BUNDLE_FILE, CONFIG_FILE, EMPTY_MODULE_PATH, assertRealPathWithinBase, classifyFormat, dependencyEcosystem, fileInBucket, archiveIntegrity, hasNodeModulesSegment, heldToAccounts, isDotEnvFile, isPathWithin, isPlainObject, JS_EXTENSIONS, LOCK_FILE, memoized, moduleFileKey, moduleFileKeys, moduleInfo, nestedMap, nodeExtFormat, observeExecutable, parseExtraOption, parseLockfilesOption, parseResourcesOption, posixPathEscapes, relativeEscapes, splitNodeModulesPath, toPosix, upsert, withAttestedOf, withMetadataOf, sameGithub } from '@exodus/stasis-core/util'
23
+ import { attestsAny, BUNDLE_FILE, CONFIG_FILE, EMPTY_MODULE_PATH, assertRealPathWithinBase, classifyFormat, dependencyEcosystem, fileInBucket, archiveIntegrity, hasNodeModulesSegment, heldToAccounts, isDotEnvFile, isPathWithin, isPlainObject, JS_EXTENSIONS, LOCK_FILE, memoized, metroFormat, moduleFileKey, moduleFileKeys, moduleInfo, nestedMap, observeExecutable, parseExtraOption, parseLockfilesOption, parseResourcesOption, posixPathEscapes, relativeEscapes, splitNodeModulesPath, toPosix, upsert, withAttestedOf, withMetadataOf, sameGithub } from '@exodus/stasis-core/util'
24
24
  import { diskHost } from '@exodus/stasis-core/host'
25
25
  import { linksOn, parsePolicy, recordedIn } from '@exodus/stasis-core/policy'
26
26
  import { parseBundleBytes } from '@exodus/stasis-core/brotli'
@@ -1151,8 +1151,8 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1151
1151
  reached.add(abs)
1152
1152
  if (restrict !== null && !isTransformInput(abs, info)) metroTaken.add(abs)
1153
1153
  if (info.resource) resourceRels.add(rel)
1154
- // Under --metro a code file has only the format its name gives it, as the Metro plugin records it.
1155
- formatsByRel.set(rel, metro && !info.resource ? nodeExtFormat(abs) : info.format)
1154
+ // Under --metro a code file has the format Metro builds it in, as the Metro plugin records it.
1155
+ formatsByRel.set(rel, metro && !info.resource ? metroFormat(abs) : info.format)
1156
1156
  for (const e of info.edges) {
1157
1157
  if (e.builtin || e.dynamic) continue
1158
1158
  let target
@@ -1338,8 +1338,11 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1338
1338
  put(rel, buf.toString('utf8'), format)
1339
1339
  }
1340
1340
  const realBase = host.realpath(baseDir)
1341
- // Metro's empty module is carried as the file it is, a code file of Metro's (no format).
1342
- if (usesEmpty && metroEmpty !== null) reached.add(metroEmpty)
1341
+ // Metro's empty module is carried as the file it is, a code file of Metro's.
1342
+ if (usesEmpty && metroEmpty !== null) {
1343
+ reached.add(metroEmpty)
1344
+ if (formatsByRel.get(toRel(metroEmpty)) === undefined) formatsByRel.set(toRel(metroEmpty), metroFormat(metroEmpty))
1345
+ }
1343
1346
  for (const abs of reached) {
1344
1347
  const rel = toRel(abs)
1345
1348
  // Security: an entry is taken as spelled (and --metro-resolver keeps a path it can't realpath), so a
@@ -1353,7 +1356,7 @@ async function buildResolvedJsBundle({ cwd = process.cwd(), env = process.env, e
1353
1356
  if (formatsByRel.has(EMPTY_MODULE_PATH)) {
1354
1357
  throw new Error(`Bundle needs the reserved empty-module path ${EMPTY_MODULE_PATH}, but the project has a real file there`)
1355
1358
  }
1356
- put(EMPTY_MODULE_PATH, '', metro ? undefined : 'commonjs')
1359
+ put(EMPTY_MODULE_PATH, '', metro ? metroFormat(EMPTY_MODULE_PATH) : 'commonjs')
1357
1360
  }
1358
1361
 
1359
1362
  const packageOf = packageLookup(baseDir, { host })
@@ -2177,10 +2180,10 @@ async function buildOfCommand({ cwd = process.cwd(), env = process.env, home, en
2177
2180
  const bundleExtra = builds !== undefined
2178
2181
  let union, root
2179
2182
  if (deptree) {
2180
- // The disk read through the tree, nothing cached (`cache: false`, and no setCacheDir here).
2183
+ // The disk read through the tree, what is fetched cached where setCacheDir says (the CLI's user cache directory).
2181
2184
  const { createClient } = await import('@preventive/upstream/github.js')
2182
2185
  const built = await withNpmToken(home, () => buildFromLockfile('bundleCommand', diskHost, {
2183
- cwd: resolve(cwd), lockfile, cache: false, lazy: false, client: createClient({ token: env.GITHUB_TOKEN || null }),
2186
+ cwd: resolve(cwd), lockfile, lazy: false, client: createClient({ token: env.GITHUB_TOKEN || null }),
2184
2187
  }, { env, plan: { builds: plans, bundleExtra }, outputsAt, refuseInstalled: true, why: '--deptree lays out the dependencies from a lockfile' }))
2185
2188
  console.warn(unpackedSummary(built.stats))
2186
2189
  root = built.root
@@ -0,0 +1,19 @@
1
+ import { isAbsolute } from 'node:path'
2
+
3
+ import { cacheDirFor } from '@preventive/upstream/npm.js'
4
+
5
+ // The directory the commands cache in: STASIS_CACHE_DIR itself where it is set, else the platform's
6
+ // cache directory for stasis as @preventive/upstream names it, the one it reads as stasis's (an
7
+ // absolute XDG_CACHE_HOME's only on Linux and the BSDs). STASIS_CACHE_DIR is the user's own choice,
8
+ // so a relative one is refused rather than ignored: it would resolve against whatever directory a
9
+ // command runs in, a project's checkout as likely as not. Both read the process's environment.
10
+ export function userCacheDir() {
11
+ const own = process.env.STASIS_CACHE_DIR
12
+ if (own) {
13
+ if (!isAbsolute(own)) throw new Error(`STASIS_CACHE_DIR must be an absolute path, not ${JSON.stringify(own)}`)
14
+ return own
15
+ }
16
+ const dir = cacheDirFor('stasis')
17
+ if (dir === undefined) throw new Error('there is no cache directory, as no absolute home directory is known: set STASIS_CACHE_DIR')
18
+ return dir
19
+ }
package/src/diff.js CHANGED
@@ -1,4 +1,4 @@
1
- import { ATTESTED_METADATA, extSetsEqual, moduleFileKey, REPO_FIELDS, sameGithub, sortPaths, upsert } from '@exodus/stasis-core/util'
1
+ import { ATTESTED_METADATA, extSetsEqual, moduleFileKey, REPO_FIELDS, sameAttested, sameGithub, sortPaths, upsert } from '@exodus/stasis-core/util'
2
2
 
3
3
  // Two parsed artifacts compared by module and file, never reading disk: each file as an SRI digest, a bundle's bytes
4
4
  // re-hashed by the injected `hash`.
@@ -62,7 +62,7 @@ export function diffArtifacts(left, right, { imports = false, hash } = {}) {
62
62
  // A dependency's repo picks the GitHub advisories audit asks, so a change to it is one to review.
63
63
  if (!reposAgree(l.repo, r.repo)) changes.repoChange = { from: l.repo, to: r.repo }
64
64
  for (const field of ATTESTED_METADATA) {
65
- if (JSON.stringify(l[field]) !== JSON.stringify(r[field])) changes[`${field}Change`] = { from: l[field], to: r[field] }
65
+ if (!sameAttested(field, l[field], r[field])) changes[`${field}Change`] = { from: l[field], to: r[field] }
66
66
  }
67
67
  if (Object.keys(changes).length > 0) modulesChanged.push({ dir, name: r.name ?? l.name, ...changes })
68
68
 
@@ -158,13 +158,15 @@ const CHANGED = '*'
158
158
  // `owner/name`, its commit and directory where recorded: `owner/name@0123abc (packages/x)`.
159
159
  const repoLabel = ({ github, commit, directory }) => `${github ?? '?'}${commit ? `@${commit.slice(0, 7)}` : ''}${directory ? ` (${directory})` : ''}`
160
160
 
161
+ const patchLabel = (patch) => (patch ? `${patch.integrity}${patch.file ? ` (${patch.file})` : ''}` : 'none')
162
+
161
163
  function describeModuleChange({ name, versionChange, nameChange, repoChange, integrityChange, patchChange }) {
162
164
  const segs = []
163
165
  if (versionChange) segs.push(`${versionChange.from} -> ${versionChange.to}`)
164
166
  if (nameChange) segs.push(`name ${nameChange.from} -> ${nameChange.to}`)
165
167
  if (repoChange) segs.push(`repo ${repoLabel(repoChange.from)} -> ${repoLabel(repoChange.to)}`)
166
168
  if (integrityChange) segs.push(`integrity ${integrityChange.from ?? 'none'} -> ${integrityChange.to ?? 'none'}`)
167
- if (patchChange) segs.push(`patch ${patchChange.from?.integrity ?? 'none'} -> ${patchChange.to?.integrity ?? 'none'}`)
169
+ if (patchChange) segs.push(`patch ${patchLabel(patchChange.from)} -> ${patchLabel(patchChange.to)}`)
168
170
  return `${name ? `${name} ` : ''}${segs.join(', ')}`
169
171
  }
170
172
 
package/src/flow.js CHANGED
@@ -1,6 +1,9 @@
1
1
  // --flow (which --metro turns on): Flow type syntax stripped as Metro's Babel (@react-native/babel-preset) strips it,
2
2
  // by flow-remove-types -- stasis's own dependency, never the project's, loaded lazily.
3
3
  import { createRequire } from 'node:module'
4
+ import { splitNodeModulesPath, toPosix } from '@exodus/stasis-core/util'
5
+
6
+ const require = createRequire(import.meta.url)
4
7
 
5
8
  // The JS family alone: flow-remove-types would corrupt TS-only constructs. It preserves JSX.
6
9
  export const FLOW_EXTS = new Set(['.js', '.cjs', '.mjs', '.jsx'])
@@ -14,14 +17,54 @@ const NOFLOW = '\n/* @noflow */'
14
17
  const NOFLOW_STRIPPED = `\n/* ${' '.repeat('@noflow'.length)} */`
15
18
 
16
19
  let flowRemoveTypes
20
+ let hermesParser
21
+ let generate
17
22
 
18
23
  // `source` with its Flow types blanked in place (offsets kept): one marked `@flow` as Flow throughout (`f<T>(x)` a
19
24
  // call), any other as a `@noflow` file, marked so past its end. Flow code (`enum`s) stays. Throws where it can't parse.
20
- export function stripFlowTypes(source, { removeEmptyImports = false } = {}) {
21
- flowRemoveTypes ??= createRequire(import.meta.url)('flow-remove-types')
25
+ function stripFlowTypes(source, { removeEmptyImports = false } = {}) {
26
+ flowRemoveTypes ??= require('flow-remove-types')
22
27
  if (hasFlowPragma(source)) return flowRemoveTypes(source, { all: true, removeEmptyImports }).toString()
23
28
  if (source.includes('@noflow')) return flowRemoveTypes(source, { removeEmptyImports }).toString()
24
29
  const stripped = flowRemoveTypes(source + NOFLOW, { removeEmptyImports }).toString()
25
30
  if (!stripped.endsWith(NOFLOW_STRIPPED)) throw new Error('flow-remove-types left the @noflow comment stasis appended')
26
31
  return stripped.slice(0, -NOFLOW_STRIPPED.length)
27
32
  }
33
+
34
+ // A file of the package installed as `react-native`, not of one nested in it.
35
+ export const inReactNative = (path) => splitNodeModulesPath(toPosix(path))?.name === 'react-native'
36
+
37
+ // `source` with its Flow code (`component`, `hook`, `match`, `enum`) lowered as Metro's Babel lowers it, types kept;
38
+ // an `enum` requires `flow-enums-runtime`. Throws where it can't parse.
39
+ function lowerFlowSyntax(source) {
40
+ hermesParser ??= require('hermes-parser')
41
+ generate ??= require('@babel/generator').default
42
+ const flow = hasFlowPragma(source)
43
+ const ast = hermesParser.parse(source, {
44
+ babel: true,
45
+ flow: flow ? 'all' : 'detect',
46
+ sourceType: 'unambiguous',
47
+ transformOptions: { TransformEnumSyntax: { enable: true } },
48
+ })
49
+ // `comments: false` drops the `@flow` mark the strip reads.
50
+ return (flow ? '/* @flow */' : '') + generate(ast, { comments: false, retainLines: true }).code
51
+ }
52
+
53
+ // `{ parsed, source, stripped }`: `text` with its types stripped, as `parse` reads it (falsy where it can't). In a file
54
+ // of react-native that doesn't parse so, `source` is `text` with its Flow code lowered first. Neither parsing: the
55
+ // plain strip, `parsed` null, with any `error` thrown.
56
+ export function stripFlow(text, path, parse, { removeEmptyImports = false } = {}) {
57
+ const read = (lower) => {
58
+ try {
59
+ const source = lower ? lowerFlowSyntax(text) : text
60
+ const stripped = stripFlowTypes(source, { removeEmptyImports })
61
+ return { parsed: parse(stripped) || null, source, stripped }
62
+ } catch (error) {
63
+ return { parsed: null, error }
64
+ }
65
+ }
66
+ const plain = read(false)
67
+ if (plain.parsed !== null || !inReactNative(path)) return plain
68
+ const lowered = read(true)
69
+ return lowered.parsed !== null ? lowered : { ...plain, error: lowered.error ?? plain.error }
70
+ }
package/src/installed.js CHANGED
@@ -4,8 +4,9 @@ import { basename, dirname, join, relative, resolve } from 'node:path'
4
4
  import { isValidRepoField } from '@exodus/stasis-core/bundle'
5
5
  import { parseGithubRepository, readJson, readText } from '@exodus/stasis-core/bundle-util'
6
6
  import { diskHost } from '@exodus/stasis-core/host'
7
- import { hexIntegrity, isNpmDependency, isPlainObject, relativeEscapes, tarballIntegrityOf, toPosix, upsert, withoutBom } from '@exodus/stasis-core/util'
7
+ import { hexIntegrity, isNpmDependency, isPlainObject, memoized, relativeEscapes, tarballIntegrityOf, toPosix, upsert, withoutBom } from '@exodus/stasis-core/util'
8
8
  import { parsePnpmLockfile } from '@preventive/lockfile/pnpm.js'
9
+ import { parseYaml } from '@preventive/lockfile/yaml.js'
9
10
  import { parseYarn1Lockfile } from '@preventive/lockfile/yarn1.js'
10
11
 
11
12
  // What the package manager that laid out a node_modules recorded there; one missing or unreadable names nothing.
@@ -26,6 +27,10 @@ export const resolutionCommit = (resolution) => {
26
27
  return resolution?.type === 'tarball' ? githubTarball(resolution.tarball ?? '') : undefined
27
28
  }
28
29
 
30
+ // As pnpm hashes a patch: a sha256 in hex of its text with its CRLFs as LFs, or as it is.
31
+ export const patchHashes = (text) => new Set([text.replaceAll('\r\n', '\n'), text].map((one) => hash('sha256', one)))
32
+ export const isPatchOf = (text, patchHash) => patchHashes(text).has(patchHash)
33
+
29
34
  export const patchIntegrity = (patchHash) => (/^[\da-f]{64}$/u.test(patchHash ?? '') ? hexIntegrity('sha256', patchHash) : undefined)
30
35
 
31
36
  const repoAt = (commit, path = '') => {
@@ -104,15 +109,47 @@ const storeNames = (key) => {
104
109
  }
105
110
  export const STORE_HASH = /_[\da-f]{32}$/u
106
111
 
107
- function pnpmLocks(file, host, strict) {
112
+ // Of the lockfile `file` and the `patchers` (`[selector, { hash, path }]`) it locks: the file each hash's patch is in.
113
+ function patchLocator(file, patchers, host) {
114
+ // node_modules/.pnpm/lock.yaml copies the pnpm-lock.yaml beside node_modules, its paths from there.
115
+ const base = basename(file) === 'lock.yaml' ? resolve(file, '../../..') : dirname(file)
116
+ const settings = join(base, 'pnpm-workspace.yaml')
117
+ const named = memoized(() => {
118
+ const patches = parseLock(settings, host, true, parseYaml)?.patchedDependencies
119
+ return isPlainObject(patches) ? patches : {}
120
+ })
121
+ // Every file named for the hash held to it; of two of one text, the first named, as deptree records it.
122
+ return memoized((patchHash) => {
123
+ const [first] = patchers.filter(([, patch]) => patch.hash === patchHash).map(([selector, patch]) => {
124
+ const refused = (why) => new Error(`${file} patches ${selector} with ${patchHash}: ${why}`)
125
+ // pnpm 11's lockfile names a patch by its hash alone, its file in pnpm-workspace.yaml.
126
+ const path = patch.path ?? named()[selector]
127
+ if (typeof path !== 'string') throw refused(`neither it nor ${settings} names the patch's file`)
128
+ const at = resolve(base, path)
129
+ const text = readText(host, at)
130
+ if (text === null) throw refused(`${at} can't be read`)
131
+ if (!isPatchOf(text, patchHash)) throw refused(`${at} is a patch of another hash`)
132
+ return at
133
+ })
134
+ return first
135
+ })
136
+ }
137
+
138
+ // `aliases`, the top-level names pnpm's copy of the lockfile lists where it is read, are those held where they are: where
139
+ // pnpm hoists, the place of one it left out (a devDependency `pnpm install --prod` skips) holds whatever hoists into it,
140
+ // held by its name and version. `named` carries the names this one lists.
141
+ function pnpmLocks(file, host, strict, aliases) {
108
142
  const parsed = parseLock(file, host, strict, (text) => ({ lockfile: parsePnpmLockfile(text).lockfile }))
109
143
  if (parsed === undefined) return undefined
110
144
  const { lockfile } = parsed
145
+ // Of its patches alone: the cache keeps a run's locators, which must not hold the lockfile.
146
+ const patchers = Object.entries(lockfile?.patchedDependencies ?? {})
147
+ const patchAt = patchers.length === 0 ? undefined : patchLocator(file, patchers, host)
111
148
  const stored = new Map()
112
149
  const keyed = new Map()
113
150
  const ids = []
114
151
  for (const [key, { name, version, resolution, patchHash }] of Object.entries(lockfile?.packages ?? {})) {
115
- const record = { version, repo: repoAt(resolutionCommit(resolution), resolution?.path), integrity: resolution?.integrity, patchHash }
152
+ const record = { version, repo: repoAt(resolutionCommit(resolution), resolution?.path), integrity: resolution?.integrity, patchHash, ...(patchHash !== undefined && { patchAt }) }
116
153
  for (const store of storeNames(key)) stored.set(store, record)
117
154
  keyed.set(key, record)
118
155
  ids.push([`${name}@${version ?? ''}`, record])
@@ -122,13 +159,14 @@ function pnpmLocks(file, host, strict) {
122
159
  for (const importer of Object.values(lockfile?.importers ?? {})) {
123
160
  for (const [as, key] of [importer.dependencies, importer.devDependencies, importer.optionalDependencies].flatMap((deps) => Object.entries(deps ?? {}))) {
124
161
  const record = keyed.get(key)
125
- if (record) top.set(as, top.has(as) && top.get(as) !== record ? undefined : record)
162
+ if (record && (aliases?.has(as) ?? true)) top.set(as, top.has(as) && top.get(as) !== record ? undefined : record)
126
163
  }
127
164
  }
128
165
  const named = byId(ids)
166
+ const atTop = (location) => (location === `node_modules/${nameAt(location)}` ? top.get(nameAt(location)) : undefined)
129
167
  return {
130
168
  stored: (name) => stored.get(name) ?? stored.get(STORE_HASH.exec(name)?.[0]),
131
- named: (location, ...of) => (location === `node_modules/${nameAt(location)}` ? top.get(nameAt(location)) : undefined) ?? named(location, ...of),
169
+ named: Object.assign((location, ...of) => atTop(location) ?? named(location, ...of), { aliases: new Set(top.keys()) }),
132
170
  }
133
171
  }
134
172
 
@@ -167,7 +205,7 @@ const MANAGERS = [
167
205
  marker: '.modules.yaml',
168
206
  lockfiles: ['pnpm-lock.yaml'],
169
207
  installed: (dir, host) => pnpmLocks(join(dir, 'node_modules', '.pnpm', 'lock.yaml'), host)?.named,
170
- locks: (file, host) => pnpmLocks(file, host, true).named,
208
+ locks: (file, host, installed) => pnpmLocks(file, host, true, installed.aliases).named,
171
209
  none: (version) => `${version}; it locks none`,
172
210
  },
173
211
  ]
@@ -182,7 +220,7 @@ function layoutOf(dir, host) {
182
220
  if (locking.length > 1 && held.length === 0) throw new Error(`${dir}: ${locking.map(([, file]) => basename(file)).join(' and ')} are both there, and which installed its node_modules isn't known`)
183
221
  const [manager = marked.length === 1 ? marked[0] : undefined, lockfile] = held[0] ?? []
184
222
  const installed = (marked.length === 1 && marked[0] === manager && manager.installed(dir, host)) || NONE
185
- return { installed, lockfile, locks: lockfile && manager.locks(lockfile, host), none: manager?.none }
223
+ return { installed, lockfile, locks: lockfile && manager.locks(lockfile, host, installed), none: manager?.none }
186
224
  }
187
225
 
188
226
  // pnpm's, where it isolates: `store`'s lock.yaml held to the pnpm-lock.yaml of the directory the store is in.
@@ -214,9 +252,11 @@ export function installedMetadata(dir, ecosystem, { repo, integrity, patchHash }
214
252
  return { ...(repo && { repo }), ...(tarball && { integrity: tarball }), ...(patch && { patch: { integrity: patch } }) }
215
253
  }
216
254
 
217
- export function installedRecords(modules, root, host = diskHost, { stores = new Map(), layouts = new Map() } = {}) {
255
+ // With `locate`, a patch's record carries its file, from `root`: found and held to its hash, else refused.
256
+ export function installedRecords(modules, root, host = diskHost, { stores = new Map(), layouts = new Map(), locate = false } = {}) {
218
257
  const accounts = new Map()
219
258
  let top // the bundle root's real path, as each package's is compared with it
259
+ const rootReal = () => (top ??= resolve(host.realpath(resolve(root))))
220
260
  const layoutAt = (dir) => upsert(layouts, dir, () => layoutOf(dir, host))
221
261
  for (const [dir, info] of modules) {
222
262
  // An untagged bucket under node_modules is npm's too (an artifact from before the tag).
@@ -235,7 +275,7 @@ export function installedRecords(modules, root, host = diskHost, { stores = new
235
275
  ;[layout, at] = [upsert(stores, store, () => storeOf(store, host)), [name]]
236
276
  } else {
237
277
  try {
238
- top ??= resolve(host.realpath(resolve(root)))
278
+ rootReal()
239
279
  } catch {
240
280
  return accounts
241
281
  }
@@ -253,13 +293,17 @@ export function installedRecords(modules, root, host = diskHost, { stores = new
253
293
  const differs = locks && lockDiffers(info.version, record, locked, none)
254
294
  if (differs) throw new Error(`${lockfile} doesn't lock ${dir} as installed (${differs}): install from it again`)
255
295
  const told = record ?? locked
256
- if (told !== undefined && !told.ambiguous) accounts.set(dir, { ...installedMetadata(dir, info.ecosystem, told), ...(record === undefined && { locked: true }) })
296
+ if (told === undefined || told.ambiguous) continue
297
+ const metadata = installedMetadata(dir, info.ecosystem, told)
298
+ // A record of the lockfile alone is held to, never recorded: its patch is not looked for.
299
+ if (locate && record !== undefined && metadata.patch) metadata.patch.file = toPosix(relative(rootReal(), told.patchAt(told.patchHash)))
300
+ accounts.set(dir, { ...metadata, ...(record === undefined && { locked: true }) })
257
301
  }
258
302
  return accounts
259
303
  }
260
304
 
261
305
  export function pinInstalled(bundle, root, host = diskHost) {
262
- const accounts = installedRecords(bundle.modules, root, host)
306
+ const accounts = installedRecords(bundle.modules, root, host, { locate: true })
263
307
  for (const [dir, { locked, ...metadata }] of accounts) if (!locked) bundle.modules.set(dir, { ...bundle.modules.get(dir), ...metadata })
264
308
  return accounts
265
309
  }
@@ -3,8 +3,8 @@ import { installedRecords } from './installed.js'
3
3
 
4
4
  // `stasis run` preloads this before the loader; what it reads is kept across a run's calls.
5
5
  const cache = { stores: new Map(), layouts: new Map() }
6
- globalThis[INSTALLED] = (modules, root) => {
7
- const accounts = installedRecords(modules, root, undefined, cache)
6
+ globalThis[INSTALLED] = (modules, root, { locate } = {}) => {
7
+ const accounts = installedRecords(modules, root, undefined, { ...cache, locate })
8
8
  for (const [dir, { repo: _repo, ...account }] of accounts) accounts.set(dir, account)
9
9
  return accounts
10
10
  }
package/src/scan.js CHANGED
@@ -9,7 +9,7 @@ import { RELATIVE_REQUEST } from './resolve-node.js'
9
9
  import { branchFolder, parseDefine } from './define.js'
10
10
  import { diskHost } from '@exodus/stasis-core/host'
11
11
  import { hostMemo } from './vfs-bundle/lazy.js'
12
- import { FLOW_EXTS, hasFlowPragma, stripFlowTypes } from './flow.js'
12
+ import { FLOW_EXTS, hasFlowPragma, stripFlow } from './flow.js'
13
13
 
14
14
  // Static require/import graph walker: parses source, never loads or executes user code.
15
15
 
@@ -169,7 +169,7 @@ export class Scan {
169
169
 
170
170
  // classifyExtension gives 'resource' only for a non-code name.
171
171
  #isResource(file) {
172
- return this.resources.size > 0 && classifyExtension(file, this.resources) === 'resource'
172
+ return classifyExtension(file, this.resources) === 'resource'
173
173
  }
174
174
 
175
175
  walk(entries) {
@@ -303,16 +303,6 @@ export class Scan {
303
303
  }
304
304
  }
305
305
 
306
- // null where stripping fails: the caller keeps oxc's parse, saying so where that failed (`failed`).
307
- #stripFlow(src, file, failed) {
308
- try {
309
- return stripFlowTypes(src)
310
- } catch (cause) {
311
- if (failed) console.warn(`[stasis] --flow: could not strip Flow types from ${file}; leaving it to oxc (${cause.message})`)
312
- return null
313
- }
314
- }
315
-
316
306
  // As Node loads it: under the format its name and package declare, or for a typeless .js/.ts the one Node detects.
317
307
  parseFile(file) {
318
308
  const ext = extname(file)
@@ -359,18 +349,19 @@ export class Scan {
359
349
 
360
350
  // --flow, as Metro's Babel reads the file: one marked `@flow` even where oxc parses it (`f<T>(x)` a comparison
361
351
  // there), any other only where oxc's parse failed. A re-parse that still fails keeps the original's errors.
352
+ let source = src
362
353
  let strippedSource = null
363
354
  if (this.flow && FLOW_EXTS.has(ext) && (errors.length > 0 || hasFlowPragma(src))) {
364
- const flowSrc = this.#stripFlow(src, file, errors.length > 0)
365
- const asFlow = flowSrc === null ? null : cleanParse(flowSrc, baseSourceType)
366
- if (asFlow !== null) {
367
- parsed = asFlow
355
+ const flow = stripFlow(src, file, (text) => cleanParse(text, baseSourceType))
356
+ if (flow.parsed !== null) {
357
+ ({ parsed, source, stripped: strippedSource } = flow)
368
358
  format = declared ?? detectedFormat(parsed)
369
359
  errors = []
370
- strippedSource = flowSrc
360
+ } else if (flow.error && errors.length > 0) {
361
+ console.warn(`[stasis] --flow: could not strip Flow types from ${file}; leaving it to oxc (${flow.error.message})`)
371
362
  }
372
363
  }
373
- return { format, parsed, errors, source: src, strippedSource }
364
+ return { format, parsed, errors, source, strippedSource }
374
365
  }
375
366
 
376
367
  #scanFile(url, queue) {
@@ -32,6 +32,7 @@ export const PACKAGE_REASONS = {
32
32
  'patch-not-locked': 'records a patch its lockfile doesn\'t patch it with',
33
33
  'patch-not-recorded': 'records an integrity and no patch, where its lockfile patches it',
34
34
  'patch-conflict': 'its copies record different patches, or one of them none, where pnpm patches every copy with one',
35
+ 'patch-file-mismatch': 'records its patch in another file than the one it is found in',
35
36
  }
36
37
 
37
38
  // Why a patch wasn't applied, told after `its patch` and its path.
package/src/validate.d.ts CHANGED
@@ -24,7 +24,7 @@ export interface PlanPackage { ecosystem: string; name: string; version: string;
24
24
  export interface ValidationPlan { files: number; own: PlanFile[]; packages: PlanPackage[]; patches: Map<string, string> }
25
25
  // A parsed bundle (Bundle.parse's, or parseFileWithKind's artifact).
26
26
  export interface PlannedBundle {
27
- modules: Map<string, { name?: string; version?: string; ecosystem?: string; integrity?: string; repo?: { github: string; directory?: string; commit?: string }; patch?: { integrity: string }; files: Record<string, unknown> }>
27
+ modules: Map<string, { name?: string; version?: string; ecosystem?: string; integrity?: string; repo?: { github: string; directory?: string; commit?: string }; patch?: { integrity: string; file?: string }; files: Record<string, unknown> }>
28
28
  formats?: Map<string, string>
29
29
  }
30
30
  export function validationPlan(bundle: PlannedBundle, options?: { lockfile?: boolean }): ValidationPlan
package/src/validate.js CHANGED
@@ -18,7 +18,7 @@ import { createVfs } from '@preventive/vfs'
18
18
  import { Lockfile } from '@exodus/stasis-core/lockfile'
19
19
  import { bundledIntegrity, sha512integrity } from '@exodus/stasis-core/state-util'
20
20
 
21
- import { nameAt, STORE_HASH, resolutionCommit } from './installed.js'
21
+ import { isPatchOf, nameAt, patchHashes, STORE_HASH, resolutionCommit } from './installed.js'
22
22
  import { findCargoLock } from './loaders/cargo.js'
23
23
  import { parseFileWithKind } from './parse.js'
24
24
  import { DIM, formatTable, painter } from './table.js'
@@ -78,7 +78,7 @@ export function validationPlan(bundle, { lockfile = false } = {}) {
78
78
  if (lockfile && rel === CARGO_CHECKSUM && ecosystem === 'cargo') continue
79
79
  const file = lockfile ? { path, dir, rel, format, integrity: content } : { path, dir, rel, format, bytes: () => (format === 'resource:base64' ? Buffer.from(content, 'base64') : utf8.encode(content)) }
80
80
  if (ecosystem !== undefined) {
81
- if (!packages.has(key)) packages.set(key, { ecosystem, name: info.name ?? '', version: info.version ?? '', files: [], recorded: new Map(), integrities: new Map(), repos: new Map() })
81
+ if (!packages.has(key)) packages.set(key, { ecosystem, name: info.name ?? '', version: info.version ?? '', files: [], recorded: new Map(), patchFiles: new Map(), integrities: new Map(), repos: new Map() })
82
82
  packages.get(key).files.push(file)
83
83
  } else if (path !== EMPTY_MODULE_PATH || content !== (lockfile ? EMPTY_INTEGRITY : '')) {
84
84
  if (!lockfile && (format === 'patch' || path.endsWith('.patch'))) patches.set(path, content)
@@ -89,6 +89,7 @@ export function validationPlan(bundle, { lockfile = false } = {}) {
89
89
  if (pkg === undefined) continue
90
90
  // In hex, as pnpm's and Cargo's lockfiles spell them.
91
91
  if (info.patch) pkg.recorded.set(dir, integrityHex(info.patch.integrity))
92
+ if (info.patch?.file) pkg.patchFiles.set(dir, info.patch.file)
92
93
  if (info.integrity) pkg.integrities.set(dir, ecosystem === 'cargo' ? integrityHex(info.integrity) : info.integrity)
93
94
  if (info.repo) pkg.repos.set(dir, info.repo)
94
95
  }
@@ -141,9 +142,7 @@ export function fetchFailure(error) {
141
142
  return 'failed'
142
143
  }
143
144
 
144
- // As pnpm hashes a patch: a sha256 in hex of its text with its CRLFs as LFs, or as it is.
145
- const patchHashes = (text) => new Set([text.replaceAll('\r\n', '\n'), text].map(sha256))
146
- export const isPatchOf = (text, hash) => patchHashes(text).has(hash)
145
+ export { isPatchOf }
147
146
 
148
147
  // git quotes a path as C writes a string, bytes past ASCII in octal; null where it is quoted otherwise.
149
148
  const ESCAPES = { a: 7, b: 8, t: 9, n: 10, v: 11, f: 12, r: 13, '"': 34, '\\': 92 }
@@ -398,22 +397,28 @@ function patchesOf(plan) {
398
397
  for (const hash of patchHashes(text)) if (!byHash.has(hash)) byHash.set(hash, held)
399
398
  if (!byName.has(name)) byName.set(name, held)
400
399
  }
401
- return { byHash, byName }
400
+ return { byHash, byName, byPath: plan.patches }
402
401
  }
403
402
 
404
403
  // A bundle's patch is own code: refused where the repository holds it otherwise (`verdicts`).
405
- async function patchOf(held, pkg, files, locked, verdicts, recorded) {
404
+ async function patchOf(held, pkg, files, locked, verdicts, recorded, recordedFile) {
406
405
  const lock = locked?.of(pkg.name, pkg.version)
407
406
  const hash = lock === undefined ? recorded : lock?.hash
408
407
  let patch
409
408
  let text
409
+ // Where it is found, from the bundle root, as a record's `file` names it.
410
+ let file
410
411
  try {
411
- const bundled = hash !== undefined ? held.byHash.get(hash) : lock === undefined && held.byName.get(pnpmPatchName(pkg.name, pkg.version))
412
+ // Of the bundle's of its hash, the one its copies record, where it holds that.
413
+ const own = hash !== undefined && held.byPath.get(recordedFile)
414
+ const bundled = own && isPatchOf(own, hash) ? { path: recordedFile, text: own }
415
+ : hash !== undefined ? held.byHash.get(hash) : lock === undefined && held.byName.get(pnpmPatchName(pkg.name, pkg.version))
412
416
  if (bundled) {
413
417
  patch = { ...(hash !== undefined && { hash }), path: bundled.path, from: 'bundle' }
414
- text = bundled.text
418
+ ;({ text, path: file } = bundled)
415
419
  } else if (lock?.path) {
416
420
  patch = { hash, path: lock.path, from: locked.from }
421
+ file = lock.file
417
422
  text = await locked.read(lock.path)
418
423
  // Held to the hash the lockfile names it by, as the bundle's are found by it.
419
424
  if (!isPatchOf(text, hash)) throw new PatchError('patch-mismatch')
@@ -422,7 +427,7 @@ async function patchOf(held, pkg, files, locked, verdicts, recorded) {
422
427
  const found = lock ? await locked.locate?.(lock) : undefined
423
428
  if (!found) return hash === undefined ? { patched: null } : { patch: { hash, error: 'patch-not-found' }, patched: null }
424
429
  patch = { hash, path: found.path, from: locked.from }
425
- ;({ text } = found)
430
+ ;({ text, file } = found)
426
431
  }
427
432
  let unverified = false
428
433
  if (patch.from === 'bundle') {
@@ -431,7 +436,7 @@ async function patchOf(held, pkg, files, locked, verdicts, recorded) {
431
436
  unverified = verdict !== 'matched'
432
437
  patch.verified = !unverified
433
438
  }
434
- return { patch, patched: patchedFiles(files, parsePatch(text)), unverified }
439
+ return { patch, patched: patchedFiles(files, parsePatch(text)), unverified, file }
435
440
  } catch (error) {
436
441
  return { patch: { ...patch, error: error instanceof PatchError ? error.code : 'patch-unreadable' }, patched: null }
437
442
  }
@@ -527,19 +532,23 @@ export async function validateBundle(plan, own, { fetch = fetchPackage, locked:
527
532
  let published
528
533
  let patch
529
534
  let patched
535
+ let patchFile
530
536
  let unverifiedPatch = false
531
537
  try {
532
538
  if (pinned) candidates = []
533
539
  else if (lock?.from === 'local') candidates = await Promise.all(lock.directories.map((dir) => locked.tree(dir)))
534
540
  else {
535
541
  published = lock?.from === 'git' ? await locked.git(lock.at) : await fetch(pkg.ecosystem, pkg.name, pkg.version, lock?.integrity ?? recordedIntegrity)
536
- ;({ patch, patched, unverified: unverifiedPatch = false } = pkg.ecosystem === 'npm' && recordedPatch !== null ? await patchOf(bundled, pkg, published, locked, verdicts, recordedPatch) : {})
542
+ ;({ patch, patched, unverified: unverifiedPatch = false, file: patchFile } = pkg.ecosystem === 'npm' && recordedPatch !== null ? await patchOf(bundled, pkg, published, locked, verdicts, recordedPatch, dirs.map((dir) => pkg.patchFiles.get(dir)).find(Boolean)) : {})
537
543
  candidates = patched && !unverifiedPatch ? [patched, published] : [published]
538
544
  }
539
545
  } catch (error) {
540
546
  const reason = fetchFailure(error)
541
547
  return reason === 'integrity-mismatch' ? fails(reason) : unverified(reason)
542
548
  }
549
+ // A copy recording where its patch is records where validation finds it.
550
+ const elsewhere = patchFile === undefined ? undefined : dirs.map((dir) => pkg.patchFiles.get(dir)).find((file) => file !== undefined && file !== patchFile)
551
+ if (elsewhere !== undefined) return fails('patch-file-mismatch', { at: elsewhere, patch: { ...patch, recorded: true } })
543
552
  const checksum = (file) => pkg.ecosystem === 'cargo' && file.rel === CARGO_CHECKSUM
544
553
  const matches = (file, bytes, candidate) => (checksum(file) ? checksumOf(bytes, candidate) : candidate.has(file.rel) && isFile(file, candidate.get(file.rel)))
545
554
  // Each installed copy apart, as one may be patched and another not.
@@ -767,7 +776,7 @@ function readLockfile(name, read) {
767
776
  const known = paths.get(hash)
768
777
  paths.set(hash, { path: known?.path ?? path, selectors: [...(known?.selectors ?? []), selector] })
769
778
  }
770
- return { entries, locals, aliases, directoryAliases, patches: { hashes, paths } }
779
+ return { entries, locals, aliases, directoryAliases, patches: { hashes, paths }, importers: new Set(Object.keys(lockfile?.importers ?? {})) }
771
780
  }
772
781
  if (name === 'Cargo.lock') {
773
782
  for (const pkg of Object.values(parseCargoLock(text).packages)) add(pkg.name, pkg.version, lockedFrom(pkg.source?.startsWith('git+'), CRATES_IO.has(pkg.source), pkg.checksum))
@@ -834,9 +843,13 @@ export function lockedBy(lockfiles) {
834
843
  const other = same.find((one) => one.at === at)
835
844
  if (other !== undefined) throw new ValidateError('lockfile-ambiguous', `${other.path} and ${path} both lock ${ecosystem} packages: give one`)
836
845
  const locks = lockOf(path, text)
837
- // npm's locks each location from its directory: the bundle's root from there is one of its importers on disk.
838
- const within = locks.importers === undefined || from !== 'local' || locks.importers.has(root) ? root : ''
839
- const one = { ...locks, path, from, read, tree, git, root: within, at, patchDirs: patches ?? [posix.join(posix.dirname(path), 'patches')] }
846
+ // Given on disk, a lockfile's place in the repository isn't known: the bundle's root from its directory is the
847
+ // longest tail of `root` that is one of its importers, else its own.
848
+ const tails = root.split('/').map((_, start, parts) => parts.slice(start).join('/'))
849
+ const within = locks.importers === undefined || from !== 'local' ? root : tails.find((tail) => locks.importers.has(tail)) ?? ''
850
+ // The bundle's root, in its paths' terms, as a recorded patch's `file` is from there.
851
+ const bundleDir = posix.join(posix.dirname(path), within)
852
+ const one = { ...locks, path, from, read, tree, git, root: within, at, bundleDir, patchDirs: patches ?? [posix.join(posix.dirname(path), 'patches')] }
840
853
  by.set(ecosystem, [...same, one])
841
854
  all.push(one)
842
855
  given.set(one, lockfile)
@@ -884,16 +897,22 @@ export function lockedBy(lockfiles) {
884
897
  if (hash === null) found = null
885
898
  if (hash == null) continue
886
899
  const { path, selectors = [] } = npm.patches.paths.get(hash) ?? {}
887
- if (path) return { hash, path: posix.join(posix.dirname(npm.path), path) }
900
+ const at = path && posix.join(posix.dirname(npm.path), path)
901
+ if (at) return { hash, path: at, file: posix.relative(npm.bundleDir, at) }
888
902
  return { hash, names: [...new Set([...selectors.map((selector) => `${selector.replace('/', '__')}.patch`), pnpmPatchName(name, version)])] }
889
903
  }
890
904
  return found
891
905
  },
892
906
  async locate({ hash, names = [] }) {
893
- for (const path of [...new Set(npms.flatMap((npm) => npm.patchDirs))].flatMap((dir) => names.map((name) => pathIn(dir, name)))) {
894
- // eslint-disable-next-line no-await-in-loop -- the first of them that is there
895
- const text = await Promise.try(() => this.read(path)).catch(() => null)
896
- if (text !== null && isPatchOf(text, hash)) return { path, text }
907
+ const tried = new Set()
908
+ for (const npm of npms) {
909
+ for (const path of npm.patchDirs.flatMap((dir) => names.map((name) => pathIn(dir, name)))) {
910
+ if (tried.has(path)) continue
911
+ tried.add(path)
912
+ // eslint-disable-next-line no-await-in-loop -- the first of them that is there
913
+ const text = await Promise.try(() => this.read(path)).catch(() => null)
914
+ if (text !== null && isPatchOf(text, hash)) return { path, text, file: posix.relative(npm.bundleDir, path) }
915
+ }
897
916
  }
898
917
  return null
899
918
  },
@@ -135,10 +135,28 @@ async function subtreeEntries(client, list, { github, directory, packageManager,
135
135
  return files === null || refersAbove(files, lockfileOf(packageManager)) ? null : files
136
136
  }
137
137
 
138
- async function repoTree(name, { github, sha, tag, directory, path, client, packageManager }, options) {
138
+ // GitHub serves a commit pushed to any fork of a repo through the repo's name too, its tarball and trees alike: one
139
+ // given by its sha is held to the repo's own history, as `stasis validate` holds it (historyOf). A tag's and the
140
+ // default branch's head are the repo's own already.
141
+ async function assertOwnCommit(name, client, github, sha) {
142
+ const { ValidateError, followingMoves, historyOf } = await import('../validate.js')
143
+ const repository = followingMoves(client, github)
144
+ let ref
145
+ try {
146
+ ref = await historyOf(repository, sha)
147
+ } catch (error) {
148
+ if (error instanceof ValidateError) throw new Error(`${name}: ${error.message}`, { cause: error })
149
+ throw error
150
+ }
151
+ if (ref === null) throw new Error(`${name}: ${sha} is in the history of no branch or tag of ${repository.name()} (a fork's, say, which GitHub serves through its name too): give a commit of its own`)
152
+ }
153
+
154
+ // `own`: a given sha is held to the repo's history (assertOwnCommit), before anything else is fetched.
155
+ async function repoTree(name, { github, sha, tag, directory, path, client, packageManager, own = false }, options) {
139
156
  checkAhead(name, packageManager, options)
140
157
  checkRepo(name, { github, sha, tag, directory, path })
141
158
  client ??= createClient({ token: null })
159
+ if (own && sha !== undefined) await debugTimed('Held the commit to the repo\'s history', () => assertOwnCommit(name, client, github, sha))
142
160
  sha ??= tag === undefined ? (await client.getRepoHead({ repo: github })).oid : (await client.getRepoTag({ repo: github, tag })).oid
143
161
  const where = `${name}: ${github}@${sha}`
144
162
  const list = memoized((dir) => client.listRepoDir({ repo: github, sha, directory: dir }))
@@ -181,7 +199,7 @@ async function repoTree(name, { github, sha, tag, directory, path, client, packa
181
199
  export async function buildGitHubBundle({ github, sha, tag, directory, path, client, packageManager, ...options } = {}) {
182
200
  checkTarget('buildGitHubBundle', options)
183
201
  checkCache('buildGitHubBundle', options.cache)
184
- const tree = await debugTimed('Fetched the repo', () => repoTree('buildGitHubBundle', { github, sha, tag, directory, path, client, packageManager }, options))
202
+ const tree = await debugTimed('Fetched the repo', () => repoTree('buildGitHubBundle', { github, sha, tag, directory, path, client, packageManager, own: true }, options))
185
203
  let { entries } = tree
186
204
  if (entries === undefined) {
187
205
  const suggested = defaultEntries('buildGitHubBundle', tree.host, tree.cwd, tree.packageManagers, options)
@@ -565,7 +565,8 @@ const githubRoot = (url) => {
565
565
  export function pinFromTree(bundle, root, tree) {
566
566
  if (tree.trees !== undefined) return new Map(tree.trees.flatMap((one) => [...pinFromTree(bundle, root, one)]))
567
567
  const { root: from, installed, packageManager } = tree
568
- const byDir = new Map(installed.map((pkg) => [posix.relative(root, posix.join(from, pkg.path)), pkg]))
568
+ const fromRoot = (path) => posix.relative(root, posix.join(from, path))
569
+ const byDir = new Map(installed.map((pkg) => [fromRoot(pkg.path), pkg]))
569
570
  const integrities = tree.integrities?.()
570
571
  const locked = ({ integrity, checksum }) => (PACKAGE_MANAGERS[packageManager].kind !== 'js' ? archiveIntegrity('soldeer', checksum) : (integrities?.has(tarballIntegrityOf(integrity)) ?? true) ? integrity : undefined)
571
572
  const accounts = new Map()
@@ -574,6 +575,8 @@ export function pinFromTree(bundle, root, tree) {
574
575
  if (pkg === undefined) continue
575
576
  const at = isValidRepoField('commit', pkg.commit) ? info.repo ?? githubRoot(pkg.git) : undefined
576
577
  const pinned = installedMetadata(dir, info.ecosystem, { repo: at && { ...at, commit: pkg.commit }, integrity: locked(pkg), patchHash: pkg.patch?.hash })
578
+ // deptree read it at the path pnpm's settings name, from the lockfile's directory, and held it to its hash.
579
+ if (pinned.patch) pinned.patch.file = fromRoot(pkg.patch.path)
577
580
  bundle.modules.set(dir, { ...info, ...pinned })
578
581
  accounts.set(dir, { integrity: pinned.integrity, patch: pinned.patch })
579
582
  }