From f0f316a7b2a4acc4547dacdd2679b293048e53ff Mon Sep 17 00:00:00 2001 From: Eva Date: Tue, 14 Jul 2026 12:15:54 +0700 Subject: [PATCH] fix(scan): canonicalize traversal without order-sensitive Rust binding --- .../src/core/ingestion/filesystem-walker.ts | 5 +++ .../ingestion/languages/rust/range-binding.ts | 9 ++++- .../test/unit/filesystem-walker-order.test.ts | 39 +++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 gitnexus/test/unit/filesystem-walker-order.test.ts diff --git a/gitnexus/src/core/ingestion/filesystem-walker.ts b/gitnexus/src/core/ingestion/filesystem-walker.ts index 0af28a958..823b3670f 100644 --- a/gitnexus/src/core/ingestion/filesystem-walker.ts +++ b/gitnexus/src/core/ingestion/filesystem-walker.ts @@ -83,6 +83,11 @@ export const walkRepositoryPaths = async ( } } + // Filesystem/glob traversal order is not stable across filesystems or repeated + // scans. Canonicalize once at the scan boundary so every downstream phase sees + // the same repository order. + entries.sort((left, right) => (left.path < right.path ? -1 : left.path > right.path ? 1 : 0)); + if (skippedLarge > 0) { const isDefault = maxFileSizeBytes === DEFAULT_MAX_FILE_SIZE_BYTES; const isOverrideUnset = !process.env.GITNEXUS_MAX_FILE_SIZE; diff --git a/gitnexus/src/core/ingestion/languages/rust/range-binding.ts b/gitnexus/src/core/ingestion/languages/rust/range-binding.ts index 285b84d52..4c7224333 100644 --- a/gitnexus/src/core/ingestion/languages/rust/range-binding.ts +++ b/gitnexus/src/core/ingestion/languages/rust/range-binding.ts @@ -89,6 +89,13 @@ export function populateRustRangeBindings( } } } + + // Publish per-type member bindings for the whole workspace before resolving + // assignments. Otherwise an importer processed before its defining file can + // miss a field or identity-method type solely because of file order. + const scopeMap = new Map(parsed.scopes.map((scope) => [scope.id, scope])); + processFieldTypeBindings(tree.rootNode, parsed, scopeMap); + processIdentityMethodBindings(parsed); } for (const parsed of parsedFiles) { @@ -122,8 +129,6 @@ export function populateRustRangeBindings( const moduleScope = parsed.scopes.find((s) => s.kind === 'Module'); if (moduleScope === undefined) continue; - processFieldTypeBindings(tree.rootNode, parsed, scopeMap); - processIdentityMethodBindings(parsed); processForLoops(tree.rootNode, parsed, scopeMap, moduleScope, allReturnTypes); processPatternBindings(tree.rootNode, parsed, scopeMap, moduleScope); processStructDestructuring(tree.rootNode, parsed, scopeMap, moduleScope, allFieldTypes); diff --git a/gitnexus/test/unit/filesystem-walker-order.test.ts b/gitnexus/test/unit/filesystem-walker-order.test.ts new file mode 100644 index 000000000..7b75128a4 --- /dev/null +++ b/gitnexus/test/unit/filesystem-walker-order.test.ts @@ -0,0 +1,39 @@ +import fs from 'node:fs/promises'; +import os from 'node:os'; +import path from 'node:path'; + +import { glob } from 'glob'; +import { afterEach, describe, expect, it, vi } from 'vitest'; + +vi.mock('glob', () => ({ glob: vi.fn() })); +vi.mock('../../src/config/ignore-service.js', () => ({ + createIgnoreFilter: vi.fn(async () => []), +})); + +import { walkRepositoryPaths } from '../../src/core/ingestion/filesystem-walker.js'; + +const temporaryRoots: string[] = []; + +afterEach(async () => { + vi.mocked(glob).mockReset(); + await Promise.all( + temporaryRoots.splice(0).map((root) => fs.rm(root, { recursive: true, force: true })), + ); +}); + +describe('walkRepositoryPaths ordering', () => { + it('returns accepted files in canonical path order when glob order is unstable', async () => { + const root = await fs.mkdtemp(path.join(os.tmpdir(), 'gitnexus-scan-order-')); + temporaryRoots.push(root); + await Promise.all( + ['zeta.ts', 'alpha.ts', 'middle.ts'].map((file) => + fs.writeFile(path.join(root, file), `export const ${file[0]} = true;\n`), + ), + ); + vi.mocked(glob).mockResolvedValue(['zeta.ts', 'alpha.ts', 'middle.ts']); + + const result = await walkRepositoryPaths(root); + + expect(result.map((entry) => entry.path)).toEqual(['alpha.ts', 'middle.ts', 'zeta.ts']); + }); +});