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

chore: remove dependency on realpath-native #9952

Merged
merged 7 commits into from May 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
- `[*]` [**BREAKING**] TypeScript definitions requires a minimum of TypeScript v3.8 ([#9823](https://github.com/facebook/jest/pull/9823))
- `[*]` [**BREAKING**] Drop support for Node 8 ([#9423](https://github.com/facebook/jest/pull/9423))
- `[*]` Upgrade to chalk@4 ([#9752](https://github.com/facebook/jest/pull/9752))
- `[*]` Remove usage of `realpath-native`
Copy link
Member Author

Choose a reason for hiding this comment

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

Should link the pr

- `[jest-runtime]` [**BREAKING**] Remove long-deprecated `require.requireActual` and `require.requireMock` methods ([#9854](https://github.com/facebook/jest/pull/9854))
- `[expect, jest-mock, pretty-format]` [**BREAKING**] Remove `build-es5` from package ([#9945](https://github.com/facebook/jest/pull/9945))
- `[jest-haste-map]` [**BREAKING**] removed `providesModuleNodeModules` ([#8535](https://github.com/facebook/jest/pull/8535))
Expand Down
4 changes: 2 additions & 2 deletions e2e/__tests__/hasteMapSize.test.ts
Expand Up @@ -7,11 +7,11 @@

import {tmpdir} from 'os';
import * as path from 'path';
import {readFileSync} from 'graceful-fs';
import HasteMap = require('jest-haste-map');
import {sync as realpath} from 'realpath-native';
import {cleanup, writeFiles} from '../Utils';

const DIR = path.resolve(realpath(tmpdir()), 'haste_map_size');
const DIR = path.resolve(readFileSync.native(tmpdir()), 'haste_map_size');

beforeEach(() => {
cleanup(DIR);
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -66,7 +66,6 @@
"prettier": "^2.0.1",
"progress": "^2.0.0",
"promise": "^8.0.2",
"realpath-native": "^2.0.0",
"resolve": "^1.15.0",
"rimraf": "^3.0.0",
"semver": "^6.3.0",
Expand Down
1 change: 0 additions & 1 deletion packages/jest-cli/package.json
Expand Up @@ -17,7 +17,6 @@
"jest-util": "^26.0.0-alpha.0",
"jest-validate": "^26.0.0-alpha.0",
"prompts": "^2.0.1",
"realpath-native": "^2.0.0",
"yargs": "^15.3.1"
},
"devDependencies": {
Expand Down
14 changes: 12 additions & 2 deletions packages/jest-cli/src/cli/index.ts
Expand Up @@ -15,7 +15,7 @@ import {getVersion, runCLI} from '@jest/core';
import chalk = require('chalk');
import exit = require('exit');
import yargs = require('yargs');
import {sync as realpath} from 'realpath-native';
import {realpathSync} from 'graceful-fs';
import init from '../init';
import * as args from './args';

Expand Down Expand Up @@ -97,7 +97,17 @@ const getProjectListFromCLIArgs = (

if (!projects.length && process.platform === 'win32') {
try {
projects.push(realpath(process.cwd()));
let path = process.cwd();

try {
path = realpathSync.native(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

projects.push(path);
} catch (err) {
// do nothing, just catch error
// process.binding('fs').realpath can throw, e.g. on mapped drives
Expand Down
15 changes: 11 additions & 4 deletions packages/jest-cli/src/init/index.ts
Expand Up @@ -9,7 +9,6 @@ import * as path from 'path';
import * as fs from 'graceful-fs';
import chalk = require('chalk');
import prompts = require('prompts');
import {sync as realpath} from 'realpath-native';
import {constants} from 'jest-config';
import defaultQuestions, {testScriptQuestion} from './questions';
import {MalformedPackageJsonError, NotFoundPackageJsonError} from './errors';
Expand All @@ -34,9 +33,17 @@ type PromptsResults = {

const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext;

export default async (
rootDir: string = realpath(process.cwd()),
): Promise<void> => {
let cwd = process.cwd();

try {
cwd = fs.realpathSync.native(cwd);
} catch (error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we use tryRealpath instead?

Copy link
Collaborator

Choose a reason for hiding this comment

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

or create a second helper just for jest-cli, since the other one is in jest-config

if (error.code !== 'ENOENT') {
throw error;
}
}

export default async (rootDir: string = cwd): Promise<void> => {
// prerequisite checks
const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON);

Expand Down
3 changes: 1 addition & 2 deletions packages/jest-config/package.json
Expand Up @@ -27,8 +27,7 @@
"jest-util": "^26.0.0-alpha.0",
"jest-validate": "^26.0.0-alpha.0",
"micromatch": "^4.0.2",
"pretty-format": "^26.0.0-alpha.0",
"realpath-native": "^2.0.0"
"pretty-format": "^26.0.0-alpha.0"
},
"devDependencies": {
"@types/babel__core": "^7.0.4",
Expand Down
8 changes: 5 additions & 3 deletions packages/jest-config/src/getCacheDirectory.ts
Expand Up @@ -7,11 +7,13 @@

import * as path from 'path';
import {tmpdir} from 'os';
import {sync as realpath} from 'realpath-native';
import type {Config} from '@jest/types';
import {tryRealpath} from './utils';

const getCacheDirectory = () => {
const tmpdirPath = path.join(tryRealpath(tmpdir()), 'jest');

const getCacheDirectory: () => Config.Path = () => {
const {getuid} = process;
const tmpdirPath = path.join(realpath(tmpdir()), 'jest');
if (getuid == null) {
return tmpdirPath;
} else {
Expand Down
5 changes: 2 additions & 3 deletions packages/jest-config/src/index.ts
Expand Up @@ -9,8 +9,7 @@ import * as path from 'path';
import * as fs from 'graceful-fs';
import type {Config} from '@jest/types';
import chalk = require('chalk');
import {sync as realpath} from 'realpath-native';
import {isJSONString, replaceRootDirInPath} from './utils';
import {isJSONString, replaceRootDirInPath, tryRealpath} from './utils';
import normalize from './normalize';
import resolveConfigPath from './resolveConfigPath';
import readConfigFileAndSetRootDir from './readConfigFileAndSetRootDir';
Expand Down Expand Up @@ -295,7 +294,7 @@ export async function readConfigs(
if (projects.length > 0) {
const projectIsCwd =
process.platform === 'win32'
? projects[0] === realpath(process.cwd())
? projects[0] === tryRealpath(process.cwd())
: projects[0] === process.cwd();

const parsedConfigs = await Promise.all(
Expand Down
6 changes: 3 additions & 3 deletions packages/jest-config/src/normalize.ts
Expand Up @@ -14,7 +14,6 @@ import {ValidationError, validate} from 'jest-validate';
import {clearLine, replacePathSepForGlob} from 'jest-util';
import chalk = require('chalk');
import micromatch = require('micromatch');
import {sync as realpath} from 'realpath-native';
import Resolver = require('jest-resolve');
import {replacePathSepForRegex} from 'jest-regex-util';
import merge = require('deepmerge');
Expand All @@ -31,6 +30,7 @@ import {
getWatchPlugin,
replaceRootDirInPath,
resolve,
tryRealpath,
} from './utils';
import {DEFAULT_JS_PATTERN, DEFAULT_REPORTER_LABEL} from './constants';
import {validateReporters} from './ReporterValidationErrors';
Expand Down Expand Up @@ -391,7 +391,7 @@ const normalizeRootDir = (

try {
// try to resolve windows short paths, ignoring errors (permission errors, mostly)
options.rootDir = realpath(options.rootDir);
options.rootDir = tryRealpath(options.rootDir);
} catch (e) {
// ignored
}
Expand Down Expand Up @@ -955,7 +955,7 @@ export default function normalize(

try {
// try to resolve windows short paths, ignoring errors (permission errors, mostly)
newOptions.cwd = realpath(process.cwd());
newOptions.cwd = tryRealpath(process.cwd());
} catch (e) {
// ignored
}
Expand Down
13 changes: 13 additions & 0 deletions packages/jest-config/src/utils.ts
Expand Up @@ -10,6 +10,7 @@ import type {Config} from '@jest/types';
import {ValidationError} from 'jest-validate';
import Resolver = require('jest-resolve');
import chalk = require('chalk');
import {realpathSync} from 'graceful-fs';

type ResolveOptions = {
rootDir: Config.Path;
Expand Down Expand Up @@ -248,3 +249,15 @@ export const getSequencer = (
prefix: 'jest-sequencer-',
rootDir,
});

export function tryRealpath(path: Config.Path): Config.Path {
try {
path = realpathSync.native(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

return path;
}
1 change: 0 additions & 1 deletion packages/jest-core/package.json
Expand Up @@ -29,7 +29,6 @@
"jest-watcher": "^26.0.0-alpha.0",
"micromatch": "^4.0.2",
"p-each-series": "^2.1.0",
"realpath-native": "^2.0.0",
"rimraf": "^3.0.0",
"slash": "^3.0.0",
"strip-ansi": "^6.0.0"
Expand Down
16 changes: 14 additions & 2 deletions packages/jest-core/src/runJest.ts
Expand Up @@ -7,7 +7,6 @@

import * as path from 'path';
import chalk = require('chalk');
import {sync as realpath} from 'realpath-native';
import {CustomConsole} from '@jest/console';
import {interopRequireDefault} from 'jest-util';
import exit = require('exit');
Expand All @@ -23,6 +22,7 @@ import {
} from '@jest/test-result';
import type TestSequencer from '@jest/test-sequencer';
import type {ChangedFiles, ChangedFilesPromise} from 'jest-changed-files';
import {realpathSync} from 'graceful-fs';
import getNoTestsFoundMessage from './getNoTestsFoundMessage';
import runGlobalHook from './runGlobalHook';
import SearchSource from './SearchSource';
Expand All @@ -32,6 +32,18 @@ import collectNodeHandles from './collectHandles';
import type TestWatcher from './TestWatcher';
import type {Filter, TestRunData} from './types';

function tryRealpath(path: Config.Path): Config.Path {
try {
path = realpathSync.native(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

return path;
}

const getTestPaths = async (
globalConfig: Config.GlobalConfig,
source: SearchSource,
Expand Down Expand Up @@ -100,7 +112,7 @@ const processResults = (
}
if (isJSON) {
if (outputFile) {
const cwd = realpath(process.cwd());
const cwd = tryRealpath(process.cwd());
const filePath = path.resolve(cwd, outputFile);

fs.writeFileSync(filePath, JSON.stringify(formatTestResults(runResults)));
Expand Down
1 change: 0 additions & 1 deletion packages/jest-resolve/package.json
Expand Up @@ -15,7 +15,6 @@
"graceful-fs": "^4.2.4",
"jest-pnp-resolver": "^1.2.1",
"read-pkg-up": "^7.0.1",
"realpath-native": "^2.0.0",
"resolve": "^1.17.0",
"slash": "^3.0.0"
},
Expand Down
7 changes: 5 additions & 2 deletions packages/jest-resolve/src/__tests__/resolve.test.ts
Expand Up @@ -234,8 +234,11 @@ describe('Resolver.getModulePaths() -> nodeModulesPaths()', () => {
// pathstrings instead of actually trying to access the physical directory.
// This test suite won't work otherwise, since we cannot make assumptions
// about the test environment when it comes to absolute paths.
jest.doMock('realpath-native', () => ({
sync: (dirInput: string) => dirInput,
jest.doMock('graceful-fs', () => ({
...jest.requireActual('graceful-fs'),
realPathSync: {
native: (dirInput: string) => dirInput,
},
}));
});

Expand Down
25 changes: 13 additions & 12 deletions packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -7,10 +7,21 @@

import * as fs from 'graceful-fs';
import {sync as resolveSync} from 'resolve';
import {sync as realpath} from 'realpath-native';
import pnpResolver from 'jest-pnp-resolver';
import type {Config} from '@jest/types';

function tryRealpath(path: Config.Path): Config.Path {
try {
path = fs.realpathSync.native(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

return path;
}

type ResolverOptions = {
basedir: Config.Path;
browser?: boolean;
Expand Down Expand Up @@ -95,17 +106,7 @@ function realpathCached(path: Config.Path): Config.Path {
return result;
}

try {
result = realpath(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

if (!result) {
result = path;
}
result = tryRealpath(path);

checkedRealpathPaths.set(path, result);

Expand Down
19 changes: 15 additions & 4 deletions packages/jest-resolve/src/index.ts
Expand Up @@ -8,14 +8,26 @@
import * as path from 'path';
import type {Config} from '@jest/types';
import type {ModuleMap} from 'jest-haste-map';
import {sync as realpath} from 'realpath-native';
import chalk = require('chalk');
import {realpathSync} from 'graceful-fs';
import nodeModulesPaths from './nodeModulesPaths';
import isBuiltinModule from './isBuiltinModule';
import defaultResolver, {clearDefaultResolverCache} from './defaultResolver';
import type {ResolverConfig} from './types';
import ModuleNotFoundError from './ModuleNotFoundError';
import shouldLoadAsEsm, {clearCachedLookups} from './shouldLoadAsEsm';
import chalk = require('chalk');

function tryRealpath(path: Config.Path): Config.Path {
try {
path = realpathSync.native(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

return path;
}

type FindNodeModuleConfig = {
basedir: Config.Path;
Expand All @@ -41,8 +53,7 @@ namespace Resolver {
const NATIVE_PLATFORM = 'native';

// We might be inside a symlink.
const cwd = process.cwd();
const resolvedCwd = realpath(cwd) || cwd;
const resolvedCwd = tryRealpath(process.cwd());
const {NODE_PATH} = process.env;
const nodePaths = NODE_PATH
? NODE_PATH.split(path.delimiter)
Expand Down
16 changes: 14 additions & 2 deletions packages/jest-resolve/src/nodeModulesPaths.ts
Expand Up @@ -9,13 +9,25 @@

import * as path from 'path';
import type {Config} from '@jest/types';
import {sync as realpath} from 'realpath-native';
import {realpathSync} from 'graceful-fs';

type NodeModulesPathsOptions = {
moduleDirectory?: Array<string>;
paths?: Array<Config.Path>;
};

function tryRealpath(path: Config.Path): Config.Path {
try {
path = realpathSync.native(path);
} catch (error) {
if (error.code !== 'ENOENT') {
throw error;
}
}

return path;
}

export default function nodeModulesPaths(
basedir: Config.Path,
options: NodeModulesPathsOptions,
Expand All @@ -40,7 +52,7 @@ export default function nodeModulesPaths(
// traverses parents of the physical path, not the symlinked path
let physicalBasedir;
try {
physicalBasedir = realpath(basedirAbs);
physicalBasedir = tryRealpath(basedirAbs);
} catch (err) {
// realpath can throw, e.g. on mapped drives
physicalBasedir = basedirAbs;
Expand Down
1 change: 0 additions & 1 deletion packages/jest-runtime/package.json
Expand Up @@ -32,7 +32,6 @@
"jest-snapshot": "^26.0.0-alpha.0",
"jest-util": "^26.0.0-alpha.0",
"jest-validate": "^26.0.0-alpha.0",
"realpath-native": "^2.0.0",
"slash": "^3.0.0",
"strip-bom": "^4.0.0",
"yargs": "^15.3.1"
Expand Down