Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: getRootDir behaviour for single directory causing wrong project root #7251

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/utils/src/getRootDir.js
Expand Up @@ -4,6 +4,8 @@ import type {FilePath} from '@parcel/types';
import {isGlob} from './glob';
import path from 'path';

// Returns the common root of the given paths.
// If there is no common root, returns the current working directory.
export default function getRootDir(files: Array<FilePath>): FilePath {
let cur = null;

Expand All @@ -27,10 +29,16 @@ export default function getRootDir(files: Array<FilePath>): FilePath {
}

cur.dir = i > 1 ? curParts.slice(0, i).join(path.sep) : cur.root;
cur.name = '';
cur.base = '';
}
}

return cur ? cur.dir : process.cwd();
if (!cur) {
return process.cwd();
}

return path.join(cur.dir, cur.name);
}

// Transforms a path like `packages/*/src/index.js` to the root of the glob, `packages/`
Expand Down
19 changes: 19 additions & 0 deletions packages/core/utils/test/getRootDir.test.js
@@ -0,0 +1,19 @@
import assert from 'assert';
import getRootDir from '../src/getRootDir';
import path from 'path';

describe('getRootDir', () => {
it('Should return the common parts if provided a file list', () => {
const rootPath = process.cwd();
const fileList = [
path.join(rootPath, 'foo', 'bar'),
path.join(rootPath, 'foo', 'bar', 'baz', 'qux'),
path.join(rootPath, 'foo.js'),
];
assert.equal(getRootDir(fileList), rootPath);
});
it('Should return the passsed path if its a directory', () => {
const rootPath = process.cwd();
assert.equal(getRootDir([rootPath]), rootPath);
});
});