You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a cache layer for target-determinator results. The cache key is
based on the digest of the git tree object among other fields.
The configured target metadata is not stored in the cache because they
take a lot space and are not needed except for `-verbose` mode. The
cache is not read from (but results are saved) if `-verbose` is set.
One can disable the loading and saving to cache with
`--nocache_results`.
Other changes:
- Add a --cache-dir option to customise the path of worktree caching and
results caching.
- The path to the worktree cache is now in the "worktrees"
sub-directory. Results are under "results" subdirectory.
Copy file name to clipboardExpand all lines: README.md
+63-8Lines changed: 63 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,19 +7,48 @@ Target determinator is a binary (and Go API) used to determine which Bazel targe
7
7
For simple listing, the `target-determinator` binary is supplied:
8
8
9
9
```
10
-
Usage of target-determinator:
11
-
target-determinator <before-revision>
12
-
Where <before-revision> may be any commit revision - full commit hashes, short commit hashes, tags, branches, etc.
10
+
Usage of bazel-bin/target-determinator/target-determinator_/target-determinator:
11
+
-analysis-cache-clear-strategy string
12
+
Strategy for clearing the analysis cache. Accepted values: skip,shutdown,discard. (default "skip")
13
13
-bazel string
14
-
Bazel binary (basename on $PATH, or absolute or relative path) to run (default "bazel")
14
+
Bazel binary (basename on $PATH, or absolute or relative path) to run. (default "bazel")
15
+
-bazel-opts value
16
+
Options to pass to Bazel. Assumed to apply to build and cquery. Options should use relative paths for repository
17
+
files (see --bazel-startup-opts).
18
+
-bazel-startup-opts value
19
+
Startup options to pass to Bazel. Options such as '--bazelrc' should use relative paths for files under the
20
+
repository to avoid issues (TD may check out the repository in a temporary directory).
21
+
-before-query-error-behavior string
22
+
How to behave if the 'before' revision query fails. Accepted values: fatal,ignore-and-build-all (default
23
+
"ignore-and-build-all")
24
+
-cache-dir string
25
+
Cache directory to avoid existing re-computations. Note: home- and system- bazelrc files, environment variables,
26
+
and host hardware/OS are not included in the results cache key. Use --nocache_results if necessary. (default
27
+
"/Users/rchossart/.cache/target-determinator")
28
+
-compare-queries-around-analysis-cache-clear
29
+
Whether to check for query result differences before and after analysis cache clears. This is a temporary flag
30
+
for performing real-world analysis.
31
+
-delete-cached-worktree
32
+
Delete created worktrees after use when created. Keeping them can make subsequent invocations faster.
33
+
-enforce-clean value
34
+
Pass --enforce-clean=enforce-clean to fail if the repository is unclean, or --enforce-clean=allow-ignored to
35
+
allow ignored untracked files (the default). (default allow-ignored)
36
+
-filter-incompatible-targets
37
+
Whether to filter out incompatible targets from the candidate set of affected targets. (default true)
15
38
-ignore-file value
16
-
Files to ignore for git operations, relative to the working-directory. These files shan't affect the Bazel graph.
39
+
Files to ignore for git operations, relative to the working-directory. These files shan't affect the Bazel
40
+
graph.
41
+
-nocache_results
42
+
Disable loading and saving of results to the cache.
17
43
-targets bazel query
18
-
Targets to consider. Accepts any valid bazel query expression (see https://bazel.build/reference/query). (default "//...")
44
+
Targets to consider. Accepts any valid bazel query expression (see https://bazel.build/reference/query).
45
+
(default "//...")
19
46
-verbose
20
-
Whether to explain (messily) why each target is getting run
47
+
Whether to explain (messily) why each target is getting run
48
+
-version
49
+
Print the version of the tool and exit.
21
50
-working-directory string
22
-
Working directory to query (default ".")
51
+
Working directory to query. (default ".")
23
52
```
24
53
25
54
This binary lists targets to stdout, one-per-line, which were affected between <before-revision> and the currently checked-out revision.
@@ -61,6 +90,32 @@ type WalkCallback func(label.Label, []Difference, *analysis.ConfiguredTarget)
61
90
62
91
This can be used to flexibly build your own logic handling the affected targets to drive whatever analysis you want.
63
92
93
+
## Caching
94
+
95
+
Target Determinator caches the results of Bazel cquery invocations across runs. On a cache hit, the expensive cquery and hashing work for a given commit is skipped entirely.
96
+
97
+
The cache key is derived from:
98
+
99
+
- The target-determinator binary itself (SHA-256 hash)
100
+
- The Bazel version (`bazel info release`)
101
+
- The git tree SHA of the queried commit
102
+
- The target pattern (e.g. `//...`)
103
+
- CLI options that may affect cquery results, such as `--filter-incompatible-targets` and the Bazel startup/build options passed via `--bazel-startup-opts` / `--bazel-opts`
104
+
105
+
*Not* included in the cache key:
106
+
107
+
- User and system bazelrc files (`~/.bazelrc`, `/etc/bazel.bazelrc`, and files they import)
108
+
- The host machine (hardware, OS). Cache entries produced on one machine are not guaranteed to be valid on another (e.g. a different CPU architecture can change which platform-constrained targets are selected). Do not share the cache directory across machines.
109
+
- Environment variables, whether they are used by Bazel or not.
110
+
111
+
### Environment variables and caching
112
+
113
+
Without caching, the "before" and "after" cquery calls are both made with the same environment variables. Taken in the context of a CI pipeline run, for example, this means that even the "before" computation uses the *current* (or "after") environment variables, not the environment variables that existed when the "before" commit was built. That answers the question "what targets differ between these two commits, assuming the environment was the same?".
114
+
115
+
With caching, however, the "before" result may have been computed in an earlier pipeline run, under the environment variables that were in effect *at that time*. If an environment variable affected Bazel's query output (e.g. because it is referenced by `--workspace_status`, `--action_env`, `--test_env`, or a repo rule), the cached result reflects the old environment, while the "after" result reflects the new one. The two results are then compared under different conditions, which may produce spurious differences.
116
+
117
+
In practice this matters most in release pipelines where stamping or versioning variables (e.g. `MY_PKG_VERSION`) change between runs. If you want to answer "which targets would have changed, assuming the environment is the same before and after?", run `target-determinator` with `--nocache_results` to force both computations to happen in the same environment.
118
+
64
119
## How to get Target Determinator
65
120
66
121
Pre-built binary releases are published as [GitHub Releases](https://github.com/bazel-contrib/target-determinator/releases) for most changes.
flag.StringVar(commonFlags.AnalysisCacheClearStrategy, "analysis-cache-clear-strategy", "skip", "Strategy for clearing the analysis cache. Accepted values: skip,shutdown,discard.")
122
128
flag.BoolVar(&commonFlags.CompareQueriesAroundAnalysisCacheClear, "compare-queries-around-analysis-cache-clear", false, "Whether to check for query result differences before and after analysis cache clears. This is a temporary flag for performing real-world analysis.")
123
129
flag.BoolVar(&commonFlags.FilterIncompatibleTargets, "filter-incompatible-targets", true, "Whether to filter out incompatible targets from the candidate set of affected targets.")
130
+
flag.StringVar(commonFlags.CacheDirectory, "cache-dir", defaultCacheDir(), "Cache directory to avoid existing re-computations. Note: home- and system- bazelrc files, environment variables, and host hardware/OS are not included in the results cache key. Use --nocache_results if necessary.")
131
+
flag.BoolVar(&commonFlags.NoCacheResults, "nocache_results", false, "Disable loading and saving of results to the cache.")
124
132
return&commonFlags
125
133
}
126
134
135
+
funcdefaultCacheDir() string {
136
+
homeDir, err:=os.UserHomeDir()
137
+
iferr!=nil {
138
+
log.Printf("failed to determine home dir: %v. Caching will be disabled.", err)
0 commit comments