-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add support for multiple subdirs each with their own go.mod #828
Comments
To add a bit of color here, this is a common issue for anybody with a multi-module monorepo (a reasonably common pattern). Given the following directory structure:
Running the following from the root of |
I came up with the following solution when trying to set up the golangci-lint GitHub Action for a monorepo:
name: golangci-lint
on: [push, pull_request]
jobs:
resolve-modules:
name: Resolve Modules
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout Sources
uses: actions/checkout@v2
- id: set-matrix
run: ./tools/resolve-modules.sh
golangci:
name: Linter
needs: resolve-modules
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJson(needs.resolve-modules.outputs.matrix) }}
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v1
with:
version: v1.28
working-directory: ${{ matrix.workdir }}
#!/bin/bash
# Recursively finds all directories with a go.mod file and creates
# a GitHub Actions JSON output option. This is used by the linter action.
echo "Resolving modules in $(pwd)"
PATHS=$(find . -mindepth 2 -type f -name go.mod -printf '{"workdir":"%h"},')
echo "::set-output name=matrix::{\"include\":[${PATHS%?}]}" This uses the newly introduced |
Adding to @twelho (thank you for this!) PATHS=$(find . -mindepth 2 -not -path "*/vendor/*" -type f -name go.mod -printf '{"workdir":"%h"},') to exclude |
I still have problems with multiple modules within my repo and @twelho 's solution did solves my problem! |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hi, any update if this will be a supported without workarounds? |
For repos:
- repo: https://github.com/golangci/golangci-lint
rev: v1.50.1
hooks:
# no built-in support for multiple go.mod
# https://github.com/golangci/golangci-lint/issues/828
- id: golangci-lint
name: golangci-lint-root
entry: bash -c 'golangci-lint run'
- id: golangci-lint
name: golangci-lint-subdir
entry: bash -c 'cd subtest && golangci-lint run --config $OLDPWD/.golangci.yml' |
Any movement on this? |
This worked for me. on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
directory: [module1, module2, module3]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '^1.21.1'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
working-directory: ${{ matrix.directory }} |
@wafer-bw Thank you for the suggestion. I was able to make it dynamic by using the following: name: golangci-lint
on:
push:
branches:
- "master"
- "main"
paths-ignore:
- "**.md"
- LICENSE
- ".github/ISSUE_TEMPLATE/*.yml"
- ".github/dependabot.yml"
pull_request:
branches:
- "*"
paths-ignore:
- "**.md"
- LICENSE
- ".github/ISSUE_TEMPLATE/*.yml"
- ".github/dependabot.yml"
jobs:
generate-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Fetch Repository
uses: actions/checkout@v4
- id: set-matrix
run: |
DIRECTORIES=$(find . -type d -not -path '*/\.*' | jq -R -s -c 'split("\n")[:-1]')
echo "matrix=${DIRECTORIES}" >> $GITHUB_OUTPUT
golangci-lint:
needs: generate-matrix
runs-on: ubuntu-latest
strategy:
matrix:
modules: ${{fromJson(needs.generate-matrix.outputs.matrix)}}
steps:
- name: Fetch Repository
uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '^1.21.x'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
working-directory: ${{ matrix.modules }} |
I was surprised that this didn't work out the box and was hiding issues because of multiple modules in the repo with no warning, definitely a gotach. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
I added examples for Go workspace and the GitHub Action: https://github.com/golangci/golangci-lint-action?tab=readme-ov-file#how-to-use |
Thank you for creating the issue!
Please include the following information:
Version of golangci-lint
Config file
./go.mod
./go.sum
forwarder/go.mod
forwarder/go.sum
k8s/go.mod
k8s/go.sum
The text was updated successfully, but these errors were encountered: