From b0e835e4dac76fe5051b891daf6d6d69cd240037 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Thu, 18 Aug 2022 17:01:25 +0200 Subject: [PATCH] Track runfiles library users in RunfilesProvider RunfilesProvider now tracks the repository names and mappings of runfiles library users among the transitive non-tool dependencies of each rule, as marked by the RunfilesLibraryInfo provider. The list of runfiles library users for an executable target will be used in a follow-up commit to create a repository mapping manifest containing only the mappings of those repositories that contain targets that actually perform runfiles lookups. --- .../lib/analysis/ConfiguredTargetFactory.java | 9 +- .../build/lib/analysis/RunfilesProvider.java | 112 ++++++- .../IncompatibleTargetChecker.java | 2 +- .../StarlarkRuleConfiguredTargetUtil.java | 5 +- .../build/lib/bazel/rules/sh/ShBinary.java | 2 +- .../build/lib/bazel/rules/sh/ShLibrary.java | 2 +- .../build/lib/rules/LateBoundAlias.java | 2 +- .../build/lib/rules/ToolchainType.java | 2 +- .../lib/rules/android/AndroidBinary.java | 6 +- .../lib/rules/android/AndroidCommon.java | 2 +- .../lib/rules/android/AndroidDevice.java | 2 +- .../android/AndroidDeviceScriptFixture.java | 2 +- .../android/AndroidHostServiceFixture.java | 2 +- .../AndroidInstrumentationTestBase.java | 2 +- .../rules/android/AndroidLocalTestBase.java | 2 +- .../build/lib/rules/cpp/CcBinary.java | 3 +- .../rules/cpp/CcHostToolchainAliasRule.java | 2 +- .../build/lib/rules/cpp/CcLibrary.java | 2 +- .../build/lib/rules/cpp/CcToolchain.java | 2 +- .../lib/rules/cpp/CcToolchainAliasRule.java | 2 +- .../build/lib/rules/cpp/CcToolchainSuite.java | 2 +- .../build/lib/rules/cpp/FdoPrefetchHints.java | 2 +- .../build/lib/rules/cpp/FdoProfile.java | 2 +- .../lib/rules/cpp/PropellerOptimize.java | 2 +- .../lib/rules/cpp/proto/CcProtoLibrary.java | 2 +- .../build/lib/rules/extra/ActionListener.java | 2 +- .../lib/rules/extra/ExtraActionFactory.java | 2 +- .../build/lib/rules/filegroup/Filegroup.java | 2 +- .../build/lib/rules/genquery/GenQuery.java | 6 +- .../build/lib/rules/genrule/GenRuleBase.java | 2 +- .../build/lib/rules/java/JavaBinary.java | 2 +- .../build/lib/rules/java/JavaImport.java | 2 +- .../rules/java/JavaPackageConfiguration.java | 2 +- .../build/lib/rules/java/JavaRuntime.java | 2 +- .../build/lib/rules/java/JavaToolchain.java | 3 +- .../build/lib/rules/objc/ObjcRuleClasses.java | 2 +- .../build/lib/rules/python/PyExecutable.java | 2 +- .../build/lib/rules/python/PyLibrary.java | 2 +- .../build/lib/rules/test/TestSuite.java | 2 +- .../google/devtools/build/lib/analysis/BUILD | 24 ++ .../analysis/RunfilesLibraryUsersTest.java | 309 ++++++++++++++++++ .../test/TrimTestConfigurationTest.java | 2 +- .../lib/analysis/util/MockRuleDefaults.java | 2 +- .../build/lib/analysis/util/TestAspects.java | 14 +- .../lib/skyframe/BuiltinsInjectionTest.java | 2 +- .../testutil/UnknownRuleConfiguredTarget.java | 2 +- 46 files changed, 501 insertions(+), 62 deletions(-) create mode 100644 src/test/java/com/google/devtools/build/lib/analysis/RunfilesLibraryUsersTest.java diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTargetFactory.java b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTargetFactory.java index 31cd6108c2f318..af79c600462f8c 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTargetFactory.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredTargetFactory.java @@ -405,7 +405,8 @@ private static ConfiguredTarget erroredConfiguredTargetWithFailures( throws ActionConflictException, InterruptedException, AnalysisFailurePropagationException { RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext); builder.addNativeDeclaredProvider(AnalysisFailureInfo.forAnalysisFailureSets(analysisFailures)); - builder.addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)); + builder.addProvider(RunfilesProvider.class, + RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)); ConfiguredTarget configuredTarget = builder.build(); if (configuredTarget == null) { // A failure here is a failure in analysis failure testing machinery, not a "normal" analysis @@ -437,7 +438,8 @@ private static ConfiguredTarget erroredConfiguredTarget(RuleContext ruleContext) RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext); builder.addNativeDeclaredProvider( AnalysisFailureInfo.forAnalysisFailures(analysisFailures.build())); - builder.addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)); + builder.addProvider(RunfilesProvider.class, + RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)); ConfiguredTarget configuredTarget = builder.build(); if (configuredTarget == null) { // See comment in erroredConfiguredTargetWithFailures. @@ -692,7 +694,8 @@ private static ConfiguredTarget createFailConfiguredTargetForMissingFragmentClas "Missing fragment class: " + missingFragmentClass.getName(), Code.FRAGMENT_CLASS_MISSING)); } - builder.addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)); + builder.addProvider(RunfilesProvider.class, + RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)); try { return builder.build(); } catch (ActionConflictException e) { diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RunfilesProvider.java b/src/main/java/com/google/devtools/build/lib/analysis/RunfilesProvider.java index 57d4b945dedb56..51906fc02ea2d9 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/RunfilesProvider.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/RunfilesProvider.java @@ -14,7 +14,19 @@ package com.google.devtools.build.lib.analysis; import com.google.common.base.Function; +import com.google.common.base.Objects; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; +import com.google.devtools.build.lib.cmdline.RepositoryName; +import com.google.devtools.build.lib.collect.nestedset.NestedSet; +import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; +import com.google.devtools.build.lib.collect.nestedset.Order; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; +import com.google.devtools.build.lib.packages.Attribute; +import com.google.devtools.build.lib.packages.Type.LabelClass; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; /** * Runfiles a target contributes to targets that depend on it. @@ -25,12 +37,60 @@ */ @Immutable public final class RunfilesProvider implements TransitiveInfoProvider { + + /** + * Holds a {@link RepositoryName} together with the corresponding {@link RepositoryMapping}. + * + *

Instances of this class compare equal iff the repositoryName members are + * equal. As a result, a {@link Set} will behave like an + * {@link Map#entrySet} of a {@link Map Map}. + */ + public static final class RepositoryNameAndMapping { + + private final RepositoryName repositoryName; + private final RepositoryMapping repositoryMapping; + + public RepositoryNameAndMapping(RepositoryName repositoryName, + RepositoryMapping repositoryMapping) { + this.repositoryName = repositoryName; + this.repositoryMapping = repositoryMapping; + } + + public RepositoryName getRepositoryName() { + return repositoryName; + } + + public RepositoryMapping getRepositoryMapping() { + return repositoryMapping; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + RepositoryNameAndMapping that = (RepositoryNameAndMapping) o; + return Objects.equal(repositoryName, that.repositoryName); + } + + @Override + public int hashCode() { + return Objects.hashCode(repositoryName); + } + } + private final Runfiles defaultRunfiles; private final Runfiles dataRunfiles; + private final NestedSet runfilesLibraryUsers; - private RunfilesProvider(Runfiles defaultRunfiles, Runfiles dataRunfiles) { + private RunfilesProvider(Runfiles defaultRunfiles, Runfiles dataRunfiles, + NestedSet runfilesLibraryUsers) { this.defaultRunfiles = defaultRunfiles; this.dataRunfiles = dataRunfiles; + this.runfilesLibraryUsers = runfilesLibraryUsers; } public Runfiles getDefaultRunfiles() { @@ -41,6 +101,10 @@ public Runfiles getDataRunfiles() { return dataRunfiles; } + public NestedSet getRunfilesLibraryUsers() { + return runfilesLibraryUsers; + } + /** * Returns a function that gets the default runfiles from a {@link TransitiveInfoCollection} or * the empty runfiles instance if it does not contain that provider. @@ -77,15 +141,51 @@ public Runfiles apply(TransitiveInfoCollection input) { } }; - public static RunfilesProvider simple(Runfiles defaultRunfiles) { - return new RunfilesProvider(defaultRunfiles, defaultRunfiles); + public static RunfilesProvider simple(RuleContext ruleContext, Runfiles defaultRunfiles) { + return new RunfilesProvider(defaultRunfiles, defaultRunfiles, + collectRunfilesLibraryUsers(ruleContext)); } public static RunfilesProvider withData( - Runfiles defaultRunfiles, Runfiles dataRunfiles) { - return new RunfilesProvider(defaultRunfiles, dataRunfiles); + RuleContext ruleContext, Runfiles defaultRunfiles, Runfiles dataRunfiles) { + return new RunfilesProvider(defaultRunfiles, dataRunfiles, + collectRunfilesLibraryUsers(ruleContext)); } public static final RunfilesProvider EMPTY = new RunfilesProvider( - Runfiles.EMPTY, Runfiles.EMPTY); + Runfiles.EMPTY, Runfiles.EMPTY, NestedSetBuilder.emptySet(Order.COMPILE_ORDER)); + + /** + * Collects the runfiles library users of all non-tool dependencies and adds the current + * repository if a runfiles library (marked with {@link RunfilesLibraryInfo}) is among these + * dependencies. + */ + private static NestedSet collectRunfilesLibraryUsers( + RuleContext ruleContext) { + NestedSetBuilder users = NestedSetBuilder.compileOrder(); + for (TransitiveInfoCollection dep : getAllNonToolPrerequisites(ruleContext)) { + RunfilesProvider provider = dep.getProvider(RunfilesProvider.class); + if (provider != null) { + users.addTransitive(provider.getRunfilesLibraryUsers()); + } + if (dep.get(RunfilesLibraryInfo.PROVIDER) != null) { + users.add(new RepositoryNameAndMapping(ruleContext.getRepository(), + ruleContext.getRule().getPackage().getRepositoryMapping())); + } + } + return users.build(); + } + + private static Iterable getAllNonToolPrerequisites( + RuleContext ruleContext) { + List prerequisites = new ArrayList<>(); + for (Attribute attribute : ruleContext.getRule().getAttributes()) { + if (attribute.getType().getLabelClass() != LabelClass.DEPENDENCY + || attribute.isToolDependency()) { + continue; + } + prerequisites.addAll(ruleContext.getPrerequisites(attribute.getName())); + } + return prerequisites; + } } diff --git a/src/main/java/com/google/devtools/build/lib/analysis/constraints/IncompatibleTargetChecker.java b/src/main/java/com/google/devtools/build/lib/analysis/constraints/IncompatibleTargetChecker.java index 5f5a306575b8d3..c67d1ee58a7796 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/constraints/IncompatibleTargetChecker.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/constraints/IncompatibleTargetChecker.java @@ -229,7 +229,7 @@ private static RuleConfiguredTargetValue createIncompatibleRuleConfiguredTarget( TransitiveInfoProviderMapBuilder providerBuilder = new TransitiveInfoProviderMapBuilder() .put(incompatiblePlatformProvider) - .add(RunfilesProvider.simple(Runfiles.EMPTY)) + .add(RunfilesProvider.EMPTY) .add(fileProvider) .add(filesToRunProvider); if (configuration.hasFragment(TestConfiguration.class)) { diff --git a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleConfiguredTargetUtil.java b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleConfiguredTargetUtil.java index 3a18f0afb86a14..d0472947733c34 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleConfiguredTargetUtil.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleConfiguredTargetUtil.java @@ -625,11 +625,12 @@ private static void addSimpleProviders( RunfilesProvider runfilesProvider = statelessRunfiles != null - ? RunfilesProvider.simple(mergeFiles(statelessRunfiles, executable, ruleContext)) + ? RunfilesProvider.simple(ruleContext, + mergeFiles(statelessRunfiles, executable, ruleContext)) : RunfilesProvider.withData( // The executable doesn't get into the default runfiles if we have runfiles states. // This is to keep Starlark genrule consistent with the original genrule. - defaultRunfiles != null ? defaultRunfiles : Runfiles.EMPTY, + ruleContext, defaultRunfiles != null ? defaultRunfiles : Runfiles.EMPTY, dataRunfiles != null ? dataRunfiles : Runfiles.EMPTY); builder.addProvider(RunfilesProvider.class, runfilesProvider); diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShBinary.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShBinary.java index 8a416be55e7f59..5070342f290652 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShBinary.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShBinary.java @@ -90,7 +90,7 @@ public ConfiguredTarget create(RuleContext ruleContext) return new RuleConfiguredTargetBuilder(ruleContext) .setFilesToBuild(filesToBuild) .setRunfilesSupport(runfilesSupport, mainExecutable) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles)) .addNativeDeclaredProvider( InstrumentedFilesCollector.collect(ruleContext, ShCoverage.INSTRUMENTATION_SPEC)) .build(); diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShLibrary.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShLibrary.java index 6d436827b2ff65..e531c799a5cb23 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/sh/ShLibrary.java @@ -50,7 +50,7 @@ public ConfiguredTarget create(RuleContext ruleContext) .build(); return new RuleConfiguredTargetBuilder(ruleContext) .setFilesToBuild(filesToBuild) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles)) .addNativeDeclaredProvider( InstrumentedFilesCollector.collect(ruleContext, ShCoverage.INSTRUMENTATION_SPEC)) .build(); diff --git a/src/main/java/com/google/devtools/build/lib/rules/LateBoundAlias.java b/src/main/java/com/google/devtools/build/lib/rules/LateBoundAlias.java index 65d5c49509b487..495768d01e455a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/LateBoundAlias.java +++ b/src/main/java/com/google/devtools/build/lib/rules/LateBoundAlias.java @@ -74,7 +74,7 @@ public ConfiguredTarget create(RuleContext ruleContext) private static ConfiguredTarget createEmptyConfiguredTarget(RuleContext ruleContext) throws ActionConflictException, InterruptedException { return new RuleConfiguredTargetBuilder(ruleContext) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/ToolchainType.java b/src/main/java/com/google/devtools/build/lib/rules/ToolchainType.java index 2d682eacfd8c59..1babaee089b898 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/ToolchainType.java +++ b/src/main/java/com/google/devtools/build/lib/rules/ToolchainType.java @@ -42,7 +42,7 @@ public ConfiguredTarget create(RuleContext ruleContext) ToolchainTypeInfo toolchainTypeInfo = ToolchainTypeInfo.create(ruleContext.getLabel()); return new RuleConfiguredTargetBuilder(ruleContext) - .addProvider(RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .addNativeDeclaredProvider(toolchainTypeInfo) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java index 5131c581d6ed72..22e227555aff58 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java @@ -854,9 +854,9 @@ public static RuleConfiguredTargetBuilder createAndroidBinary( .addProvider( RunfilesProvider.class, RunfilesProvider.simple( - new Runfiles.Builder( - ruleContext.getWorkspaceName(), - ruleContext.getConfiguration().legacyExternalRunfiles()) + ruleContext, new Runfiles.Builder( + ruleContext.getWorkspaceName(), + ruleContext.getConfiguration().legacyExternalRunfiles()) .addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES) .addTransitiveArtifacts(filesToBuild) .build())) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java index bf15c10d5bd3e2..2b274b0285d33d 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidCommon.java @@ -786,7 +786,7 @@ public RuleConfiguredTargetBuilder addTransitiveInfoProviders( return builder .setFilesToBuild(filesToBuild) .addNativeDeclaredProvider(javaInfo) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(getRunfiles())) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, getRunfiles())) .addNativeDeclaredProvider( createAndroidIdeInfoProvider( ruleContext, diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDevice.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDevice.java index c13802ceb16266..fb55c13537028b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDevice.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDevice.java @@ -141,7 +141,7 @@ public ConfiguredTarget create(RuleContext ruleContext) ruleContext.attributes().get("pregenerate_oat_files_for_tests", Type.BOOLEAN); return new RuleConfiguredTargetBuilder(ruleContext) .setFilesToBuild(filesToBuild) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles)) .setRunfilesSupport(runfilesSupport, executable) .addFilesToRun(extraFilesToRun) .addNativeDeclaredProvider(new ExecutionInfo(executionInfo)) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDeviceScriptFixture.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDeviceScriptFixture.java index f2fbf3ec397cd9..bc34821074ac64 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDeviceScriptFixture.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidDeviceScriptFixture.java @@ -48,7 +48,7 @@ public ConfiguredTarget create(RuleContext ruleContext) .addProvider( RunfilesProvider.class, RunfilesProvider.simple( - new Runfiles.Builder(ruleContext.getWorkspaceName()) + ruleContext, new Runfiles.Builder(ruleContext.getWorkspaceName()) .addArtifact(fixtureScript) .build())) .addNativeDeclaredProvider( diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidHostServiceFixture.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidHostServiceFixture.java index c8b724a1f4f4a8..b57f6a601e2e5e 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidHostServiceFixture.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidHostServiceFixture.java @@ -57,7 +57,7 @@ public ConfiguredTarget create(RuleContext ruleContext) .build(); return ruleBuilder .setFilesToBuild(filesToBuild) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles)) .addNativeDeclaredProvider( new AndroidHostServiceFixtureInfoProvider( executable.getExecutable(), diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestBase.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestBase.java index 45720b13462f39..dd608f1ac7ea8a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestBase.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidInstrumentationTestBase.java @@ -98,7 +98,7 @@ public ConfiguredTarget create(RuleContext ruleContext) return new RuleConfiguredTargetBuilder(ruleContext) .setFilesToBuild(NestedSetBuilder.stableOrder().add(testExecutable).build()) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles)) .setRunfilesSupport( RunfilesSupport.withExecutable(ruleContext, runfiles, testExecutable), testExecutable) .addNativeDeclaredProvider(getExecutionInfoProvider(ruleContext)) diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java index bef2604d50bc91..5ee51f9885d653 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java @@ -396,7 +396,7 @@ public ConfiguredTarget create(RuleContext ruleContext) .addProvider( RunfilesProvider.class, RunfilesProvider.withData( - defaultRunfiles, + ruleContext, defaultRunfiles, new Runfiles.Builder(ruleContext.getWorkspaceName()) .merge(runfilesSupport) .build())) diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java index 83c87ebc2530e7..6321cbb93e7d4c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcBinary.java @@ -749,7 +749,8 @@ public static void init( CcStarlarkApiProvider.maybeAdd(ruleContext, ruleBuilder); ruleBuilder - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(runtimeFiles.runfiles())) + .addProvider(RunfilesProvider.class, + RunfilesProvider.simple(ruleContext, runtimeFiles.runfiles())) .addNativeDeclaredProvider( new DebugPackageProvider(ruleContext.getLabel(), strippedFile, binary, explicitDwpFile)) .setRunfilesSupport(runfilesSupport, binary) diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcHostToolchainAliasRule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcHostToolchainAliasRule.java index 8097512d788975..c862cb4b217137 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcHostToolchainAliasRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcHostToolchainAliasRule.java @@ -74,7 +74,7 @@ public ConfiguredTarget create(RuleContext ruleContext) ToolchainInfo toolchain = ccToolchainAlias.get(ToolchainInfo.PROVIDER); return new RuleConfiguredTargetBuilder(ruleContext) - .addProvider(RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .addNativeDeclaredProvider(ccToolchainProvider) .addNativeDeclaredProvider(toolchain) .addNativeDeclaredProvider(templateVariableInfo) diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java index 2666554512929a..622bf6b3497177 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcLibrary.java @@ -500,7 +500,7 @@ public static void init( CcCommon.mergeOutputGroups( ImmutableList.of(currentOutputGroups, outputGroups.buildOrThrow()))) .addNativeDeclaredProvider(instrumentedFilesProvider) - .addProvider(RunfilesProvider.withData(defaultRunfiles.build(), dataRunfiles.build())); + .addProvider(RunfilesProvider.withData(ruleContext, defaultRunfiles.build(), dataRunfiles.build())); } private static void warnAboutEmptyLibraries(RuleContext ruleContext, diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java index 3b99e5b93d192a..2caa18eaaa9f70 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchain.java @@ -71,7 +71,7 @@ public ConfiguredTarget create(RuleContext ruleContext) RuleConfiguredTargetBuilder ruleConfiguredTargetBuilder = new RuleConfiguredTargetBuilder(ruleContext) .addNativeDeclaredProvider(attributes) - .addProvider(RunfilesProvider.simple(Runfiles.EMPTY)); + .addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)); if (attributes.getLicensesProvider() != null) { ruleConfiguredTargetBuilder.add(LicensesProvider.class, attributes.getLicensesProvider()); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAliasRule.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAliasRule.java index 8ed40b6fb31116..427fd22db190a9 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAliasRule.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainAliasRule.java @@ -88,7 +88,7 @@ public ConfiguredTarget create(RuleContext ruleContext) .build()); return new RuleConfiguredTargetBuilder(ruleContext) - .addProvider(RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .addNativeDeclaredProvider(ccToolchainProvider) .addNativeDeclaredProvider(toolchain) .addNativeDeclaredProvider(templateVariableInfo) diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSuite.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSuite.java index a7468c766dbdf8..43362c50dd976b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSuite.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainSuite.java @@ -95,7 +95,7 @@ public ConfiguredTarget create(RuleContext ruleContext) .addNativeDeclaredProvider(ccToolchainProvider) .addNativeDeclaredProvider(templateVariableInfo) .setFilesToBuild(ccToolchainProvider.getAllFilesIncludingLibc()) - .addProvider(RunfilesProvider.simple(Runfiles.EMPTY)); + .addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)); if (ccToolchainProvider.getLicensesProvider() != null) { builder.add(LicensesProvider.class, ccToolchainProvider.getLicensesProvider()); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoPrefetchHints.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoPrefetchHints.java index 369f19ee2f63f7..632c4ec019723c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoPrefetchHints.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoPrefetchHints.java @@ -37,7 +37,7 @@ public ConfiguredTarget create(RuleContext ruleContext) return new RuleConfiguredTargetBuilder(ruleContext) .addNativeDeclaredProvider(new FdoPrefetchHintsProvider(inputFile)) - .addProvider(RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .build(); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoProfile.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoProfile.java index 212af35789277f..531f8c61f0ce71 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoProfile.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/FdoProfile.java @@ -43,7 +43,7 @@ public ConfiguredTarget create(RuleContext ruleContext) return new RuleConfiguredTargetBuilder(ruleContext) .addNativeDeclaredProvider(new FdoProfileProvider(inputFile, protoProfileArtifact)) - .addProvider(RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .build(); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/PropellerOptimize.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/PropellerOptimize.java index 84ffbafaa90b67..bba9555ced6a70 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/PropellerOptimize.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/PropellerOptimize.java @@ -37,7 +37,7 @@ public ConfiguredTarget create(RuleContext ruleContext) return new RuleConfiguredTargetBuilder(ruleContext) .addNativeDeclaredProvider(new PropellerOptimizeProvider(inputFile)) - .addProvider(RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider(RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .build(); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibrary.java index fe84dd30e92453..fffa95d858521f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/proto/CcProtoLibrary.java @@ -50,7 +50,7 @@ public ConfiguredTarget create(RuleContext ruleContext) ruleContext) .setFilesToBuild(depProviders.filesBuilder) .addProvider( - RunfilesProvider.class, RunfilesProvider.withData(Runfiles.EMPTY, Runfiles.EMPTY)) + RunfilesProvider.class, RunfilesProvider.withData(ruleContext, Runfiles.EMPTY, Runfiles.EMPTY)) .addProviders(depProviders.providerMap); for (String groupName : depProviders.outputGroupInfo) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/extra/ActionListener.java b/src/main/java/com/google/devtools/build/lib/rules/extra/ActionListener.java index f458a30b13325e..cfce9d9279c9ab 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/extra/ActionListener.java +++ b/src/main/java/com/google/devtools/build/lib/rules/extra/ActionListener.java @@ -58,7 +58,7 @@ public ConfiguredTarget create(RuleContext ruleContext) } extraActionMap = extraActionMapBuilder.build(); return new RuleConfiguredTargetBuilder(ruleContext) - .add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) + .add(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .add(ExtraActionMapProvider.class, new ExtraActionMapProvider(extraActionMap)) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/extra/ExtraActionFactory.java b/src/main/java/com/google/devtools/build/lib/rules/extra/ExtraActionFactory.java index 183393a10b5952..ac5024cc8495cc 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/extra/ExtraActionFactory.java +++ b/src/main/java/com/google/devtools/build/lib/rules/extra/ExtraActionFactory.java @@ -92,7 +92,7 @@ public ConfiguredTarget create(RuleContext context) requiresActionOutput); return new RuleConfiguredTargetBuilder(context) .addProvider(ExtraActionSpec.class, spec) - .add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) + .add(RunfilesProvider.class, RunfilesProvider.simple(context, Runfiles.EMPTY)) .build(); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/filegroup/Filegroup.java b/src/main/java/com/google/devtools/build/lib/rules/filegroup/Filegroup.java index db947c56949a44..c3796a1cab5a86 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/filegroup/Filegroup.java +++ b/src/main/java/com/google/devtools/build/lib/rules/filegroup/Filegroup.java @@ -87,7 +87,7 @@ public ConfiguredTarget create(RuleContext ruleContext) RunfilesProvider runfilesProvider = RunfilesProvider.withData( - new Runfiles.Builder( + ruleContext, new Runfiles.Builder( ruleContext.getWorkspaceName(), configuration.legacyExternalRunfiles()) .addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES) .build(), diff --git a/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java b/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java index 02fb57b9096300..ade1039c48158d 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java +++ b/src/main/java/com/google/devtools/build/lib/rules/genquery/GenQuery.java @@ -200,9 +200,9 @@ public ConfiguredTarget create(RuleContext ruleContext) .addProvider( RunfilesProvider.class, RunfilesProvider.simple( - new Runfiles.Builder( - ruleContext.getWorkspaceName(), - ruleContext.getConfiguration().legacyExternalRunfiles()) + ruleContext, new Runfiles.Builder( + ruleContext.getWorkspaceName(), + ruleContext.getConfiguration().legacyExternalRunfiles()) .addTransitiveArtifacts(filesToBuild) .build())) .addOutputGroup( diff --git a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java index 3deb2258699f10..72d524435c8f2f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java +++ b/src/main/java/com/google/devtools/build/lib/rules/genrule/GenRuleBase.java @@ -257,7 +257,7 @@ public String toString() { RunfilesProvider runfilesProvider = RunfilesProvider.withData( // No runfiles provided if not a data dependency. - Runfiles.EMPTY, + ruleContext, Runfiles.EMPTY, // We only need to consider the outputs of a genrule // No need to visit the dependencies of a genrule. They cross from the target into the host // configuration, because the dependencies of a genrule are always built for the host diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java index a03d43fa55e2f4..2a94a76e52e52b 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaBinary.java @@ -405,7 +405,7 @@ public ConfiguredTarget create(RuleContext ruleContext) RunfilesProvider runfilesProvider = RunfilesProvider.withData( - defaultRunfiles, + ruleContext, defaultRunfiles, new Runfiles.Builder( ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()) diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java index 47d6e0ae1f7b94..81ce90772ac186 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaImport.java @@ -182,7 +182,7 @@ public ConfiguredTarget create(RuleContext ruleContext) return ruleBuilder .setFilesToBuild(filesToBuild) .addNativeDeclaredProvider(javaInfo) - .add(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) + .add(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles)) .addNativeDeclaredProvider(new ProguardSpecProvider(proguardSpecs)) .addOutputGroup(JavaSemantics.SOURCE_JARS_OUTPUT_GROUP, transitiveJavaSourceJars) .addOutputGroup( diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaPackageConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaPackageConfiguration.java index 2ab7c02b81daa6..d30c69917ca0ce 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaPackageConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaPackageConfiguration.java @@ -49,7 +49,7 @@ public ConfiguredTarget create(RuleContext ruleContext) ImmutableList javacopts = ImmutableList.copyOf(ruleContext.getExpander().withDataLocations().tokenized("javacopts")); return new RuleConfiguredTargetBuilder(ruleContext) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .setFilesToBuild(NestedSetBuilder.emptySet(Order.STABLE_ORDER)) .addProvider(JavaPackageConfigurationProvider.create(packages, javacopts, data)) .build(); diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java index b77cd763b771a0..3ce9d90087f9a3 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntime.java @@ -129,7 +129,7 @@ public ConfiguredTarget create(RuleContext ruleContext) new ToolchainInfo( ImmutableMap.builder().put("java_runtime", javaRuntime).buildOrThrow()); return new RuleConfiguredTargetBuilder(ruleContext) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfiles)) .setFilesToBuild(filesToBuild) .addNativeDeclaredProvider(javaRuntime) .addNativeDeclaredProvider(templateVariableInfo) diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchain.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchain.java index 782cc1abb5acd4..614a625928dada 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchain.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchain.java @@ -203,7 +203,8 @@ public ConfiguredTarget create(RuleContext ruleContext) .addStarlarkTransitiveInfo(JavaToolchainProvider.LEGACY_NAME, provider) .addNativeDeclaredProvider(provider) .addNativeDeclaredProvider(toolchainInfo) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider(RunfilesProvider.class, + RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .setFilesToBuild(new NestedSetBuilder(Order.STABLE_ORDER).build()); return builder.build(); diff --git a/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcRuleClasses.java b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcRuleClasses.java index f6ed945625d85d..f6499498601d28 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcRuleClasses.java +++ b/src/main/java/com/google/devtools/build/lib/rules/objc/ObjcRuleClasses.java @@ -153,7 +153,7 @@ private static SpawnAction.Builder spawnOnDarwinActionBuilder(XcodeConfigInfo xc static RuleConfiguredTargetBuilder ruleConfiguredTarget(RuleContext ruleContext, NestedSet filesToBuild) { RunfilesProvider runfilesProvider = RunfilesProvider.withData( - new Runfiles.Builder( + ruleContext, new Runfiles.Builder( ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()) .addRunfiles(ruleContext, RunfilesProvider.DEFAULT_RUNFILES).build(), diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java index 289e5bdc704b97..0063534762086c 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyExecutable.java @@ -109,7 +109,7 @@ public ConfiguredTarget create(RuleContext ruleContext) dataRunfiles = commonRunfiles; } - RunfilesProvider runfilesProvider = RunfilesProvider.withData(defaultRunfiles, dataRunfiles); + RunfilesProvider runfilesProvider = RunfilesProvider.withData(ruleContext, defaultRunfiles, dataRunfiles); RuleConfiguredTargetBuilder builder = new RuleConfiguredTargetBuilder(ruleContext); common.addCommonTransitiveInfoProviders(builder, common.getFilesToBuild()); diff --git a/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java index 091b5efd585891..479f3b0749dbb1 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/python/PyLibrary.java @@ -71,7 +71,7 @@ public ConfiguredTarget create(RuleContext ruleContext) .addNativeDeclaredProvider( new PyCcLinkParamsProvider( semantics.buildCcInfoProvider(ruleContext, ruleContext.getPrerequisites("deps")))) - .add(RunfilesProvider.class, RunfilesProvider.simple(runfilesBuilder.build())) + .add(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, runfilesBuilder.build())) .build(); } } diff --git a/src/main/java/com/google/devtools/build/lib/rules/test/TestSuite.java b/src/main/java/com/google/devtools/build/lib/rules/test/TestSuite.java index 40eeb359852495..cc949b02823b45 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/test/TestSuite.java +++ b/src/main/java/com/google/devtools/build/lib/rules/test/TestSuite.java @@ -86,7 +86,7 @@ public ConfiguredTarget create(RuleContext ruleContext) return new RuleConfiguredTargetBuilder(ruleContext) .add(RunfilesProvider.class, - RunfilesProvider.withData(Runfiles.EMPTY, runfiles)) + RunfilesProvider.withData(ruleContext, Runfiles.EMPTY, runfiles)) .add(TransitiveTestsProvider.class, new TransitiveTestsProvider()) .build(); } diff --git a/src/test/java/com/google/devtools/build/lib/analysis/BUILD b/src/test/java/com/google/devtools/build/lib/analysis/BUILD index ce4ce95fd37fff..84b189e3148953 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/BUILD +++ b/src/test/java/com/google/devtools/build/lib/analysis/BUILD @@ -405,6 +405,30 @@ java_test( ], ) +java_test( + name = "RunfilesLibraryUsersTest", + srcs = ["RunfilesLibraryUsersTest.java"], + deps = [ + "//src/main/java/com/google/devtools/build/lib/actions", + "//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster", + "//src/main/java/com/google/devtools/build/lib/analysis:configured_target", + "//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution_impl", + "//src/main/java/com/google/devtools/build/lib/bazel/repository:repository_options", + "//src/main/java/com/google/devtools/build/lib/cmdline", + "//src/main/java/com/google/devtools/build/lib/events", + "//src/main/java/com/google/devtools/build/lib/skyframe:precomputed_value", + "//src/main/java/com/google/devtools/build/lib/skyframe:repository_mapping_value", + "//src/main/java/com/google/devtools/build/lib/vfs", + "//src/main/java/com/google/devtools/build/skyframe", + "//src/main/java/com/google/devtools/build/skyframe:skyframe-objects", + "//src/test/java/com/google/devtools/build/lib/analysis/util", + "//src/test/java/com/google/devtools/build/lib/bazel/bzlmod:util", + "//src/test/java/com/google/devtools/build/lib/skyframe:testutil", + "//third_party:junit4", + "//third_party:truth", + ], +) + test_suite( name = "AllAnalysisTests", tests = [ diff --git a/src/test/java/com/google/devtools/build/lib/analysis/RunfilesLibraryUsersTest.java b/src/test/java/com/google/devtools/build/lib/analysis/RunfilesLibraryUsersTest.java new file mode 100644 index 00000000000000..2090f442235d3c --- /dev/null +++ b/src/test/java/com/google/devtools/build/lib/analysis/RunfilesLibraryUsersTest.java @@ -0,0 +1,309 @@ +// Copyright 2020 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.analysis; + +import static com.google.common.truth.Truth.assertThat; +import static com.google.devtools.build.lib.bazel.bzlmod.BzlmodTestUtil.createModuleKey; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.devtools.build.lib.analysis.RunfilesProvider.RepositoryNameAndMapping; +import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; +import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleResolutionFunction; +import com.google.devtools.build.lib.bazel.bzlmod.FakeRegistry; +import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileFunction; +import com.google.devtools.build.lib.bazel.repository.RepositoryOptions.CheckDirectDepsMode; +import com.google.devtools.build.lib.cmdline.Label; +import com.google.devtools.build.lib.cmdline.RepositoryMapping; +import com.google.devtools.build.lib.cmdline.RepositoryName; +import com.google.devtools.build.lib.skyframe.PrecomputedValue; +import com.google.devtools.build.lib.skyframe.PrecomputedValue.Injected; +import com.google.devtools.build.lib.skyframe.RepositoryMappingValue; +import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils; +import com.google.devtools.build.lib.vfs.Path; +import com.google.devtools.build.skyframe.EvaluationResult; +import com.google.devtools.build.skyframe.SkyKey; +import java.io.IOException; +import java.util.Map; +import java.util.stream.Collectors; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class RunfilesLibraryUsersTest extends BuildViewTestCase { + + private Path moduleRoot; + private FakeRegistry registry; + + @Override + protected ImmutableList extraPrecomputedValues() { + try { + moduleRoot = scratch.dir("modules"); + } catch (IOException e) { + throw new IllegalStateException(e); + } + registry = FakeRegistry.DEFAULT_FACTORY.newFakeRegistry(moduleRoot.getPathString()); + return ImmutableList.of( + PrecomputedValue.injected( + ModuleFileFunction.REGISTRIES, ImmutableList.of(registry.getUrl())), + PrecomputedValue.injected(ModuleFileFunction.IGNORE_DEV_DEPS, false), + PrecomputedValue.injected(ModuleFileFunction.MODULE_OVERRIDES, ImmutableMap.of()), + PrecomputedValue.injected( + BazelModuleResolutionFunction.CHECK_DIRECT_DEPENDENCIES, CheckDirectDepsMode.WARNING)); + } + + @Test + public void testRunfilesLibraryUsersCollectedInRunfilesProvider() throws Exception { + setBuildLanguageOptions("--enable_bzlmod"); + scratch.file("MODULE.bazel", + "module()", + "bazel_dep(name='starlark_non_user',version='1.0')"); + registry + .addModule( + createModuleKey("utils", "1.0"), + "module(name='utils',version='1.0')", + "bazel_dep(name='bar_user',version='1.0')") + .addModule( + createModuleKey("cc_user", "1.0"), + "module(name='cc_user',version='1.0')", + "bazel_dep(name='utils',version='1.0')") + .addModule( + createModuleKey("cc_non_user", "1.0"), + "module(name='cc_non_user',version='1.0')", + "bazel_dep(name='cc_user',version='1.0')") + .addModule( + createModuleKey("java_user", "1.0"), + "module(name='java_user',version='1.0')", + "bazel_dep(name='utils',version='1.0')") + .addModule( + createModuleKey("java_non_user", "1.0"), + "module(name='java_non_user',version='1.0')", + "bazel_dep(name='java_user',version='1.0')") + .addModule( + createModuleKey("bar_user", "1.0"), + "module(name='bar_user',version='1.0')", + "bazel_dep(name='utils',version='1.0')", + "register_toolchains('//:bar_toolchain')") + .addModule( + createModuleKey("starlark_user", "1.0"), + "module(name='starlark_user',version='1.0')", + "bazel_dep(name='utils',version='1.0')") + .addModule( + createModuleKey("starlark_tool_user", "1.0"), + "module(name='starlark_tool_user',version='1.0')", + "bazel_dep(name='utils',version='1.0')") + .addModule( + createModuleKey("starlark_non_user", "1.0"), + "module(name='starlark_non_user',version='1.0')", + "bazel_dep(name='utils',version='1.0')", + "bazel_dep(name='starlark_user',version='1.0')", + "bazel_dep(name='starlark_tool_user',version='1.0')", + "bazel_dep(name='java_non_user',version='1.0')", + "bazel_dep(name='cc_non_user',version='1.0')", + "bazel_dep(name='bar_user',version='1.0')"); + + scratch.file(moduleRoot.getRelative("utils~1.0").getRelative("WORKSPACE").getPathString()); + scratch.file(moduleRoot.getRelative("utils~1.0").getRelative("BUILD").getPathString(), + "load(':utils.bzl', 'runfiles_lib')", + "runfiles_lib(name='runfiles_lib',visibility=['//visibility:public'])"); + scratch.file( + moduleRoot.getRelative("utils~1.0").getRelative("utils.bzl").getPathString(), + "def _runfiles_lib_impl(ctx):", + " return [", + " CcInfo(),", + " RunfilesLibraryInfo(),", + " ]", + "runfiles_lib = rule(_runfiles_lib_impl)", + "", + "def _starlark_rule_impl(ctx):", + // Explicitly do not merge in runfiles, just create a new runfiles object. + " return [DefaultInfo(runfiles = ctx.runfiles())]", + "starlark_rule = rule(", + " implementation = _starlark_rule_impl,", + " attrs = {", + " 'deps': attr.label_list(),", + " 'tool_deps': attr.label_list(cfg = 'exec'),", + " },", + " toolchains = ['@bar_user//:toolchain_type'],", + ")"); + + scratch.file(moduleRoot.getRelative("cc_user~1.0").getRelative("WORKSPACE").getPathString()); + scratch.file( + moduleRoot.getRelative("cc_user~1.0").getRelative("BUILD").getPathString(), + "cc_library(", + " name = 'cc_user',", + " deps = ['@utils//:runfiles_lib'],", + " visibility = ['//visibility:public'],", + ")"); + + scratch.file( + moduleRoot.getRelative("cc_non_user~1.0").getRelative("WORKSPACE").getPathString()); + scratch.file( + moduleRoot.getRelative("cc_non_user~1.0").getRelative("BUILD").getPathString(), + "cc_library(", + " name = 'cc_non_user',", + " deps = ['@cc_user//:cc_user'],", + ")"); + + scratch.file(moduleRoot.getRelative("java_user~1.0").getRelative("WORKSPACE").getPathString()); + scratch.file( + moduleRoot.getRelative("java_user~1.0").getRelative("BUILD").getPathString(), + "java_library(", + " name = 'java_user',", + " runtime_deps = ['@utils//:runfiles_lib'],", + " visibility = ['//visibility:public'],", + ")"); + + scratch.file( + moduleRoot.getRelative("java_non_user~1.0").getRelative("WORKSPACE").getPathString()); + scratch.file( + moduleRoot.getRelative("java_non_user~1.0").getRelative("BUILD").getPathString(), + "java_library(", + " name = 'java_non_user',", + " srcs = ['Lib.java'],", + " deps = ['@java_user//:java_user'],", + ")"); + + scratch.file( + moduleRoot.getRelative("bar_user~1.0").getRelative("WORKSPACE").getPathString()); + scratch.file( + moduleRoot.getRelative("bar_user~1.0").getRelative("BUILD").getPathString(), + "load(':toolchain.bzl', 'bar_toolchain')", + "java_library(", + " name = 'bar_user',", + " srcs = ['Runtime.java'],", + " deps = ['@utils//:runfiles_lib'],", + ")", + "bar_toolchain(", + " name = 'bar',", + " runtime = ':bar_user',", + ")", + "toolchain_type(name = 'toolchain_type')", + "toolchain(", + " name = 'bar_toolchain',", + " toolchain = ':bar',", + " toolchain_type = ':toolchain_type',", + ")"); + scratch.file( + moduleRoot.getRelative("bar_user~1.0").getRelative("toolchain.bzl").getPathString(), + "def _bar_toolchain_impl(ctx):", + " return [platform_common.ToolchainInfo(type = 'bar')]", + "bar_toolchain = rule(", + " implementation = _bar_toolchain_impl,", + " attrs = {'runtime': attr.label()},", + ")"); + + scratch.file( + moduleRoot.getRelative("starlark_user~1.0").getRelative("WORKSPACE").getPathString()); + scratch.file( + moduleRoot.getRelative("starlark_user~1.0").getRelative("BUILD").getPathString(), + "load('@utils//:utils.bzl', 'starlark_rule')", + "starlark_rule(", + " name = 'starlark_user',", + " deps = ['@utils//:runfiles_lib'],", + " visibility = ['//visibility:public'],", + ")"); + + scratch.file( + moduleRoot.getRelative("starlark_tool_user~1.0").getRelative("WORKSPACE").getPathString()); + scratch.file( + moduleRoot.getRelative("starlark_tool_user~1.0").getRelative("BUILD").getPathString(), + "load('@utils//:utils.bzl', 'starlark_rule')", + "starlark_rule(", + " name = 'starlark_tool_user',", + " deps = ['@utils//:runfiles_lib'],", + " visibility = ['//visibility:public'],", + ")"); + + scratch.file( + moduleRoot.getRelative("starlark_non_user~1.0").getRelative("WORKSPACE").getPathString()); + scratch.file( + moduleRoot.getRelative("starlark_non_user~1.0").getRelative("BUILD").getPathString(), + "load('@utils//:utils.bzl', 'starlark_rule')", + "starlark_rule(", + " name = 'starlark_non_user',", + " deps = [", + " '@starlark_user//:starlark_user',", + " '@java_non_user//:java_non_user',", + " '@cc_non_user//:cc_non_user',", + " ],", + " tool_deps = [", + " '@starlark_tool_user//:starlark_tool_user',", + " '@utils//:runfiles_lib',", + " ],", + " visibility = ['//visibility:public'],", + ")"); + + assertThat(getRunfilesLibraryUsers("@@utils~1.0//:runfiles_lib")).isEmpty(); + + RepositoryName ccUser = RepositoryName.createUnvalidated("cc_user~1.0"); + Map ccUserRunfilesLibraryUsers = getRunfilesLibraryUsers( + "@@cc_user~1.0//:cc_user"); + RepositoryMapping ccUserMapping = getRepositoryMapping(ccUser); + assertThat(ccUserRunfilesLibraryUsers).containsExactly(ccUser, ccUserMapping); + + Map ccNonUserRunfilesLibraryUsers = getRunfilesLibraryUsers( + "@@cc_non_user~1.0//:cc_non_user"); + assertThat(ccNonUserRunfilesLibraryUsers).containsExactly(ccUser, ccUserMapping); + + RepositoryName javaUser = RepositoryName.createUnvalidated("java_user~1.0"); + Map javaUserRunfilesLibraryUsers = getRunfilesLibraryUsers( + "@@java_user~1.0//:java_user"); + RepositoryMapping javaUserMapping = getRepositoryMapping(javaUser); + assertThat(javaUserRunfilesLibraryUsers).containsExactly(javaUser, javaUserMapping); + + Map javaNonUserRunfilesLibraryUsers = getRunfilesLibraryUsers( + "@@java_non_user~1.0//:java_non_user"); + assertThat(javaNonUserRunfilesLibraryUsers).containsExactly(javaUser, javaUserMapping); + + RepositoryName barUser = RepositoryName.createUnvalidated("bar_user~1.0"); + Map barUserRunfilesLibraryUsers = getRunfilesLibraryUsers( + "@@bar_user~1.0//:bar_user"); + RepositoryMapping barUserMapping = getRepositoryMapping(barUser); + assertThat(barUserRunfilesLibraryUsers).containsExactly(barUser, barUserMapping); + + RepositoryName starlarkUser = RepositoryName.createUnvalidated("starlark_user~1.0"); + Map starlarkUserRunfilesLibraryUsers = getRunfilesLibraryUsers( + "@@starlark_user~1.0//:starlark_user"); + RepositoryMapping starlarkUserMapping = getRepositoryMapping(starlarkUser); + assertThat(starlarkUserRunfilesLibraryUsers).containsExactly(starlarkUser, starlarkUserMapping); + + Map starlarkNonUserRunfilesLibraryUsers = getRunfilesLibraryUsers( + "@@starlark_non_user~1.0//:starlark_non_user"); + assertThat(starlarkNonUserRunfilesLibraryUsers).containsExactly( + javaUser, javaUserMapping, ccUser, ccUserMapping, starlarkUser, starlarkUserMapping, + barUser, barUserMapping); + } + + private RepositoryMapping getRepositoryMapping(RepositoryName ccUser) + throws InterruptedException { + SkyKey key = RepositoryMappingValue.key(ccUser); + EvaluationResult result = SkyframeExecutorTestUtils.evaluate( + getSkyframeExecutor(), key, /*keepGoing=*/ false, reporter); + return result.get(key).getRepositoryMapping(); + } + + private Map getRunfilesLibraryUsers(String label) + throws Exception { + ConfiguredTarget target = getConfiguredTarget(Label.parseCanonical(label), targetConfig); + RunfilesProvider runfilesProvider = target.getProvider(RunfilesProvider.class); + + assertThat(runfilesProvider).isNotNull(); + assertThat(runfilesProvider.getRunfilesLibraryUsers()).isNotNull(); + return runfilesProvider.getRunfilesLibraryUsers().toList().stream().collect( + Collectors.toMap(RepositoryNameAndMapping::getRepositoryName, + RepositoryNameAndMapping::getRepositoryMapping)); + } +} diff --git a/src/test/java/com/google/devtools/build/lib/analysis/test/TrimTestConfigurationTest.java b/src/test/java/com/google/devtools/build/lib/analysis/test/TrimTestConfigurationTest.java index 82a42ae373c00d..835fe30d827138 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/test/TrimTestConfigurationTest.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/test/TrimTestConfigurationTest.java @@ -71,7 +71,7 @@ public ConfiguredTarget create(RuleContext context) new Runfiles.Builder(context.getWorkspaceName()).addArtifact(executable).build(); return new RuleConfiguredTargetBuilder(context) .setFilesToBuild(NestedSetBuilder.create(Order.STABLE_ORDER, executable)) - .addProvider(RunfilesProvider.class, RunfilesProvider.simple(runfiles)) + .addProvider(RunfilesProvider.class, RunfilesProvider.simple(context, runfiles)) .setRunfilesSupport( RunfilesSupport.withExecutable(context, runfiles, executable), executable) .build(); diff --git a/src/test/java/com/google/devtools/build/lib/analysis/util/MockRuleDefaults.java b/src/test/java/com/google/devtools/build/lib/analysis/util/MockRuleDefaults.java index 65e48274120298..a7ec97e63978c9 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/util/MockRuleDefaults.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/util/MockRuleDefaults.java @@ -97,7 +97,7 @@ public ConfiguredTarget create(RuleContext ruleContext) return new RuleConfiguredTargetBuilder(ruleContext) .setFilesToBuild(filesToBuild) .setRunfilesSupport(null, null) - .add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) + .add(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .build(); } } diff --git a/src/test/java/com/google/devtools/build/lib/analysis/util/TestAspects.java b/src/test/java/com/google/devtools/build/lib/analysis/util/TestAspects.java index f31f641ac3fb9e..af991681623264 100644 --- a/src/test/java/com/google/devtools/build/lib/analysis/util/TestAspects.java +++ b/src/test/java/com/google/devtools/build/lib/analysis/util/TestAspects.java @@ -171,7 +171,7 @@ public ConfiguredTarget create(RuleContext ruleContext) new RuleInfo(collectAspectData("rule " + ruleContext.getLabel(), ruleContext))) .setFilesToBuild(NestedSetBuilder.create(Order.STABLE_ORDER)) .setRunfilesSupport(null, null) - .add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)); + .add(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)); if (ruleContext.getRule().getRuleClassObject().getName().equals("honest")) { builder.addProvider(new RequiredProvider()); @@ -189,11 +189,11 @@ public static class DummyRuleFactory2 implements RuleConfiguredTargetFactory { public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException, ActionConflictException { return new RuleConfiguredTargetBuilder(ruleContext) - .addProvider( - new RuleInfo(collectAspectData("rule " + ruleContext.getLabel(), ruleContext))) - .setFilesToBuild(NestedSetBuilder.create(Order.STABLE_ORDER)) - .setRunfilesSupport(null, null) - .add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) + .addProvider( + new RuleInfo(collectAspectData("rule " + ruleContext.getLabel(), ruleContext))) + .setFilesToBuild(NestedSetBuilder.create(Order.STABLE_ORDER)) + .setRunfilesSupport(null, null) + .add(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .addProvider(new RequiredProvider()) .addProvider(new RequiredProvider2()) .build(); @@ -226,7 +226,7 @@ public ConfiguredTarget create(RuleContext ruleContext) new RuleInfo(infoBuilder.build())) .setFilesToBuild(NestedSetBuilder.create(Order.STABLE_ORDER)) .setRunfilesSupport(null, null) - .add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)); + .add(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)); return builder.build(); } diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/BuiltinsInjectionTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/BuiltinsInjectionTest.java index 4fe249221e3ca0..930d2848108674 100644 --- a/src/test/java/com/google/devtools/build/lib/skyframe/BuiltinsInjectionTest.java +++ b/src/test/java/com/google/devtools/build/lib/skyframe/BuiltinsInjectionTest.java @@ -155,7 +155,7 @@ public ConfiguredTarget create(RuleContext ruleContext) .setFilesToBuild( NestedSetBuilder.wrap(Order.STABLE_ORDER, ruleContext.getOutputArtifacts())) .setRunfilesSupport(null, null) - .add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) + .add(RunfilesProvider.class, RunfilesProvider.simple(ruleContext, Runfiles.EMPTY)) .build(); } } diff --git a/src/test/java/com/google/devtools/build/lib/testutil/UnknownRuleConfiguredTarget.java b/src/test/java/com/google/devtools/build/lib/testutil/UnknownRuleConfiguredTarget.java index 2bd4116c81c3ed..e3120f25ae4a03 100644 --- a/src/test/java/com/google/devtools/build/lib/testutil/UnknownRuleConfiguredTarget.java +++ b/src/test/java/com/google/devtools/build/lib/testutil/UnknownRuleConfiguredTarget.java @@ -62,7 +62,7 @@ public ConfiguredTarget create(RuleContext context) Code.FAIL_ACTION_UNKNOWN)); return new RuleConfiguredTargetBuilder(context) .setFilesToBuild(filesToBuild) - .add(RunfilesProvider.class, RunfilesProvider.simple(Runfiles.EMPTY)) + .add(RunfilesProvider.class, RunfilesProvider.simple(context, Runfiles.EMPTY)) .build(); } }