Fix #507 - Always run from WORKSPACE root in compile_pip_requirements update exe (#564)
diff --git a/python/pip_install/pip_compile.py b/python/pip_install/pip_compile.py index 278e848..ce40285 100644 --- a/python/pip_install/pip_compile.py +++ b/python/pip_install/pip_compile.py
@@ -41,11 +41,22 @@ ) copyfile(requirements_txt, requirements_out) -elif "BUILD_WORKING_DIRECTORY" in os.environ: - os.chdir(os.environ['BUILD_WORKING_DIRECTORY']) +elif "BUILD_WORKSPACE_DIRECTORY" in os.environ: + # This value, populated when running under `bazel run`, is a path to the + # "root of the workspace where the build was run." + # This matches up with the values passed in via the macro using the 'rootpath' Make variable, + # which for source files provides a path "relative to your workspace root." + # + # Changing to the WORKSPACE root avoids 'file not found' errors when the `.update` target is run + # from different directories within the WORKSPACE. + os.chdir(os.environ['BUILD_WORKSPACE_DIRECTORY']) else: + err_msg = ( + "Expected to find BUILD_WORKSPACE_DIRECTORY (running under `bazel run`) or " + "TEST_TMPDIR (running under `bazel test`) in environment." + ) print( - "Expected to find BUILD_WORKING_DIRECTORY in environment", + err_msg, file=sys.stderr, ) sys.exit(1)