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

Open
#5,489 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@muskan-creates352 is already working on this.

Since Nov 15, 2026.

  • #5490 by @muskan-creates352 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python
Domain
backend

Research direction

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.

Written by the indexing model from the issue text.

Description

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
Dominant language
Python
Stars
2.7k
Forks
871
Avg merge
1d 10h
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from aboutcode-org/scancode-toolkit

All issues in aboutcode-org/scancode-toolkit

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.