aws / aws/aws-cdk

aws-cdk-lib(core): ILocalBundling is missing access to asset source path

Open
#23,439 5 comments 4 reactions 0 assignees View on GitHub
@aws-cdk/core aws-cdk-lib feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

[When a `ILocalBundling`'s `tryBundle` is called](https://github.com/aws/aws-cdk/blob/813c2f17b6c0f1056ed43a8a93f4cffbe9ae9736/packages/%40aws-cdk/core/lib/asset-staging.ts#L452) there is no way to find out the asset's source path from within the function. The `tryBundle` function only receives the output dir and the bundling options:

```js
export interface ILocalBundling {
/**
* This method is called before attempting docker bundling to allow the
* bundler to be executed locally. If the local bundler exists, and bundling
* was performed locally, return `true`. Otherwise, return `false`.
*
* @param outputDir the directory where the bundled asset should be output
* @param options bundling options for this asset
*/
tryBundle(outputDir: string, options: BundlingOptions): boolean;
}
```

I was able to work around this problem by passing the asset path to my classes constructur but I don't think that is the intended way to call it (pardon the Python - I am not very versed in TypeScript):

```python
@jsii.implements(ILocalBundling)
class MyLocalBundling:
def __init__(self, *, asset_path):
self.asset_path = asset_path

def try_bundle(self, output_dir, *, bundling_options):
# TODO: put self.asset_path into output_dir somehow
pass

class MyStack(Stack):
def add_my_asset(self):
my_asset_path = my_get_asset_path()
asset = Asset(
self,
"MyAsset",
path=my_asset_path,
bundling=BundlingOptions(
local=MyLocalBundling(asset_path=asset_path),
# other options, like docker image etc. go here
...
)
)
```

This is needlessly complicated (because I have to create a local variable for the asset path) and potentially error-prone (I might pass the wrong asset path to the bundler). If this is indeed the intended behaviour I wish this was called out in the documentation.

I suggest extending the `ILocalBundling` interface with an additional, optional method:
```js
/**
* This method is called before attempting docker bundling to allow the
* bundler to be executed locally. If the local bundler exists, and bundling
* was performed locally, return `true`. Otherwise, return `false`.
*
* @param assetPath the path where the input asset is found
* @param outputDir the directory where the bundled asset should be output
* @param options bundling options for this asset
*/
tryBundleAsset?(assetPath: string, outputDir: string, options: BundlingOptions): boolean;
```

The asset staging logic could try to call `tryBundleAsset` if it exists and if not fall back to `tryBundle`.

### Expected Behavior

The asset path should be a parameter to the `tryBundle` method of a `ILocalBundling`

### Current Behavior

An `ILocalBundling` has no way of knowing the asset path unless it's passed in the constructure or through a closure.

### Reproduction Steps

See above.

### Possible Solution

See above.

### Additional Information/Context

_No response_

### CDK CLI Version

2.55.0 (build 077d77d)

### Framework Version

_No response_

### Node.js Version

v14.21.1

### OS

Linux

### Language

Typescript, Python

### Language Version

Python 3.8

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Begin with packages/@aws-cdk/core/lib/asset-staging.ts at the linked ILocalBundling call and inspect the ILocalBundling and BundlingOptions definitions. Verify that local bundlers can receive the asset source path while existing tryBundle behavior remains compatible; completion should cover both the new path-aware call and the fallback.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python, typescript
Domain
infrastructure, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.