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

asset staging: do not recurse into the output directory #3899

Open
jeshan opened this issue Sep 1, 2019 · 18 comments
Open

asset staging: do not recurse into the output directory #3899

jeshan opened this issue Sep 1, 2019 · 18 comments
Labels
@aws-cdk/assets Related to the @aws-cdk/assets package bug This issue is a bug. effort/small Small work item – less than a day of effort p2

Comments

@jeshan
Copy link

jeshan commented Sep 1, 2019

🐛 Bug Report

What is the problem?

I have hit a corner case with the code related to copy directories. It's going in recursively until Node can no longer handle such long file names.

export function copyDirectory(srcDir: string, destDir: string, options: CopyOptions = { }, rootDir?: string) {
const follow = options.follow !== undefined ? options.follow : FollowMode.EXTERNAL;
const exclude = options.exclude || [];
rootDir = rootDir || srcDir;
if (!fs.statSync(srcDir).isDirectory()) {
throw new Error(`${srcDir} is not a directory`);
}
const files = fs.readdirSync(srcDir);
for (const file of files) {

The offending snippet is when I had a docker asset declared as follows:

        new DockerImageAsset(this, 'app-image-asset', {
            directory: '.',
            repositoryName: 'my-repo/app',
        });

Running cdk deploy showed me this error:

Error: ENAMETOOLONG: name too long, mkdir 'cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.7e4b3cc320bdf668daa4c0d065bb1a9841162c955cb702f7a01d7d3d7e88dbfe/cdk.out/asset.03918990104d8973acd2ad0abe022844192fcce618cf74f7734a7077f4111d1f'
    at Object.mkdirSync (fs.js:750:3)
    at copyDirectory (/mnt/sdb/ideaProjects/my-project/node_modules/@aws-cdk/assets/lib/fs/copy.js:39:16)
    at copyDirectory (/mnt/sdb/ideaProjects/my-project/node_modules/@aws-cdk/assets/lib/fs/copy.js:40:13)
    at copyDirectory (/mnt/sdb/ideaProjects/my-project/node_modules/@aws-cdk/assets/lib/fs/copy.js:40:13)
    at copyDirectory (/mnt/sdb/ideaProjects/my-project/node_modules/@aws-cdk/assets/lib/fs/copy.js:40:13)
    at copyDirectory (/mnt/sdb/ideaProjects/my-project/node_modules/@aws-cdk/assets/lib/fs/copy.js:40:13)
    at copyDirectory (/mnt/sdb/ideaProjects/my-project/node_modules/@aws-cdk/assets/lib/fs/copy.js:40:13)
    at copyDirectory (/mnt/sdb/ideaProjects/my-project/node_modules/@aws-cdk/assets/lib/fs/copy.js:40:13)
    at copyDirectory (/mnt/sdb/ideaProjects/my-project/node_modules/@aws-cdk/assets/lib/fs/copy.js:40:13)
    at copyDirectory (/mnt/sdb/ideaProjects/my-project/node_modules/@aws-cdk/assets/lib/fs/copy.js:40:13)

(Declaring the directory as its full path did not work neither.)
This is quite a serious issue for me, personally.

A possible solution is to exclude cdk.out. I think it's reasonable to assume that cdk.out name is reserved for this project.

As a temporary workaround for me, I've edited the code so that it looks like the following:

    const exclude = options.exclude || ['cdk.out'];

Another possible solution is to avoid recursion if we've already seen the directory in question.

Apparently, it's an issue where the docker build context is the same path as the cdk project.
That said, I'm not sure if the above is enough to reproduce the issue. When I comment the code to create the docker asset, the issue disappears (probably more of a symptom rather than the cause). I've checked if symlinks are the issue (apparently, it's not)

Environment

  • CDK CLI Version: 1.6.1
  • OS: all
  • Language: all
@jeshan jeshan added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 1, 2019
@rhboyd
Copy link
Contributor

rhboyd commented Sep 1, 2019

This usually happens when you try to build an image from within a directory you want included in the image as well. It creates a recursive path that never ends.

@joezhou888
Copy link

I had the same problem.

The only way to make it work is:
Project
-App Directory
-Env Directory

@jeshan
Copy link
Author

jeshan commented Sep 2, 2019

Thanks Joe.
@rhboyd , we could make the exclude directory list an option in cdk.json. What do you think?

@SomayaB SomayaB added the @aws-cdk/core Related to core CDK functionality label Sep 3, 2019
@eladb
Copy link
Contributor

eladb commented Sep 4, 2019

This is a bug. It makes sense that when we copy assets into the output directory, we won't descend into the same directory as we copy.

@eladb eladb removed the needs-triage This issue or PR still needs to be triaged. label Sep 4, 2019
@eladb eladb changed the title cdk crashes with "name too long" with Docker Asset asset staging: do not recurse into the output directory Sep 4, 2019
@eladb eladb added the p1 label Sep 5, 2019
@eladb eladb added p2 and removed p1 labels Nov 11, 2019
@askulkarni2
Copy link

askulkarni2 commented Nov 21, 2019

Saw this issue for a similar use case while using the aws-s3-assets module. I was trying to zip up my cdk project into s3 and use it to bootstrap CodeCommit. Excluding cdk.out seems to work.

const asset = new Asset(this, 'CodeAsset', {
      path: path.join(__dirname, '../'),
      exclude: [
        'node_modules',
        '.git',
        'cdk.out'
      ]
    });

@BinaryShrub
Copy link

I added cdk* to .dockerignore to get it working:

// Create Fargate Service
const fargateService = new ecs_patterns.NetworkLoadBalancedFargateService(
  this,
  'sample-app',
  {
    cluster,
    taskImageOptions: {
      image: ecs.ContainerImage.fromAsset('./') // blows up without cdk.out in .dockerignore
    }
  }
)

@victorL91
Copy link

I bumped into this issue again. It may be stupid but I solved it by making sure that the version of aws-cdk on my machine was the same as on the package.json. I was not bootstrapping it the correct way.

@wdolek
Copy link

wdolek commented Feb 4, 2021

I added cdk* to .dockerignore to get it working:

Don't forget to check your cdk.json, it should contain:

{
  "context": {
    "@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true
  }
}

Otherwise .dockerignore is ignored 🤷

@eladb eladb removed their assignment Feb 25, 2021
@mikeshi80
Copy link

I added cdk* to .dockerignore to get it working:

Don't forget to check your cdk.json, it should contain:

{
  "context": {
    "@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true
  }
}

Otherwise .dockerignore is ignored

You saved me!!!

@bashleigh
Copy link

bashleigh commented Jul 28, 2021

I added cdk* to .dockerignore to get it working:

Don't forget to check your cdk.json, it should contain:

{
  "context": {
    "@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true
  }
}

Otherwise .dockerignore is ignored 🤷

Mine seems to ignore my .dockerignore file anyway. Could be because I've changed to context dir but I have also tried adding the .dockerignore file to the root of my context

const lith = new lambda.DockerImageFunction(this, `${name}lith`, {
      code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, '../../../../'), { // root of monolith 
        file: 'packages/deployment-service/Dockerfile', // location of Dockerfile
        ignoreMode: IgnoreMode.DOCKER,
        exclude: [
          'cdk.out',
        ],
      }),
      vpc,
    })

Tried both ~/.dockerfile and ~/packages/deployment-service/.dockerfile. Not sure what else to try!

Edit

Ok so after some investigation: .dockerignore needs to be at the root of the monorepo in my case, in other words: at ../../../../. However I still have a related issue which I'm debugging

another edit

Figured it out. Paths are specified from the asset path. So I needed to be more specific however I'd prefer it if my dockerignore file excluded the cdk dir globally as I specified but hey ho

exclude: [
  'packages/deployment-service/cdk',
],

@stevemer
Copy link

Re:
"@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,

It looks like this is no longer necessary.

jsii.errors.JSIIError: Unsupported feature flag '@aws-cdk/aws-ecr-assets:dockerIgnoreSupport'. This flag existed on CDKv1 but has been removed in CDKv2. CDK will now behave as the same as when the flag is enabled.

@rix0rrr rix0rrr added p1 and removed p2 labels Mar 16, 2022
@corymhall
Copy link
Contributor

It looks like this was fixed in #16034. Let us know if anyone is still seeing this issue.

@corymhall corymhall added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label May 25, 2022
@github-actions
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added closing-soon This issue will automatically close in 4 days unless further comments are made. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. and removed closing-soon This issue will automatically close in 4 days unless further comments are made. labels May 27, 2022
@github-actions github-actions bot closed this as completed Jun 1, 2022
phuwin added a commit to phuwin/cdk-fargate-docker-nextjs that referenced this issue Jul 1, 2022
@wallacepreston
Copy link

I'm still seeing this issue. I get this when adding the flag, alright. But I still get the ENAMETOOLONG: name too long error about the cdk.out dir

Re: "@aws-cdk/aws-ecr-assets:dockerIgnoreSupport": true,

It looks like this is no longer necessary.

jsii.errors.JSIIError: Unsupported feature flag '@aws-cdk/aws-ecr-assets:dockerIgnoreSupport'. This flag existed on CDKv1 but has been removed in CDKv2. CDK will now behave as the same as when the flag is enabled.

@abury
Copy link

abury commented Nov 30, 2023

Seeing this issue pop up again in random repos :(

Figured it out thanks to @bashleigh 's detailed comment! I ignored the entire cdk folder at the root and all was well

@kellertk kellertk removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. closed-for-staleness This issue was automatically closed because it hadn't received any attention in a while. labels Nov 30, 2023
@kellertk kellertk reopened this Nov 30, 2023
@austinschrader
Copy link

Seeing this issue pop up again in random repos :(

Figured it out thanks to @bashleigh 's detailed comment! I ignored the entire cdk folder at the root and all was well

Could you share how you ignored the entire cdk folder at the root? .dockerignore doesn't seem to work as the flag @aws-cdk/aws-ecr-assets:dockerIgnoreSupport was deprecated in cdk v2

@comcalvi comcalvi added p2 and removed p1 labels Feb 13, 2024
@github-actions github-actions bot added p1 and removed p2 labels Feb 18, 2024
Copy link

This issue has received a significant amount of attention so we are automatically upgrading its priority. A member of the community will see the re-prioritization and provide an update on the issue.

@robertIanClarkson
Copy link

I am experimenting with polylith monorepo with components, bases, projects that looks similar to this...

├── .dockerignore << add */**/cdk.out
├── bases
│   └── ts
│       └── some-base-pkg
│           └── src
├── components
│   └── ts
│       └── some-component-pkg
│           └── src
└── projects
    └── ts
        └── some-project-pkg << running cdk deploy
            └── Dockerfile
            └── cdk.json
            └── src
            └── bin
                └── cdk-app.ts << cdk code

My cdk-app.ts includes some code like this

const asset = new cdk.aws_ecr_assets.DockerImageAsset(this, "some-id", {
  directory: "/my/project/root,
  file: "projects/ts/some-project-pkg/Dockerfile"
})

what worked for me was added */**/cdk.out to my root .dockerignore.
Hope this helps someone!

@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/assets Related to the @aws-cdk/assets package bug This issue is a bug. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

No branches or pull requests