grunt-contrib-copy icon indicating copy to clipboard operation
grunt-contrib-copy copied to clipboard

Copying files to a folder does not work without "cwd" option

Open theage opened this issue 11 years ago • 15 comments

The following will copy ../src/index.html to itself rather than failing or copying to ../dest/index.html as one would expect:

copy: {
    main : {
        files: [
            {
                expand: true,
                src   : '../src/index.html',
                dest  : '../dest/'
            }
        ]
    }
}

However, this works:

copy: {
    main : {
        files: [
            {
                expand: true,
                cwd   :'../src/',
                src   : 'index.html',
                dest  : '../dest/'
            }
        ]
    }
}

theage avatar Apr 16 '14 08:04 theage

what does -v show?

ctalkington avatar Apr 16 '14 16:04 ctalkington

Nothing unexpected actually. It shows the thing it does wrong correctly.

Initializing
Command-line options: --verbose

Reading "Gruntfile.js" Gruntfile...OK

Registering Gruntfile tasks.
Reading package.json...OK
Parsing package.json...OK
Initializing config...OK

Registering "grunt-contrib-copy" local Npm module tasks.
Reading C:\mydir\node_modules\grunt-contrib-copy\package.json...OK
Parsing C:\mydir\node_modules\grunt-contrib-copy\package.json...OK
Loading "copy.js" tasks...OK
+ copy
Loading "Gruntfile.js" tasks...OK
+ default

Running tasks: copy

Running "copy" task

Running "copy:main" (copy) task
Verifying property copy.main exists in config...OK
Files: ../src/index.html -> ../src/index.html
Options: encoding="utf8", processContent=false, processContentExclude=[], mode=false
Copying ../src/index.html -> ../src/index.html
Reading ../src/index.html...OK
Writing ../src/index.html...OK
Copied 1 files

Done, without errors.

theage avatar Apr 17 '14 07:04 theage

What if you remove expand:true from the first config you listed? It isn't necessary there, y'know.

jamesplease avatar Apr 17 '14 12:04 jamesplease

Well, it is necessary to have the destination path created if it is missing unless I'm mistaken.

Curiously, omitting it at least helps grunt display what it should be doing in one line.
However, this holds only for the line with Files:. It will still copy the file to itself. And ../dest/ is missing now.

Running "copy:main" (copy) task
Verifying property copy.main exists in config...OK
Files: ../src/index.html -> ../dest/
Options: encoding="utf8", processContent=false, processContentExclude=[], mode=false
Copying ../src/index.html -> ../src/index.html
Reading ../src/index.html...OK
Writing ../src/index.html...OK
Copied 1 files

theage avatar Apr 17 '14 13:04 theage

its most likely the below line that is making this act weird as copy expects expand to mean cwd passed too as its always been promoted together.

https://github.com/gruntjs/grunt-contrib-copy/blob/master/tasks/copy.js#L46

ctalkington avatar Apr 17 '14 16:04 ctalkington

Interesting find, @ctalkington! I think you're onto something. I'm a bit caught up lately but if someone makes a PR that addresses this I will review it.

jamesplease avatar Apr 17 '14 16:04 jamesplease

i bet if i trace the git history back, i prob wrote that line as compress uses it too. if only i could remember the exact use case for it.

ctalkington avatar Apr 17 '14 22:04 ctalkington

ah i so called it ;)

https://github.com/gruntjs/grunt-contrib-copy/commit/bffcef8680ead762f32b3bbd6bc0472b03f35640#diff-951068604e1c942635e6a1f96a88994dR39

ctalkington avatar Apr 17 '14 22:04 ctalkington

Any updates here?

jnsflint avatar May 02 '14 18:05 jnsflint

@jvetterick not yet, but PRs are welcome :)

jamesplease avatar May 03 '14 17:05 jamesplease

Just wanted to add a +1 as this just hit me too, and no, I'm not volunteering to write a PR. ;)

cfjedimaster avatar Aug 07 '14 14:08 cfjedimaster

+1 just got this error.

etoxin avatar Aug 27 '14 02:08 etoxin

+1 also

ghost avatar Aug 27 '14 09:08 ghost

+1, me to. Continue spending second day on solving this problem

Nodarii avatar Oct 28 '14 11:10 Nodarii

+1

YanshuoH avatar May 26 '15 12:05 YanshuoH