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

fix(assets): add missing SAM asset metadata information #17591

Merged
merged 5 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/@aws-cdk/aws-s3-assets/lib/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,29 @@ export class Asset extends CoreConstruct implements cdk.IAsset {

public readonly assetHash: string;

/**
* The original Asset Path before it got staged.
*
* If asset staging is disabled, this will be same value as assetPath.
* If asset staging is enabled, it will be the Asset original path before staging.
*/
private readonly originalAssetPath: string;

/**
* Indicates if this asset got bundled before staged, or not.
*/
private readonly isBundled: boolean;

constructor(scope: Construct, id: string, props: AssetProps) {
super(scope, id);

this.originalAssetPath = path.resolve(props.path);
this.isBundled = props.bundling != null;

// stage the asset source (conditionally).
const staging = new cdk.AssetStaging(this, 'Stage', {
...props,
sourcePath: path.resolve(props.path),
sourcePath: this.originalAssetPath,
follow: props.followSymlinks ?? toSymlinkFollow(props.follow),
assetHash: props.assetHash ?? props.sourceHash,
});
Expand Down Expand Up @@ -191,6 +207,8 @@ export class Asset extends CoreConstruct implements cdk.IAsset {
// points to a local path in order to enable local invocation of this function.
resource.cfnOptions.metadata = resource.cfnOptions.metadata || { };
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY] = this.assetPath;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_ORIGINAL_PATH_KEY] = this.originalAssetPath;
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we injecting absolute paths into templates? Doesn't that break build determinism across machines?

See https://github.com/cdklabs/construct-hub/pull/648/files#r764446322 (copy @Chriscbr).

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can't afford for this to be an absolute path because it means that CDK output will differ between systems (e.g. where the project directory is, etc).

Copy link
Contributor

Choose a reason for hiding this comment

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

Tracking the fix here: #17706

resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_IS_BUNDLED_KEY] = this.isBundled;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY] = resourceProperty;
}

Expand Down
8 changes: 8 additions & 0 deletions packages/@aws-cdk/aws-s3-assets/test/asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ test('addResourceMetadata can be used to add CFN metadata to resources', () => {
expect(stack).toHaveResource('My::Resource::Type', {
Metadata: {
'aws:asset:path': 'asset.6b84b87243a4a01c592d78e1fd3855c4bfef39328cd0a450cc97e81717fea2a2',
'aws:asset:original-path': location,
'aws:asset:is-bundled': false,
'aws:asset:property': 'PropName',
},
}, ResourcePart.CompleteDefinition);
Expand All @@ -222,6 +224,8 @@ test('asset metadata is only emitted if ASSET_RESOURCE_METADATA_ENABLED_CONTEXT
expect(stack).not.toHaveResource('My::Resource::Type', {
Metadata: {
'aws:asset:path': SAMPLE_ASSET_DIR,
'aws:asset:original-path': SAMPLE_ASSET_DIR,
'aws:asset:is-bundled': false,
'aws:asset:property': 'PropName',
},
}, ResourcePart.CompleteDefinition);
Expand Down Expand Up @@ -351,6 +355,8 @@ describe('staging', () => {
const template = SynthUtils.synthesize(stack).template;
expect(template.Resources.MyResource.Metadata).toEqual({
'aws:asset:path': 'asset.6b84b87243a4a01c592d78e1fd3855c4bfef39328cd0a450cc97e81717fea2a2',
'aws:asset:original-path': SAMPLE_ASSET_DIR,
'aws:asset:is-bundled': false,
'aws:asset:property': 'PropName',
});
});
Expand All @@ -377,6 +383,8 @@ describe('staging', () => {
const template = SynthUtils.synthesize(stack).template;
expect(template.Resources.MyResource.Metadata).toEqual({
'aws:asset:path': SAMPLE_ASSET_DIR,
'aws:asset:original-path': SAMPLE_ASSET_DIR,
'aws:asset:is-bundled': false,
'aws:asset:property': 'PropName',
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/cx-api/lib/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const ASSET_RESOURCE_METADATA_DOCKERFILE_PATH_KEY = 'aws:asset:dockerfile
export const ASSET_RESOURCE_METADATA_DOCKER_BUILD_ARGS_KEY = 'aws:asset:docker-build-args';
export const ASSET_RESOURCE_METADATA_DOCKER_BUILD_TARGET_KEY = 'aws:asset:docker-build-target';
export const ASSET_RESOURCE_METADATA_PROPERTY_KEY = 'aws:asset:property';
export const ASSET_RESOURCE_METADATA_IS_BUNDLED_KEY = 'aws:asset:is-bundled';
export const ASSET_RESOURCE_METADATA_ORIGINAL_PATH_KEY = 'aws:asset:original-path';

/**
* Separator string that separates the prefix separator from the object key separator.
Expand Down