From 3b4080956a0257f78d8178abc78518ee832ebe7b Mon Sep 17 00:00:00 2001 From: Garrett Malmquist Date: Thu, 12 Jun 2014 18:26:19 -0400 Subject: [PATCH] Fixes the bug where pants doesn't recognize build targets on top-level directories. The previous argument parser naively simply checked whether a command-line argument contained a slash (/) or a colon (:). This is a reasonable assumption most of the time, but in the case where a BUILD file is located only one directory deep, (eg, buildroot/folder/BUILD), this makes it impossible to run pants on the target simply by calling (eg) ./pants goal bundle folder. See github issue for example: https://github.com/pantsbuild/pants/pull/159 This fixes it by actually checking to see if a phase with goals is defined by the command-line argument. It still checks before that whether the argument contains a slash or a colon, because that if an argument has those characters it can't mean a goal. But an argument not having those characters doesn't mean it is a goal. --- src/python/pants/commands/goal.py | 17 ++++++++++++++++- tests/BUILD | 12 ++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/BUILD diff --git a/src/python/pants/commands/goal.py b/src/python/pants/commands/goal.py index 947c782e39f2..11a72bad065d 100644 --- a/src/python/pants/commands/goal.py +++ b/src/python/pants/commands/goal.py @@ -53,7 +53,22 @@ def parse_args(args): explicit_multi = False def is_spec(spec): - return os.sep in spec or ':' in spec + if os.sep in spec or ':' in spec: + return True # Definitely not a goal. + if Phase(spec).goals(): + return False # Definitely is a goal. + # Here we're going to guess based on whether the BUILD file exists. + spec_dir = os.path.join(get_buildroot(), spec) + # Have to check for build files that have extensions, to support 'BUILD.gen', etc. + if os.path.exists(spec_dir) and os.path.isdir(spec_dir): + for file_name in os.listdir(spec_dir): + dot = file_name.find('.') + if dot >= 0: + file_name = file_name[:dot] + if file_name == 'BUILD': + return True + return False + for i, arg in enumerate(args): if not arg.startswith('-'): diff --git a/tests/BUILD b/tests/BUILD new file mode 100644 index 000000000000..043b2aaa7b30 --- /dev/null +++ b/tests/BUILD @@ -0,0 +1,12 @@ +# Copyright 2014 Pants project contributors (see CONTRIBUTORS.md). +# Licensed under the Apache License, Version 2.0 (see LICENSE). + +# Yes, this is a weird place for this BUILD file. It's here to test if pants can handle targets that +# are only 1 directory deep, and thus might not have a ':' or '/' on the spec path. (which goal.py +# previously has been bad about.) +java_library(name='tests', + sources=[], + dependencies=[ + 'tests/java/com/pants/examples/hello/greet', + ], +)