Skip to content

Commit

Permalink
[7.4.0] Support --target_pattern_file for fetch and vendor command (#…
Browse files Browse the repository at this point in the history
…23663)

Fixes: #23628

RELNOTES: Bazel fetch and vendor command now supports
--target_pattern_file for specifying target patterns.

Closes #23640.

PiperOrigin-RevId: 676063442
Change-Id: Ibbbf7879dfec4ec10093631fb002f87c9dddc8ef

Commit
3120d35

---------

Co-authored-by: Yun Peng <[email protected]>
Co-authored-by: Xùdōng Yáng <[email protected]>
  • Loading branch information
3 people authored Sep 18, 2024
1 parent 7361bcc commit d769217
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.runtime.KeepGoingOption;
import com.google.devtools.build.lib.runtime.LoadingPhaseThreadsOption;
import com.google.devtools.build.lib.runtime.commands.TargetPatternsHelper;
import com.google.devtools.build.lib.runtime.commands.TestCommand;
import com.google.devtools.build.lib.server.FailureDetails;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
Expand Down Expand Up @@ -76,10 +77,7 @@ public final class FetchCommand implements BlazeCommand {

@Override
public void editOptions(OptionsParser optionsParser) {
// We only need to inject these options with fetch target (when there is a residue)
if (!optionsParser.getResidue().isEmpty()) {
TargetFetcher.injectNoBuildOption(optionsParser);
}
TargetFetcher.injectNoBuildOption(optionsParser);
}

@Override
Expand Down Expand Up @@ -111,9 +109,20 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti

BlazeCommandResult result;
LoadingPhaseThreadsOption threadsOption = options.getOptions(LoadingPhaseThreadsOption.class);
List<String> targets;
try {
targets = TargetPatternsHelper.readFrom(env, options);
} catch (TargetPatternsHelper.TargetPatternsHelperException e) {
env.getReporter().handle(Event.error(e.getMessage()));
return BlazeCommandResult.failureDetail(e.getFailureDetail());
}
try {
if (!options.getResidue().isEmpty()) {
result = fetchTarget(env, options, options.getResidue());
if (!targets.isEmpty()) {
if (!fetchOptions.repos.isEmpty()) {
return createFailedBlazeCommandResult(
env.getReporter(), "Target patterns and --repo cannot both be specified");
}
result = fetchTarget(env, options, targets);
} else if (!fetchOptions.repos.isEmpty()) {
result = fetchRepos(env, threadsOption, fetchOptions.repos);
} else { // --all, --configure, or just 'fetch'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.runtime.KeepGoingOption;
import com.google.devtools.build.lib.runtime.LoadingPhaseThreadsOption;
import com.google.devtools.build.lib.runtime.commands.TargetPatternsHelper;
import com.google.devtools.build.lib.runtime.commands.TestCommand;
import com.google.devtools.build.lib.server.FailureDetails;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
Expand Down Expand Up @@ -123,10 +124,7 @@ public void setDownloadManager(DownloadManager downloadManager) {

@Override
public void editOptions(OptionsParser optionsParser) {
// We only need to inject these options with fetch target (when there is a residue)
if (!optionsParser.getResidue().isEmpty()) {
TargetFetcher.injectNoBuildOption(optionsParser);
}
TargetFetcher.injectNoBuildOption(optionsParser);
}

@Override
Expand Down Expand Up @@ -158,9 +156,20 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
Path vendorDirectory =
env.getWorkspace().getRelative(options.getOptions(RepositoryOptions.class).vendorDirectory);
this.vendorManager = new VendorManager(vendorDirectory);
List<String> targets;
try {
targets = TargetPatternsHelper.readFrom(env, options);
} catch (TargetPatternsHelper.TargetPatternsHelperException e) {
env.getReporter().handle(Event.error(e.getMessage()));
return BlazeCommandResult.failureDetail(e.getFailureDetail());
}
try {
if (!options.getResidue().isEmpty()) {
result = vendorTargets(env, options, options.getResidue());
if (!targets.isEmpty()) {
if (!vendorOptions.repos.isEmpty()) {
return createFailedBlazeCommandResult(
env.getReporter(), "Target patterns and --repo cannot both be specified");
}
result = vendorTargets(env, options, targets);
} else if (!vendorOptions.repos.isEmpty()) {
result = vendorRepos(env, threadsOption, vendorOptions.repos);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.function.Predicate;

/** Provides support for reading target patterns from a file or the command-line. */
final class TargetPatternsHelper {
public final class TargetPatternsHelper {

private TargetPatternsHelper() {}

Expand Down Expand Up @@ -77,15 +77,15 @@ public static List<String> readFrom(CommandEnvironment env, OptionsParsingResult
}

/** Thrown when target patterns couldn't be read. */
static class TargetPatternsHelperException extends Exception {
public static class TargetPatternsHelperException extends Exception {
private final TargetPatterns.Code detailedCode;

private TargetPatternsHelperException(String message, TargetPatterns.Code detailedCode) {
super(Preconditions.checkNotNull(message));
this.detailedCode = detailedCode;
}

FailureDetail getFailureDetail() {
public FailureDetail getFailureDetail() {
return FailureDetail.newBuilder()
.setMessage(getMessage())
.setTargetPatterns(TargetPatterns.newBuilder().setCode(detailedCode))
Expand Down
38 changes: 38 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_fetch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,44 @@ def testFetchTarget(self):
_, stdout, _ = self.RunBazel(['run', '//:main', '--nofetch'])
self.assertIn('Hello there! => [email protected]', stdout)

def testFetchWithTargetPatternFile(self):
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
'bbb', '1.0', {'aaa': '1.0'}
)
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name = "bbb", version = "1.0")',
],
)
self.ScratchFile(
'BUILD',
[
'cc_binary(',
' name = "main",',
' srcs = ["main.cc"],',
' deps = [',
' "@bbb//:lib_bbb",',
' ],',
')',
],
)
self.ScratchFile(
'main.cc',
[
'#include "aaa.h"',
'int main() {',
' hello_aaa("Hello there!");',
'}',
],
)
self.ScratchFile('targets.params', ['//:main'])
self.RunBazel(['fetch', '--target_pattern_file=targets.params'])
# If we can run the target with --nofetch, this means we successfully
# fetched all its needed repos
_, stdout, _ = self.RunBazel(['run', '//:main', '--nofetch'])
self.assertIn('Hello there! => [email protected]', stdout)


if __name__ == '__main__':
absltest.main()
31 changes: 31 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_vendor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,37 @@ def on_rm_error(func, path, exc_info):
self.assertIn('bbb~', os.listdir(self._test_cwd + '/vendor'))
self.assertNotIn('ccc~', os.listdir(self._test_cwd + '/vendor'))

def testVendorWithTargetPatternFile(self):
self.main_registry.createCcModule('aaa', '1.0').createCcModule(
'bbb', '1.0'
).createCcModule('ccc', '1.0')
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name = "aaa", version = "1.0")',
'bazel_dep(name = "bbb", version = "1.0")',
'bazel_dep(name = "ccc", version = "1.0")',
],
)
self.ScratchFile('BUILD')
self.ScratchFile(
'targets.params',
[
'@aaa//:lib_aaa',
'@bbb//:lib_bbb',
],
)

self.RunBazel([
'vendor',
'--target_pattern_file=targets.params',
'--vendor_dir=vendor',
])
# Assert aaa & bbb and are vendored
self.assertIn('aaa~', os.listdir(self._test_cwd + '/vendor'))
self.assertIn('bbb~', os.listdir(self._test_cwd + '/vendor'))
self.assertNotIn('ccc~', os.listdir(self._test_cwd + '/vendor'))

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

0 comments on commit d769217

Please sign in to comment.