Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions scripts/lib/project-detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,23 @@ function getGoDeps(projectDir) {
if (!fs.existsSync(modPath)) return [];
const content = fs.readFileSync(modPath, 'utf8');
const deps = [];
const requireBlock = content.match(/require\s*\(([\s\S]*?)\)/);
if (requireBlock) {
requireBlock[1].split('\n').forEach(line => {
const trimmed = line.trim();
if (trimmed && !trimmed.startsWith('//')) {
const parts = trimmed.split(/\s+/);
if (parts[0]) deps.push(parts[0]);
}
});
let inRequireBlock = false;
for (const rawLine of content.split(/\r?\n/)) {
const line = rawLine.split('//')[0].trim();
if (!line) continue;
if (line === ')' && inRequireBlock) {
inRequireBlock = false;
continue;
}
const directive = line.match(/^require(?:\s+|(?=\())(.+)$/);
if (directive?.[1] === '(') {
inRequireBlock = true;
continue;
}
if (!directive && !inRequireBlock) continue;
const requirement = (directive ? directive[1] : line)
.match(/^(?:"([^"]+)"|`([^`]+)`|(\S+))\s+\S+/);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if (requirement) deps.push(requirement[1] || requirement[2] || requirement[3]);
}
return deps;
} catch {
Expand Down
89 changes: 89 additions & 0 deletions tests/lib/project-detect-go-requires.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';

const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { test } = require('node:test');
const { detectProjectType, getGoDeps } = require('../../scripts/lib/project-detect');

function withGoMod(contents, check) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'ecc-go-requires-'));
try {
fs.writeFileSync(path.join(dir, 'go.mod'), contents);
check(dir);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
}

test('detects a framework declared by a standalone require directive', () => {
withGoMod('module example.com/app\n\ngo 1.23\nrequire github.com/gin-gonic/gin v1.10.0\n', dir => {
assert.deepEqual(getGoDeps(dir), ['github.com/gin-gonic/gin']);
assert.equal(detectProjectType(dir).primary, 'gin');
});
});

test('reads every require block and standalone directive in source order', () => {
withGoMod([
'module example.com/app',
'require (',
' example.com/first v1.0.0 // indirect',
')',
'require github.com/labstack/echo/v4 v4.13.0',
'require ( // a second dependency group',
' github.com/gin-gonic/gin v1.10.0',
')',
].join('\n'), dir => {
assert.deepEqual(getGoDeps(dir), [
'example.com/first', 'github.com/labstack/echo/v4', 'github.com/gin-gonic/gin',
]);
assert.deepEqual(detectProjectType(dir).frameworks, ['gin', 'echo']);
});
});

test('ignores commented and replacement-only framework references', () => {
withGoMod([
'module example.com/app',
'// require github.com/gin-gonic/gin v1.10.0',
'replace github.com/gin-gonic/gin => ../gin',
'exclude github.com/labstack/echo/v4 v4.12.0',
'require (',
' // github.com/gin-gonic/gin v1.10.0',
' example.com/actual v1.0.0',
')',
].join('\n'), dir => {
assert.deepEqual(getGoDeps(dir), ['example.com/actual']);
assert.deepEqual(detectProjectType(dir).frameworks, []);
});
});

test('accepts quoted module paths and CRLF require directives', () => {
withGoMod('module example.com/app\r\nrequire "github.com/gin-gonic/gin" v1.10.0\r\n', dir => {
assert.deepEqual(getGoDeps(dir), ['github.com/gin-gonic/gin']);
});
});

test('accepts quoted versions in standalone and grouped requirements', () => {
withGoMod([
'module example.com/app',
'require "github.com/gin-gonic/gin" "v1.10.0"',
'require (',
' github.com/labstack/echo/v4 "v4.13.0"',
')',
].join('\n'), dir => {
assert.deepEqual(getGoDeps(dir), ['github.com/gin-gonic/gin', 'github.com/labstack/echo/v4']);
});
});

test('accepts adjacent block punctuation and raw string tokens', () => {
withGoMod([
'module example.com/app',
'require(',
' github.com/gin-gonic/gin `v1.10.0`',
')',
'require `github.com/labstack/echo/v4` "v4.13.0"',
].join('\n'), dir => {
assert.deepEqual(getGoDeps(dir), ['github.com/gin-gonic/gin', 'github.com/labstack/echo/v4']);
});
});