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

Support for --preserve-symlinks #7364

Closed
wants to merge 9 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/jest-cli/src/cli/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,13 @@ export const options = {
'Will not fail if no tests are found (for example while using `--testPathPattern`.)',
type: 'boolean',
},
preserveSymlinks: {
default: false,
description:
'Find symlinked files and do not expand their file paths during' +
'module resolution',
type: 'boolean',
},
preset: {
description: "A preset that is used as a base for Jest's configuration.",
type: 'string',
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const groupOptions = (
modulePathIgnorePatterns: options.modulePathIgnorePatterns,
modulePaths: options.modulePaths,
name: options.name,
preserveSymlinks: options.preserveSymlinks,
prettierPath: options.prettierPath,
resetMocks: options.resetMocks,
resetModules: options.resetModules,
Expand Down
1 change: 1 addition & 0 deletions packages/jest-config/src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ export default function normalize(options: InitialOptions, argv: Argv) {
case 'onlyChanged':
case 'outputFile':
case 'passWithNoTests':
case 'preserveSymlinks':
case 'replname':
case 'reporters':
case 'resetMocks':
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-haste-map/src/crawlers/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ function findNative(
roots: Array<string>,
extensions: Array<string>,
ignore: IgnoreMatcher,
preserveSymlinks: boolean,
callback: Callback,
): void {
const args = [].concat(roots);
if (preserveSymlinks) {
args.unshift('-L');
}
args.push('-type', 'f');
if (extensions.length) {
args.push('(');
Expand Down Expand Up @@ -137,6 +141,7 @@ module.exports = function nodeCrawl(
extensions,
forceNodeFilesystemAPI,
ignore,
preserveSymlinks,
rootDir,
roots,
} = options;
Expand All @@ -163,7 +168,7 @@ module.exports = function nodeCrawl(
if (forceNodeFilesystemAPI || process.platform === 'win32') {
find(roots, extensions, ignore, callback);
} else {
findNative(roots, extensions, ignore, callback);
findNative(roots, extensions, ignore, preserveSymlinks, callback);
}
});
};
10 changes: 9 additions & 1 deletion packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Options = {
mocksPattern?: string,
name: string,
platforms: Array<string>,
preserveSymlinks: boolean,
providesModuleNodeModules?: Array<string>,
resetCache?: boolean,
retainAllFiles: boolean,
Expand All @@ -86,6 +87,7 @@ type InternalOptions = {
mocksPattern: ?RegExp,
name: string,
platforms: Array<string>,
preserveSymlinks: boolean,
resetCache: ?boolean,
retainAllFiles: boolean,
rootDir: string,
Expand Down Expand Up @@ -246,12 +248,16 @@ class HasteMap extends EventEmitter {
: null,
name: options.name,
platforms: options.platforms,
preserveSymlinks: options.preserveSymlinks,
resetCache: options.resetCache,
retainAllFiles: options.retainAllFiles,
rootDir: options.rootDir,
roots: Array.from(new Set(options.roots)),
throwOnModuleCollision: !!options.throwOnModuleCollision,
useWatchman: options.useWatchman == null ? true : options.useWatchman,
// Watchman can not handle symlinks: https://github.com/facebook/watchman/issues/105
useWatchman: options.preserveSymlinks == true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should throw a hard error somewhere if both Watchman and preserve symlink options are provided

? false
: options.useWatchman == null ? true : options.useWatchman,
watch: !!options.watch,
};
this._console = options.console || global.console;
Expand Down Expand Up @@ -705,6 +711,7 @@ class HasteMap extends EventEmitter {
forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
ignore,
mapper: options.mapper,
preserveSymlinks: options.preserveSymlinks,
rootDir: options.rootDir,
roots: options.roots,
}).catch(e => {
Expand All @@ -726,6 +733,7 @@ class HasteMap extends EventEmitter {
extensions: options.extensions,
forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
ignore,
preserveSymlinks: options.preserveSymlinks,
rootDir: options.rootDir,
roots: options.roots,
}).catch(retry);
Expand Down
1 change: 1 addition & 0 deletions packages/jest-haste-map/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type CrawlerOptions = {|
forceNodeFilesystemAPI: boolean,
ignore: IgnoreMatcher,
mapper?: ?Mapper,
preserveSymlinks: boolean,
rootDir: string,
roots: Array<string>,
|};
Expand Down
16 changes: 9 additions & 7 deletions packages/jest-resolve/src/defaultResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ResolverOptions = {|
extensions?: Array<string>,
moduleDirectory?: Array<string>,
paths?: ?Array<Path>,
preserveSymlinks: boolean,
rootDir: ?Path,
|};

Expand All @@ -36,6 +37,7 @@ export default function defaultResolver(
extensions: options.extensions,
moduleDirectory: options.moduleDirectory,
paths: options.paths,
preserveSymlinks: options.preserveSymlinks,
rootDir: options.rootDir,
});
}
Expand All @@ -50,7 +52,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
if (REGEX_RELATIVE_IMPORT.test(target)) {
// resolve relative import
const resolveTarget = path.resolve(basedir, target);
const result = tryResolve(resolveTarget);
const result = tryResolve(resolveTarget, options.preserveSymlinks);
if (result) {
return result;
}
Expand All @@ -62,7 +64,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
});
for (let i = 0; i < dirs.length; i++) {
const resolveTarget = path.join(dirs[i], target);
const result = tryResolve(resolveTarget);
const result = tryResolve(resolveTarget, options.preserveSymlinks);
if (result) {
return result;
}
Expand All @@ -82,13 +84,13 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
/*
* contextual helper functions
*/
function tryResolve(name: Path): ?Path {
function tryResolve(name: Path, preserveSymlinks: boolean): ?Path {
const dir = path.dirname(name);
let result;
if (isDirectory(dir)) {
result = resolveAsFile(name) || resolveAsDirectory(name);
result = resolveAsFile(name) || resolveAsDirectory(name, preserveSymlinks);
}
if (result) {
if (result && !preserveSymlinks) {
// Dereference symlinks to ensure we don't create a separate
// module instance depending on how it was referenced.
result = fs.realpathSync(result);
Expand All @@ -111,7 +113,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {
return undefined;
}

function resolveAsDirectory(name: Path): ?Path {
function resolveAsDirectory(name: Path, preserveSymlinks: boolean): ?Path {
if (!isDirectory(name)) {
return undefined;
}
Expand All @@ -125,7 +127,7 @@ function resolveSync(target: Path, options: ResolverOptions): Path {

if (pkgmain && !isCurrentDirectory(pkgmain)) {
const resolveTarget = path.resolve(name, pkgmain);
const result = tryResolve(resolveTarget);
const result = tryResolve(resolveTarget, preserveSymlinks);
if (result) {
return result;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-resolve/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ResolverConfig = {|
moduleNameMapper: ?Array<ModuleNameMapperConfig>,
modulePaths: Array<Path>,
platforms?: Array<string>,
preserveSymlinks?: boolean,
resolver: ?Path,
rootDir: ?Path,
|};
Expand All @@ -38,6 +39,7 @@ type FindNodeModuleConfig = {|
extensions?: Array<string>,
moduleDirectory?: Array<string>,
paths?: Array<Path>,
preserveSymlinks?: boolean,
resolver?: ?Path,
rootDir?: ?Path,
|};
Expand Down Expand Up @@ -80,6 +82,7 @@ class Resolver {
moduleNameMapper: options.moduleNameMapper,
modulePaths: options.modulePaths,
platforms: options.platforms,
preserveSymlinks: options.preserveSymlinks,
resolver: options.resolver,
rootDir: options.rootDir,
};
Expand All @@ -106,6 +109,7 @@ class Resolver {
extensions: options.extensions,
moduleDirectory: options.moduleDirectory,
paths: paths ? (nodePaths || []).concat(paths) : nodePaths,
preserveSymlinks: options.preserveSymlinks,
rootDir: options.rootDir,
});
} catch (e) {}
Expand Down Expand Up @@ -161,6 +165,7 @@ class Resolver {
extensions,
moduleDirectory,
paths,
preserveSymlinks: this._options.preserveSymlinks,
resolver: this._options.resolver,
rootDir: this._options.rootDir,
});
Expand Down Expand Up @@ -380,6 +385,7 @@ class Resolver {
extensions,
moduleDirectory,
paths,
preserveSymlinks: this._options.preserveSymlinks,
resolver,
rootDir: this._options.rootDir,
});
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-resolve/src/nodeModulesPaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {sync as realpath} from 'realpath-native';
type NodeModulesPathsOptions = {|
moduleDirectory?: Array<string>,
paths?: ?Array<Path>,
preserveSymlinks?: boolean,
|};

export default function nodeModulesPaths(
Expand All @@ -42,7 +43,11 @@ export default function nodeModulesPaths(
// traverses parents of the physical path, not the symlinked path
let physicalBasedir;
try {
physicalBasedir = realpath(basedirAbs);
if (!options.preserveSymlinks) {
physicalBasedir = realpath(basedirAbs);
} else {
physicalBasedir = basedirAbs;
}
} catch (err) {
// realpath can throw, e.g. on mapped drives
physicalBasedir = basedirAbs;
Expand Down
1 change: 1 addition & 0 deletions packages/jest-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class Runtime {
moduleNameMapper: getModuleNameMapper(config),
modulePaths: config.modulePaths,
platforms: config.haste.platforms,
preserveSymlinks: config.preserveSymlinks,
resolver: config.resolver,
rootDir: config.rootDir,
});
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-runtime/src/script_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const projectCaches: WeakMap<ProjectConfig, ProjectCache> = new WeakMap();
// To reset the cache for specific changesets (rather than package version).
const CACHE_VERSION = '1';

const preserveSymlinks = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? Should probably have a todo above it or something :)


export default class ScriptTransformer {
static EVAL_RESULT_VARIABLE: string;
_cache: ProjectCache;
Expand Down Expand Up @@ -197,7 +199,7 @@ export default class ScriptTransformer {
}

transformSource(filepath: Path, content: string, instrument: boolean) {
const filename = this._getRealPath(filepath);
const filename = preserveSymlinks ? filepath : this._getRealPath(filepath);
const transform = this._getTransformer(filename);
const cacheFilePath = this._getFileCachePath(filename, content, instrument);
let sourceMapPath = cacheFilePath + '.map';
Expand Down
1 change: 1 addition & 0 deletions types/Argv.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type Argv = {|
notifyMode: string,
onlyChanged: boolean,
outputFile: string,
preserveSymlinks: boolean,
preset: ?string,
projects: Array<string>,
replname: ?string,
Expand Down
1 change: 1 addition & 0 deletions types/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ export type ProjectConfig = {|
modulePathIgnorePatterns: Array<string>,
modulePaths: Array<string>,
name: string,
preserveSymlinks: boolean,
prettierPath: string,
resetMocks: boolean,
resetModules: boolean,
Expand Down