nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.27k stars 2.32k forks source link

Failed to run Karma tests on NX if karma.conf.js path contains space #5477

Closed segrey closed 3 years ago

segrey commented 3 years ago

Current Behavior

Error when running Karma tests if path to karma.conf.js contains space.

Expected Behavior

Karma tests are run.

Steps to Reproduce

  1. Setup a default Nx workspace with Angular:
    npx create-nx-workspace my-workspace --preset=empty --cli=angular
    cd my-workspace
    ./node_modules/.bin/ng add @nrwl/angular
    ./node_modules/.bin/ng g app myApp --unit-test-runner=karma

    Or clone https://github.com/segrey/nx-karma-path-with-space-example.

  2. Run tests with ng test my-app --karma-config apps/my-app/karma.conf.js. Tests are run successfully.
  3. Run the same karma.conf.js, but now with a space in its path. For example, to achieve this you can copy karma.conf.js to a file with a space in name:
    cp apps/my-app/karma.conf.js apps/my-app/karma\ conf.js
    
    ng test my-app --karma-config apps/my-app/karma\ conf.js 

nx run my-app:test --karma-config="apps/my-app/karma conf.js" --karmaConfig="apps/my-app/karma conf.js" 27 04 2021 19:57:18.851:ERROR [config]: Error in config file! Error: Cannot find module '/home/segrey/example/my-workspace/"apps/my-app/karma conf.js"' Require stack:

  • /home/segrey/example/my-workspace/node_modules/karma/lib/config.js
  • /home/segrey/example/my-workspace/node_modules/karma/lib/server.js
  • /home/segrey/example/my-workspace/node_modules/karma/lib/index.js
  • /home/segrey/example/my-workspace/node_modules/@angular-devkit/build-angular/src/karma/index.js
  • /home/segrey/example/my-workspace/node_modules/@angular-devkit/architect/node/node-modules-architect-host.js
  • /home/segrey/example/my-workspace/node_modules/@angular-devkit/architect/node/index.js
  • /home/segrey/example/my-workspace/node_modules/@nrwl/tao/src/commands/ngcli-adapter.js
  • /home/segrey/example/my-workspace/node_modules/@nrwl/tao/src/commands/run.js
  • /home/segrey/example/my-workspace/node_modules/@nrwl/tao/index.js
  • /home/segrey/example/my-workspace/node_modules/@nrwl/cli/lib/run-cli.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15) at Function.Module._load (internal/modules/cjs/loader.js:725:27) at Module.require (internal/modules/cjs/loader.js:952:19) at Module.require (/home/segrey/example/my-workspace/packages/tao/src/compat/compat.ts:9:36) at require (internal/modules/cjs/helpers.js:88:18) at Object.parseConfig (/home/segrey/example/my-workspace/node_modules/karma/lib/config.js:358:22) at new Server (/home/segrey/example/my-workspace/node_modules/karma/lib/server.js:65:24) at Observable._subscribe (/home/segrey/example/my-workspace/node_modules/@angular-devkit/build-angular/src/karma/index.js:105:29) at Observable._trySubscribe (/home/segrey/example/my-workspace/node_modules/rxjs/src/internal/Observable.ts:238:19) at Observable.subscribe (/home/segrey/example/my-workspace/node_modules/rxjs/src/internal/Observable.ts:219:14) at Object.innerSubscribe (/home/segrey/example/my-workspace/node_modules/rxjs/src/internal/innerSubscribe.ts:111:19) at SwitchMapSubscriber._innerSub (/home/segrey/example/my-workspace/node_modules/rxjs/src/internal/operators/switchMap.ts:137:30) at SwitchMapSubscriber._next (/home/segrey/example/my-workspace/node_modules/rxjs/src/internal/operators/switchMap.ts:126:10) at SwitchMapSubscriber.Subscriber.next (/home/segrey/example/my-workspace/node_modules/rxjs/src/internal/Subscriber.ts:99:12) at /home/segrey/example/my-workspace/node_modules/rxjs/src/internal/util/subscribeToPromise.ts:8:20
    
    ### Failure Logs
    <!-- Please include any relevant log snippets or files here. -->

Environment

>  NX  Report complete - copy this into the issue template

  Node : 14.16.0
  OS   : linux x64
  npm  : 6.14.11

  nx : Not Found
  @nrwl/angular : 12.0.8
  @nrwl/cli : 12.0.8
  @nrwl/cypress : 12.0.8
  @nrwl/devkit : 12.0.8
  @nrwl/eslint-plugin-nx : 12.0.8
  @nrwl/express : Not Found
  @nrwl/jest : 12.0.8
  @nrwl/linter : 12.0.8
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : Not Found
  @nrwl/react : Not Found
  @nrwl/schematics : Not Found
  @nrwl/tao : 12.0.8
  @nrwl/web : Not Found
  @nrwl/workspace : 12.0.8
  @nrwl/storybook : Not Found
  @nrwl/gatsby : Not Found
  typescript : 4.1.5
segrey commented 3 years ago

Likely, this causes https://youtrack.jetbrains.com/issue/WEB-50713.

segrey commented 3 years ago

Looks like the problem is that forkProcessDirectOutputCapture converts task.overrides.karma-config (value: apps/my-app/karma conf.js, without double quotes) to --karma-config="apps/my-app/karma conf.js" image

and then, parseRunOpts converts args containing --karma-config="apps/my-app/karma conf.js" to opts.runOptions.karmaConfig (value: "apps/my-app/karma conf.js", with wrapping double quotes) image

Then, options.karmaConfig="apps/my-app/karma conf.js" (with double quotes) is passed to resolve(context.workspaceRoot, options.karmaConfig) in Angular where it's resolved to an incorrect /home/segrey/example/my-workspace/"apps/my-app/karma conf.js".

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.