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

(aws-codecommit): Repository code initialization fails when empty files exist within the contents #19012

Open
codeJack opened this issue Feb 17, 2022 · 18 comments
Labels
@aws-cdk/aws-codecommit Related to AWS CodeCommit bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@codeJack
Copy link

codeJack commented Feb 17, 2022

What is the problem?

When attempting to initialize a CodeCommit repository with some code contents the stack fails with a Code supplied is not a valid .zip archive error if, among the contents, one or more empty files exist (eg. __init__.py).

This behavior applies to any of the supported Code sources : from_asset, from_directory, from_zip_file.

Reproduction Steps

# Example with from_asset
dirname = os.path.dirname(__file__)
repository_code = Asset(
    scope=self,
    id="RepositoryCodeAsset",
    exclude=[".vscode", "__pycache__"],
    path=os.path.join(dirname, "code"),
)

aws_codecommit.Repository(
    scope=self,
    id="CodeCommitRepository",
    repository_name="repository_name",
    code=aws_codecommit.Code.from_asset(asset=repository_code),
)

The code folder shall contain one or more empty files.

What did you expect to happen?

The stack should have been deployed successfully, the code commit repository "repository_name" should have been created and initiated with the contents from the code folder.

Also, the error message could have been more precise : the zip archive is perfectly valid, the issue (assuming there is one) is with its contents.

What actually happened?

cdk deploy fails with a Code supplied is not a valid .zip archive error on the CodeCommit resource.

CDK CLI Version

2.8.0 (build 8a5eb49)

Framework Version

aws-cdk-lib==2.8.0

Node.js Version

v16.10.0

OS

MacOS 11.6.3

Language

Python

Language Version

Python(3.9.10)

Other information

No response

@codeJack codeJack added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 17, 2022
@github-actions github-actions bot added the @aws-cdk/aws-codecommit Related to AWS CodeCommit label Feb 17, 2022
@skinny85
Copy link
Contributor

Hey @codeJack,

thanks a lot for opening the issue. Confirming I was able to reproduce it. Fascinating! I'm curious whether this is limited to CodeCommit, or is this some bigger issue in how we handle Assets in the CDK.

@peterwoodworth peterwoodworth added p1 and removed needs-triage This issue or PR still needs to be triaged. labels Feb 18, 2022
@skinny85
Copy link
Contributor

Interestingly, I just tried it with a Lambda function whose code includes an empty file, and everything worked without a problem!

I wonder if this is some error in how the CodeCommit API handles ZIPs that include empty files...?

@skinny85
Copy link
Contributor

This seems related: jupyterlab/jupyterlab-git#286

@skinny85
Copy link
Contributor

Hmm. I just tried with a manually-created ZIP file that contains just an empty file (not a CDK asset), and it was created successfully. So it does seem like this is something weird that the CDK Assets do.

@jmejco
Copy link

jmejco commented Mar 17, 2022

repository_code = s3_assets.Asset(
    scope=self,
    id="RepositoryCodeAsset",
    exclude=[".vscode", "__pycache__"],
    path=os.path.join(dirname, "code"),
    bundling=BundlingOptions(
        image=DockerImage.from_registry(
            image="public.ecr.aws/docker/library/alpine:latest"
        ),
        command=[
            "sh",
            "-c",
            """
                apk update && apk add zip
                zip -r -j /asset-output/code.zip /asset-input/*
                """,
        ],
        user="root",
    ),
)

It seems to work fine when using BundlingOptions to create the zip manually.

@skinny85
Copy link
Contributor

What the hell is this ZIP sorcery that is making this fail 😄

@skinny85
Copy link
Contributor

skinny85 commented Mar 29, 2022

Here's the CDK code creating the archive:

export function zipDirectory(directory: string, outputFile: string): Promise<void> {
return new Promise(async (ok, fail) => {
// The below options are needed to support following symlinks when building zip files:
// - nodir: This will prevent symlinks themselves from being copied into the zip.
// - follow: This will follow symlinks and copy the files within.
const globOptions = {
dot: true,
nodir: true,
follow: true,
cwd: directory,
};
const files = glob.sync('**', globOptions); // The output here is already sorted
const output = createWriteStream(outputFile);
const archive = archiver('zip');
archive.on('warning', fail);
archive.on('error', fail);
// archive has been finalized and the output file descriptor has closed, resolve promise
// this has to be done before calling `finalize` since the events may fire immediately after.
// see https://www.npmjs.com/package/archiver
output.once('close', ok);
archive.pipe(output);
// Append files serially to ensure file order
for (const file of files) {
const fullPath = path.resolve(directory, file);
const [data, stat] = await Promise.all([fs.readFile(fullPath), fs.stat(fullPath)]);
archive.append(data, {
name: file,
date: new Date('1980-01-01T00:00:00.000Z'), // reset dates to get the same hash for the same content
mode: stat.mode,
});
}
await archive.finalize();
});
}

I don't see anything suspicious there...

@sawalanipawan
Copy link

Got same error when the code contents had empty __init__.py file.

After running cdk deploy command, CDK first creates the zip file and uploads it to the S3 bucket and then initiates the stack creation.

I tried to reference the ZIP created by CDK in below CFN template and tried deploying the stack using this template:

Resources:
  Repository:
    Type: AWS::CodeCommit::Repository
    Properties:
      RepositoryName: MyRepositoryName
      Code:
        BranchName: main
        S3:
          Bucket: my-cdk-bucket
          Key: 'assets/code-zipped-by-cdk.zip'

The stack creation failed with same Code supplied is not a valid .zip archive error.

Then, I tried to create the ZIP manually and added that zip to a S3 bucket. Then I created a CFN stack using below template:

Resources:
  Repository:
    Type: AWS::CodeCommit::Repository
    Properties:
      RepositoryName: MyRepositoryName
      Code:
        BranchName: main
        S3:
          Bucket: mybucket
          Key: 'mymanuallyzippedcode.zip'

The stack was created successfully.

Now, this means that there is an issue in CDK during creation of ZIP file.

@skinny85 skinny85 added the effort/medium Medium work item – several days of effort label May 5, 2022
@skinny85 skinny85 removed their assignment May 5, 2022
@abdulahad95
Copy link

Got the same error, was rather confusing to see a "Code supplied is not a valid .zip archive" when the issue was just an empty file in the directory. If at least the error message could be changed that would be helpful

@Snipervuk
Copy link

Legend mate

@mi-laf
Copy link

mi-laf commented Dec 2, 2022

Just got the same error. It took me a while to find the reason, also because the .zip archive uploaded to S3 is actually valid and correctly contains the empty file.

@sdpoueme
Copy link

Still not working as of today.

@kaiwensun
Copy link

It's been a year since the issue is opened. Still not working.

@kaiwensun
Copy link

kaiwensun commented Feb 9, 2023

I deleted the .git folder then the stack creation succeeded. I only tested once, so not sure if the .git folder is always the cause. I am using Java Code.fromDirectory

@lemked
Copy link

lemked commented Mar 27, 2023

Encountered the same when trying to create a CodeCommit repository from a small directory with some source code.

    const repo = new codecommit.Repository(this, 'Repository', {
      repositoryName: 'sample-app-repo',
      description: 'Repository for sample app.', // optional property
      code: codecommit.Code.fromDirectory(path.join(__dirname, '../../sample-app/'), 'main')
    });

Using a zip file instead worked fine.

    const repo = new codecommit.Repository(this, 'Repository', {
      repositoryName: 'sample-app-repo',
      description: 'Repository for sample app.', // optional property
      code: codecommit.Code.fromZipFile(path.join(__dirname, '../../sample-app/deployment.zip'), 'main')
    });

@dineshmane
Copy link

Any updates on this please?

@RyuuyaS
Copy link

RyuuyaS commented Apr 3, 2024

Can comment inside init file so it is not empty anymore 😀

@davidhessler
Copy link

+1 this behavior seems to also be in Typescript

@pahud pahud added p2 and removed p1 labels Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-codecommit Related to AWS CodeCommit bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests