Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to run golangci_lint in a specific directory / change project directory for linter #4370

Open
angelahnicole opened this issue Dec 9, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@angelahnicole
Copy link

Is your feature request related to a problem? Please describe.

It'd be nice to be able to run golangci_lint in a sub-directory of a golang repository so megalinter works for projects that are structured like this:

.
├── .mega-linter.yml
├── README.md
├── src
│   ├── cmd
│   │   └── myservice
│   │       └── main.go
│   ├── .golangci.yml
│   ├── go.mod
│   └── go.sum
└── terraform

Currently this does not work because golangci_lint is ran in the top-level directory where there isn't a go.mod or go.sum file, and so you get this error:

❌ Linted [GO] files with [golangci-lint]: Found 1 error(s) - (0.74s)
- Using [golangci-lint v1.59.1] https://megalinter.io/7.13.0/descriptors/go_golangci_lint
- MegaLinter key: [GO_GOLANGCI_LINT]
- Rules config: [.golangci.yml]
--Error detail:
level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"

Describe the solution you'd like

It'd be nice to specify something like this in `.mega-linter.yml:

GO_PROJECT_DIRECTORY: ./src

And have megalinter basically do a cd ./src before running golangci_lint.

Describe alternatives you've considered

I've tried passing in a different lint mode and regex like so:

GO_GOLANGCI_LINT_CLI_LINT_MODE: list_of_files
GO_FILTER_REGEX_INCLUDE: (src/)

But it doesn't work since only project is supported for GO_GOLANGCI_LINT_CLI_LINT_MODE.

I also looked into updating GO_GOLANGCI_LINT_ARGUMENTS somehow, but golangci_lint seems to need the go.mod and go.sum files in the directory that it's ran from and there is no way to specify them.

Additional context

N/A

@angelahnicole angelahnicole added the enhancement New feature or request label Dec 9, 2024
@nvuillam
Copy link
Member

@angelahnicole it seems that golangci_lint allows to specify folders to analyze (sources: ChatGPT & golangci doc)

Please can you try these solutions and confirm if it works ?

Using command line

Add in .mega-linter.yml

GO_GOLANGCI_LINT_ARGUMENTS: ["./src"]

Using ENV variable

GOLANGCI_LINT_DIR=./src

Using configuration file .golangci.yml

run:
  # List of directories to include
  # "." means current directory; you can specify "src" here
  dirs:
    - ./src

@angelahnicole
Copy link
Author

angelahnicole commented Dec 17, 2024

Hey @nvuillam! We did try that, but it doesn't work :/

error when we added GO_GOLANGCI_LINT_ARGUMENTS: ["src/"]:

�[31m❌ Linted [GO] files with [golangci-lint]: Found 1 error(s) - (0.42s)�[0m
- Using [golangci-lint v1.62.0] https://megalinter.io/8.3.0/descriptors/go_golangci_lint
- MegaLinter key: [GO_GOLANGCI_LINT]
- Rules config: [/.golangci.yml]
- Command: [golangci-lint run --fix src/ -c /tmp/lint/.golangci.yml]
--Error detail:
level=error msg="Running error: context loading failed: no go files to analyze: running `go mod tidy` may solve the problem"

error when we added GO_GOLANGCI_LINT_ARGUMENTS: ["src/..."]:

�[31m❌ Linted [GO] files with [golangci-lint]: Found 1 error(s) - (0.89s)�[0m
- Using [golangci-lint v1.62.0] https://megalinter.io/8.3.0/descriptors/go_golangci_lint
- MegaLinter key: [GO_GOLANGCI_LINT]
- Rules config: [/.golangci.yml]
- Command: [golangci-lint run --fix src/... -c /tmp/lint/.golangci.yml]
--Error detail:
level=error msg="[linters_context] typechecking error: pattern ./src/...: directory prefix src does not contain main module or its selected dependencies"

We're assuming it's because the go.mod and go.sum files are in ./src and having these files in a sub-directory isn't supported by golangci-lint:

EDIT:

FWIW, we also tried the env variable (GOLANG_LINT_DIR) and the suggested config file for .golangci-lint and got the same error.

@nvuillam
Copy link
Member

Oops... you can ask on golangci-lint repo what is the good call to perform for your use case ? ( I'm not a go expert :/ )

Then once we'll have the good call i'll be able to help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants