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

fix(prune) respect dir permissions during prune #2066

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 7 commits into from
Sep 26, 2022

Conversation

chris-olszewski
Copy link
Member

Fixes #1872

This PR ended up having larger scope than I originally intended. A few things that changed:

  • RecursiveCopyFile now attempts to copy directories with the correct permissions
  • Added MkdirAllMode which is similar to MkdirAll but it will ensure that the created directory has the permissions passed
  • Change prune to use MkdirAllMode instead of EnsureDir

Interaction with umask

Currently we don't consider umask when creating files. When copying files that we get from users we should make sure that the permissions are kept, but when creating files for turbo itself, we should probably respect umask.

When copying user files and we want to make sure permissions remain the same, the standard solution is to chmod after creating a new dir/file. This would add an additional syscall to all of the file/directory copy methods which seems like a little much. Another option would be to blow away umask at the beginning to ensure that we are creating files with the permissions we expect.

Testing

Wrote some unit tests, also did a quick spot check that dir permissions are copied over

olszewski@chriss-mbp perm-issue % turbo_dev prune --scope=ui                                 
Generating pruned monorepo for ui in /private/tmp/perm-issue/out
 - Added ui
 - Added eslint-config-custom
 - Added tsconfig
olszewski@chriss-mbp perm-issue % ls -lh packages 
total 0
drwxr-xr-x  5 olszewski  wheel   160B Sep 22 10:43 eslint-config-custom
drwxr-xr-x  7 olszewski  wheel   224B Sep 22 10:43 tsconfig
drwxrwxrwx  7 olszewski  wheel   224B Sep 22 10:43 ui
olszewski@chriss-mbp perm-issue % ls -lh out/packages
total 0
drwxr-xr-x  5 olszewski  wheel   160B Sep 23 11:34 eslint-config-custom
drwxr-xr-x  7 olszewski  wheel   224B Sep 23 11:34 tsconfig
drwxrwxrwx  7 olszewski  wheel   224B Sep 23 11:34 ui

@vercel
Copy link

vercel bot commented Sep 23, 2022

@chris-olszewski is attempting to deploy a commit to the Vercel Team on Vercel.

A member of the Team first needs to authorize it.

@chris-olszewski chris-olszewski marked this pull request as ready for review September 23, 2022 19:14
@chris-olszewski chris-olszewski requested a review from a team as a code owner September 23, 2022 19:14
@gsoltis gsoltis added the pr: fixship A PR which is approved with notes and does not require re-review. label Sep 23, 2022
if err != nil {
return err
}
return os.MkdirAll(dest, mode)
}
// name is absolute, (originates from godirwalk)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this comment should move up to where src is allocated.

@@ -146,10 +146,16 @@ func (p *prune) prune(opts *opts) error {
continue
}
workspaces = append(workspaces, ctx.PackageInfos[internalDep].Dir)
originalDir := p.base.RepoRoot.UntypedJoin(ctx.PackageInfos[internalDep].Dir.ToStringDuringMigration())
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is supposed to be ctx.PackageInfos[internalDep].Dir.RestoreAnchor(p.base.RepoRoot)?

@nathanhammond what do you think about also including a method on AbsoluteSystemPath that is essentially Join but accepts a single AnchoredSystemPath instance?

At any rate, this shouldn't be Untyped, we have the types here for both pieces of the path.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, thanks for pointing that out. I missed RestoreAnchor

Copy link
Contributor

Choose a reason for hiding this comment

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

@gsoltis @chris-olszewski I'm game for that method; I just couldn't find a name for it that I was happy with so I didn't create it.

I agree that it isn't intuitive to start with the Anchored path and prepend the Absolute one.

targetDir := fullDir.UntypedJoin(ctx.PackageInfos[internalDep].Dir.ToStringDuringMigration())
if err := targetDir.EnsureDir(); err != nil {
return errors.Wrapf(err, "failed to create folder %v for %v", targetDir, internalDep)
if err = targetDir.MkdirAllMode(info.Mode()); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

One advantage of this particular pattern is to scope a new err. By using if err := ..., you shadow any existing err, and don't leak this err beyond the if block. It's a common enough pattern that I would say you probably don't want to use it if you aren't using :=, or at least not without a comment explaining why.

In this case, you aren't using the value of err beyond this block so I would switch to :=

return os.Chmod(p.ToString(), mode)
} else {
// Path exists as file, remove it
if err = p.Remove(); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

:=

}
}
}
if err = os.MkdirAll(p.ToString(), mode); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

:=

@chris-olszewski
Copy link
Member Author

Just notice this part of os.Chmod:

On Windows, only the 0200 bit (owner writable) of mode is used; it controls whether the file's read-only attribute is set or cleared. The other bits are currently unused.

Which explains the failing windows tests

@chris-olszewski chris-olszewski 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
@chris-olszewski chris-olszewski merged commit d5c18b6 into vercel:main Sep 26, 2022
@chris-olszewski chris-olszewski deleted the fix_prune_directory_mode branch September 26, 2022 18:32
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.

turbo --prune --scope=[app] doesn't preserve directory file permissions
4 participants