File: workspace.go

package info (click to toggle)
golang-github-cue-lang-cue 0.12.0.-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,072 kB
  • sloc: sh: 57; makefile: 17
file content (112 lines) | stat: -rw-r--r-- 3,583 bytes parent folder | download
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
105
106
107
108
109
110
111
112
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cache

import (
	"context"
	"errors"
	"fmt"
	"path/filepath"

	"cuelang.org/go/internal/golangorgx/gopls/file"
	"cuelang.org/go/internal/golangorgx/gopls/protocol"
	"golang.org/x/mod/modfile"
)

// isGoWork reports if uri is a go.work file.
func isGoWork(uri protocol.DocumentURI) bool {
	return filepath.Base(uri.Path()) == "go.work"
}

// goWorkModules returns the URIs of go.mod files named by the go.work file.
func goWorkModules(ctx context.Context, gowork protocol.DocumentURI, fs file.Source) (map[protocol.DocumentURI]unit, error) {
	fh, err := fs.ReadFile(ctx, gowork)
	if err != nil {
		return nil, err // canceled
	}
	content, err := fh.Content()
	if err != nil {
		return nil, err
	}
	filename := gowork.Path()
	dir := filepath.Dir(filename)
	workFile, err := modfile.ParseWork(filename, content, nil)
	if err != nil {
		return nil, fmt.Errorf("parsing go.work: %w", err)
	}
	var usedDirs []string
	for _, use := range workFile.Use {
		usedDirs = append(usedDirs, use.Path)
	}
	return localModFiles(dir, usedDirs), nil
}

// localModFiles builds a set of local go.mod files referenced by
// goWorkOrModPaths, which is a slice of paths as contained in a go.work 'use'
// directive or go.mod 'replace' directive (and which therefore may use either
// '/' or '\' as a path separator).
func localModFiles(relativeTo string, goWorkOrModPaths []string) map[protocol.DocumentURI]unit {
	modFiles := make(map[protocol.DocumentURI]unit)
	for _, path := range goWorkOrModPaths {
		modDir := filepath.FromSlash(path)
		if !filepath.IsAbs(modDir) {
			modDir = filepath.Join(relativeTo, modDir)
		}
		modURI := protocol.URIFromPath(filepath.Join(modDir, "go.mod"))
		modFiles[modURI] = unit{}
	}
	return modFiles
}

// isGoMod reports if uri is a go.mod file.
func isGoMod(uri protocol.DocumentURI) bool {
	return filepath.Base(uri.Path()) == "go.mod"
}

// goModModules returns the URIs of "workspace" go.mod files defined by a
// go.mod file. This set is defined to be the given go.mod file itself, as well
// as the modfiles of any locally replaced modules in the go.mod file.
func goModModules(ctx context.Context, gomod protocol.DocumentURI, fs file.Source) (map[protocol.DocumentURI]unit, error) {
	fh, err := fs.ReadFile(ctx, gomod)
	if err != nil {
		return nil, err // canceled
	}
	content, err := fh.Content()
	if err != nil {
		return nil, err
	}
	filename := gomod.Path()
	dir := filepath.Dir(filename)
	modFile, err := modfile.Parse(filename, content, nil)
	if err != nil {
		return nil, err
	}
	var localReplaces []string
	for _, replace := range modFile.Replace {
		if modfile.IsDirectoryPath(replace.New.Path) {
			localReplaces = append(localReplaces, replace.New.Path)
		}
	}
	modFiles := localModFiles(dir, localReplaces)
	modFiles[gomod] = unit{}
	return modFiles, nil
}

// fileExists reports whether the file has a Content (which may be empty).
// An overlay exists even if it is not reflected in the file system.
func fileExists(fh file.Handle) bool {
	_, err := fh.Content()
	return err == nil
}

// errExhausted is returned by findModules if the file scan limit is reached.
var errExhausted = errors.New("exhausted")

// Limit go.mod search to 1 million files. As a point of reference,
// Kubernetes has 22K files (as of 2020-11-24).
//
// Note: per golang/go#56496, the previous limit of 1M files was too slow, at
// which point this limit was decreased to 100K.
const fileLimit = 100_000