MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / resolveGoCrossPackageReference

Function resolveGoCrossPackageReference

src/resolution/import-resolver.ts:2030–2078  ·  view source on GitHub ↗

* Resolve a Go cross-package qualified reference (`pkga.FuncX`) by matching * the package alias against an in-module import, stripping the module prefix * to a project-relative directory, and locating the exported symbol in any * `.go` file under that directory. Returns `null` for stdlib / third-

(
  ref: UnresolvedRef,
  imports: ImportMapping[],
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

2028 * can still try the file-based path.
2029 */
2030function resolveGoCrossPackageReference(
2031 ref: UnresolvedRef,
2032 imports: ImportMapping[],
2033 context: ResolutionContext
2034): ResolvedRef | null {
2035 const mod = context.getGoModule?.();
2036 if (!mod) return null;
2037
2038 // Qualified call: receiver before `.`, member after. A bare reference
2039 // (no dot) is a same-file/in-package call — handled elsewhere.
2040 const dotIdx = ref.referenceName.indexOf('.');
2041 if (dotIdx <= 0) return null;
2042 const receiver = ref.referenceName.substring(0, dotIdx);
2043 const memberName = ref.referenceName.substring(dotIdx + 1);
2044 if (!memberName) return null;
2045
2046 for (const imp of imports) {
2047 if (imp.localName !== receiver) continue;
2048 // Only in-module imports map to a known directory.
2049 if (imp.source !== mod.modulePath && !imp.source.startsWith(mod.modulePath + '/')) {
2050 continue;
2051 }
2052 const pkgDir = imp.source === mod.modulePath
2053 ? ''
2054 : imp.source.substring(mod.modulePath.length + 1);
2055
2056 // Look up the member by name and pick the candidate whose file lives
2057 // directly in the package directory. Match the immediate parent dir
2058 // exactly so a call to `pkga.FuncX` doesn't accidentally land on a
2059 // `FuncX` declared in `pkga/subpkg/`.
2060 const candidates = context.getNodesByName(memberName);
2061 for (const node of candidates) {
2062 if (node.language !== 'go') continue;
2063 if (!node.isExported) continue;
2064 const fp = node.filePath.replace(/\\/g, '/');
2065 const lastSlash = fp.lastIndexOf('/');
2066 const fileDir = lastSlash >= 0 ? fp.substring(0, lastSlash) : '';
2067 if (fileDir === pkgDir) {
2068 return {
2069 original: ref,
2070 targetNodeId: node.id,
2071 confidence: 0.9,
2072 resolvedBy: 'import',
2073 };
2074 }
2075 }
2076 }
2077 return null;
2078}
2079
2080/** Recursive depth cap for re-export chain following. Real codebases
2081 * rarely chain barrels more than 2–3 deep; 8 is a generous safety

Callers 1

resolveViaImportFunction · 0.85

Calls 2

getGoModuleMethod · 0.80
getNodesByNameMethod · 0.65

Tested by

no test coverage detected