Skip to content

pkg_install: Add destdir attr & read rel paths. #886

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 1 commit into from
Aug 28, 2024

Conversation

jacky8hyf
Copy link
Contributor

Implementation notes:

Relative paths are interpreted against BUILD_WORKSPACE_DIRECTORY, not BUILD_WORKING_DIRECTORY. This is for the following reasons:

The TODO tag explicitly convey the intention of using BUILD_WORKSPACE_DIRECTORY for relative paths.

If destdir is specified in the attribute of the pkg_install() target, interpreting it against BUILD_WORKSPACE_DIRECTORY is much stabler. That is, no matter where your current cwd is, the destdir attribute always refers to a path relative to the workspace root. For example:

pkg_install(name = "my_pkg_install", destdir = "out/dest")
cd <workspace_root>/<some_subdir>
bazel run //:my_pkg_install

This clearly conveys that the default destdir is
<workspace_root>/out/dest regardless of where the user runs the command.

The cost is that the --destdir command line argument becomes trickier to understand. For example, if one is not familiar with pkg_install, and below the workspace root they run:

cd <workspace_root>/out
bazel run //:my_pkg_install -- --destdir dest

They may expect the destdir to be set to <workspace_root>/out/dest; but it is in fact <workspace_root>/dest.

We could also interpret the target attribute & the command line argument separately (e.g. pkg_install(destdir_against_workspace)), but honestly I think that's even more confusing when they interpret relative paths differently. Please let me know if this is preferred by the maintainers.

Implementation notes:

Relative paths are interpreted against BUILD_WORKSPACE_DIRECTORY, not
BUILD_WORKING_DIRECTORY. This is for the following reasons:

The TODO tag explicitly convey the intention of using
BUILD_WORKSPACE_DIRECTORY for relative paths.

If destdir is specified in the attribute of the pkg_install() target,
interpreting it against BUILD_WORKSPACE_DIRECTORY is much stabler.
That is, no matter where your current cwd is, the destdir attribute
always refers to a path relative to the workspace root. For example:
```
pkg_install(name = "my_pkg_install", destdir = "out/dest")
```
```
cd <workspace_root>/<some_subdir>
bazel run //:my_pkg_install
```
This clearly conveys that the default destdir is
<workspace_root>/out/dest regardless of where the user runs the command.

The cost is that the --destdir command line argument becomes trickier
to understand. For example, if one is not familiar with pkg_install,
and below the workspace root they run:

```
cd <workspace_root>/out
bazel run //:my_pkg_install -- --destdir dest
```

They may expect the destdir to be set to <workspace_root>/out/dest; but
it is in fact `<workspace_root>/dest`.

We could also interpret the target attribute & the command line argument
separately (e.g. pkg_install(destdir_against_workspace)), but honestly
I think that's even more confusing when they interpret relative paths
differently. Please let me know if this is preferred by the maintainers.
@jacky8hyf
Copy link
Contributor Author

Also, it appears that I am making --destdir required when DESTDIR is not set with this change. However, this is already the case before my change. That is, before my change, if --destdir & DESTDIR are both unset:

Traceback (most recent call last):
  File "/home/elsk/.cache/bazel/_bazel_elsk/e1898a9d0a5eb5576d9bd9bb7a32f269/execroot/_main/bazel-out/k8-fastbuild/bin/tests/install/test_installer.runfiles/_main/tests/install/test_installer_install_script.py", line 237, in <module>
    sys.exit(main(sys.argv))
             ^^^^^^^^^^^^^^
  File "/home/elsk/.cache/bazel/_bazel_elsk/e1898a9d0a5eb5576d9bd9bb7a32f269/execroot/_main/bazel-out/k8-fastbuild/bin/tests/install/test_installer.runfiles/_main/tests/install/test_installer_install_script.py", line 233, in main
    installer.do_the_thing()
  File "/home/elsk/.cache/bazel/_bazel_elsk/e1898a9d0a5eb5576d9bd9bb7a32f269/execroot/_main/bazel-out/k8-fastbuild/bin/tests/install/test_installer.runfiles/_main/tests/install/test_installer_install_script.py", line 148, in do_the_thing
    self._install_directory(entry)
  File "/home/elsk/.cache/bazel/_bazel_elsk/e1898a9d0a5eb5576d9bd9bb7a32f269/execroot/_main/bazel-out/k8-fastbuild/bin/tests/install/test_installer.runfiles/_main/tests/install/test_installer_install_script.py", line 101, in _install_directory
    self._maybe_make_unowned_dir(os.path.dirname(entry.dest))
  File "/home/elsk/.cache/bazel/_bazel_elsk/e1898a9d0a5eb5576d9bd9bb7a32f269/execroot/_main/bazel-out/k8-fastbuild/bin/tests/install/test_installer.runfiles/_main/tests/install/test_installer_install_script.py", line 93, in _maybe_make_unowned_dir
    os.makedirs(path, 0o755, exist_ok=True)
  File "<frozen os>", line 225, in makedirs
FileNotFoundError: [Errno 2] No such file or directory: ''

I am just making the error more user friendly.

Copy link
Collaborator

@cgrindel cgrindel left a comment

Choose a reason for hiding this comment

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

LGTM. I will wait for @aiuto to review before merging.

@jacky8hyf
Copy link
Contributor Author

Hello, could you please take a look @aiuto ?

Copy link
Collaborator

@aiuto aiuto left a comment

Choose a reason for hiding this comment

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

I'm glad to see someone taking an interested in this slice of the code.

@aiuto aiuto merged commit 4afd0b3 into bazelbuild:main Aug 28, 2024
2 checks passed
@jacky8hyf jacky8hyf deleted the pkg_install_destdir branch August 28, 2024 19:14
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