Having trouble with sst.Function bundling in my mo...
# help
m
Having trouble with sst.Function bundling in my monorepo - https://github.com/serverless-stack/serverless-stack/issues/1575
t
yeah seems like your sense of why this is happening is right
we try to replicate your repo structure inside the function bundle so it works for people using sourcemaps with things like sentry
but this looks like a bug with that
m
right now I'm using SST with CDK NodejsFunction but I'd rather just use Function if possible
i have a monorepo with
packages/service/...
- lambda handlers
packages/infra/..
- CDK IAC
t
yeah I think this is one of the lingering issues when sst.json is not at root, we kind of assume that in a few places
m
what do you suggest?
t
well our official structure we recommend has sst in the root package and the rest of it in packages/* subpackages
but if you can't do that then you'll need to wait a bit for us to restructure this - we're going to be looking at these things mid april
m
i tried that and had problems, i forget what exactly, but it's not really compatible with a monorepo setup
t
It's the structure I use for my monorepo
m
and you have all the CDK deps in the top level package?
t
I'm using yarn 1.x + yarn workspaces
Yeah
m
i'm using npm workspaces
t
which is just aws-cdk-lib at this point
m
sounds wack imo
i want to keep infra stuff separate in its own package
t
it just comes down to how you view it, what you have can make sense if you think of your infra as a standalone subpackage of your project. But others view it as the thing they use to operate the whole project just like how you'd install typescript at the root
If the above issue is your only problem you can probably patch a fix that doesn't nest the handlers in the directory. Should be somewhere here: https://github.com/serverless-stack/serverless-stack/blob/master/packages/core/src/runtime/handler/node.ts#L100
m
ok
I tried moving sst to the top level
Copy code
➜  platform git:(sst-function) ✗ npx sst deploy                                                                                                                                                                                            <aws:tombo-dev>
Using stage: mish
Preparing your SST app

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/cyber/dev/platform/node_modules/dirname-filename-esm/index.js
require() of ES modules is not supported.
require() of /Users/cyber/dev/platform/node_modules/dirname-filename-esm/index.js from /Users/cyber/dev/platform/.build/lib/index.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename /Users/cyber/dev/platform/node_modules/dirname-filename-esm/index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/cyber/dev/platform/node_modules/dirname-filename-esm/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (/Users/cyber/dev/platform/packages/infra/lib/paths.ts:3:25)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)

There was an error synthesizing your app.
t
this is another problem we need to fix, currently when sst synthesizes your stacks it's very cjs focused. So your root package.json can't be a module. Another one on our list
I have the root package as non-module and my subpackages as modules (I think)
m
I set top-level package.json to
commonjs
but I still have the same error
problem seems to be
/Users/cyber/dev/platform/node_modules/dirname-filename-esm/package.json.
t
oh yeah just noticed that
m
Copy code
function absolutePathToRelativePath(absolutePath: string): string {
  if (!path.isAbsolute(absolutePath)) {
    return absolutePath;
  }

  // For win32: root for D:\\path\\to\\dir is D:\\
  // For posix: root for /path/to/dir is /
  const { root } = path.parse(absolutePath);
  return absolutePath.substring(root.length);
}
not sure I understand this last part, or how this affects the folder structure of the output package
or why the bundle is empty when i pass a relative path of
../..
in
I'd love to switch to using sst.Function but I'm not sure how to proceed from here
t
@Frank can you elaborate on that function
j
@Frank just bringing this up again
m
would really like to be using SST.Function but this is blocking me still
sst.Queue doesn't like my fake sst.Function
Copy code
Error: Please use sst.Function instead of lambda.Function for the "Consumer_ProfileImportQueue" Function.
    at Function.fromDefinition (/Users/cyber/dev/platform/node_modules/@serverless-stack/resources/src/Function.ts:527:13)
    at Queue.addConsumer (/Users/cyber/dev/platform/node_modules/@serverless-stack/resources/src/Queue.ts:100:32)
Any word on this issue with bundling in a monorepo? Would really love to use SST in my main big project but this is blocking me
t
I'm going to fix this problem this week
m
swizzle
any luck on this?
t
sorry not yet, conf prep took over this week