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

Aperta
#5,489 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@muskan-creates352 ci sta già lavorando.

Dal 15/11/2026.

  • #5490 di @muskan-creates352 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
Python
Stelle
2.7k
Fork
871
Merge medio
1g 10h
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di aboutcode-org/scancode-toolkit

Tutte le issue di aboutcode-org/scancode-toolkit

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.