verify-package-paths.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Find stale root-relative `packages/...` references in repo-authored prose and
  3. * TypeScript. A missing path is reported only when it names a real package leaf
  4. * outside its own explaining group directory; globs, placeholders, hypothetical
  5. * packages, and unbuilt `lib/` output are outside the check.
  6. */
  7. import { existsSync, globSync } from 'node:fs'
  8. import { resolve } from 'node:path'
  9. import {
  10. findReferenceViolations,
  11. isArchivedAgentNotePath,
  12. uniqueRepoFiles,
  13. type ReferenceViolation as Violation,
  14. } from './repo-files.ts'
  15. const root = resolve(import.meta.dirname, '..')
  16. /** Markdown + repo-authored TypeScript that may cite package paths. */
  17. const PATTERNS = [
  18. 'README.md',
  19. '.agents/notes/**/*.md',
  20. 'docs/**/*.md',
  21. 'packages/*/*.md',
  22. 'packages/*/*/*.md',
  23. 'AGENTS.md',
  24. 'packages/AGENTS.md',
  25. 'packages/**/*.ts',
  26. ]
  27. /** Paths excluded from the scan: built output and vendored upstream source. */
  28. const isExcluded = (p: string): boolean =>
  29. isArchivedAgentNotePath(p) || p.includes('/lib/') || p.endsWith('.d.ts') || p.startsWith('vendor/')
  30. /**
  31. * Directory names of every real package, `packages/<group>/<pkg>`. A broken
  32. * reference is only flagged when one of its segments is in this set — that is
  33. * what scopes the gate to DRIFT (a moved real package) rather than typos or
  34. * not-yet-existing packages named in a proposal.
  35. */
  36. function realPackageNames(): Set<string> {
  37. const names = new Set<string>()
  38. for (const pkg of globSync('packages/*/*', { cwd: root, withFileTypes: true })) {
  39. if (pkg.isDirectory()) names.add(pkg.name)
  40. }
  41. return names
  42. }
  43. const packageNames = realPackageNames()
  44. /**
  45. * Match a `packages/<path>` reference token. The character class is plain path
  46. * characters only, so a glob (`*`), placeholder (`<`, `>`), or brace expansion
  47. * (`{`, `}`, `,`) terminates the match before those chars and is never probed —
  48. * those are patterns, not real paths. A trailing `.`/`/` (e.g. a sentence-ending
  49. * period) is trimmed before the existence check.
  50. */
  51. const PKG_REF = /\bpackages\/[A-Za-z0-9._/-]+/g
  52. function isDriftedPackageReference(ref: string): boolean {
  53. if (existsSync(resolve(root, ref))) return false
  54. // Ignore unbuilt `lib/` paths only under an existing depth-two package root:
  55. // CI runs this gate before build, while stale group-less paths must still fail.
  56. const parts = ref.split('/')
  57. const libAt = parts.indexOf('lib')
  58. if (libAt === 3 && existsSync(resolve(root, parts.slice(0, 3).join('/')))) return false
  59. // A missing reference is drift only when a path segment names a live package.
  60. // A leading segment that is itself an existing group directory is explained by
  61. // the group, not by a relocated leaf sharing its name (`client` is both the
  62. // client-modules group and the sdk leaf), so only later segments count.
  63. const segments = ref.split('/').slice(1)
  64. const [group] = segments
  65. const scanned = group !== undefined && segments.length > 1 && existsSync(resolve(root, 'packages', group))
  66. ? segments.slice(1)
  67. : segments
  68. return scanned.some(segment => packageNames.has(segment))
  69. }
  70. /** Find missing package references whose path names a live package; bare paths, typos, and illustrative skeletons do not count. */
  71. function findViolations(absPath: string): Violation[] {
  72. return findReferenceViolations(
  73. root,
  74. absPath,
  75. PKG_REF,
  76. // Remove trailing separators or sentence punctuation matched greedily.
  77. ref => ref.replace(/[./]+$/, ''),
  78. isDriftedPackageReference,
  79. )
  80. }
  81. const files = uniqueRepoFiles(root, PATTERNS, isExcluded)
  82. const all = files.flatMap(file => findViolations(file.real))
  83. const checked = files.length
  84. if (all.length === 0) {
  85. console.log(`verify-package-paths: ${checked} file(s) checked, all packages/* references resolve.`)
  86. process.exit(0)
  87. }
  88. console.error('verify-package-paths: broken packages/* references found (target does not exist):')
  89. for (const v of all) {
  90. console.error(` ${v.file}:${v.line} ${v.ref}`)
  91. }
  92. process.exit(1)