Skip to content
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

feat(core): add include property specifying particular files for assets #33132

Open
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Jan 24, 2025

Issue # (if applicable)

Closes #26107.

Reason for this change

Currently the option for Assets etc. has the exclude only for filtering files. Also I had submitted the PR that adds include before, but closed it because the exclude can cover situations for staging particular files by using negation patterns such as * and !filenames. (see: #26107 (comment)).

But I encountered the situation that it is difficult to express our requirements with exclude only.

The example of directories and files is the following (y: required, x: not required).

- main.py          y
- dummy-1          x
- dummy-2          x
- dummy-3          x
- dummy-...        x
- subdir           
  - examples       x    
    - test.sh      x
    - test.py      x
    - README.md    x
  - a.py           y
  - b.py           y
  - binary         y
  - my.json        y

Now for the situation, we may use the following pattern with exclude only.

exclude: ['*', '!main.py', '!subdir', '!subdir/**/*', 'subdir/examples/*'],

However, there are some confusing points if the double negative is desired:

  • Negation (!) and positive are mixed and difficult to understand.
  • It does not work if you change the order in which you specify them (i.e. the double negative no longer works).
    • e.g. ['*', 'subdir/examples/*', '!main.py', '!subdir', '!subdir/**/*']
    • -> It includes ‘subdir/examples/*’ that should be excluded.

In such cases, the include option makes it simple to achieve the following. (This is very easy to understand and you don't have to mind the order.)

include: ['main.py', 'subdir', 'subdir/**/*'],
exclude: ['subdir/examples/*'],

The include property is also present in other modules that filter particular files, such as cdk watch, codepipeline Git triggers and BucketDeployment for S3, so it isn't unnatural.

In particular, it should be good for developers to be able to match the filtering settings of cdk watch with hotswap and the settings of the asset files added in this PR.

Description of changes

  • Add an include parameter to interface FileOptions as well as exclude.
  • Check for matching include in copyDirectory, where the actual file search is done.
  • If the same file is specified in include and exclude, exclude takes precedence.

Describe any new or updated permissions being added

Description of how you validated changes

Both unit and integ tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@github-actions github-actions bot added p2 distinguished-contributor [Pilot] contributed 50+ PRs to the CDK labels Jan 24, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team January 24, 2025 12:21
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

comments

comments

comments

rm comments
Copy link

codecov bot commented Jan 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.27%. Comparing base (5eeee75) to head (9479e5e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #33132      +/-   ##
==========================================
+ Coverage   82.20%   82.27%   +0.06%     
==========================================
  Files         119      120       +1     
  Lines        6862     6889      +27     
  Branches     1158     1167       +9     
==========================================
+ Hits         5641     5668      +27     
  Misses       1118     1118              
  Partials      103      103              
Flag Coverage Δ
suite.unit 82.27% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk ∅ <ø> (∅)
packages/aws-cdk-lib/core 82.27% <100.00%> (+0.06%) ⬆️

@aws-cdk-automation
Copy link
Collaborator

This PR has been in the CHANGES REQUESTED state for 3 weeks, and looks abandoned. Note that PRs with failing linting check or builds are not reviewed, please ensure your build is passing

To prevent automatic closure:

  • Resume work on the PR
  • OR request an exemption by adding a comment containing 'Exemption Request' with justification e.x "Exemption Request: "
  • OR request clarification by adding a comment containing 'Clarification Request' with a question e.x "Clarification Request: "

This PR will automatically close in 7 days if no action is taken.

@kaizencc
Copy link
Contributor

hi @go-to-k, what's the status on this PR? i don't want to rush you but the automation may eventually close your PR as abandoned...

@go-to-k
Copy link
Contributor Author

go-to-k commented Feb 18, 2025

hi @kaizencc . Thanks for letting me know!
I've been working on other PRs and put this one off. I'm going to do it later so I'll update the branch so it doesn't get closed.

@aws-cdk-automation aws-cdk-automation dismissed their stale review February 18, 2025 00:41

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@github-actions github-actions bot added effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. labels Feb 18, 2025
@go-to-k go-to-k changed the title feat(core): add include property specifying particular files for assets feat(core): add include property specifying particular files for assets Feb 18, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review February 18, 2025 08:05

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@aws-cdk-automation aws-cdk-automation dismissed their stale review February 23, 2025 09:14

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

new test
// eslint-disable-next-line @typescript-eslint/no-require-imports
const minimatch = require('minimatch');

export function matchIncludePatterns(patterns: string[], absoluteRootPath: string, absoluteFilePath: string): boolean {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +89 to +91
* If matching the symlink but not its target, it is included (i.e. the `include` patterns match
* the symlink path itself, regardless of its target). This is the same behavior even if `follow`
* is not `SymlinkFollowMode.NEVER`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To match the current exclude behavior

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 9479e5e
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@go-to-k go-to-k marked this pull request as ready for review February 23, 2025 13:45
@go-to-k go-to-k requested a review from a team as a code owner February 23, 2025 13:45
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Feb 23, 2025
Comment on lines +39 to +42
const destDirPath = path.dirname(destFilePath);
if (!fs.existsSync(destDirPath)) {
fs.mkdirSync(destDirPath, { recursive: true });
}
Copy link
Contributor Author

@go-to-k go-to-k Feb 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This directory creation code was added because:

  1. Previously, parent directories were already created by the isDirectory() processing before copying files/symlinks (*1).
  2. With the include option to copy only specific files, directories that don't match the include patterns are not created (*2). Without this behavior, we would end up creating unnecessary directories along the traversal path when we finally reach files that aren't included. As a result, we need to explicitly create parent directories for included files/symlinks, particularly to handle cases where those parent directories aren't included.

*1

    if (stat && stat.isDirectory()) {
      fs.mkdirSync(destFilePath);
      copyDirectory(sourceFilePath, destFilePath, options, rootDir);
      stat = undefined;
    }

*2

    if (stat && stat.isDirectory()) {
      if (included) {
        fs.mkdirSync(destFilePath, { recursive: true });
      }
      copyDirectory(sourceFilePath, destFilePath, options, rootDir);
      stat = undefined;

@go-to-k
Copy link
Contributor Author

go-to-k commented Feb 23, 2025

@kaizencc

I've completed the necessary implementation, so I'm ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
distinguished-contributor [Pilot] contributed 50+ PRs to the CDK effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

core: support specifying included files for Code.from_asset
3 participants