From 701585c40aaa4c9464cc88564afed41a29806e56 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Mon, 14 Oct 2024 13:12:51 -0400 Subject: [PATCH] sst.aws.Function: do version check to make sure sst sdk matches sst cli version --- pkg/project/project.go | 2 +- pkg/runtime/node/build.go | 44 ++++++++++++++++++++++++++++++++++++++- pkg/runtime/node/node.go | 5 +++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/pkg/project/project.go b/pkg/project/project.go index c7ade2d10..63c93e9c3 100644 --- a/pkg/project/project.go +++ b/pkg/project/project.go @@ -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(), ), diff --git a/pkg/runtime/node/build.go b/pkg/runtime/node/build.go index 032b3735d..90856b9c5 100644 --- a/pkg/runtime/node/build.go +++ b/pkg/runtime/node/build.go @@ -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" @@ -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)) } diff --git a/pkg/runtime/node/node.go b/pkg/runtime/node/node.go index b8fd3774f..973cf56e0 100644 --- a/pkg/runtime/node/node.go +++ b/pkg/runtime/node/node.go @@ -58,13 +58,13 @@ 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) @@ -72,6 +72,7 @@ func New() *Runtime { return &Runtime{ contexts: sync.Map{}, results: sync.Map{}, + version: version, concurrency: semaphore.NewWeighted(weight), } }