aws/aws-sam-cli

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

hcz1 opened this issue · 2 comments

hcz1 commented

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.

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