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

Allow specifying cache outputs outside of package folder #861

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 2 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions cli/internal/cache/cache_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cache
import (
"encoding/json"
"fmt"
"runtime"
"path/filepath"
"io/ioutil"
"path/filepath"
"runtime"

"github.com/vercel/turborepo/cli/internal/analytics"
"github.com/vercel/turborepo/cli/internal/config"
"github.com/vercel/turborepo/cli/internal/fs"
Expand Down Expand Up @@ -67,22 +68,18 @@ func (f *fsCache) logFetch(hit bool, hash string, duration int) {
func (f *fsCache) Put(target, hash string, duration int, files []string) error {
g := new(errgroup.Group)

numDigesters := runtime.NumCPU()
numDigesters := runtime.NumCPU()
fileQueue := make(chan string, numDigesters)

for i := 0; i < numDigesters; i++ {
g.Go(func() error {
for file := range fileQueue {
rel, err := filepath.Rel(target, file)
if err != nil {
return fmt.Errorf("error constructing relative path from %v to %v: %w", target, file, err)
}
if !fs.IsDirectory(file) {
if err := fs.EnsureDir(filepath.Join(f.cacheDirectory, hash, rel)); err != nil {
if err := fs.EnsureDir(filepath.Join(f.cacheDirectory, hash, file)); err != nil {
return fmt.Errorf("error ensuring directory file from cache: %w", err)
}

if err := fs.CopyOrLinkFile(file, filepath.Join(f.cacheDirectory, hash, rel), fs.DirPermissions, fs.DirPermissions, true, true); err != nil {
if err := fs.CopyOrLinkFile(file, filepath.Join(f.cacheDirectory, hash, file), fs.DirPermissions, fs.DirPermissions, true, true); err != nil {
return fmt.Errorf("error copying file from cache: %w", err)
}
}
Expand Down
6 changes: 3 additions & 3 deletions cli/internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"golang.org/x/sync/errgroup"
)

const GLOBAL_CACHE_KEY = "snozzberries"
const GLOBAL_CACHE_KEY = "snozzberries are delicious"

// Context of the CLI
type Context struct {
Expand Down Expand Up @@ -106,8 +106,8 @@ func isWorkspaceReference(packageVersion string, dependencyVersion string, cwd s
// Other protocols are assumed to be external references ("github:", "link:", "file:" etc)
return false
} else if dependencyVersion == "*" {
return true
}
return true
}

// If we got this far, then we need to check the workspace package version to see it satisfies
// the dependencies range to determin whether or not its an internal or external dependency.
Expand Down
2 changes: 1 addition & 1 deletion cli/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func (c *RunCommand) runOperation(g *completeGraph, rs *runSpec, backend *api.La
// Cache ---------------------------------------------
var hit bool
if !rs.Opts.forceExecution {
hit, _, _, err = turboCache.Fetch(pack.Dir, hash, nil)
hit, _, _, err = turboCache.Fetch(rs.Opts.cwd, hash, nil)
if err != nil {
targetUi.Error(fmt.Sprintf("error fetching from cache: %s", err))
} else if hit {
Expand Down
4 changes: 3 additions & 1 deletion cli/scripts/e2e/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function runSmokeTests<T>(
assert.ok(!!hash, "No hash for c#test");
const cachedLogFilePath = getCachedLogFilePathForTask(
getCachedDirForHash(repo, hash),
path.join("packages", "c"),
"test"
);
let text = "";
Expand Down Expand Up @@ -334,7 +335,8 @@ function getCachedDirForHash(repo: Monorepo, hash: string): string {

function getCachedLogFilePathForTask(
cacheDir: string,
pathToPackage: string,
taskName: string
): string {
return path.join(cacheDir, ".turbo", `turbo-${taskName}.log`);
return path.join(cacheDir, pathToPackage, ".turbo", `turbo-${taskName}.log`);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@gsoltis is this PR and change going to work us into a weird hole in the future? im thinking that we lose flexibility by storing cache items with their directory structure, but maybe not

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess moving a package will cause a cache miss. That seems fine.

I think we'll always know the path to the package, there's no notion of running turbo in a subtree of the repo without knowing anything about the root of the repo.

We should probably validate that the outputs are inside the monorepo if they are no longer going to be inside the corresponding tasks's package. I don't think we would want a future cache hit to make changes outside of the monorepo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True but do we really care about outside of monorepo use case enough to warn against it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose "just don't do that" is ok.

}