Skip to content

feat: support go modules not in repository root #40

@honey-yogurt

Description

@honey-yogurt
gomodguard: Panic stack trace: goroutine 1 [running]:\nruntime/debug.Stack() & can't run linter goanalysis_metalinter: panic occurred

Description

My go.mod file is not in the root directory of the project. I used the pre-commit-hook project to apply golangci-lint. After renaming the directory where go.mod is located, I encountered a problem with the title.
This is my project structure:
image

Configuration

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
    - repo: https://github.com/pre-commit/pre-commit-hooks
      rev: v4.3.0
      hooks:
          - id: trailing-whitespace
          - id: end-of-file-fixer
          - id: check-yaml
          - id: check-added-large-files
    - repo: https://github.com/golangci/golangci-lint
      rev: v1.55.2
      hooks:
          - id: golangci-lint
    - repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
      rev: v9.10.0
      hooks:
          - id: commitlint
            stages: [ commit-msg ]
            additional_dependencies: [ '@commitlint/config-conventional' ]

Go environment

$ go version && go env
go version go1.20.7 windows/amd64

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\DELL\AppData\Local\go-build
set GOENV=C:\Users\DELL\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\DELL\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\DELL\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.7
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\DELL\AppData\Local\Temp\go-build3470650649=/tmp/go-build -gno-record-gcc-switches

Verbose output of running

$ golangci-lint cache clean
$ golangci-lint run -v
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\DELL\.cache\pre-commit\patch1702005891-24412.
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
check yaml...........................................(no files to check)Skipped
check for added large files..............................................Passed
golangci-lint............................................................Failed
- hook id: golangci-lint
- exit code: 3
level=warning msg="[runner] The linter 'interfacer' is deprecated (since v1.38.0) due to: The repository of the linter has been archived by the owner. "
level=warning msg="[runner] The linter 'structcheck' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused."
level=warning msg="[runner] The linter 'deadcode' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused."
level=warning msg="[runner] The linter 'golint' is deprecated (since v1.41.0) due to: The repository of the linter has been archived by the owner. Replaced by revive."
level=warning msg="[runner] The linter 'varcheck' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused."
level=warning msg="[runner] The linter 'maligned' is deprecated (since v1.38.0) due to: The repository of the linter has been archived by the owner. Replaced by govet 'fieldalignment'."
level=error msg="[runner] Panic stack trace: goroutine 1 [running]:\nruntime/debug.Stack()\n\tC:/Program Files/Go/src/runtime/debug/stack.go:24 +0x65\ngithub.com/golangci/golangci-lint/pkg/lint.(*Runner).runLinterSafe.func1()\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/lint/runner.go:126 +0x28d\npanic({0x1b05560, 0x26657c0})\n\tC:/Program Files/Go/src/runtime/panic.go:884 +0x213\ngithub.com/ryancurrah/gomodguard.(*Processor).SetBlockedModules(0xc0023bbed8)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/golangenv-system/pkg/mod/github.com/ryancurrah/gomodguard@v1.3.0/processor.go:146 +0x5e\ngithub.com/ryancurrah/gomodguard.NewProcessor(0xc000a60540)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/golangenv-system/pkg/mod/github.com/ryancurrah/gomodguard@v1.3.0/processor.go:68 +0x170\ngithub.com/golangci/golangci-lint/pkg/golinters.NewGomodguard.func1(0xc0001ba770)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/golinters/gomodguard.go:68 +0x45\ngithub.com/golangci/golangci-lint/pkg/golinters/goanalysis.(*Linter).preRun(0xc000a39f80, 0xc00111f5e8?)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/golinters/goanalysis/linter.go:157 +0xe2\ngithub.com/golangci/golangci-lint/pkg/golinters/goanalysis.MetaLinter.Run({{0xc00068ec00, 0x25, 0x40}, 0xc000b7cf30}, {0xc00007c800?, 0xc0006869a0?}, 0x0?)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/golinters/goanalysis/metalinter.go:26 +0xa5\ngithub.com/golangci/golangci-lint/pkg/lint.(*Runner).runLinterSafe(0x3?, {0x1ecbe68?, 0xc000a59c50?}, 0xc0001ba770, 0xc000175900)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/lint/runner.go:131 +0xcf\ngithub.com/golangci/golangci-lint/pkg/lint.Runner.Run.func1()\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/lint/runner.go:215 +0x65\ngithub.com/golangci/golangci-lint/pkg/timeutils.(*Stopwatch).TrackStage(0xc0007c4190, {0x1cd745f, 0x15}, 0xc00111f940)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/timeutils/stopwatch.go:111 +0x4a\ngithub.com/golangci/golangci-lint/pkg/lint.Runner.Run({{0xc0006869a0, 0x15, 0x15}, {0x1ed1750, 0xc0008b2060}}, {0x1ecbe68, 0xc000a59c50}, {0xc00000b378, 0x1, 0x1}, ...)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/lint/runner.go:214 +0x2ce\ngithub.com/golangci/golangci-lint/pkg/commands.(*Executor).runAnalysis(0xc000167200, {0x1ecbe68, 0xc000a59c50}, {0xc000a59bc0?, 0x45ec1606ad?, 0xc154c14c0e2e0334?})\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/commands/run.go:358 +0x611\ngithub.com/golangci/golangci-lint/pkg/commands.(*Executor).runAndPrint(0xc000167200, {0x1ecbe68, 0xc000a59c50}, {0xc000a59bc0, 0x0, 0x3})\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/commands/run.go:393 +0x1dd\ngithub.com/golangci/golangci-lint/pkg/commands.(*Executor).executeRun(0xc000167200, 0x0?, {0xc000a59bc0, 0x0, 0x3})\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/commands/run.go:510 +0x24f\ngithub.com/spf13/cobra.(*Command).execute(0xc000005200, {0xc000a59b60, 0x3, 0x3})\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/golangenv-system/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:944 +0x847\ngithub.com/spf13/cobra.(*Command).ExecuteC(0xc000004f00)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/golangenv-system/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:1068 +0x3bd\ngithub.com/spf13/cobra.(*Command).Execute(...)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/golangenv-system/pkg/mod/github.com/spf13/cobra@v1.7.0/command.go:992\ngithub.com/golangci/golangci-lint/pkg/commands.(*Executor).Execute(...)\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/pkg/commands/executor.go:151\nmain.main()\n\tC:/Users/DELL/.cache/pre-commit/repoyc5ufnm2/cmd/golangci-lint/main.go:41 +0x1de\n"
level=warning msg="[runner] Can't run linter goanalysis_metalinter: panic occurred: runtime error: invalid memory address or nil pointer dereference"
level=error msg="Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: panic occurred: runtime error: invalid memory address or nil pointer dereference\n\n"
[INFO] Restored changes from C:\Users\DELL\.cache\pre-commit\patch1702005891-24412.

A minimal reproducible example or link to a public repository

package main

import (
	"crypto/ecdsa"
	"crypto/elliptic"
	"crypto/rand"
	"fmt"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

func main() {

	r := gin.Default()

	r.GET("/health", func(c *gin.Context) {
		// 随便处理一些逻辑,以便于调试
		start := time.Now()
		priv, err := randomPrivateKey()
		if err != nil {
			fmt.Println("err", err)
		}
		fmt.Printf("%+v \n", priv)
		fmt.Println("time-consuming", time.Now().Sub(start))
		c.String(http.StatusOK, "I am ok!")
	})
	r.Run(":8000")
}

func randomPrivateKey() (*ecdsa.PrivateKey, error) {
	// 选择椭圆曲线类型
	curve := elliptic.P256()
	// 生成私钥
	privateKey, err := ecdsa.GenerateKey(curve, rand.Reader)
	if err != nil {
		return nil, err
	}
	return privateKey, nil
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions