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

[4.x] fix(runfiles): use normalized paths when guarding runfiles root and node_modules on Windows #3409

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion internal/node/launcher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# It helps to determine if we are running on a Windows environment (excludes WSL as it acts like Unix)
function isWindows {
case "$(uname -s)" in
CYGWIN*) local IS_WINDOWS=1 ;;
MINGW*) local IS_WINDOWS=1 ;;
MSYS_NT*) local IS_WINDOWS=1 ;;
*) local IS_WINDOWS=0 ;;
esac

echo $IS_WINDOWS
return
}

# It helps to normalizes paths when running on Windows.
#
# Example:
# C:/Users/XUser/_bazel_XUser/7q7kkv32/execroot/A/b/C -> /c/users/xuser/_bazel_xuser/7q7kkv32/execroot/a/b/c
function normalizeWindowsPath {
# Apply the followings paths transformations to normalize paths on Windows
# -process driver letter
# -convert path separator
# -lowercase everything
echo $(sed -e 's#^\(.\):#/\L\1#' -e 's#\\#/#g' -e 's/[A-Z]/\L&/g' <<< "$1")
return
}

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -uo pipefail; f=build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel/tools/bash/runfiles/runfiles.bash
Expand Down Expand Up @@ -49,7 +75,13 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
# Case 6a is handled like case 3.
if [[ -n "${RUNFILES_MANIFEST_ONLY:-}" ]]; then
# Windows only has a manifest file instead of symlinks.
RUNFILES=${RUNFILES_MANIFEST_FILE%/MANIFEST}
if [[ $(isWindows) -eq "1" ]]; then
# If Windows normalizing the path and case insensitive removing the `/MANIFEST` part of the path
NORMALIZED_RUNFILES_MANIFEST_FILE_PATH=$(normalizeWindowsPath $RUNFILES_MANIFEST_FILE)
RUNFILES=$(sed 's|\/MANIFEST$||i' <<< $NORMALIZED_RUNFILES_MANIFEST_FILE_PATH)
else
RUNFILES=${RUNFILES_MANIFEST_FILE%/MANIFEST}
fi
elif [[ -n "${TEST_SRCDIR:-}" ]]; then
# Case 4, bazel has identified runfiles for us.
RUNFILES="${TEST_SRCDIR:-}"
Expand Down
21 changes: 12 additions & 9 deletions internal/node/test/env.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,30 @@ describe('launcher.sh environment', function() {
expectPathsToMatch(process.env['BAZEL_WORKSPACE'], 'build_bazel_rules_nodejs');
expectPathsToMatch(process.env['BAZEL_TARGET'], '//internal/node/test:env_test');
expectPathsToMatch(process.cwd(), `${process.env['RUNFILES_DIR']}/build_bazel_rules_nodejs`);
expectPathsToMatch(process.env['PWD'], `${process.env['RUNFILES_DIR']}/build_bazel_rules_nodejs`);
expectPathsToMatch(
process.env['PWD'],
`${process.env['RUNFILES_DIR']}/build_bazel_rules_nodejs`
);
expectPathsToMatch(process.env['BAZEL_NODE_MODULES_ROOTS'], ':npm');
const expectedRoots = [
`${execroot}`,
`${execroot}/node_modules`,
`${runfilesRoot}`,
`${runfilesRoot}/build_bazel_rules_nodejs/node_modules`,
]
];
if (isWindows) {
expectedRoots.push(
process.env['RUNFILES'],
`${process.env['RUNFILES']}/build_bazel_rules_nodejs/node_modules`,
)
`${process.env['RUNFILES']}/build_bazel_rules_nodejs/node_modules`
);
}
expectPathsToMatch(process.env['BAZEL_PATCH_ROOTS'].split(','), expectedRoots);
});

it('should setup correct bazel environment variables when in execroot with no third party deps', function() {
const env = require(runfiles.resolvePackageRelative('dump_build_env.json'));
// On Windows, the RUNFILES path ends in a /MANIFEST segment in this context
const runfilesRoot = normPath(isWindows ? path.dirname(env['RUNFILES']) : env['RUNFILES']);
const runfilesRoot = normPath(env['RUNFILES']);
const match = runfilesRoot.match(/\/bazel-out\//);
expect(!!match).toBe(true);
const execroot = runfilesRoot.slice(0, match.index);
Expand All @@ -83,11 +86,11 @@ describe('launcher.sh environment', function() {
];
expectPathsToMatch(env['BAZEL_PATCH_ROOTS'].split(','), expectedRoots);
});

it('should setup correct bazel environment variables when in execroot with third party deps', function() {
const env = require(runfiles.resolvePackageRelative('dump_build_env_alt.json'));
// On Windows, the RUNFILES path ends in a /MANIFEST segment in this context
const runfilesRoot = normPath(isWindows ? path.dirname(env['RUNFILES']) : env['RUNFILES']);
const runfilesRoot = normPath(env['RUNFILES']);
const match = runfilesRoot.match(/\/bazel-out\//);
expect(!!match).toBe(true);
const execroot = runfilesRoot.slice(0, match.index);
Expand All @@ -104,7 +107,7 @@ describe('launcher.sh environment', function() {
];
expectPathsToMatch(env['BAZEL_PATCH_ROOTS'].split(','), expectedRoots);
});

it('should setup correct bazel environment variables from env attr', function() {
const env = require(runfiles.resolvePackageRelative('dump_build_env_attr.json'));
expect(env['FOO']).toBe('BAR');
Expand Down