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

Resolve realpath in default resolver #5508

Merged
merged 27 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
04506b8
Use inputFS.realpathSync get realpath of module
dishuostec Jul 27, 2020
5b53c6f
Add test for symlink structure
dishuostec Jul 27, 2020
c7ca642
add symlink file
dishuostec Jul 27, 2020
a370cbc
dynamically create symlinks for test
dishuostec Jul 27, 2020
a1e2734
Merge branch 'v2' into resolve-realpath
mischnic Aug 23, 2020
1429c5d
Update packages/utils/node-resolver-core/src/NodeResolver.js
DeMoorJasper Aug 31, 2020
3af4f55
Update NodeResolver.js
DeMoorJasper Aug 31, 2020
9af66a4
Update packages/utils/node-resolver-core/src/NodeResolver.js
DeMoorJasper Aug 31, 2020
be69f61
Merge branch 'v2' into resolve-realpath
DeMoorJasper Aug 31, 2020
ef77778
Update packages/utils/node-resolver-core/src/NodeResolver.js
DeMoorJasper Aug 31, 2020
a6d3e1d
Merge v2 into resolve-realpath
dishuostec Sep 25, 2020
0b646bd
use realpath
dishuostec Sep 25, 2020
36f481b
Tests use overlayFS
dishuostec Sep 26, 2020
61e5432
Merge branch 'v2' into resolve-realpath
dishuostec Sep 26, 2020
f8f5378
Update packages/utils/node-resolver-core/src/NodeResolver.js
dishuostec Oct 21, 2020
f67dae0
Merge branch 'v2' into resolve-realpath
mischnic Oct 27, 2020
2b5f75c
Merge branch 'v2' into resolve-realpath
Nov 30, 2020
5042a3e
Merge branch 'v2' into resolve-realpath
DeMoorJasper Dec 17, 2020
3083b04
Merge branch 'v2' of github.com:parcel-bundler/parcel into realpath
devongovett Dec 20, 2020
8acc9b4
Merge branch 'resolve-realpath' of git://github.com/dishuostec/parcel…
devongovett Dec 20, 2020
77bfe86
Realpath at the end of resolution
devongovett Dec 20, 2020
2e3b937
Remove realpaths that are now unnecessary
devongovett Dec 20, 2020
55e9c3f
Fix sourcemaps tests
devongovett Dec 20, 2020
56d98f4
Add tests for symlink resolution
devongovett Dec 20, 2020
286d3e2
Remove realpath comment
devongovett Dec 20, 2020
0219720
Merge branch 'fs-search' of github.com:parcel-bundler/parcel into rea…
devongovett Dec 20, 2020
4f3fe28
prettier
devongovett Dec 20, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/core/core/src/requests/AssetRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ function getId(input: AssetRequestInput) {
}

async function run({input, api, options, farm}: RunInput) {
let realpath = await options.inputFS.realpath(input.filePath);
api.invalidateOnFileUpdate(realpath);
api.invalidateOnFileUpdate(input.filePath);
let start = Date.now();
let {optionsRef, ...request} = input;
let {cachePath} = nullthrows(
Expand All @@ -68,7 +67,9 @@ async function run({input, api, options, farm}: RunInput) {
// These are used to compute the cache key for assets during transformation.
request.invalidations = api.getInvalidations().filter(invalidation => {
// Filter out invalidation node for the input file itself.
return invalidation.type !== 'file' || invalidation.filePath !== realpath;
return (
invalidation.type !== 'file' || invalidation.filePath !== input.filePath
);
});

let {assets, configRequests, invalidations} = (await farm.createHandle(
Expand Down
13 changes: 4 additions & 9 deletions packages/core/core/src/summarizeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,13 @@ export default async function summarizeRequest(
fs: FileSystem,
req: {|filePath: FilePath, code?: string|},
): Promise<{|content: Blob, hash: string, size: number, isSource: boolean|}> {
let [{content, hash, size}, isSource] = await Promise.all([
summarizeDiskRequest(fs, req),
isFilePathSource(fs, req.filePath),
]);
let {content, hash, size} = await summarizeDiskRequest(fs, req);
let isSource = isFilePathSource(fs, req.filePath);
return {content, hash, size, isSource};
}

async function isFilePathSource(fs: FileSystem, filePath: FilePath) {
return (
!filePath.includes(NODE_MODULES) ||
(await fs.realpath(filePath)) !== filePath
);
function isFilePathSource(fs: FileSystem, filePath: FilePath) {
return !filePath.includes(NODE_MODULES);
}

async function summarizeDiskRequest(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import count from 'pkg';
import init from 'library';

init();
export default count();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "library",
"main": "src/index.js",
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import count from 'pkg';

export default function init() {
count();
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
packages:
# all packages in subdirs of packages/
- 'packages/*'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {answer} from 'library';

export default answer;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"private": true
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 80 additions & 1 deletion packages/core/integration-tests/test/resolver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import path from 'path';
import {bundle, run} from '@parcel/test-utils';
import {bundle, run, ncp, overlayFS, outputFS} from '@parcel/test-utils';

describe('resolver', function() {
it('should support resolving tilde in monorepo packages', async function() {
Expand Down Expand Up @@ -256,4 +256,83 @@ describe('resolver', function() {
let output = await run(b);
assert.deepEqual(output.default, {});
});

it('should support symlinked node_modules structure', async function() {
const rootDir = path.join(
__dirname,
'integration/resolve-symlinked-node_modules-structure',
);

await overlayFS.mkdirp(rootDir);
await ncp(rootDir, rootDir);

await outputFS.symlink(
path.join(
rootDir,
'node_modules/.origin/[email protected]/node_modules/library',
),
path.join(rootDir, 'node_modules/library'),
);
await outputFS.symlink(
path.join(
rootDir,
'node_modules/.origin/[email protected]/node_modules/library-dep',
),
path.join(
rootDir,
'node_modules/.origin/[email protected]/node_modules/library-dep',
),
);

let b = await bundle(
path.join(
__dirname,
'/integration/resolve-symlinked-node_modules-structure/index.js',
),
{
inputFS: overlayFS,
outputFS,
},
);

let output = await run(b);
assert.strictEqual(output.default, 42);
});

it('should support symlinked monorepos structure', async function() {
const rootDir = path.join(
__dirname,
'integration/resolve-symlinked-monorepos',
);

await overlayFS.mkdirp(rootDir);
await ncp(rootDir, rootDir);

await outputFS.symlink(
path.join(rootDir, 'packages/library'),
path.join(rootDir, 'packages/app/node_modules/library'),
);
await outputFS.symlink(
path.join(rootDir, 'node_modules/.origin/[email protected]/node_modules/pkg'),
path.join(rootDir, 'packages/app/node_modules/pkg'),
);
await outputFS.symlink(
path.join(rootDir, 'node_modules/.origin/[email protected]/node_modules/pkg'),
path.join(rootDir, 'packages/library/node_modules/pkg'),
);

let b = await bundle(
path.join(
__dirname,
'/integration/resolve-symlinked-monorepos/packages/app/index.js',
),
{
inputFS: overlayFS,
outputFS,
},
);

let output = await run(b);
assert.strictEqual(output.default, 2);
});
});
8 changes: 4 additions & 4 deletions packages/core/integration-tests/test/sourcemaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ describe('sourcemaps', function() {
assert.equal(mapData.sources.length, 2);
assert.deepEqual(mapData.sources, [
'./index.ts',
'../../../../../../node_modules/@parcel/transformer-js/src/esmodule-helpers.js',
'../../../../../transformers/js/src/esmodule-helpers.js',
]);

let input = await inputFS.readFile(
Expand Down Expand Up @@ -505,7 +505,7 @@ describe('sourcemaps', function() {
assert.deepEqual(mapData.sources, [
'./index.ts',
'./local.ts',
'../../../../../../node_modules/@parcel/transformer-js/src/esmodule-helpers.js',
'../../../../../transformers/js/src/esmodule-helpers.js',
]);

let input = await inputFS.readFile(
Expand Down Expand Up @@ -959,7 +959,7 @@ describe('sourcemaps', function() {
assert.equal(map.file, 'index.js.map');
assert.deepEqual(map.sources, [
'./index.js',
'../../../../../../node_modules/@parcel/transformer-js/src/esmodule-helpers.js',
'../../../../../transformers/js/src/esmodule-helpers.js',
]);
assert.equal(map.sourcesContent[0], sourceContent);
});
Expand Down Expand Up @@ -993,7 +993,7 @@ describe('sourcemaps', function() {
assert.equal(map.file, 'index.js.map');
assert.deepEqual(map.sources, [
'./index.js',
'../../../../../../node_modules/@parcel/transformer-js/src/esmodule-helpers.js',
'../../../../../transformers/js/src/esmodule-helpers.js',
]);
});

Expand Down
2 changes: 0 additions & 2 deletions packages/core/utils/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export function resolveConfig(
filepath: FilePath,
filenames: Array<FilePath>,
): Promise<?FilePath> {
// TODO: realpath
return Promise.resolve(
fs.findAncestorFile(filenames, path.dirname(filepath)),
);
Expand All @@ -32,7 +31,6 @@ export function resolveConfigSync(
filepath: FilePath,
filenames: Array<FilePath>,
): ?FilePath {
// TODO: realpath
return fs.findAncestorFile(filenames, path.dirname(filepath));
}

Expand Down
4 changes: 2 additions & 2 deletions packages/utils/node-resolver-core/src/NodeResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default class NodeResolver {
} else if (module.filePath) {
if (module.code != null) {
return {
filePath: module.filePath,
filePath: await this.fs.realpath(module.filePath),
code: module.code,
};
}
Expand All @@ -151,7 +151,7 @@ export default class NodeResolver {

if (resolved) {
return {
filePath: resolved.path,
filePath: await this.fs.realpath(resolved.path),
sideEffects:
resolved.pkg && !this.hasSideEffects(resolved.path, resolved.pkg)
? false
Expand Down
44 changes: 36 additions & 8 deletions packages/utils/node-resolver-core/test/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ describe('resolver', function() {
path.join(rootDir, 'packages/source-alias-glob'),
path.join(rootDir, 'node_modules/source-alias-glob'),
);
await outputFS.symlink(
path.join(rootDir, 'bar.js'),
path.join(rootDir, 'baz.js'),
);
await outputFS.symlink(
path.join(rootDir, 'nested'),
path.join(rootDir, 'symlinked-nested'),
);

resolver = new NodeResolver({
fs: overlayFS,
Expand Down Expand Up @@ -693,7 +701,7 @@ describe('resolver', function() {
parent: path.join(rootDir, 'foo.js'),
});
assert.deepEqual(resolved, {
filePath: path.join(rootDir, 'node_modules', 'source', 'source.js'),
filePath: path.join(rootDir, 'packages', 'source', 'source.js'),
sideEffects: undefined,
});
});
Expand Down Expand Up @@ -724,12 +732,7 @@ describe('resolver', function() {
parent: path.join(rootDir, 'foo.js'),
});
assert.deepEqual(resolved, {
filePath: path.join(
rootDir,
'node_modules',
'source-alias',
'source.js',
),
filePath: path.join(rootDir, 'packages', 'source-alias', 'source.js'),
sideEffects: undefined,
});
});
Expand All @@ -744,7 +747,7 @@ describe('resolver', function() {
assert.deepEqual(resolved, {
filePath: path.join(
rootDir,
'node_modules',
'packages',
'source-alias-glob',
'src',
'test.js',
Expand All @@ -754,6 +757,31 @@ describe('resolver', function() {
});
});

describe('symlinks', function() {
it('should resolve symlinked files to their realpath', async function() {
let resolved = await resolver.resolve({
env: BROWSER_ENV,
filename: './baz.js',
isURL: false,
parent: path.join(rootDir, 'foo.js'),
});
assert.equal(nullthrows(resolved).filePath, path.join(rootDir, 'bar.js'));
});

it('should resolve symlinked directories to their realpath', async function() {
let resolved = await resolver.resolve({
env: BROWSER_ENV,
filename: './symlinked-nested',
isURL: false,
parent: path.join(rootDir, 'foo.js'),
});
assert.equal(
nullthrows(resolved).filePath,
path.join(rootDir, 'nested', 'index.js'),
);
});
});

describe('error handling', function() {
it('should return diagnostics when package.module does not exist', async function() {
let result = await resolver.resolve({
Expand Down