aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.51k stars 1.17k forks source link

Bug: `sam build` Ignores Files in dist/ Directory When Specified in .gitignore #7402

Open hcz1 opened 1 month ago

hcz1 commented 1 month ago

Title: sam build Ignores Files in dist/ Directory When Specified in .gitignore

Description

Issue Summary

When specifying the dist/ directory in the .gitignore file, the sam build process also ignores the files in this directory. This behaviour causes issues when the build output is expected to be in the dist/ directory, as the necessary files are not included in the build.

Steps to Reproduce

  1. Create a Node.js project with AWS SAM.
  2. Add a .gitignore file with the following content:
    
    dist/
  3. Configure the project to output build files to the dist/ directory.
  4. Run the sam build command.

    Expected Behaviour

    The sam build process should include the files in the dist/ directory, even if it is specified in the .gitignore file, as the .gitignore file should only affect Git operations.

Actual Behaviour

The sam build process ignores the files in the dist/ directory, causing the dist folder not to be copied to .aws-sam/build/function_name/.

Workaround

Removing the dist/ entry from the .gitignore file allows the sam build process to include the necessary files, but this is not an ideal solution as it causes the dist/ directory to be tracked by Git.

Environment

SAM CLI Version: 1.123.0 Node.js Version: v20.9.0 OS: 14.3 (23D56)

Additional Context

My build process is to run tsc && sam build && sam deploy, the lambda handler is set to dist/path_to_handler.

lucashuy commented 1 month ago

Hi, normally sam build is capable of building your Typescript project using esbuild as a build method (docs). Using tsc to perform a "preprocessing" build, then using sam build will have unintended side effects depending on the project structure.

Namely, that sam build will have it's own build process that may or may not include the dist folder. sam build will call npm pack under the hood to create a "production" ready copy of the project. npm pack will ignore any files that is listed in either .gitignore, or .npmignore, preferring to use .npmignore if both files exist. This may be the root cause of the issue you are facing.

Can you provide a sample project that utilizes your build process to help us further diagnose?

hcz1 commented 1 month ago

Thank you for getting back to me. I’m currently on holiday and will get back to this the week of 9th September.

On Wed, 28 Aug 2024 at 20:07, Lucas @.***> wrote:

Hi, normally sam build is capable of building your Typescript project using esbuild as a build methods (docs https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-build-typescript.html). Using tsc to perform a "preprocessing" build, then using sam build will have unintended side effects depending on the project structure.

Namely, that sam build will have it's own build process that may or may not include the dist folder. sam build will call npm pack under the hood to create a "production" ready copy of the project. npm pack will ignore any files that is listed in either .gitignore, or .npmignore, preferring to use .npmignore if both files exist. This may be the root cause of the issue you are facing.

Can you provide a sample project that utilizes your build process to help us further diagnose?

— Reply to this email directly, view it on GitHub https://github.com/aws/aws-sam-cli/issues/7402#issuecomment-2316066361, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADSIDVMPBHPQZH7EHK3K4YLZTYNW3AVCNFSM6AAAAABNBSVQ4KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJWGA3DMMZWGE . You are receiving this because you authored the thread.Message ID: @.***>