Skip to content
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

Move code around to declutter the src/python/pants directory #11458

Merged
merged 4 commits into from
Jan 14, 2021

Conversation

gshuflin
Copy link
Contributor

@gshuflin gshuflin commented Jan 13, 2021

  • remove clean_global_runtime_state in lieu of calling its single line at its single callsite
  • Move lock.py into the pantsd directory, since it is only ever called there. This allows src/python/pants/process to be removed.
  • Move is_child_of and its associated test to its single callsite. This allows src/python/pants/fs to be removed.

Copy link
Contributor

@Eric-Arellano Eric-Arellano left a comment

Choose a reason for hiding this comment

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

Excellent!

@@ -21,6 +20,11 @@ class DistDir:
relpath: Path


def is_child_of(path: Path, directory: Path) -> bool:
abs_path = path if path.is_absolute() else directory.joinpath(path).resolve()
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm this technically breaks the "no side effects" thing with the engine by calling .resolve(). It's good this is now isolated to one place at least so that we're less likely to keep using it.

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 spent a bit of time looking to see if there was a Python stdlib function that did this check for us already, but didn't find a convenient one.

Copy link
Contributor

Choose a reason for hiding this comment

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

My concern is more using Path().resolve(). Ideally this function would solely use PurePath and not Path so that it didn't do any file operations. Those FS operations break the contract with the Rules API to avoid side effects.

This was a problem already before, so it's not a blocker. I'm not sure what the fix is in general, and this PR is good that it isolates this issue to only one call site.

Copy link
Member

Choose a reason for hiding this comment

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

This should probably be implemented in terms of fast_relpath_optional.

Comment on lines 32 to 36
if len(workdir_src) > _MAX_FILENAME_LENGTH:
logger.warning(
f"The specified --pants-workdir `{workdir_src}` is longer than {_MAX_FILENAME_LENGTH}. This may be too long of a filename for some systems."
)
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 fine, but please wait for an ack from someone else.

@gshuflin
Copy link
Contributor Author

test_util.py was breaking without the change just introduced in 17c507e - apparently the safe_filename_from_path function was concatenating the entire --pants-workdir path into a single long filename and creating a file by that name in --pants-physical-workdir-base. So when I removed that method, os.path.join was trying to concatenate two absolute paths, which resulted in it just using the 2nd path instead of creating a symlink within the first path. @stuhood want to make sure that this doesn't do anything unexpected.

@gshuflin gshuflin force-pushed the various-cleanup branch 3 times, most recently from 3218b2e to 7f04c9f Compare January 13, 2021 22:46
@stuhood
Copy link
Member

stuhood commented Jan 13, 2021

Stop using safe_filename_from_path and delete the associated code. Outside of tests it is only used in one place, to make sure that the file path specified with the --pants-workdir option is less than 100 characters. This is potentially not even a problem, so that invocation has been replaced with a warning stating that long filenames may cause a problem on systems that limit the total length of a filename; and the code removed

So, while we should stop using safe_filename_from_path, we should almost certainly replace usages with creation of "rebased" directory structures rather than removing handling of long paths entirely.

Path-length and path-component-length are limited on real systems (many Docker mounts, for example), and when issues are encountered, they can break everything in surprising ways ("breaks only on my machine"). Particularly in large monorepos

By "rebased directory structures", I'm referring to generating alternate destinations for a file/target like src/python/example/something:blah or dist/src/python/example/something/blah.py, that re-base the relative path into a new destination like dist/src/python/example/something$blah or dist/src/python/example/something/blah.py.

@gshuflin
Copy link
Contributor Author

Stop using safe_filename_from_path and delete the associated code. Outside of tests it is only used in one place, to make sure that the file path specified with the --pants-workdir option is less than 100 characters. This is potentially not even a problem, so that invocation has been replaced with a warning stating that long filenames may cause a problem on systems that limit the total length of a filename; and the code removed

So, while we should stop using safe_filename_from_path, we should almost certainly replace usages with creation of "rebased" directory structures rather than removing handling of long paths entirely.

Path-length and path-component-length are limited on real systems (many Docker mounts, for example), and when issues are encountered, they can break everything in surprising ways ("breaks only on my machine"). Particularly in large monorepos

By "rebased directory structures", I'm referring to generating alternate destinations for a file/target like src/python/example/something:blah or dist/src/python/example/something/blah.py, that re-base the relative path into a new destination like dist/src/python/example/something$blah or dist/src/python/example/something/blah.py.

Makes sense. This was intended to be a quick refactor commit, and it sounds like actually getting rid of this functionality is both a good idea and would best be done in a separate chunk of work. I will put safe_filename_from_path back for now.

@stuhood
Copy link
Member

stuhood commented Jan 13, 2021

..

Makes sense. This was intended to be a quick refactor commit, and it sounds like actually getting rid of this functionality is both a good idea and would best be done in a separate chunk of work. I will put safe_filename_from_path back for now.

Mm, yea. Another reason to tackle this independently is that we now have duplicated codepaths doing this:

@property
def path_safe_spec(self) -> str:
"""
:API: public
"""
if self._relative_file_path:
parent_count = self._relative_file_path.count(os.path.sep)
parent_prefix = "@" * parent_count if parent_count else "."
file_portion = f".{self._relative_file_path.replace(os.path.sep, '.')}"
else:
parent_prefix = "."
file_portion = ""
if parent_prefix == ".":
target_portion = f"{parent_prefix}{self._target_name}" if self._target_name else ""
else:
target_name = self._target_name or os.path.basename(self.spec_path)
target_portion = f"{parent_prefix}{target_name}"
return f"{self.spec_path.replace(os.path.sep, '.')}{file_portion}{target_portion}"

[ci skip-rust]

[ci skip-build-wheels]
lock.py is only impored from in pantsd code. Moving the file
to within pantsd makes the `src/python` directory structure
less cluttered.

[ci skip-rust]

[ci skip-build-wheels]
[ci skip-build-wheels]

# Rust tests and lints will be skipped. Delete if not intended.
[ci skip-rust]
@gshuflin gshuflin merged commit d9891ee into pantsbuild:master Jan 14, 2021
@gshuflin gshuflin deleted the various-cleanup branch January 14, 2021 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants