Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 417815015
  • Loading branch information
Googler authored and copybara-github committed Dec 22, 2021
1 parent 30fd508 commit 80ada0f
Show file tree
Hide file tree
Showing 79 changed files with 493 additions and 400 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,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().getNameWithAt())) {
if (rdeLabel != null && !"@_builtins".equals(rdeLabel.getRepository().getName())) {
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 @@ -444,7 +444,7 @@ public boolean isStrictFilesetOutput() {
}

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

/**
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.getName());
Path execRoot = directories.getExecRoot(mainRepositoryName.strippedName());
// 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.getName());
this.execRoot = directories.getExecRoot(mainRepositoryName.strippedName());
}

private static void addMnemonicPart(
Expand Down Expand Up @@ -265,7 +265,7 @@ private ArtifactRoot buildDerivedRoot(
execRoot,
rootType,
directories.getRelativeOutputPath(),
repository.getName(),
repository.strippedName(),
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.getNameWithAt())) {
if (!"@_builtins".equals(repository.getName())) {
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 @@ -457,7 +457,7 @@ public Descriptor labelAttribute(
Label label =
((BazelModuleContext) Module.ofInnermostEnclosingStarlarkFunction(thread).getClientData())
.label();
if (!label.getPackageIdentifier().getRepository().getName().equals("_builtins")) {
if (!label.getPackageIdentifier().getRepository().toString().equals("@_builtins")) {
throw Starlark.errorf("Rule in '%s' cannot use private API", label.getPackageName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public StarlarkCallable rule(
}

if (compileOneFiletype instanceof Sequence) {
if (!bzlModule.label().getRepository().getNameWithAt().equals("@_builtins")) {
if (!bzlModule.label().getRepository().getName().equals("@_builtins")) {
throw Starlark.errorf(
"Rule in '%s' cannot use private API", bzlModule.label().getPackageName());
}
Expand Down Expand Up @@ -909,7 +909,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().getNameWithAt().equals("@_builtins")) {
if (!starlarkLabel.getRepository().getName().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::getNameWithAt)
.map(RepositoryName::getName)
.collect(Collectors.toSet());
if (!conflicting.isEmpty()) {
String message =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static BazelModuleResolutionValue createValue(
BiMap<String, ModuleExtensionId> extensionUniqueNames = HashBiMap.create();
for (ModuleExtensionId id : extensionUsagesById.rowKeySet()) {
String bestName =
id.getBzlFileLabel().getRepository().getName() + "." + id.getExtensionName();
id.getBzlFileLabel().getRepository().strippedName() + "." + id.getExtensionName();
if (extensionUniqueNames.putIfAbsent(bestName, id) == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public final RepositoryMapping getFullRepoMapping(ModuleKey key) {
String canonicalRepoName =
getExtensionUniqueNames().get(extensionId) + "." + entry.getValue();
mapping.put(
RepositoryName.createUnvalidated(entry.getKey()),
RepositoryName.createUnvalidated(canonicalRepoName));
RepositoryName.createFromValidStrippedName(entry.getKey()),
RepositoryName.createFromValidStrippedName(canonicalRepoName));
}
}
return getDepGraph()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ public final RepositoryMapping getRepoMappingWithBazelDepsOnly() {
// If this is the root module, this perfectly falls into @<module name> => @
if (!getName().isEmpty()) {
mapping.put(
RepositoryName.createUnvalidated(getName()),
RepositoryName.createUnvalidated(getCanonicalRepoName()));
RepositoryName.createFromValidStrippedName(getName()),
RepositoryName.createFromValidStrippedName(getCanonicalRepoName()));
}
for (Map.Entry<String, ModuleKey> dep : getDeps().entrySet()) {
// Special note: if `dep` is actually the root module, its ModuleKey would be ROOT whose
// canonicalRepoName is the empty string. This perfectly maps to the main repo ("@").
mapping.put(
RepositoryName.createUnvalidated(dep.getKey()),
RepositoryName.createUnvalidated(dep.getValue().getCanonicalRepoName()));
RepositoryName.createFromValidStrippedName(dep.getKey()),
RepositoryName.createFromValidStrippedName(dep.getValue().getCanonicalRepoName()));
}
return RepositoryMapping.create(mapping.build(), getCanonicalRepoName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private GetModuleFileResult getModuleFile(
(RepositoryDirectoryValue)
env.getValue(
RepositoryDirectoryValue.key(
RepositoryName.createUnvalidated(canonicalRepoName)));
RepositoryName.createFromValidStrippedName(canonicalRepoName)));
if (repoDir == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ private StarlarkBazelModule(String name, String version, Tags tags) {
static Label createModuleRootLabel(String canonicalRepoName) {
return Label.createUnvalidated(
PackageIdentifier.create(
RepositoryName.createUnvalidated(canonicalRepoName), PathFragment.EMPTY_FRAGMENT),
RepositoryName.createFromValidStrippedName(canonicalRepoName),
PathFragment.EMPTY_FRAGMENT),
"unused_dummy_target_name");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
// TODO(aehlig): avoid the detour of serializing and then parsing the repository name
try {
repositoriesToFetch.add(
RepositoryDirectoryValue.key(RepositoryName.create(rule.getName())));
RepositoryDirectoryValue.key(RepositoryName.create("@" + rule.getName())));
} catch (LabelSyntaxException e) {
String errorMessage =
String.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.google.common.eventbus.Subscribe;
import com.google.devtools.build.lib.bazel.repository.cache.RepositoryCacheHitEvent;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.repository.RepositoryFailedEvent;
Expand Down Expand Up @@ -56,7 +57,7 @@ public synchronized void cacheHit(RepositoryCacheHitEvent event) {

@Subscribe
public void failed(RepositoryFailedEvent event) {
String repo = event.getRepo();
String repo = RepositoryName.stripName(event.getRepo());
Set<Pair<String, URL>> cacheHits = cacheHitsByRepo.get(repo);
if (cacheHits != null && !cacheHits.isEmpty()) {
StringBuilder info = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public RepositoryOverride convert(String input) throws OptionsParsingException {
"Repository override directory must be an absolute path", input);
}
try {
return RepositoryOverride.create(RepositoryName.create(pieces[0]), path);
return RepositoryOverride.create(RepositoryName.create("@" + pieces[0]), path);
} catch (LabelSyntaxException e) {
throw new OptionsParsingException("Invalid repository name given to override", input);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/cmdline/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/vfs:ospathpolicy",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//third_party:auto_value",
"//third_party:caffeine",
"//third_party:guava",
"//third_party:jsr305",
],
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/com/google/devtools/build/lib/cmdline/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ private static Label parseCanonical(String raw) throws LabelSyntaxException {
Parts parts = Parts.parse(raw);
parts.checkPkgIsAbsolute();
RepositoryName repoName =
parts.repo == null ? RepositoryName.MAIN : RepositoryName.createUnvalidated(parts.repo);
parts.repo == null
? RepositoryName.MAIN
: RepositoryName.createFromValidStrippedName(parts.repo);
return createUnvalidated(
PackageIdentifier.create(repoName, PathFragment.create(parts.pkg)), parts.target);
}
Expand All @@ -103,7 +105,7 @@ private static RepositoryName computeRepoNameWithRepoContext(
return ABSOLUTE_PACKAGE_NAMES.contains(parts.pkg) ? RepositoryName.MAIN : currentRepo;
}
// TODO(b/200024947): Make repo mapping take a string and return a RepositoryName.
return repoMapping.get(RepositoryName.createUnvalidated(parts.repo));
return repoMapping.get(RepositoryName.createFromValidStrippedName(parts.repo));
}

// TODO(b/200024947): Make this public.
Expand Down Expand Up @@ -275,7 +277,9 @@ public static Label parseCommandLineLabel(String raw, PathFragment workspaceRela
}
// TODO(b/200024947): This method will eventually need to take a repo mapping too.
RepositoryName repoName =
parts.repo == null ? RepositoryName.MAIN : RepositoryName.createUnvalidated(parts.repo);
parts.repo == null
? RepositoryName.MAIN
: RepositoryName.createFromValidStrippedName(parts.repo);
return create(PackageIdentifier.create(repoName, pathFragment), parts.target);
}

Expand Down Expand Up @@ -402,7 +406,7 @@ public String getCanonicalForm() {
}

public String getUnambiguousCanonicalForm() {
return packageIdentifier.getRepository().getNameWithAt()
return packageIdentifier.getRepository()
+ "//"
+ packageIdentifier.getPackageFragment()
+ ":"
Expand All @@ -418,7 +422,7 @@ public String getUnambiguousCanonicalForm() {
+ "<pre class=language-python>Label(\"@foo//bar:baz\").workspace_name"
+ " == \"foo\"</pre>")
public String getWorkspaceName() {
return packageIdentifier.getRepository().getName();
return packageIdentifier.getRepository().strippedName();
}

/**
Expand All @@ -435,7 +439,7 @@ public String toShorthandString() {
if (packageIdentifier.getRepository().isMain()) {
repository = "";
} else {
repository = packageIdentifier.getRepository().getNameWithAt();
repository = packageIdentifier.getRepository().getName();
}
return repository + "//" + getPackageFragment();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,14 @@ static Parts parse(String rawLabel) throws LabelSyntaxException {
return validateAndCreate(repo, pkgIsAbsolute, pkg, target, rawLabel);
}

@Nullable
private static void validateRepoName(@Nullable String repo) throws LabelSyntaxException {
if (repo != null) {
RepositoryName.validate(repo);
if (repo == null) {
return;
}
String error = RepositoryName.validate('@' + repo);
if (error != null) {
throw syntaxErrorf("invalid repository name '@%s': %s", repo, error);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static PackageIdentifier discoverFromExecPath(
if (tofind.startsWith(prefix)) {
// Using the path prefix can be either "external" or "..", depending on whether the sibling
// repository layout is used.
RepositoryName repository = RepositoryName.createUnvalidated(tofind.getSegment(1));
RepositoryName repository = RepositoryName.createFromValidStrippedName(tofind.getSegment(1));
return PackageIdentifier.create(repository, tofind.subFragment(2));
} else {
return PackageIdentifier.createInMainRepo(tofind);
Expand Down Expand Up @@ -116,7 +116,9 @@ public static PackageIdentifier parse(String input) throws LabelSyntaxException
}
LabelParser.Parts parts = LabelParser.Parts.parse(input + ":dummy_target");
RepositoryName repoName =
parts.repo == null ? RepositoryName.MAIN : RepositoryName.createUnvalidated(parts.repo);
parts.repo == null
? RepositoryName.MAIN
: RepositoryName.createFromValidStrippedName(parts.repo);
return create(repoName, PathFragment.create(parts.pkg));
}

Expand Down Expand Up @@ -146,7 +148,7 @@ public PathFragment getPackagePath(boolean siblingRepositoryLayout) {
return repository.isMain() || siblingRepositoryLayout
? pkgName
: LabelConstants.EXTERNAL_PATH_PREFIX
.getRelative(repository.getName())
.getRelative(repository.strippedName())
.getRelative(pkgName);
}

Expand All @@ -169,7 +171,8 @@ public PathFragment getRunfilesPath() {
*/
// TODO(bazel-team): Maybe rename to "getDefaultForm"?
public String getCanonicalForm() {
return repository.getCanonicalForm() + "//" + getPackageFragment();
String repository = getRepository().getCanonicalForm();
return repository + "//" + getPackageFragment();
}

/**
Expand All @@ -182,10 +185,7 @@ public String getCanonicalForm() {
// that disparity?
@Override
public String toString() {
if (repository.isMain()) {
return getPackageFragment().getPathString();
}
return getCanonicalForm();
return (repository.isMain() ? "" : repository + "//") + pkgName;
}

@Override
Expand Down Expand Up @@ -218,7 +218,7 @@ public int compareTo(PackageIdentifier that) {
return pkgName.compareTo(that.pkgName);
}
return ComparisonChain.start()
.compare(repository.getName(), that.repository.getName())
.compare(repository.toString(), that.repository.toString())
.compare(pkgName, that.pkgName)
.result();
}
Expand Down
Loading

0 comments on commit 80ada0f

Please sign in to comment.