工具大全
AI 教程作者:Coocon2026年3月4日175 次阅读

How to Use Gemini for Code Debugging (2025)

How to Use Gemini for Code Debugging (2025)

Debugging is the worst part of writing code. You stare at the same error for an hour, and it turns out to be a missing semicolon. Gemini can cut that time down significantly — here's how to actually use it.


Scenario 1: You Got an Error and Have No Idea What It Means

Paste the full error into Gemini. Don't summarize it. Copy the whole thing.

Say you're working in Python and you see this:

Traceback (most recent call last):
  File "app.py", line 14, in <module>
    result = process(data)
  File "app.py", line 8, in process
    return data["user"]["name"]
KeyError: 'user'

Just paste that into Gemini and say:

"I got this error in Python. What does it mean and how do I fix it?"

Gemini will tell you that data doesn't have a key called "user" — either it's None, the key name is wrong, or the API response changed. It'll also suggest using .get() with a fallback or adding a check before accessing the key.

This works for JavaScript, TypeScript, Go, Rust — any language.


Scenario 2: Your Code Runs But the Output Is Wrong

No error, just wrong behavior. Give Gemini the expected output and the actual output — that context is everything.

function applyDiscount(price, discountPercent) {
  return price - discountPercent / 100;
}

console.log(applyDiscount(100, 20)); // Expected: 80, Got: 99.8

Ask:

"This function should apply a percentage discount. Input is 100 and 20%, but I'm getting 99.8 instead of 80. What's wrong?"

Gemini catches it immediately: you're dividing discountPercent by 100 first (getting 0.2), then subtracting from price. The fix is return price - (price * discountPercent / 100).


Scenario 3: You Have Two Versions of Code and Don't Know What Changed

Paste both into Gemini:

"Here's version A and version B. What are the differences? Could any of those differences cause a bug?"

Version A:

def get_user(user_id):
    user = db.query(User).filter(User.id == user_id).first()
    return user.to_dict()

Version B:

def get_user(user_id):
    user = db.query(User).filter(User.id == str(user_id)).first()
    if user:
        return user.to_dict()
    return None

Gemini will spot the str() cast and null check, and tell you which change is more likely to introduce a regression.

For visual diffs, use MagicTools Diff Checker — paste both versions and see differences highlighted side by side before asking Gemini.


Scenario 4: Let Gemini Write Test Cases to Expose the Bug

Give Gemini your function and say: "Write unit tests. Try edge cases that might expose bugs."

function slugify(text) {
  return text.toLowerCase().replace(/ /g, '-');
}

Gemini will write tests revealing the function doesn't handle punctuation or multiple spaces — exposing multiple bugs at once.


Prompt Templates

Explain an error:

I got this error in [language]:
[paste full error]
What does this mean and what's the most likely fix?

Logic bug:

This function should [describe goal].
Input: [input]
Expected output: [expected]
Actual output: [actual]
Code: [paste code]
What's wrong with the logic?

Write tests to find bugs:

Write unit tests for this function. Focus on edge cases that might expose bugs.
[paste code]

FAQ

Is Gemini accurate when debugging code?

Most of the time, yes — especially for common errors. It's best at explaining error messages, spotting logic mistakes in small functions, and suggesting edge cases. Treat it like a smart colleague, not an oracle.

What if Gemini gives me a fix that doesn't work?

Tell it. Say "that fix didn't work, here's what happened" and paste the new error. Debugging with Gemini is a conversation, not a one-shot query.

Should I paste my entire codebase?

No. Paste only the relevant function or file. Too much context makes the response vague. If the bug spans multiple files, explain the connection in plain text and paste only the key parts.

Does Gemini work better with some languages than others?

It's strongest with Python, JavaScript, TypeScript, and Go. It still handles Rust, C++, and others reasonably well, but for niche languages you may need to provide more context.

Can I use Gemini in my editor?

Yes. Gemini Code Assist is available in VS Code and JetBrains IDEs. You can highlight code and ask questions directly in the sidebar — saves a lot of copy-paste time.


Before asking Gemini about a diff, use MagicTools Diff Checker to visualize the two versions first. It highlights additions, deletions, and changes line by line — making it easier to write a focused question.

相关文章

上周教你手写 git worktree,这周发现有人做成了一条命令:worktrunk 实测

上一篇讲用 git worktree 替代 stash,留了个尾巴:node_modules 得重装一遍。worktrunk 是一个 7.4k 星的 Rust 命令行工具,把 worktree 的建、切、列、删、合做成了 wt 一条命令,还顺手把 node_modules 的问题用 APFS 克隆解决了:56,233 个文件 1.7 GiB,3 秒拷完,磁盘一字节不多占。本文在一个真实仓库里跑了一遍,讲清哪几步真被省掉、哪个默认行为会把 .env 一起拷走、哪些功能我不会开。

工作流ai-agent+5
developer2026年9月13日6 min
7

码农早餐 · 2026-09-13

今日头菜:换了台 Mac,你的登录钥匙串打不开了:Tahoe 把密钥锁进了 Secure Enclave。另有 7 条快讯:7.2k 星、256 个 fork:worktrunk 把 git worktree 做成了一条命令;220 美元买来的 56 次安装:33 次是机器人;等。

daily-intel2026年9月13日17 min
33

npm install 会改你的 package-lock.json:CI 必须用 npm ci 的 3 个理由

npm install 和 npm ci 的区别,一半人会答成「后者快一点」。真正的区别是:npm install 有权改写 package-lock.json,npm ci 没有。本文用三个真实场景讲清这个差别为什么在 CI 里是致命的——lock 文件被镜像源来回翻转、macOS 生成的 lock 在 Linux 上缺二进制依赖、package.json 手改后 lock 静默失步——以及 yarn、pnpm 对应的命令是什么。

cicdnodejs+5
developer2026年9月12日4 min
26

改到一半要修线上 bug?别 git stash,用 git worktree 再开一个目录

写到一半被叫去修线上 bug,大多数人的肌肉记忆是 git stash → 切分支 → 修完 → 切回来 → stash pop。这条路每一步都有坑:stash 不带未跟踪文件、pop 会冲突、忘掉的 stash 堆成坟场。git worktree 让同一个仓库同时有两个目录、两个分支,修 bug 在另一个目录里做,主目录一个字节都不动。本文讲清 worktree 的原理、四条常用命令、和 stash 的适用边界,以及 node_modules 怎么办。

工作流git+4
developer2026年9月12日5 min
18