【Copilot极限实践日记】DAY 2: 使用Copilot/ChatGPT解决项目编译问题

这个系列之所以叫极限实践是因为我也不知道这是不是最佳实践。我现在尝试把ChatGPT用到极致,即几乎所有的具体编码或者写作都交由ChatGPT完成,看可以提升多少效率,或者降低效率。也看看能不能研究出更好的使用Copilot的方法。

名词介绍

平台和模型

讨论AI就必须加上平台和模型,否则就不客观。所以以下讨论都会标注平台和模型

Copilot

Copilot 就是微软出的使用ChatGPT的聊天机器人,你可以把它看作是ChatGPT的一种封装形式。你可以切换模型,目前可选的有Gemini,Claude和ChatGPT

Github Copilot

Github Copilot 是一款AI 编码助手。它把Copilot整合到了Github里面

我使用的模型是 GPT-4o

Github Copilot 的 VSCode 插件

Github开发了Github的Copilot插件。该插件通过跟Github Copilot联网实现在IDE内使用Copilot帮助程序员编码。目前支持VSCode和Intellij。

DAY 2 背景

原本今天的计划是使用Copilot尝试生成unit tests。但是由于我的node.js项目的依赖更新了,所以跟eslint有关的代码报错了。所以我索性把这个node.js项目的eslint从版本4升级成9,由于4采用的配置文件.eslintrc.js已经废弃了,我换成了新的配置文件 eslint.config.mjs。之后构建就一直出错。所以我就用Copilot和ChatGPT来帮我解决问题。

这是jenkins构建报的错

sh 复制代码
(node:163) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/home/jenkins/agent/workspace/xxxxxx/src/build/build.js:1
import copyfiles from 'copyfiles';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

我看到这个错误叫我在package.json里面 set "type": "module" ,我就照做了,然后项目就启动不起来了。我就知道我得问ChatGPT或者Copilot了。

步骤

ChatGPT(4o)

我首先想到的是ChatGPT因为jenkins是在项目以外的。我不知道Copilot能不能解决超过项目代码以外的问题。

Step 1 直接询问

一开始先用最懒的问法,直接把错误一贴就开问

复制代码
jenkins构建报错:

(node:163) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use node --trace-warnings ... to show where the warning was created)
/home/jenkins/agent/workspace/xxxxxx/src/build/build.js:1
import copyfiles from 'copyfiles';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49

这是什么问题,要怎么解决

ChatGPT 回答

这个错误的核心问题是: 🔴 Jenkins 运行 build.js 时,Node.js 解析 import

语法失败,因为它仍然认为你的项目是 CommonJS,但 build.js 里用了 import 语法。

CommonJS,但 build.js 里用了 import 语法。 🔍 为什么会发生?

你的 build.js 代码里使用了: import copyfiles from 'copyfiles';

Node.js 默认使用 CommonJS,不支持 import,除非:

你的 package.json 里有 "type": "module"

文件扩展名是 .mjs(比如 build.mjs)

用 node --experimental-modules(旧方案,不推荐)

Jenkins 运行时,node 解析 build.js 时发现 import,但环境是 CommonJS,所以报错。

✅ 解决方案

你有几种方法可以修复这个问题,取决于你是否要保留 import 语法。 方案 1️⃣:改回 require()(最简单)

如果你不打算切换到 ESM,把 import 改成 CommonJS 的 require(): const copyfiles = require("copyfiles"); \ // 你的代码... 👉 适用于:你的项目主要还是 CommonJS(你之前也是用 require() 的)。 方案 2️⃣:让

build.js 运行在 ESM(ES 模块)

如果你希望继续使用 import 语法,需要让 Node.js 识别它: 方法 2.1:在 package.json 里加 "type":

"module" { "type": "module" }

📌 注意:这样会让整个项目变成 ESM,所有 require() 代码都要改成 import,否则可能引发其他报错。 方法 2.2:把

build.js 改成 .mjs

把文件改名: mv src/build/build.js src/build/build.mjs 然后 Jenkins

里调用 node src/build/build.mjs,这样 Node.js 会自动解析它为 ESM。

后面还有很长,因为太长了所以我就不贴到文章中了。而且整体格式很漂亮。给你看下它回答的格式

一次就得到了我想要的结果。我采用了方案1,然后问题就解决了。

Copilot(GPT-4o)

接下来尝试使用了GPT-4o模型的Copilot。照说模型相同回答应该不会差太多。

Step 1 直接询问

这次我尝试对Copilot问同样的问题

复制代码
jenkins build error

(node:163) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use node --trace-warnings ... to show where the warning was created) /home/jenkins/agent/workspace/xxxxxx/src/build/build.js:1 import copyfiles from 'copyfiles'; ^^^^^^

SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:77:18) at wrapSafe (node:internal/modules/cjs/loader:1288:20) at Module._compile (node:internal/modules/cjs/loader:1340:27) at Module._extensions..js (node:internal/modules/cjs/loader:1435:10) at Module.load (node:internal/modules/cjs/loader:1207:32) at Module._load (node:internal/modules/cjs/loader:1023:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12) at node:internal/main/run_main_module:28:49

how to fix it

Copilot 回答

它给的建议虽然也可以解决问题,但是我如果真的照它给的建议来改,会改变文件或者项目的属性,我可能会遇到更多的问题。这个回答有两个问题:

  • 它并没有给出所有回答
  • 它不会考虑解决方案的成本和得失

Step 3 增加询问细节

我对以上回答不满意,于是我追问,如果我不想按以上的所有方案,看它能否提供给我一个chatGPT给的方案

复制代码
If you prefer not to make major changes, you can use the CommonJS require syntax instead of the ES module import syntax. This way, you don't need to change the file extensions or update the package.json.

Copilot 回答

If you prefer not to make major changes, you can use the CommonJS

require syntax instead of the ES module import syntax. This way, you

don't need to change the file extensions or update the package.json.

build.js Update the import statement to use require instead of import.

build.js+5-4 By using require instead of import, you can avoid the

need to set "type": "module" in your package.json or rename your files

to .mjs. This should resolve the issue with minimal changes to your

codebase.

这回它给出了正确的回答。实际上我很惊讶Copilot的第一个回答跟直接问ChatGPT差别很大。相同的模型居然有这么大的差别,导致我怀疑我用的Copilot使用的4o模型不是一个完整版。

结论

  • 整体来说Copilot这次还是输给了ChatGPT,还是失败了。因为问Copilot不如问ChatGPT。
  • 在项目整体问题的解决上ChatGPT比Copilot强
  • Copilot不会衡量成本和收益
  • 你需要多追问Copilot来获得预期的答案

明天我再次尝试使用Copilot生成unit tests.

相关推荐
坤坤爱学习2.02 天前
求医十年,病因不明,ChatGPT:你看起来有基因突变
人工智能·ai·chatgpt·程序员·大模型·ai编程·大模型学
Leinwin2 天前
微软开源GitHub Copilot Chat,AI编程领域迎新突破
microsoft·github·copilot
Ven%2 天前
破译AI黑箱:如何用20行Python理解ChatGPT?
人工智能·python·chatgpt
我不是哆啦A梦2 天前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt
陈敬雷-充电了么-CEO兼CTO2 天前
大模型技术原理 - 基于Transformer的预训练语言模型
人工智能·深度学习·语言模型·自然语言处理·chatgpt·aigc·transformer
小溪彼岸2 天前
GPT-4o的修图能力强的可怕?
chatgpt·aigc
youngqqcn2 天前
VSCode-Copilot的系统提示词
ide·vscode·copilot
asyxchenchong8882 天前
ChatGPT、DeepSeek等大语言模型助力高效办公、论文与项目撰写、数据分析、机器学习与深度学习建模
机器学习·语言模型·chatgpt
lyh13443 天前
VS Code 的 Copilot Chat 扩展程序
copilot
赛丽曼3 天前
Assistant API的原理及应用
人工智能·chatgpt