Skip to content

Commit

Permalink
Rename RepositoryName getters
Browse files Browse the repository at this point in the history
- #getName (returns "@repo") is renamed to #getNameWithAt
- #strippedName (returns "repo") is renamed to #getName
- #isDefault is removed; it already has no callers

PiperOrigin-RevId: 441481678
  • Loading branch information
Wyverald authored and pull[bot] committed Nov 27, 2023
1 parent 2bc2ab7 commit 3702848
Show file tree
Hide file tree
Showing 31 changed files with 81 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ private void propagateTransitiveValidationOutputGroups() {
Label rdeLabel =
ruleContext.getRule().getRuleClassObject().getRuleDefinitionEnvironmentLabel();
// only allow native and builtins to override transitive validation propagation
if (rdeLabel != null && !"@_builtins".equals(rdeLabel.getRepository().getName())) {
if (rdeLabel != null && !"@_builtins".equals(rdeLabel.getRepository().getNameWithAt())) {
ruleContext.ruleError(rdeLabel + " cannot access the _transitive_validation private API");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public boolean isStrictFilesetOutput() {
}

public String getMainRepositoryName() {
return mainRepositoryName.strippedName();
return mainRepositoryName.getName();
}

/**
Expand Down Expand Up @@ -597,7 +597,7 @@ public boolean stampBinariesForStarlark(StarlarkThread thread) throws EvalExcept
BazelModuleContext.of(Module.ofInnermostEnclosingStarlarkFunction(thread))
.label()
.getRepository();
if (!"@_builtins".equals(repository.getName())) {
if (!"@_builtins".equals(repository.getNameWithAt())) {
throw Starlark.errorf("private API only for use in builtins");
}
return stampBinaries();
Expand Down Expand Up @@ -677,7 +677,7 @@ public boolean isToolConfigurationForStarlark(StarlarkThread thread) throws Eval
BazelModuleContext.of(Module.ofInnermostEnclosingStarlarkFunction(thread))
.label()
.getRepository();
if (!"@_builtins".equals(repository.getName())) {
if (!"@_builtins".equals(repository.getNameWithAt())) {
throw Starlark.errorf("private API only for use in builtins");
}
return isToolConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public enum OutputDirectory {
public ArtifactRoot getRoot(
String outputDirName, BlazeDirectories directories, RepositoryName mainRepositoryName) {
// e.g., execroot/repo1
Path execRoot = directories.getExecRoot(mainRepositoryName.strippedName());
Path execRoot = directories.getExecRoot(mainRepositoryName.getName());
// e.g., [[execroot/repo1]/bazel-out/config/bin]
return ArtifactRoot.asDerivedRoot(
execRoot,
Expand Down Expand Up @@ -160,7 +160,7 @@ public ArtifactRoot getRoot(

this.mergeGenfilesDirectory = options.mergeGenfilesDirectory;
this.siblingRepositoryLayout = siblingRepositoryLayout;
this.execRoot = directories.getExecRoot(mainRepositoryName.strippedName());
this.execRoot = directories.getExecRoot(mainRepositoryName.getName());
}

private static void addMnemonicPart(
Expand Down Expand Up @@ -265,7 +265,7 @@ private ArtifactRoot buildDerivedRoot(
execRoot,
rootType,
directories.getRelativeOutputPath(),
repository.strippedName(),
repository.getName(),
outputDirName,
nameFragment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public boolean starlarkMatches(Label label, StarlarkThread starlarkThread) throw
BazelModuleContext.of(ofInnermostEnclosingStarlarkFunction(starlarkThread))
.label()
.getRepository();
if (!"@_builtins".equals(repository.getName())) {
if (!"@_builtins".equals(repository.getNameWithAt())) {
throw Starlark.errorf("private API only for use by builtins");
}
return Allowlist.isAvailableFor(getPackageSpecifications(), label);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public StarlarkCallable rule(
}

if (compileOneFiletype instanceof Sequence) {
if (!bzlModule.label().getRepository().getName().equals("@_builtins")) {
if (!bzlModule.label().getRepository().getNameWithAt().equals("@_builtins")) {
throw Starlark.errorf(
"Rule in '%s' cannot use private API", bzlModule.label().getPackageName());
}
Expand Down Expand Up @@ -895,7 +895,7 @@ public void export(EventHandler handler, Label starlarkLabel, String ruleClassNa
}
// TODO(b/121385274): remove when we stop allowlisting starlark transitions
if (hasStarlarkDefinedTransition) {
if (!starlarkLabel.getRepository().getName().equals("@_builtins")) {
if (!starlarkLabel.getRepository().getNameWithAt().equals("@_builtins")) {
if (!hasFunctionTransitionAllowlist) {
errorf(
handler,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public BazelRepositoryModule() {
Set<String> conflicting =
overrides.keySet().stream()
.filter(repositoryNamesWithManagedDirs::contains)
.map(RepositoryName::getName)
.map(RepositoryName::getNameWithAt)
.collect(Collectors.toSet());
if (!conflicting.isEmpty()) {
String message =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ static BazelModuleResolutionValue createValue(
BiMap<String, ModuleExtensionId> extensionUniqueNames = HashBiMap.create();
for (ModuleExtensionId id : extensionUsagesById.rowKeySet()) {
String bestName =
id.getBzlFileLabel().getRepository().strippedName() + "." + id.getExtensionName();
id.getBzlFileLabel().getRepository().getName() + "." + id.getExtensionName();
if (extensionUniqueNames.putIfAbsent(bestName, id) == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public String getUnambiguousCanonicalForm() {
+ "<pre class=language-python>Label(\"@foo//bar:baz\").workspace_name"
+ " == \"foo\"</pre>")
public String getWorkspaceName() {
return packageIdentifier.getRepository().strippedName();
return packageIdentifier.getRepository().getName();
}

/**
Expand All @@ -442,7 +442,7 @@ public String toShorthandString() {
if (packageIdentifier.getRepository().isMain()) {
repository = "";
} else {
repository = packageIdentifier.getRepository().getName();
repository = packageIdentifier.getRepository().getNameWithAt();
}
return repository + "//" + getPackageFragment();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public PathFragment getPackagePath(boolean siblingRepositoryLayout) {
return repository.isMain() || siblingRepositoryLayout
? pkgName
: LabelConstants.EXTERNAL_PATH_PREFIX
.getRelative(repository.strippedName())
.getRelative(repository.getName())
.getRelative(pkgName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public final class RepositoryName {
});

/**
* Makes sure that name is a valid repository name and creates a new RepositoryName using it.
* Makes sure that name is a valid repository name and creates a new RepositoryName using it. The
* given string must begin with a '@'.
*
* @throws LabelSyntaxException if the name is invalid
*/
Expand Down Expand Up @@ -128,7 +129,7 @@ public static Pair<RepositoryName, PathFragment> fromPathFragment(
/**
* Store the name if the owner repository where this repository name is requested. If this field
* is not null, it means this instance represents the requested repository name that is actually
* not visible from the owner repository and should fail in {@link RepositoryDelegatorFunction}
* not visible from the owner repository and should fail in {@code RepositoryDelegatorFunction}
* when fetching the repository.
*/
private final String ownerRepoIfNotVisible;
Expand Down Expand Up @@ -166,11 +167,8 @@ static String validate(String name) {
return null;
}

/**
* Returns the repository name without the leading "{@literal @}". For the default repository,
* returns "".
*/
public String strippedName() {
/** Returns the repository name without the leading "{@literal @}". */
public String getName() {
if (name.isEmpty()) {
return name;
}
Expand All @@ -179,7 +177,7 @@ public String strippedName() {

/**
* Create a {@link RepositoryName} instance that indicates the requested repository name is
* actually not visible from the owner repository and should fail in {@link
* actually not visible from the owner repository and should fail in {@code
* RepositoryDelegatorFunction} when fetching with this {@link RepositoryName} instance.
*/
public RepositoryName toNonVisible(String ownerRepo) {
Expand All @@ -204,16 +202,7 @@ public static String stripName(String repoName) {
return repoName.startsWith("@") ? repoName.substring(1) : repoName;
}

/**
* Returns if this is the default repository, that is, {@link #name} is "".
*/
public boolean isDefault() {
return name.isEmpty();
}

/**
* Returns if this is the main repository, that is, {@link #name} is "@".
*/
/** Returns if this is the main repository, that is, {@link #getName} is empty. */
public boolean isMain() {
return name.equals("@");
}
Expand All @@ -222,7 +211,7 @@ public boolean isMain() {
* Returns the repository name, with leading "{@literal @}" (or "" for the default repository).
*/
// TODO(bazel-team): Use this over toString()- easier to track its usage.
public String getName() {
public String getNameWithAt() {
return name;
}

Expand Down Expand Up @@ -250,7 +239,7 @@ public PathFragment getExecPath(boolean siblingRepositoryLayout) {
siblingRepositoryLayout
? LabelConstants.EXPERIMENTAL_EXTERNAL_PATH_PREFIX
: LabelConstants.EXTERNAL_PATH_PREFIX;
return prefix.getRelative(strippedName());
return prefix.getRelative(getName());
}

/**
Expand All @@ -260,7 +249,7 @@ public PathFragment getExecPath(boolean siblingRepositoryLayout) {
public PathFragment getRunfilesPath() {
return isMain()
? PathFragment.EMPTY_FRAGMENT
: PathFragment.create("..").getRelative(strippedName());
: PathFragment.create("..").getRelative(getName());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ public TargetPattern parse(String pattern) throws TargetParsingException {
throw new TargetParsingException(
String.format(
"Repository '%s' is not visible from repository '@%s'",
repository.getName(), repository.getOwnerRepoIfNotVisible()),
repository.getNameWithAt(), repository.getOwnerRepoIfNotVisible()),
Code.PACKAGE_NOT_FOUND);
}

Expand Down Expand Up @@ -965,7 +965,7 @@ public TargetPattern parse(String pattern) throws TargetParsingException {
PackageIdentifier packageIdentifier;
try {
packageIdentifier =
PackageIdentifier.parse(repository.getName() + "//" + realPackagePart);
PackageIdentifier.parse(repository.getNameWithAt() + "//" + realPackagePart);
} catch (LabelSyntaxException e) {
throw new TargetParsingException(
"Invalid package name '" + realPackagePart + "': " + e.getMessage(),
Expand All @@ -981,7 +981,8 @@ public TargetPattern parse(String pattern) throws TargetParsingException {
if (ALL_RULES_IN_SUFFIXES.contains(targetPart)) {
PackageIdentifier packageIdentifier;
try {
packageIdentifier = PackageIdentifier.parse(repository.getName() + "//" + packagePart);
packageIdentifier =
PackageIdentifier.parse(repository.getNameWithAt() + "//" + packagePart);
} catch (LabelSyntaxException e) {
throw new TargetParsingException(
"Invalid package name '" + packagePart + "': " + e.getMessage(),
Expand All @@ -994,7 +995,8 @@ public TargetPattern parse(String pattern) throws TargetParsingException {
if (ALL_TARGETS_IN_SUFFIXES.contains(targetPart)) {
PackageIdentifier packageIdentifier;
try {
packageIdentifier = PackageIdentifier.parse(repository.getName() + "//" + packagePart);
packageIdentifier =
PackageIdentifier.parse(repository.getNameWithAt() + "//" + packagePart);
} catch (LabelSyntaxException e) {
throw new TargetParsingException(
"Invalid package name '" + packagePart + "': " + e.getMessage(),
Expand All @@ -1006,7 +1008,7 @@ public TargetPattern parse(String pattern) throws TargetParsingException {

if (includesRepo || wasOriginallyAbsolute || pattern.contains(":")) {
PackageIdentifier packageIdentifier;
String fullLabel = repository.getName() + "//" + pattern;
String fullLabel = repository.getNameWithAt() + "//" + pattern;
try {
PackageAndTarget packageAndTarget = LabelValidator.validateAbsoluteLabel(fullLabel);
packageIdentifier =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static void throwIfNotBuiltinUsage(StarlarkThread thread) throws EvalExce
BazelModuleContext.of(Module.ofInnermostEnclosingStarlarkFunction(thread))
.label()
.getRepository();
if (!"@_builtins".equals(repository.getName())) {
if (!"@_builtins".equals(repository.getNameWithAt())) {
throw Starlark.errorf("private API only for use in builtins");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Path getPackageBuildFileNullable(
Path buildFile =
outputBase
.getRelative(LabelConstants.EXTERNAL_REPOSITORY_LOCATION)
.getRelative(packageIdentifier.getRepository().strippedName())
.getRelative(packageIdentifier.getRepository().getName())
.getRelative(packageIdentifier.getSourceRoot())
.getRelative(buildFileName.getFilenameFragment());
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public boolean isMandatoryMinimumVersionForStarlark(StarlarkThread thread) throw
BazelModuleContext.of(Module.ofInnermostEnclosingStarlarkFunction(thread))
.label()
.getRepository();
if (!"@_builtins".equals(repository.getName())) {
if (!"@_builtins".equals(repository.getNameWithAt())) {
throw Starlark.errorf("private API only for use by builtins");
}
return isMandatoryMinimumVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
new ComputedDefault() {
@Override
public Object getDefault(AttributeMap rule) {
return env.getToolsRepository().strippedName();
return env.getToolsRepository().getName();
}
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,7 @@ public static boolean isOldStarlarkApiWhiteListed(

RuleClass ruleClass = rule.getRuleClassObject();
Label label = ruleClass.getRuleDefinitionEnvironmentLabel();
if (label.getRepository().getName().equals("@_builtins")) {
if (label.getRepository().getNameWithAt().equals("@_builtins")) {
// always permit builtins
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ public static String getDLLHashSuffix(
&& (!ruleContext.isAttrDefined("win_def_file", LABEL)
|| ruleContext.getPrerequisiteArtifact("win_def_file") == null)) {
Fingerprint digest = new Fingerprint();
digest.addString(ruleContext.getRepository().getName());
digest.addString(ruleContext.getRepository().getNameWithAt());
digest.addPath(ruleContext.getPackageDirectory());
return "_" + digest.hexDigestAndReset().substring(0, 10);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private void refreshMappings(@Nullable WorkspaceFileValue newValue) {
}

ImmutableSortedMap.Builder<RepositoryName, ImmutableSet<PathFragment>> reposMapBuilder =
new ImmutableSortedMap.Builder<>(Comparator.comparing(RepositoryName::getName));
new ImmutableSortedMap.Builder<>(Comparator.comparing(RepositoryName::getNameWithAt));
for (Map.Entry<RepositoryName, Set<PathFragment>> entry : reposMap.entrySet()) {
reposMapBuilder.put(entry.getKey(), ImmutableSet.copyOf(entry.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,20 +249,20 @@ public SkyValue compute(SkyKey skyKey, Environment env)
DONT_FETCH_UNCONDITIONALLY.equals(DEPENDENCY_FOR_UNCONDITIONAL_FETCHING.get(env));
boolean needsConfiguring = false;

Path repoRoot = RepositoryFunction.getExternalRepositoryDirectory(directories)
.getRelative(repositoryName.strippedName());
Path repoRoot =
RepositoryFunction.getExternalRepositoryDirectory(directories)
.getRelative(repositoryName.getName());

if (Preconditions.checkNotNull(overrides).containsKey(repositoryName)) {
DigestWriter.clearMarkerFile(directories, repositoryName);
return setupOverride(
overrides.get(repositoryName), env, repoRoot, repositoryName.strippedName());
return setupOverride(overrides.get(repositoryName), env, repoRoot, repositoryName.getName());
}

Rule rule = null;

if (Preconditions.checkNotNull(ENABLE_BZLMOD.get(env))) {
// Trys to get a repository rule instance from Bzlmod generated repos.
SkyKey key = BzlmodRepoRuleValue.key(repositoryName.strippedName());
SkyKey key = BzlmodRepoRuleValue.key(repositoryName.getName());
BzlmodRepoRuleValue value = (BzlmodRepoRuleValue) env.getValue(key);

if (env.valuesMissing()) {
Expand Down Expand Up @@ -410,7 +410,7 @@ private RepositoryDirectoryValue.Builder fetchRepository(

setupRepositoryRoot(repoRoot);

String repositoryName = ((RepositoryName) skyKey.argument()).getName();
String repositoryName = ((RepositoryName) skyKey.argument()).getNameWithAt();
env.getListener().post(new RepositoryFetching(repositoryName, false));

RepositoryDirectoryValue.Builder repoBuilder;
Expand Down Expand Up @@ -448,7 +448,7 @@ private RepositoryDirectoryValue.Builder fetchRepository(
private Rule getRepoRuleFromWorkspace(RepositoryName repositoryName, Environment env)
throws InterruptedException, RepositoryFunctionException, NoSuchRepositoryException {
try {
return externalPackageHelper.getRuleByName(repositoryName.strippedName(), env);
return externalPackageHelper.getRuleByName(repositoryName.getName(), env);
} catch (ExternalRuleNotFoundException e) {
// This is caught and handled immediately in compute().
throw new NoSuchRepositoryException();
Expand Down Expand Up @@ -526,7 +526,7 @@ private static class DigestWriter {
Rule rule,
ImmutableSet<PathFragment> managedDirectories) {
ruleKey = computeRuleKey(rule);
markerPath = getMarkerPath(directories, repositoryName.strippedName());
markerPath = getMarkerPath(directories, repositoryName.getName());
this.rule = rule;
markerData = Maps.newHashMap();

Expand Down Expand Up @@ -646,7 +646,7 @@ private static Path getMarkerPath(BlazeDirectories directories, String ruleName)
static void clearMarkerFile(BlazeDirectories directories, RepositoryName repoName)
throws RepositoryFunctionException {
try {
getMarkerPath(directories, repoName.strippedName()).delete();
getMarkerPath(directories, repoName.getName()).delete();
} catch (IOException e) {
throw new RepositoryFunctionException(e, Transience.TRANSIENT);
}
Expand Down
Loading

0 comments on commit 3702848

Please sign in to comment.