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

Function loadGoModule

src/resolution/go-module.ts:32–49  ·  view source on GitHub ↗
(moduleDir: string)

Source from the content-addressed store, hash-verified

30 * Returns `null` if no `go.mod` exists there or it has no `module` directive.
31 */
32export function loadGoModule(moduleDir: string): GoModule | null {
33 const goModPath = path.join(moduleDir, 'go.mod');
34 let content: string;
35 try {
36 content = fs.readFileSync(goModPath, 'utf-8');
37 } catch {
38 return null;
39 }
40 // `module <path>` is the first non-comment directive in any valid go.mod.
41 // Strip line comments so a `// module foo` doesn't false-match.
42 const stripped = content.replace(/\/\/[^\n]*/g, '');
43 const match = stripped.match(/^\s*module\s+(\S+)\s*$/m);
44 if (!match) return null;
45 // Strip optional quoting around the module path.
46 const modulePath = match[1]!.replace(/^["']|["']$/g, '');
47 if (!modulePath) return null;
48 return { modulePath, rootDir: moduleDir };
49}
50
51/**
52 * The module an import path belongs to: the one whose module path equals it

Callers 2

moduleConfigDigestFunction · 0.90
nearestGoModuleMethod · 0.90

Calls 1

joinMethod · 0.45

Tested by

no test coverage detected