goimportcycle

Import of github.com/Ullaakut/nmap fails with "missing metadata for import"/"import cycle not allowed"


I use Windows 11 and could not import github.com/Ullaakut/nmap (missing metadata for import of "github.com/Ullaakut/nmap")

I try to import github.com/Ullaakut/nmap with:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/Ullaakut/nmap"
)

func main() {
    // Equivalent to
    // nmap -sV -T4 192.168.0.0/24 with a filter to remove non-RTSP ports.
    scanner, err := nmap.NewScanner(
        context.Background(),
        nmap.WithTargets("192.168.0.0/24"),
        nmap.WithPorts("80", "554", "8554"),
        nmap.WithServiceInfo(),
        nmap.WithTimingTemplate(nmap.TimingAggressive),
        // Filter out ports that are not RTSP
        nmap.WithFilterPort(func(p nmap.Port) bool {
            return p.Service.Name == "rtsp"
        }),
        // Filter out hosts that don't have any open ports
        nmap.WithFilterHost(func(h nmap.Host) bool {
            // Filter out hosts with no open ports.
            for idx := range h.Ports {
                if h.Ports[idx].Status() == "open" {
                    return true
                }
            }

            return false
        }),
    )
    if err != nil {
        log.Fatalf("unable to create nmap scanner: %v", err)
    }

    result, warnings, err := scanner.Run()
    if len(*warnings) > 0 {
        log.Printf("run finished with warnings: %s\n", *warnings) // Warnings are non-critical errors from nmap.
    }
    if err != nil {
        log.Fatalf("nmap scan failed: %v", err)
    }

    for _, host := range result.Hosts {
        fmt.Printf("Host %s\n", host.Addresses[0])

        for _, port := range host.Ports {
            fmt.Printf("\tPort %d open with RTSP service\n", port.ID)
        }
    }
}

I try go mod init github.com/Ullaakut/nmap; a go.mod file appeard

module github.com/Ullaakut/nmap

go 1.22.4

An error occured after that : import cycle not allowed

go install github.com/Ullaakut/nmap/v3@latest
package github.com/Ullaakut/nmap/v3 is not a main package

then I try:

go install github.com/Ullaakut/nmap@latest package github.com/Ullaakut/nmap is not a main package

then I try

go get github.com/Ullaakut/nmap/v3@latest    
go: added github.com/Ullaakut/nmap/v3 v3.0.3
go: added golang.org/x/sync v0.1.0

a go.sum file appeared and my go.mod file changed:

module github.com/Ullaakut/nmap

go 1.22.4

require (
    github.com/Ullaakut/nmap/v3 v3.0.3 // indirect
    golang.org/x/sync v0.1.0 // indirect
)

Then I write go mod tidy; my go.mod file is now:

module github.com/Ullaakut/nmap
go 1.22.4

but still

could not import github.com/Ullaakut/nmap (missing metadata for import of "github.com/Ullaakut/nmap")
import cycle not allowed

How can I import it?


Solution

  • I try go mod init github.com/Ullaakut/nmap; a go.mod file appeard

    This will be part of your problem, the module path (e.g. go mod init [module-path]) should identify your module and not the path of something you are trying to import. The docs say:

    module path: A path that identifies a module and acts as a prefix for package import paths within the module. For example, "golang.org/x/net".

    The next thing to look at is your import; the docs suggest import github.com/Ullaakut/nmap/v3. The v3 indicates that there have been previous versions of this library that are incompatible with the current release (so best to use the current release). So to fix your issue I'd suggest changing your code to use import "github.com/Ullaakut/nmap/v3". After making this change you can resolve the other issues by running:

    go mod edit -module myModule 
    go mod tidy
    

    This will change your module name to myModule, ideally the module path should be the path to the repository where your code is stored (e.g. github.com/my/repo) but using myModule will be fine for now (see the docs and this answer for more info. With these changes your code compiles on my machine.

    Note: I rarely use go install or go get. Adding the import to the relevant .go file, and running go mod tidy is generally all that is needed (go mod tidy automatically retrieves any needed packages).