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

Move fs.AbsolutePath to turbopath.AbsolutePath #1779

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 3 commits into from
Aug 31, 2022

Conversation

nathanhammond
Copy link
Contributor

This PR does a few things:

  1. Moves fs.AbsolutePath to turbopath.AbsolutePath.
  2. (temporarily) Duplicates some utils and constants from fs into turbopath/absolute_path.go to prevent cycles. These will be removed in a followup commit.
  3. (in a separate commit) \b(fs\.)?AbsolutePath\b => turbopath.AbsolutePath. (excluding the turbopath folder)

The goal here is to make what are sweeping changes to the codebase still feel as incremental as possible. The next increment is turbopath.AbsolutePath to turbopath.AbsoluteSystemPath by combining the files and fixing the expectations of method calls throughout the codebase.

@nathanhammond nathanhammond requested a review from a team as a code owner August 26, 2022 08:45
@vercel
Copy link

vercel bot commented Aug 26, 2022

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

A member of the Team first needs to authorize it.

Comment on lines +11 to +52
// dirPermissions are the default permission bits we apply to directories.
const dirPermissions = os.ModeDir | 0775

// ensureDir ensures that the directory of the given file has been created.
func ensureDir(filename string) error {
dir := filepath.Dir(filename)
err := os.MkdirAll(dir, dirPermissions)
if err != nil && fileExists(dir) {
// It looks like this is a file and not a directory. Attempt to remove it; this can
// happen in some cases if you change a rule from outputting a file to a directory.
log.Printf("Attempting to remove file %s; a subdirectory is required", dir)
if err2 := os.Remove(dir); err2 == nil {
err = os.MkdirAll(dir, dirPermissions)
} else {
return err
}
}
return err
}

var nonRelativeSentinel string = ".." + string(filepath.Separator)

// dirContainsPath returns true if the path 'target' is contained within 'dir'
// Expects both paths to be absolute and does not verify that either path exists.
func dirContainsPath(dir string, target string) (bool, error) {
// In Go, filepath.Rel can return a path that starts with "../" or equivalent.
// Checking filesystem-level contains can get extremely complicated
// (see https://github.com/golang/dep/blob/f13583b555deaa6742f141a9c1185af947720d60/internal/fs/fs.go#L33)
// As a compromise, rely on the stdlib to generate a relative path and then check
// if the first step is "../".
rel, err := filepath.Rel(dir, target)
if err != nil {
return false, err
}
return !strings.HasPrefix(rel, nonRelativeSentinel), nil
}

// fileExists returns true if the given path exists and is a file.
func fileExists(filename string) bool {
info, err := os.Lstat(filename)
return err == nil && !info.IsDir()
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are duplicated utils from fs/fs.go (with the caveat of being unexported). They will be cleaned up in a followup PR.

@vercel
Copy link

vercel bot commented Aug 26, 2022

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

Name Status Preview Updated
turbo-site ✅ Ready (Inspect) Visit Preview Aug 31, 2022 at 10:46AM (UTC)

Copy link
Contributor

@mehulkar mehulkar left a comment

Choose a reason for hiding this comment

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

These changes look good, but what's the difference between turbopath.AbsolutePath to turbopath.AbsoluteSystemPath

@nathanhammond
Copy link
Contributor Author

These changes look good, but what's the difference between turbopath.AbsolutePath to turbopath.AbsoluteSystemPath

There isn't one. That's next step.

@nathanhammond nathanhammond reopened this Aug 31, 2022
@nathanhammond nathanhammond added the pr: automerge Kodiak will merge these automatically after checks pass label Aug 31, 2022
@kodiakhq kodiakhq bot merged commit 69bef74 into vercel:main Aug 31, 2022
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.

3 participants