-
Notifications
You must be signed in to change notification settings - Fork 159
fix: Enable --manifest flag for esbuild and npm build #513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,13 @@ | |
|
|
||
| import json | ||
| import logging | ||
| import os | ||
| from pathlib import Path | ||
|
|
||
| from aws_lambda_builders.actions import ( | ||
| CleanUpAction, | ||
| CopySourceAction, | ||
| LinkSinglePathAction, | ||
| LinkSourceAction, | ||
| MoveDependenciesAction, | ||
| ) | ||
|
|
@@ -53,6 +55,10 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim | |
| self.osutils = osutils or OSUtils() | ||
| self.subprocess_npm = SubprocessNpm(self.osutils) | ||
| self.subprocess_esbuild = self._get_esbuild_subprocess() | ||
| self.manifest_dir = self.osutils.dirname(self.manifest_path) | ||
|
|
||
| is_building_in_source = self.build_dir == self.source_dir | ||
| is_external_manifest = self.manifest_dir != self.source_dir | ||
|
|
||
| bundler_config = self.get_build_properties() | ||
|
|
||
|
|
@@ -80,22 +86,45 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim | |
| # if we're building in the source directory, we don't have to copy the source code | ||
| self.actions = ( | ||
| [] | ||
| if self.build_dir == self.source_dir | ||
| if is_building_in_source | ||
| else [CopySourceAction(source_dir=self.source_dir, dest_dir=self.build_dir, excludes=self.EXCLUDED_FILES)] | ||
| ) | ||
|
|
||
| if is_external_manifest and not is_building_in_source: | ||
| # copy the manifest file (package.json) to the build directory in case if the manifest file is not in the | ||
| # same directory as the source code, and customer is not building in source. | ||
| self.actions.append( | ||
| CopySourceAction(source_dir=self.manifest_dir, dest_dir=self.build_dir, excludes=self.EXCLUDED_FILES) | ||
| ) | ||
|
|
||
| if self.download_dependencies: | ||
| self.actions.append( | ||
| NodejsNpmWorkflow.get_install_action( | ||
| source_dir=source_dir, | ||
| install_dir=self.build_dir, | ||
| # run npm install in the directory where the manifest (package.json) exists if customer is building | ||
| # in source, and manifest directory is different from source. | ||
| # This will let NPM find the local dependencies that are defined in the manifest file (they are | ||
| # usually defined as relative to the manifest location, and that is why we run `npm install` in the | ||
| # manifest directory instead of source directory). | ||
| # If customer is not building in source, so it is ok to run `npm install` in the build | ||
| # directory (the artifacts directory in this case), as the local dependencies are not supported. | ||
| install_dir=self.manifest_dir if is_building_in_source and is_external_manifest else self.build_dir, | ||
| subprocess_npm=self.subprocess_npm, | ||
| osutils=self.osutils, | ||
| build_options=self.options, | ||
| install_links=self.build_dir == self.source_dir, | ||
| install_links=is_building_in_source, | ||
| ) | ||
| ) | ||
|
|
||
| if is_building_in_source and is_external_manifest: | ||
| # Since we run `npm install` in the manifest directory, so we need to link the node_modules directory in | ||
| # the source directory. | ||
| source_dependencies_path = os.path.join(self.source_dir, "node_modules") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! This is a clever solution. |
||
| manifest_dependencies_path = os.path.join(self.manifest_dir, "node_modules") | ||
| self.actions.append( | ||
| LinkSinglePathAction(source=manifest_dependencies_path, dest=source_dependencies_path) | ||
| ) | ||
|
|
||
| bundle_action = EsbuildBundleAction( | ||
| working_directory=self.build_dir, | ||
| output_directory=self.artifacts_dir, | ||
|
|
@@ -109,7 +138,7 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim | |
| # If there's no dependencies_dir, just bundle and we're done. | ||
| # Same thing if we're building in the source directory (since the dependencies persist in | ||
| # the source directory, we don't want to move them or symlink them back to the source) | ||
| if not self.dependencies_dir or self.build_dir == self.source_dir: | ||
| if not self.dependencies_dir or is_building_in_source: | ||
| self.actions.append(bundle_action) | ||
| return | ||
|
|
||
|
|
@@ -118,7 +147,7 @@ def __init__(self, source_dir, artifacts_dir, scratch_dir, manifest_path, runtim | |
| self.actions += [ | ||
| bundle_action, | ||
| CleanUpAction(self.dependencies_dir), | ||
| MoveDependenciesAction(self.source_dir, self.scratch_dir, self.dependencies_dir), | ||
| MoveDependenciesAction(self.source_dir, self.scratch_dir, self.dependencies_dir, self.manifest_dir), | ||
| ] | ||
| else: | ||
| # if we're reusing dependencies, then we need to symlink them before bundling | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little confused by this. I thought that today this feature worked for regular Node.js builds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if it was a bug or a regression, but I verified that it is not working for Node. Build will succeed, but actually if you try to run
sam local invokeit will fail, as the source code does not exist in the artifacts directoryThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but if I used the new lambda builders library:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so I also added some integration test cases to SAM CLI to cover this use case to avoid regressions in the future if it was a regression.