Skip to content

Allow multiple main in same folder (for adhoc scripts, now fails with "main redeclared...") #1370

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

Closed
timonwong opened this issue Sep 16, 2020 · 7 comments
Labels
question Further information is requested stale No recent correspondence or work activity

Comments

@timonwong
Copy link
Contributor

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

Sometimes we include several ad-hoc golang scripts in same folder, for example "scripts", to do ad-hoc jobs (executed them with go run).
However, since golangci-lint cannot just ignore "packages", it will fail like this:

WARN [runner] Can't run linter goanalysis_metalinter: S1004: failed prerequisites: [(inspect@xxx, isgenerated@xxx): analysis skipped: errors in package: [xxx/script-1.go:13:6: main redeclared in this block xxx/script-2.go:13:6:       other declaration of main]] 
ERRO Running error: S1004: failed prerequisites: [(inspect@xxx, isgenerated@xxx): analysis skipped: errors in package: [xxx/script-1.go:13:6: main redeclared in this block xxx/script-2.go:13:6:         other declaration of main]] 

Currently, as a workaround, we run golangci-lint individually for each valid packages, and then run golangci-lint for those scripts one by one, this is sometimes cumbersome so I think it's better to do it in golangci-lint.

Describe the solution you'd like

Add an option to not treat these directories as packages, instead, scan them as golang files individually.

Additional context

#489 (comment)

@timonwong timonwong added the enhancement New feature or improvement label Sep 16, 2020
@boring-cyborg
Copy link

boring-cyborg bot commented Sep 16, 2020

Hey, thank you for opening your first Issue ! 🙂 If you would like to contribute we have a guide for contributors.

@SVilgelm
Copy link
Member

you can use skip-dirs in main golangci.yml: https://github.com/golangci/golangci-lint/blob/master/.golangci.yml#L137

and add individual golangci.yml for every sub project and just run all of them

@timonwong
Copy link
Contributor Author

you can use skip-dirs in main golangci.yml: https://github.com/golangci/golangci-lint/blob/master/.golangci.yml#L137

skip-dirs won't work since the loader will include those packages first.

So provided the following project structure:

.
├── .golanci.yml
├── cmd
│   └── demo-server
│       └── main.go
├── go.mod
├── pkg
│   └── httputil
│       └── httputil.go
└── scripts
    ├── script1.go
    └── script2.go

In order to make golangci-lint work, we need to run the following respectively:

  • golangci-lint run ./cmd/... # package
  • golangci-lint run ./pkg/... # package
  • golangci-lint run scripts/script1.go # file
  • golangci-lint run scripts/script1.go # file

And this is not straight forward

@SVilgelm
Copy link
Member

because you need to put script1.go and script2.go into different packages

scripts
|-- script1
|    |-- script1.go
|-- script2
     |-- script2.go

you structure incorrect, you cannot declare functions/variable with same name in same package. Just put to different packages

@ldez ldez added question Further information is requested and removed enhancement New feature or improvement labels Mar 22, 2021
@stale
Copy link

stale bot commented Mar 30, 2022

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.

@stale stale bot added the stale No recent correspondence or work activity label Mar 30, 2022
@stale stale bot closed this as completed Apr 29, 2022
@vegerot
Copy link

vegerot commented Jul 29, 2024

you structure incorrect,

@SVilgelm can you explain how it's incorrect? I can have multiple main functions in the same main package in the same directory and go run them fine

@ldez
Copy link
Member

ldez commented Jul 29, 2024

can you explain how it's incorrect?

golangci-lint works by package, it's the same thing as run go run . (or go run ./scripts/).

Also, you can create multiple main functions but you cannot call them by package because all those files are a part of the same package.

From the point of view of a package, it's impossible to have multiple functions/variables with the same name.

Go is an app-oriented language, not a script-oriented language, so it's a bad practice to have a main package with multiple main functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested stale No recent correspondence or work activity
Projects
None yet
Development

No branches or pull requests

4 participants