-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate selection from bazel dependency graph
* Created new K/F/V for Bazel dependency graph named "BazelDepGraph" * Refactored "BazelModuleResolution" to only run resolution (discovery, selection and checks) to create pruned and unpruned graph and not create any dependency value * BazelDepGraphResolution calls BazelModuleResolution and extracts the dependency graph from it * Updated tests PiperOrigin-RevId: 499026445 Change-Id: Id1237f9d09015ffe8987d933431fccfcfa0c0963
- Loading branch information
1 parent
763f966
commit 9860051
Showing
25 changed files
with
741 additions
and
502 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package com.google.devtools.build.lib.bazel.bzlmod; | ||
|
||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
|
||
import com.google.common.collect.BiMap; | ||
import com.google.common.collect.HashBiMap; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableTable; | ||
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleResolutionFunction.BazelModuleResolutionFunctionException; | ||
import com.google.devtools.build.lib.cmdline.LabelSyntaxException; | ||
import com.google.devtools.build.lib.cmdline.PackageIdentifier; | ||
import com.google.devtools.build.lib.cmdline.RepositoryName; | ||
import com.google.devtools.build.lib.packages.LabelConverter; | ||
import com.google.devtools.build.lib.server.FailureDetails.ExternalDeps.Code; | ||
import com.google.devtools.build.lib.vfs.PathFragment; | ||
import com.google.devtools.build.skyframe.SkyFunction; | ||
import com.google.devtools.build.skyframe.SkyFunctionException; | ||
import com.google.devtools.build.skyframe.SkyFunctionException.Transience; | ||
import com.google.devtools.build.skyframe.SkyKey; | ||
import com.google.devtools.build.skyframe.SkyValue; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* This function runs Bazel module resolution, extracts the dependency graph from it and creates a | ||
* value containing all Bazel modules, along with a few lookup maps that help with further usage. By | ||
* this stage, module extensions are not evaluated yet. | ||
*/ | ||
public class BazelDepGraphFunction implements SkyFunction { | ||
|
||
@Override | ||
@Nullable | ||
public SkyValue compute(SkyKey skyKey, Environment env) | ||
throws SkyFunctionException, InterruptedException { | ||
|
||
BazelModuleResolutionValue selectionResult = | ||
(BazelModuleResolutionValue) env.getValue(BazelModuleResolutionValue.KEY); | ||
if (env.valuesMissing()) { | ||
return null; | ||
} | ||
|
||
ImmutableMap<ModuleKey, Module> depGraph = selectionResult.getResolvedDepGraph(); | ||
ImmutableMap<RepositoryName, ModuleKey> canonicalRepoNameLookup = | ||
depGraph.keySet().stream() | ||
.collect(toImmutableMap(ModuleKey::getCanonicalRepoName, key -> key)); | ||
|
||
// For each extension usage, we resolve (i.e. canonicalize) its bzl file label. Then we can | ||
// group all usages by the label + name (the ModuleExtensionId). | ||
ImmutableTable.Builder<ModuleExtensionId, ModuleKey, ModuleExtensionUsage> | ||
extensionUsagesTableBuilder = ImmutableTable.builder(); | ||
for (Module module : depGraph.values()) { | ||
LabelConverter labelConverter = | ||
new LabelConverter( | ||
PackageIdentifier.create(module.getCanonicalRepoName(), PathFragment.EMPTY_FRAGMENT), | ||
module.getRepoMappingWithBazelDepsOnly()); | ||
for (ModuleExtensionUsage usage : module.getExtensionUsages()) { | ||
try { | ||
ModuleExtensionId moduleExtensionId = | ||
ModuleExtensionId.create( | ||
labelConverter.convert(usage.getExtensionBzlFile()), usage.getExtensionName()); | ||
extensionUsagesTableBuilder.put(moduleExtensionId, module.getKey(), usage); | ||
} catch (LabelSyntaxException e) { | ||
throw new BazelModuleResolutionFunctionException( | ||
ExternalDepsException.withCauseAndMessage( | ||
Code.BAD_MODULE, | ||
e, | ||
"invalid label for module extension found at %s", | ||
usage.getLocation()), | ||
Transience.PERSISTENT); | ||
} | ||
} | ||
} | ||
ImmutableTable<ModuleExtensionId, ModuleKey, ModuleExtensionUsage> extensionUsagesById = | ||
extensionUsagesTableBuilder.buildOrThrow(); | ||
|
||
// Calculate a unique name for each used extension id. | ||
BiMap<String, ModuleExtensionId> extensionUniqueNames = HashBiMap.create(); | ||
for (ModuleExtensionId id : extensionUsagesById.rowKeySet()) { | ||
// Ensure that the resulting extension name (and thus the repository names derived from it) do | ||
// not start with a tilde. | ||
RepositoryName repository = id.getBzlFileLabel().getRepository(); | ||
String nonEmptyRepoPart; | ||
if (repository.isMain()) { | ||
nonEmptyRepoPart = "_main"; | ||
} else { | ||
nonEmptyRepoPart = repository.getName(); | ||
} | ||
String bestName = nonEmptyRepoPart + "~" + id.getExtensionName(); | ||
if (extensionUniqueNames.putIfAbsent(bestName, id) == null) { | ||
continue; | ||
} | ||
int suffix = 2; | ||
while (extensionUniqueNames.putIfAbsent(bestName + suffix, id) != null) { | ||
suffix++; | ||
} | ||
} | ||
|
||
return BazelDepGraphValue.create( | ||
depGraph, | ||
canonicalRepoNameLookup, | ||
depGraph.values().stream().map(AbridgedModule::from).collect(toImmutableList()), | ||
extensionUsagesById, | ||
ImmutableMap.copyOf(extensionUniqueNames.inverse())); | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelDepGraphValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2022 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
package com.google.devtools.build.lib.bazel.bzlmod; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.collect.ImmutableTable; | ||
import com.google.devtools.build.lib.cmdline.RepositoryMapping; | ||
import com.google.devtools.build.lib.cmdline.RepositoryName; | ||
import com.google.devtools.build.lib.skyframe.SkyFunctions; | ||
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant; | ||
import com.google.devtools.build.skyframe.SkyKey; | ||
import com.google.devtools.build.skyframe.SkyValue; | ||
import java.util.Map; | ||
|
||
/** | ||
* The result of running Bazel module resolution, containing the Bazel module dependency graph | ||
* post-version-resolution. | ||
*/ | ||
@AutoValue | ||
public abstract class BazelDepGraphValue implements SkyValue { | ||
@SerializationConstant public static final SkyKey KEY = () -> SkyFunctions.BAZEL_DEP_GRAPH; | ||
|
||
public static BazelDepGraphValue create( | ||
ImmutableMap<ModuleKey, Module> depGraph, | ||
ImmutableMap<RepositoryName, ModuleKey> canonicalRepoNameLookup, | ||
ImmutableList<AbridgedModule> abridgedModules, | ||
ImmutableTable<ModuleExtensionId, ModuleKey, ModuleExtensionUsage> extensionUsagesTable, | ||
ImmutableMap<ModuleExtensionId, String> extensionUniqueNames) { | ||
return new AutoValue_BazelDepGraphValue( | ||
depGraph, | ||
canonicalRepoNameLookup, | ||
abridgedModules, | ||
extensionUsagesTable, | ||
extensionUniqueNames); | ||
} | ||
|
||
/** | ||
* The post-selection dep graph. Must have BFS iteration order, starting from the root module. For | ||
* any KEY in the returned map, it's guaranteed that {@code depGraph[KEY].getKey() == KEY}. | ||
*/ | ||
public abstract ImmutableMap<ModuleKey, Module> getDepGraph(); | ||
|
||
/** A mapping from a canonical repo name to the key of the module backing it. */ | ||
public abstract ImmutableMap<RepositoryName, ModuleKey> getCanonicalRepoNameLookup(); | ||
|
||
/** All modules in the same order as {@link #getDepGraph}, but with limited information. */ | ||
public abstract ImmutableList<AbridgedModule> getAbridgedModules(); | ||
|
||
/** | ||
* All module extension usages grouped by the extension's ID and the key of the module where this | ||
* usage occurs. For each extension identifier ID, extensionUsagesTable[ID][moduleKey] is the | ||
* ModuleExtensionUsage of ID in the module keyed by moduleKey. | ||
*/ | ||
public abstract ImmutableTable<ModuleExtensionId, ModuleKey, ModuleExtensionUsage> | ||
getExtensionUsagesTable(); | ||
|
||
/** | ||
* A mapping from the ID of a module extension to a unique string that serves as its "name". This | ||
* is not the same as the extension's declared name, as the declared name is only unique within | ||
* the .bzl file, whereas this unique name is guaranteed to be unique across the workspace. | ||
*/ | ||
public abstract ImmutableMap<ModuleExtensionId, String> getExtensionUniqueNames(); | ||
|
||
/** | ||
* Returns the full {@link RepositoryMapping} for the given module, including repos from Bazel | ||
* module deps and module extensions. | ||
*/ | ||
public final RepositoryMapping getFullRepoMapping(ModuleKey key) { | ||
ImmutableMap.Builder<String, RepositoryName> mapping = ImmutableMap.builder(); | ||
for (Map.Entry<ModuleExtensionId, ModuleExtensionUsage> e : | ||
getExtensionUsagesTable().column(key).entrySet()) { | ||
ModuleExtensionId extensionId = e.getKey(); | ||
ModuleExtensionUsage usage = e.getValue(); | ||
for (Map.Entry<String, String> entry : usage.getImports().entrySet()) { | ||
String canonicalRepoName = | ||
getExtensionUniqueNames().get(extensionId) + "~" + entry.getValue(); | ||
mapping.put(entry.getKey(), RepositoryName.createUnvalidated(canonicalRepoName)); | ||
} | ||
} | ||
return getDepGraph() | ||
.get(key) | ||
.getRepoMappingWithBazelDepsOnly() | ||
.withAdditionalMappings(mapping.buildOrThrow()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.