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(s3-assets): cannot publish a file without extension #30597

Merged
merged 8 commits into from
Oct 29, 2024

Conversation

sakurai-ryo
Copy link
Contributor

@sakurai-ryo sakurai-ryo commented Jun 20, 2024

Issue # (if applicable)

Closes #30471

Reason for this change

Publishing a file with no extension using the Asset class with BundlingOutput.SINGLE_FILE and AssetHashType.SOURCE(default), as shown below, will result in an error fail: EISDIR: illegal operation on a directory, read, and publishing will fail.

export class AssetTestStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
      super(scope, id, props);

      const asset = new s3assets.Asset(this, 'asset', {
          path: path.join(__dirname, '../assets/main'),
          bundling: {
              image: DockerImage.fromRegistry('golang:1.21'),
              entrypoint: ["bash", "-c"],
              command: ["echo 123 > /asset-output/main"], // a file without extension
              outputType: BundlingOutput.SINGLE_FILE,
          },
      });
      new CfnOutput(this, 'AssetHash', { value: asset.assetHash });
  }
}

This is because the path in *.asset.json is different from the actual file path.
The *.asset.json expects the file to be in asset.bead5b2c0d128650228f146d2326d5f3cbfb36738a9383fc6a09b1e9278803f0, but when I check the cdk.out directory, I see that asset.bead5b2c0d128650228f146d2326d5f3cbfb36738a9383fc6a09b1e9278803f0 is a directory, not a file.

{
  "version": "36.0.0",
  "files": {
    "bead5b2c0d128650228f146d2326d5f3cbfb36738a9383fc6a09b1e9278803f0": {
      "source": {
        "path": "asset.bead5b2c0d128650228f146d2326d5f3cbfb36738a9383fc6a09b1e9278803f0",
        "packaging": "file"
      },
      "destinations": {
        "current_account-us-east-1": {
          "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-us-east-1",
          "objectKey": "bead5b2c0d128650228f146d2326d5f3cbfb36738a9383fc6a09b1e9278803f0.bead5b2c0d128650228f146d2326d5f3cbfb36738a9383fc6a09b1e9278803f0",
          "region": "us-east-1",
          "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-us-east-1"
        }
      }
    }
}
cdk.out
├── SampleStack.assets.json
├── SampleStack.template.json
├── asset.bead5b2c0d128650228f146d2326d5f3cbfb36738a9383fc6a09b1e9278803f0
│   └── main
├── cdk.out
├── manifest.json
└── tree.json

If I change it to a file with an extension, as shown below, I see that the file with the extension is staged under cdk.out dir, and the asset is published successfully.

export class AssetTestStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
      super(scope, id, props);

      const asset = new s3assets.Asset(this, 'asset', {
          path: path.join(__dirname, '../assets/main'),
          bundling: {
              image: DockerImage.fromRegistry('golang:1.21'),
              entrypoint: ["bash", "-c"],
              command: ["echo 123 > /asset-output/main.bin"], // a file with an extension
              outputType: BundlingOutput.SINGLE_FILE,
          },
      });
      new CfnOutput(this, 'AssetHash', { value: asset.assetHash });
  }
}
cdk.out
├── SampleStack.assets.json
├── SampleStack.template.json
├── asset.dc5ce447844d7490834e46df016edc7f671b4fae19ab55b6c78973dcb5af98f8
│   └── main.bin
├── asset.dc5ce447844d7490834e46df016edc7f671b4fae19ab55b6c78973dcb5af98f8.bin # !! staged file here !!
├── cdk.out
├── manifest.json
└── tree.json
{
  "version": "36.0.0",
  "files": {
    "dc5ce447844d7490834e46df016edc7f671b4fae19ab55b6c78973dcb5af98f8": {
      "source": {
        "path": "asset.dc5ce447844d7490834e46df016edc7f671b4fae19ab55b6c78973dcb5af98f8.bin",
        "packaging": "file"
      },
      "destinations": {
        "current_account-us-east-1": {
          "bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-us-east-1",
          "objectKey": "dc5ce447844d7490834e46df016edc7f671b4fae19ab55b6c78973dcb5af98f8.bin",
          "region": "us-east-1",
          "assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-us-east-1"
        }
      }
    }
}

Files without extensions must be staged correctly in order to be published correctly.

Description of changes

The directory to write the bundling output is usually cdk.out/asset.{asset hash}.
If the extension exists, it will be renamed from cdk.out/asset.{asset hash}/{asset file name} to cdk.out/{asset hash}.{asset file extension}.

fs.renameSync(sourcePath, targetPath);

If the extension does not exist, the file name cdk.out/asset.{asset hash} (without extension) will be the same as the directory where bundling output is written.
Therefore, the file is already considered staged and will not be staged correctly.

Therefore, in such cases, I fix to change the file name by adding a suffix such as noext after the file name so that the file is correctly renamed.

Description of how you validated changes

unit tests and integ test.

Checklist


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

@aws-cdk-automation aws-cdk-automation requested a review from a team June 20, 2024 12:43
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p1 star-contributor [Pilot] contributed between 25-49 PRs to the CDK labels Jun 20, 2024
@sakurai-ryo sakurai-ryo changed the title fix(s3-assets): cannot publish files without extension fix(s3-assets): cannot publish a file without extension Jun 20, 2024
@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Jun 20, 2024
This was referenced Jun 24, 2024
@sakurai-ryo
Copy link
Contributor Author

@Mergifyio update

Copy link
Contributor

mergify bot commented Jul 30, 2024

update

❌ Mergify doesn't have permission to update

For security reasons, Mergify can't update this pull request. Try updating locally.
GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/github-merit-badger.yml without workflows permission

Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

Thanks for the deep dive!

I wonder if we can simplify this a bit, but apart from that looks good!

private renderStagedPath(sourcePath: string, targetPath: string): string {
// Add a suffix to the asset file name
// because when a file without extension is specified, the source directory name is the same as the staged asset file name.
if (this.hashType === AssetHashType.SOURCE && path.dirname(sourcePath) === targetPath) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this even on the hashType? It seems that if dirname(sourcePath) === targetPath we always have a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed the conditional to this.hashType !== AssetHashType.OUTPUT and added the reason to the comment.
9f5adb5

// Add a suffix to the asset file name
// because when a file without extension is specified, the source directory name is the same as the staged asset file name.
if (this.hashType === AssetHashType.SOURCE && path.dirname(sourcePath) === targetPath) {
targetPath = targetPath + 'noext';
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
targetPath = targetPath + 'noext';
targetPath = targetPath + '_noext';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed suffix to _noext.
ecee169

Comment on lines 27 to 29
// If the fileName begins with 'asset.', remove it here
// because when a file without extension is added to the manifest, the path.extension() will return an file hash.
// ex) path.extension('asset.dc5ce447844') => 'dc5ce447844'
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment seems to not match what the code is doing? Or does it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Deleted comment.
I forgot to delete it.
69889ec

@rix0rrr rix0rrr self-assigned this Sep 4, 2024
@iliapolo iliapolo added @aws-cdk/core Related to core CDK functionality @aws-cdk/aws-s3-assets labels Oct 1, 2024
@sakurai-ryo
Copy link
Contributor Author

@rix0rrr
Thanks for your review, and sorry for the late response.
I've addressed all of your comments.

rix0rrr
rix0rrr previously approved these changes Oct 25, 2024
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

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

<3

Copy link
Contributor

mergify bot commented Oct 25, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Oct 25, 2024
Copy link
Contributor

mergify bot commented Oct 25, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot dismissed rix0rrr’s stale review October 28, 2024 10:41

Pull request has been modified.

@aws-cdk-automation aws-cdk-automation added the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Oct 28, 2024
Copy link
Contributor

mergify bot commented Oct 29, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation aws-cdk-automation removed the pr/needs-maintainer-review This PR needs a review from a Core Team Member label Oct 29, 2024
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@mergify mergify bot merged commit ccab485 into aws:main Oct 29, 2024
15 checks passed
Copy link
Contributor

mergify bot commented Oct 29, 2024

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-s3-assets @aws-cdk/core Related to core CDK functionality bug This issue is a bug. effort/medium Medium work item – several days of effort p1 star-contributor [Pilot] contributed between 25-49 PRs to the CDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws-s3-assets: Asset publish fails on files with no extension if outputType is BundlingOutput.SINGLE_FILE
4 participants