Skip to content

Commit

Permalink
fix: always include files from the same workspace as the build target…
Browse files Browse the repository at this point in the history
… in `copy_to_directory`

Fixes bazel-contrib#359.

This updates the `copy_to_directory` tool to accept a workspace name representing the workspace of the target it is executing under. Any files in this workspace are automatically included, regardless of the `include_external_repositories` option. This makes it support usage within an external target (such as `@wksp//:dir`).
  • Loading branch information
dgp1130 committed Jul 30, 2023
1 parent e549125 commit 3c366bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/private/copy_to_directory.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ def copy_to_directory_bin_action(
inputs = file_inputs + [config_file],
outputs = [dst],
executable = copy_to_directory_bin,
arguments = [config_file.path],
arguments = [config_file.path, ctx.label.workspace_name],
mnemonic = "CopyToDirectory",
progress_message = "Copying files to directory %s" % _progress_path(dst),
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
Expand Down
15 changes: 10 additions & 5 deletions tools/copy_to_directory/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type config struct {
Verbose bool `json:"verbose"`

ReplacePrefixesKeys []string
TargetWorkspace string
}

type copyMap map[string]fileInfo
Expand All @@ -52,7 +53,7 @@ type pathSet map[string]bool
var copySet = copyMap{}
var mkdirSet = pathSet{}

func parseConfig(configPath string) (*config, error) {
func parseConfig(configPath string, wkspName string) (*config, error) {
f, err := os.Open(configPath)
if err != nil {
return nil, fmt.Errorf("failed to open config file: %w", err)
Expand All @@ -70,6 +71,7 @@ func parseConfig(configPath string) (*config, error) {
}

cfg.ReplacePrefixesKeys = maps.Keys(cfg.ReplacePrefixes)
cfg.TargetWorkspace = wkspName

return &cfg, nil
}
Expand Down Expand Up @@ -230,7 +232,9 @@ func (w *walker) copyPath(cfg *config, file fileInfo) error {
outputRoot := path.Dir(outputPath)

// apply include_external_repositories (if the file is from an external repository)
if file.Workspace != "" {
// automatically include files from the same workspace as this target, even if
// that is an external workspace with respect to `__main__`
if file.Workspace != "" && file.Workspace != cfg.TargetWorkspace {
match, err := anyGlobsMatch(cfg.IncludeExternalRepositories, file.Workspace)
if err != nil {
return err
Expand Down Expand Up @@ -376,12 +380,13 @@ func main() {
}
}

if len(args) != 1 {
fmt.Println("Usage: copy_to_directory config_file")
if len(args) != 2 {
fmt.Println("Usage: copy_to_directory config_file workspace_name")
os.Exit(1)
}

cfg, err := parseConfig(args[0])
wksp := args[1]
cfg, err := parseConfig(args[0], wksp)
if err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 3c366bf

Please sign in to comment.