codymikol / karma-webpack

Karma webpack Middleware
MIT License
830 stars 222 forks source link

TypeError: Path must be a string. Received [ 'js/main.spec.js', #350

Closed casufi closed 6 years ago

casufi commented 6 years ago

x「wdm」: wait until bundle finished: noop 03 09 2018 10:51:35.026:ERROR [karma]: TypeError: Path must be a string. Received [ 'js/main.spec.js', 'css/tiscc.min.css', 'js/main.spec.js.map', 'css/tiscc.min.css.map' ] at assertPath (path.js:28:11) at Object.join (path.js:1236:7) at Plugin.<anonymous> (/builds/tis/tis/tiscommandcenter/node_modules/karma-webpack/lib/karma-webpack.js:262:68) at Plugin.readFile (/builds/tis/tis/tiscommandcenter/node_modules/karma-webpack/lib/karma-webpack.js:281:5) at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickCallback (internal/process/next_tick.js:180:9)

Code

https://gist.github.com/casufi/f298bcab3f2f95a82616591446e380de

How Do We Reproduce?

rythos42 commented 6 years ago

I'm brand new to using karma-webpack and ran into this issue yesterday while trying to set up my environment. Is there a version that would be better to use, while this gets looked at? Thanks!

nstepien commented 6 years ago

@rythos42 3.0.0 is working fine for me.

yibn2008 commented 6 years ago

+1 I also found this bug, I've fallback to 3.0.0.

vladimiry commented 6 years ago

Facing the same error running karma-webpack@3.0.2 on Windows OS, see output here.

karma-webpack@3.0.1 doesn't have such problem, but it requires this workaround.

npetruzzelli commented 6 years ago

the problem is it is this line:

https://github.com/webpack-contrib/karma-webpack/blob/d2f5a538d864e7b1f4bb431324de67be1ccbb51d/src/karma-webpack.js#L255

The problem is not line 255 itself, that is just where it occurs.

file is a file system path that is normalized to the operating system, meaning the path seperator on Windows is \ BUT the keys in this.outputs always uses the forward slash / regardless of the operating system. so the result of this.outputs[file] will always be undefined.

This will always be a problem on Windows. It will just happen to work on POSIX systems, but its fragile.

It appears this is a new issue. I was recently using 3.0.0 without any problems, when someone went to review my code "karma-webpack": "^3.0.0" in package.json resolved to 3.0.2 and the errors started.

lusarz commented 6 years ago

I've also met this (or similar) issue. In my case it occured when I was using karma in autowatch mode. The value of this.outputs[file] was an array:

[
  'javascript/index.bundle.js',
  '46.0848c6149c8de4ab841b.hot-update.js' 
]
43081j commented 6 years ago

This can be encountered if a preprocessor beforehand has already produced multiple outputs, too.

For example, sourcemaps preprocessing will result in ['foo.js', 'foo.js.map'].

npetruzzelli commented 6 years ago

this.outputs appears 6 places in the file. I haven't finished looking through it, so I don't know what the impact of what I am about to suggest is (yet).

https://github.com/webpack-contrib/karma-webpack/blob/d2f5a538d864e7b1f4bb431324de67be1ccbb51d/src/karma-webpack.js#L125-L133

console.log(this.entries);
/* =>
{
    'apps\\front-end\\src\\_assets\\debug\\root.spec': 'apps/front-end/src/_assets/debug/root.spec.tsx',
    'apps\\front-end\\src\\_assets\\main\\main.spec': 'apps/front-end/src/_assets/main/main.spec.tsx'
}
*/

console.log(entry);
// => "apps\\front-end\\src\\_assets\\main\\main.spec"
// NOTE: the file extension `.tsx` is not present

console.log(entryPath);
// => "apps/front-end/src/_assets/main/main.spec.tsx"

// ... later in the file

console.log(file);
// => "apps\\front-end\\src\\_assets\\main\\main.spec.tsx"

console.log(this.outputs);
/* =>
{
    'apps/front-end/src/_assets/debug/root.spec.tsx': [
        '_assets/bundles/apps\\front-end\\src\\_assets\\debug\\root.spec/apps\\front-end\\src\\_assets\\debug\\root.spec.js',
        '_assets/bundles/apps\\front-end\\src\\_assets\\debug\\root.spec/apps\\front-end\\src\\_assets\\debug\\root.spec.js.map'
    ],
    'apps/front-end/src/_assets/main/main.spec.tsx': [
        '_assets/bundles/apps\\front-end\\src\\_assets\\main\\main.spec/apps\\front-end\\src\\_assets\\main\\main.spec.js',
        '_assets/bundles/apps\\front-end\\src\\_assets\\main\\main.spec/apps\\front-end\\src\\_assets\\main\\main.spec.js.map'
    ]
}
*/

// For context, here is a relevant portion of the webpack configuration:
/*
{
...
    output: {
        path: PATH_TO_DIST,
        publicPath: '/',
        filename: IS_PRODUCTION_ENV
            ? '_assets/bundles/[name]/[name].[hash:20].js'
            : '_assets/bundles/[name]/[name].js'
    }
...
}
*/

A change similar to this might fix the issue:

           var entryPath = this.entries[entry]
           var outputPath = stats.assetsByChunkName[entry]
-          this.outputs[entryPath] = outputPath
+          var entryPathNormalized = entryPath.replace(/\//g, path.sep)
+          this.outputs[entryPathNormalized] = outputPath

path.normalize() does more than handle the path separator, so I'm not sure if it should be used instead or not.

The above is a first step, the final fix may be more involved.

npetruzzelli commented 6 years ago

I took another look at the original post. It looks like my issue is separate (but similar) to this one, where instead of receiving an unexpected array (like the OP), it is receiving undefined, which is also an invalid argument for the path.join() method as used in lines 238 and 255.

thijstriemstra commented 6 years ago

3.0.0 is working fine for me.

but this doesn't support webpack 4, does it? is there any workaround for webpack 4 users? this issue effectively broke all our CI builds/PRs.

43081j commented 6 years ago

Yes, there are two issues.

One is the mismatch of path separators (#353 and #351 are related). Simple to fix.

The other is that chunks can have multiple assets so assetsByChunkName[entry] can be an array or a string (as per webpack's docs). This is not a simple fix as we can't really infer which of the assets is the code, unless we maybe pick the first .js entry.

edit:

@thijstriemstra webpack 4 and 3.0.0 do work together (its what im using now until these get fixed).

ryanclark commented 6 years ago

Will get a fix out asap

nstepien commented 6 years ago

@thijstriemstra well I'm on webpack ^4 and karma-webpack 3.0.0, and my tests run. ¯\_(ツ)_/¯

thijstriemstra commented 6 years ago

@MayhemYDG oh damn, i'll try that, cheers.

Update: same results, wtf

> wavesurfer.js@2.0.7 test /home/thijs/projects/wavesurfer.js
> karma start karma.conf.js

(node:4394) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:109:14)
    at Array.forEach (<anonymous>)
    at Plugin (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:108:16)
    at invoke (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:75:15)
    at Array.instantiate (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:59:20)
    at get (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:48:43)
    at /home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:71:14
    at Array.map (<anonymous>)
    at Array.invoke (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:70:31)
    at Injector.get (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:48:43)
    at instantiatePreprocessor (/home/thijs/projects/wavesurfer.js/node_modules/karma/lib/preprocessor.js:50:20)
    at Array.forEach (<anonymous>)
    at createPreprocessor (/home/thijs/projects/wavesurfer.js/node_modules/karma/lib/preprocessor.js:68:20)
    at Array.invoke (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:75:15)
    at get (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:48:43)
    at /home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:71:14
ℹ 「wdm」: wait until bundle finished: noop
ℹ 「wdm」: wait until bundle finished: noop
ℹ 「wdm」: wait until bundle finished: noop
ℹ 「wdm」: wait until bundle finished: noop
06 09 2018 17:25:33.856:ERROR [karma]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
    at assertPath (path.js:39:11)
    at Object.join (path.js:1158:7)
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:262:68)
    at Plugin.readFile (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:281:5)
    at process._tickCallback (internal/process/next_tick.js:176:11)
06 09 2018 17:25:33.858:ERROR [karma]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
    at assertPath (path.js:39:11)
    at Object.join (path.js:1158:7)
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:262:68)
    at Plugin.readFile (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:281:5)
    at process._tickCallback (internal/process/next_tick.js:176:11)
06 09 2018 17:25:33.859:ERROR [karma]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
    at assertPath (path.js:39:11)
    at Object.join (path.js:1158:7)
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:262:68)
    at Plugin.readFile (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:281:5)
    at process._tickCallback (internal/process/next_tick.js:176:11)
06 09 2018 17:25:33.859:ERROR [karma]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
    at assertPath (path.js:39:11)
    at Object.join (path.js:1158:7)
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:262:68)
    at Plugin.readFile (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:281:5)
    at process._tickCallback (internal/process/next_tick.js:176:11)
ℹ 「wdm」: 
ℹ 「wdm」: Compiled successfully.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! wavesurfer.js@2.0.7 test: `karma start karma.conf.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the wavesurfer.js@2.0.7 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
nstepien commented 6 years ago

@thijstriemstra I get the same error on karma-webpack 3.0.2. Make sure you install version 3.0.0 exactly. Delete your node_modules and lock files, re-install your npm dependencies, and retry.

michael-ciniawsky commented 6 years ago

Fixed by #353, released in v3.0.3 🎉

uncleramsay commented 6 years ago

Er, I'm still getting the same error reported by the OP in 3.0.3

43081j commented 6 years ago

@michael-ciniawsky only one of the two issues was fixed.

the other issue was that a source can have multiple assets so the output entry may be an array.

michael-ciniawsky commented 6 years ago

@43081j I'm semi-aware of the regression introduced by https://github.com/webpack-contrib/karma-webpack/commit/d2f5a538d864e7b1f4bb431324de67be1ccbb51d and may need to revert it for now, but I need more info why it doesn't work. Could you please open a new issue elaborating on it?

michael-ciniawsky commented 6 years ago

Released in v3.0.4 :tada:

kirschre commented 6 years ago

I'm still seeing the same issue on 3.0.4 (tests succeed on 3.0.0):

07 09 2018 12:30:23.322:ERROR [karma]: TypeError: Path must be a string. Received [ 'path/to/karma_index.bundle.js',
  'path/to/karma_index.bundle.js.map' ]
    at assertPath (path.js:28:11)
    at Object.join (path.js:1236:7)
    at Plugin.<anonymous> (path/to/node_modules/karma-webpack/lib/karma-webpack.js:266:68)
    at Plugin.readFile (path/to/karma-webpack/lib/karma-webpack.js:285:5)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
ℹ 「wdm」: Hash: 95085f23983f7eeef8e0
Version: webpack 4.17.2
Time: 22273ms
Built at: 2018-09-07 11:30:23
thijstriemstra commented 6 years ago

Note to maintainer: let us test your fixes first instead of putting out new buggy releases?

michael-ciniawsky commented 6 years ago

@thijstriemstra Sure, do you want to be the new maintainer of this plugin and write the tests ? 😛 I'm only administrating here...

@kirschre #355 (it's a different known bug, introduced by https://github.com/webpack-contrib/karma-webpack/commit/ab4dde9a7b859e815e11925a0f5cc5c986360264, e.g when using sourcemaps)

thijstriemstra commented 6 years ago

No but put out a release candidate for testing first. Oh well.

nstepien commented 6 years ago

Or revert regressions. 🤔

JonUK commented 6 years ago

I have also seen this issue with sourcemap generation disabled but with a JS file that imports SASS:

ERROR [karma]: TypeError: Path must be a string. Received [ 'src\components\person\person.test.css', 'src\components\person\person.test-bundle.js' ]

hans-pragt commented 6 years ago

This can be encountered if a preprocessor beforehand has already produced multiple outputs, too.

For example, sourcemaps preprocessing will result in ['foo.js', 'foo.js.map'].

Yep, setting 'devtool' to 'none' fixed this issue for me.

joseph-jja commented 6 years ago

Same issue in 3.0.5, so staying on 3.0.0. Mac and linux OSes

nronnei commented 5 years ago

Confirming that this is still an issue on 3.0.5 running Ubuntu 16.04 and 18.04.

aburai commented 5 years ago

@MayhemYDG oh damn, i'll try that, cheers.

Update: same results, wtf

> wavesurfer.js@2.0.7 test /home/thijs/projects/wavesurfer.js
> karma start karma.conf.js

(node:4394) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:109:14)
    at Array.forEach (<anonymous>)
    at Plugin (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:108:16)
    at invoke (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:75:15)
    at Array.instantiate (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:59:20)
    at get (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:48:43)
    at /home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:71:14
    at Array.map (<anonymous>)
    at Array.invoke (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:70:31)
    at Injector.get (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:48:43)
    at instantiatePreprocessor (/home/thijs/projects/wavesurfer.js/node_modules/karma/lib/preprocessor.js:50:20)
    at Array.forEach (<anonymous>)
    at createPreprocessor (/home/thijs/projects/wavesurfer.js/node_modules/karma/lib/preprocessor.js:68:20)
    at Array.invoke (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:75:15)
    at get (/home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:48:43)
    at /home/thijs/projects/wavesurfer.js/node_modules/di/lib/injector.js:71:14
ℹ 「wdm」: wait until bundle finished: noop
ℹ 「wdm」: wait until bundle finished: noop
ℹ 「wdm」: wait until bundle finished: noop
ℹ 「wdm」: wait until bundle finished: noop
06 09 2018 17:25:33.856:ERROR [karma]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
    at assertPath (path.js:39:11)
    at Object.join (path.js:1158:7)
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:262:68)
    at Plugin.readFile (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:281:5)
    at process._tickCallback (internal/process/next_tick.js:176:11)
06 09 2018 17:25:33.858:ERROR [karma]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
    at assertPath (path.js:39:11)
    at Object.join (path.js:1158:7)
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:262:68)
    at Plugin.readFile (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:281:5)
    at process._tickCallback (internal/process/next_tick.js:176:11)
06 09 2018 17:25:33.859:ERROR [karma]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
    at assertPath (path.js:39:11)
    at Object.join (path.js:1158:7)
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:262:68)
    at Plugin.readFile (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:281:5)
    at process._tickCallback (internal/process/next_tick.js:176:11)
06 09 2018 17:25:33.859:ERROR [karma]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
    at assertPath (path.js:39:11)
    at Object.join (path.js:1158:7)
    at Plugin.<anonymous> (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:262:68)
    at Plugin.readFile (/home/thijs/projects/wavesurfer.js/node_modules/karma-webpack/lib/karma-webpack.js:281:5)
    at process._tickCallback (internal/process/next_tick.js:176:11)
ℹ 「wdm」: 
ℹ 「wdm」: Compiled successfully.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! wavesurfer.js@2.0.7 test: `karma start karma.conf.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the wavesurfer.js@2.0.7 test script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

@thijstriemstra hope it helps

Fixed "path" error with var webpackConfig = require('./build-config/webpack.prod.main.js'); webpackConfig.devtool = 'none'; in karma.conf.js

DeprecationWarning is gone with "karma-webpack": "^4.0.0-rc.2"

elliotykim commented 5 years ago

I took another look at the original post. It looks like my issue is separate (but similar) to this one, where instead of receiving an unexpected array (like the OP), it is receiving undefined, which is also an invalid argument for the path.join() method as used in lines 238 and 255.

@npetruzzelli were you able to resolve this? for now I just pinned to version 3.0.0 and it works

npetruzzelli commented 5 years ago

@elliotykim - I have not been able to explore the issue further nor have I been able to contribute to resolving it. Life has kept me busy with other things. I have not been able to test if updates have resolved the issue.

My work around is exactly the same as you in that I explicitly use:

chrisnicola commented 5 years ago

Still seeing this in 3.0.5 and in 4.0.0-rc.6. The test failures are intermittent however for me.

dsuckau commented 5 years ago

Why is this closed? Still having the same issue with 3.0.5. Have to pin hard to 3.0.0 and it works.

trcarden commented 5 years ago

@michael-ciniawsky should this still stay closed? Seems like people are still coming in with issues.

Likewise seeing intermittent test failures. We are running "karma-webpack": "^4.0.2", adding or removing the dev-tool didn't seem to have an effect.

28 06 2019 23:52:50.773:ERROR [karma-server]: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
    at validateString (internal/validators.js:125:11)
    at Object.join (path.js:1037:7)
    at Plugin.<anonymous> (/path/to/node_modules/karma-webpack/dist/karma-webpack.js:317:70)
    at Plugin.readFile (/path/to/node_modules/karma-webpack/dist/karma-webpack.js:333:5)
    at /path/to/node_modules/karma-webpack/dist/karma-webpack.js:352:19
    at nextPreprocessor (/path/to/node_modules/karma/lib/preprocessor.js:32:26)
    at /path/to/node_modules/karma/lib/preprocessor.js:121:9
    at module.exports (/path/to/node_modules/isbinaryfile/index.js:29:12)
    at readFileCallback (/path/to/node_modules/karma/lib/preprocessor.js:87:7)
trcarden commented 5 years ago

I created a new ticket with a possible solution to the problem. See #422 and the "Workaround" section