Skip to content

Commit 71cfbba

Browse files
[6.3.0] rules_go & rules_python are failing in Downstream CI with Bazel@HEAD (#18447)
* Store repospec instead of package for module extension PiperOrigin-RevId: 529679264 Change-Id: Ib9d410f5de1bac171c3c6f642b8584986181e44b * Stop checking .bzl files visibility in RepoRuleFunction Fixes #18346 At this point we are creating a rule from an extension repospec and that is always public PiperOrigin-RevId: 532558852 Change-Id: I245de0a23f92ba6283be4bf3be6af4863b7a8c68 --------- Co-authored-by: salma-samy <[email protected]>
1 parent e023bd3 commit 71cfbba

File tree

6 files changed

+53
-41
lines changed

6 files changed

+53
-41
lines changed

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

+20-17
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

+5-5
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
200200
ModuleExtensionMetadata metadata = (ModuleExtensionMetadata) returnValue;
201201
metadata.evaluate(
202202
usagesValue.getExtensionUsages().values(),
203-
threadContext.getGeneratedRepos().keySet(),
203+
threadContext.getGeneratedRepoSpecs().keySet(),
204204
env.getListener());
205205
}
206206
} catch (NeedsSkyframeRestartException e) {
@@ -228,7 +228,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
228228
// Check that all imported repos have been actually generated
229229
for (ModuleExtensionUsage usage : usagesValue.getExtensionUsages().values()) {
230230
for (Entry<String, String> repoImport : usage.getImports().entrySet()) {
231-
if (!threadContext.getGeneratedRepos().containsKey(repoImport.getValue())) {
231+
if (!threadContext.getGeneratedRepoSpecs().containsKey(repoImport.getValue())) {
232232
throw new SingleExtensionEvalFunctionException(
233233
ExternalDepsException.withMessage(
234234
Code.BAD_MODULE,
@@ -240,15 +240,15 @@ public SkyValue compute(SkyKey skyKey, Environment env)
240240
repoImport.getKey(),
241241
usage.getLocation(),
242242
SpellChecker.didYouMean(
243-
repoImport.getValue(), threadContext.getGeneratedRepos().keySet())),
243+
repoImport.getValue(), threadContext.getGeneratedRepoSpecs().keySet())),
244244
Transience.PERSISTENT);
245245
}
246246
}
247247
}
248248

249249
return SingleExtensionEvalValue.create(
250-
threadContext.getGeneratedRepos(),
251-
threadContext.getGeneratedRepos().keySet().stream()
250+
threadContext.getGeneratedRepoSpecs(),
251+
threadContext.getGeneratedRepoSpecs().keySet().stream()
252252
.collect(
253253
toImmutableBiMap(
254254
e ->

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
public abstract class SingleExtensionEvalValue implements SkyValue {
3535
/**
3636
* Returns the repos generated by the extension. The key is the "internal name" (as specified by
37-
* the extension) of the repo, and the value is the package containing (only) the repo rule.
37+
* the extension) of the repo, and the value is the the repo specs that defins the repository .
3838
*/
39-
public abstract ImmutableMap<String, Package> getGeneratedRepos();
39+
public abstract ImmutableMap<String, RepoSpec> getGeneratedRepoSpecs();
4040

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

4747
@AutoCodec.Instantiator
4848
public static SingleExtensionEvalValue create(
49-
ImmutableMap<String, Package> generatedRepos,
49+
ImmutableMap<String, RepoSpec> generatedRepoSpecs,
5050
ImmutableBiMap<RepositoryName, String> canonicalRepoNameToInternalNames) {
51-
return new AutoValue_SingleExtensionEvalValue(generatedRepos, canonicalRepoNameToInternalNames);
51+
return new AutoValue_SingleExtensionEvalValue(
52+
generatedRepoSpecs, canonicalRepoNameToInternalNames);
5253
}
5354

5455
public static Key key(ModuleExtensionId id) {

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

+5-4
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());
@@ -238,16 +239,16 @@ private ImmutableMap<String, Module> loadBzlModules(
238239

239240
// Load the .bzl module.
240241
try {
241-
// TODO(b/22193153, wyv): Determine whether .bzl load visibility should apply at all to this
242-
// type of .bzl load. As it stands, this call checks that bzlFile is visible to package @//.
242+
// No need to check visibility for an extension repospec that is always public
243243
return PackageFunction.loadBzlModules(
244244
env,
245245
PackageIdentifier.EMPTY_PACKAGE_ID,
246246
"Bzlmod system",
247247
programLoads,
248248
keys,
249249
starlarkSemantics,
250-
null);
250+
null,
251+
/* checkVisibility= */ false);
251252
} catch (NoSuchPackageException e) {
252253
throw new BzlmodRepoRuleFunctionException(e, Transience.PERSISTENT);
253254
}

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

+16-10
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ private static FileValue getBuildFileValue(Environment env, RootedPath buildFile
636636
* construction to the caller, so that loadPrelude can become just a call to the factored-out
637637
* code.
638638
*/
639+
// TODO(18422): Cleanup/refactor this method's signature.
639640
@Nullable
640641
static ImmutableMap<String, Module> loadBzlModules(
641642
Environment env,
@@ -644,7 +645,8 @@ static ImmutableMap<String, Module> loadBzlModules(
644645
List<Pair<String, Location>> programLoads,
645646
List<BzlLoadValue.Key> keys,
646647
StarlarkSemantics semantics,
647-
@Nullable BzlLoadFunction bzlLoadFunctionForInlining)
648+
@Nullable BzlLoadFunction bzlLoadFunctionForInlining,
649+
boolean checkVisibility)
648650
throws NoSuchPackageException, InterruptedException {
649651
List<BzlLoadValue> bzlLoads;
650652
try {
@@ -657,14 +659,17 @@ static ImmutableMap<String, Module> loadBzlModules(
657659
}
658660
// Validate that the current BUILD/WORKSPACE file satisfies each loaded dependency's
659661
// load visibility.
660-
BzlLoadFunction.checkLoadVisibilities(
661-
packageId,
662-
requestingFileDescription,
663-
bzlLoads,
664-
keys,
665-
programLoads,
666-
/*demoteErrorsToWarnings=*/ !semantics.getBool(BuildLanguageOptions.CHECK_BZL_VISIBILITY),
667-
env.getListener());
662+
if (checkVisibility) {
663+
BzlLoadFunction.checkLoadVisibilities(
664+
packageId,
665+
requestingFileDescription,
666+
bzlLoads,
667+
keys,
668+
programLoads,
669+
/* demoteErrorsToWarnings= */ !semantics.getBool(
670+
BuildLanguageOptions.CHECK_BZL_VISIBILITY),
671+
env.getListener());
672+
}
668673
} catch (BzlLoadFailedException e) {
669674
Throwable rootCause = Throwables.getRootCause(e);
670675
throw PackageFunctionException.builder()
@@ -1325,7 +1330,8 @@ private LoadedPackage loadPackage(
13251330
programLoads,
13261331
keys.build(),
13271332
starlarkBuiltinsValue.starlarkSemantics,
1328-
bzlLoadFunctionForInlining);
1333+
bzlLoadFunctionForInlining,
1334+
/* checkVisibility= */ true);
13291335
} catch (NoSuchPackageException e) {
13301336
throw new PackageFunctionException(e, Transience.PERSISTENT);
13311337
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,8 @@ public SkyValue compute(SkyKey skyKey, Environment env)
333333
programLoads,
334334
keys.build(),
335335
starlarkSemantics,
336-
bzlLoadFunctionForInlining);
336+
bzlLoadFunctionForInlining,
337+
/* checkVisibility= */ true);
337338
} catch (NoSuchPackageException e) {
338339
throw new WorkspaceFileFunctionException(e, Transience.PERSISTENT);
339340
}

0 commit comments

Comments
 (0)