3303-find-nested-gitignore.test.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  5. import { createFindToolDefinition } from "../../../src/core/tools/find.ts";
  6. /**
  7. * Regression test for https://github.com/earendil-works/pi-mono/issues/3303
  8. *
  9. * The `find` tool previously collected every `.gitignore` under the search
  10. * path and passed them to `fd` via `--ignore-file`. fd treats `--ignore-file`
  11. * entries as a single global ignore source, so rules from `a/.gitignore`
  12. * also filtered files under sibling `b/`. The fix switches to fd's
  13. * hierarchical `.gitignore` handling via `--no-require-git` and drops the
  14. * manual collection.
  15. */
  16. describe("issue #3303 nested .gitignore rules leak into sibling directories", () => {
  17. let tempRoot: string;
  18. async function runFind(pattern: string): Promise<string[]> {
  19. const def = createFindToolDefinition(tempRoot);
  20. const ctx = {} as Parameters<typeof def.execute>[4];
  21. const result = (await def.execute("call-1", { pattern }, undefined, undefined, ctx)) as {
  22. content: Array<{ type: string; text?: string }>;
  23. };
  24. const text = result.content[0]?.text ?? "";
  25. if (text === "No files found matching pattern") return [];
  26. return text
  27. .split("\n")
  28. .map((l) => l.trim())
  29. .filter((l) => l.length > 0 && !l.startsWith("["))
  30. .sort();
  31. }
  32. afterEach(() => {
  33. if (tempRoot) rmSync(tempRoot, { recursive: true, force: true });
  34. });
  35. describe("flat sibling case", () => {
  36. beforeEach(() => {
  37. tempRoot = mkdtempSync(join(tmpdir(), "pi-3303-flat-"));
  38. mkdirSync(join(tempRoot, "a"), { recursive: true });
  39. mkdirSync(join(tempRoot, "b"), { recursive: true });
  40. writeFileSync(join(tempRoot, "a", ".gitignore"), "ignored.txt\n");
  41. writeFileSync(join(tempRoot, "a", "ignored.txt"), "");
  42. writeFileSync(join(tempRoot, "a", "kept.txt"), "");
  43. writeFileSync(join(tempRoot, "b", "ignored.txt"), "");
  44. writeFileSync(join(tempRoot, "b", "kept.txt"), "");
  45. writeFileSync(join(tempRoot, "root.txt"), "");
  46. });
  47. it("applies a/.gitignore only inside a/ and leaves b/ untouched", async () => {
  48. const files = await runFind("**/*.txt");
  49. expect(files).toEqual(["a/kept.txt", "b/ignored.txt", "b/kept.txt", "root.txt"]);
  50. });
  51. });
  52. describe("deeply nested case", () => {
  53. beforeEach(() => {
  54. tempRoot = mkdtempSync(join(tmpdir(), "pi-3303-deep-"));
  55. mkdirSync(join(tempRoot, "a", "deep"), { recursive: true });
  56. mkdirSync(join(tempRoot, "b"), { recursive: true });
  57. writeFileSync(join(tempRoot, "a", ".gitignore"), "ignored.txt\n");
  58. writeFileSync(join(tempRoot, "a", "deep", ".gitignore"), "secret.txt\n");
  59. writeFileSync(join(tempRoot, "a", "ignored.txt"), "");
  60. writeFileSync(join(tempRoot, "a", "kept.txt"), "");
  61. writeFileSync(join(tempRoot, "a", "deep", "ignored.txt"), "");
  62. writeFileSync(join(tempRoot, "a", "deep", "secret.txt"), "");
  63. writeFileSync(join(tempRoot, "a", "deep", "kept.txt"), "");
  64. writeFileSync(join(tempRoot, "b", "ignored.txt"), "");
  65. writeFileSync(join(tempRoot, "b", "kept.txt"), "");
  66. writeFileSync(join(tempRoot, "root.txt"), "");
  67. });
  68. it("scopes each .gitignore to its own subtree", async () => {
  69. const files = await runFind("**/*.txt");
  70. // a/.gitignore ignores 'ignored.txt' within a/ and a/deep/.
  71. // a/deep/.gitignore additionally ignores 'secret.txt' within a/deep/.
  72. // b/ is untouched by either.
  73. expect(files).toEqual(["a/deep/kept.txt", "a/kept.txt", "b/ignored.txt", "b/kept.txt", "root.txt"]);
  74. });
  75. });
  76. });