aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/tools/go/packages/golist_overlay.go
blob: 71ffcd9d55bb12682535b6cac5aa8fffae27531b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package packages

import (
	"go/parser"
	"go/token"
	"path/filepath"
	"strconv"
	"strings"
)

// processGolistOverlay provides rudimentary support for adding
// files that don't exist on disk to an overlay. The results can be
// sometimes incorrect.
// TODO(matloob): Handle unsupported cases, including the following:
// - test files
// - adding test and non-test files to test variants of packages
// - determining the correct package to add given a new import path
// - creating packages that don't exist
func processGolistOverlay(cfg *Config, response *driverResponse) (modifiedPkgs, needPkgs []string, err error) {
	havePkgs := make(map[string]string) // importPath -> non-test package ID
	needPkgsSet := make(map[string]bool)
	modifiedPkgsSet := make(map[string]bool)

	for _, pkg := range response.Packages {
		// This is an approximation of import path to id. This can be
		// wrong for tests, vendored packages, and a number of other cases.
		havePkgs[pkg.PkgPath] = pkg.ID
	}

outer:
	for path, contents := range cfg.Overlay {
		base := filepath.Base(path)
		if strings.HasSuffix(path, "_test.go") {
			// Overlays don't support adding new test files yet.
			// TODO(matloob): support adding new test files.
			continue
		}
		dir := filepath.Dir(path)
		for _, pkg := range response.Packages {
			var dirContains, fileExists bool
			for _, f := range pkg.GoFiles {
				if sameFile(filepath.Dir(f), dir) {
					dirContains = true
				}
				if filepath.Base(f) == base {
					fileExists = true
				}
			}
			if dirContains {
				if !fileExists {
					pkg.GoFiles = append(pkg.GoFiles, path) // TODO(matloob): should the file just be added to GoFiles?
					pkg.CompiledGoFiles = append(pkg.CompiledGoFiles, path)
					modifiedPkgsSet[pkg.ID] = true
				}
				imports, err := extractImports(path, contents)
				if err != nil {
					// Let the parser or type checker report errors later.
					continue outer
				}
				for _, imp := range imports {
					_, found := pkg.Imports[imp]
					if !found {
						needPkgsSet[imp] = true
						// TODO(matloob): Handle cases when the following block isn't correct.
						// These include imports of test variants, imports of vendored packages, etc.
						id, ok := havePkgs[imp]
						if !ok {
							id = imp
						}
						pkg.Imports[imp] = &Package{ID: id}
					}
				}
				continue outer
			}
		}
	}

	needPkgs = make([]string, 0, len(needPkgsSet))
	for pkg := range needPkgsSet {
		needPkgs = append(needPkgs, pkg)
	}
	modifiedPkgs = make([]string, 0, len(modifiedPkgsSet))
	for pkg := range modifiedPkgsSet {
		modifiedPkgs = append(modifiedPkgs, pkg)
	}
	return modifiedPkgs, needPkgs, err
}

func extractImports(filename string, contents []byte) ([]string, error) {
	f, err := parser.ParseFile(token.NewFileSet(), filename, contents, parser.ImportsOnly) // TODO(matloob): reuse fileset?
	if err != nil {
		return nil, err
	}
	var res []string
	for _, imp := range f.Imports {
		quotedPath := imp.Path.Value
		path, err := strconv.Unquote(quotedPath)
		if err != nil {
			return nil, err
		}
		res = append(res, path)
	}
	return res, nil
}