sindresorhus/globby

Path *** is not in cwd ***

fjc0k opened this issue · 12 comments

fjc0k commented

Just running this code:

const paths = await globby(
 '../src/*.ts',
  {
    dot: true,
    onlyFiles: false,
    gitignore: true,
    cwd: __dirname,
    absolute: true,
  },
)

Then you will get an error:

Path *** is not in cwd ***

But if you set gitignore to false, all works well.

After a bit of investigation, most likely it is caused by this PR: #120

I'm seeing this error too, when trying to use an glob containing an absolute os.tmpdir() path with { gitignore: true }.

Absolute temp dir paths are very useful in tests.

#137 fixes at least some of the scenarios where this error is being thrown, though it isn't clear if it is all of them.

@fjc0k Can you try out https://github.com/sindresorhus/globby/releases/tag/v10.0.2 and let us know if that fixes your issue?

fjc0k commented

@sindresorhus No, always the same error for each version:

10.0.0, 10.0.1, 10.0.2, 11.0.0:

image

I'm also seeing a version of this issue. In my case, it seems to be caused by my being on Windows, so the absolute path being checked and the cwd are not in the same format. That means using startsWith() to check the two paths doesn't work (https://github.com/sindresorhus/globby/blob/master/gitignore.js#L48) (and I would expect would cause other errors).

The error I'm getting:

Path C:/Users/Nicholas/Documents/projects/nitpik/cli/nitpik.config.js is not in cwd C:\Users\Nichol
as\Documents\projects\nitpik\cli

I opened a PR to at least solve the issue in Windows I was seeing:
#143

I'm not sure if it helps with other use cases, too.

I'm seeing this error too, when trying to use an glob containing an absolute os.tmpdir() path with { gitignore: true }.

Absolute temp dir paths are very useful in tests.

@jaydenseric did you ever get around this error? (I know I'm asking a whole year late :P)

@navoneel07 I can't remember anything about this problem, sorry!

I just encountered this error on @yarnpkg/doctor. It seems like this error is still happening in v11.0.1.

It could be a solution.

// gitignore.js#48
const ensureAbsolutePathForCwd = (cwd, p) => {
+	p = path.normalize(p);
	cwd = slash(cwd);
	if (path.isAbsolute(p)) {
		if (p.startsWith(cwd)) {
			return p;
		}

		throw new Error(`Path ${p} is not in cwd ${cwd}`);
	}

	return path.join(cwd, p);
};

@sindresorhus Hi!
I haven't tested but it seems this issue might be simply solved by @CaoMeiYouRen's solution.
Would you fix this and publish a new version?
Thank you very much.

P.S. I am using ^11 as I cannot depend on a pure ESM module, yet. I hope you publish a bugfix version for 11 as well. Maintaining the last CJS version and the latest ESM version simultaneously would be great for people who really cannot move on to ESM right now for a while.

ignore bring the old(v4) behavior back by adding allowRelativePaths options, maybe we should consider use that. kaelzhang/node-ignore@052a722