Skip to content

docs: remove invalid example about mixing files and directory #4566

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

Merged
merged 1 commit into from
Mar 23, 2024

Conversation

ldez
Copy link
Member

@ldez ldez commented Mar 23, 2024

The mix of file and package/directory (as documented inside the quick start) doesn't work as expected.

Same thing for the usage of multiple files from different packages.

Examples
$ golangci-lint run main.go          
main.go:11:5: Error return value is not checked (errcheck)
        Bar()
           ^
$ golangci-lint run pkga/... main.go
ERRO Running error: context loading failed: failed to load packages: failed to load packages: failed to load with go/packages: -: named files must be .go files: ./pkga/...
$ golangci-lint run pkga main.go 
ERRO Running error: context loading failed: failed to load packages: failed to load packages: failed to load with go/packages: -: named files must be .go files: ./pkga
$ golangci-lint run pkga/file1.go pkgb/file1.go 
ERRO [linters_context] typechecking error: named files must all be in one directory; have pkga and pkgb

This limitation is related to the package loader of x/tools.

There is a hack: the usage of the prefix file= allows to mix files and packages but this has a major side effect.

The usage of file=, despite what the name of the query suggests, will not limit the loading to the file but to the package that contains the file.
In other words, instead of analyzing one file, all the sibling files will be analyzed.
So this is not an acceptable solution.

In this context, I edited the documentation to remove this invalid example.

To reproduce
#!/bin/sh -e

## setup module
rm -rf sandbox
mkdir sandbox && cd $_
go mod init github.com/golangci/sandbox

## main.go
cat > main.go  <<EOF
package main

import (
	"github.com/golangci/sandbox/pkga"
	"github.com/golangci/sandbox/pkgb"
)

func main() {
	pkga.Foo()
	pkgb.Foo()
	Bar()
}

func Bar() error {
	return nil
}

EOF

## pkga/file1.go
cat > pkga/file1.go  <<EOF
package pkga

func Foo() {
	foo()
	return
}

func foo() error {
	return nil
}
EOF

## pkgb/file1.go
cat > pkgb/file1.go  <<EOF
package pkgb

func Foo() {
	foo()
	return
}

func foo() error {
	return nil
}
EOF

## .golangci.yml
cat > .golangci.yml  <<EOF
linters:
  disable-all: true
  enable:
    - errcheck
EOF

Fixes #3200
Fixes #1081
Fixes #942

@ldez ldez added bug Something isn't working area: docs labels Mar 23, 2024
@ldez ldez added this to the next milestone Mar 23, 2024
@ldez ldez changed the title docs: remove invalid exmaple about mixing files and directory. docs: remove invalid example about mixing files and directory. Mar 23, 2024
@ldez ldez changed the title docs: remove invalid example about mixing files and directory. docs: remove invalid example about mixing files and directory Mar 23, 2024
@ldez ldez merged commit 13f7f68 into golangci:master Mar 23, 2024
@ldez ldez deleted the fix/doc-mix-file-pkg branch March 23, 2024 17:04
@ldez ldez modified the milestones: next, v1.57 Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: docs bug Something isn't working
Projects
None yet
2 participants