Skip to content

Commit

Permalink
Escape URLs before adding them to bazel rules
Browse files Browse the repository at this point in the history
Currently we produce the final URL by performing string
concatenation, which works fine in most cases.

The URL, however, might contain special characters that bazel
is later unable to process correctly. One such example:

  Error downloading passt-0^20221110.g4129764-1.el9.x86_64.rpm: Illegal character in path at index 78:
  https://mirror.stream.centos.org/9-stream/AppStream/x86_64/os/Packages/passt-0^20221110.g4129764-1.el9.x86_64.rpm

This issue made it necessary to manually tweak the WORKSPACE
file generated by bazeldnf:

  kubevirt/kubevirt@8052a7f

To solve this entire class of issues, use the functionality
offered by the net/url package to manipulare URLs. This will
automatically take care of performing all the necessary
escaping, as well as avoiding unnecessary slashes.

Signed-off-by: Andrea Bolognani <[email protected]>
  • Loading branch information
andreabolognani committed Feb 28, 2023
1 parent 5dd53c1 commit ced018c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bazel
import (
"fmt"
"io/ioutil"
"net/url"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -236,11 +237,15 @@ func (r *RPMRule) URLs() []string {
return nil
}

func (r *RPMRule) SetURLs(urls []string, href string) error {
func (r *RPMRule) SetURLs(mirrors []string, href string) error {
urlsAttr := []build.Expr{}
for _, url := range urls {
u := strings.TrimSuffix(url, "/") + "/" + strings.TrimSuffix(href, "/")
urlsAttr = append(urlsAttr, &build.StringExpr{Value: u})
for _, mirror := range mirrors {
u, err := url.Parse(mirror)
if err != nil {
return err
}
u = u.JoinPath(href)
urlsAttr = append(urlsAttr, &build.StringExpr{Value: u.String()})
}
r.Rule.SetAttr("urls", &build.ListExpr{List: urlsAttr})
return nil
Expand Down

0 comments on commit ced018c

Please sign in to comment.