From faae74bf5a3bb96b00c63b439dc1c780fc50bb83 Mon Sep 17 00:00:00 2001 From: Matej Vasek Date: Thu, 9 Mar 2023 17:10:56 +0100 Subject: [PATCH] fix: node s2i build when node_modules present This is workaround for two bug in another components: * The s2i CLI/library is not honoring the `--exclude` flag when used with the `--as-dockerfile` flag. * The node s2i image is not working if project contains `node_modules` directory with NodeJS modules. If only one of the bugs above were fixed this commit wouldn't be necessary. Signed-off-by: Matej Vasek --- pkg/builders/s2i/builder.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/builders/s2i/builder.go b/pkg/builders/s2i/builder.go index 1c09846f98..fdad35a31d 100644 --- a/pkg/builders/s2i/builder.go +++ b/pkg/builders/s2i/builder.go @@ -10,6 +10,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "strings" "github.com/docker/docker/api/types" @@ -204,6 +205,9 @@ func (b *Builder) Build(ctx context.Context, f fn.Function) (err error) { pr, pw := io.Pipe() + // s2i apparently is not excluding the files in --as-dockerfile mode + exclude := regexp.MustCompile(cfg.ExcludeRegExp) + const up = ".." + string(os.PathSeparator) go func() { tw := tar.NewWriter(pw) @@ -220,6 +224,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function) (err error) { return nil } + p = filepath.ToSlash(p) + + if exclude.MatchString(p) { + return nil + } + lnk := "" if fi.Mode()&fs.ModeSymlink != 0 { lnk, err = os.Readlink(path) @@ -241,7 +251,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function) (err error) { if err != nil { return fmt.Errorf("cannot create tar header: %w", err) } - hdr.Name = filepath.ToSlash(p) + hdr.Name = p err = tw.WriteHeader(hdr) if err != nil {