Replace getOutputDirRelativePath() calls in ProtoCommon with getRepositoryRelativePath()
Basically the same deal as CcCompilationHelper in https://github.com/bazelbuild/bazel/commit/f060b55820bb8b2df382c4c9ef394d0250a56fdd. proto_library.strip_import_prefix doesn't support paths including workspace roots, so we can just rely on package fragments and repository relative paths when handling it.
PiperOrigin-RevId: 334151898
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
index 0bbaddf..412d402 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
@@ -36,7 +36,6 @@
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
import javax.annotation.Nullable;
-import net.starlark.java.eval.StarlarkSemantics;
import net.starlark.java.syntax.Location;
/**
@@ -266,49 +265,20 @@
}
PathFragment stripImportPrefix;
- PathFragment importPrefix;
-
- StarlarkSemantics starlarkSemantics =
- ruleContext.getAnalysisEnvironment().getStarlarkSemantics();
- boolean siblingRepositoryLayout =
- starlarkSemantics.getBool(BuildLanguageOptions.EXPERIMENTAL_SIBLING_REPOSITORY_LAYOUT);
- if (stripImportPrefixAttribute != null || importPrefixAttribute != null) {
- if (stripImportPrefixAttribute == null) {
- stripImportPrefix =
- PathFragment.create(ruleContext.getLabel().getWorkspaceRoot(starlarkSemantics));
- } else if (stripImportPrefixAttribute.isAbsolute()) {
- stripImportPrefix =
- ruleContext
- .getLabel()
- .getPackageIdentifier()
- .getRepository()
- .getExecPath(siblingRepositoryLayout)
- .getRelative(stripImportPrefixAttribute.toRelative());
- } else {
- stripImportPrefix =
- ruleContext
- .getLabel()
- .getPackageIdentifier()
- .getExecPath(siblingRepositoryLayout)
- .getRelative(stripImportPrefixAttribute);
- }
-
- if (importPrefixAttribute != null) {
- importPrefix = importPrefixAttribute;
- } else {
- importPrefix = PathFragment.EMPTY_FRAGMENT;
- }
-
- if (importPrefix.isAbsolute()) {
- ruleContext.attributeError("import_prefix", "should be a relative path");
- return null;
- }
+ if (stripImportPrefixAttribute == null) {
+ stripImportPrefix = PathFragment.EMPTY_FRAGMENT;
+ } else if (stripImportPrefixAttribute.isAbsolute()) {
+ stripImportPrefix = stripImportPrefixAttribute.toRelative();
} else {
- // Has generated sources, but neither strip_import_prefix nor import_prefix
stripImportPrefix =
- ruleContext.getLabel().getPackageIdentifier().getRepository().getPackagePath();
+ ruleContext.getLabel().getPackageFragment().getRelative(stripImportPrefixAttribute);
+ }
- importPrefix = PathFragment.EMPTY_FRAGMENT;
+ PathFragment importPrefix =
+ importPrefixAttribute != null ? importPrefixAttribute : PathFragment.EMPTY_FRAGMENT;
+ if (importPrefix.isAbsolute()) {
+ ruleContext.attributeError("import_prefix", "should be a relative path");
+ return null;
}
ImmutableList.Builder<Artifact> symlinks = ImmutableList.builder();
@@ -317,9 +287,7 @@
PathFragment sourceRootPath = ruleContext.getUniqueDirectory("_virtual_imports");
for (Artifact realProtoSource : protoSources) {
- if (siblingRepositoryLayout && realProtoSource.isSourceArtifact()
- ? !realProtoSource.getExecPath().startsWith(stripImportPrefix)
- : !realProtoSource.getOutputDirRelativePath().startsWith(stripImportPrefix)) {
+ if (!realProtoSource.getRepositoryRelativePath().startsWith(stripImportPrefix)) {
ruleContext.ruleError(
String.format(
".proto file '%s' is not under the specified strip prefix '%s'",
@@ -328,13 +296,7 @@
}
Pair<PathFragment, Artifact> importsPair =
computeImports(
- ruleContext,
- realProtoSource,
- sourceRootPath,
- importPrefix,
- stripImportPrefix,
- starlarkSemantics.getBool(
- BuildLanguageOptions.EXPERIMENTAL_SIBLING_REPOSITORY_LAYOUT));
+ ruleContext, realProtoSource, sourceRootPath, importPrefix, stripImportPrefix);
protoSourceImportPair.add(new Pair<>(realProtoSource, importsPair.first.toString()));
symlinks.add(importsPair.second);
}
@@ -353,18 +315,10 @@
Artifact realProtoSource,
PathFragment sourceRootPath,
PathFragment importPrefix,
- PathFragment stripImportPrefix,
- boolean siblingRepositoryLayout) {
- PathFragment importPath;
-
- if (siblingRepositoryLayout && realProtoSource.isSourceArtifact()) {
- importPath =
- importPrefix.getRelative(realProtoSource.getExecPath().relativeTo(stripImportPrefix));
- } else {
- importPath =
- importPrefix.getRelative(
- realProtoSource.getOutputDirRelativePath().relativeTo(stripImportPrefix));
- }
+ PathFragment stripImportPrefix) {
+ PathFragment importPath =
+ importPrefix.getRelative(
+ realProtoSource.getRepositoryRelativePath().relativeTo(stripImportPrefix));
Artifact virtualProtoSource =
ruleContext.getDerivedArtifact(