Skip to content

Commit

Permalink
Fix: TypeScript Build Bugs (#115)
Browse files Browse the repository at this point in the history
* Fixing the last few typescript generator bugs

Signed-off-by: Shivansh Vij <[email protected]>

* Fixing lint timeout issue

Signed-off-by: Shivansh Vij <[email protected]>

---------

Signed-off-by: Shivansh Vij <[email protected]>
  • Loading branch information
ShivanshVij authored Oct 9, 2023
1 parent d435a5d commit 2b5dc02
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
version: latest
args: --timeout=5m
2 changes: 2 additions & 0 deletions build/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func LocalGolang(options *LocalGolangOptions) (*scalefunc.Schema, error) {
cmd.Dir = compilePath
cmd.Stdout = options.Output
cmd.Stderr = options.Output
cmd.Env = os.Environ()
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("unable to compile scale function: %w", err)
Expand All @@ -212,6 +213,7 @@ func LocalGolang(options *LocalGolangOptions) (*scalefunc.Schema, error) {
cmd.Dir = compilePath
cmd.Stdout = options.Output
cmd.Stderr = options.Output
cmd.Env = os.Environ()
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("unable to compile scale function: %w", err)
Expand Down
20 changes: 11 additions & 9 deletions build/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,6 @@ func LocalRust(options *LocalRustOptions) (*scalefunc.Schema, error) {
return nil, fmt.Errorf("unable to create lib.rs file: %w", err)
}

cmd := exec.Command(options.CargoBin, "check")
cmd.Dir = compilePath
cmd.Stdout = options.Output
cmd.Stderr = options.Output
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("unable to compile scale function: %w", err)
}

var target string
switch options.Target {
case WASITarget:
Expand All @@ -179,6 +170,16 @@ func LocalRust(options *LocalRustOptions) (*scalefunc.Schema, error) {
return nil, fmt.Errorf("unknown build target %d", options.Target)
}

cmd := exec.Command(options.CargoBin, "check", "--target", target)
cmd.Dir = compilePath
cmd.Stdout = options.Output
cmd.Stderr = options.Output
cmd.Env = os.Environ()
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("unable to compile scale function: %w", err)
}

buildArgs := append([]string{"build"}, options.Args...)
if options.Release {
buildArgs = append(buildArgs, "--release")
Expand All @@ -189,6 +190,7 @@ func LocalRust(options *LocalRustOptions) (*scalefunc.Schema, error) {
cmd.Dir = compilePath
cmd.Stdout = options.Output
cmd.Stderr = options.Output
cmd.Env = os.Environ()
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("unable to compile scale function: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions build/typescript.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func LocalTypescript(options *LocalTypescriptOptions) (*scalefunc.Schema, error)
cmd.Dir = compilePath
cmd.Stderr = options.Output
cmd.Stdout = options.Output
cmd.Env = os.Environ()
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("unable to compile scale function and update npm: %w", err)
Expand Down Expand Up @@ -266,6 +267,7 @@ func LocalTypescript(options *LocalTypescriptOptions) (*scalefunc.Schema, error)
cmd.Stdin = strings.NewReader(string(result.OutputFiles[0].Contents))
cmd.Stderr = options.Output
cmd.Stdout = options.Output
cmd.Env = os.Environ()
err = cmd.Run()
if err != nil {
return nil, fmt.Errorf("unable to compile scale function using js_builder: %w", err)
Expand Down
8 changes: 6 additions & 2 deletions compile/rust/templates/lib.rs.templ
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ pub unsafe extern "C" fn initialize() -> u64 {
{{ if .package_schema.Initialize }}
return match {{ .package_schema.Name }}::{{ .package_schema.Initialize }}() {
Ok(_) => { 0 },
Err(err) => { packUint32(signature.Error(err)) }
Err(err) => {
let (ptr, len) = signature::error(err);
pack_uint32(ptr, len)
}
};
{{ end }}
{{ else }}
0
{{ end }}
}

#[cfg_attr(target_arch = "wasm32", export_name = "run")]
Expand Down
2 changes: 1 addition & 1 deletion compile/typescript/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,5 @@ func (m *Manifest) Write() ([]byte, error) {
m.packageJSON.internal["devDependencies"] = m.packageJSON.DevDependencies
}

return json.Marshal(m.packageJSON.internal)
return json.MarshalIndent(m.packageJSON.internal, "", "\t")
}
12 changes: 5 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@ go 1.20

require (
github.com/BurntSushi/toml v1.3.2
github.com/evanw/esbuild v0.19.3
github.com/evanw/esbuild v0.19.4
github.com/google/uuid v1.3.1
github.com/hashicorp/hcl/v2 v2.18.0
github.com/hashicorp/hcl/v2 v2.18.1
github.com/loopholelabs/polyglot v1.1.3
github.com/loopholelabs/scale-signature-interfaces v0.1.7
github.com/stretchr/testify v1.8.4
github.com/tetratelabs/wazero v1.5.0
golang.org/x/mod v0.12.0
golang.org/x/mod v0.13.0
golang.org/x/text v0.13.0
)

require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/zclconf/go-cty v1.13.2 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/tools v0.8.1-0.20230428195545-5283a0178901 // indirect
github.com/zclconf/go-cty v1.14.1 // indirect
golang.org/x/sys v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
25 changes: 11 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo=
github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v13 v13.0.0 h1:Y+KvPE1NYz0xl601PVImeQfFyEy6iT90AvPUL1NNfNw=
github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkEghdlcqw7yxLeM89kiTRPUo=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/evanw/esbuild v0.19.3 h1:foPr0xwQM3lBWKBtscauTN9FrmJzRDVI2+EGOs82H/I=
github.com/evanw/esbuild v0.19.3/go.mod h1:iINY06rn799hi48UqEnaQvVfZWe6W9bET78LbvN8VWk=
github.com/evanw/esbuild v0.19.4 h1:Etk+6ZCjtNxZZLEgMKSqpO0/oM0k1WYKJabaPMJ39iQ=
github.com/evanw/esbuild v0.19.4/go.mod h1:iINY06rn799hi48UqEnaQvVfZWe6W9bET78LbvN8VWk=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl/v2 v2.18.0 h1:wYnG7Lt31t2zYkcquwgKo6MWXzRUDIeIVU5naZwHLl8=
github.com/hashicorp/hcl/v2 v2.18.0/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/hashicorp/hcl/v2 v2.18.1 h1:6nxnOJFku1EuSawSD81fuviYUV8DxFr3fp2dUi3ZYSo=
github.com/hashicorp/hcl/v2 v2.18.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -44,17 +42,16 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tetratelabs/wazero v1.5.0 h1:Yz3fZHivfDiZFUXnWMPUoiW7s8tC1sjdBtlJn08qYa0=
github.com/tetratelabs/wazero v1.5.0/go.mod h1:0U0G41+ochRKoPKCJlh0jMg1CHkyfK8kDqiirMmKY8A=
github.com/zclconf/go-cty v1.13.2 h1:4GvrUxe/QUDYuJKAav4EYqdM47/kZa672LwmXFmEKT0=
github.com/zclconf/go-cty v1.13.2/go.mod h1:YKQzy/7pZ7iq2jNFzy5go57xdxdWoLLpaEp4u238AE0=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
github.com/zclconf/go-cty v1.14.1 h1:t9fyA35fwjjUMcmL5hLER+e/rEPqrbCK1/OSE4SI9KA=
github.com/zclconf/go-cty v1.14.1/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.8.1-0.20230428195545-5283a0178901 h1:0wxTF6pSjIIhNt7mo9GvjDfzyCOiWhmICgtO/Ah948s=
golang.org/x/tools v0.8.1-0.20230428195545-5283a0178901/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
Expand Down
102 changes: 92 additions & 10 deletions signature/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ type GuestRegistryPackage struct {
}

type GuestLocalPackage struct {
GolangFiles []File
RustFiles []File
TypescriptFiles []File
GolangFiles []File
RustFiles []File
TypescriptFiles []File
TypescriptPackage *bytes.Buffer
}

type HostRegistryPackage struct {
Expand All @@ -59,8 +60,9 @@ type HostRegistryPackage struct {
}

type HostLocalPackage struct {
GolangFiles []File
TypescriptFiles []File
GolangFiles []File
TypescriptFiles []File
TypescriptPackage *bytes.Buffer
}

type Options struct {
Expand Down Expand Up @@ -324,10 +326,50 @@ func GenerateGuestLocal(options *Options) (*GuestLocalPackage, error) {
NewFile("package.json", "package.json", packageJSON),
}

typescriptBuffer := new(bytes.Buffer)
gzipTypescriptWriter := gzip.NewWriter(typescriptBuffer)
tarTypescriptWriter := tar.NewWriter(gzipTypescriptWriter)

var header *tar.Header
for _, file := range typescriptFiles {
header, err = tar.FileInfoHeader(file, file.Name())
if err != nil {
_ = tarTypescriptWriter.Close()
_ = gzipTypescriptWriter.Close()
return nil, fmt.Errorf("failed to create tar header for %s: %w", file.Name(), err)
}

header.Name = path.Join("package", header.Name)

err = tarTypescriptWriter.WriteHeader(header)
if err != nil {
_ = tarTypescriptWriter.Close()
_ = gzipTypescriptWriter.Close()
return nil, fmt.Errorf("failed to write tar header for %s: %w", file.Name(), err)
}
_, err = tarTypescriptWriter.Write(file.Data())
if err != nil {
_ = tarTypescriptWriter.Close()
_ = gzipTypescriptWriter.Close()
return nil, fmt.Errorf("failed to write tar data for %s: %w", file.Name(), err)
}
}

err = tarTypescriptWriter.Close()
if err != nil {
return nil, fmt.Errorf("failed to close tar writer: %w", err)
}

err = gzipTypescriptWriter.Close()
if err != nil {
return nil, fmt.Errorf("failed to close gzip writer: %w", err)
}

return &GuestLocalPackage{
GolangFiles: golangFiles,
RustFiles: rustFiles,
TypescriptFiles: typescriptFiles,
GolangFiles: golangFiles,
RustFiles: rustFiles,
TypescriptFiles: typescriptFiles,
TypescriptPackage: typescriptBuffer,
}, nil
}

Expand Down Expand Up @@ -508,8 +550,48 @@ func GenerateHostLocal(options *Options) (*HostLocalPackage, error) {
NewFile("package.json", "package.json", packageJSON),
}

typescriptBuffer := new(bytes.Buffer)
gzipTypescriptWriter := gzip.NewWriter(typescriptBuffer)
tarTypescriptWriter := tar.NewWriter(gzipTypescriptWriter)

var header *tar.Header
for _, file := range typescriptFiles {
header, err = tar.FileInfoHeader(file, file.Name())
if err != nil {
_ = tarTypescriptWriter.Close()
_ = gzipTypescriptWriter.Close()
return nil, fmt.Errorf("failed to create tar header for %s: %w", file.Name(), err)
}

header.Name = path.Join("package", header.Name)

err = tarTypescriptWriter.WriteHeader(header)
if err != nil {
_ = tarTypescriptWriter.Close()
_ = gzipTypescriptWriter.Close()
return nil, fmt.Errorf("failed to write tar header for %s: %w", file.Name(), err)
}
_, err = tarTypescriptWriter.Write(file.Data())
if err != nil {
_ = tarTypescriptWriter.Close()
_ = gzipTypescriptWriter.Close()
return nil, fmt.Errorf("failed to write tar data for %s: %w", file.Name(), err)
}
}

err = tarTypescriptWriter.Close()
if err != nil {
return nil, fmt.Errorf("failed to close tar writer: %w", err)
}

err = gzipTypescriptWriter.Close()
if err != nil {
return nil, fmt.Errorf("failed to close gzip writer: %w", err)
}

return &HostLocalPackage{
GolangFiles: golangFiles,
TypescriptFiles: typescriptFiles,
GolangFiles: golangFiles,
TypescriptFiles: typescriptFiles,
TypescriptPackage: typescriptBuffer,
}, nil
}
34 changes: 34 additions & 0 deletions storage/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,21 @@ func GenerateSignature(sig *signature.Schema, name string, tag string, org strin
return err
}

err = os.MkdirAll(path.Join(directory, "typescript", "guest"), 0755)
if err != nil {
return err
}

err = os.MkdirAll(path.Join(directory, "golang", "host"), 0755)
if err != nil {
return err
}

err = os.MkdirAll(path.Join(directory, "typescript", "host"), 0755)
if err != nil {
return err
}

guestPackage, err := generator.GenerateGuestLocal(&generator.Options{
Signature: sig,

Expand Down Expand Up @@ -338,6 +348,18 @@ func GenerateSignature(sig *signature.Schema, name string, tag string, org strin
}
}

for _, file := range guestPackage.TypescriptFiles {
err = os.WriteFile(path.Join(directory, "typescript", "guest", file.Path()), file.Data(), 0644)
if err != nil {
return err
}
}

err = os.WriteFile(path.Join(directory, "typescript", "guest.tar.gz"), guestPackage.TypescriptPackage.Bytes(), 0644)
if err != nil {
return err
}

hostPackage, err := generator.GenerateHostLocal(&generator.Options{
Signature: sig,

Expand All @@ -358,5 +380,17 @@ func GenerateSignature(sig *signature.Schema, name string, tag string, org strin
}
}

for _, file := range hostPackage.TypescriptFiles {
err = os.WriteFile(path.Join(directory, "typescript", "host", file.Path()), file.Data(), 0644)
if err != nil {
return err
}
}

err = os.WriteFile(path.Join(directory, "typescript", "host.tar.gz"), hostPackage.TypescriptPackage.Bytes(), 0644)
if err != nil {
return err
}

return nil
}

0 comments on commit 2b5dc02

Please sign in to comment.