diff options
2 files changed, 49 insertions, 39 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/pkgcache/TargetPatternResolverUtil.java b/src/main/java/com/google/devtools/build/lib/pkgcache/TargetPatternResolverUtil.java index 6bbf55c7fe..a291150dda 100644 --- a/src/main/java/com/google/devtools/build/lib/pkgcache/TargetPatternResolverUtil.java +++ b/src/main/java/com/google/devtools/build/lib/pkgcache/TargetPatternResolverUtil.java @@ -13,12 +13,16 @@ // limitations under the License. package com.google.devtools.build.lib.pkgcache; +import com.google.common.collect.ImmutableSet; +import com.google.devtools.build.lib.cmdline.LabelValidator; import com.google.devtools.build.lib.cmdline.ResolvedTargets; import com.google.devtools.build.lib.cmdline.TargetParsingException; +import com.google.devtools.build.lib.cmdline.TargetPatternResolver; import com.google.devtools.build.lib.packages.Package; import com.google.devtools.build.lib.packages.Target; import com.google.devtools.build.lib.syntax.Label; import com.google.devtools.build.lib.util.StringUtilities; +import com.google.devtools.build.lib.vfs.PathFragment; /** * Common utility methods for target pattern resolution. @@ -66,4 +70,44 @@ public final class TargetPatternResolverUtil { } return builder.build(); } + + public static void validatePatternPackage(String originalPattern, + PathFragment packageNameFragment, TargetPatternResolver<?> resolver) + throws TargetParsingException { + String packageName = packageNameFragment.toString(); + // It's possible for this check to pass, but for + // Label.validatePackageNameFull to report an error because the + // package name is illegal. That's a little weird, but we can live with + // that for now--see test case: testBadPackageNameButGoodEnoughForALabel. + if (LabelValidator.validatePackageName(packageName) != null) { + throw new TargetParsingException("'" + packageName + "' is not a valid package name"); + } + if (!resolver.isPackage(packageName)) { + throw new TargetParsingException( + TargetPatternResolverUtil.getParsingErrorMessage( + "no such package '" + packageName + "': BUILD file not found on package path", + originalPattern)); + } + } + + public static PathFragment getPathFragment(String pathPrefix) throws TargetParsingException { + PathFragment directory = new PathFragment(pathPrefix); + if (directory.containsUplevelReferences()) { + throw new TargetParsingException("up-level references are not permitted: '" + + directory.getPathString() + "'"); + } + if (!pathPrefix.isEmpty() && (LabelValidator.validatePackageName(pathPrefix) != null)) { + throw new TargetParsingException("'" + pathPrefix + "' is not a valid package name"); + } + return directory; + } + + public static ImmutableSet<PathFragment> getPathFragments(ImmutableSet<String> pathPrefixes) + throws TargetParsingException { + ImmutableSet.Builder<PathFragment> pathFragmentsBuilder = ImmutableSet.builder(); + for (String pathPrefix : pathPrefixes) { + pathFragmentsBuilder.add(TargetPatternResolverUtil.getPathFragment(pathPrefix)); + } + return pathFragmentsBuilder.build(); + } } diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/RecursivePackageProviderBackedTargetPatternResolver.java b/src/main/java/com/google/devtools/build/lib/skyframe/RecursivePackageProviderBackedTargetPatternResolver.java index cd7a7be567..942ed83fcb 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/RecursivePackageProviderBackedTargetPatternResolver.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/RecursivePackageProviderBackedTargetPatternResolver.java @@ -14,7 +14,6 @@ package com.google.devtools.build.lib.skyframe; import com.google.common.collect.ImmutableSet; -import com.google.devtools.build.lib.cmdline.LabelValidator; import com.google.devtools.build.lib.cmdline.ResolvedTargets; import com.google.devtools.build.lib.cmdline.TargetParsingException; import com.google.devtools.build.lib.cmdline.TargetPatternResolver; @@ -47,7 +46,7 @@ public class RecursivePackageProviderBackedTargetPatternResolver private final PathPackageLocator pkgPath; public RecursivePackageProviderBackedTargetPatternResolver( - final RecursivePackageProvider recursivePackageProvider, + RecursivePackageProvider recursivePackageProvider, EventHandler eventHandler, FilteringPolicy policy, PathPackageLocator pkgPath) { @@ -120,22 +119,7 @@ public class RecursivePackageProviderBackedTargetPatternResolver private ResolvedTargets<Target> getTargetsInPackage(String originalPattern, PathFragment packageNameFragment, FilteringPolicy policy) throws TargetParsingException, InterruptedException { - String packageName = packageNameFragment.toString(); - - // It's possible for this check to pass, but for - // Label.validatePackageNameFull to report an error because the - // package name is illegal. That's a little weird, but we can live with - // that for now--see test case: testBadPackageNameButGoodEnoughForALabel. - if (LabelValidator.validatePackageName(packageName) != null) { - throw new TargetParsingException("'" + packageName + "' is not a valid package name"); - } - if (!isPackage(packageName)) { - throw new TargetParsingException( - TargetPatternResolverUtil.getParsingErrorMessage( - "no such package '" + packageName + "': BUILD file not found on package path", - originalPattern)); - } - + TargetPatternResolverUtil.validatePatternPackage(originalPattern, packageNameFragment, this); try { Package pkg = getPackage(PackageIdentifier.createInDefaultRepo(packageNameFragment)); return TargetPatternResolverUtil.resolvePackageTargets(pkg, policy); @@ -163,15 +147,9 @@ public class RecursivePackageProviderBackedTargetPatternResolver FilteringPolicy actualPolicy = rulesOnly ? FilteringPolicies.and(FilteringPolicies.RULES_ONLY, policy) : policy; - - PathFragment pathFragment = getPathFragment(directory); - - ImmutableSet.Builder<PathFragment> excludedPathFragmentsBuilder = ImmutableSet.builder(); - for (String excludedDirectory : excludedSubdirectories) { - excludedPathFragmentsBuilder.add(getPathFragment(excludedDirectory)); - } - ImmutableSet<PathFragment> excludedPathFragments = excludedPathFragmentsBuilder.build(); - + ImmutableSet<PathFragment> excludedPathFragments = + TargetPatternResolverUtil.getPathFragments(excludedSubdirectories); + PathFragment pathFragment = TargetPatternResolverUtil.getPathFragment(directory); ResolvedTargets.Builder<Target> targetBuilder = ResolvedTargets.builder(); for (Path root : pkgPath.getPathEntries()) { RootedPath rootedPath = RootedPath.toRootedPath(root, pathFragment); @@ -200,17 +178,5 @@ public class RecursivePackageProviderBackedTargetPatternResolver } return filteredBuilder.build(); } - - private static PathFragment getPathFragment(String pathPrefix) throws TargetParsingException { - PathFragment directory = new PathFragment(pathPrefix); - if (directory.containsUplevelReferences()) { - throw new TargetParsingException("up-level references are not permitted: '" - + directory.getPathString() + "'"); - } - if (!pathPrefix.isEmpty() && (LabelValidator.validatePackageName(pathPrefix) != null)) { - throw new TargetParsingException("'" + pathPrefix + "' is not a valid package name"); - } - return directory; - } } |