BlazeOptionHandler: only check in the parent directory, if there is one When checking for a file called DO_NOT_BUILD_HERE in the parent direcotry of the workspace path, first verify that there is such a parent directory. This avoids a null pointer exception if the WORKSPACE file is in the top-level directory. Fixes #5349 Change-Id: I81289a27a3f7fb0f4b5a112343de1b454fb5b8c5 PiperOrigin-RevId: 199790735
diff --git a/src/main/java/com/google/devtools/build/lib/packages/Package.java b/src/main/java/com/google/devtools/build/lib/packages/Package.java index 1ea31a1..d6f7944 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/Package.java +++ b/src/main/java/com/google/devtools/build/lib/packages/Package.java
@@ -340,7 +340,9 @@ Path current = buildFile.getParentDirectory(); for (int i = 0, len = packageFragment.segmentCount(); i < len && !packageFragment.equals(PathFragment.EMPTY_FRAGMENT); i++) { - current = current.getParentDirectory(); + if (current != null) { + current = current.getParentDirectory(); + } } return Root.fromPath(current); } @@ -367,8 +369,8 @@ this.packageDirectory = filename.getParentDirectory(); this.sourceRoot = getSourceRoot(filename, packageIdentifier.getSourceRoot()); - if ((sourceRoot == null - || !sourceRoot.getRelative(packageIdentifier.getSourceRoot()).equals(packageDirectory)) + if ((sourceRoot.asPath() == null + || !sourceRoot.getRelative(packageIdentifier.getSourceRoot()).equals(packageDirectory)) && !filename.getBaseName().equals("WORKSPACE")) { throw new IllegalArgumentException( "Invalid BUILD file name for package '" + packageIdentifier + "': " + filename);
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeOptionHandler.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeOptionHandler.java index 81a6f03..107cf45 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeOptionHandler.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeOptionHandler.java
@@ -144,17 +144,19 @@ return ExitCode.LOCAL_ENVIRONMENTAL_ERROR; } - Path doNotBuild = - workspacePath.getParentDirectory().getRelative(BlazeWorkspace.DO_NOT_BUILD_FILE_NAME); + if (workspacePath.getParentDirectory() != null) { + Path doNotBuild = + workspacePath.getParentDirectory().getRelative(BlazeWorkspace.DO_NOT_BUILD_FILE_NAME); - if (doNotBuild.exists()) { - if (!commandAnnotation.canRunInOutputDirectory()) { - eventHandler.handle(Event.error(getNotInRealWorkspaceError(doNotBuild))); - return ExitCode.COMMAND_LINE_ERROR; - } else { - eventHandler.handle( - Event.warn( - runtime.getProductName() + " is run from output directory. This is unsound.")); + if (doNotBuild.exists()) { + if (!commandAnnotation.canRunInOutputDirectory()) { + eventHandler.handle(Event.error(getNotInRealWorkspaceError(doNotBuild))); + return ExitCode.COMMAND_LINE_ERROR; + } else { + eventHandler.handle( + Event.warn( + runtime.getProductName() + " is run from output directory. This is unsound.")); + } } } return ExitCode.SUCCESS;