stryker-js icon indicating copy to clipboard operation
stryker-js copied to clipboard

Yarn workspaces installation

Open HonzaMac opened this issue 2 years ago • 10 comments

Summary

Unable to simply install with command stryker init when using yarn workspaces.

Steps:

  1. Go into root of yarn monorepo with workspaces defined.
  2. Run as suggested npm install -g stryker-cli
  3. Run stryker init --fileLogLevel trace

Expected: initiation is finished, config is created, log file contains informations.

Current: Received error, which lead to manual installation.

➜ web git:(feature/stryker-common-package) stryker init --fileLogLevel trace
Stryker is currently not installed.
? Do you want to install Stryker locally? yarn

             |STRYKER|              
       ~control the mutants~        

           ..####@####..            
        .########@########.         
      .#####################.       
     #########################      
    ###########################     
    ###########################     
    @@@#####################@@@     
    ###########################     
    ###########################     
     #########################      
      '######################'      
        '########@#########'        
           ''####@####''            

Installing: yarn add @stryker-mutator/core --dev
yarn add v1.22.19
error Running this command will add the dependency to the workspace root rather than the workspace itself, which might not be what you want - if you really meant it, make it explicit by running this command again with the -W flag (or --ignore-workspace-root-check).
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
Command failed: yarn add @stryker-mutator/core --dev
➜ web git:(feature/stryker-common-package)

Stryker config

  • not created yet

Test runner config

Jest

Your Environment

software version(s)
node v16.15.1
npm 8.11.0
Operating System Mac Os Monterey 12.4 (21F79)

I am unable to provide stryker --version version as command returns

Stryker is currently not installed.
? Do you want to install Stryker locally? (Use arrow keys)
...

Add stryker.log

None created.

HonzaMac avatar Aug 13 '22 19:08 HonzaMac

Hi @HonzaMac thanks for opening this issue.

I think stryker init should provide the -W flag in this case.

As a workaround, you can use yarn add --dev -W @stryker-mutator/core. After that, the stryker cli command should work as expected.

nicojs avatar Aug 15 '22 06:08 nicojs

I've run yarn add --dev -W @stryker-mutator/core manually and proceed with stryker init --fileLogLevel trace once more.

I've received this error:

➜ web git:(feature/stryker-common-package) ✗ stryker init --fileLogLevel trace      
file:///Users/machala/web/node_modules/@stryker-mutator/core/dist/src/process/1-prepare-executor.js:4
import { execaCommand } from 'execa';
         ^^^^^^^^^^^^
SyntaxError: Named export 'execaCommand' not found. The requested module 'execa' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'execa';
const { execaCommand } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:128:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:194:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
➜ web git:(feature/stryker-common-package) ✗ ll

Package.json looks like this now:

+    "@stryker-mutator/core": "^6.1.2",

HonzaMac avatar Aug 15 '22 07:08 HonzaMac

What is your node version? And does this command work?

yarn run stryker run

nicojs avatar Aug 15 '22 11:08 nicojs

node: v16.15.1

yarn run stryker run

It has exactly same output.

import pkg from 'execa';
const { execaCommand } = pkg;

If I modify code directly, it continues.

HonzaMac avatar Aug 16 '22 06:08 HonzaMac

And after this I receive another issue. Seems it ignores proxy settings.

➜  web git:(feature/stryker-common-package) ✗ stryker init --fileLogLevel trace
? Are you using one of these frameworks? Then select a preset configuration. None/other
11:21:21 (74153) ERROR NpmClient Unable to reach npms.io (for query /v2/search?q=keywords:@stryker-mutator/test-runner-plugin). Please check your internet connection. Error: ECONNREFUSED (connect) Error: connect ECONNREFUSED 188.114.97.0:443
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)
? What build command should be executed just before running your tests? For example: "npm run build" or "tsc -b" (leave empty when this is not needed). (none) 
➜

I've tried to bypass this in source code like this:

const settings: IRequestOptions = {
  ignoreSslError: true,
  proxy: {
    proxyUrl: 'http://webproxy.company.com:8080'
  }};
export function initializerFactory(): StrykerInitializer {
  return provideLogger(createInjector())
    .provideValue(initializerTokens.out, console.log)
    .provideValue(initializerTokens.strykerPresets, strykerPresets)
    .provideValue(initializerTokens.restClientNpmSearch, new RestClient('npmSearch', BASE_NPM_SEARCH, undefined, settings))
    .provideValue(initializerTokens.restClientNpm, new RestClient('npm', BASE_NPM_PACKAGE, undefined, settings))
    .provideClass(initializerTokens.npmClient, NpmClient)
...

And after this node_modules in node_modules/@stryker-mutator/core/src/initializer/index.ts fix i got another with -W flag.

➜  web git:(feature/stryker-common-package) ✗ stryker init --fileLogLevel trace
? Are you using one of these frameworks? Then select a preset configuration. None/other
? Which test runner do you want to use? If your test runner isn't listed here, you can choose "command" (it uses your `npm test` command, but will come with a big performance penalty) jest
? Which reporter(s) do you want to use? html, clear-text, progress
? Which package manager do you want to use? yarn
? What file type do you want for your config file? JavaScript
Writing & formatting stryker.conf.mjs ...
Installing NPM dependencies...
yarn add @stryker-mutator/jest-runner --dev
yarn add v1.22.19
error Running this command will add the dependency to the workspace root rather than the workspace itself, which might not be what you want - if you really meant it, make it explicit by running this command again with the -W flag (or --ignore-workspace-root-check).
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
An error occurred during installation, please try it yourself: "yarn add @stryker-mutator/jest-runner --dev"
Done configuring stryker. Please review "stryker.conf.mjs", you might need to configure your test runner correctly.
Let's kill some mutants with this command: `stryker run`
➜

I installed manually via that command.

And with stryker run I received successful result:

➜ web git:(feature/stryker-common-package) ✗ stryker init --fileLogLevel trace
? Are you using one of these frameworks? Then select a preset configuration. None/other
? Which test runner do you want to use? If your test runner isn't listed here, you can choose "command" (it uses your `npm test` command, but will come with a big performance penalty) jest
? Which reporter(s) do you want to use? html, clear-text, progress
? Which package manager do you want to use? yarn
? What file type do you want for your config file? JavaScript
Writing & formatting stryker.conf.mjs ...
Installing NPM dependencies...
yarn add @stryker-mutator/jest-runner --dev
yarn add v1.22.19
error Running this command will add the dependency to the workspace root rather than the workspace itself, which might not be what you want - if you really meant it, make it explicit by running this command again with the -W flag (or --ignore-workspace-root-check).
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
An error occurred during installation, please try it yourself: "yarn add @stryker-mutator/jest-runner --dev"
Done configuring stryker. Please review "stryker.conf.mjs", you might need to configure your test runner correctly.
Let's kill some mutants with this command: `stryker run`
➜

HonzaMac avatar Aug 16 '22 11:08 HonzaMac

This is all very strange to me!

Seems it ignores proxy settings.

This shouldn't be the case. We use typed-rest-client, which supports these env variables:

https://github.com/microsoft/typed-rest-client/blob/master/lib/HttpClient.ts#L96-L100

enum EnvironmentVariables {
    HTTP_PROXY = "HTTP_PROXY",
    HTTPS_PROXY = "HTTPS_PROXY",
    NO_PROXY = "NO_PROXY",
}
import pkg from 'execa';
const { execaCommand } = pkg;

If I modify code directly, it continues.

Interesting. Which version of exaca is it using? I'm wondering if yarn installed an older version of execa, namely v5.1.1 or older:

mkdir tmp && cd tmp
npm init --yes
npm i execa
echo "import { execaCommand } from 'execa';" > main.mjs
node main.mjs
# This works (no error)
npm i execa@5
node main.mjs
file:///home/nicojs/github/main.mjs:1
import { execaCommand } from 'execa';
         ^^^^^^^^^^^^
SyntaxError: Named export 'execaCommand' not found.

The package.json of @stryker-mutator/core clearly specifies the execa version: "execa": "~6.1.0",

nicojs avatar Aug 16 '22 11:08 nicojs

Thank you for pointing me to execa version. We actually have execa v4 in yarn resolutions 🙈.

HonzaMac avatar Aug 17 '22 05:08 HonzaMac

I've take a look once again, the only problematic part is that yarn install is missing -W for repositories with workspaces.

HonzaMac avatar Sep 08 '22 21:09 HonzaMac

True. What do you suggest to improve this? Can we add -W in all our docs? Or add to the troubleshooting page?

nicojs avatar Sep 10 '22 07:09 nicojs

I would prefer to add -W in background and detect need for it during stryker init, so user can finish init without error.

We may detect that by reading workspace from package json.

HonzaMac avatar Sep 11 '22 07:09 HonzaMac

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 16 '23 19:09 stale[bot]