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

Prevent a ENOENT crash by checking for file existence in jest-message-util #5405

Merged
merged 4 commits into from
Jan 28, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

### Fixes

* `[jest-message-util]` Prevent an `ENOENT` crash when the test file contained a malformed source-map.
Copy link
Member

Choose a reason for hiding this comment

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

please add a link to this PR (and run yarn lint:md to format the file)

* `[jest]` Add `import-local` to `jest` package.
([#5353](https://github.com/facebook/jest/pull/5353))
* `[expect]` Support class instances in `.toHaveProperty()` matcher.
Expand Down
16 changes: 16 additions & 0 deletions integration-tests/__tests__/bad_source_map.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
'use strict';

const runJest = require('../runJest');

test('suite with test cases that contain malformed sourcemaps', () => {
const result = runJest('bad-source-map');
expect(result.stderr).not.toMatch('ENOENT');
});
15 changes: 15 additions & 0 deletions integration-tests/bad-source-map/__tests__/bad-source-map.js

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.

5 changes: 5 additions & 0 deletions integration-tests/bad-source-map/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"jest": {
"testEnvironment": "node"
}
}
2 changes: 1 addition & 1 deletion packages/jest-message-util/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const formatStackTrace = (
if (topFrame) {
const filename = topFrame.file;

if (path.isAbsolute(filename)) {
if (path.isAbsolute(filename) && fs.existsSync(filename)) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better to just catch the readFileSync to save an io operation?

Copy link
Member

Choose a reason for hiding this comment

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

What we should do is check the hastefs cache and just read from that

Copy link
Member

Choose a reason for hiding this comment

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

See #5087 (comment). Fixing that (ignoring files not in the cache) is the real fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@SimenB I don't know how HasteFS works or how to read its cache, however I suspect there could be cases where the file would not appear in it either. One of those cases could be running vm.runInThisContext or vm.runInNewContext from within a test, and the contents of the file is evaled (rather than being read from the disk). Unless that somehow ends up in the HasteFS too? I'm only mentioning it, since it's also exactly what happens in the webpack case.

Could I try/catch the readFileSync for now, so you could ship this bugfix as soon as possible, and we can prepare the proper fix at a later time, since I don't have enough knowledge about HasteFS? Unless it's something really simple, and you could point me to the right place?

Cheers!

Copy link
Member

Choose a reason for hiding this comment

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

It's not a simple change as we have to somehow thread the cache all the way through.

Sticking the readFileSync in a try should be good enough

renderedCallsite = codeFrameColumns(
fs.readFileSync(filename, 'utf8'),
{
Expand Down