Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
sst.aws.Function: do version check to make sure sst sdk matches sst c…
Browse files Browse the repository at this point in the history
…li version
  • Loading branch information
thdxr committed Oct 14, 2024
1 parent 50ffae2 commit 701585c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func New(input *ProjectConfig) (*Project, error) {
env: map[string]string{},
Runtime: runtime.NewCollection(
input.Config,
node.New(),
node.New(input.Version),
worker.New(),
python.New(),
),
Expand Down
44 changes: 43 additions & 1 deletion pkg/runtime/node/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"slices"
"strings"

"github.com/evanw/esbuild/pkg/api"
esbuild "github.com/evanw/esbuild/pkg/api"
"github.com/sst/ion/internal/fs"
"github.com/sst/ion/pkg/js"
Expand Down Expand Up @@ -64,7 +65,48 @@ func (r *Runtime) Build(ctx context.Context, input *runtime.BuildInput) (*runtim
loader[key] = mapped
}

plugins := []esbuild.Plugin{}
plugins := []esbuild.Plugin{
{
Name: "sst-version-check",
Setup: func(build esbuild.PluginBuild) {
skipResolve := struct{}{}
build.OnResolve(api.OnResolveOptions{Filter: `^sst$`}, func(args api.OnResolveArgs) (api.OnResolveResult, error) {
// avoid recursion
if args.PluginData == skipResolve {
return api.OnResolveResult{}, nil
}
pkg := build.Resolve("sst", esbuild.ResolveOptions{
ResolveDir: args.ResolveDir,
Importer: args.Importer,
Kind: args.Kind,
With: args.With,
PluginName: "sst-version-check",
PluginData: skipResolve,
Namespace: args.Namespace,
})
if pkg.Path != "" {
path, err := fs.FindUp(pkg.Path, "package.json")
if err != nil {
return api.OnResolveResult{}, err
}
var pkgjson js.PackageJson
data, err := os.Open(path)
if err != nil {
return api.OnResolveResult{}, err
}
err = json.NewDecoder(data).Decode(&pkgjson)
if err != nil {
return api.OnResolveResult{}, err
}
if r.version != "dev" && pkgjson.Version != r.version {
return api.OnResolveResult{}, fmt.Errorf("The sst package your application is importing (%v) does not match the sst cli version (%v). Make sure the version of sst in package.json is correct across your entire repo.", pkgjson.Version, r.version)
}
}
return api.OnResolveResult{Path: pkg.Path}, nil
})
},
},
}
if properties.Plugins != "" {
plugins = append(plugins, plugin(properties.Plugins))
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/runtime/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ var LoaderToString = []string{
}

type Runtime struct {
cfgPath string
version string
contexts sync.Map
results sync.Map
concurrency *semaphore.Weighted
}

func New() *Runtime {
func New(version string) *Runtime {
weight := int64(4)
if flag.SST_BUILD_CONCURRENCY != "" {
weight, _ = strconv.ParseInt(flag.SST_BUILD_CONCURRENCY, 10, 64)
}
return &Runtime{
contexts: sync.Map{},
results: sync.Map{},
version: version,
concurrency: semaphore.NewWeighted(weight),
}
}
Expand Down

0 comments on commit 701585c

Please sign in to comment.