Skip to content

Commit

Permalink
Automatically ignore vendor dir for finding packages
Browse files Browse the repository at this point in the history
- Adding `--vendor_dir` to ignored package prefixes as if it's added in the `.bazelignore` file.
- This allows users to do `bazel build //...` after vendoring external deps in the source root without manually adding the vendor dir in `.bazelignore`.

Fixes #23521

Closes #23771.

PiperOrigin-RevId: 680994986
Change-Id: I9c8b76ca20b9060232dfe05460a8fc857c92e93f
  • Loading branch information
meteorcloudy authored and copybara-github committed Oct 1, 2024
1 parent caa2c41 commit d42301b
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/skyframe/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/io:inconsistent_filesystem_exception",
"//src/main/java/com/google/devtools/build/lib/pkgcache",
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_directory_value",
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_function",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.
package com.google.devtools.build.lib.skyframe;

import static com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction.VENDOR_DIRECTORY;

import com.google.common.collect.ImmutableSet;
import com.google.common.io.CharStreams;
import com.google.common.io.LineProcessor;
Expand Down Expand Up @@ -82,7 +84,17 @@ public SkyValue compute(SkyKey key, Environment env)
}

if (repositoryName.isMain()) {
PathFragment vendorDir = null;
if (VENDOR_DIRECTORY.get(env).isPresent()) {
vendorDir = VENDOR_DIRECTORY.get(env).get().asFragment();
}

for (Root packagePathEntry : pkgLocator.getPathEntries()) {
PathFragment workspaceRoot = packagePathEntry.asPath().asFragment();
if (vendorDir != null && vendorDir.startsWith(workspaceRoot)) {
ignoredPackagePrefixesBuilder.add(vendorDir.relativeTo(workspaceRoot));
}

RootedPath rootedPatternFile =
RootedPath.toRootedPath(packagePathEntry, ignoredPackagePrefixesFile);
FileValue patternFileValue = (FileValue) env.getValue(FileValue.key(rootedPatternFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,9 @@ private void setUpSkyframe() {
skyframeExecutor.injectExtraPrecomputedValues(
ImmutableList.of(
PrecomputedValue.injected(
RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty())));
RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()),
PrecomputedValue.injected(
RepositoryDelegatorFunction.VENDOR_DIRECTORY, Optional.empty())));
}

protected void setPackageOptions(String... options) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/util/io",
"//src/main/java/com/google/devtools/build/lib/vfs",
"//src/main/java/com/google/devtools/common/options",
"//src/test/java/com/google/devtools/build/lib/analysis/util",
"//src/test/java/com/google/devtools/build/lib/buildtool/util",
"//third_party:guava",
"//third_party:junit4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.analysis.Project;
import com.google.devtools.build.lib.analysis.util.AnalysisMock;
import com.google.devtools.build.lib.buildtool.util.BuildIntegrationTestCase;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
Expand Down Expand Up @@ -60,6 +61,7 @@ public void setupSkyframePackageSemantics() {
ImmutableMap.of(),
QuiescingExecutorsImpl.forTesting(),
new TimestampGranularityMonitor(null));
getSkyframeExecutor().injectExtraPrecomputedValues(AnalysisMock.get().getPrecomputedValues());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ private SkyframeExecutor createSkyframeExecutor() {
ImmutableList.of(
PrecomputedValue.injected(
RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty())));
skyframeExecutor.injectExtraPrecomputedValues(
ImmutableList.of(
PrecomputedValue.injected(
RepositoryDelegatorFunction.VENDOR_DIRECTORY, Optional.empty())));
SkyframeExecutorTestHelper.process(skyframeExecutor);
return skyframeExecutor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,9 @@ public DiffAwareness maybeCreate(
skyframeExecutor.injectExtraPrecomputedValues(
ImmutableList.of(
PrecomputedValue.injected(
RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE,
Optional.empty())));
RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()),
PrecomputedValue.injected(
RepositoryDelegatorFunction.VENDOR_DIRECTORY, Optional.empty())));
BuildLanguageOptions buildLanguageOptions = Options.getDefaults(BuildLanguageOptions.class);
buildLanguageOptions.incompatibleAutoloadExternally = ImmutableList.of();
skyframeExecutor.preparePackageLoading(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public final void setUp() throws Exception {
PrecomputedValue.STARLARK_SEMANTICS.set(differencer, StarlarkSemantics.DEFAULT);
RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE.set(
differencer, Optional.empty());
RepositoryDelegatorFunction.VENDOR_DIRECTORY.set(differencer, Optional.empty());

createTestFiles();
}
Expand Down
43 changes: 43 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_vendor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,49 @@ def testVendorWithTargetPatternFile(self):
self.assertIn('bbb+', os.listdir(self._test_cwd + '/vendor'))
self.assertNotIn('ccc+', os.listdir(self._test_cwd + '/vendor'))

def testVendorDirIsIgnored(self):
self.main_registry.createCcModule('aaa', '1.0')
self.ScratchFile(
'MODULE.bazel',
['bazel_dep(name = "aaa", version = "1.0")'],
)
self.ScratchFile(
'BUILD',
[
'cc_binary(',
' name = "main",',
' srcs = ["main.cc"],',
' deps = [',
' "@aaa//:lib_aaa",',
' ],',
')',
],
)
self.ScratchFile(
'main.cc',
[
'#include "aaa.h"',
'int main() {',
' hello_aaa("Hello there!");',
'}',
],
)

self.RunBazel([
'vendor',
'//...',
'--vendor_dir=vendor',
])
# Assert aaa is vendored
self.assertIn('aaa+', os.listdir(self._test_cwd + '/vendor'))

# bazel build //... should succeed because vendor dir is ignored.
self.RunBazel([
'build',
'//...',
'--vendor_dir=vendor',
])

def testBuildVendoredTargetOffline(self):
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
'bbb', '1.0', {'aaa': '1.0'}
Expand Down

0 comments on commit d42301b

Please sign in to comment.