Skip to content

Commit

Permalink
[7.4.0] Automatically ignore vendor dir for finding packages (#23919)
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

Backporting
d42301b
  • Loading branch information
meteorcloudy authored Oct 9, 2024
1 parent b090c83 commit cb5bc05
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 3 deletions.
2 changes: 2 additions & 0 deletions bazel_downloader.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ rewrite (maven.google.com)/(.*) https://mirror.bazel.build/$1/$2
rewrite (maven.google.com)/(.*) https://$1/$2
rewrite (cdn.azul.com)/(.*) https://mirror.bazel.build/$1/$2
rewrite (cdn.azul.com)/(.*) https://$1/$2
rewrite (aka.ms)/(.*) https://mirror.bazel.build/$1/$2
rewrite (aka.ms)/(.*) https://$1/$2
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 @@ -814,6 +814,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",
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 @@ -526,7 +526,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 @@ -149,6 +149,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 @@ -474,8 +474,9 @@ public DiffAwareness maybeCreate(Root pathEntry, ImmutableSet<Path> ignoredPaths
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())));
skyframeExecutor.preparePackageLoading(
new PathPackageLocator(
outputBase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ 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 cb5bc05

Please sign in to comment.