bcomnes / cpx2

Maintenance fork of cpx. A cli tool to watch and copy file globs. 🤞hoping to get this work merged one day.
MIT License
46 stars 3 forks source link

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

Closed anodynos closed 1 month ago

anodynos commented 1 year ago

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!

bcomnes commented 1 year ago

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 commented 1 year ago

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?

anodynos commented 1 year ago

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!

bcomnes commented 1 year ago

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 commented 12 months ago

Still planning on working on this.

bcomnes commented 10 months ago

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.

thorsent commented 9 months ago

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.

bcomnes commented 9 months ago

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

artsiommiksiuk commented 1 month ago

@bcomnes I've hit this as well.

mcollina commented 1 month ago

@bcomnes got hurt by this as well :(

bcomnes commented 1 month ago

Arg sorry folks. I'll get a hot fix out. I can just bypass the ignore glob error for the outer relative path case.

bcomnes commented 1 month ago

Taking a stab at this right now. Sorry for taking so long.

bcomnes commented 1 month ago

If you are still with me, give cpx2@7.0.2 or v8.0.0 (updated deps) a try.

mcollina commented 1 month ago

works here