Repository: serverless/serverless (JavaScript)
Date: 2020-02-25 09:05:08+00:00
Task type: Bug Fix

Issue:
deploy --package with layers issue
# Bug Report

## Description

1. What did you do?

I create a package in my CI using `sls package --package /tmp/mypackage` and this successfully packages everything including my layers. 

I then store the output of the publish as a build artefact and my CI gets the artefact  to deploy. 

I deploy using `sls deploy --package /tmp/mypackage` pointing to the artefact folder that was created during packaging. 

1. What happened?


I get an error saying File does not exist `/original/source/location/.serverless/rpackages.zip` 

1. What should've happened?

Should look for the layer zip in the package folder provided not in the original source location, i don't have the original source, just the output of the packaging. The output has everything it needs but the deploy seems to look for the layer zips in the original source location rather than the package location

1. What's the content of your `serverless.yml` file?

```
service: cj-rfunctions

provider:
  name: aws

# you can overwrite defaults here
  stage: dev
  region: eu-west-1
  
package:
  include:
    - src/**
  exclude:
    - serverless.yml
    - package.json
    - yarn.lock
    - node_modules/**
    - bootstrap.sh

layers:
  rpackages:
    path: rpackages
functions:
  plackettLuce:
    handler: plackettLuce.handler
    description: plackettLuce R
    memorySize: 3008
    timeout: 60
    runtime: provided
    layers: 
      - arn:aws:lambda:eu-west-1:131329294410:layer:r-runtime-3_6_0:12
      - arn:aws:lambda:eu-west-1:131329294410:layer:r-recommended-3_6_0:12
      - {Ref: RpackagesLambdaLayer}
resources:
  - Outputs:
        PlackettLuceArn:
          Value: 
            Fn::GetAtt: PlackettLuceLambdaFunction.Arn
          Export:
            Name: ${self:custom.stage}-PlackettLuceFunctionArn
plugins:


custom:
    # Our stage is based on what is passed in when running serverless
  # commands. Or fallsback to what we have set in the provider section.
  stage: ${opt:stage, self:provider.stage}
```



Issue discussion:

Solution patch:
diff --git a/lib/plugins/aws/lib/getServiceState.js b/lib/plugins/aws/lib/getServiceState.js
index fe5b95b018c..b5edce78548 100644
--- a/lib/plugins/aws/lib/getServiceState.js
+++ b/lib/plugins/aws/lib/getServiceState.js
@@ -8,7 +8,7 @@ module.exports = {
     const servicePath = this.serverless.config.servicePath;
     const packageDirName = this.options.package || '.serverless';
 
-    const stateFilePath = path.join(servicePath, packageDirName, stateFileName);
+    const stateFilePath = path.resolve(servicePath, packageDirName, stateFileName);
     return this.serverless.utils.readFileSync(stateFilePath);
   },
 };


Test patch:
diff --git a/lib/plugins/aws/lib/getServiceState.test.js b/lib/plugins/aws/lib/getServiceState.test.js
index f7540287f85..44db155ee9c 100644
--- a/lib/plugins/aws/lib/getServiceState.test.js
+++ b/lib/plugins/aws/lib/getServiceState.test.js
@@ -32,14 +32,14 @@ describe('#getServiceState()', () => {
   });
 
   it('should use the default state file path if the "package" option is not used', () => {
-    const stateFilePath = path.join('my-service', '.serverless', 'serverless-state.json');
+    const stateFilePath = path.resolve('my-service', '.serverless', 'serverless-state.json');
     awsPlugin.getServiceState();
 
     expect(readFileSyncStub).to.be.calledWithExactly(stateFilePath);
   });
 
   it('should use the argument-based state file path if the "package" option is used ', () => {
-    const stateFilePath = path.join('my-service', 'some-package-path', 'serverless-state.json');
+    const stateFilePath = path.resolve('my-service', 'some-package-path', 'serverless-state.json');
     options.package = 'some-package-path';
 
     awsPlugin.getServiceState();