Skip to content

Commit 0b7542c

Browse files
committed
Updating RFC with improved design for shareableCache.
1 parent a09bbcf commit 0b7542c

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

designs/2023-relative-cache/README.md

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ The suggested approach is to truncate the given absolute path of the file to a r
3030

3131
### Example existing cache file
3232

33-
Here is an example existing cache file for an extremely small project with two source files. You see that the full path of the file is included in the
34-
filenames, '/home/USER/git/samplecode'.
33+
Here is an example existing cache file for an extremely small project with two source files. It has been formatted for readability.
34+
35+
You can see that the full path of the file is included in the filenames, '/home/USER/git/samplecode'. There is also an obscure field, the value
36+
1k0siqs. That is the hash-value of a stringified ConfigArray object that contains all the configuration for the entire eslint run. It is there so
37+
that any rule changes or configuration changes will cause a complete eslint recheck on all files.
3538

3639
```
3740
[
@@ -87,6 +90,9 @@ filenames, '/home/USER/git/samplecode'.
8790
Here is a suggested updated cache file, with relative paths instead of full paths. The . is the directory from which
8891
eslint is run. Typically, eslint is always run from the same place, so using a relative path should not cause a problem.
8992

93+
Note that the obscure hash value is still there, and is now Ak0sovs. This hash value will need to be calculated using
94+
relative paths only.
95+
9096
```
9197
[
9298
{
@@ -115,7 +121,7 @@ eslint is run. Typically, eslint is always run from the same place, so using a r
115121
"fixableErrorCount": 0,
116122
"fixableWarningCount": 0
117123
},
118-
"1k0siqs",
124+
"Ak0sovs",
119125
{
120126
"filePath": "9",
121127
"messages": "10",
@@ -138,13 +144,24 @@ eslint is run. Typically, eslint is always run from the same place, so using a r
138144
### Adding the command line parameter
139145
- conf/default-cli-options.js: add the property 'shareableCache' with a default of false. It should be put in the section with the other cache variables, below cacheStrategy.
140146
- docs/src/use/command-line-interface.md: Add an explanation of the 'shareable-cache' variable with the other cache variables: "By default, an eslintcache contains full file paths and thus cannot readily be shared across developers or ci machines. "False" is that default. "True" changes the internal storage to store a relative path to the process execution directory, thus making the eslintcache shareable across developers and ci machines. . If you change this setting, you must regenerate your entire eslint cache."
141-
- eslint-helpers.js: add shareableCache variable (set to false) to the processOptions
147+
- eslint-helpers.js: add shareableCache variable (set to false) to the processOptions, and make sure it must be a boolean.
142148
- lib/options.js: Add an option 'shareable-cache' of type Boolean with a nice description for people to read: By default, an eslintcache contains full file paths and thus cannot readily be shared across developers or ci machines. "False" is that default. "True" changes the internal storage to store a relative path to the process execution directory, thus making the eslintcache shareable across developers and ci machines. . If you change this setting, you must regenerate your entire eslint cache.
143-
149+
- lib/cli.js: Add the shareableCache option, defaulted to false, in the translateOptions function.
144150

145151
### Changing cache file serialization
146-
- lib/eslint/flat-eslint.js: Define the shearableCache property at the top, with the other properties, as a boolean
147-
- lib/eslint/flat-eslint.js: Around line 873, where the flat-eslint.js file is saving the cache in the cachefile, if processOptions.shareableCache is set to true, pass a PARTIAL path to the lintResultCache.setCachedLintResults() function instead of the full path.
152+
- lib/cli-engine/lint-result-cache.js: Add the properties 'cwd' and 'shareableCache' to the LintResultCache class. 'cwd' is a string, the current working directory. shareableCache is a boolean, the result of the user's passed in command line parameter. Use these values in two main places:
153+
1. Whenever we store or retrieve file results in the cache, use the results of the new class function, getFileKey(filePath). If shareableCache is false, it uses a full file path as the key. If shareableCache is true, uses a relative file path.
154+
2. Update the function hashOfConfigFor, so that when the configuration object is hashed into a hash key and put into the eslintcache, if shareableCache is set to true, the file paths hashed are all relative paths. Implementation detail:
155+
```
156+
function hashOfConfigFor(config, cwd) {
157+
if (!configHashCache.has(config)) {
158+
// replace the full directory path in the config string to make the hash location-neutral
159+
const stringConfig = stringify(config).replaceAll(cwd, '.');
160+
configHashCache.set(config, hash(`${pkg.version}_${nodeVersion}_${stringConfig}`));
161+
}
162+
```
163+
- lib/cli-engine/cli-engine.js: Update the constructor call to LintResultCache to add the new parameters cwd and shareableCache.
164+
- lib/eslint/flat-eslint.js: Update the constructor call to LintResultCache to add the new parameters cwd and shareableCache.
148165

149166
## Documentation
150167

@@ -159,21 +176,35 @@ Furthermore, defaulting to the existing behavior does help make upgrading less p
159176
## Backwards Compatibility Analysis
160177

161178
The new flag, shareable-cache, passed in on the command line, will default to false. Unless people explicitly set it to true, then the old algorithms will
162-
be used. They will not need to regenerate their cache unless they set this flag.
179+
be used. Users of the eslint cache will still need to regenerate their cache no matter how they set this flag, because the current implementation forces a cache rebuild if there are
180+
any configuration changes. Adding a new property counts as a change.
163181

164182
## Alternatives
165183

166-
A CLI tool which translates the existing cache to the current folder structure. This could be any sort of script that will basically do a replace-all on the paths in the .eslintcache file so that they are the new location rather than the old. A downside to this is that any tool based on string replacement would be fragile at best.
184+
A CLI tool which translates the existing cache to the current folder structure. This could be any sort of script that will basically do a replace-all on the paths in
185+
the .eslintcache file and also recalculates the hash so that they are the new location rather than the old. A downside to this is that any tool based on string
186+
replacement would be fragile at best.
167187

168188
## Open Questions
169189

170190
No.
171191

172192
## Help Needed
173193

174-
I have a branch with code changes, but I am trying to figure out how to build an artifact to test with. Using the eslint.js artifact in the browser does not make sense
175-
with a cache change, and I would like to test it in my project. How can I build a package that I can install with NPM into my project to test?? I am able to run the tests
176-
on the eslintproject and they all pass. My fork is here: https://github.com/cs6cs6/eslint_patch/tree/fork/shareable-eslintcache
194+
Decision needed: Make every cache shareable, or keep the shreable-cache flag default to false?
195+
196+
I originally planned the shareable-cache flag so that users of eslint could not be surprised by changed cache behavior, and would also not have to regenerate their cache
197+
for a patch version change. But I discovered by testing that there is no way to NOT force people to regenerate their eslint cache with this change. That's because by adding
198+
the 'shareable-cache' flag, it adds a property to the ConfigArray object. Even if it's set to false (the old behavior), the object's structure changes with a new property
199+
field. This in turn changes the ConfigArray object's stringify results, which are fed into a hash function to create a hash key. (See lint-result-cache.js function hashOfConfigFor.)
200+
In turn, that becomes a part of the eslintcache file. I think it's so that everything will be re-scanned with configuration changes.
201+
202+
For that reason, should we simply have a shareable cache full of relative paths be the only behavior? That means no command line flag, and strictly internal changes that
203+
would be invisible to the end user. On the other hand, if this change DOES have undesirable side effects because people are using eslint in unexpected ways, having the flag
204+
default to the old behavior would save them from surprises on upgrade. Judging by the number of .gitignore files with .eslintcache in them, people are used to this
205+
cache not being shareable.
206+
207+
What would the eslint team prefer? No flag, or keep the shareable-cache flag and default to false?
177208

178209
## Frequently Asked Questions
179210

0 commit comments

Comments
 (0)