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

x/tools/go/packages: document whether Package.GoFiles and Package.Syntax contain the files in the same order #26915

Open
kevinburke opened this Issue Aug 10, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@kevinburke
Copy link
Contributor

kevinburke commented Aug 10, 2018

The documentation for packages.Package.GoFiles says:

    // GoFiles lists the absolute file paths of the package's Go source files.
    GoFiles []string

The documentation for packages.Package.Syntax says:

    // Syntax is the package's syntax trees, for the files listed in GoFiles.
    //
    // Mode LoadSyntax set this field for packages matching the patterns.
    // Mode LoadSyntaxAll sets this field for all packages, including dependencies.
    Syntax []*ast.File

Are these always going to be in the same order?

Specifically, I'm trying to alter the contents of an *ast.File and write it back to disk. How can I get the filename of the abstract syntax tree I am trying to use?

@gopherbot gopherbot added this to the Unreleased milestone Aug 10, 2018

@bcmills

This comment has been minimized.

Copy link
Member

bcmills commented Aug 10, 2018

@alandonovan

This comment has been minimized.

Copy link
Contributor

alandonovan commented Aug 10, 2018

The order will usually be the same, but GoFiles is not always the precise list of files seen by the compiler---look at the CompiledGoFiles field for that. You can assume a correspondence between CompiledGoFiles and Syntax, or you can obtain the filename from the syntax tree using fset.Position(Syntax[i].Pos()).File.

We should indeed document this more clearly.

@bcmills bcmills added the NeedsFix label Aug 10, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment