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

go/parser: ParseDir does not respect Build Constraints #21157

umarniz opened this issue Jul 25, 2017 · 2 comments

go/parser: ParseDir does not respect Build Constraints #21157

umarniz opened this issue Jul 25, 2017 · 2 comments


Copy link

umarniz commented Jul 25, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

Atleast go 1.7 & 1.8 have this error

What operating system and processor architecture are you using (go env)?

Can reproduce this error on Mac & Linux

What did you do?

I can build my program fine even if there is a file that is only supposed to compile with go 1.9 with a build constraint for example:

The ParseDir function in "go/parser" does not respect the "+build 1.9" constraint in the comment and tries to parse the file anyways causing it to throw an error.

What did you expect to see?

I expected the ParseDir to ignore the file as the file has an Build Constraint which should signal ParseDir which is trying to parse all *.go" files for their AST that it cannot fully parse this file.

What did you see instead?

The ParseDir throws a mysterious error:

src/ pkg cause error: src/ expected type, found '=' (and 1 more errors)
Copy link

cznic commented Jul 25, 2017

go/parser.ParseDir does not and by design must not consider build tags.

If you need to discriminate by build constraints you can use the PackageClauseOnly|ParseComments mode to handle them by yourself or you can use go/build.ImportDir.

umarniz added a commit to umarniz/swagger that referenced this issue Jul 25, 2017
Copy link

mvdan commented Jul 25, 2017

What @cznic said - there doesn't seem to be a bug here. Its godoc even says "all files with names ending in .go".

@mvdan mvdan closed this as completed Jul 25, 2017
@mikioh mikioh changed the title ParseDir does not respect Build Constraints go/parser: ParseDir does not respect Build Constraints Aug 2, 2017
@golang golang locked and limited conversation to collaborators Aug 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet

No branches or pull requests

4 participants