这是indexloc提供的服务,不要输入任何密码
Skip to content

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

Merged
merged 15 commits into from
Sep 26, 2022
Merged

Conversation

nathanhammond
Copy link
Contributor

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) and Fetch the anchor of the enumerated list of files being saved or restored.

@vercel
Copy link

vercel bot commented Sep 23, 2022

@nathanhammond is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

@nathanhammond nathanhammond requested review from gaspar09 and gsoltis and removed request for tknickman and NicholasLYang September 23, 2022 08:50
Copy link
Contributor Author

@nathanhammond nathanhammond left a 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() {
Copy link
Contributor Author

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() {
Copy link
Contributor Author

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
Copy link
Contributor Author

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.

Comment on lines -18 to -46
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) })
}
}

Copy link
Contributor Author

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)
Copy link
Contributor Author

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())
Copy link
Contributor Author

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)
Copy link
Contributor Author

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.

Comment on lines +193 to +199
for _, file := range files {
gotSet.Add(file.ToString())
}
Copy link
Contributor Author

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)
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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)
Copy link
Contributor Author

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.

@vercel
Copy link

vercel bot commented Sep 23, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
turbo-site ✅ Ready (Inspect) Visit Preview Sep 26, 2022 at 7:31AM (UTC)

@@ -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)
Copy link
Contributor Author

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.

@gsoltis gsoltis added the pr: fixship A PR which is approved with notes and does not require re-review. label Sep 23, 2022
src := filepath.Join(cacheDir, "the-hash", "some-package")
err = os.MkdirAll(src, os.ModeDir|0777)
// cwd, err := fs.GetCwd()
// assert.NilError(t, err, "GetCwd")
Copy link
Contributor

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())
Copy link
Contributor

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 like AbsoluteSystemPathFromUpstream, which communicates that the author has verified that wherever the string is coming from produces absolute paths.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're going to make this a rule we should write a custom linter that disallows all casts into turbopath that aren't from a string:
Screen Shot 2022-09-26 at 1 46 57 PM

(Splitting this change into a separate PR.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followup PR: #2077

@nathanhammond nathanhammond force-pushed the cache-dir-absolute-path branch from 05974dc to 1f53362 Compare September 26, 2022 05:33
@nathanhammond nathanhammond added pr: automerge Kodiak will merge these automatically after checks pass and removed pr: fixship A PR which is approved with notes and does not require re-review. labels Sep 26, 2022
@kodiakhq kodiakhq bot merged commit 45ff205 into vercel:main Sep 26, 2022
@nathanhammond nathanhammond deleted the cache-dir-absolute-path branch September 26, 2022 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: automerge Kodiak will merge these automatically after checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants