globby icon indicating copy to clipboard operation
globby copied to clipboard

Path *** is not in cwd ***

Open fjc0k opened this issue 6 years ago • 12 comments

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: https://github.com/sindresorhus/globby/pull/120

fjc0k avatar Jul 17 '19 05:07 fjc0k

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 avatar Jul 25 '19 02:07 jaydenseric

https://github.com/sindresorhus/globby/pull/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.

rwjblue avatar Oct 16 '19 17:10 rwjblue

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

sindresorhus avatar Jan 06 '20 08:01 sindresorhus

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

10.0.0, 10.0.1, 10.0.2, 11.0.0:

image

fjc0k avatar Jan 06 '20 13:01 fjc0k

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

nzakas avatar Mar 12 '20 20:03 nzakas

I opened a PR to at least solve the issue in Windows I was seeing: https://github.com/sindresorhus/globby/pull/143

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

nzakas avatar May 26 '20 17:05 nzakas

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 avatar Jun 24 '20 10:06 navoneel07

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

jaydenseric avatar Jun 24 '20 10:06 jaydenseric

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

BasixKOR avatar Nov 03 '20 15:11 BasixKOR

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);
};

CaoMeiYouRen avatar Feb 05 '21 13:02 CaoMeiYouRen

@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.

jjangga0214 avatar Jul 27 '22 05:07 jjangga0214

ignore bring the old(v4) behavior back by adding allowRelativePaths options, maybe we should consider use that. https://github.com/kaelzhang/node-ignore/commit/052a7229e39ab113cbb6b796748597632aca0f2d

fisker avatar Nov 17 '22 05:11 fisker