Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support --target_pattern_file for fetch and vendor command #23640

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 @@ -77,10 +78,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 @@ -112,9 +110,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 @@ -45,6 +45,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 @@ -124,10 +125,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 @@ -159,9 +157,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()
29 changes: 29 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_vendor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,35 @@ 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
Loading