-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add path types for cache. #2059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@nathanhammond is attempting to deploy a commit to the Vercel Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A guide to the non-mechanical changes.
|
||
// If it's not in the cache bail now | ||
if !fs.PathExists(cachedFolder) { | ||
if !cachedFolder.DirExists() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have always been a DirExists
check, not a PathExists
check.
|
||
if !fs.PathExists(cachedFolder) { | ||
if !cachedFolder.DirExists() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have always been a DirExists
check, not a PathExists
check.
return ok, actualFiles, duration, err | ||
} | ||
} | ||
return false, files, 0, nil | ||
|
||
return false, nil, 0, nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With ok
being false, we should be returning nil
instead of the input list of files
.
type testingUtil interface { | ||
Helper() | ||
Cleanup(f func()) | ||
} | ||
|
||
// subdirForTest creates a sub directory of `cwd` and registers it for | ||
// deletion by the testing framework at the end of the test. | ||
// Some cache code currently assumes relative paths from `cwd`, so it is not | ||
// yet feasible to use temp directories. | ||
func subdirForTest(t *testing.T) string { | ||
var tt interface{} = t | ||
if tu, ok := tt.(testingUtil); ok { | ||
tu.Helper() | ||
} | ||
cwd, err := fs.GetCwd() | ||
assert.NilError(t, err, "cwd") | ||
dir, err := os.MkdirTemp(cwd.ToString(), "turbo-test") | ||
assert.NilError(t, err, "MkdirTemp") | ||
deleteOnFinish(t, dir) | ||
return filepath.Base(dir) | ||
} | ||
|
||
func deleteOnFinish(t *testing.T, dir string) { | ||
var tt interface{} = t | ||
if tu, ok := tt.(testingUtil); ok { | ||
tu.Cleanup(func() { _ = os.RemoveAll(dir) }) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Directories are now specified by the first argument to Put
and Fetch
.
files = append(files, hdr.Name) | ||
filename := root.UntypedJoin(hdr.Name) | ||
// FIXME: THIS IS A BUG. | ||
restoredName := turbopath.AnchoredUnixPath(hdr.Name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are cases where this is a bug. This will be addressed in the cache-to-tar portion.
filename := root.UntypedJoin(hdr.Name) | ||
// FIXME: THIS IS A BUG. | ||
restoredName := turbopath.AnchoredUnixPath(hdr.Name) | ||
files = append(files, restoredName.ToSystemPath()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is conversion can do the wrong thing with \
.
// FIXME: THIS IS A BUG. | ||
restoredName := turbopath.AnchoredUnixPath(hdr.Name) | ||
files = append(files, restoredName.ToSystemPath()) | ||
filename := restoredName.ToSystemPath().RestoreAnchor(root) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra-verbose for now, will get cleaned up in cache-to-tar.
for _, file := range files { | ||
gotSet.Add(file.ToString()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Files are no longer strings! 🎉
@@ -283,10 +284,10 @@ func (tc TaskCache) SaveOutputs(ctx context.Context, logger hclog.Logger, termin | |||
terminal.Error(fmt.Sprintf("%s%s", ui.ERROR_PREFIX, color.RedString(" %v", fmt.Errorf("File path cannot be made relative: %w", err)))) | |||
continue | |||
} | |||
relativePaths[index] = relativePath | |||
relativePaths[index] = fs.UnsafeToAnchoredSystemPath(relativePath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a known value at this point, but I think this whole loop should be cleaned up. We're doing too much work here.
} | ||
|
||
if err = tc.rc.cache.Put(tc.pt.Pkg.Dir.ToStringDuringMigration(), tc.hash, duration, relativePaths); err != nil { | ||
if err = tc.rc.cache.Put(tc.rc.repoRoot, tc.hash, duration, relativePaths); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a material change, but previously the value passed in here was unused.
It now uses this as the anchor for the relativePaths
slice that is getting passed in here and prevents us from depending on cwd
.
} | ||
files, err := restoreTar(root, tar) | ||
assert.NilError(t, err, "readTar") | ||
|
||
expectedSet := util.SetFromStrings(expectedFiles) | ||
expectedSet := make(util.Set) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously restoreTar
was always returning Unix-separated paths, even on Windows. However, downstream uses were treating these as system-separated paths. Likely didn't manifest in user-facing issues because Windows supports /
as a separator, but no guarantees.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
0269228
to
05974dc
Compare
@@ -76,7 +76,7 @@ func (c *asyncCache) Shutdown() { | |||
// run implements the actual async logic. | |||
func (c *asyncCache) run() { | |||
for r := range c.requests { | |||
c.realCache.Put(r.anchor, r.key, r.duration, r.files) | |||
_ = c.realCache.Put(r.anchor, r.key, r.duration, r.files) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm doing this to silence the lint error. It doesn't make sense unless we have an error group here and that's something that cache-to-tar will obviate the need for.
cli/internal/cache/cache_fs_test.go
Outdated
src := filepath.Join(cacheDir, "the-hash", "some-package") | ||
err = os.MkdirAll(src, os.ModeDir|0777) | ||
// cwd, err := fs.GetCwd() | ||
// assert.NilError(t, err, "GetCwd") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete commented-out code
src := subdirForTest(t) | ||
childDir := filepath.Join(src, "child") | ||
err := os.Mkdir(childDir, os.ModeDir|0777) | ||
src := turbopath.AbsoluteSystemPath(t.TempDir()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not blocking, but I'm really not a fan of casting for getting an instance of a path type:
- it's hard to grep / find-usages for
- they can be auto-inserted by the IDE
- they don't communicate anything about why this is ok.
I would expect this to be something likeAbsoluteSystemPathFromUpstream
, which communicates that the author has verified that wherever the string is coming from produces absolute paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "rule" I've been following is that I've only been doing type casts in test code, and only on initialization from "raw" values (string primitives, or calls into stdlib). You'll see I didn't do casts in any application code or even in any of the more-hairy test code.
I don't feel strongly either way, so I'll defer to your opinion on this and get the casts swapped out before landing Monday.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Followup PR: #2077
05974dc
to
1f53362
Compare
As prework for merging cache-to-tar, this ensures that we know what types of paths we're dealing with at (almost) every step in the cache process.
It also makes the first argument to
Put
(previously unused) andFetch
the anchor of the enumerated list of files being saved or restored.