Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit f8088cf

Browse files
cjhopmanfacebook-github-bot
authored andcommitted
Document some of sh_binary (including my confusion about the implementation)
Summary: This thing is crazy. The implementation makes no sense. It's doing some things that are just really strange and then it's doing some similar things in two completely different ways (one of which is really complex). I've probably spent hours now trying to figure out wtf this does. I'm adding some documentation and commentary to help me or the next person waste less time. Reviewed By: ndmitchell fbshipit-source-id: e967ab5de27f9f9f9f37fc7e4a01cfc5b22e0142
1 parent a8d35a8 commit f8088cf

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/com/facebook/buck/shell/ShBinary.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@
6767
import java.util.logging.Level;
6868
import java.util.stream.Stream;
6969

70+
// TODO(cjhopman): Don't allow people to just write complex, hard to follow behavior without
71+
// documenting it.
72+
// TODO(cjhopman): Figure out wtf this code does. It's creating symlinks all over the place, some of
73+
// them within this things own getBuildSteps, others by passing information into the generated
74+
// template and then creating them when invoked. It looks like it's creating multiple symlinks for
75+
// everything that appears in resources. Why does it do that? And where are they going?
7076
public class ShBinary extends AbstractBuildRuleWithDeclaredAndExtraDeps
7177
implements BinaryBuildRule, HasRuntimeDeps {
7278

@@ -185,6 +191,8 @@ public ImmutableList<Step> getBuildSteps(
185191
valuesBuilder.put("cell_names", cellsNamesBuilder.build());
186192
valuesBuilder.put("cell_paths", cellsPathsStringsBuilder.build());
187193

194+
// TODO(cjhopman): Why is this so similar to the resource path construction above? What's the
195+
// difference and why?
188196
Path defaultRuntimeResourcesPath =
189197
runtimeResourcesDir
190198
.resolve(ROOT_CELL_LINK_NAME)

src/com/facebook/buck/shell/sh_binary_template

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
<!
2+
This template creates a shell script that invokes a user shell script in a simple sandbox.
3+
4+
Expects the following template arguments:
5+
6+
path_to_project_root_file - path relative to this script's directory to the "project_root" file
7+
in buck-out. That file is expected to contain the path of the project root.
8+
script_to_run - path (relative to the __default__ cell link) for the script to run
9+
resources - TODO(cjhopman): Figure out what these two resources mean and why they both exist
10+
default_runtime_resources - TODO(cjhopman): Figure out what these two resources mean and why they both exist
11+
cell_names - list of cell names (including a __default__ for this script's own cell)
12+
cell_paths - list of paths corresponding to the names in cell_names
13+
14+
!><\\>
115
#!/bin/bash
216
# Run with -e so the script will fail if any of the steps fail.
317
set -e
@@ -94,6 +108,13 @@ trap "rm -rf $BUCK_TMP_ROOT" EXIT HUP INT TERM
94108
<cell_paths:{x| <x><\n>}>
95109
)
96110

111+
# TODO(cjhopman): This is crazy. we are modifying the binary's own output directory here. Why
112+
# wouldn't this have just been done in java?
113+
# I think this code is creating a symlink for each cell at a path of basically
114+
# `dirname <<THIS_SCRIPT>>`/cells/<cell_name>. But there's no reason to do that here, and it
115+
# forces us to do really complex stuff to get it correct. The code is hard to follow, so I came
116+
# to the conclusion based on observation.
117+
# Also, what are these even used for? The path that they are at isn't exported to the user script?
97118
# The following operation needs to be atomic.
98119
if [ ! -d "$BUCK_REAL_ROOT/cells/" ] ; then
99120
tmpfolder="`mktemp -d "$BUCK_REAL_ROOT/tmp.XXXXXXXXXX"`"
@@ -111,11 +132,19 @@ trap "rm -rf $BUCK_TMP_ROOT" EXIT HUP INT TERM
111132
"__default__/$SCRIPT_TO_RUN"
112133
)
113134

135+
# TODO(cjhopman): Why do we do this here? We already create links for these in Java.
136+
# This code appears to be creating links for all of our resources in the invocation-specific
137+
# directory pointing back at the files through the cell symlinks we created above... Why do we do
138+
# this? And how do we expect people to access these paths?
114139
for path in "${SYMLINK_PATHS[@]}"; do
115140
mkdir -p "$(dirname "$path")"
116141
ln -sf "$BUCK_REAL_ROOT/cells/$path" "$BUCK_TMP_ROOT/$path"
117142
done
118143
)
119144

145+
# It seems like people can access resources through either BUCK_PROJECT_ROOT, in which case they
146+
# are accessing invocation-specific links or through BUCK_DEFAULT_RUNTIME_RESOURCES in which case
147+
# they are accessing the links created in Java. #1, there's no reason to have invocation-specific
148+
# links. #2 there's also no reason to have multiple structures of these links laid out.
120149
export BUCK_PROJECT_ROOT="$BUCK_TMP_ROOT/__default__"
121150
exec "$BUCK_TMP_ROOT/__default__/$SCRIPT_TO_RUN" "$@"

0 commit comments

Comments
 (0)