GCP Cloudbuild中webpack4构建报"version"必填参数错误的排查与解决
问题背景
- 已在webpack官方仓库提交bug反馈issue,但因无法提供最小可复现示例,担心issue被维护者关闭
- 核心复现场景为GCP Cloudbuild流程触发基于webpack4的生产构建,报错仅在该云构建环境下出现,无法直接登录构建环境手动调试
关联的Cloudbuild构建步骤配置如下:
- name: node:$_NODE_VERSION entrypoint: npm args: ['run', 'build:ci'] # 生产环境配置,自动跳过BundleAnalyzerPlugin dir: "v3/client/" waitFor: ['css-built', 'secret-available'] id: 'client-built' substitutions: # Node15版本下canvas依赖安装会触发pangocairo缺失报错 # 报错信息:command failed sh -c node-pre-gyp install --fallback-to-build # 报错信息:No package 'pangocairo' found _NODE_VERSION: "14"
构建触发链路
上述Cloudbuild步骤执行的是项目内的生产构建脚本,对应package.json配置为:
"scripts": { "build:ci": "NODE_ENV=production npm run build -- --env ci" }
运行报错信息
构建执行到client-built步骤时抛出完整日志如下:
Step #4 - "client-built": (node:29) UnhandledPromiseRejectionWarning: Error: "version" is a required argument. Step #4 - "client-built": at Object.getArg (/workspace/v3/client/node_modules/webpack-sources/node_modules/source-map/lib/util.js:24:11) Step #4 - "client-built": at new BasicSourceMapConsumer (/workspace/v3/client/node_modules/webpack-sources/node_modules/source-map/lib/source-map-consumer.js:294:22) Step #4 - "client-built": at new SourceMapConsumer (/workspace/v3/client/node_modules/source-map/lib/source-map-consumer.js:22:7) Step #4 - "client-built": at SourceMapSource.node (/workspace/v3/client/node_modules/webpack-sources/lib/SourceMapSource.js:32:62) Step #4 - "client-built": at SourceMapSource.proto.sourceAndMap (/workspace/v3/client/node_modules/webpack-sources/lib/SourceAndMapMixin.js:29:18) Step #4 - "client-built": at getTaskForFile (/workspace/v3/client/node_modules/webpack/lib/SourceMapDevToolPlugin.js:65:30) Step #4 - "client-built": at /workspace/v3/client/node_modules/webpack/lib/SourceMapDevToolPlugin.js:215:20 Step #4 - "client-built": at Array.forEach (<anonymous>) Step #4 - "client-built": at /workspace/v3/client/node_modules/webpack/lib/SourceMapDevToolPlugin.js:186:12 Step #4 - "client-built": at SyncHook.eval (eval at create (/workspace/v3/client/node_modules/tapable/lib/HookCodeFactory.js:1:1), <anonymous>:12:1) Step #4 - "client-built": at SyncHook.lazyCompileHook (/workspace/v3/client/node_modules/tapable/lib/Hook.js:154:20) Step #4 - "client-built": at /workspace/v3/client/node_modules/webpack/lib/Compilation.js:1413:42 Step #4 - "client-built": at eval (eval at create (/workspace/v3/client/node_modules/tapable/lib/HookCodeFactory.js:1:1), <anonymous>:19:1) Step #4 - "client-built": (Use `node --trace-warnings ...` to show where the warning was created) Step #4 - "client-built": (node:29) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict`(参考Node.js CLI参数说明)。(rejection id: 1) Step #4 - "client-built": (node:29) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
核心报错为source-map模块抛出的"version" is a required argument,异常链路从source-map工具方法一路传递到webpack的SourceMapDevToolPlugin
已完成的排查动作
- 梳理当前webpack配置中所有和source-map相关的设置:
export default { mode: "production", devtool: "source-map", optimization: { minimizer: [ new TerserPlugin({ sourceMap: true, // 生产环境开启source-map需保持该配置为true terserOptions: { compress: { drop_console: true, }, output: { comments: false, }, }, }), ] }, module: { rules: [{ test: /\.css$/i, use: [ { loader: "css-loader", options: { importLoaders: 1, modules: false, sourceMap: true, }, }, ], }, { // 引入source-map支持 enforce: "pre", test: /\.js$/, exclude: /node_modules/, loader: "source-map-loader", }], } };
- 对比上一次成功构建的配置差异:
- 为适配@apollo/client v3.3.0的格式要求,在resolve.extensions数组中新增
.mjs后缀。根据webpack官方提交记录,2018年8月起就已支持mjs格式的sourcemap,初步判断该变更和本次报错无关,配置差异如下:
- 为适配@apollo/client v3.3.0的格式要求,在resolve.extensions数组中新增
resolve: { - extensions: [".ts", ".tsx", ".js", ".jsx"], + // .mjs needed from @apollo/client v3.3.0 + extensions: [".mjs", ".ts", ".tsx", ".js", ".jsx"], },
- 另一项变更是为了规避canvas测试依赖在Node15环境下的pangocairo安装报错,将构建基础镜像从Node15调整为Node14
- 临时验证结果:临时关闭生产构建的source-map功能后,构建可正常通过,但该操作会大幅提升生产环境问题的定位难度,不属于可长期落地的方案
核心诉求
- 梳理适配Cloudbuild环境的无侵入调试思路,定位报错根因
- 拿到可落地的修复方案,保障生产构建的source-map功能正常开启
内容的提问来源于stack exchange,提问作者Adrien Lemaire
相关产品推荐
相关产品推荐

