-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Conversation
@nathanhammond is attempting to deploy a commit to the Vercel Team on Vercel. A member of the Team first needs to authorize it. |
// 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() | ||
} |
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.
These are duplicated utils from fs/fs.go
(with the caveat of being unexported). They will be cleaned up in a followup PR.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
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.
These changes look good, but what's the difference between turbopath.AbsolutePath
to turbopath.AbsoluteSystemPath
There isn't one. That's next step. |
f730a37
to
dc93689
Compare
This PR does a few things:
fs.AbsolutePath
toturbopath.AbsolutePath
.fs
intoturbopath/absolute_path.go
to prevent cycles. These will be removed in a followup 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
toturbopath.AbsoluteSystemPath
by combining the files and fixing the expectations of method calls throughout the codebase.