(moduleDir: string)
| 30 | * Returns `null` if no `go.mod` exists there or it has no `module` directive. |
| 31 | */ |
| 32 | export 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 |
no test coverage detected