Skip to content

Commit a6ebf07

Browse files
SalmaSamycopybara-github
authored andcommitted
Store repospec instead of package for module extension
PiperOrigin-RevId: 529679264 Change-Id: Ib9d410f5de1bac171c3c6f642b8584986181e44b
1 parent f1af869 commit a6ebf07

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionEvalStarlarkThreadContext.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.google.devtools.build.lib.cmdline.RepositoryName;
2525
import com.google.devtools.build.lib.events.ExtendedEventHandler;
2626
import com.google.devtools.build.lib.packages.NoSuchPackageException;
27-
import com.google.devtools.build.lib.packages.Package;
2827
import com.google.devtools.build.lib.packages.Rule;
2928
import com.google.devtools.build.lib.packages.RuleClass;
3029
import com.google.devtools.build.lib.packages.RuleFactory.InvalidRuleException;
@@ -53,14 +52,14 @@ public static ModuleExtensionEvalStarlarkThreadContext from(StarlarkThread threa
5352
}
5453

5554
@AutoValue
56-
abstract static class PackageAndLocation {
57-
abstract Package getPackage();
55+
abstract static class RepoSpecAndLocation {
56+
abstract RepoSpec getRepoSpec();
5857

5958
abstract Location getLocation();
6059

61-
static PackageAndLocation create(Package pkg, Location location) {
62-
return new AutoValue_ModuleExtensionEvalStarlarkThreadContext_PackageAndLocation(
63-
pkg, location);
60+
static RepoSpecAndLocation create(RepoSpec repoSpec, Location location) {
61+
return new AutoValue_ModuleExtensionEvalStarlarkThreadContext_RepoSpecAndLocation(
62+
repoSpec, location);
6463
}
6564
}
6665

@@ -69,7 +68,7 @@ static PackageAndLocation create(Package pkg, Location location) {
6968
private final RepositoryMapping repoMapping;
7069
private final BlazeDirectories directories;
7170
private final ExtendedEventHandler eventHandler;
72-
private final Map<String, PackageAndLocation> generatedRepos = new HashMap<>();
71+
private final Map<String, RepoSpecAndLocation> generatedRepos = new HashMap<>();
7372

7473
public ModuleExtensionEvalStarlarkThreadContext(
7574
String repoPrefix,
@@ -84,11 +83,6 @@ public ModuleExtensionEvalStarlarkThreadContext(
8483
this.eventHandler = eventHandler;
8584
}
8685

87-
/** The string that should be prefixed to the names of all repos generated by this extension. */
88-
public String getRepoPrefix() {
89-
return repoPrefix;
90-
}
91-
9286
public void createRepo(StarlarkThread thread, Dict<String, Object> kwargs, RuleClass ruleClass)
9387
throws InterruptedException, EvalException {
9488
Object nameValue = kwargs.getOrDefault("name", Starlark.NONE);
@@ -98,7 +92,7 @@ public void createRepo(StarlarkThread thread, Dict<String, Object> kwargs, RuleC
9892
}
9993
String name = (String) nameValue;
10094
RepositoryName.validateUserProvidedRepoName(name);
101-
PackageAndLocation conflict = generatedRepos.get(name);
95+
RepoSpecAndLocation conflict = generatedRepos.get(name);
10296
if (conflict != null) {
10397
throw Starlark.errorf(
10498
"A repo named %s is already generated by this module extension at %s",
@@ -116,8 +110,17 @@ public void createRepo(StarlarkThread thread, Dict<String, Object> kwargs, RuleC
116110
"RepositoryRuleFunction.createRule",
117111
ruleClass,
118112
Maps.transformEntries(kwargs, (k, v) -> k.equals("name") ? prefixedName : v));
119-
generatedRepos.put(
120-
name, PackageAndLocation.create(rule.getPackage(), thread.getCallerLocation()));
113+
114+
Map<String, Object> attributes = Maps.transformEntries(kwargs, (k, v) -> rule.getAttr(k));
115+
String bzlFile = ruleClass.getRuleDefinitionEnvironmentLabel().getUnambiguousCanonicalForm();
116+
RepoSpec repoSpec =
117+
RepoSpec.builder()
118+
.setBzlFile(bzlFile)
119+
.setRuleClassName(ruleClass.getName())
120+
.setAttributes(AttributeValues.create(attributes))
121+
.build();
122+
123+
generatedRepos.put(name, RepoSpecAndLocation.create(repoSpec, thread.getCallerLocation()));
121124
} catch (InvalidRuleException | NoSuchPackageException e) {
122125
throw Starlark.errorf("%s", e.getMessage());
123126
}
@@ -128,8 +131,8 @@ public void createRepo(StarlarkThread thread, Dict<String, Object> kwargs, RuleC
128131
* specified by the extension) of the repo, and the value is the package containing (only) the
129132
* repo rule.
130133
*/
131-
public ImmutableMap<String, Package> getGeneratedRepos() {
134+
public ImmutableMap<String, RepoSpec> getGeneratedRepoSpecs() {
132135
return ImmutableMap.copyOf(
133-
Maps.transformValues(generatedRepos, PackageAndLocation::getPackage));
136+
Maps.transformValues(generatedRepos, RepoSpecAndLocation::getRepoSpec));
134137
}
135138
}

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/SingleExtensionEvalFunction.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
199199
ModuleExtensionMetadata metadata = (ModuleExtensionMetadata) returnValue;
200200
metadata.evaluate(
201201
usagesValue.getExtensionUsages().values(),
202-
threadContext.getGeneratedRepos().keySet(),
202+
threadContext.getGeneratedRepoSpecs().keySet(),
203203
env.getListener());
204204
}
205205
} catch (NeedsSkyframeRestartException e) {
@@ -227,7 +227,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
227227
// Check that all imported repos have been actually generated
228228
for (ModuleExtensionUsage usage : usagesValue.getExtensionUsages().values()) {
229229
for (Entry<String, String> repoImport : usage.getImports().entrySet()) {
230-
if (!threadContext.getGeneratedRepos().containsKey(repoImport.getValue())) {
230+
if (!threadContext.getGeneratedRepoSpecs().containsKey(repoImport.getValue())) {
231231
throw new SingleExtensionEvalFunctionException(
232232
ExternalDepsException.withMessage(
233233
Code.BAD_MODULE,
@@ -239,15 +239,15 @@ public SkyValue compute(SkyKey skyKey, Environment env)
239239
repoImport.getKey(),
240240
usage.getLocation(),
241241
SpellChecker.didYouMean(
242-
repoImport.getValue(), threadContext.getGeneratedRepos().keySet())),
242+
repoImport.getValue(), threadContext.getGeneratedRepoSpecs().keySet())),
243243
Transience.PERSISTENT);
244244
}
245245
}
246246
}
247247

248248
return SingleExtensionEvalValue.create(
249-
threadContext.getGeneratedRepos(),
250-
threadContext.getGeneratedRepos().keySet().stream()
249+
threadContext.getGeneratedRepoSpecs(),
250+
threadContext.getGeneratedRepoSpecs().keySet().stream()
251251
.collect(
252252
toImmutableBiMap(
253253
e ->

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/SingleExtensionEvalValue.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
public abstract class SingleExtensionEvalValue implements SkyValue {
3434
/**
3535
* Returns the repos generated by the extension. The key is the "internal name" (as specified by
36-
* the extension) of the repo, and the value is the package containing (only) the repo rule.
36+
* the extension) of the repo, and the value is the the repo specs that defins the repository .
3737
*/
38-
public abstract ImmutableMap<String, Package> getGeneratedRepos();
38+
public abstract ImmutableMap<String, RepoSpec> getGeneratedRepoSpecs();
3939

4040
/**
4141
* Returns the mapping from the canonical repo names of the repos generated by this extension to
@@ -45,9 +45,10 @@ public abstract class SingleExtensionEvalValue implements SkyValue {
4545

4646
@AutoCodec.Instantiator
4747
public static SingleExtensionEvalValue create(
48-
ImmutableMap<String, Package> generatedRepos,
48+
ImmutableMap<String, RepoSpec> generatedRepoSpecs,
4949
ImmutableBiMap<RepositoryName, String> canonicalRepoNameToInternalNames) {
50-
return new AutoValue_SingleExtensionEvalValue(generatedRepos, canonicalRepoNameToInternalNames);
50+
return new AutoValue_SingleExtensionEvalValue(
51+
generatedRepoSpecs, canonicalRepoNameToInternalNames);
5152
}
5253

5354
public static Key key(ModuleExtensionId id) {

src/main/java/com/google/devtools/build/lib/skyframe/BzlmodRepoRuleFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ public SkyValue compute(SkyKey skyKey, Environment env)
140140
if (internalRepo == null) {
141141
return BzlmodRepoRuleValue.REPO_RULE_NOT_FOUND_VALUE;
142142
}
143-
Package pkg = extensionEval.getGeneratedRepos().get(internalRepo);
143+
RepoSpec extRepoSpec = extensionEval.getGeneratedRepoSpecs().get(internalRepo);
144+
Package pkg = createRuleFromSpec(extRepoSpec, starlarkSemantics, env).getRule().getPackage();
144145
Preconditions.checkNotNull(pkg);
145146

146147
return new BzlmodRepoRuleValue(pkg, repositoryName.getName());

0 commit comments

Comments
 (0)