Skip to content
Merged
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
36 changes: 36 additions & 0 deletions cmd/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -159,3 +160,38 @@ func ToStringSlice(args []any) []string {
}
return result
}

// Gets a build command for either Golang or Typescript based on the filename
func GetBuildCmd(inputFile string, outputFile string, rootFolder string) *exec.Cmd {
isTypescriptWorkflow := strings.HasSuffix(inputFile, ".ts")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not safe after this change: https://github.com/smartcontractkit/cre-cli/pull/36/files#r2382877260

cc @timothyF95 who is working on providing a path instead of the main.go.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeffrifwaldsmartcontract for typescipt, do we have to specify the x.ts file or it is okay to just give the path of the workflow directory where the x.ts lives? Relevant ticket: https://smartcontract-it.atlassian.net/browse/DEVSVCS-2420

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can adapt this to however Go wants to ultimately address figuring out what the input file is. Right now the inputFile is read from the command line, so examining that filename is the easiest.

Looking at the proposals, maybe we can instead just read the directory and see if main.go or main.ts exists in the folder.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you think this is OK for now or if we want to wait on this until those other tickets are resolved.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we go with the directory approach, what if there are multiple ts files in the directory? For go, it compiles and build the whole directory, so it doesnt matter. I am not sure about TS.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'll need to see how the Go compile command will ultimately be utilized. It is certainly possible to scan a directory tree for ts files, but is there something inherently special about the file name main.go or main.ts in those directories?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, how do we figure out the workflow entry point when dealing with a directory? I might have a bunch of scripts sitting around with .ts extensions that I don't want compiled.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the way Go build is gonna be GOOS=wasip1 GOARCH=wasm go build -o w.wasm ./DirectoryOfWorkflowEntry.

I guess if TS has to specify the TS entry file, we can treat Go and TS differently in the setting, where Go is directory of workflow, while TS is the path to ts file (e.g. main.ts)

Thoughts @ejacquier @timothyF95


var buildCmd *exec.Cmd
if isTypescriptWorkflow {
buildCmd = exec.Command(
"bun",
"cre-compile",
inputFile,
outputFile,
)
} else {
// The build command for reproducible and trimmed binaries.
// -trimpath removes all file system paths from the compiled binary.
// -ldflags="-buildid= -w -s" further reduces the binary size:
// -buildid= removes the build ID, ensuring reproducibility.
// -w disables DWARF debugging information.
// -s removes the symbol table.
buildCmd = exec.Command(
"go",
"build",
"-o", outputFile,
"-trimpath",
"-ldflags=-buildid= -w -s",
inputFile,
)
buildCmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm", "CGO_ENABLED=0")
}

buildCmd.Dir = rootFolder

return buildCmd
}
22 changes: 3 additions & 19 deletions cmd/workflow/deploy/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"encoding/base64"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/andybalholm/brotli"

cmdcommon "github.com/smartcontractkit/cre-cli/cmd/common"
)

func (h *handler) Compile() error {
Expand Down Expand Up @@ -43,24 +44,7 @@ func (h *handler) Compile() error {

tmpWasmFileName := "tmp.wasm"
workflowMainFile := filepath.Base(h.inputs.WorkflowPath)

// The build command for reproducible and trimmed binaries.
// -trimpath removes all file system paths from the compiled binary.
// -ldflags="-buildid= -w -s" further reduces the binary size:
// -buildid= removes the build ID, ensuring reproducibility.
// -w disables DWARF debugging information.
// -s removes the symbol table.
buildCmd := exec.Command(
"go",
"build",
"-o", tmpWasmFileName,
"-trimpath",
"-ldflags=-buildid= -w -s",
workflowMainFile,
)

buildCmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm", "CGO_ENABLED=0")
buildCmd.Dir = workflowRootFolder
buildCmd := cmdcommon.GetBuildCmd(workflowMainFile, tmpWasmFileName, workflowRootFolder)
h.log.Debug().
Str("Workflow directory", buildCmd.Dir).
Str("Command", buildCmd.String()).
Expand Down
33 changes: 3 additions & 30 deletions cmd/workflow/simulate/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"math"
"math/big"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -38,6 +37,7 @@ import (
simulator "github.com/smartcontractkit/chainlink/v2/core/services/workflows/cmd/cre/utils"
v2 "github.com/smartcontractkit/chainlink/v2/core/services/workflows/v2"

cmdcommon "github.com/smartcontractkit/cre-cli/cmd/common"
"github.com/smartcontractkit/cre-cli/internal/runtime"
"github.com/smartcontractkit/cre-cli/internal/settings"
"github.com/smartcontractkit/cre-cli/internal/validation"
Expand Down Expand Up @@ -182,35 +182,8 @@ func (h *handler) Execute(inputs Inputs) error {
workflowRootFolder := filepath.Dir(inputs.WorkflowPath)
tmpWasmFileName := "tmp.wasm"
workflowMainFile := filepath.Base(inputs.WorkflowPath)
isTypescriptWorkflow := strings.HasSuffix(workflowMainFile, ".ts")

var buildCmd *exec.Cmd
if isTypescriptWorkflow {
buildCmd = exec.Command(
"bun",
"cre-compile",
workflowMainFile,
tmpWasmFileName,
)
} else {
// The build command for reproducible and trimmed binaries.
// -trimpath removes all file system paths from the compiled binary.
// -ldflags="-buildid= -w -s" further reduces the binary size:
// -buildid= removes the build ID, ensuring reproducibility.
// -w disables DWARF debugging information.
// -s removes the symbol table.
buildCmd = exec.Command(
"go",
"build",
"-o", tmpWasmFileName,
"-trimpath",
"-ldflags=-buildid= -w -s",
workflowMainFile,
)
buildCmd.Env = append(os.Environ(), "GOOS=wasip1", "GOARCH=wasm", "CGO_ENABLED=0")
}

buildCmd.Dir = workflowRootFolder
buildCmd := cmdcommon.GetBuildCmd(workflowMainFile, tmpWasmFileName, workflowRootFolder)

h.log.Debug().
Str("Workflow directory", buildCmd.Dir).
Str("Command", buildCmd.String()).
Expand Down
Loading