cpx2 icon indicating copy to clipboard operation
cpx2 copied to clipboard

RangeError: path should be a `path.relative()`d string, but got ....

Open anodynos opened this issue 2 years ago • 8 comments

Hey and thanks for this great lib! It's the best copy out there! But I faced a daring bug:

(Also related to same error message https://github.com/bcomnes/cpx2/issues/44)

My cmd is

╭─anodynos@Giga /mnt/projects/__fake/macos/newin
╰─$ cpx "/mnt/projects/devzen-tools/packages/newin/**" "./" --ignore "node_modules/,dist/,build/" --verbose --update  

so I want to copy from a deep path, into my CWD.

It fails with a a gazzillion of these - it enumerates all my files and node_modules (despite being ignored). For each file (mine & node_modules) it throws:

RangeError: path should be a `path.relative()`d string, but got "../../../devzen-tools/packages/newin/node_modules/expect/build/types.js"
    at throwError (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/ignore/index.js:379:9)
    at checkPath (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/ignore/index.js:398:12)
    at Ignore._test (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/ignore/index.js:519:5)
    at Ignore.ignores (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/ignore/index.js:564:17)
    at Object.ignored (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/lib/utils/apply-action.js:42:41)
    at #ignored (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/glob/dist/commonjs/walker.js:54:64)
    at GlobWalker.matchFinish (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/glob/dist/commonjs/walker.js:124:26)
    at GlobWalker.match (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/glob/dist/commonjs/walker.js:148:18)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
RangeError: path should be a `path.relative()`d string, but got "../../../devzen-tools/packages/newin/node_modules/expect/build/toThrowMatchers.js"
    at throwError (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/ignore/index.js:379:9)
    at checkPath (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/ignore/index.js:398:12)
    at Ignore._test (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/ignore/index.js:519:5)
    at Ignore.ignores (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/ignore/index.js:564:17)
    at Object.ignored (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/lib/utils/apply-action.js:42:41)
    at #ignored (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/glob/dist/commonjs/walker.js:54:64)
    at GlobWalker.matchFinish (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/glob/dist/commonjs/walker.js:124:26)
    at GlobWalker.match (/home/anodynos/.asdf/installs/nodejs/20.8.0/lib/node_modules/cpx2/node_modules/glob/dist/commonjs/walker.js:148:18)
....
RangeError: path should be a `path.relative()`d string, but got "../../../../c/dev/newin/node_modules/lws-conditional-get"
...

Then it copies all the files I wanted, witch a glitch: ALWAYS, despite having --update.

Now the interesting thing is, if I reverse the paths, everything works fine:

╭─anodynos@Giga /mnt/projects/__fake/macos/newin
╰─$ cpx "./src/**/*" "/mnt/projects/devzen-tools/packages/newin/src" --ignore "node_modules/,dist/,build/" --verbose --update

Copy: ./src/**/* --> /mnt/projects/devzen-tools/packages/newin/src

Copied: src/tsconfig.json --> ../../../devzen-tools/packages/newin/src/tsconfig.json
Copied: src/tsconfig.deploy.json --> ../../../devzen-tools/packages/newin/src/tsconfig.deploy.json
Copied: src/package.json --> ../../../devzen-tools/packages/newin/src/package.json
Copied: src/package-lock.json --> ../../../devzen-tools/packages/newin/src/package-lock.json
Copied: src/nodemon.copy.json --> ../../../devzen-tools/packages/newin/src/nodemon.copy.json
....

Something's wrong with the globbing apparently, but after some digging I think it doesnt like absolute directories.

I converted to relative paths, and boom, all good.

$ cpx "./**/*" /mnt/projects/__fake/macos/newin --verbose

UPDATE: It's not relative paths either, it crashes the same with them as well.

But when I add --watch the problem goes away!

Works:

╭─anodynos@Giga /mnt/projects/__fake/macos/newin
╰─$ cpx "../../../newin/**/*" . --verbose --ignore "node_modules" --watch  

Fails:

╭─anodynos@Giga /mnt/projects/__fake/macos/newin
╰─$ cpx "../../../newin/**/*" . --verbose --ignore "node_modules"

Great irony, I wanted to do it in a sleep loop "while sleep 5 ; cpx ...." cause watch dosnt work on Network drives apparently...

Any ideas?

Thanks again!

anodynos avatar Nov 02 '23 14:11 anodynos

Probably a bug introduced after I updated the glob library, which required a few changes to how ignore strings were handled. Thank you for the example, I will get it fixed. In the meantime, feel free to run the last latest version to avoid the issue.

bcomnes avatar Nov 02 '23 16:11 bcomnes

cause watch dosn't work on Network drives apparently...

Looks like this lib uses fs.watch, with has a fairly well known set of limitations: https://nodejs.org/api/fs.html#caveats

I would be open to swapping out to https://github.com/paulmillr/chokidar and exposing a --polling flag or something which WOULD work for network file systems, but I probably won't get around to it any time soon. Interested in looking into it?

bcomnes avatar Nov 02 '23 16:11 bcomnes

Hey Bret, thanks for your response and concern.

I use chokidar as well, it's not without its caveats, but you can navigate around them. The network watch I think is tricky, and not so important. I just run cpx2 in a loop every couple of seconds, and done. Since --update works, I don't care about some extra reads in my ./src files!

I would absolutely love to work on it, your library in spec is the best out there, supporting --update & --watch & --ignore, I don't think any other does it... Unfortunately no time atm ;-(

I use the latest 6.0.1 already, do you mean the main branch directly?

Also, I noticed it doesn't copy hidden files (eg starting with .) and there's no option for it! This should be a trivial change, can you please add it?

Angelos ;-)

PS: nice stuff you got on https://bret.io/, but I think cpx2 deserves a mention too!

anodynos avatar Nov 04 '23 06:11 anodynos

I’m planning on fixing the relative path error asap. Chokidar would be nice but it might be a bit longer till I get to it. I don’t work with nfs very often.

I think the dotfile thing is a glob option that's just a matter of exposing so it should be easy.

Mostly credit goes to mysticatea who did the bulk of the work originally!

bcomnes avatar Nov 04 '23 14:11 bcomnes

Still planning on working on this.

bcomnes avatar Nov 21 '23 20:11 bcomnes

Bumping to remind myself to look at this issue soon. Please use an older version or a workaround (working out of a parent of the src directory) in the meantime.

bcomnes avatar Dec 29 '23 17:12 bcomnes

Oh no, I just hit this and now my release is bust. I'm gonna go back to prior version but +1 on this being the best copy library out there.

thorsent avatar Feb 01 '24 21:02 thorsent

Sorry about that, I may have some time to fix here soon.

bcomnes avatar Feb 01 '24 21:02 bcomnes

@bcomnes I've hit this as well.

artsiommiksiuk avatar Sep 20 '24 12:09 artsiommiksiuk