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

Make the default behavior of pkg_files#strip_prefix more intuitive; rename it to srcs_strip_prefix #656

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

nacl
Copy link
Collaborator

@nacl nacl commented Jan 5, 2023

pkg_files#strip_prefix provides the capability to remove path segments from files prior to them entering the output package. The default for this attribute is to flatten the outputs (e.g. remove all directory structure), which is potentially confusing to users and can cause odd problems. What is generally more expected (and error-prone) is to preserve the package-relative path, as specified when you pass strip_prefix.from_pkg() to pkg_files,

This change implements this update to the default, and implements the following additional changes:

  • pkg_files#strip_prefix is now pkg_files#srcs_strip_prefix -- making it more clear what strip_prefix is actually applying to
  • strip_prefix#files_only (which flattens the directory structure) is now strip_prefix#flatten.

Given that this is a breaking change, we have provided a migration script that users can run to implement this change in their trees. I have partially tested this in an internal tree, and the outputs are identical. This script uses libcst to process the BUILD files and implement transformations. Updates to .bzl files were not attempted due to complexity requirements; users will have to apply them manually.

All BUILD changes were implemented by applying the script like so:

git ls-files '**/BUILD' | xargs bazel run //migration:strip_prefix --

bazel run the above target for more details. It can be run as @rules_pkg//migration:strip_prefix if the appropriate WORKSPACE stuffs are added.

Fixes #354. Supplants #492.

TODO: provide instructions for migrating without downloading rules_pkg -- copying and tweaking some of the stuff added to the WORKSPACE file should be enough.

Andrew Psaltis added 6 commits January 5, 2023 17:34
- `strip_prefix#files_only()` is now `strip_prefix#flatten()`

- `pkg_files#strip_prefix` is now `pkg_files#srcs_strip_prefix`

- The default for (now) `pkg_files#srcs_strip_prefix` is now to strip prefix
  from the package root instead of flattening.
This script migrates BUILD files to the new behavior.  It is written using
libCST, which provides concrete syntax tree functionality for python -- in
particular, it knows how to preserve whitespace.
The migration script, due to its relative simplicity, only knows how to operate
on BUILD files.  This change provides analogous changes within our macros, which
are mostly used in our unit tests.
This is the most substantial part of the change -- we automatically update all
of rules_pkg to account for this new behavior.
Examples should reflect how you should do things, and specifying
`srcs_strip_prefix` everywhere in the intuitive case is no longer necessary.
@aiuto
Copy link
Collaborator

aiuto commented Jan 11, 2023

This seems to be different from what I thought we agreed on, which was that strip prefix would strip from root or package relative, and flatten was a separate attribute to flatten paths after the stripping.

Also, quick side question. does strip_prefix='.' still do the right thing, which is to strip the package and not flatten the rest.

@@ -337,19 +337,18 @@ pkg_files = rule(
""",
default = "",
),
"strip_prefix": attr.string(
doc = """What prefix of a file's path to discard prior to installation.
"srcs_strip_prefix": attr.string(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really have to do this rename? It will force incompatibility between versions.
That will basically mean I'll have to fork this for Google to keep the old naming for a very long time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do we really have to do this rename?

It is not necessary. It came up in one of our older discussions that it may be nonobvious what strip_prefix refers to.

It will force incompatibility between versions.
That will basically mean I'll have to fork this for Google to keep the old naming for a very long time.

Even if we didn't make this change, there is still an incompatibility, as the default has changed. It may be easier without this attribute change, though.

Also, FWIW, theoretically a vast majority of the use cases can be taken care of using the migration script or something like it, but it may be trickier than it looks.

@@ -82,6 +83,7 @@ pkg_files(
srcs = [
"mappings_test.bzl",
],
srcs_strip_prefix = strip_prefix.flatten(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hm. Need to fix indentation here.

@nacl
Copy link
Collaborator Author

nacl commented Jan 11, 2023

This seems to be different from what I thought we agreed on, which was that strip prefix would strip from root or package relative, and flatten was a separate attribute to flatten paths after the stripping.

I think we were thinking different things when we came to an agreement on this in December. Could you provide an example of what you intend the behavior to be with the separate flatten attribute ? I tried reasoning about it a bit, and there doesn't seem to be much that couldn't be otherwise be accomplished by either creating a new BUILD file in a subdirectory, or by specifying prefix.

Also, quick side question. does strip_prefix='.' still do the right thing, which is to strip the package and not flatten the rest.

I believe so; that code hasn't changed.

@aiuto aiuto added the P3 An issue that we are not working on but will review quarterly label Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change P3 An issue that we are not working on but will review quarterly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pkg_files(strip_prefix) behavior is confusing w.r.t. flattening a tree of files
2 participants