get_base_path() fails when it invoked from a rule file listed in buildfile.includes section
Created by: davido
In Gerrit Code Review related projects we are using bucklets as git submodule to share build recipes between Gerrit itself and related projects. To not include bucklets in every single BUCK rule file we source all bucklets in bucklets.def that is sourced in buildfile.includes
section:
[buildfile]
includes = //bucklets.defs
Moreover we want to check the existence of bucklets directory in case the Gir repository wasn't cloned recursively. The problem is to figure out the base directory. For that we are trying to use get_base_path()
method. Unfortunately it doesn't seem to work. Two ugly workarounds are included under "Do it wrong I/II" sections:
$ cat bucklets.defs
import os
import sys
d = get_base_path()
# Do it wrong I
#d = os.getcwd()
#while not os.path.exists(os.path.join(d, '.buckconfig')):
# d = os.path.dirname(d)
#
# Do it wrong II
#d = os.getcwd()
#while not os.path.exists(os.path.join(d, '.buckconfig')) \
# and d != "/":
# d = os.path.dirname(d)
#
#if d == "/":
# sys.stderr.write('.buckconfig file is missing')
# sys.exit(1)
bd = os.path.join(d, 'bucklets')
if not os.path.isdir(bd) or not os.listdir(bd):
sys.stderr.write(('Bucklets directory is missing or empty: %s\n'
'Run `git submodule update --init`') % bd)
sys.exit(1)
bucklets = [
'java_doc.bucklet',
'java_sources.bucklet',
'maven_jar.bucklet',
'maven_package.bucklet',
'war.bucklet',
]
for bucklet in bucklets:
path = os.path.join(bd, bucklet)
if not os.path.isfile(path):
sys.stderr.write('Missing bucklet: %s\n' % path)
sys.exit(1)
include_defs('//bucklets/%s' % bucklet)
The error is:
$ buck build all
Using buckd.
Traceback (most recent call last):
File "/home/davido/projects/gitiles/.buckd/tmp/buck_run.Gtr2dT/buck20693366995834908.py", line 1197, in <module>
main()
File "/home/davido/projects/gitiles/.buckd/tmp/buck_run.Gtr2dT/buck20693366995834908.py", line 558, in main
options.server)
File "/home/davido/projects/gitiles/.buckd/tmp/buck_run.Gtr2dT/buck20693366995834908.py", line 449, in __init__
include_defs(include, build_env)
File "/home/davido/projects/gitiles/.buckd/tmp/buck_run.Gtr2dT/buck20693366995834908.py", line 398, in include_defs
execfile(include_file, build_env['BUILD_FILE_SYMBOL_TABLE'])
File "/home/davido/projects/gitiles/bucklets.defs", line 4, in <module>
d = get_base_path()
File "/home/davido/projects/gitiles/.buckd/tmp/buck_run.Gtr2dT/buck20693366995834908.py", line 85, in invoke
return self.func(*args, **updated_kwargs)
File "/home/davido/projects/gitiles/.buckd/tmp/buck_run.Gtr2dT/buck20693366995834908.py", line 414, in get_base_path
return build_env['BASE']
KeyError: 'BASE'