Closed willtrking closed 3 years ago
@willtrking Would you be willing to make a tiny reproduction repo so I can diagnose the root cause of the error? Unfortunately I'm not quite smart enough to discern what the correct fix is from the details you've provided even though you were very thorough. If you do make a reproduction I promise to investigate
Will do @mrmeku ! Thank you!
@mrmeku
Here's a reproduction repository. Currently it has a reproduction of the jest failing under test but working with run. I will update it a bit later to add in a reproduction for cypress
https://github.com/willtrking/repro-bazel-rulesnodejs-2325
Thanks again!
@willtrking I was able to get the test to pass under bazel test
by changing the deps field here to @yarn//:node_modules
What this means is that under bazel run, the sandbox was turned off which allowed the runtime to see all the node_modules it needed, but under bazel test, with sandboxing turned on, one or more required node_modules was missing.
So in short, use the :node_modules target to rule out that type of sandboxing error and if you notice a difference, that means you need to keep adding npm deps to your target until it passes.
Cheers
@mrmeku Thanks for looking into this!
However, it looks like changing the deps field to include @yarn//:node_modules
does not fix the issue for me. I also tried manually specifying every dependency in package.json in the deps list and that also did not resolve the issue
Can this issue be re-opened?
I should mention my tests are all in the reproduction repo provided
To be specific, my BUILD file with @yarn//:node_modules
looks like this
load(":jest.bzl", "jest_test")
# Note this also exports a jest_test.updateSnap name
jest_test(
name = "jest",
deps = [
"@yarn//:node_modules",
# Local files
"//jest-config:jest.config.js",
"//jest-config:cssTransform.js",
"//jest-config:fileTransform.js",
"//jest-config:bazelReporter.js",
"//:tsconfig.json"
],
)
Here is the BUILD file with every dependency in package.json, for reference
jest_test(
name = "jest",
deps = [
# Local files
"//jest-config:jest.config.js",
"//jest-config:cssTransform.js",
"//jest-config:fileTransform.js",
"//jest-config:bazelReporter.js",
"//:tsconfig.json",
# Everything in package.json
"@yarn//@babel/core",
"@yarn//@bazel/cypress",
"@yarn//@babel/preset-env",
"@yarn//@babel/preset-react",
"@yarn//@babel/preset-typescript",
"@yarn//@babel/plugin-proposal-class-properties",
"@yarn//@bazel/typescript",
"@yarn//@cypress/webpack-batteries-included-preprocessor",
"@yarn//@cypress/webpack-preprocessor",
"@yarn//@testing-library/react",
"@yarn//@types/jest",
"@yarn//@types/node",
"@yarn//@types/react",
"@yarn//@types/react-dom",
"@yarn//@types/testing-library__react",
"@yarn//@typescript-eslint/parser",
"@yarn//react-dev-utils",
"@yarn//babel-eslint",
"@yarn//babel-jest",
"@yarn//babel-loader",
"@yarn//babel-plugin-named-asset-import",
"@yarn//case-sensitive-paths-webpack-plugin",
"@yarn//css-loader",
"@yarn//cypress",
"@yarn//eslint",
"@yarn//eslint-config-react-app",
"@yarn//eslint-loader",
"@yarn//eslint-plugin-flowtype",
"@yarn//eslint-plugin-import",
"@yarn//eslint-plugin-jsx-a11y",
"@yarn//eslint-plugin-react",
"@yarn//eslint-plugin-react-hooks",
"@yarn//file-loader",
"@yarn//filemanager-webpack-plugin",
"@yarn//fork-ts-checker-webpack-plugin",
"@yarn//html-webpack-plugin",
"@yarn//identity-obj-proxy",
"@yarn//jest",
"@yarn//mini-css-extract-plugin",
"@yarn//optimize-css-assets-webpack-plugin",
"@yarn//pnp-webpack-plugin",
"@yarn//postcss-flexbugs-fixes",
"@yarn//postcss-loader",
"@yarn//postcss-normalize",
"@yarn//postcss-preset-env",
"@yarn//postcss-safe-parser",
"@yarn//prettier",
"@yarn//react",
"@yarn//react-dom",
"@yarn//resolve",
"@yarn//resolve-url-loader",
"@yarn//sass-loader",
"@yarn//style-loader",
"@yarn//terser-webpack-plugin",
"@yarn//ts-jest",
"@yarn//ts-loader",
"@yarn//tslib",
"@yarn//typescript",
"@yarn//typescript-plugin-css-modules",
"@yarn//url-loader",
"@yarn//wait-on",
"@yarn//webpack",
"@yarn//webpack-cli",
"@yarn//webpack-dev-server",
"@yarn//webpack-manifest-plugin",
"@yarn//workbox-webpack-plugin",
],
)
@mrmeku
Some more information for you:
I just tried running this with the latest version of bazel
, 3.7.1
to no avail.
I also ran using --sandbox_debug
with the BUILD
file that includes @yarn//:node_modules
(the one listed above) AND --node_options=--preserve-symlinks
turned manually in jest.bzl
(even though preserve_symlinks
is already enabled in node_repositories
).
It still failed, but gave some interesting information. In this failure case, bazel test
reports a missing file. However, the file is present after looking for it in the leftover sandbox from --sandbox_debug
See the following output
C02Y21FGJGH6:repro-bazel-rulesnodejs-2325 will.king$ cat /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/execroot/__main__/bazel-out/darwin-fastbuild/testlogs/jest/test.log
exec ${PAGER:-/usr/bin/less} "$0" || exit 1
Executing tests from //:jest
-----------------------------------------------------------------------------
FAIL src/subcomponents/FancyDiv.test.tsx
● Test suite failed to run
Cannot find module '../websockets/WebSocket-impl.js'
Require stack:
- /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jsdom/lib/jsdom/living/generated/WebSocket.js
- /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jsdom/lib/jsdom/living/interfaces.js
- /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jsdom/lib/jsdom/browser/Window.js
- /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jsdom/lib/api.js
- /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jest-environment-jsdom/build/index.js
- /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jest-runner/build/runTest.js
- /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jest-runner/build/testWorker.js
- /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jest-worker/build/workers/processChild.js
at Object.<anonymous> (../../../../../../node_modules/jsdom/lib/jsdom/living/generated/WebSocket.js:423:14)
PASS src/App.test.tsx
Test Suites: 1 failed, 1 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 1 passed, 1 total
Time: 4.013 s
Ran all test suites within paths "src/App.module.css", "src/App.test.tsx", "src/App.tsx", "src/__snapshots__/App.test.tsx.snap", "src/index.tsx", "src/serviceWorker.ts", "src/setupTests.ts", "src/subcomponents/FancyDiv.module.css", "src/subcomponents/FancyDiv.test.tsx", "src/subcomponents/FancyDiv.tsx", "src/subcomponents/__snapshots__/FancyDiv.test.tsx.snap".
This would indicate jsdom/lib/jsdom/living/generated/WebSocket.js
is searching for jsdom/lib/jsdom/living/websockets/WebSocket-impl.js
, but can't find it.
However, that file DOES EXIST in the resulting sandbox, see the following:
C02Y21FGJGH6:repro-bazel-rulesnodejs-2325 will.king$ ls -al /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jsdom/lib/jsdom/living/websockets/ | grep WebSocket-impl.js
lrwxr-xr-x 1 will.king wheel 163 Dec 8 09:43 WebSocket-impl.js -> /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/execroot/__main__/external/yarn/node_modules/jsdom/lib/jsdom/living/websockets/WebSocket-impl.js
And the file does have content
C02Y21FGJGH6:repro-bazel-rulesnodejs-2325 will.king$ head /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/3/execroot/__main__/node_modules/jsdom/lib/jsdom/living/websockets/WebSocket-impl.js
"use strict";
const nodeURL = require("url");
const DOMException = require("domexception/webidl2js-wrapper");
const { parseURL, serializeURL, serializeURLOrigin } = require("whatwg-url");
const WebSocket = require("ws");
const { setupForSimpleEventAccessors } = require("../helpers/create-event-accessor");
const { fireAnEvent } = require("../helpers/events");
I then tried the same run config, except I removed preserve_symlinks = True
from node_repositories
and removed --node_options=--preserve-symlinks
from jest.bzl
.
This again resulted in a failure, with the following error
C02Y21FGJGH6:repro-bazel-rulesnodejs-2325 will.king$ cat /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/execroot/__main__/bazel-out/darwin-fastbuild/testlogs/jest/test.log
exec ${PAGER:-/usr/bin/less} "$0" || exit 1
Executing tests from //:jest
-----------------------------------------------------------------------------
FAIL src/App.test.tsx
● Test suite failed to run
Cannot find module '/private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/5/execroot/__main__/node_modules/xml-name-validator/lib/xml-name-validator.js'. Please verify that the package.json has a valid "main" entry
at Object.<anonymous> (../../../../../../node_modules/jsdom/lib/jsdom/living/helpers/validate-names.js:2:13)
PASS src/subcomponents/FancyDiv.test.tsx
Test Suites: 1 failed, 1 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 1 passed, 1 total
Time: 5.073 s
Ran all test suites within paths "src/App.module.css", "src/App.test.tsx", "src/App.tsx", "src/__snapshots__/App.test.tsx.snap", "src/index.tsx", "src/serviceWorker.ts", "src/setupTests.ts", "src/subcomponents/FancyDiv.module.css", "src/subcomponents/FancyDiv.test.tsx", "src/subcomponents/FancyDiv.tsx", "src/subcomponents/__snapshots__/FancyDiv.test.tsx.snap".
And again, the file it is searching for DOES exist
C02Y21FGJGH6:repro-bazel-rulesnodejs-2325 will.king$ cat /private/var/tmp/_bazel_will.king/55c051ec652d37f5a3ded1b8d7f5a204/sandbox/darwin-sandbox/5/execroot/__main__/node_modules/xml-name-validator/lib/xml-name-validator.js
"use strict";
const parser = require("./generated-parser.js");
exports.name = function (potentialName) {
return mapResult(parser.startWith("Name").exec(potentialName));
};
exports.qname = function (potentialQname) {
return mapResult(parser.startWith("QName").exec(potentialQname));
};
function mapResult(result) {
return {
success: result.success,
error: result.error && parser.getTrace(result.error.message)
};
}
@mrmeku Bumping this, any chance of getting it re-opened?
This issue has been automatically marked as stale because it has not had any activity for 90 days. It will be closed if no further activity occurs in two weeks. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs!
This issue was automatically closed because it went two weeks without a reply since it was labeled "Can Close?"
I'm experiencing the same issue and I'm not sure what to do about it either.
I am seeing a similar behaviour for jest snapshots. bazel run
is similar to npm test
but differs for bazel test
🐞 bug report
Affected Rule
Jest tests from the generated jest-cli rule as well as cypress tests from the cypress rules_nodejs package.
Is this a regression?
No
Description
When certain jest/cypress tests are run using
bazel test
they fail, where they succeed withbazel run
.In both cases test seem to fail to communicate with a child process, for example with jest we're seeing output like:
Interestingly, the amount of tests that succeed / fail seem to be random with jest. Sometimes a run will produce a situation where all tests fail, and others some succeed as is the case with the output above.
In the case of cypress, we are launching a webpack dev server inside our cypress plugins.ts file, which produces worker communication errors when it runs
fork-ts-checker-webpack-plugin
, and will hang indefinitely if that plugin is removed.Again these tests work fine using
bazel run
and are not flaky using that method.🔬 Minimal Reproduction
🔥 Exception or Error
Jest error
🌍 Your Environment
Operating System:
Output of
bazel version
:Rules_nodejs version:
Anything else relevant?