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

Build tags ignored in test files #113

Open
rclark opened this issue Jul 6, 2024 · 2 comments · May be fixed by #115
Open

Build tags ignored in test files #113

rclark opened this issue Jul 6, 2024 · 2 comments · May be fixed by #115

Comments

@rclark
Copy link

rclark commented Jul 6, 2024

I am seeing that when an example function is defined in a _test.go file has some build tag constraint, those constraints don't seem to be respected by gomarkdoc.

I put together a little repo to demonstrate this. One func Example() is defined in a file that should only be selected when the build tag example is on, another func Example() is in a file that should only be selected when the build tag is off. But both examples show up when I run gomarkdoc, regardless of whether I specify any build tags in GOFLAGS or --tags.

Screenshot 2024-07-06 at 1 03 36 PM
@rclark
Copy link
Author

rclark commented Jul 7, 2024

I suspect that this is because you're passing an unfiltered []*ast.File (which includes files that should be filtered by build tags) from cfg.Files into the doc.Examples() method here:

examples := doc.Examples(cfg.Files...)

One solution I could imagine would be to pass the *build.Package into NewConfig so that in addition to .Files the config struct might also have a field to capture a set of []*ast.File filtered according to the build tags. The build.Context MatchFile method could help.

func NewConfig(log logger.Logger, workDir string, pkgDir string, opts ...ConfigOption) (*Config, error) {


Or, maybe define another unexported function like getDocPkg designed to return the []*doc.Example? You'd have to do this AST parsing again in another loop.

gomarkdoc/lang/package.go

Lines 278 to 282 in 8b1606a

func getDocPkg(pkg *build.Package, fs *token.FileSet, includeUnexported bool) (*doc.Package, error) {
pkgs, err := parser.ParseDir(
fs,
pkg.Dir,
func(info os.FileInfo) bool {

I built a pretty hackneyed solution to it that gets me unstuck in the project where I ran into this. But its not elegant at all. I'd be happy to open a PR if you're interested, let me know if one of these options ☝️ sounds good or if you see another possible approach.

@rclark rclark linked a pull request Jul 9, 2024 that will close this issue
@rogueloop
Copy link

This is very useful feature, hopes to get merged soon.

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

Successfully merging a pull request may close this issue.

2 participants