serverless-plugin-typescript icon indicating copy to clipboard operation
serverless-plugin-typescript copied to clipboard

Is this plugin compatible when using multiple runtimes?

Open riltsken opened this issue 6 years ago • 12 comments

Heya. First off just wanted to say this plugin made my life a whole lot easier by not dealing with compilation configuration :)

The current issue I am running into is that I wanted to add a python lambda alongside my typescript lambdas. Regardless of my tsconfig / serverless.yml I can't seem to prevent serverless-plugin-typescript from trying to compile my python functions.

If you think this is NOT an issue with serverless-plugin-typescript I am happy to investigate more, but my current understanding is that this seems to be the culprit since I don't expect it to be jumping into my non-typescript folders.

If you think this is an issue that is easily fixable I am happy to dive in, but I just wanted to make sure I wasn't missing something and any direction you might provide before I do so.

My current layout is the following

root
 \_funcs
   \_geocode
   \_queue
 \_funcs_python
   \_logentries
 \_serverless.yml
 \_tsconfig.json

tsconfig

    "compilerOptions": {
        "allowSyntheticDefaultImports": false,
        "alwaysStrict": true,
        "allowJs": true,
        "declaration": false,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "noImplicitAny": false,
        "noUnusedLocals": true,
        "suppressImplicitAnyIndexErrors": false,
        "outDir": ".build",
        "rootDir": "./",
        "target": "es5",
        "lib": ["es2015"],
        "sourceMap": true,
        "alwaysStrict": true
    },
    "include": ["funcs/**/*.ts"],
    "compileOnSave": false,
    "atom": {
        "rewriteTsconfig": false
    },
    "exclude": [
        "dist"
    ]

serverless.yml (couple things omitted for privacy

service: mapping
frameworkVersion: "=1.22.0"
stage: dev

provider:
  name: aws
  runtime: nodejs6.10
  memorySize: 128
  timeout: 5

package:
  excludeDevDependencies: true
  include:
    - node_modules/**/*
    - funcs/**/*.ts

functions:
  geocode:
    name: ${self:service}-geocode
    handler: funcs/geocode/index.main
  queue:
    name: ${self:service}-queue
    handler: funcs/queue/index.main
  logentries:
    name: ${self:service}-logentries
    handler: funcs_python/logentries/main.lambda_handler
    runtime: python2.7

plugins:
  - serverless-plugin-typescript
  - serverless-prune-plugin

custom:
  prune:
    automatic: true
    number: 5

Error

$ SLS_DEBUG=* . ./settings.staging.sh && serverless deploy -s staging
Serverless: Load command run
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command create
Serverless: Load command install
Serverless: Load command package
Serverless: Load command deploy
Serverless: Load command deploy:function
Serverless: Load command deploy:list
Serverless: Load command deploy:list:functions
Serverless: Load command invoke
Serverless: Load command invoke:local
Serverless: Load command info
Serverless: Load command logs
Serverless: Load command login
Serverless: Load command logout
Serverless: Load command metrics
Serverless: Load command remove
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command slstats
Serverless: Load command plugin
Serverless: Load command plugin
Serverless: Load command plugin:install
Serverless: Load command plugin
Serverless: Load command plugin:uninstall
Serverless: Load command plugin
Serverless: Load command plugin:list
Serverless: Load command plugin
Serverless: Load command plugin:search
Serverless: Load command emit
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command prune
Serverless: Invoke deploy
Serverless: Invoke package
Serverless: Invoke aws:common:validate
Serverless: Invoke aws:common:cleanupTempDir
Serverless: Compiling with Typescript...
Serverless: Using local tsconfig.json
{ file: undefined,
  start: undefined,
  length: undefined,
  messageText: 'File \'funcs_python/logentries/main.ts\' not found.',
  category: 1,
  code: 6053 }
 
  Type Error ---------------------------------------------
 
  Cannot read property 'getLineAndCharacterOfPosition' of undefined
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Stack Trace --------------------------------------------
 
TypeError: Cannot read property 'getLineAndCharacterOfPosition' of undefined
    at allDiagnostics.forEach.diagnostic (/home/storiel/work/mapping/lambda/node_modules/serverless-plugin-typescript/dist/src/typescript.js:71:56)
    at Array.forEach (native)
    at Object.<anonymous> (/home/storiel/work/mapping/lambda/node_modules/serverless-plugin-typescript/dist/src/typescript.js:67:24)
    at Generator.next (<anonymous>)
    at /home/storiel/work/mapping/lambda/node_modules/serverless-plugin-typescript/dist/src/typescript.js:7:71
    at __awaiter (/home/storiel/work/mapping/lambda/node_modules/serverless-plugin-typescript/dist/src/typescript.js:3:12)
    at Object.run (/home/storiel/work/mapping/lambda/node_modules/serverless-plugin-typescript/dist/src/typescript.js:62:12)
    at TypeScriptPlugin.<anonymous> (/home/storiel/work/mapping/lambda/node_modules/serverless-plugin-typescript/dist/src/index.js:110:50)
    at Generator.next (<anonymous>)
    at /home/storiel/work/mapping/lambda/node_modules/serverless-plugin-typescript/dist/src/index.js:7:71
    at __awaiter (/home/storiel/work/mapping/lambda/node_modules/serverless-plugin-typescript/dist/src/index.js:3:12)
    at TypeScriptPlugin.compileTs (/home/storiel/work/mapping/lambda/node_modules/serverless-plugin-typescript/dist/src/index.js:99:16)
    at BbPromise.reduce (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/classes/PluginManager.js:358:55)
From previous event:
    at PluginManager.invoke (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/classes/PluginManager.js:358:22)
    at PluginManager.spawn (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/classes/PluginManager.js:376:17)
    at Deploy.BbPromise.bind.then.then (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/plugins/deploy/deploy.js:117:50)
From previous event:
    at Object.before:deploy:deploy [as hook] (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/plugins/deploy/deploy.js:107:10)
    at BbPromise.reduce (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/classes/PluginManager.js:358:55)
From previous event:
    at PluginManager.invoke (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/classes/PluginManager.js:358:22)
    at PluginManager.run (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/classes/PluginManager.js:389:17)
    at variables.populateService.then (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/Serverless.js:99:33)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
From previous event:
    at Serverless.run (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/lib/Serverless.js:86:74)
    at serverless.init.then (/home/storiel/.nvm/versions/node/v7.10.1/lib/node_modules/serverless/bin/serverless:39:50)
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless
 
  Your Environment Information -----------------------------
     OS:                     linux
     Node Version:           7.10.1
     Serverless Version:     1.22.0
 
error Command failed with exit code 1.

riltsken avatar Oct 24 '17 21:10 riltsken

stumbled into the same issue when trying to use two runtimes, any updates or solutions/workarounds?

jhaals avatar Feb 08 '18 18:02 jhaals

I did not solve this issue or find a workaround. At some point I was planning on compiling manually to avoid this issue, but just haven't had a big need to do it. Still think this lib is the bees knees if not using multiple runtimes :)

riltsken avatar Feb 08 '18 18:02 riltsken

Also ran into the same issue. Exclude doesn't seem to do anything.

liam-betsworth avatar Jul 02 '18 12:07 liam-betsworth

Running into the same issue! I think this issue prevents usage of: https://github.com/svdgraaf/serverless-basic-authentication as it requires some python

ACollectionOfAtoms avatar May 22 '19 23:05 ACollectionOfAtoms

Same issue here. When trying to run serverless-offline it crashes for python lambda functions as its looking for .js or .ts file extensions.

Jordan-Eckowitz avatar May 18 '20 16:05 Jordan-Eckowitz

super ugly hack, but just because there is no workaround listed here yet...

patch dist/src/index.js with this:

--- index.js.orig       2021-01-18 10:54:23.000000000 -1000
+++ index.js.new        2021-01-18 10:54:57.000000000 -1000
@@ -81,7 +81,7 @@
                 [options.function]: service.functions[this.options.function]
             };
         }
-        return service.functions;
+        return Object.keys(service.functions).reduce(function (memo, k) { if (service.functions[k].typescript !== false) memo[k] = service.functions[k]; }, {});
     }
     get rootFileNames() {
         return typescript.extractFileNames(this.originalServicePath, this.serverless.service.provider.name, this.functions);

and then add typescript: false to any function that isn't Typescript. (you'll see a warning about an unknown property)

bobzoller avatar Jan 18 '21 20:01 bobzoller

@bobzoller thanks a lot for the hack! Thanks to your comment I found how to avoid this problem without any changes of the node_modules files.

TS compiling successfully done if I creates a same name .ts file near the Python handler.

It means, I have a function config in the serverles.yml:

functions:
  prepare-data:
    name: prepare-data
    handler: functions/prepare-data/src/handler.convert_data
    runtime: python3.6

And two files

Python:

# functions/prepare-data/src/handler.py
def convert_data(event, context):
    print('Hello Python!!1')

    return 0

TS:

// functions/prepare-data/src/handler.ts
// empty file

And it helps to avoid the compilation failture

serp-ya avatar Aug 06 '21 01:08 serp-ya

Hi @serp-ya and @bobzoller , with your hack TS compiling successfully, but lambda contains only js file. Is there a setting for maintain the py file?

Thanks, Andrea

Cittadini avatar Aug 26 '21 13:08 Cittadini

@Cittadini I'm not sure why it happened on your side but my lambda works fine with python code... Above I shown a my Lambda configs and it contains important line runtime: python3.6 and I have a global config provider.runtime: nodejs12.x in `serverless.yml too.

I hope it will helps you.

P.S. And not forget to describe what is a function will need to use for a Lambda deploy. I use a converter_data here =>handler: functions/prepare-data/src/handler.convert_data

serp-ya avatar Aug 27 '21 12:08 serp-ya

Hi @serp-ya thanks for your tips. I have a config like yours. Maybe I have a plugin that remove the py files: I hope to find it!

Thanks, Andrea

Cittadini avatar Sep 01 '21 07:09 Cittadini

This is fixed now in master, fixed by PR #277

vectorjohn avatar Oct 03 '22 19:10 vectorjohn

I ran into the error Error: Typescript compilation failed. Please ensure handlers exists with ext .ts or .js when trying to use both JS/TS and Go handlers.

I was using v2.1.2 Upgrading to v2.1.4 solved it for me.

ScopeSV avatar Oct 18 '22 17:10 ScopeSV