super-linter / super-linter

Combination of multiple linters to run as a GitHub Action or standalone
https://github.com/super-linter/super-linter
MIT License
9.49k stars 969 forks source link

GO Linter fails: Can't run linter goanalysis_metalinter #5211

Closed kuritka closed 8 months ago

kuritka commented 9 months ago

Is there an existing issue for this?

Current Behavior

I am not able to get the validations for GO to work, although golangci-lint runs locally without problems.

# no issues
lint:
    @echo -e "\n$(YELLOW)Running the linters$(NC)"
    goimports -w ./
    brew install golangci-lint
    golangci-lint run
SUPER_LINTER_VERSION=slim-v5.7.2
lint:
    @echo -e "\n$(YELLOW)Running the linters$(NC)"
    docker pull ghcr.io/super-linter/super-linter:$(SUPER_LINTER_VERSION)
    docker run \
      -e ACTIONS_RUNNER_DEBUG=false \
      -e DEFAULT_BRANCH=main \
      -e LOG_LEVEL=DEBUG \
      -e RUN_LOCAL=true \
      -e VALIDATE_ALL_CODEBASE=false \
      -e VALIDATE_GO=true \
      -v $(PWD):/tmp/lint \
      ghcr.io/super-linter/super-linter:$(SUPER_LINTER_VERSION)

for main.go I'm running local super-linter:

// Package main
package main

import (
    "fmt"

    "github.com/alecthomas/kong"
    "github.com/gin-gonic/gin"
    "github.com/rs/zerolog/log"
)

// CLI https://github.com/alecthomas/kong
type CLI struct {
    Port              int  `env:"PORT"`
    PretifyJSONOutput bool `env:"INDENT_JSON_OUTPUT"`
    Version           string
}

var cli = &CLI{}

var version = "development"

func main() {
    fmt.Println("Hello, World!")
    log.Info().Msg("Starting application")
    _ = kong.Parse(cli)
    log.Info().Msgf("listening on port :%v", cli.Port)
    gin.SetMode(gin.ReleaseMode)
    cli.Version = version
    _ = gin.Default()
}

This constantly generates a very strange error, which varies in the package

------
level=warning msg="[runner] Can't run linter goanalysis_metalinter: inspect: failed to load package embed: could not load export data: no export data for \"fmt\""
level=error msg="Running error: 1 error occurred:
        * can't run linter goanalysis_metalinter: inspect: failed to load package embed: could not load export data: no export data for \"embed\"

"
------

I tried to run it on different architectures. The name of the package is often changed, probably depending on how the linter takes it. My go.mod has GO version v1.21.

I'm not able to identify where the bug came from, but I find it strange that the golangci-lint run always ran without problems locally and as a GitHub action.

Expected Behavior

linter pass, or help identify issue

Super-Linter version

v5.7.2

Relevant log output

see output.log

Steps To Reproduce

  1. Run my Makefile against main.go, which I supplied.

Anything else?

No response

ferrarimarco commented 9 months ago

Hi @kuritka !

We just released v6. Can you try with that? Thanks!

ferrarimarco commented 9 months ago

Also, your golint-ci invocation is different from the one that super-linter uses. Can you try running golint-ci locally with the same command? Thanks!

kuritka commented 9 months ago

Hi @ferrarimarco, I think it doesn't copy files into workspace. (I did for both v6.0.0 + slim-v6.0.0)

2024-01-31 18:16:34 [WARN]   No files were found in the GITHUB_WORKSPACE:[/tmp/lint] to lint!
2024-01-31 18:16:39 [NOTICE]   All files and directories linted successfully
SUPER_LINTER_VERSION=slim-v6.0.0
lint:
    @echo -e "\n$(YELLOW)Running the linters$(NC)"
    docker pull ghcr.io/super-linter/super-linter:$(SUPER_LINTER_VERSION)
    docker run \
      -e ACTIONS_RUNNER_DEBUG=false \
      -e DEFAULT_BRANCH=main \
      -e LOG_LEVEL=NOTICE\
      -e RUN_LOCAL=true \
      -e VALIDATE_ALL_CODEBASE=false \
      -e VALIDATE_GO=true \
      -v $(PWD):/tmp/lint \
      ghcr.io/super-linter/super-linter:$(SUPER_LINTER_VERSION)
kuritka commented 9 months ago

Here is what I'm running locally:

❯ golangci-lint --version 
golangci-lint has version v1.51.2 built from (unknown, mod sum: "h1:yIcsT1X9ZYHdSpeWXRT1ORC/FPGSqDHbHsu9uk4FK7M=") on (unknown)

# ./.golangci.yml  is copied from super-linter
golangci-lint run --fast -c ./.golangci.yml 
ferrarimarco commented 9 months ago

What is your current working directory (the value of PWD)?

kuritka commented 9 months ago

sorry, I can't expose internal information

/Users/<user>/GolandProjects/github.com/<owner>/<test_project>

I hardcoded path instead of PWD but see same result:

2024-01-31 18:33:28 [WARN]   No files were found in the GITHUB_WORKSPACE:[/tmp/lint] to lint!
2024-01-31 18:33:34 [NOTICE]   All files and directories linted successfully
ferrarimarco commented 9 months ago

Are you running this on an ARM-based Mac by any chance? I'm not sure how compatible that would be because we don't publish an arm64 image. Anyway, it looks like the mounted directory is either empty, or it doesn't contain any .go files.

I think the command you're running might not be mounting what you expect. To check this, I would try opening a shell in the super-linter container by adding the --entrypoint /bin/bash option to the docker run command, and then checking if the content of /tmp/lint is what you expect. It should match with /Users/<user>/GolandProjects/github.com/<owner>/<test_project>.

ferrarimarco commented 9 months ago

Also, there something interesting in the log you posted:

2024-01-31 16:05:27 [DEBUG]   File:[/tmp/lint/internal/controller/aws.go], File_type:[go], Base_file:[aws.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/controller/aws.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/controller/controller.go], File_type:[go], Base_file:[controller.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/controller/controller.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/entities.go], File_type:[go], Base_file:[entities.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/entities.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/acounts.go], File_type:[go], Base_file:[acounts.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/acounts.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/aws.go], File_type:[go], Base_file:[aws.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/aws.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/check.go], File_type:[go], Base_file:[check.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/check.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/nacl.go], File_type:[go], Base_file:[nacl.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/nacl.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/parser/policy.go], File_type:[go], Base_file:[policy.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/parser/policy.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/parser/regex.go], File_type:[go], Base_file:[regex.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/parser/regex.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/parser/tags.go], File_type:[go], Base_file:[tags.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/parser/tags.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/roles.go], File_type:[go], Base_file:[roles.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/roles.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/route53.go], File_type:[go], Base_file:[route53.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/route53.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/users.go], File_type:[go], Base_file:[users.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/users.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/validator-dns.go], File_type:[go], Base_file:[validator-dns.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/validator-dns.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/validator-iam.go], File_type:[go], Base_file:[validator-iam.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/validator-iam.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/validator-identity.go], File_type:[go], Base_file:[validator-identity.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/validator-identity.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/validator-networking.go], File_type:[go], Base_file:[validator-networking.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/validator-networking.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/validator-unknown.go], File_type:[go], Base_file:[validator-unknown.go]
2024-01-31 16:05:28 [WARN]   File:{/tmp/lint/internal/model/maws/validator-unknown.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:28 [DEBUG]   File:[/tmp/lint/internal/model/maws/vpc.go], File_type:[go], Base_file:[vpc.go]
2024-01-31 16:05:29 [WARN]   File:{/tmp/lint/internal/model/maws/vpc.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:29 [DEBUG]   File:[/tmp/lint/internal/model/provider.go], File_type:[go], Base_file:[provider.go]
2024-01-31 16:05:29 [WARN]   File:{/tmp/lint/internal/model/provider.go} existed in commit data, but not found on file system, skipping...
2024-01-31 16:05:29 [DEBUG]   File:[/tmp/lint/main.go], File_type:[go], Base_file:[main.go]

That check is here:

https://github.com/super-linter/super-linter/blob/5c67776f9d9d3065efda30d47aef21b332fa2173/lib/functions/buildFileList.sh#L212-L216

Could it be that those files are not accessible somehow?

kuritka commented 9 months ago

those are files I deleted to keep the binary to a minimum and provide an example.

kuritka commented 9 months ago

I'm pretty sure, that the problem is that to lint GO, I have to have the downloaded files including all GO dependencies. That's why it is necessary to call go mod tidy before calling golangci-lint within container.

ferrarimarco commented 8 months ago

Hi @kuritka ! I was able to reproduce the issue with the example you provided. I added it as a (failing, for now) test case. That's the first step into finding a solution :)

ferrarimarco commented 8 months ago

By the way, running go mod tidy will also generate a go.mod file, or add any missing modules to the go.mod file. That would require modifying the source code, and it's something that super-linter currently tries to avoid.

If you're trying to lint a Go module (i.e. you have a go.mod file), you should set VALIDATE_GO=false, and set VALIDATE_GO_MODULES=true (the default for all VALIDATE_XXXX is true).

Can you try that?