| 28 | * a follow-up if a real repro shows up. |
| 29 | */ |
| 30 | export function loadGoModule(projectRoot: string): GoModule | null { |
| 31 | const goModPath = path.join(projectRoot, 'go.mod'); |
| 32 | let content: string; |
| 33 | try { |
| 34 | content = fs.readFileSync(goModPath, 'utf-8'); |
| 35 | } catch { |
| 36 | return null; |
| 37 | } |
| 38 | // `module <path>` is the first non-comment directive in any valid go.mod. |
| 39 | // Strip line comments so a `// module foo` doesn't false-match. |
| 40 | const stripped = content.replace(/\/\/[^\n]*/g, ''); |
| 41 | const match = stripped.match(/^\s*module\s+(\S+)\s*$/m); |
| 42 | if (!match) return null; |
| 43 | // Strip optional quoting around the module path. |
| 44 | const modulePath = match[1]!.replace(/^["']|["']$/g, ''); |
| 45 | if (!modulePath) return null; |
| 46 | return { modulePath, rootDir: projectRoot }; |
| 47 | } |