Skip to content

Commit

Permalink
Use unqualified label where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Gunnar Wagenknecht committed Aug 7, 2024
1 parent 05c3e77 commit 7385e89
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,31 @@

public class AddDependenciesJob extends WorkspaceJob {

protected static String toUnqualifiedLabelIfPossible(Label l) {
var targetName = l.targetName().toString();

var externalWorkspaceName = l.externalWorkspaceName();
if (l.blazePackage().isWorkspaceRoot() && targetName.equals(externalWorkspaceName)) {
// just the external workspace
return "@" + externalWorkspaceName;
}

if (!l.blazePackage().isWorkspaceRoot()) {
var lastFolderInPackagePath = l.blazePackage().asPath().getFileName();
if ((lastFolderInPackagePath != null) && lastFolderInPackagePath.toString().equals(targetName)) {
// shorten to just the package path
var label = "//" + l.blazePackage();
return externalWorkspaceName != null ? "@" + externalWorkspaceName + label : label;
}
}

// use full qualified label
return l.toString();
}

protected final BazelProject bazelProject;
protected final Collection<Label> labelsToAdd;

protected final Collection<ClasspathEntry> newClasspathEntries;

public AddDependenciesJob(BazelProject bazelProject, Collection<Label> labelsToAdd,
Expand Down Expand Up @@ -190,7 +213,9 @@ protected int updateTargetsUsingBuildozer(List<String> targetsToUpdate, String d
var workspaceRoot = bazelProject.getBazelWorkspace().getLocation().toPath();
var buildozerCommand = new BuildozerCommand(
workspaceRoot,
labelsToAdd.stream().map(l -> format("add %s %s", depsAttributeName, l)).collect(toList()),
labelsToAdd.stream()
.map(l -> format("add %s %s", depsAttributeName, toUnqualifiedLabelIfPossible(l)))
.collect(toList()),
targetsForBuildozerToUpdate,
format(
"Add label(s) '%s' to '%s'",
Expand Down

0 comments on commit 7385e89

Please sign in to comment.