-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
global_hash.go
191 lines (169 loc) · 6.38 KB
/
global_hash.go
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package run
import (
"fmt"
"path/filepath"
"github.com/hashicorp/go-hclog"
"github.com/vercel/turbo/cli/internal/env"
"github.com/vercel/turbo/cli/internal/fs"
"github.com/vercel/turbo/cli/internal/globby"
"github.com/vercel/turbo/cli/internal/hashing"
"github.com/vercel/turbo/cli/internal/lockfile"
"github.com/vercel/turbo/cli/internal/packagemanager"
"github.com/vercel/turbo/cli/internal/turbopath"
"github.com/vercel/turbo/cli/internal/util"
)
const _globalCacheKey = "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo"
// Variables that we always include
var _defaultEnvVars = []string{
"VERCEL_ANALYTICS_ID",
}
// GlobalHashableInputs represents all the things that we use to create the global hash
type GlobalHashableInputs struct {
globalFileHashMap map[turbopath.AnchoredUnixPath]string
rootExternalDepsHash string
envVars env.DetailedMap
globalCacheKey string
envVarPassthroughs []string
envMode util.EnvMode
frameworkInference bool
}
type newGlobalHashable struct {
globalFileHashMap map[turbopath.AnchoredUnixPath]string
rootExternalDepsHash string
envVars env.EnvironmentVariablePairs
globalCacheKey string
envVarPassthroughs []string
envMode util.EnvMode
frameworkInference bool
}
// newGlobalHash is a transformation of GlobalHashableInputs.
// It's used for the situations where we have an `EnvMode` specified
// as that is not compatible with existing global hashes.
func newGlobalHash(full GlobalHashableInputs) (string, error) {
return fs.HashObject(newGlobalHashable{
globalFileHashMap: full.globalFileHashMap,
rootExternalDepsHash: full.rootExternalDepsHash,
envVars: full.envVars.All.ToHashable(),
globalCacheKey: full.globalCacheKey,
envVarPassthroughs: full.envVarPassthroughs,
envMode: full.envMode,
frameworkInference: full.frameworkInference,
})
}
type oldGlobalHashable struct {
globalFileHashMap map[turbopath.AnchoredUnixPath]string
rootExternalDepsHash string
envVars env.EnvironmentVariablePairs
globalCacheKey string
}
// oldGlobalHash is a transformation of GlobalHashableInputs.
// This exists because the existing global hashes are still usable
// in some configurations that do not include a specified `EnvMode`.
// We can remove this whenever we want to migrate users.
func oldGlobalHash(full GlobalHashableInputs) (string, error) {
return fs.HashObject(oldGlobalHashable{
globalFileHashMap: full.globalFileHashMap,
rootExternalDepsHash: full.rootExternalDepsHash,
envVars: full.envVars.All.ToHashable(),
globalCacheKey: full.globalCacheKey,
})
}
// calculateGlobalHashFromHashableInputs returns a hash string from the GlobalHashableInputs
func calculateGlobalHashFromHashableInputs(full GlobalHashableInputs) (string, error) {
switch full.envMode {
case util.Infer:
if full.envVarPassthroughs != nil {
// In infer mode, if there is any passThru config (even if it is an empty array)
// we'll hash the whole object, so we can detect changes to that config
// Further, resolve the envMode to the concrete value.
full.envMode = util.Strict
return newGlobalHash(full)
}
// If you tell us not to infer framework you get the new hash.
if !full.frameworkInference {
return newGlobalHash(full)
}
// If we're in infer mode, and there is no global pass through config,
// we use the old struct layout. this will be true for everyone not using the strict env
// feature, and we don't want to break their cache.
return oldGlobalHash(full)
case util.Loose:
// Remove the passthroughs from hash consideration if we're explicitly loose.
full.envVarPassthroughs = nil
return newGlobalHash(full)
case util.Strict:
// Collapse `nil` and `[]` in strict mode.
if full.envVarPassthroughs == nil {
full.envVarPassthroughs = make([]string, 0)
}
return newGlobalHash(full)
default:
panic("unimplemented environment mode")
}
}
func getGlobalHashInputs(
rootpath turbopath.AbsoluteSystemPath,
rootPackageJSON *fs.PackageJSON,
envVarDependencies []string,
globalFileDependencies []string,
packageManager *packagemanager.PackageManager,
lockFile lockfile.Lockfile,
envVarPassthroughs []string,
envMode util.EnvMode,
frameworkInference bool,
logger hclog.Logger,
) (GlobalHashableInputs, error) {
// Calculate env var dependencies
envVars := []string{}
envVars = append(envVars, envVarDependencies...)
envVars = append(envVars, _defaultEnvVars...)
globalHashableEnvVars, err := env.GetHashableEnvVars(envVars, nil, "")
if err != nil {
return GlobalHashableInputs{}, err
}
logger.Debug("global hash env vars", "vars", globalHashableEnvVars.All.Names())
// Calculate global file dependencies
globalDeps := make(util.Set)
if len(globalFileDependencies) > 0 {
ignores, err := packageManager.GetWorkspaceIgnores(rootpath)
if err != nil {
return GlobalHashableInputs{}, err
}
f, err := globby.GlobFiles(rootpath.ToStringDuringMigration(), globalFileDependencies, ignores)
if err != nil {
return GlobalHashableInputs{}, err
}
for _, val := range f {
globalDeps.Add(val)
}
}
if lockFile == nil {
// If we don't have lockfile information available, add the specfile and lockfile to global deps
globalDeps.Add(filepath.Join(rootpath.ToStringDuringMigration(), packageManager.Specfile))
globalDeps.Add(filepath.Join(rootpath.ToStringDuringMigration(), packageManager.Lockfile))
}
// No prefix, global deps already have full paths
globalDepsArray := globalDeps.UnsafeListOfStrings()
globalDepsPaths := make([]turbopath.AnchoredSystemPath, len(globalDepsArray))
for i, path := range globalDepsArray {
fullyQualifiedPath := turbopath.AbsoluteSystemPathFromUpstream(path)
anchoredPath, err := fullyQualifiedPath.RelativeTo(rootpath)
if err != nil {
return GlobalHashableInputs{}, err
}
globalDepsPaths[i] = anchoredPath
}
globalFileHashMap, err := hashing.GetHashesForFiles(rootpath, globalDepsPaths)
if err != nil {
return GlobalHashableInputs{}, fmt.Errorf("error hashing files: %w", err)
}
return GlobalHashableInputs{
globalFileHashMap: globalFileHashMap,
rootExternalDepsHash: rootPackageJSON.ExternalDepsHash,
envVars: globalHashableEnvVars,
globalCacheKey: _globalCacheKey,
envVarPassthroughs: envVarPassthroughs,
envMode: envMode,
frameworkInference: frameworkInference,
}, nil
}