[BUG] parse_gomod crashes with AttributeError on comments or blank lines inside require/exclude blocks

Offen
#5,489 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@muskan-creates352 arbeitet bereits daran.

Seit 15.11.2026.

  • #5490 von @muskan-creates352 — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
backend

Rechercherichtung

Start with src/packagedcode/go_mod.py, where the issue identifies the failing require and exclude parsing paths, and inspect related Go module parser tests. Reproduce with comments or blank lines in both block types, then confirm parsing completes and dependencies are extracted without an AttributeError. Pull request #5490 is already open against this issue.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Problem Description

parse_gomod in src/packagedcode/go_mod.py raises AttributeError: 'NoneType' object has no attribute 'group' whenever a go.mod file contains comments (e.g., // indirect) or blank lines inside a multiline require (...) or exclude (...) block.

When running a scan via the CLI (scancode --package), this unhandled exception causes ScanCode to fail silently on the go.mod file, resulting in 0 detected packages and missing all Go dependencies.

Steps to Reproduce
  1. Create a minimal go.mod file containing comments or an empty line in a require block (typical output from go mod tidy):
module example.com/hello

go 1.21

require (
    // Direct dependencies
    github.com/stretchr/testify v1.8.4

    // Indirect dependencies
    github.com/davecgh/go-spew v1.1.1
)
  1. Run package scan:
scancode --package --json-pp scan.json go.mod
  1. Notice that scan.json reports packages: [] (empty list, no dependencies extracted).

  2. Calling parse_gomod or GoModHandler.parse directly reveals the traceback:

Traceback (most recent call last):
  File "src/packagedcode/golang.py", line 53, in parse
    gomods = go_mod.parse_gomod(location)
  File "src/packagedcode/go_mod.py", line 133, in parse_gomod
    ns_name = parsed_dep_link.group('ns_name')
AttributeError: 'NoneType' object has no attribute 'group'
Root Cause

In src/packagedcode/go_mod.py, lines 132-135:

parsed_dep_link = parse_dep_link(req)
ns_name = parsed_dep_link.group('ns_name')
namespace, _, name = ns_name.rpartition('/')
if parsed_dep_link:

When req is a blank line or comment line, preprocess(req) strips it down to an empty string "". Consequently, parse_dep_link(req) returns None.

However, parsed_dep_link.group('ns_name') is called on line 133 before the if parsed_dep_link: check on line 135.

The exact same bug also exists on line 150 in the exclude (...) block:

parsed_dep_link = parse_dep_link(exc)
ns_name = parsed_dep_link.group('ns_name')
namespace, _, name = ns_name.rpartition('/')
if parsed_dep_link:
Proposed Solution

Move the attribute access inside the if parsed_dep_link: conditional block for both require and exclude:

parsed_dep_link = parse_dep_link(req)
if parsed_dep_link:
    ns_name = parsed_dep_link.group('ns_name')
    namespace, _, name = ns_name.rpartition('/')
    require.append(
        GoModule(
            namespace=namespace,
            name=name,
            version=parsed_dep_link.group('version'),
        )
    )

And similarly for exclude:

parsed_dep_link = parse_dep_link(exc)
if parsed_dep_link:
    ns_name = parsed_dep_link.group('ns_name')
    namespace, _, name = ns_name.rpartition('/')
    exclude.append(
        GoModule(
            namespace=namespace,
            name=name,
            version=parsed_dep_link.group('version'),
        )
    )
Environment
  • ScanCode version: 33.0.0rc1 / develop branch
  • OS: All
Vorherrschende Sprache
Python
Sterne
2.7k
Forks
871
Ø Merge
1 T. 10 Std.
Gemergte PRs (30 T.)
3

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus aboutcode-org/scancode-toolkit

Alle Issues in aboutcode-org/scancode-toolkit

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.