Skip to content

x/tools/go/packages: needs more explanations and examples #35872

Open
@zigo101

Description

@zigo101

Edit: It looks I used this package wrongly. On the other hand, I think the docs of this package is lack of detailed explanations and examples. I converted this issue to a document enrichment request. For examples, what are "the package as compiled for the test" and "the test binary". And some command line argument examples may be helpful.

And is the paragraph problematic?

As noted earlier, the Config.Mode controls the amount of detail reported about the loaded packages, with each mode returning all the data of the previous mode with some extra added. See the documentation for type LoadMode for details.

Is it intended to explain the deprecated LoadFiles<LoadImports<LoadTypes<LoadSyntax<LoadAllSyntax preset modes?

(The following is the old content)

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

$ go version
go version go1.13.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

package main

import (
    "fmt"
    "time"
    
    "go/ast"
    "go/parser"
    "go/token"

    "golang.org/x/tools/go/packages"
)

func main() {
	pkgs, err := packages.Load(nil, "std")
	if err != nil {
		panic(err)
	}

	var configForParsing = &packages.Config{
		Mode: packages.NeedName | packages.NeedImports | packages.NeedTypes |
			packages.NeedDeps | packages.NeedExportsFile | packages.NeedFiles |
			packages.NeedCompiledGoFiles | packages.NeedTypesSizes |
			packages.NeedSyntax | packages.NeedTypesInfo,
		Tests: false,

		ParseFile: func(fset *token.FileSet, parseFilename string, _ []byte) (*ast.File, error) {
			var src interface{}
			mode := parser.ParseComments // | parser.AllErrors
			file, err := parser.ParseFile(fset, parseFilename, src, mode)
			if file == nil {
				return nil, err
			}
			for _, decl := range file.Decls {
				if fd, ok := decl.(*ast.FuncDecl); ok {
					fd.Body = nil
				}
			}
			return file, nil
		},
	}

	parsedPkgs := make([]*packages.Package, 0, 1000)
	for _, pkg := range pkgs {

		fmt.Println(pkg.PkgPath)
		
		pkgs, err := packages.Load(configForParsing, pkg.PkgPath)
		if err != nil {
			fmt.Println("packages.Load error:", err)
		}
		parsedPkgs = append(parsedPkgs, pkgs...)
	}
	
	fmt.Println("[done]")
    
	for {
		time.Sleep(time.Hour)
	}
	
	fmt.Println(parsedPkgs)
}

What did you expect to see?

<1G memory is consumed.

What did you see instead?

7G memory is consumed.

Initially I posted this result here: https://groups.google.com/forum/#!topic/golang-tools/cLIwae8rx1g
Alan Donovan said this is not normal and Josh Bleecher Snyder suggested me post an issue here, so I do.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions