multi-repo-workspace.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * Multi-repo workspaces (#514): a directory holding several independent git
  3. * repositories must index as a whole.
  4. *
  5. * Two enumeration paths are exercised:
  6. * - git path: the workspace root is itself a git repo (a "super-repo") whose
  7. * `.gitignore` hides the child repos to keep `git status` quiet. git never
  8. * lists ignored dirs, so the embedded repos were invisible (0 files). They
  9. * are now discovered via the ignored-directories listing and enumerated by
  10. * their own `git ls-files`. (#193 covered the *untracked* embedded case.)
  11. * - sync path: `git status` in the parent says nothing about embedded repos;
  12. * change detection now recurses into them.
  13. *
  14. * The non-git-parent case (plain folder of repos) already worked via the
  15. * filesystem walk — locked in here so it stays that way.
  16. */
  17. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  18. import * as fs from 'fs';
  19. import * as path from 'path';
  20. import * as os from 'os';
  21. import { execFileSync } from 'child_process';
  22. import CodeGraph from '../src/index';
  23. import { scanDirectory, buildScopeIgnore, discoverEmbeddedRepoRoots } from '../src/extraction';
  24. function git(cwd: string, ...args: string[]): void {
  25. execFileSync('git', args, { cwd, stdio: ['ignore', 'ignore', 'ignore'] });
  26. }
  27. /** git init + commit everything currently in `dir` as one repo. */
  28. function makeRepo(dir: string): void {
  29. git(dir, 'init', '-q');
  30. git(dir, 'add', '-A');
  31. git(dir, '-c', 'user.email=t@t', '-c', 'user.name=t', 'commit', '-qm', 'init', '--allow-empty');
  32. }
  33. function write(file: string, content: string): void {
  34. fs.mkdirSync(path.dirname(file), { recursive: true });
  35. fs.writeFileSync(file, content);
  36. }
  37. describe('multi-repo workspaces (#514)', () => {
  38. let ws: string;
  39. beforeEach(() => {
  40. ws = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-multirepo-'));
  41. });
  42. afterEach(() => {
  43. fs.rmSync(ws, { recursive: true, force: true });
  44. });
  45. it('indexes embedded repos hidden by the super-repo .gitignore', () => {
  46. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() { return 1; }\n');
  47. write(path.join(ws, 'packages/proj-b/src/billing.ts'), 'export function charge() { return 2; }\n');
  48. makeRepo(path.join(ws, 'packages/proj-a'));
  49. makeRepo(path.join(ws, 'packages/proj-b'));
  50. write(path.join(ws, '.gitignore'), '/packages/\n');
  51. write(path.join(ws, 'tools.ts'), 'export function tool() { return 0; }\n');
  52. makeRepo(ws);
  53. const files = scanDirectory(ws);
  54. expect(files).toContain('packages/proj-a/src/auth.ts');
  55. expect(files).toContain('packages/proj-b/src/billing.ts');
  56. expect(files).toContain('tools.ts'); // the parent's own tracked code still indexes
  57. });
  58. it('keeps respecting the parent .gitignore for the parent own (non-repo) dirs', () => {
  59. write(path.join(ws, 'scratch/junk.ts'), 'export function junk() { return 9; }\n');
  60. write(path.join(ws, 'src/app.ts'), 'export function app() { return 1; }\n');
  61. write(path.join(ws, '.gitignore'), '/scratch/\n');
  62. makeRepo(ws);
  63. const files = scanDirectory(ws);
  64. expect(files).toContain('src/app.ts');
  65. // scratch/ is gitignored and contains NO embedded repo — stays excluded.
  66. expect(files.some((f) => f.startsWith('scratch/'))).toBe(false);
  67. });
  68. it('never descends into git repos inside node_modules (npm git-dependencies)', () => {
  69. // Embedded repo first (clean), node_modules dropped in afterwards —
  70. // matching reality, where node_modules is never committed.
  71. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() {}\n');
  72. makeRepo(path.join(ws, 'packages/proj-a'));
  73. write(path.join(ws, 'packages/proj-a/node_modules/inner/src/evil2.ts'), 'export function evil2() {}\n');
  74. makeRepo(path.join(ws, 'packages/proj-a/node_modules/inner')); // npm git-dep: has commits
  75. // Workspace-level git-dep too.
  76. write(path.join(ws, 'node_modules/git-dep/src/evil.ts'), 'export function evil() {}\n');
  77. makeRepo(path.join(ws, 'node_modules/git-dep'));
  78. write(path.join(ws, '.gitignore'), '/packages/\nnode_modules\n');
  79. makeRepo(ws);
  80. const files = scanDirectory(ws);
  81. expect(files).toContain('packages/proj-a/src/auth.ts');
  82. expect(files.some((f) => f.includes('node_modules'))).toBe(false);
  83. });
  84. it('still indexes UNTRACKED embedded repos (#193 regression)', () => {
  85. write(path.join(ws, 'vendor-src/lib/src/util.ts'), 'export function util() {}\n');
  86. makeRepo(path.join(ws, 'vendor-src/lib'));
  87. write(path.join(ws, 'main.ts'), 'export function main() {}\n');
  88. makeRepo(ws); // vendor-src/ is untracked (not ignored) — committed ws has only main.ts + nothing else
  89. // NOTE: makeRepo committed vendor-src too via add -A… recreate untracked state:
  90. git(ws, 'rm', '-r', '--cached', '-q', 'vendor-src');
  91. git(ws, '-c', 'user.email=t@t', '-c', 'user.name=t', 'commit', '-qm', 'untrack');
  92. const files = scanDirectory(ws);
  93. expect(files).toContain('vendor-src/lib/src/util.ts');
  94. expect(files).toContain('main.ts');
  95. });
  96. it('non-git workspace: walks children and respects each child own .gitignore', () => {
  97. write(path.join(ws, 'proj-a/src/auth.ts'), 'export function login() {}\n');
  98. write(path.join(ws, 'proj-a/build/out.ts'), 'export function generated() {}\n');
  99. write(path.join(ws, 'proj-a/.gitignore'), 'build/\n');
  100. write(path.join(ws, 'proj-b/src/billing.ts'), 'export function charge() {}\n');
  101. makeRepo(path.join(ws, 'proj-a'));
  102. makeRepo(path.join(ws, 'proj-b'));
  103. // ws itself is NOT a git repo.
  104. const files = scanDirectory(ws);
  105. expect(files).toContain('proj-a/src/auth.ts');
  106. expect(files).toContain('proj-b/src/billing.ts');
  107. expect(files.some((f) => f.includes('build/'))).toBe(false);
  108. });
  109. it('does not search beyond the embedded-repo depth cap', () => {
  110. // Repo buried 5 levels under the ignored dir — past EMBEDDED_REPO_SEARCH_DEPTH (4).
  111. const deep = path.join(ws, 'pkgs/a/b/c/d/e');
  112. write(path.join(deep, 'src/deep.ts'), 'export function deep() {}\n');
  113. makeRepo(deep);
  114. write(path.join(ws, 'main.ts'), 'export function main() {}\n');
  115. write(path.join(ws, '.gitignore'), '/pkgs/\n');
  116. makeRepo(ws);
  117. const files = scanDirectory(ws);
  118. expect(files).toContain('main.ts');
  119. expect(files.some((f) => f.includes('deep.ts'))).toBe(false);
  120. });
  121. it('discovers embedded roots (ignored + untracked kinds); none for non-git roots', () => {
  122. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() {}\n');
  123. makeRepo(path.join(ws, 'packages/proj-a'));
  124. write(path.join(ws, 'vendor-src/lib/util.ts'), 'export function util() {}\n');
  125. makeRepo(path.join(ws, 'vendor-src/lib'));
  126. write(path.join(ws, '.gitignore'), '/packages/\n'); // vendor-src stays untracked
  127. makeRepo(ws);
  128. git(ws, 'rm', '-r', '--cached', '-q', 'vendor-src');
  129. git(ws, '-c', 'user.email=t@t', '-c', 'user.name=t', 'commit', '-qm', 'untrack');
  130. const roots = discoverEmbeddedRepoRoots(ws);
  131. expect(roots).toContain('packages/proj-a/');
  132. expect(roots).toContain('vendor-src/lib/');
  133. const plain = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-nongit-'));
  134. try {
  135. expect(discoverEmbeddedRepoRoots(plain)).toEqual([]);
  136. } finally {
  137. fs.rmSync(plain, { recursive: true, force: true });
  138. }
  139. });
  140. it('ScopeIgnore: embedded files use the child rules; the watcher can descend to them', () => {
  141. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() {}\n');
  142. write(path.join(ws, 'packages/proj-a/.gitignore'), 'build/\n');
  143. makeRepo(path.join(ws, 'packages/proj-a'));
  144. write(path.join(ws, '.gitignore'), '/packages/\n');
  145. makeRepo(ws);
  146. const scope = buildScopeIgnore(ws);
  147. // Inside the embedded repo: the CHILD's rules decide.
  148. expect(scope.ignores('packages/proj-a/src/auth.ts')).toBe(false);
  149. expect(scope.ignores('packages/proj-a/build/out.ts')).toBe(true);
  150. // Under the ignored dir but NOT in any embedded repo: parent rules apply.
  151. expect(scope.ignores('packages/stray.ts')).toBe(true);
  152. // Directory form: ancestors of an embedded root are never pruned —
  153. // the Linux per-directory watcher must descend through `packages/`.
  154. expect(scope.ignores('packages/')).toBe(false);
  155. // Ordinary paths: unchanged semantics.
  156. expect(scope.ignores('node_modules/dep/index.ts')).toBe(true);
  157. expect(scope.ignores('src/app.ts')).toBe(false);
  158. });
  159. it('sync picks up a change inside a gitignored embedded repo', async () => {
  160. write(path.join(ws, 'packages/proj-a/src/auth.ts'), 'export function login() { return 1; }\n');
  161. makeRepo(path.join(ws, 'packages/proj-a'));
  162. write(path.join(ws, '.gitignore'), '/packages/\n');
  163. makeRepo(ws);
  164. const cg = CodeGraph.initSync(ws, { config: { include: ['**/*.ts'], exclude: [] } });
  165. try {
  166. await cg.indexAll();
  167. expect(cg.searchNodes('login', { limit: 5 }).length).toBeGreaterThan(0);
  168. // Change inside the embedded repo — invisible to the parent's `git status`.
  169. write(path.join(ws, 'packages/proj-a/src/auth.ts'),
  170. 'export function login() { return 1; }\nexport function logout() { return 0; }\n');
  171. await cg.sync();
  172. expect(cg.searchNodes('logout', { limit: 5 }).length).toBeGreaterThan(0);
  173. } finally {
  174. cg.destroy();
  175. }
  176. });
  177. });