Skip to content

A valid service attribute to satisfy the declaration 'self:service.name' could not be found #4744

Closed

Description

@Vadorequest

This is a (Bug Report)

Description

For bug reports:

  • What went wrong?

I'm trying to use dynamic variables in serverless.yml and it fails.
Basically, I have the following in my provider:

  environment:
    SERVICE_NAME: ${self:service.name}
    DYNAMODB_TABLE: ${self:service.name}-test

The SERVICE_NAME is correctly resolved IF I comment the DYNAMODB_TABLE: "${self:service.name}-test", otherwise it fails with the following stack trace.

If I use ${self:service.name} alone it works, but if I add some string after, then it fails: ${self:service.name}-test

  • What did you expect should have happened?

Resolving variables correctly

  • What stacktrace or error message from your provider did you see?
➜  simulator-feedback git:(master) ✗ sls print
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 print
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 webpack
 
 Serverless Warning --------------------------------------
 
  A valid service attribute to satisfy the declaration 'self:service.name' could not be found.
 
 
 Serverless Warning --------------------------------------
 
  A valid service attribute to satisfy the declaration 'self:service.name' could not be found.
 
 
  Serverless Error ---------------------------------------
 
  Trying to populate non string value into a string for variable ${self:service.name}. Please make sure the value of the property is a string.
 
  Stack Trace --------------------------------------------
 
ServerlessError: Trying to populate non string value into a string for variable ${self:service.name}. Please make sure the value of the property is a string.
    at Variables.populateVariable (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:156:13)
    at pendingMatches.push.pendingMatch.then.matchedValue (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:124:21)
    at runCallback (timers.js:789:20)
    at tryOnImmediate (timers.js:751:5)
    at processImmediate [as _immediateCallback] (timers.js:722:5)
From previous event:
    at property.match.forEach (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:122:40)
    at Array.forEach (<anonymous>)
    at Variables.populateProperty (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:111:41)
    at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:85:14)
    at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:75:14)
    at addContext (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:62:42)
    at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13392:38
    at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:4917:15
    at baseForOwn (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:3002:24)
    at Function.mapValues (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13391:7)
    at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:73:37)
    at addContext (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:62:42)
    at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13392:38
    at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:4917:15
    at baseForOwn (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:3002:24)
    at Function.mapValues (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13391:7)
    at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:73:37)
    at addContext (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:62:42)
    at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13392:38
    at /Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:4917:15
    at baseForOwn (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:3002:24)
    at Function.mapValues (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/node_modules/lodash/lodash.js:13391:7)
    at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:73:37)
    at Variables.populateObject (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:79:5)
    at Variables.populateService (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/classes/Variables.js:47:17)
    at Serverless.run (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/lib/Serverless.js:89:27)
    at serverless.init.then (/Users/vadorequest/.nvm/versions/node/v8.9.4/lib/node_modules/serverless/bin/serverless:42:50)
    at <anonymous>
 
  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:                     darwin
     Node Version:           8.9.4
     Serverless Version:     1.26.0

Similar or dependent issues:

Additional Data

  • Operating System: MacOS X

Activity

  1. Vadorequest commented on Feb 15, 2018

    @Vadorequest
    ContributorAuthor

    Additional information: This seems to be related to the env and particular self:service mostly, for instance:

    • ✅ Resource: "arn:aws:dynamodb:${self:provider.region}:035907498810:table/simulator-feedback-${self:provider.stage}"
    • ❗️ Resource: "arn:aws:dynamodb:${self:provider.region}:035907498810:table/${self:service.name}-${self:provider.stage}"
    • ❗️ Resource: "arn:aws:dynamodb:${self:provider.region}:035907498810:table/${env:SERVICE_NAME}-${self:provider.stage}"

    When using self:provider, it seems to work, but when using env: or self:service.name, it just fails. Maybe it's not possible to actually get the value from self:service? But I was following this official example so I assume it's possible but broken https://github.com/serverless/examples/blob/master/aws-node-rest-api-with-dynamodb/serverless.yml

    I debug using the sls print command.

  2. chrisoverzero commented on Feb 15, 2018

    @chrisoverzero
    Contributor

    I've found the "name" value available at ${self:service}. I agree that it's odd that it doesn't match the shape of the YAML, but it matches the shape of the value of the "service" field in ".serverless/serverless-state.json", so I suspect that's the input being used.

    I'd guess that this is for backwards compatibility reasons? The presence of "serviceObject" with the field "name" seems like evidence of that.

  3. Vadorequest commented on Feb 16, 2018

    @Vadorequest
    ContributorAuthor

    Actually, I tried again and you're right. Both SERVICE_NAME: ${self:service} and SERVICE_NAME: ${self:service.name} work.

    And both fail when used inside a string:

    • "${self:service}-${self:provider.stage}"
    • "${self:service.name}-${self:provider.stage}"

    Also, when defining an SERVICE_NAME environment variable, those fail as well:

    • "${env:SERVICE_NAME}-${self:provider.stage}"
    • "${self:provider.environment.SERVICE_NAME}-${self:provider.stage}"

    Failures seems to be related to the fact it's mixed up with string.

  4. Vadorequest commented on Feb 20, 2018

    @Vadorequest
    ContributorAuthor

    Could this get labeled/assigned? If you need more information, please let me know.

  5. HyperBrain commented on Feb 20, 2018

    @HyperBrain
    Contributor

    Can you try the same with the current master? I ask because #4713 just was merged, which cleans up variabe dependencies and also respects the use variables for region etc.

  6. Vadorequest commented on Feb 20, 2018

    @Vadorequest
    ContributorAuthor

    Yup, I can. I guess I just have to change my package.json to link to the github master branch instead of the npm package to do so?

  7. HyperBrain commented on Feb 20, 2018

    @HyperBrain
    Contributor

    Yes "serverless": "github:serverless/serverless#master"

  8. Vadorequest commented on Feb 20, 2018

    @Vadorequest
    ContributorAuthor

    Sumary

    The PR seem to have fixed one use case when using "${self:service}-${self:provider.stage}" (see Scenario 1). Possible backward breaking change added (See Scenario 2).


    Running tests with the following setup:

    sls: refers to my globally npm installed package 1.26.6
    ./node_modules/serverless/bin/serverless: refers to my locally installed package, based on master branch at 39caddc

    Scenario 1

    serverless.yml file:

    service:
      name: simulator-feedback
    custom:
      test: "${self:service}-${self:provider.stage}"
    

    Test with sls 1.26.6 => Failure (expected)

    (studylink-dev) ➜  simulator-feedback git:(master) ✗ sls print                                     
    Serverless: Invoke print
     
      Serverless Error ---------------------------------------
     
      Trying to populate non string value into a string for variable ${self:service}. Please make sure the value of the property is a string.
     
      Stack Trace --------------------------------------------
     
    ServerlessError: Trying to populate non string value into a string for variable ${self:service}. Please make sure the value of the property is a string.
        at Variables.populateVariable (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:156:13)
        at pendingMatches.push.pendingMatch.then.matchedValue (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:124:21)
    From previous event:
        at property.match.forEach (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:122:40)
        at Array.forEach (native)
        at Variables.populateProperty (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:111:41)
        at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:85:14)
        at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:75:14)
        at addContext (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:62:42)
        at /Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13392:38
        at /Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:4917:15
        at baseForOwn (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:3002:24)
        at Function.mapValues (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13391:7)
        at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:73:37)
        at addContext (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:62:42)
        at /Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13392:38
        at /Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:4917:15
        at baseForOwn (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:3002:24)
        at Function.mapValues (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/node_modules/lodash/lodash.js:13391:7)
        at forEachLeaf (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:73:37)
        at Variables.populateObject (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/Variables.js:79:5)
        at getServerlessConfigFile.then (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/plugins/print/print.js:40:42)
    From previous event:
        at Print.print (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/plugins/print/print.js:31:8)
    From previous event:
        at Object.print:print [as hook] (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/plugins/print/print.js:22:10)
        at BbPromise.reduce (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/PluginManager.js:372:55)
    From previous event:
        at PluginManager.invoke (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/PluginManager.js:372:22)
        at PluginManager.run (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/classes/PluginManager.js:403:17)
        at variables.populateService.then (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/Serverless.js:102: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 (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/lib/Serverless.js:89:74)
        at serverless.init.then (/Users/vadorequest/.nvm/versions/node/v6.10.3/lib/node_modules/serverless/bin/serverless:42: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:                     darwin
         Node Version:           6.10.3
         Serverless Version:     1.26.0
    

    Test with sls on master branch (success => 💯 )

    ./node_modules/serverless/bin/serverless print
    custom:
      test: simulator-feedback-development
    
    

    Scenario 2

    service:
      name: simulator-feedback
    custom:
      test: "${self:service.name}-${self:provider.stage}"
    

    N.B: added "self:service**.name**" compared to Scenario 1

    Test with sls (Failure, expected)

    Same output as Scenario 1 sls

    Test with sls on master branch (failure => 👎 )

      A valid service attribute to satisfy the declaration 'self:service.name' could not be found.
     
     
      Serverless Error ---------------------------------------
     
      Trying to populate non string value into a string for variable ${self:service.name}. Please make sure the value of the property is a string.
    
    

    It looks like ${self:service.name} is invalid, when ${self:service} is valid. This may lead to breaking changes.

    Scenario 3

    I tried adding test2: "${env:SERVICE_NAME}-${self:provider.stage}" in custom and provider, both failed with the following message:

     Serverless Warning --------------------------------------
     
      A valid environment variable to satisfy the declaration 'env:SERVICE_NAME' could not be found.
     
     
      Serverless Error ---------------------------------------
     
      Trying to populate non string value into a string for variable ${env:SERVICE_NAME}. Please make sure the value of the property is a string.
     
      Stack Trace --------------------------------------------
     
    ServerlessError: Trying to populate non string value into a string for variable ${env:SERVICE_NAME}. Please make sure the value of the property is a string.
        at Variables.populateVariable (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:361:13)
        at Variables.renderMatches (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:302:21)
        at BbPromise.all.then.results (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:322:29)
    From previous event:
        at Variables.populateValue (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:322:8)
        at _.map.variable (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:206:24)
        at arrayMap (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/lodash/lodash.js:631:23)
        at Function.map (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/lodash/lodash.js:9546:14)
        at Variables.populateVariables (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:205:14)
        at Variables.populateObject (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:231:30)
        at prepopulateService.then (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:117:24)
        at runCallback (timers.js:672:20)
        at tryOnImmediate (timers.js:645:5)
        at processImmediate [as _immediateCallback] (timers.js:617:5)
    From previous event:
        at Variables.populateService (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:117:8)
        at Serverless.run (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/Serverless.js:89:27)
        at serverless.init.then (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/bin/serverless:42:50)
    
    

    I don't know how variables are intended to be used, but it looks like creating new variables based on existing environment variables just fail.

    Scenario 4

    I tried adding test2: "${self:custom.test}-${self:provider.stage}" in custom and provider, both failed with the following message:

     Serverless Warning --------------------------------------
     
      A valid undefined to satisfy the declaration 'deep:0' could not be found.
     
     
      Serverless Error ---------------------------------------
     
      Trying to populate non string value into a string for variable ${deep:0}. Please make sure the value of the property is a string.
     
      Stack Trace --------------------------------------------
     
    ServerlessError: Trying to populate non string value into a string for variable ${deep:0}. Please make sure the value of the property is a string.
        at Variables.populateVariable (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:361:13)
        at Variables.renderMatches (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:302:21)
        at BbPromise.all.then.results (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:322:29)
    From previous event:
        at Variables.populateValue (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:322:8)
        at _.map.variable (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:206:24)
        at arrayMap (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/lodash/lodash.js:631:23)
        at Function.map (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/lodash/lodash.js:9546:14)
        at Variables.populateVariables (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:205:14)
        at Variables.populateObject (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:231:30)
        at assignProperties.then (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:234:14)
    From previous event:
        at Variables.populateObject (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:233:8)
        at prepopulateService.then (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:117:24)
        at runCallback (timers.js:672:20)
        at tryOnImmediate (timers.js:645:5)
        at processImmediate [as _immediateCallback] (timers.js:617:5)
    From previous event:
        at Variables.populateService (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/classes/Variables.js:117:8)
        at Serverless.run (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/lib/Serverless.js:89:27)
        at serverless.init.then (/Users/vadorequest/dev/student-loan-simulator-serverless/aws/simulator-feedback/node_modules/serverless/bin/serverless:42:50)
    
    
  9. HyperBrain commented on Feb 20, 2018

    @HyperBrain
    Contributor

    @Vadorequest Wow 💯 . Thanks for the very detailed report. That's really helpful.

    @erikerikson Please have a look at the previous summary. Especially scenarios 3 and 4 are interesting, while scenario 3 might be an issue before the variable cleanup, I think scenario 4 is still something that is broken now.

  10. erikerikson commented on Feb 20, 2018

    @erikerikson
    Contributor

    Thank you very much for these cases. Will definitely dive into them and fix. There's a lot of untangling to do but we have a better position to do so from.

  11. erikerikson commented on Feb 21, 2018

    @erikerikson
    Contributor

    Please note that I have made some changes in the meantime. There was a bug I had missed.
    I believe that I've completely solved that issue. Please accept my apologies. If you would like to use the code I am testing with, you can update your package.json with (the source for #4754):

    "serverless": "github:erikerikson/serverless#4713-follow-up-fixes"

    That said, I've tested this out with the scenarios that you provided...

    Scenario 1: when I run

          it.only('should handle unquoted text after a variable listing', () => {
            service.service = {
              name: 'a-name',
            };
            service.custom = {
              test: '${self:service}-${self:provider.stage}',
            };
            const expected = {
              test: `${service.service.name}-development`,
            };
            return serverless.variables.populateObject(service.custom)
              .should.become(expected);
          });
    

    I get a failure that I expect (${self.service} is the object { name: 'a-name' }):

    ServerlessError: Trying to populate non string value into a string for variable ${self:service}. Please make sure the value of the property is a string.
        at Variables.populateVariable (lib/classes/Variables.js:370:13)
        at Variables.renderMatches (lib/classes/Variables.js:296:21)
        at __dirname.populateValue.BbPromise.all.then.results (lib/classes/Variables.js:316:29)
    From previous event:
        at Variables.populateValue (lib/classes/Variables.js:316:8)
        at __dirname.populateVariables._.map.variable (lib/classes/Variables.js:206:24)
        at arrayMap (node_modules/lodash/lodash.js:660:23)
        at Function.map (node_modules/lodash/lodash.js:9571:14)
        at Variables.populateVariables (lib/classes/Variables.js:205:14)
        at Variables.populateObject (lib/classes/Variables.js:231:30)
        at Context.it.only (lib/classes/Variables.test.js:417:37)
    

    regardless of whether the #4754 patch is applied. I am a little confused by this given your report. Am I missing something important?

    Senario 2: If I change the test slightly:

          it.only('should handle unquoted text after a variable listing', () => {
            service.provider.stage = 'development';
            service.service = {
              name: 'a-name',
            };
            service.custom = {
              test1: '${self:service.name}-${self:provider.stage}',
            };
            const expected = {
              test1: `${service.service.name}-development`,
            };
            return serverless.variables.populateObject(service.custom)
              .should.become(expected);
          });
    

    the test succeeds.

    Scenario 3: If I add the environment variable and reference it in addition:

          it.only('should handle unquoted text after a variable listing', () => {
            process.env.SERVICE_NAME = 'a-name-1'
            service.provider.stage = 'development';
            service.service = {
              name: 'a-name-2',
            };
            service.custom = {
              test1: '${self:service.name}-${self:provider.stage}',
              test2: '${env:SERVICE_NAME}-${self:provider.stage}',
            };
            const expected = {
              test1: `${service.service.name}-development`,
              test2: `${process.env.SERVICE_NAME}-development`,
            };
            return serverless.variables.populateObject(service.custom)
              .should.become(expected)
              .then(() => { delete process.env.SERVICE_NAME; });
          });
    

    The test again succeeds.

    Please let me know if what I've reported here is in line with your expectations and if not what your expectations are. I'm afraid that you were hit with a temporary state of the variables resolution. That or I am missing the full configuration required for reproduction.

    If you could provide me with a full set of materials for reproduction, it would help very much. If not, if you could guide me in reproducing this issue, I will get it resolved.

    Thank you very much!

  12. Vadorequest commented on Feb 21, 2018

    @Vadorequest
    ContributorAuthor

    @erikerikson thanks for the quick feedback.

    Scenario A

    service:
      name: simulator-feedback
    custom:
      test1: "${self:service}-${self:provider.stage}"
      test2: "${self:custom.test1}-test2"

    This works fine.


    Scenario B

    service:
      name: simulator-feedback
    custom:
      test1: "${self:service.name}-${self:provider.stage}" # added ".name"
      test2: "${self:custom.test1}-test2"

    This fails. (I still don't know if that's the desired behavior, it may be)


    Scenario C

    service:
      name: simulator-feedback
    custom:
      test1: "${self:service}-${self:provider.stage}"
      test2: "${self:custom.test1}-test2"
    provider:
      environment:
        NODE_ENV: ${self:provider.stage}
        SERVICE_NAME: ${env:NODE_ENV}

    This fails silently, SERVICE_NAME isn't printed. But there are no exception thrown.


    Scenario D

    service:
      name: simulator-feedback
    custom:
      test1: "${self:service}-${self:provider.stage}"
      test2: "${self:custom.test1}-test2"
    provider:
      environment:
        NODE_ENV: ${self:provider.stage}
        SERVICE_NAME: "${env:NODE_ENV}-whatever"

    This fails with exception :

    Serverless Warning --------------------------------------
     A valid environment variable to satisfy the declaration 'env:NODE_ENV' could not be found
     Serverless Error ---------------------------------------
     Trying to populate non string value into a string for variable ${env:NODE_ENV}. Please make sure the value of the property is a string.
    

    I think we need to clearly define what we want to achieve here. What are our specs?

    1. Should ${self:service} and ${self:service.name} behave the same? Or should me only allow ${self:service} despite the fact it's not developer friendly when using the following format?
    service:
      name: simulator-feedback
    
    1. Should we allow environment variables to define other environment variables? I know nothing of the execution workflow, but I guess that if we have some custom variables depending on environment variable and vice-versa, we're going to have a problem with circular dependencies. Maybe there is an existing workflow, but I ain't aware of none.
    2. Can env variables depend on each other? If so, does the order matters? (see Scenario D)
  13. erikerikson commented on Feb 21, 2018

    @erikerikson
    Contributor

    Oh... Scenarios C and D...

    The ${env:*} variables reference process.env, not ${self:provider.environment}

    So if you export NODE_ENV=foo (or set, if appropriate) the errors around your environment use should clear up.

    Thanks for these, I will look closer at these today.

  14. Vadorequest commented on Feb 21, 2018

    @Vadorequest
    ContributorAuthor

    Ahhhh, that explains why I couldn't make it work since the beginning :/
    Good to know. Some of my tests were biased due to this misunderstanding. I thought both ${env:NODE_ENV} and ${self:provider.environment.NODE_ENV} were equivalent.

    Maybe I missed something in the doc, or maybe the docs are missing something. :)

  15. erikerikson commented on Feb 21, 2018

    @erikerikson
    Contributor
  16. erikerikson commented on Feb 21, 2018

    @erikerikson
    Contributor

    On my latest...

    Scenario A fails with:

      Serverless Error ---------------------------------------
     
      "provider" property is missing in serverless.yml
     
      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:                     darwin
         Node Version:           6.11.0
         Serverless Version:     1.26.0
    

    If I add:

    provider:
      name: aws
    

    Then I see:

    service:
      name: simulator-feedback
    custom:
      test1: simulator-feedback-dev
      test2: simulator-feedback-dev-test2
    provider:
      name: aws
    

    Scenario B (adding the same provider config) results in:

    Serverless Warning --------------------------------------
     
      A valid service attribute to satisfy the declaration 'self:service.name' could not be found.
     
     
      Serverless Error ---------------------------------------
     
      Trying to populate non string value into a string for variable ${self:service.name}. Please make sure the value of the property is a string.
     
      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:                     darwin
         Node Version:           6.11.0
         Serverless Version:     1.26.0
    

    Printing the state of the "self"/service object results in:

    valueToPopulate: {
      "service": "simulator-feedback",
      "serviceObject": {
        "name": "simulator-feedback"
      },
      "provider": {
        "stage": "dev",
        "region": "us-east-1",
        "name": "aws",
        "versionFunctions": true,
        "remoteFunctionData": null
      },
      "custom": {
        "test1": "${self:service.name}-${self:provider.stage}",
        "test2": "${self:custom.test1}-test2"
      },
      "functions": {},
      "package": {}
    }
    deepProperties: ["service","name"]
    

    Which explains the results of scenarios A and B. It's an odd transformation that is happening during service loading. This is done here: https://github.com/serverless/serverless/blob/master/lib/classes/Service.js#L118. Unfortunately, the VM rejects the addition of the name attribute on a string due to being in "strict" mode ('use strict' is at the head of that file). You can see this by popping the following into your browser console:

    'use strict'
    s = 'foo'
    s.bar = 'biz'
    

    In chrome, I get:

    Uncaught TypeError: Cannot create property 'bar' on string 'foo'
        at <anonymous>:3:7
    

    Are there any other issues that I am missing besides the cognitive dissonance of the ${self:service} vs ${self:service.name} issue?

    I suppose that we could go through the effort to remove 'use strict' and deal with the resulting consequences. Not sure how to prioritize this.

  17. erikerikson commented on Feb 21, 2018

    @erikerikson
    Contributor

    @HyperBrain have an opinion about my last sentence? Or what the right path forward is for this issue, pending @Vadorequest's confirmation that the issue is understood?

  18. HyperBrain commented on Feb 21, 2018

    @HyperBrain
    Contributor

    service is special. Since we introduced the dual nature (string vs. object) a long time ago, the correct way in Serverless and plugins is to use service.getServiceObject() which will return it as object, regardless how it is defined in your yaml.

    As the yaml definition is a raw state before the service is parsed and structured into an object by the Service class, it might be insecure to use the yaml representation here. Thus I'm not sure how we should define the expected behavior.

    Anybody tried to use ${self:service.service.name} already?

  19. HyperBrain commented on Feb 21, 2018

    @HyperBrain
    Contributor

    Regarding the use strict: The CLI is still required to be Node 4 compliant. As far as I remember, only Node 6 supports classes without specifying strict?

  20. dschep commented on Feb 21, 2018

    @dschep
    Contributor

    Here's a trick I just figured out for using ${self:service.name} using YAML references:

    service: &service
      name: hello                                                                      
    custom:
      service: *service

    Then use ${self:custom.service.name} instead.

  21. HyperBrain commented on Feb 21, 2018

    @HyperBrain
    Contributor

    @dschep That's a nice one that qualifies to be added to the docs 😄

  22. erikerikson commented on Feb 22, 2018

    @erikerikson
    Contributor

    Wow. Yeah. But I'd rather just fix it. 😄

  23. added a commit that references this issue on Feb 23, 2018
  24. erikerikson commented on Feb 23, 2018

    @erikerikson
    Contributor

    The following adds support for the expected-to-be valid references below:

     +    if (variable === 'self:service.name') {
     +      variable = 'self:service';
     +    }
     +    if (variable === 'self:provider') {
     +      variable = 'self:provider.name';
     +    }
    

    This is available on #4754 if you would like to test it out. Others, feel free to object.

  25. added this to the 1.27 milestone on Feb 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions