Files copied to wrong path #60

Closed
webberig opened this Issue Mar 16, 2013 · 19 comments

Comments

Projects
None yet

I find that the behavior of this plugin is very strange. I have the following task configuration in my grunt file:

        copy:
        {
            bootstrap: {
                files: [
                    {
                        src: ['components/bootstrap/img/*'],
                        dest: 'web/img/bootstrap/'
                    }
                ]
            }
        }

One would expect that components/bootstrap/img/myFile.png is copied to web/img/bootstrap/myFile.png. But it's being copied to web/img/components/bootstrap/img/myFile.png.

Is this the normal behavior, or am I doing something wrong ?

Thanks for your comment, tkellen.

I understand now this is not a bug, but at least consider this to be a usability error... My oppinion still stands that the behavior is odd. One would expect that the given example in the OP would be the same as a common file copy command:

cp components/bootstrap/img/* web/img/bootstrap

Copying files like this always copies the contents of the given folder, and does not copy the directory explicitly.

Does the cwd property work?

{expand: true, cwd: ['public/assets/'], src:["swf/**"], dest: 'public/dist/<%= pkg.version %>/'}

this matches no files, but

{expand: true, cwd: ['public/assets/swf/'], src:["**"], dest: 'public/dist/<%= pkg.version %>/'}

copies the whole of my project into the dest directory, and

{expand: true, src:["public/assets/swf/**"], dest: 'public/dist/<%= pkg.version %>/'}

copies the right files but with all their parent directories under the dest dir?

Owner

tkellen commented Mar 21, 2013

Can you post your output with --verbose for these?

Dakuan commented Apr 11, 2013

I'm pretty confused by this also...should add it was easily fixed by using cwd, but was confused by the behaviour all the same :)

Owner

tkellen commented Apr 11, 2013

Your cwd needs to be a string, not an array.

This still seems to be an issue, having a quick look into it seems to make me think 'src' should be prefixed by 'cwd' when iterating over the 'filePair' elements in the task.

grunt.file.copy is essentially being passed a file relative to cwd, when not actually trying to copy relative to cwd (thus the path prefix is missing for the source file - resulting in a ENOENT error because the path is wrong).

1 line lazy fix (src = (filePair.cwd || '') + src;) fixes this for me.

Owner

tkellen commented Apr 15, 2013

The source should not be prefixed by the cwd, cwd works as though you are copying from within that directory. If you need paths higher than the cwd included in the copy, set your cwd to that path instead.

cwd seems to be ignored?

snippit from Gruntfile.js

, copy: {
      main: {
          files: [
              { cwd: 'browser/', src: ['app.js'], dest: 'public/'}
            , { cwd: 'browser', src: ['jquery.js'], dest: 'public'}
          ]
      }
  }

Output of grunt --verbose

Running "copy:main" (copy) task
Verifying property copy.main exists in config...OK
Files: app.js -> public/
Files: jquery.js -> public/
Options: processContent=false, processContentExclude=[]
Copying app.js -> public/app.js
Reading app.js...ERROR
Warning: Unable to read "app.js" file (Error code: ENOENT). Use --force to continue.

Hmm... added in expand: true and now it works. But I'm not expanding anything.

Owner

tkellen commented Apr 18, 2013

You are expanding the wildcard in your src to create a bunch of src -> dest
pairs.
Http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically

On Apr 18, 2013, at 4:56 PM, AJ ONeal notifications@github.com wrote:

Hmm... added in expand: true and now it works. But I'm not expanding
anything.


Reply to this email directly or view it on
GitHubhttps://github.com/gruntjs/grunt-contrib-copy/issues/60#issuecomment-16613716
.

I don't have a wildcard in my src.

Owner

tkellen commented Apr 19, 2013

Ah, right you are. That's what I get for responding quickly via my phone. So, expand triggers the usage of the cwd option, otherwise it won't do anything. Eventually that won't be the case, but right now it is. If you'd like to see it fixed, please open an issue on the grunt issue tracker.

tkellen closed this Apr 19, 2013

gregwym commented Jun 19, 2013

Finally I find an explanation for expand. Properties/options like expand and rename are useful, and prob should be better documented.

+1 for totaly confused by this behavior. Why on earth wouldn't you want to emulate every other copy / cp command the world has ever seen?

Abdull commented Jan 30, 2014

To recap the answers to @webberig's initial question:
Instead of:

    copy: {
        bootstrap: {
            files: [
                {
                    src: ['components/bootstrap/img/*'],
                    dest: 'web/img/bootstrap/'
                }
            ]
        }
    }

try the following:

    copy: {
        bootstrap: {
            files: [
                {
                    expand: true,
                    cwd: 'components/bootstrap/img/'
                    src: ['*'],
                    dest: 'web/img/bootstrap/'
                }
            ]
        }
    }

With this setup, a file components/bootstrap/img/myFile.png is copied to web/img/bootstrap/myFile.png.

I also had an issue with this and adding expand:true even though I don't expand anything works.

This should be noted; using cwd requires expand to be true... it should perhaps be defaulted to true if undefined.

ptica commented Mar 18, 2015

ok

expand: true,
cwd: 'Vendor/bootstrap/fonts',
src: '*',
dest:'webroot/fonts/'

is definitely the most strange syntax for what used to be

cp Vendor/bootstrap/fonts/* webroot/fonts/

in the old days of Makefiles

faebser commented Oct 6, 2015

fonts: {
                src: 'css/fonts/**/*',
                dest: 'dist/'
            }

leadings to the following behavior:

Running "copy:fonts" (copy) task
Verifying property copy.fonts exists in config...OK
Files: css/fonts/generator_config.txt, css/fonts/icomoon.eot, css/fonts/icomoon.svg, css/fonts/icomoon.ttf, css/fonts/icomoon.woff, css/fonts/opensans-bold-webfont.eot, css/fonts/opensans-bold-webfont.ttf, css/fonts/opensans-bold-webfont.woff, css/fonts/opensans-bold-webfont.woff2, css/fonts/opensans-light-webfont.eot, css/fonts/opensans-light-webfont.ttf, css/fonts/opensans-light-webfont.woff, css/fonts/opensans-light-webfont.woff2 -> dist/
Options: encoding="utf8", processContent=false, processContentExclude=[], timestamp=false, mode=false
Copying css/fonts/generator_config.txt -> dist/css/fonts/generator_config.txt
Reading css/fonts/generator_config.txt...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/icomoon.eot -> dist/css/fonts/generator_config.txt
Reading css/fonts/icomoon.eot...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/icomoon.svg -> dist/css/fonts/generator_config.txt
Reading css/fonts/icomoon.svg...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/icomoon.ttf -> dist/css/fonts/generator_config.txt
Reading css/fonts/icomoon.ttf...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/icomoon.woff -> dist/css/fonts/generator_config.txt
Reading css/fonts/icomoon.woff...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/opensans-bold-webfont.eot -> dist/css/fonts/generator_config.txt
Reading css/fonts/opensans-bold-webfont.eot...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/opensans-bold-webfont.ttf -> dist/css/fonts/generator_config.txt
Reading css/fonts/opensans-bold-webfont.ttf...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/opensans-bold-webfont.woff -> dist/css/fonts/generator_config.txt
Reading css/fonts/opensans-bold-webfont.woff...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/opensans-bold-webfont.woff2 -> dist/css/fonts/generator_config.txt
Reading css/fonts/opensans-bold-webfont.woff2...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/opensans-light-webfont.eot -> dist/css/fonts/generator_config.txt
Reading css/fonts/opensans-light-webfont.eot...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/opensans-light-webfont.ttf -> dist/css/fonts/generator_config.txt
Reading css/fonts/opensans-light-webfont.ttf...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/opensans-light-webfont.woff -> dist/css/fonts/generator_config.txt
Reading css/fonts/opensans-light-webfont.woff...OK
Writing dist/css/fonts/generator_config.txt...OK
Copying css/fonts/opensans-light-webfont.woff2 -> dist/css/fonts/generator_config.txt
Reading css/fonts/opensans-light-webfont.woff2...OK
Writing dist/css/fonts/generator_config.txt...OK
Copied 13 files

Why does it write everything into the same file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment